Stream mythtv recordings to mobile devices

From MythTV Official Wiki
Revision as of 17:15, 5 January 2009 by UltramaticOrange (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This is a quick guide describing how to stream your recordings to mobile devices using 3G technology. It is written for users who have mobile phones or other devices capable of playing video streams of this type as most modern devices can. The phone must be capable of playing a 3GP stream over RTSP, this is pretty standard and can be done on all new Nokia handsets from the last year and probably most other makes.

Prerequisites

  1. Working MythTV - already making recordings
  2. ffmpeg capable of creating 3GP recordings (see my guide to streaming via flash video)

Concept

MythTV makes recordings as usual, a user job is ran against the recording to convert it to 3GP and add extra data required for streaming, 3GP files are streamed using Darwin Streaming Server

This works successfully over both Wifi and 3G. If you are in the UK and on the Three network sign up for the X-Series add-on for 5GBP per month. It gives you "unlimited" (actually 1 gig) data transfer per month which can be used for streaming TV over 3G to your phone. The settings listed below use approximately 44megs per hour of viewing and video plays smoothly over their 3G network.

Tested on

Server: Gentoo box running kernel 2.6.12 with MythTV 0.20 and Darwin Streaming Server 5.5.3. Nokia N80 streaming videos over Wifi and viewing on RealPlayer (comes with phone)

Video playback is of excellent quality with no pauses for buffering.

Guide

Setup Darwin Streaming Server

Darwin streaming server is an open source streaming media server made by the good guys at Apple. Darwin Streaming Server is now very easy to install. full instructions are included with the application. Darwin streaming server is available for Linux, Mac, Windows and Solaris.

1. Download the source code tarball from: http://dss.macosforge.org/

2. Create Group qtss
 groupadd qtss 
3. Create user qtss
 useradd qtss -g qtss 
4. cd into Darwin source dir:
 cd DarwinStreamingSrvr* 
5. 6.0.3 won't compile on linux without patch. Download it from http://www.abrahamsson.com/dss-6.0.3.patch and apply it
 patch -p1 < dss-6.0.3.patch 

If you're running on a 64-bit install, download and apply this patch at http://www.abrahamsson.com/dss-6.0.3-x86_64.patch too.

 patch -p1 < dss-6.0.3-x86_64.patch 
6. Compile DSS
 ./buildtarball 
7. Inside the source directory cd in to DarwinStreamingSrvr-Linux
 cd DarwinStreamingSrvr-Linux 
8. Install DSS
 ./Install 

Check the installation by opening http://localhost:1220 in a web browser to confirm that the server is up and running.

When configuring the server make sure you add a shared directory and note this down, this is where your 3GP files for streaming will be stored. I use /video, the same directory as my main mythtv recordings.

Setup MP4Box

To ensure that media is streamed correctly "hints" have to be added to help with timing and sync. To make your 3GP files MPEG4 compliant and to add RTSP hints you need to use a program called MP4Box, available for download as part of the GPAC framework here: http://sourceforge.net/project/showfiles.php?group_id=84101

Install this somewhere in your path as per the readme included with the file.

Alternatively if you using gentoo simply type

emerge gpac

Add user job in mythtv

Next you need to create the script which will be ran as a user job in mythtv. The following is an example:

Script.png /usr/local/bin/myth3gp.sh

#!/bin/bash

out="/video"
outfile=`echo $2 | sed 's/ /_/g' | sed 's/://g'`
ffmpeg -y -i "$1" -r 20 -s 352x288 -b 400k -ac 1 -ar 8000 -ab 24k "$out/$outfile.3gp"
cd $out
MP4Box -3gp -mtu 1450 -hint "$outfile.3gp"

Save this to /usr/local/bin/myth3gp.sh

Set the out variable to wherever your Darwin Streaming Server is configured to share. This encodes video at a resolution of 352x288, bitrate 400, 20 frames per second.

Next you need to setup the user job in mythtv. Run mythtv-setup and go to the General Settings section. Under user jobs find a spare job and give it a name of something appropriate (eg. Myth3GP) and the following command:

/usr/local/bin/myth3gp.sh "%DIR%" "%FILE%"

Set this job to run for all recordings you want to convert and share. On my Athlon 2800 box it takes 11 mins to convert and share a 30min recording.

Create web page linking to shows

Finally you need a web page to link to your recordings. The following PHP script will work nicely, set the directory and hostname parameters as appropriate.

Script.png mobile.php

<?php

$directory = "/video";
$hostname  = "uour.host";

print("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
print("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
print("<head>\n");
print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n");
print("<title>MythTV Mobile</title>\n");
print("</head>\n");
print("<body><p>\n");

print("<span style=\"font-weight: bold; font-size: 1.2em;\">MythTV Mobile</span><br /> <br />\n");

/* using shell_exec(ls) because opendir and dir class both give seemingly random ordering of files */
$output = '';
$ls = shell_exec(escapeshellcmd("ls $directory"));
$files = explode("\n", $ls);
foreach($files as $file) {
        if (preg_match('/.3gp$/', $file)) {
                $file_trimmed = rtrim($file, '.3gp');
                $data = preg_split('/---/', $file_trimmed);
                $show = preg_replace('/_/', ' ', $data[1]);
                $show_sub = preg_replace('/_/', ' ', $data[2]);
                ($data[2]) ? $title = "$show - $show_sub" : $title = $show;
                $date_formatted = date('D n/j/Y, g:i A', mktime(substr($data[0], 8, 2), 
                                                                substr($data[0], 10, 2), 
                                                                substr($data[0], 12, 2), 
                                                                substr($data[0], 4, 2), 
                                                                substr($data[0], 6, 2), 
                                                                substr($data[0], 0, 4)));
                $output = "<a href=\"rtsp://$hostname/$file\">$title</a>: $date_formatted<br /> <br />\n".$output;
        }
}

print($output);
print("</p></body>\n");
print("</html>\n");

?>

To Do

Expire 3GP files with normal recordings.

Please see the discussion section for proposed mythexpire.pl script that works for me. -- UKDude

Uninstall Darwin Streaming Server

If for any reason you decide you don't want or need the Darwin Streaming Server anymore, you can try the following script, but please make note of the following facts:

  • This script is a hacked up version of the install script for Darwin Streaming Server v6.0.3 (current as of Jan 5th, 2009). Using it to uninstall newer versions is NOT recommended.
  • This script has only been tested on Mythbuntu 8.10. Check that the paths, file names and programs used in this script are valid for your distribution of Linux.
#!/bin/bash
############################################################
# BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM,
# TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING
# THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND,
# EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.
# SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
############################################################

# uninstall script for the Darwin Streaming Server


echo;echo Uninstalling Darwin Streaming Server;echo

INSTALL_OS=`uname`

if [ $INSTALL_OS != "SunOS" ]; then
        USERID=`id -u`
else
        USERID=`/usr/xpg4/bin/id -u`
fi

if [ $USERID = 0 ]; then

        echo Checking for and Killing currently running Darwin Streaming Server
        if [ `uname` = "FreeBSD" ]; then
                ps -ax | awk '/DarwinStreamingServer/ {print $1}' | xargs kill -9
                ps -ax | awk '/streamingadminserver.pl/ {print $1}' | xargs kill -9
        fi

        if [ `uname` = "Linux" ]; then
                ps ax | awk '{print $1" " $5}' | awk '/DarwinStreamingServer/ {print $1}' | xargs -r kill -9
                ps ax | awk '/streamingadminserver.pl/ {print $1}' | xargs -r kill -9
        fi

        if [ `uname` = "SunOS" -o `uname` = "IRIX" -o `uname` = "IRIX64" ]; then
                ps -aef | awk '/DarwinStreamingServer/ {print $2}' | xargs -n 2 kill -9
                ps -aef | awk '/streamingadminserver.pl/ {print $2}' | xargs -n 1 kill -9
        fi

## REMOVED OLD VERSION ##

        echo Removing previous versions of Darwin Streaming Server
        if [ -f /usr/local/sbin/DarwinStreamingServer ]; then
                echo removing /usr/local/sbin/DarwinStreamingServer
                rm -f /usr/local/sbin/DarwinStreamingServer
        fi

        if [ -f /usr/local/bin/PlaylistBroadcaster ]; then
                echo removing /usr/local/bin/PlaylistBroadcaster
                rm -f /usr/local/bin/PlaylistBroadcaster
        fi

        if [ -f /usr/local/bin/MP3Broadcaster ]; then
                echo removing /usr/local/bin/MP3Broadcaster
                rm -f /usr/local/bin/MP3Broadcaster
        fi

        if [ -f /usr/local/bin/qtpasswd ]; then
                echo removing /usr/local/bin/qtpasswd
                rm -f /usr/local/bin/qtpasswd
        fi

        if [ -f /usr/local/sbin/streamingadminserver.pl ]; then
                echo removing /usr/local/sbin/streamingadminserver.pl
                rm -f /usr/local/sbin/streamingadminserver.pl
        fi

        if [ -f /usr/local/bin/streamingadminserver.pl ]; then
                echo removing /usr/local/bin/streamingadminserver.pl
                rm -f /usr/local/bin/streamingadminserver.pl
        fi

        if [ -f /usr/local/bin/SpamPro ]; then
                echo removing /usr/local/bin/SpamPro
                rm -f /usr/local/bin/SpamPro
        fi

if [ -e StreamingLoadTool ]; then
        if [ -f /usr/local/bin/StreamingLoadTool ]; then
                echo removing /usr/local/bin/StreamingLoadTool
                rm -f /usr/local/bin/StreamingLoadTool
        fi
fi
        echo

## Delete config files
        echo Removing config files
        if [ -d /etc/streaming ]; then
                rm -rf /etc/streaming
        fi

        echo

        echo "Deleting unprivileged user to run the server = \"qtss\"."
        groupdel qtss
        userdel qtss

        if [ -d /usr/local/sbin/StreamingServerModules ]; then
                echo deleting "/usr/local/sbin/StreamingServerModules" directory
                rm -rf /usr/local/sbin/StreamingServerModules
        fi

        if [ -d /var/streaming ]; then
                echo removing "/var/streaming" directory
                rm -rf /var/streaming
        fi

        if [ -d /usr/local/movies ]; then
                echo removing "/usr/local/movies" directory
                rm -rf /usr/local/movies
        fi

        echo;echo uninstallation complete!
else

        echo "Unable to perform uninstall"
        echo "You must be logged in as root to uninstall Darwin Streaming Server";echo
        exit 1
fi