Throttling Bandwidth in Mac OS X

November 16th, 2010

I recently had the need to simulate the speed of a home broadband connection (10Mb/s down, 64Kb/s up) in an environment with 100Mb/s down and 60Mb/s up. With Mac OS X this is quite easy to do.

First of all, if you’re throttling HTTP traffic only, it’s probably easier to use a debugging proxy like Charles to do so. Otherwise, Mac OS X includes a tool called ipfw (ipfirewall) built in which is able to throttle traffic system-wide.

throttle.sh

#!/bin/sh
 
LIMIT_DOWN="10Mbits/s"
LIMIT_UP="64Kbytes/s"
 
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root." 1>&2
   exit 1
fi
 
ipfw pipe 1 config bw $LIMIT_DOWN
ipfw pipe 2 config bw $LIMIT_UP
ipfw add 1 pipe 1 tcp from any to me
ipfw add 2 pipe 2 tcp from me to any

The script above will throttle the connection to 10Mb/s down and 64Kb/s up. This script must be run as root via the sudo command:

chris@macbookpro:~/bin$ sudo ./throttle.sh

To undo the throttling, use the following script.

unthrottle.sh

#!/bin/sh
 
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root." 1>&2
   exit 1
fi
 
ipfw delete 1
ipfw delete 2

Again, run the script as root.

chris@macbookpro:~/bin$ sudo ./unthrottle.sh

Chris vs. Synergy

November 14th, 2010

My HTPC setup was less than ideal, seeing how it was basically a mini tower stuffed in a desk by the TV, so I decided to move the system into my wall unit proper. To do this, the first thing I needed a smaller case for the PC. After a lot of research, I decided on the Antec Fusion Black.

It’s a really nice case. It runs quiet and cool and it looks like an everyday receiver, not a PC, which is exactly what I wanted. One thing though, the front LCD display is horrendous. I recommend just leaving it turned off. It’s very washed out, has slow refresh rate, and IMO there’s no need to look that the front to the PC to see the name of the movie you’re playing.

My mouse and keyboard are a Logitech G7 and an Apple Wireless Keyboard. I installed the USB dongles for RF and Bluetooth inside the case thanks to a couple of USB 4-pin to female adapters.

After setting up the keyboard and mouse on the coffee table, I realized why I rarely use the HTPC — it’s just so counter intuitive to a have a KB/M out all the time, especially when my MacBook Pro is always out anyway. Waitaminute!

Enter Synergy. What is Synergy? It’s is a really neat application that allows you to share a keyboard and mouse across multiple devices. It’s sort of the same concept of a KV/M but turned upside-down, not to mention not as clumsy or expensive, not to mention to toggle switch! With Synergy you install the software, do a simple *cough*BS*cough* config, and you’re sharing your keyboard and mouse between systems.

Ok, setup was not so easy. On client side, the PC, I’m running Windows 7 Ultimate x64. I installed the latest stable release of Synergy thinking that was my best bet, but I was wrong. The problem is Synergy needs to run as administrator or when an UAC prompt pops out you lose control of the keyboard and mouse.

The solution was to install the latest beta (I grabbed 1.4.1) for Windows and have it run as a service. This way Synergy has the privileges to interact with UAC prompts and you don’t need to keep a keyboard under the couch for UAC prompts alone.

The server side, the Mac, was a lot harder to configure. The server runs as a daemon, meaning there’s no UI, so config is an under-the-hood experience. There is a product called SynergyKM which has a UI configuration, but based on my evaluation it would not fit my needs. Time to roll up my sleeves.

I used MacPorts to install the Synergy binaries:

chris@macbookpro:~$ sudo port install synergy

If you don’t have MacPorts you can install Synergy by by downloading it from the web site. The current MacPorts version is a few point releases behind, but that’s not a problem for me.

