Watching TV on your Android via a Raspberry Pi

on April 2, 2013 in Linux with 18 comments by
Screenshot of XBMC Live TV
During the long Easter holiday I’ve kept myself busy with a little pet project for my Raspberry Pi. So far I’ve been using the RPI as a small intranet server, DNS server and Proxy server. But it had plenty of room, both in RAM and storage, to do other things. As I had recently acquired a (dirt-cheap!) Android-based tablet, I was wondering if it would be possible to stream live TV directly to it.

There are plenty of commercial solutions available, as well as some apps, that stream directly over the Internet. But, as I had mentioned, the tablet was dirt cheap and so it should be indicative of the amount of money I was willing to spend.

I already owned a USB TV stick, which I used on my laptop, and wanted to use this as the receiver. It’s a PCTV Nanostick T2, based on the Sony CXD2820R demodulator, which is (or was?) the only device to support the UK’s DVB-T2 format for HD reception.

The USB TV stick is directly supported by the 3.x kernel (as an em28xx device), and naturally, the RPI did too. So it was a matter of finding what software would stream from the TV source over the network, and what software that would be able to accept and display this stream – particularly on an Android device.

This is where XBMC comes into play. Since version 12 of XBMC (“Frodo”), it is now possible to watch live TV via a PVR back-end. What’s great too, is that XBMC runs on a plethora of operating systems and devices, including Android. The app is not yet available in the Google Play store, so you need to manually download and install it. Just note, that before you install it on your Android, you may need to enable the Unknown sources option found under the Android’s system settings (under the Security tab).

Note! While the Raspberry Pi will detect the USB TV stick that is directly connected to it, it cannot provide enough power to it. This means it will not be able to tune into any channels. So the USB TV stick must be attached to a self-powered USB hub (which in turn is connected to the RPI, of course).

Tvheadend

So with the “front-end” sorted, all I needed was the back-end for the Raspberry Pi. I settled on using Tvheadend, because it is very lightweight and runs on the ARM device just fine. However, I didn’t install it from binary as, well, there wasn’t one available – at least not in individual form. Compiling is a breeze though, and the developers already have everything available for a .DEB package. I skipped building a .DEB package, simply for sake of simplicity, but used their init script, config files, etc.

To compile Tvheadend, you start by getting the necessary packages. Some I will list here are optional, but I prefer to cram as much “optional” stuff into a build, so I won’t need to recompile everything it when I do need it at one point.

sudo apt-get install git build-essential \
  libavcodec-dev libavformat-dev libssl-dev \
  libavutil-dev libavahi-client-dev libcurl3 \
  w-scan

After the packages have been installed, proceed to clone the source code of Tvheadend using git. This will create a directory called tvheadend based on the location where you are, generally /home/pi/ if you’ve logged in via SSH.

git clone git://github.com/tvheadend/tvheadend.git

Now proceed to the directory and run the initial configuration. Running the configuration will check if you have the required packages installed, and fetch DVB scan data from a remote server.

cd tvheadend
./configure

If everything went well, you should not have received any fatal errors. You may see a few “fail” messages, but these are benign.

As I live in the London area, the Freeview transmitter is Crystal Palace. However, the source from which the configuration obtains the DVB scan data appears to be slightly outdated and may cause some hiccups. In my case, I’ve simply provided my own scan data using the previously installed w_scan application. You may skip this, if it doesn’t apply to you.

w_scan -cGB -x > ./data/dvb-scan/dvb-t/uk-CrystalPalace

This will directly replace the file in tvheadend/data/dvb-scan/dvb-t/uk-CrystalPalace, so if you’re concerned, you may wish to make a copy first.

Then proceed to compile Tvheadend, which for obvious reasons will take a little longer on a Raspberry Pi than a conventional computer:

make clean && make

Installation

Once it has been completed, you’re ready to install it. As mentioned earlier, Tvheadend already contains everything needed to build a .DEB package from which you could install it. However, I chose not to, and simply used some of the existing files to install it manually:

