Monday 2 May 2016

Broadcasting Terrestrial TV across your LAN

First off you need a bit of background into how this all works, TV transmitters have transponders which multiplex their digital channels onto the transponder, they are then transmitted to you and I over the air.

So one frequency can contain many different channels, in the old days of analogue this meant that one frequency meant one channel, but digital TV has opened up the radio spectrum to many more channels by splitting channels into different streams and transmitting them on one frequency.

This makes it ideal for computers to deal with because its just a digital stream and it can be split from the signal and displayed on appropriate hardware.

The hard bit is converting a digital tv signal into a signal that can be broadcast over wires or wifi.


There are two ways to do this, you can use Multicast traffic or Unicast traffic, they each have their plus points and minus points....


Multicast
Multicast traffic is one continuous stream of data broadcast on the multicast IP's all of the time.

IP Multicast Address Ranges and Uses
224.0.0.0 - 224.0.0.255
Reserved for special “well-known” multicast addresses.
224.0.1.0 - 238.255.255.255
Globally-scoped (Internet-wide) multicast addresses.
239.0.0.0 - 239.255.255.255
Administratively-scoped (local) multicast addresses.

The advantages of multicast broadcasting is reduction in bandwidth usage, as its only ever
one stream of data and the clients "join" the stream.

One of the disadvantages of Multicast are that if your router or switch doesn't support IGMP sniffing you will flood your lan with multicast traffic, older HP switches are prone to this behaviour, newer HP switches can be configured to behave themselves instead of bringing your network to a grinding halt.

So to sum up broadcasting and entire transponder of say the BBC channels wouldn't eat up much bandwidth at all. 

Unicast
This is a one to one connection to the TV server/streaming server and each unique connection adds to the bandwidth consumed.

now a standard definition broadcast will consume in its raw broadcast format around 3 - 4 megabits/sec and a HD broadcast up to 8 or more megabits/sec.

Now without doing the mathematics you will have realised that you'll soon flood a 100Mbit lan link with stream data. Which is true but there are things you can do to reduce this to make it very workable in a LAN.

There are many different ways you can broadcast your TV on your lan but I've found these two the simplest and most reliable.

How to do it 



first off there is Mumudvb which is available under Linux for quite a few different distributions, including the Raspberry Pi

http://www.mumudvb.net/


this extremely small piece of software will allow you to multicast your TV channels using a very simple configuration file and using very cheap USB adapters, bear in mind you will need a separate tuner for each frequency/transponder you want to broadcast, ours here in Wales where I live is Preseli and it has 6 transponders.

698000000 khz
738000000 khz
770000000 khz
730000000 khz
786000000 khz

as of 2nd May 2016


This simple configuration file below I used to broadcast the 786Mhz digital tv transponder across my network, some 31 channels in all, on a single usb NOVA-T stick

#For DVB-T if the bandwith is 8MHz you don't have to set other parameters
card=0
freq=786
port_http=9091 <---anything already="" font="" isn="" like="" t="" taken="" that="" you="">
timeout_no_diff=1000
dvr_thread
rewrite_pat=1
rtp_header=1
check_status=1
pol=h
srate=27500
autoconfiguration=full
autoconf_radios=1
autoconf_pid_update=0
autoconf_ip4_header=239.10
multicast_ttl=2
multicast_auto_join=1
unicast=1
ip_http=192.168.1.175
sap=1  <-- broadcast="" can="" font="" pick="" so="" streams="" that="" them="" up="" vlc="" your="">

sap_default_group=MCAST


the items in bold are the things you'll need to change, obviously the frequency of the transmitter and the tuner card(s) you have, if you've only got one it will be 0 (zero) two and it will be 1 (one) etc... and the IP address of the computer which has the tuner card fitted. 

I wouldn't advise you setup six tuners on a home network unless your router understands multicasting (BT homehubs seem to)  otherwise you'll effectively kill your network for any other use......as all transponder frequencies pushed to individual multicast addresses would be well in excess of 80Mbit/sec, which is fine if you have 1Gbit connection but not 100Mbit....

a saner setup for home use would be TVheadend, which has a very simple web based front end which is setup very easily and with the newer versions above 4.1 you can even stream your tv in your browser .

https://tvheadend.org/projects/tvheadend

and there are clients out there for Android and iPhone/IOS, with TVheadend and one tuner you can tune to one transponder at a time, but that transponder will make all the channels on that transponder available, so say if one person in your house was watching BBC1, you could have another client watching BBC2 or any other BBC channel at the same time, but if one of the users decided they wanted to watch ITV it would kick off the other client tuned to BBC, so if you need all channels available at all times, you'd need more than one tuner, if you are never going to have more than three people using the tv streamer then you'd only need three tuners, as the likelihood is three transponders would serve the majority of the channels at any one time, you could possibly get away with two tuners, but you may get fights occurring with clients who want channels on other transponders. 


If you are intent on broadcasting the entire content of your local TV transmitter across your lan (as I am in the school I work in) then TVHeadend is the sanest approach, as it also allows DVR recordings which Mumudvb doesn't.

You'll need at least 6 tuners preferably PCI not USB such as the excellent TBS 6205 and a gigabit lan on the backbone of your lan, fortunately we have that where I work, remember with unicast you add to the bandwidth requirements with every client you serve.... so if 30 people each watched a channel it would be 30 x 3 megabits = 90 Megabits/sec if you upped that to 60 clients it would be 180 Mbits/sec, similarly if 30 people wanted to watch BBC1 simultaneously it would still be 3 - 4 megabits each, under multicast it would only EVER be 3 -4 Megabits regardless of how many clients you had, so for scalability multicast is the way to go.

You can serve multi channels with just one tuner lets just say for arguments sake the BBC transponder and that will allow as many clients per channel as your pc and lan can stand, watching or listening to any BBC broadcast.

You will need a very beefy computer to run TVheadend where that many people are going to stream from, I've specified an i7 top of the range 6 core beast with 3 TB of disk space and 16GB ram, because I'm going to transcode the video and audio streams into a lower quality and hence lower bandwidth, which takes CPU power.

I've managed to produce a perfectly respectable picture and sound consuming 400 - 500 kbit/sec on the VP8 video codec, which is fine for Windows 7 on Google Chrome .....So that is 1 Mbit/sec per two clients ......

Small tricks like only encoding one sound stream instead of stereo cuts it down a fair amount and I've found that setting the maximum bandwidth for sound at around 32kbit's in mono was fine, if you really needed stereo then 64kbit/sec was the bare minimum, without noticing poor sound.

I've calculated that at maximum bandwidth on our lan a PC of that calibre could in theory stream to 300 + clients at the same time, realistically the client count will be much lower in the low 50s I should imagine, but you do need the extra grunt if a lot of people hit the server at the same time.

I've yet to implement this but my tests so far have shown that it works very well. So once I've successfully implemented this I will come back and post my configs and setup and some real world figures to back up my theory......


Watch this space !!!


Update

I've since implemented this configuration and successfully streamed to over 75 clients and the CPU barely got above 25% across all cores, and was streaming to all of those clients at 1Mbit so the Gigabit backbone peaked at around 65 - 70Mbits and with a theoretical limit of 125Mbits on a Gigabit switch.... I suspect it would support at least 100 clients comfortably........


If anyone requires help on implementing this solution please leave a comment and I'll get back to you with the configuration and streaming profiles