My config is fairly straight-forward with a couple of exceptions. I treat the PC as being to the right of the MacBook. That means when I move my mouse to the right of the screen it appears on the PC. I wanted control-click to act like a right mouse button click as it does on the Mac and Command to act as Control on the PC. That way I don’t have to Command-C to copy on the Mac and Control-V to paste on the PC, I can just use Command for everything. Finally, I wanted Option-Command-Plus and Option-Command-Minus to zoom the screen via Windows Magnifier (it’s hard to see tiny text from six feet away).

Here’s my config file, which I store in ~/.synergy/synergy.conf

 section: screens
    macbookpro:
    monster:
        super = alt
        alt = ctrl
        meta = super
 end
 section: aliases
    macbookpro: 
        macbookpro.local
 end
 section: links
    macbookpro:
        right = monster
    monster:
        left = macbookpro.local
 end
 section: options
    mousebutton(control+1) = mousebutton(3)
    keystroke(super+alt+Plus) = keystroke(meta+Plus)
    keystroke(super+alt+Minus) = keystroke(meta+Minus)
 end

Section by section. “screens” defines the system. “monster” is my PC and obviously “macbookpro” is the Mac.

The block…

    super = alt
    alt = ctrl
    meta = super

…is where I map the keys for Command and Option. The names are misleading in Synergy. On the Mac side, “super” is the Option key, “alt” is the Command key and “ctrl” is control. There is no “meta” key. Why that’s in the config will be explained later.

“aliases” simply maps the OS X name for the host to a more friendly format:

     section: aliases
        macbookpro: 
            macbookpro.local
     end

“links” is a straight-forward section:

    macbookpro:
        right = monster
    monster:
        left = macbookpro

Here the Mac is treated as the left screen, PC is on the right.

Finally, “options”:

     section: options
        mousebutton(control+1) = mousebutton(3)
        keystroke(super+alt+Plus) = keystroke(meta+Plus)
        keystroke(super+alt+Minus) = keystroke(meta+Minus)
     end

The first directive maps Control + Click to the right mouse button on the PC. The next two define Option + Command + (Plus | Minus) to the zoom commands on Windows 7 (Windows Key + [Plus | Minus]). If you remember from before “meta” on the Mac maps to “Super” on the PC, which is the Windows key, so we define a faux mapping for “meta” to do this. Yes, it is as confusing as it reads.

Now if you run the synergy server from the terminal, you can test it out:

chris@macbookpro:~$ synergys -f --config ~/.synergy/synergy.conf

If that command does not bomb out you’re in good shape, so far.

Now to get Synergy to run when you log in is a challenge. This is accomplished through launchd.

I created the following file as ~/Library/LaunchAgents/net.sourceforge.synergy2.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" 
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>net.sourceforge.synergy2</string>
        <key>ProgramArguments</key>
        <array>
            <string>/Users/chris/bin/synergy.sh</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
    </dict>
</plist>

This will launch a shell script in my local bin directory at login. For me the script is ~/bin/synergy.sh. The contents are:

#! /bin/sh
 
SYNERGYS=/opt/local/bin/synergys
CONFIG=/Users/chris/.synergy/synergy.conf
 
if ! ps axco command | grep -q "^synergys\$"
then    
    $SYNERGYS --daemon --config $CONFIG
 
    if [ $? -ne 0 ]
    then
        echo "synergys failed to load."
    fi
 
else
    echo "synergys already loaded."
fi

This script looks to see if the Synergy server is running, if not it will launch it. Pretty simple.

That’s about it. If anything fails be sure to check Console (Command-Space and type “Console”).

One very cool thing is the multi-touch commands are at a very low level of the OS, so two-finger scroll etc. will all work on the Windows side just as it does on the Mac.

Home Theatre

February 16th, 2008

home-theatre.png

This project is complete for now. I’m waiting a couple of years to see how OLED or SED pans out before replacing the display, but the major additions are done.