sudo su
make install
ln -s /usr/local/bin/tvheadend /usr/bin/tvheadend

cp ./debian/tvheadend.init /etc/init.d/tvheadend
chmod +x /etc/init.d/tvheadend
update-rc.d tvheadend defaults

cp ./debian/tvheadend.default /etc/default/tvheadend
adduser --quiet --system --group --shell /bin/bash hts

mkdir -p "/home/hts/.hts/tvheadend"

cat > /home/hts/.hts/tvheadend/superuser <<EOF
{
"username": "superadmin",
"password": "MySuperSecretPassword"
}
EOF

chmod 600 /home/hts/.hts/tvheadend/superuser
chown -R hts:hts "/home/hts/.hts"

exit
The one thing that you should pay attention to, is the portion here it creates the /home/hts/.hts/tvheadend/superuser file. The username and password provided here are only for sample purposes, and you should change this to something you’ve thought up yourself.

If you have a firewall setup on your RPI (and you should!), remember to open up the TCP ports 9981 and 9982 for the appropriate network. UDP is not required.

Configuration

Now that you have installed it, fire up Tvheadend:

sudo service tvheadend start

And configure it further at http://(ip address or domain name of your PI):9981. You will be prompted for a username and password, which was provided earlier in the superuser file.

Some guidance for configuration can be found in XBMC’s Wiki – it does not mention that after you’ve selected DVB input, you should tick the box Enabled found in the Adapter Configuration box, and then save the settings before it will perform an initial scan. You can follow its progress by expanding the window at the bottom (click the chevrons pointing upwards in the bottom-right corner).

Additionally, you need to setup the Access Control list, also found in the Configuration area, to allow/restrict access from devices. This is explained in more detail in the Tvheadend Wiki.

After this, you can configure XBMC, by going into its system settings, and enabling the Tvheadend HTSP Client add-on. Once that has been enabled, click Configure for it, and provide the necessary details, which at minimum is the IP or domain name for the Raspberry Pi. Finally, still within XBMC’s system settings, enable Live TV. You are now able to watch live TV! Unless …

Bugs!

There’s a bug with the Raspberry Pi firmware related to its USB. Or, there’s a problem with the USB itself. Either way, it affects many video streaming devices, such as webcams and TV USB sticks (like the em28xx devices). The result is that you may find yourself rather annoyed by the fact that live TV is barely “live”, and mostly consists of black screens or what appears to be a very bad signal. Looking at the logs for Tvheadend, you may notice many lines similar to this:

Apr 1 21:45:02 raspberrypi tvheadend[13259]: TS: Sony CXD2820R/London: 490,000 kHz/BBC ONE: MPEG2AUDIO @ #102: Continuity counter error, 533 duplicate log lines suppressed
Apr 1 21:45:03 raspberrypi tvheadend[13259]: TS: Sony CXD2820R/London: 490,000 kHz/BBC ONE: MPEG2VIDEO @ #101: Continuity counter error, 5777 duplicate log lines suppressed
Apr 1 21:45:03 raspberrypi tvheadend[13259]: TS: Sony CXD2820R/London: 490,000 kHz/BBC ONE: MPEG2AUDIO @ #106: Continuity counter error, 121 duplicate log lines suppressed
Apr 1 21:45:03 raspberrypi tvheadend[13259]: TS: Sony CXD2820R/London: 490,000 kHz/BBC ONE: MPEG2AUDIO @ #102: Continuity counter error, 534 duplicate log lines suppressed

Thankfully, this problem has been addressed by the Raspbian teams, and requires that you update your kernel (firmware). You can double-check the current version, which will likely produce something similar to this:

# uname -a
Linux raspberrypi 3.6.11+ #371 PREEMPT Thu Feb 7 16:31:35 GMT 2013 armv6l GNU/Linux

