12 Mar 2007

controlling tv-playing linux machine using apple remote


i whipped a little home mashup of a tv tuner, mplayer on linux and the apple remote.

background

patricia has a samsung laptop which i have hijacked and replaced windows with linux. right now it is in our living room where it is connected to a USB1 tv tuner i bought a couple years back (i never got it to work on my powerbook.) so with the magic of the usbvision linux module and mplayer, it acts as a tv.

the only problem is that we have to get up off our lazy asses to change the channels, and that is no fun. given that we both have macs with the apple remote, i decided to figure out a way to control it using the apple remote.

by combining a udp server, running mplayer in slave mode and with some public domain code for listening to apple remote events on the mac, i now have a mplayer powered apple remote controller tv player on a laptop.

mplayer controls over udp

i'm sure there are other network control protocols for mplayer, but i didn't look very hard. i initially wrote a 20 line python udp server that pipes the output into mplayer's stdin. you can switch mplayer into slave mode so that you can type commands on the console to control it.

but after thinking about it a little, my 20 line udp server was really just:

netcat -l -u -p 8070 | mplayer -slave -quiet -flip -fs tv://bbc1 ...
i also configured gdm to auto login (after a 10s timeout) to the tv user and the xinitrc just starts this application. so when mplayer starts up, it will open in slave mode which means it accepts commands from stdin.

apple remote control

the apple remote control uses infrared. i couldn't really find much about getting it working under linux, but then again, i didn't have many infrared receivers on the hardware side of things. so instead, i just took some sample code from here on controlling the apple remote and hacked it to send a UDP packet on each keypress i was interested in containing a mplayer control string, eg:

tv_step_channel -1
so all it was doing was blasting this mplayer slave command through my home network and mplayer on my linux machine was picking it up and acting on it.

conclusion

although it would be more interesting to hack apple remote support in linux, this is the second best thing that can be done easier if you happen to have a macbook pro connected to the same network as your linux tv viewing machine.

for kicks and giggles, where is the python code that i wrote before i remembered that all that could boil down to a netcat command, and also the patch against the AppleRemote example code that controls mplayer over the network.

downloads

tv_serv.py
apple_remote-mplayer_network_slave_control.diff


You can reply to me about this on Twitter: