123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- //--------------------------------------------------------------------
- // $Id: index.php,v 0.7 2008/07/24 18:46:16 tmerkel Exp $
- // Copyright 2008 Frubar Network (drscream@frubar.net | wiedi@frubar.net)
- //--------------------------------------------------------------------
- /*
- * information:
- * - vlc under gentoo is compiled with the following useflags:
- * X a52 aac aalib alsa avahi bidi dbus dts dvb dvd esd ffmpeg flac
- * gnutls hal httpd libgcrypt mmx modplug mp3 mpeg musepack musicbrainz
- * ncurses ogg opengl png pvr qt4 samba sdl shout sse stream taglib
- * truetype upnp v4l v4l2 vcd vcdx vlm vorbis xml xv
- * - apache must in the same group that the dvb device, for example
- * video and audio.
- * - the channels.dvb must be in the following format (the first is the
- * line number :P ):
- * 1: # generated by tmerkel
- * 2: info:channel name:frequency:orientation:unknown:symbols:unused:unused:channel
- * 3: TV :Das Erste :11836 :h :0 :27500 :101 :102 :28106
- * example line:
- * 4: TV:Das Erste:11836:h:0:27500:101:102:28106
- *
- */
- define( 'NL', "\n" );
- error_reporting( 0 );
- /*
- * functions
- */
- function rmnl( $in ) {
- return str_replace("\n", "", $in);
- }
- function init( $station = 0 ) {
- $channels = array();
- $chandb[0] = file('channels.dvb');
- $chandb[1] = count($chandb[0]);
- foreach( $chandb[0] as $chan ) {
- $channels[] = rmnl(explode(':', $chan));
- }
- for( $s = 0; $chandb[1] >= $s; $s++ ) {
- $channels[$s]['channel'] = $channels[$s][8];
- $channels[$s]['frequency'] = $channels[$s][2] . '000';
- $channels[$s]['symbols'] = $channels[$s][5] . '000';
- if($channels[$s][3] == 'v')
- $channels[$s]['voltage'] = 13;
- else
- $channels[$s]['voltage'] = 18;
- }
- if( $station > 0 )
- return $channels[$station];
- else
- return $channels;
- }
- function inerr( $error, $text ) {
- if ( $error ) {
- echo $text; exit();
- } else {
- echo $text;
- }
- }
- function channelname( $station, $channels ) {
- return $channels[$station][1];
- }
- function epg( $channel ) {
- if( $xml = @simplexml_load_file('epg.xml') ) {
- $path ="/tv/programme[@channel='".$channel.".dvb.guide']";
- foreach( $xml->xpath($path) as $res ) {
- $datetime['start'] = strtotime((string)$res->attributes()->start);
- $datetime['stop'] = strtotime((string)$res->attributes()->stop);
- if( $datetime['start'] < time() && $datetime['stop'] > time() ) {
- $return = array(
- 'title' => (string)$res->title,
- 'start' => (string)$res->attributes()->start,
- 'stop' => (string)$res->attributes()->stop,
- 'desc' => (string)$res->desc
- );
- return $return;
- }
- }
- }
- return false;
- }
- function vlc_status( ) {
- // proccess check
- $pid = @file_get_contents('vlc.pid');
- if( $pid )
- $s['s'] = true;
- else
- $s['s'] = false;
- if( $s['s'] ) {
- $tuned = @file_get_contents('tuning.dat');
- $s['i'] = $tuned;
- $s['p'] = $pid;
- }
- return $s;
- }
- function exec_vlc( $station, $cmd_http ) {
- $status = vlc_status();
- if( $status['s'] ) {
- kill_vlc( $status['p'] );
- }
- shell_exec( 'env -i bash -c '.escapeshellarg($cmd_http) );
- file_put_contents( 'tuning.dat', $station );
- $status = vlc_status();
- if( $status['s'] )
- return true;
- else
- return false;
- }
- function kill_vlc( $pid = 0 ) {
- if( !$pid ) {
- $status = vlc_status();
- $pid = $status['p'];
- }
- exec( 'kill -9 '.(int)$pid );
- unlink( 'vlc.pid' );
- sleep( 1 );
- $status = vlc_status();
- if( !$status['s'] ) {
- return true;
- }
- else {
- return false;
- }
- }
- function playlist( $channels ) {
- header('Content-type: application/xspf+xml');
- header('Content-Disposition: attachment; filename="tv.xspf"');
- echo '<?xml version="1.0" encoding="UTF-8"?>' . NL
- . '<playlist version="1" xmlns="http://xspf.org/ns/0/">' . NL
- . '<trackList>' . NL
- . '<track><location>http://'.$_SERVER[HTTP_HOST].':8080</location><title>Last Tuned</title></track>';
- foreach( $channels as $num => $c ) {
- if( $c[0] != 'TV' && $c[0] != 'RA' ) continue;
- echo '<track><location>http://'.$_SERVER[HTTP_HOST].'/frutv/?station=' . $num
- . '&playlist=true</location><title>' . $c[0]
- . ': ' . htmlspecialchars(utf8_encode($c[1])). '</title></track>'.NL;
- }
- echo '</trackList></playlist>';
- exit();
- }
- /*
- * parameters
- */
- function cmd( $channel, $frequency, $voltage, $symbols ) {
- $cmd_http = '/usr/bin/vlc -d --pidfile vlc.pid --ts-es-id-pid'
- . ' --program ' . $channel
- . ' --dvb-frequency=' . $frequency
- . ' --dvb-adapter=0'
- . ' dvb://'
- . ' -I telnet --telnet-host 127.0.0.1 --telnet-port 4212 --telnet-password asdfjedn3jdsw3jd'
- . ' --dvb-voltage=' . $voltage
- . ' --dvb-srate=' . $symbols
- . ' --sout \'#duplicate{dst="standard{access'
- . '=http,mux=ts,url=lafe.doc.fruky.net:8080/tv},select="program=' . $channel . '"}\' ';
- // . ' --sout \'#duplicate{dst="transcode{acodec=mpga, vcodec=mp4v,vb=1600,ab=128,vt=800000,keyint=80,deinterlace}:standard{access'
- // . '=http,mux=ts,url=lafe.doc.fruky.net:8080/tv}",select="program=' . $channel . '"}\' ';
- return $cmd_http;
- }
- /*
- * main
- */
- // param
- $playlist = $_GET['playlist'];
- $station = (int)$_GET['station'];
- $stop = $_GET['stop'];
- $channels = init( $station );
- if( $station > 0 ) {
- $cmd = cmd( $channels['channel'],
- $channels['frequency'],
- $channels['voltage'],
- $channels['symbols']);
- $status = exec_vlc( $station, $cmd );
-
- if( isset($playlist) ) {
- sleep(5); // fix for a slow machine, slow dvb card...
- header( 'Location: http://'.$_SERVER[HTTP_HOST].':8080/', true, 303 );
- } elseif( $status )
- header( 'Location: http://'.$_SERVER[HTTP_HOST].'/frutv/' );
- }
- elseif( isset($playlist) ) {
- playlist( $channels );
- }
- elseif( isset($stop) ) {
- kill_vlc();
- header( 'Location: http://'.$_SERVER[HTTP_HOST].'/frutv/' );
- }
- else {
- $status = vlc_status();
- $channelname = channelname( $status['i'], $channels );
- $channelname = ( $channelname ) ? 'currrent channel: '.$channelname.' (<a href="?stop">stop</a>)' : '';
- // default channel list
- echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'.NL
- .'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'.NL
- .'<html xmlns="http://www.w3.org/1999/xhtml">'.NL
- .' <head>'.NL
- .' <title>FruTV</title>'.NL
- .' <meta http-equiv="content-type" content="text/html; charset=utf-8" />'.NL
- .' <link rel="stylesheet" type="text/css" href="frutv.css" />'.NL
- .' <link rel="shortcut icon" type="image/png" href="frutv_ico.png" />'.NL
- .' </head>'.NL
- .' <body>'.NL;
- echo ' <div class="head">FruTV</div>'.NL
- .' <p>'.$channelname.'</p>'.NL
- .' <p><a href="?playlist">» playlist</a></p>'.NL
- .' <div class="content"><div>'.NL;
- foreach($channels as $num => $c) {
- if($num == 0) continue;
- if(preg_match("/^#/", $c[0])) {
- echo '</div><div class="headline">'.preg_replace("/^# /", "", $c[0]).'</div>'.NL
- .' <div class="box">'.NL;
- }
- if($c[0] != 'TV' && $c[0] != 'RA') continue;
- $epg = epg( $c[8] ); $epg = $epg['title']; if( empty($epg) ) $epg = "-";
- echo ' <div class="box_head"><a href="?station=' . $num . '">' . $c[1] . '</a></div>'.NL
- .' <div class="box_content"><div class="box_text">'.$epg.'</div></div>'.NL;
- }
- echo ' </div></div>'.NL
- .' <div class="footer">© FruTV, Frubar Network</div>'.NL
- .' </body>'.NL
- .'</html>';
- }
- // vim:syntax=php
- ?>
|