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.
Software Selection
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.
Hardware Architecture
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!
Setup
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.
Testing
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.
Control
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.
Performance Tweaks
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.
Price List
- 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.
Summary
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.
Updates
20/06/2015: Changed all references of XBMC to Kodi after the project was renamed.
Hi.
Thanks for the guide
I wonder if it is possible with the new Pi 2 to have TVHeadend and XBMC/KODI on the same Pi? or will the pi struggle to keep up?
Also, have you got an update on the PVR setup using TVHeadend and KODI (this is precisely what I want it for, record, pause rewind etc)?
Finally, I am about to buy a DVB-T2 receiver. Does the receiver I choose effect the ability to record, or play and record, more than 1 channel at a time?
I basically want to replicate the functionality of my Tivo box, so I can get rid of it and same myself the £50/month price tag.
Many thanks
W
I expect it would be possible – the new Raspberry Pi is 6x as powerful overall compared to the original Raspberry Pi. Kodi does run reasonably well on a Raspberry Pi 1, the menu may not be hugely responsive but so long as the video can be decoded in hardware, playback is flawless. I was also running TVHeadend off a separate Raspberry Pi 1 when writing this article, and didn’t notice a huge CPU load so I expect with the Raspberry Pi 2 you could have a responsive system running Kodi and TVHeadend.
Different DVB-T2 receivers have different levels of support and stability under Linux. The one I used in this article was an old Freecom USB DVB-T stick, however a few months after this article I tried using the August DVB-T210, an inexpensive USB DVB-T2 receiver which worked fantastically. With this receiver, I was able to record/watch more than one channel at a time so long as they were on the same multiplex. You can see which channels are on which multiplexes here. If you want to record from more than one multiplex at a time, you’ll need to use two receivers. However, the Raspberry Pi’s USB bandwidth is somewhat limited (and also shared with the Ethernet controller) so I don’t know if you would be able to receive two multiplexes and stream over the network at the same time.
Would it be possible to replace the Raspberry Pi + Kodi fronted with a Roku?
I don’t own a Roku so I don’t know for sure, but a quick Google didn’t turn up anything that seemed to imply that you could use a Roku as a viewer for Tvheadend unfortunately.
RPi2 performance over v1 is huge. I would never recommend the original for media anymore.
Just a tip that you can actually run tvheadend server off of kodi openelec if you wanted to avoid raspian. That way you could also use the back end Pi as a client too.
Hi I sort of skimmed through this, day I purchase a usb tv tuner and aerial ones with great reviews and I saw on open elec site that it’s supported could I set up without using a laptop or pc wish to do this without having to have a laptop on bit confused with the to headendd software. Or is it just set up with pc but that’s it? Or have i got wrong idea hope to hear from you
I have a Pi3 and a DVB-T2 tuner running through TVHeadend. I’ve found playback choppy during transcoding. Do you have to do anything specific to enable hardware transcoding?
Do you find it choppy only when watching SD channels, or both SD and HD channels? Although the Pi can decode both in hardware, by default only the HD decoder (MPEG4) is enabled. To enable the SD decoder (MPEG2) you need to buy a cheap licence key from them. This is one of the ways they keep the Pi cheap, most people don’t need the MPEG2 hardware decoder and so it’s sold as an add-on.
Choppy on everything, HD and SD. Passthrough works beautifully, but my aim is to send it over the internet via my OpenVPN. What stream settings are you using?
Unfortunately i’m not actually running this system anymore – I don’t have a TV licence/watch live TV now so I took down this system. If you’re truly transcoding the video as opposed to just capturing it and streaming at the original bitrare, you’re going to have a lot of problems with the Pi, even a Pi 3. The processor on it is relatively weak, hence why it performs video decoding in hardware. There is no video encoding hardware, so that has to be done on the Pi’s processor which isn’t strong enough.
If you could stream it without transcoding that would resolve the problem, however it’s going to be extremely bandwidth intensive – around 20mbit/sec from memory.
I should also say, I’ve compiled TVHeadend 4.1. I wondered if there were flags that need to be set during compiling the deb.
There is no option for “Close device handle when idle” but as far as I’m aware this has been replaced with “Power save”. I can’t find “Disable PMT monitoring” anywhere in the settings.
And when I say “choppy” I mean I get 5-6 second pauses, a few seconds of video then more pauses. I get this on all stream options; webtv-h264-aac-matroska, webtv-h264-aac-mpegts & webtv-vp8-vorbis-webm. I haven’t messed with default settings too much.
Turns out switching codecs from aac and libx264 to “Copy codec type” fixes the choppiness. It works really well on SD and HD streams. I’ve also tested it out across mobile using OpenVPN. I’m not even getting 4G at the moment either – this is only HSPA so I’m super stoked. Next thing to test is Chromecast!
Thanks for all your help.
Glad to hear it’s working for you now! Changing the setting to that should mean that it’s just directly copying the broadcast rather than trying to convert it, which is far less processor intensive.
hi, when i try and click enable in Settings-TV-General- it tells me i need a tuner (which i do), backend software (which i do), and an add-on for the backend (which i do).
So i click OK and then nothing happens. it just unchecks the Enable section.
Any help available? Using the Sony PlayTV tuner on a RPi 2b running Kodi Isengard