TV: Samsung LN-T4665F 46″ LCD
Receiver: Onkyo TX-SR605 7.1 receiver
Speakers:
Paradigm CC-70 Center, 4x Paradigm Atom
DVR:
Scientific Atlanta 8300HD (running Passport)
Console/Blu-ray: Playstation 3 (60GB)
Console: Xbox 360 Elite (Falcon)
PC: Home-built Asus P5E-VM HDMI E6400 @ 3.2Ghz, eVGA 7950 GT KO
Music: Apple Airport Express
Router: Linksys WRT54G running DD-WRT
Power: Panamax M4300-PM Line conditioner
Remote: Logitech Harmony 550 Advanced Universal Remote
Keyboard: Apple Wireless Keyboard (aluminum)
Mouse: Logitech G7 gaming mouse

An AppleTV may be in the future, but $230 for the privilege of renting movies is not worth it, especially since I have a PC directly plugged into the setup. I don’t see any of the digital download/streaming services working for layfolk without a standard set top box solution in the picture. Apple TV is a possible contender to be that device. Using Unbox, Netflix, Joost, etc. made no sense until I had the PC jacked right into my HT setup. I don’t see any layperson going that route though.

I know I was a big proponent of streaming, but all the hassle of TVsersity, Windows Media Center, etc. seems like such a pain-in-the-ass compared to playing content directly from the PC in Zoom Player. I realize this is not a viable option for everyone.

This has turned out to be a fun (albeit expensive) hobby. I can wait to get a house so I can go big time with it.

Optimizing TVersity for the PS3 (Part I)

August 4th, 2007

tversity-xmb.png

With the introduction of firmware 1.80, the Playstation 3 has become a full-fledged media center device, thanks mainly to DLNA (Digital Living Network Alliance) support. There has however been a lot of confusion on how to get the PS3 working with the various DLNA and UPnP (Universal Plug and Play) servers. There are quite a few servers available, including TVersity, TwonkyMedia, Nero MediaHome, and Windows Media Player 11. This guide will focus on TVersity, as it is currently the most popular and full-featured solution available.

The main benefit of TVersity is its ability to transcode media into a format the client device can process. Currently, the PS3 can only render MPEG and AVS (H.264) formats. This is a problem because the majority of content available on the Internet is in alternative and open formats such as Xvid, Divx, and x264. This guide will explain how to optimize TVersity to stream these formats at the best quality possible.

What you will find here is the result of scouring countless forums, trial and error, and good old fashion common sense. The goal here is not to take credit for discovering the optimal settings for streaming media with TVersity, but to get everything down one place, written in a clean, concise fashion.

Step 1: Clean Out Your Codecs

The number one reason people have problems with TVersity is that the codecs installed on their system are not in order. The majority of “Unsupported Format” and “Corrupt Data” errors are the result of missing or invalid codecs.

The first thing to do is to get rid of all the disparate codecs installed on your system. This includes stand-alone Xvid, etc. codecs as well as installs you may not suspect such as Nero Premium and tools like AVI Splitter.

A good test to see if your system is clean is to try and play a Xvid or x264 file from Media Player Classic or VLC and it not playing. If the video renders there’s a codec still installed on your PC and you need to track it down and uninstall it.

Step 2: Install CCCP (Combined Community Codec Pack)

cccp-white2.png

CCCP is an organized collection which contains all the codecs and tools you will ever need to decode the various media formats out there. It is recommended that you first run the CCCP Insurgent tool to verify there are no lingering codec packs on your system. After you ensure your system is clean, install CCCP and reboot. Do not skip the reboot step! It’s a pain but it’s important.

If for some reason you do not want to use CCCP, there’s the K-Lite Codec Pack. I found it does not play nearly as many files as CCCP, but it’s a good alternative. Do not install both! Another alternative is to install ffdshow by itself. You may not be able to play as many formats, but your system will not have a gazillion codecs installed (which some consider bloat).

Step 3: Install TVersity

TVersity Logo

The current version of TVersity is 0.9.11.4 (December 30, 2007). TVersity is very much a work in progress. It can crash or stop working at times but for that most part it’s the best tool out there for streaming media to the PS3.

TVersity consists of two components: The media server itself (which is invisible to the user) and the Flash front-end. The front-end does not need to be running for Tversity to operate. The media server runs as a system service. Unfortunately, due to a bug involving permissions in 0.9.10.7 the media server Windows service needs to be tweaked to ensure proper operation.