The important bit is the #371 part here, which is the one that has the above described issue. Updating the kernel/firmware is slightly different than what you’d expect from most Debian distributions (which is simply apt-get a linux image), but Liam McLoughlin has created a simple script to help you with that. Here’s a quick how-to:

sudo wget http://goo.gl/1BOfJ -O /usr/bin/rpi-update && sudo chmod +x /usr/bin/rpi-update
sudo rpi-update

Just let it run its course, which involves updating itself and then obtaining the latest firmware. If this is the first time you’ve run it, it will download about 42 Megabytes worth of files. After it has successfully completed downloading and installing, you need to reboot the Raspberry Pi. Once back up, the kernel/firmware version should have been updated to at least the following:

# uname -a
Linux raspberrypi 3.6.11+ #401 PREEMPT Fri Mar 29 22:59:09 GMT 2013 armv6l GNU/Linux

Again, the important part here is the #401, changed from the previous #371.

NOTE: If you still have all the original Raspbian bells and whistles installed, such as X-server/Gnome, then before you reboot the RPI, double check your available disk space.

Crystal Palace scan file

By request, the following is the scan file for Crystal Palace with which I receive the HD channels:

#------------------------------------------------------------------------------
# file automatically generated by w_scan
# (http://wirbel.htpc-forum.de/w_scan/index2.html)
#! <w_scan> 20120605 1 0 TERRESTRIAL GB </w_scan>
#------------------------------------------------------------------------------
# location and provider: <add description here>
# date (yyyy-mm-dd) : 2013-06-15
# provided by (opt) : <your name or email here>
#
# T[2] [plp_id] [system_id] <freq> <bw> <fec_hi> <fec_lo> <mod> <tm> <guard> <hi> [# comment]
#------------------------------------------------------------------------------
T 482000000 8MHz 3/4 NONE QAM64 8k 1/32 NONE # London
T 490000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE # London
T 514000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE # London
T 506000000 8MHz 3/4 NONE QAM64 8k 1/32 NONE # London
T 529833000 8MHz 3/4 NONE QAM64 8k 1/32 NONE # London
T2 0 16435 546000000 8MHz AUTO AUTO AUTO AUTO AUTO AUTO # London
T2 0 16435 746000000 8MHz AUTO AUTO AUTO AUTO AUTO AUTO # London

 

