Monday, February 23, 2009

remote Tv

So about a year ago my now-roommate bequeathed me with a PCI-WinTv decoder/PVR/tv card. Always excited to try legacy hardware in my ubuntu box I popped it in, and got MythTv up and running. But much to my dismay MythTv ran like crap on my slightly older, surplussed hardware. Audio was not decoding at the correct sample rate, the display was lagged and pixilated, and MythTv was hogging all of my precious resources (is a mysql DB really necessary for watching TV?). At the time, recording Tv shows was lower on the priority totem than having clear crisp Tv on my Desktop.

TvTime was next and did what it does very well. Clear audio, crisp full screen Tv, no lag. But how quickly our satisfaction fades. One thing about the winTv cards is that the defacto hardware install calls for a patch cable to be run from the line out on the card to a line in on the motherBord/sound card in order to get analog sound. And this is Fine untill I move my computer, or the cat walks behind it or some other such thing that causes the cable to become unplugged. Plus it just seems so...extraneous.

Of course all this begged the question, why not use my favorite open source, seemingly trans-platform, streaming media player, VLC? why not indeed. First Run didn't produce much. In Linux world we get a V4L tab ( in windows its DirectShow ). pointing it at my video device (/dev/video0), only produced a screen full of static and no sound at all. Ultimately though, spurred on with much success streaming from webcams and the like, I knew I really wouldn't be happy until I could watch and stream TV with vlc. Last weekend, however, my patience and perseverance paid off and Now I am streaming TV from home to work as I write this very satisfied with my technological prowess.

and of course here is a road map to help other perspective tv streamers and sopCasters.


1. what driver do you need? where do you get it?

This part, mythTv took care of for me, but I know there are a variety of options here in terms of open source drivers. Mine ended up being Cx88xx (or some such thing) more information check on the v4l home page (http://www.linuxtv.org/)
Chances are, if you're using something like a Debian/ubuntu these are already included in your kernel so not much to worry about.

2. what's what in /dev

after I installed the card and mythTv did it's thing I noticed some new listings under the /dev directory. radio0, video0, vbi0, audio2. The trick is figuring out what's what. This step took me a while and turned out to be a huge pain in the ass as on the surface linux isn't very forthcoming about details (at least if you don't know how to ask). On top of that I also have two webcams plugged in to the machine in question. On reboot my kernel would sometimes switch around all the asignments of the name space (IE video0, a webcam might be come video1).

I don't reboot my machine that often, but when I do, I like it when my scripts dont get confused about what piece of hardware they should be talking to. Enter the magic of UDEV rules. Only really ever having used the ubuntu distro I can't speak to weather udev is common among other *nixes or not, but I'm glad that they are in Ubuntu. for those who don't know, they allow for a rule sheet with mappings to a consistent file system name space.

For example,
from (/etc/udev/rules.d/95-perso.rules)

KERNEL=="video[0-9]", SYSFS{name}=="cx88*video*", SYMLINK+="tv"

will tell the kernel at start up, after its done its initial hardware mapping to make an additional symlink to whatever device that comes up with "cx88" and "video" to /dev/tv. Of course thats only one way in an entire search syntax to have it map your devices for you (see the ubuntu forums and http://reactivated.net/writing_udev_rules.html for more on this).
so after a quick script to run through all the items in /dev and list only the ones that had something to do with the cx88 dirver...


for i in /dev/* ; do udevinfo -q path -n $i ; done 2>&1 | grep -v node | while read a ; do udevinfo -a -p "$a" | if grep -i some part of the driver name that you are searching for (like "cx88") ; then echo $a ; fi ; done

running that from a command line or script should list all the devices that use the search term in question.

3. VLC

with the freshly created /dev/tv and /dev/tvAudio we can now move on to working with VLC. Knowing that I would want to be able to remotely stop and start the stream with vlc I whipped up this little bash script called remotetv.

#!/bin/bash

if [[ $1 == '' ]] ; then
iam=$(ps ax | grep vlc | grep '/dev/tv');
iam="${iam# }";
iam="${iam%% *}";
if [[ $iam == '' ]] ; then
exit 0;
else
echo "$iam";
fi;
elif [[ $1 == 'start' ]] ; then


vlc -vvv -I dummy --ttl 12 v4l:/dev/tv:size=320x240:samplerate=48000:adev=/dev/tvAudio --sout '#transcode{vcodec=DIV3,vb=512,scale=1,acodec=mp3,ab=64,channels=1}:duplicate{dst=std{access=http,mux=ogg,dst=0.0.0.0:1235}}' 2>/dev/null 1>/dev/null &



elif [[ $1 == 'stop' ]] ; then
killMe=$(ps ax | grep vlc | grep '/dev/tv') ;
killMe="${killMe# }";
killMe="${killMe%% *}";
# echo "$killMe";
kill -9 "$killMe";
fi


put it in /usr/local/bin, chmod ug+x, and bam! start/stop vlc with remotetv stop or remotetv start. check the process-ID by simply calling remotetv and if its running it will tell you the PID
some things to note...

v4l:/dev/tv:size=320x240:samplerate=48000:adev=/dev/tvAudio

/dev/tv & /dev/tvAudio are the two devices that we made earlier with the UDEV rules.

:samplerate=48000

this one took me a loooooong time to figure out. my card's default sample rate is 48000. VLC calls v4l with a default sample rate of 42000 but this causes the audio to sound like its being squeezed through a tin horn. setting it corectly should take care of that.

for the rest of the parameters dealing with streaming (codecs, containers, bit-rates and stream destinations) and on the fly transcoding check out the videolan wiki (http://wiki.videolan.org)


once remotetv is running, you should be able to connect to the stream from multiple machines simultaneously with

vlc http://your.ip.address:1235


4. Changing Channels?

so there may be a way to pass the frequency to VLC directly, but from what I understand it will only take the frequency not the channel, so you need to have a mapping of frequency to channel. Fortunately we don't need VLC to change channels for us we can use the tools that came with ivtv (which were probably installed when we installed and ran tvtime or mythtv, if not a simple apt-get install ivtv should do it)

from the command line something like this will change to channel XX

me@ubunbox:~$ ivtv-tune -d /dev/tv -c XX

**a quick note: it is possible to change channels with the v4lctl command, something like v4lctl -c /dev/tv setchannel XX is supposed to work, but on my box with my card, it seems to cuase a bit of unpleasant panic**

and for some, that might be everything you need to enjoy some streaming tv and remotely change channels, but for me I wanted a more friendly interface, maybe even something I could use from my phone.

so after a little more coding I wrote up a page in php with so called smart phones in mind (but also works in the usual browsers, FF IE safari. not yet tested in chrome).

I've put the script and the php page I wrote up on source forge please feel free to check it out and check out the additional documentation I included (though really there is a lot in this post that isn't covered there).

check out
https://sourceforge.net/projects/remotetv/