Go to the Windows Services applet (from the Run… menu type “services.msc”) and in the list find the “TVersityMediaServer” service. Double-click on it and go to the “Log On” tab and change the process to run under your Windows account as shown below:

Service Settings

If the service is not already started, start it. Also insure the Startup Type is set to “Automatic”.

At this point TVersity should be operational. I am not going to go into how to add your media to the server as that’s beyond the scope of this guide and it should be fairly straight forward.

Step 4: Optimize the Transcoder

The goal here is to optimize the transcoder to output the best possible video quality possible. Keep in mind that this involves a great deal of horsepower and network bandwidth. I am transcoding on a 3.2Ghz Core 2 Duo processor over a 802.11g wireless network (with an excellent connection) and have yet to hit my head on the ceiling with these settings. Your mileage may vary and if it does you will need to scale back where appropriate, especially if dealing with HD content.

Start up the TVersity front-end and navigate to the Settings->Transcoder tab.

When To Transcode?

Transcoder settings

This should default to “Only when needed” so keep it there. This will allow TVersity to pass-through MPEG and AVS formats without transcoding overhead and image degradation.

Maximum Video and Image Resolution

Resolution settings

This determines how the transcoder will scale (down) your media in order to conserve network bandwidth. We want the best image possible so set both of these fields to the maximum resolution of your television. I have a 1080p native set so I set it to 1920×1080. If you’re at 720p set it to 1280×720. The “Image resolution” boxes pertain to photos, it does not hurt to crank them all the way up as well.

Windows Media Encoder

TVersity uses DirectShow under the hood to do the actual media transcoding. CCCP installs a DirectShow encoding/decoding filter called ffdshow which does all the magic behind the scene. Further versions of this guide will go into optimizing ffdshow for better video quality, but let’s get everything working first.

Encoder settings

Make sure the “Use DirectShow…” checkbox is checked and that the Windows Media Video version is set to “9″. You can choose an older version of Windows Media for faster decoding but 9 produces the best image quality (at least on paper).

Optimization

Optimization settings

This is a no-brainer. Tag it for quality.

Connection Speed and Quality

Connection settings

Here’s where things can get sticky. I’m on a 802.11g connection in a small apartment and have no problem settings the connection type to “Wired” and the signal strength to “Excellent”. If you notice network stuttering or dropouts definitely scale these settings back.

In my experience the PS3 does a thorough job in buffering content. As long as your PC can encode at a pretty decent rate (2x or greater) the connection settings don’t mean much as the PS3 will buffer way ahead of what is being played, assuming your network can keep up.

Compression

Compression settings

By transcoding we’re essentially re-compressing and already compressed file. This equates to a degradation of image quality. Set compression to “Minimum”. This is going to result in a larger file being sent over the network, but it results in better image quality at playback.

Decoding Speed

Decoder settings

Finally, ensure the “Decode the media as fast as possible…” box is checked. The PS3 times out pretty quickly if the media does not load fast enough and this setting helps with that.

Step 5: Optional Tweak

Output to MPEG2

In the TVersity install folder (C:\Program Files\TVersity\Media Server) find and edit the file “profiles.xml”. Ensure you are in the “Sony Playstation 3″ profile block, there should be a block of code that looks like:

<!-- When transcoding is needed to which format should we transcode -->
<transcodeTarget
audio="audio/x-wav"
video="video/mpeg16"
photo="image/jpeg"
onlineAudio="audio/mpeg"
onlineVideo="video/mpeg16"
onlinePhoto="image/jpeg"
adjustReadStartPos="false"
audioFailFutureSeek="false"
videoFailFutureSeek="true" />

Change it to read:

<!-- When transcoding is needed to which format should we transcode -->
<transcodeTarget
audio="audio/x-wav"
video="video/mpeg2"
photo="image/jpeg"
onlineAudio="audio/mpeg"
onlineVideo="video/mpeg2"
onlinePhoto="image/jpeg"
adjustReadStartPos="false"
audioFailFutureSeek="false"
videoFailFutureSeek="true" />

This will ensure the transcoder produces MPEG2 video as opposed to MPEG1, which results in overall better looking video. You will need to restart the media sharing service from the TVersity front-end after making this particular change.

Conclusion

You should now be able to playback nearly any video file you throw at your PS3 in high-quality. Granted, I’ve come across one or two files that refused to play (mainly video podcasts or Flash video) but for the most part I’ve been enjoying high-quality steaming media using TVersity and the PS3.

If you encounter any problems be sure to check the TVersity Support Forums, the PS3 Forum in particular.

Part II of this guide will go into further quality optimizations including tweaking of ffdshow and profile.xml hacking.

Enjoy!

Ownage of the Decade

June 26th, 2007

With the iPhone launch and all, I find this to be appropriate:

The BrownFury writes

“At an invitation only event Apple has released their new MP3 player called the iPod. iPod is the size of a deck of cards. 2.4″ wide by 4″ tall by .78″ thick 6.5 ounces. 5 GB HDD, 10 hr battery life, charged via FireWire. Works as a firewire drive as well. Works in conjunctions with iTunes 2. Here are Live updates”.

No wireless. Less space than a nomad. Lame.

Link

How I Beat the Grey Bars

April 20th, 2007

Time Warner Cable provides a Scientific Atlanta 8300HD box with their hi-def package. It’s annoyed me for weeks that standard-def channels have neutral grey bars on the sides of the display with no way to change them to another color, like, say, black.

I came across a rather inelegant solution, but it works and it beats looking at the annoying grey bars.

1) Tell the cable box to stretch the 4:3 image to 16:9 (press the # key to select “Stretch picture” setting).

2) Tell your TV to display the image in 4:3 (thus de-stretching the image). This will squish the image back to 4:3 but this time with black bars. Some sets do this differently than others so check your manual if you have to. Lucky for me this setting sticks for 4:3 stretched, but does not attempt to squish legit 16:9 content so I don’t have to switch back and forth as I change between SD and HD.

I doesn’t seem visibly effect the picture, but at 46″, SD looks horrible pretty much no matter what.

I understand why Time Warner gimped the box by disabling the ability to set the color of the bars (other cable providers allow this functionality on the same box). The phosphors in plasma displays could retain the black side bars thus discoloring the display. The color neutral grey consists of equal bars of red, green, and blue at 50% brightness thus all phosphors get equal work out. At least that’s my theory, and it sounds better than, Time Warner are simply idiots. Sucks for those of us with LCDs who need not worry about burn in and image retention.

Two Things I Learned About Amazon

March 26th, 2007

Placing a large order at Amazon recently has taught me two things:

1) They do indeed have a customer service number. You won’t find it anywhere on the site, but you can dig it up.

The number is: (800) 201-7575.

2) If they lower the price on an item you purchased within the last 30-days, they will offer you a refund for the difference, no questions asked. Just fill out the Returns & Refunds Form explaining your situation. You should get a reply within 24-hours.

Two more reasons to keep shopping Amazon, IMO.

Greatest Slashdot Comment Ever

March 14th, 2007

Sums up the whole ball of wax, IMO.

If you weren’t aware of it before, you probably know it by now. Anything interesting or useful that rears its head on Slashdot will likely be ripped to shreds by what has quickly become the nets most vicious and petty peanut gallery.

Slashdottians know nothing, they accomplish nothing, and their opinions are worth nothing. They are uniformly bitter, small-minded geeks who overestimate their own importance and their own skillz. They are, for the most part, losers. Their biggest accomplishment is in insulting others’ spelling and grammar, attacking the GPL license despite their grade level understanding of it, and tricking people into clicking on goatse.cx links. They are know-it-all blowhards who use their computers primarily for Pornography and online gaming, at which they cheat regularly to offset their complete lack of motor skills.

Despite touting the wonderous greatness of linux and open source, they all use Windows and Internet Explorer. They like Macs because of OSX, but want it to run on X86 so they can steal a copy and give nothing back. They will eventually buy a Mac due to their inability to run Windows without crashing it constantly by their own stupidity, and become raving unbalanced lunatics who do more harm than good for the Mac community by claiming that the G4 is quadruple the speed of a dual 3Ghz Xeon box.

