Jens Krämer

Streaming with Ruby

 |  streaming, http, vlc, ruby

Here’s a small script that distributes a live HTTP video stream to a number of clients. I use it to save upstream bandwidth when connecting multiple clients inside the LAN to a live stream outside the LAN.

The script acts like a proxy. It opens one HTTP connection to the remote server to get the stream, and listens for requests of local clients on a given port. Received data then will be delivered to all connected clients.

The great thing is that I didn’t expect to get this done with Ruby in the first place. I already had started looking into Java’s nio packages, when I found this post on ruby-talk.

Armed with the knowledge of how to use the ominous select call, putting things together was really easy.

Soccer time :-)

Use VLC to grab the image from a video4linux capable device, e.g. a TV card inside a box at home:

vlc -v v4l:/dev/video0:adev=/dev/dsp:audio=1:norm=pal:size=352x240:samplerate=44100 \
--sout '#transcode{vcodec=mp4v,vb=600,acodec=mpga,ab=64,deinterlace}:std{access=http,mux=ts,url=:443}'

The stream now will be accessible via HTTP on port 443. To make it accessible for multiple clients, you’ll need a proxy (like the one I hacked together) since the average DSL connection can only serve the stream once due to the poor upstream.

You might have to change the audio/video devices and the bitrates (vb and ab parameters) to match your setup and connection quality.

Above settings give a quite ok picture and sound quality and the resulting stream fits into 1MBit DSL upstream without problems.

Disclaimers:

  • You should restrict access to your home box that serves the stream to the IP of the machine you intend to connect from - you probably don’t want anybody else to take away your bandwidth ;-)
  • Better ask your boss before distributing soccer matches through his LAN…