Streaming Live TV using Tvheadend and Kodi on Raspberry Pis
I recently had an interesting request from some relatives – they wanted to be able to watch TV in the kitchen. A simple task I thought, I grabbed an old analogue TV, digital set-top box and internal aerial. Unfortunately, despite the post-switchover boost in transmission power, the signal received was just slightly too weak to be useful, resulting in only a handful of channels reliably working, not to mention how untidy the jumble of equipment looked.
This lead to a crazy idea – what if a computer attached to the main aerial, which has perfect reception, received live TV and retransmitted it over the internal network to be received by another computer attached to the TV? It was crazy enough that it might just work.
Since the launch of the Raspberry Pi almost 2 years ago, it has found its way in to a number of scenarios that were never anticipated. It’s been used for everything from controlling advertising boards, small micro-servers, distributed computing and, most importantly for this project, media centres.
Whilst the processing power of the Raspberry Pi is low, the GPU is able to fully decode H.264 video at up to 40Mbit/sec – no easy feat. Due to this, the Raspberry Pi can play back most media flawlessly. The attractive price point ($35), small size, low power requirements and video processing power make it particularly suited to this project.
I then needed to identify two bits of software that would run on the Raspberry Pi – one on the back end to receive and re-transmit the TV signal and one on the front end to receive and display the re-transmitted signal. For receiving, the long-running Kodi project makes for a fantastic media centre and, most importantly for this project, supports reception from a wide range of PVR hosts. Thus, selecting Kodi for the front end display wouldn’t affect my choice of back end. As Kodi is just a Linux application, I could theoretically choose any distribution to run it on. However, I instead chose to go with OpenELEC – a stripped down distribution that’s designed only to run Kodi. It works like an appliance, and so handles updates both to itself and the underlying Linux system automatically. A downside for tinkers, but an upside for simplicity. As this system was for a relative, simplicity is key – it reduces the number of calls I get about things not working.
On the back end, there are a number of potential options, however only a few offer support for the Raspberry Pi. These are Tvheadend and VDR. I went with Tvheadend as the setup looked relatively simple. There’s no appliance version of Tvheadend as there was with Kodi and OpenELEC, so I opted to run it on Raspbian, the ‘official’ distribution of Linux for the Raspberry Pi.
Next up, hardware. I had decided that I was going to run this project using Raspberry Pis, but other hardware was also required. A wired connection was available next to an aerial point for the back end Raspberry Pi, but the front end Raspberry Pi (in the kitchen) wasn’t anywhere near a wired network connection, and running a new cable wasn’t an option. Instead, I used a spare Edimax EW-7811Un USB adaptor to add WiFi capabilities to the front end Raspberry Pi. With WiFi devices on the Raspberry Pi, you’re recommended to run them through a powered USB hub due to their power draw requirements, but I found that it worked well connected to just the on board USB ports. There are no other USB peripherals connected (e.g. Keyboard/Mouse) so this reduced the overall power requirements.
As for receiving the signal, I had an old Freecom DVB-T USB stick, which has Linux support. Unfortunately, the power draw of this was too much for the Raspberry Pi and so I had to run it through a powered hub. Being old, it also does not support the newer DVB-T2 standard, which is used for Freeview HD transmissions. This isn’t a major issue however considering that the front end is an analogue TV and so can’t display HD anyway!
Front End Installation
Next up was setting up everything. OpenELEC was relatively painless, I just needed to write the provided image to an SD card and then go through the initial setup. Easy!
Back End Installation
Tvheadend was more involved however. After writing the Raspbian image to the SD card and going through the initial setup, I needed to add the repository for Tvheadend and install it. Once installed it runs automatically as a background service. Tvheadend is administered entirely through a web interface which runs on port 9981. To get my channels into Tvheadend I had to do the following;
- Browse to Configuration > DVB Inputs > TV Adapters. Select the connected adapter from the list. My Freecom DVB-T USB stick appeared as “Wideview USB DVB-T”.
- Configure the adapter. Due to compatibility issues, I had to make sure that “Close device handle when idle” was checked and “Disable PMT monitoring” was unchecked. If I didn’t do this, I would quickly hit the file handle limit for the USB stick and need to reboot to use it again. Once you’ve configured the right settings for your adapter click Save.
- Browse to the Multiplexes tab. You can click “Add DVB Network by location” on the General tab to automatically add the Multiplexes for your area however I found that they were out of date.
- Add the multiplexes for your area one by one by clicking “Add mux(es) manually”. For Freeview in the UK, you can find the values you need to enter at ukfree.tv and entering your postcode to find your nearest Freeview transmitter. Enter the frequency, bandwidth (normally 8Mhz), constellation and transmission mode (normally 8k), leave all other values at Auto. Click add to finish adding the multiplex. Repeat for as many multiplexes as you wish to add. When you add a multiplex, Tvheadend will automatically scan the multiplex for available channels.
- Browse to the General tab. On the information and capabilities section, wait for the “Muxes awaiting initial scan” count to drop to 0. Now, click “Map DVB services to channels” and Tvheadend will map the discovered channels.
- Done! Tvheadend is now advertising the channels.
Connecting the Front End to the Back End
Now that both the Front End and the Back End are configured and hopefully functional, we can tell the Front End (Kodi) to use the Back End (Tvheadend) to receive TV. To configure Kodi;
- Browse to System > Settings > Live TV > General.
- Click “Enabled” to Enable Live TV reception.
- You will be warned that there is currently no configured add on for Live TV. Click OK and you will be taken to the list of available Live TV back ends.
- Find “Tvheadend HTSP Client” and select Configure. Enter the IP address or hostname of your back end and then select OK.
- Select Enable. Kodi will now try and connect to the Tvheadend back end and retrieve channel listings etc.
At this point, you should be able to receive Live TV on your Kodi front end. Browse to Live TV > TV channels and select a channel to start watching. If you have no channels listed or can’t view any channels, check on Tvheadend that you’ve performed the mapping and check the debug log by clicking the little arrow on the bottom right of the web interface.
However, at this point, you may find that while you have audio, you don’t have any video. Earlier, it was noted that part of the power of the Raspberry Pi is that it has a built in GPU that can decode video, doing tasks which would be impossible on the weak CPU. However, as a cost-saving measure to help the Raspberry Pi meet its $35 price point, not all of the video formats that the GPU can decode are enabled. The majority of video content today is encoded in MPEG4 H.264, which the Raspberry Pi’s GPU can decode by default. However, UK Freeview DVB-T transmissions use the older MPEG2 format – simply because MPEG4 didn’t exist when Freeview originally launched. Unfortunately, while the Raspberry Pi’s GPU is able to decode MPEG2, it is one of the codecs which was not enabled by default in order to save costs. Thankfully, you can buy a licence key to enable it for the cheap price of £2.40 from the Raspberry Pi web store. Buy it, await your key, and then install it on your Raspberry Pi. It is very important that you give the serial number of the front end Raspberry Pi – that’s the device that does the decoding and display! The back end Raspberry Pi just receives the MPEG2 stream from the aerial and re-transmits it over the network – it does not alter the stream.
Once you’ve got this all setup, you’ll probably wonder how you’re going to control it. Having a keyboard and mouse attached to the front end probably isn’t practical or convenient for control. You could likely buy an Infrared receiver to plug in to the Raspberry Pi and use a remote. However, since Kodi is connected to your network, why not control it from another device that’s also on your network, say a phone or tablet.
Indeed, Kodi supports remote control by other devices. A web interface is available, or even better an API. This can be used by other applications to control Kodi and receive information.
I use Yatse on my Android devices to control Kodi, solutions for other devices also likely exist but I wouldn’t know about them as i’m purely an Android user. The great thing about Yatse is that it can act as a normal remote (e.g. going left, right. up, down etc in menu interfaces) or it can also be used as a “second screen” to select content which will immediately start playing back on Kodi – easier than navigating through several menus. For Live TV, just tap the PVR button then tap the channel you want to watch. It’s that simple.
The Raspberry Pi is just slightly too underpowered to run the Kodi interface smoothly (but as video playing is done by the GPU, there’s no issues there). If the lack of a smooth interface annoys you though, you can either make tweaks to the Kodi interface, such as disabling the RSS feed, or you can overclock your Raspberry Pi. Overclocking runs your Raspberry Pi past its rated performance and may cause instability. Overclocking will not void the warranty on your Raspberry Pi, so long as you overclock it in a way which is endorsed by the Raspberry Pi foundation.
I run the the front end Raspberry Pi using the pre-set Turbo setting. The supported method of overclocking is using the raspi-config utility, but this isn’t present on OpenELEC. Instead, if you want to overclock you’ll need to edit some files as described in this forum thread. It is very important that you stick to the pre-set values (Modest, Medium, High, Turbo) and do not set force_turbo=1 as this will definitely invalidate your warranty. Without force_tubro=1, the Raspberry Pi will automatically overclock itself subject to the requirements of the current workload (i.e. it won’t overclock if the extra processing power isn’t needed) and the current temperature of the processor (it won’t be overclocked if it is 85 centigrade or higher, but you’re unlikely to hit this. Mine in a case runs at around 65 centigrade with a Turbo overclock and full load.
- 2x Raspberry Pis (Model B, 512MB RAM): £58.14 via Amazon.
- 2x Cyntech Case for Raspberry Pi: £11.50 via Amazon.
- 2x 8GB SanDisk Ultra Micro SD Card with SD Adapter: £17.70 via Amazon.
- 1x Freecom DVB-T USB Stick: Discontinued. You may wish to buy the PCTV 290e instead, with the added bonus of supporting Freeview HD on DVB-T2. However, ensure you don’t buy the newer 292e as it is not compatible with Linux yet.
- Edimax EW-7811Un USB WiFi Adapter: £6.78 via Amazon.
Total cost: £94.12, excluding DVB-T receiver. For me, the total cost is lower than this as I already owned some of these components. Still, not a bad price at all, and certainly much cheaper than having a new aerial feed run to the kitchen.
The system that i’ve built works quite well. Setup wasn’t exactly an easy process, but once it’s working it is very stable. This certainly made for a fun weekend project and the reception from my relatives has been positive, they have no issues using Yatse as a remote control from their phones either.
As for the next steps on this project, i’m looking in to the DVR and Timeshifting options offered by Tvheadend as well as buying a more recent Freeview receiver, such as the previously mentioned PCTV 290e in order to also receive HD Freeview transmissions.
20/06/2015: Changed all references of XBMC to Kodi after the project was renamed.