18 comments

  1. posted on Feb 20, 2014 at 12:40 PM  |  reply

    Hi there, I was just wondering ho i could undo everything that was done in this tutorial. thanks

  2. posted on Oct 12, 2013 at 7:01 PM  |  reply

    Great tutorial thanks. One question, do you get the HD channels? If so can you please share your Crystal Place mux scan file, I am following the same instructions as I can see both Crystal Palace and Bluebell hill but not getting any HD channels, I assume you need to run make again after modifying the Mux file.

    • posted on Oct 13, 2013 at 2:53 PM  |  reply

      Thanks. I do get the HD channels, yes. I’ve updated the post to include the Crystal Palace scan file, so you can simply copy & paste the contents if you wish (it’s the last two “T2″ lines – so your USB TV stick would need to support that format). After compilation and installation, TVHeadEnd uses the following locations for the DVB-T scan files:

      /usr/local/share/tvheadend/data/dvb-scan/dvb-t/

      The file is uk-CrystalPalace

      I do believe you can specify it manually through the TVHeadEnd interface, if I’m not mistaken, but haven’t actually used that option.

  3. Marco
    posted on Oct 02, 2013 at 8:37 AM  |  reply

    Have you monitored the performance of the RPI while watching / recording HD channels? How much would the raspberry pi be able to handle (as a server only,) Does it handle 2 or 3 hd streams at the same time?

    • posted on Oct 13, 2013 at 2:56 PM  |  reply

      I haven’t tried that, no. The decoding for the TV signal is handled by the TV USB stick, but there’s further encoding done for transmitting it over the network – that might be the clincher for the little Pi.

  4. sathish
    posted on Aug 18, 2013 at 7:20 AM  |  reply

    hmmmmm..

  5. David Marsden
    posted on Aug 01, 2013 at 5:01 PM  |  reply

    I’m having a problem starting Tvheadend. It’s compiled clean and installed OK but the script /etc/init.d/tvheadend is exiting on the line:

    [ -x “$DAEMON” ] || exit 0

    about line 20 which means the package is not installed although, as I say, make install ran OK.
    What have I done wrong?

  6. Steve P
    posted on Jul 17, 2013 at 9:39 AM  |  reply

    Does it allow you to record Freeview HD channels?

    • posted on Jul 17, 2013 at 9:49 AM  |  reply

      To be fair, I’ve had difficulty recording in HD at first – however, last month I had to re-install everything, which includes a newer version of TVHeadEnd, and it is working now. But I wouldn’t recommend storing it on the SD card, not just because of the size, but the speed requirements.

      • Steve P
        posted on Jul 17, 2013 at 11:34 AM  |  reply

        Thanks for getting back.

        Have ordered PCTV nanoStick T2.

        So will give it a try :-)

        • Steve P
          posted on Jul 25, 2013 at 2:15 PM  |  reply

          Got it working using your instructions.

          The only 2 problems I had were :

          1. The PCTV Nanostick coming with an indoor aerial that useless. Had to buy a Co-axial to MCX adaptor to plug my external aerial into the PCTV Nanostick.

          2. I couldn’t record prgramming to the external usb stick I attached to the PI. Got a file missing error in tvheadend. This was due to file permissions after giving it the right permissions it work perfectly.

  7. Vincent
    posted on May 22, 2013 at 4:06 PM  |  reply

    Does it allow you to change channel on your Android tablet?

    • posted on May 22, 2013 at 6:13 PM  |  reply

      Yes it does. You can change channels, record them, etc.

  8. posted on Apr 28, 2013 at 8:11 AM  |  reply

    I’m going to try this right now. My Pi is really going to start earning its keep!

    VERY IMPORTANT FIRST STEP that must be done on a fresh Pi before following the above instructions:

    sudo apt-get update

    If you don’t do that then a lot of the downloads will fail, and so later steps won’t have a chance of working.

  9. freulein
    posted on Apr 12, 2013 at 1:21 PM  |  reply

    Ooops sorry… after sitting back and rethinking a bit (ERROR… hmmm what does ERROR really mean) I found that apt-getting libssl-dev fixed the problem… ;)

    • posted on Apr 12, 2013 at 1:37 PM  |  reply

      Oh, nice catch! I already had it installed, but will add it to the article to help others. Thanks!

  10. freulein
    posted on Apr 12, 2013 at 1:13 PM  |  reply

    Nice article, thanks a lot!

    It also seems possible now to use the app TVHGuide on Android to watch video streamed by tvheadend.

    However, I don’t succeed in getting tvheadend compiled on my pi.

    ./configure gives me:

    Checking support/features
    checking for cc execinfo.h … ok
    checking for cc -mmmx … fail
    checking for cc -msse2 … fail
    checking for cc getloadavg … ok
    checking for cc atomic64 … fail
    checking for py module gzip … ok
    checking for bzip2 … ok
    checking for pkg openssl … fail
    checking for pkg libssl … fail
    ERROR: SSL development support not found

    Is that a fatal error?

    “make clean” and “make” give me:

    Makefile:23: .config.mk: No such file or directory
    Makefile:285: /support/.mk: No such file or directory
    make: *** No rule to make target `/support/.mk’. Stop.

    Any amount of fiddling with the Makefile wouldn’t let me build it… but I have to admit I’m an absolute make-newbie…

    • Sam
      posted on Apr 30, 2013 at 2:27 PM  |  reply

      You need to sort the missing SSL issues, i’m pretty sure you should have got them in the ‘libssl-dev’ install you did in the first step…

Join the discussion

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>