They lie about their own experience to make their case, and when you win an argument with them, they post anonymously in order to tell you they’ve had sex with your mother.

Don’t become a regular here, you will become retarded.

Signed,
Yoda the Retard

Link

Secure Remote Desktop Access Over SSH

July 9th, 2006

Remote Desktop is an excellent tool for accessing Windows machines across locations. While there have not been many security exploits involving RD, I do not feel comfortable leaving the service directly open to the internet. Also, many corporate internal firewalls restrict outbound traffic to a handful of ports, and in my experience port 3389 which RD runs on is often blocked.

I’ve come up with a simple method of accessing a Remote Desktop machine over SSH which buys A) Port 3389 is no longer open “to the wild” on the host machine B) If port 3389 is blocked outbound on the client’s network, Remote Desktop will still be accessible if the common port 22 (SSH) is available.

All that is needed for this method is an SSH server either running on the host machine or on its local network. If you’re using a Linksys router DD-WRT may be an option as it offers a full SSH server that runs right on your router. In this example I will be using DD-WRT, but any SSH server will work.

Step 1) Enable the remote access SSH service under Administration->Management in the DD-WRT configuration:

1-remote-access.png

Step 2) You must also enable the Secure Shell service under Administration->Services:

2-services.png

I strongly suggest disallowing password login and instead use the authorized keys method with a strong passphrase.

Step 3) At this point the host-end is set up. Ensure the router has access to the machine running Remote Desktop by pinging it from the router’s shell. In this case the host machine is at 192.16.1.100 on the internal LAN:

3-ping.png

You’ll need a SSH client on the client-side. For Windows PuTTY is the best game in town. The key here is to set up access to your host and tunnel a local port through SSH to the host’s Remote Desktop service. With PuTTY it’s just a couple of settings.

Step 4) Add the remote router or SSH server IP address to the Session settings:

4-setup-host.png

Step 5) Configure the tunnel under Connection->SSH->Tunnels:

5-setup-tunnel.png

What’s important here is to pick an open port on your local computer because we are going to point Remote Desktop at that port. Under Windows XP Pro port 3389 is already taken by the local Remote Desktop service, so in this instance the port we use is 3390.

The destination is the internal IP or hostname of the host as it is known to the machine running the SSH server (in this case 192.168.1.100). The destination port will is 3389 (the listening RD service on the remote host).

Step 6) Connect to the host with SSH and login. At this point if everything is working correctly you should have a Remote Desktop port live on the client PC on port 3390. That port is being tunneled securely over SSH to the SSH server and forwarded on to the host machine. Keep the PuTTY session open or you will shutdown the tunnel.

Step 7) Time to test things out. Start the Remote Desktop Connection client and point to localhost:3390.

6-remote-desktop.png

If all was configured correctly you should pop into a Remote Desktop session on the host computer. The speed is snappy enough for me on a 45KB/s connection with all the bells & whistles turned on, even with the additional encryption overhead.

Enjoy!

Phantom Drive

August 4th, 2005

Trying to help anyone Googling for this…

I noticed an extra CD/DVD ROM listed on my XP system. Device manager listed it as “AVX CD/DVD ROM” but there was no physical drive associated with it. Removing it from the device manager only restored it at startup.

Turns out it is an artifact of an Alcohol 120% install. The software does a lousy job cleaning up after an uninstall.

The cleanup is easy:

  1. In Device Manager->SCSI and RAID controllers, uninstall a device called “A347SCSI SCSI Controller,” the phantom CD/DVD will disappear.
  2. It will return again at startup if you don’t delete the drivers. Delete the files “a347scsi.sys” and “a347bus.sys” from c:\windows\system32\drivers.
  3. Reboot

Hope this helps someone, because it was pissing me off.

If you want virtual drives without paying for Alcohol 120% just download DAEMON Tools. It’s free and a much lighter install.