Documentation is available at VLCServer.class.php
- <?php
- class VLCServer {
- /**
- * @access private
- * @var array
- */
- private $lDimensions;
- /**
- * @access private
- * @var string
- */
- private $lChannel;
- /**
- * @access private
- * @var string
- */
- private $lAudioCodec;
- /**
- * @access private
- * @var string
- */
- private $lVideoCodec;
- /**
- * @access private
- * @var int
- */
- private $lFPS;
- /**
- * @access private
- * @var string
- */
- private $lStreamType;
- /**
- * @access private
- * @var int
- */
- private $lBitrate;
- /**
- * @access private
- * @var int
- */
- private $lAudioBitrate;
- /**
- * @access private
- * @var int
- */
- private $lVolume;
- /**
- * @access private
- * @var int
- */
- private $lScale;
- /**
- * @access private
- * @var string
- */
- private $lVLCExecutable;
- /**
- * @access private
- * @var int
- */
- private $lChannels;
- /**
- * @access private
- * @var string
- */
- private $lEnigmaVersion;
- /**
- * @access private
- * @var array
- */
- private $lAuthentication;
- /**
- * @access private
- * @var string
- */
- private $lSettingsFile;
- /**
- * Constructor. This creates a VLC Server object. Some defaults are already set. These are: volume, scale and audio channels. It alse detects if it is running on Windows or Linux.
- * @return VLCServer
- */
- function __construct($pEnigmaVersion = "enigma2", $pAuthentication = array()) {
- $this->lDimensions = array();
- $this->lVLCExecutable = Settings::getVLCLocation(($this->isLinux()? 0 : 1));
- $this->lVolume = 60;
- $this->lScale = "1";
- $this->lChannels = 2;
- $this->setEnigmaVersion($pEnigmaVersion);
- $this->lAuthentication = $pAuthentication;
- $this->lSettingsFile = Settings::getVLCSettingsFile();
- }
- /**
- * Set the Enigma type. Valid values are 'enigma1' and 'enigma2'.
- * @param string $pType
- * @return boolean
- */
- public function setEnigmaVersion($pType) {
- if ( ($pType == trim($pType)) == "") return false;
- switch ($pType) {
- case "enigma1":
- $this->lEnigmaVersion = "enigma1";
- break;
- case "enigma2";
- $this->lEnigmaVersion = "enigma2";
- break;
- default:
- return false;
- break;
- }
- return true;
- }
- /**
- * Set the VLC Server encoding dimentions. This is the size of the re-encoded stream.
- * @param string $pSizes
- */
- public function setDimentions($pSizes) {
- $pSizes = explode("x",$pSizes);
- if (is_array($pSizes) && sizeof($pSizes) == 2 && is_numeric($pSizes[0]) && is_numeric($pSizes[1])) {
- $this->lDimensions = $pSizes;
- }
- }
- /**
- * Set the VLC Server source channel. This is the ID of a Channel Object.
- * @param string $pChannel
- */
- public function setChannel($pChannel) {
- // pChannel trimmed and than not empty, set pChannel;
- if ( ($pChannel = trim($pChannel)) != "") $this->lChannel = $pChannel;
- }
- /**
- * Returns the current playing channel. This is red form the vlc settings file.
- * @return string
- */
- public function getCurrentChannel() {
- return $this->lChannel;
- }
- /**
- * Set the VLC Server encoding audio codec. This sets the output audio codec.
- * @param string $pCodec
- */
- public function setAudioCodec($pCodec) {
- if ( ($pCodec = trim($pCodec)) != "") $this->lAudioCodec = $pCodec;
- }
- /**
- * Set the VLC Server encoding video codec. This sets the output video codec.
- * @param string $pCodec
- */
- public function setVideoCodec($pCodec) {
- if ( ($pCodec = trim($pCodec)) != "") $this->lVideoCodec = $pCodec;
- }
- /**
- * Set the VLC Server frames per second.
- * @param string $pFPS
- */
- public function setFPS($pFPS) {
- if ( is_numeric($pFPS = trim($pFPS)) ) $this->lFPS = $pFPS;
- }
- /**
- * Set the VLC Server stream type. This sets also the streamplayer type when you need the embedded player.
- * @param string $pStreamType
- */
- public function setStreamType($pStreamType) {
- if ( ($pStreamType = trim($pStreamType)) != "") $this->lStreamType = $pStreamType;
- }
- /**
- * Set the VLC Server streaming video bitrate. The higher the bitrate, the better the image qualitiy will be.
- * @param string $pRate
- */
- public function setBitrate($pRate) {
- if ( is_numeric($pRate = trim($pRate)) ) $this->lBitrate = $pRate;
- }
- /**
- * Set the VLC Server streaming audio bitrate. The higher the bitrate, the better the audio qualitiy will be.
- * @param string $pRate
- */
- public function setAudioBitrate($pRate) {
- if ( is_numeric($pRate = trim($pRate)) ) $this->lAudioBitrate = $pRate;
- }
- /**
- * Get the return type code for the code that generates the embed player. If it is rtmp, which is flash, it should be done with javascript. Else you could just send back the html code
- * @return string
- */
- public function getEmbbedType() {
- if ($this->lStreamType == "rtmp") {
- return "javascript";
- } else {
- return "normal";
- }
- }
- /**
- * Set the current settings to the page with javascript. This will generate a bounche of jQuery actions that set the bitrares, boutique and channel etc.
- * @return string
- */
- public function getJSSettings() {
- $lReturnValue = "jQuery('#resolution').val('". $this->lDimensions[0] . "x" . $this->lDimensions[1] . "');
- jQuery('#fps').val('". $this->lFPS . "');
- jQuery('#bitrate').val('". $this->lBitrate . "');
- jQuery('#video').val('". $this->lVideoCodec . "');
- jQuery('#audio').val('". $this->lAudioCodec . "');
- jQuery('#transport').val('". $this->lStreamType . "');";
- return $lReturnValue;
- }
- /**
- * Start the VLC Server by executing the vlc command line. You have to set all options before starting this server. If the server is started, you should be able to connect to it and watch some tv.
- * @param boolean $pDebug
- * @return string
- */
- public function startServer($pDebug = 0) {
- $this->stopServer();
- $lExtraUrl = "";
- if ($this->lAuthentication[0] != "" || $this->lAuthentication[1] != "") {
- $lExtraUrl = $this->lAuthentication[0] . ":" . $this->lAuthentication[1] . "@";
- }
- switch ($this->lEnigmaVersion) {
- case "enigma1":
- if (substr($this->lChannel,-2) == "ts") { # Recording
- $recording = explode(":",$this->lChannel);
- $lStreamUrl = Settings::getStreamProtocol() . "://" . $lExtraUrl . Settings::getDreamboxIP() . ":31342". urldecode($recording[10]);
- } else{
- //Zap first
- $lZapUrl = Settings::getStreamProtocol() . "://" . $lExtraUrl . Settings::getDreamboxIP() . "/cgi-bin/zapTo?path=". $this->lChannel ."&curBouquet=0&curChannel=0";
- $Dummy = @fopen($lZapUrl,"r");
- sleep(1); // Wait for the dreambox to change channel
- $lStreamUrl = trim(file_get_contents(Settings::getStreamProtocol() . "://" . $lExtraUrl . Settings::getDreamboxIP() . "/video.m3u"));
- }
- break;
- case "enigma2":
- if (substr($this->lChannel,-2) == "ts") {
- $lStreamUrl = Settings::getStreamProtocol() . "://" . $lExtraUrl . Settings::getDreamboxIP() . "/file/?file=" . urlencode($this->lChannel);
- } else {
- $lStreamUrl = Settings::getStreamProtocol() . "://" . $lExtraUrl . Settings::getDreamboxIP() . ":8001/" . $this->lChannel;
- }
- break;
- }
- $lCMD = $this->lVLCExecutable . " -I dummy " . $lStreamUrl;
- if (Settings::getGgrabLocation() != "") {
- $lCMD = Settings::getGgrabLocation() . " -host " . Settings::getDreamboxIP() . " -q -o - -p 0x1006 0x1106 | " . $this->lVLCExecutable . " - ";
- }
- $lCMD .= " --sout=";
- if ($this->lStreamType == "rtmp") {
- // The order of params is not free. This order is needed!!!
- $lTransCode = "#transcode{venc=". $this->lVideoCodec . ",vcodec=". $this->lVideoCodec . ",vb=". $this->lBitrate . ",scale=". $this->lScale .",acodec=". $this->lAudioCodec .",ab=" . $this->lAudioBitrate . ",channels=". $this->lChannels .",samplerate=44100,deinterlace,width=". $this->lDimensions[0] .",height=". $this->lDimensions[1] . "}:rtp{dst=127.0.0.1,port-video=10000,port-audio=10002,sdp=http://". Settings::getVLCWanIP() .":". Settings::getVLCStreamPort() ."/restream.sdp}";
- } else {
- // TheYOSH-Update Should be tested first....
- $lTransCode = "#transcode{venc=". $this->lVideoCodec . ",vcodec=". $this->lVideoCodec . ",vb=". $this->lBitrate . ",scale=". $this->lScale .",acodec=". $this->lAudioCodec .",ab=" . $this->lAudioBitrate . ",channels=". $this->lChannels .",samplerate=44100,deinterlace,width=". $this->lDimensions[0] .",height=". $this->lDimensions[1];
- // $lTransCode = "#transcode{vcodec=". $this->lVideoCodec . ",acodec=". $this->lAudioCodec .",samplerate=44100,ab=" . $this->lAudioBitrate . ",vb=". $this->lBitrate . ",scale=". $this->lScale .",deinterlace,fps=". $this->lFPS .",channels=". $this->lChannels .",width=". $this->lDimensions[0] .",height=". $this->lDimensions[1];
- if($this->lStreamType == "rtp") {
- $lTransCode .= "}:rtp{dest=127.0.0.1,port-video=10000,port-audio=10002,sdp=rtsp://". Settings::getVLCWanIP() .":". Settings::getVLCStreamPort() ."/restream.sdp}";
- } else {
- switch ($this->lVideoCodec) {
- case "ASFH":
- $mux = "asfh";
- break;
- /*
- Disable VLC flash streaming..... should be able in the future... Now we have Wowza Media
- case "FLV1":
- $mux = "ffmpeg{mux=flv}";
- $lTransCode .= ",samplerate=44100";
- break;
- */
- default:
- $mux = "ts";
- break;
- }
- if ($this->lStreamType == "mmsh") {
- $mux = "asfh";
- }
- // Stream on all interfaces
- $lTransCode .= "}:std{access=". $this->lStreamType .",mux=". $mux .",url=:". Settings::getVLCStreamPort() . "} ";
- }
- }
- if (VLCServer::isLinux()) $lTransCode = "\"" . $lTransCode . "\"";
- $lCMD .= $lTransCode;
- if (VLCServer::isLinux()) {
- $lCMD .= " >>/dev/null 2>>/dev/null &";
- shell_exec($lCMD);
- } else {
- $WshShell = new COM("WScript.Shell");
- $oExec = $WshShell->Run("$lCMD", 3, false);
- }
- $this->saveSettingsToFile();
- if ($pDebug == 1) return $lCMD;
- }
- /**
- * Stop the VLC Server. This is handy to spare some system resources.
- */
- public function stopServer() {
- if (VLCServer::isLinux()) {
- $lCMD = "killall -Iq vlc";
- shell_exec($lCMD);
- } else {
- // But you need to download this one: http://www.beyondlogic.org/solutions/processutil/processutil.htm
- // And install it in a dir called apps in the restream root dir
- exec(getcwd() . "/apps/Process.exe -k vlc.exe");
- }
- }
- /**
- * Check if the server is still running.
- * @return boolean
- */
- public function isServerRunning() {
- if (VLCServer::isLinux()) {
- $lData = shell_exec("ps fax | grep vlc | grep -v grep | grep " . Settings::getDreamboxIP());
- return (strlen($lData) > 0);
- } else {
- // TODO Windows process check
- }
- }
- /**
- * Get the streaming url. This is based on the protocols and ip addresses.
- * @return string
- */
- public function getStreamingUrl() {
- $lStreamPort = Settings::getVLCWanStreamPort();
- $lStreamProtocol = Settings::getStreamProtocol();
- if ($this->lStreamType == "rtp" || $this->lStreamType == "rtmp") {
- $lStreamPort = Settings::getVLCStreamPort() . "/restream.sdp";
- if ($this->lStreamType == "rtp") $lStreamProtocol = "rtsp";
- else $lStreamProtocol = "http";
- } elseif ($this->lStreamType == "mmsh") {
- $lStreamProtocol = "mms";
- } elseif ($this->lVideoCodec == "FLV") {
- $lStreamPort .= "/stream.flv";
- } elseif ($this->lStreamType == "udp") {
- $lStreamProtocol = "udp";
- }
- return $lStreamProtocol . "://" . Settings::getVLCWanIP() . ":". $lStreamPort;
- }
- /**
- * This method writes the settings to a vlc settings file. These settings will be loaded later to show what is currently played. Returns tru on success
- * @return boolean
- */
- public function saveSettingsToFile() {
- $lContent = "resolution=" . $this->lDimensions[0] . "x" . $this->lDimensions[1] . "\n";
- $lContent .= "fps=" . $this->lFPS . "\n";
- $lContent .= "bitrate=" . $this->lBitrate . "\n";
- $lContent .= "videocodec=" . $this->lVideoCodec . "\n";
- $lContent .= "audiocodec=" . $this->lAudioCodec . "\n";
- $lContent .= "transport=" . $this->lStreamType . "\n";
- $lContent .= "channel=" . $this->lChannel . "\n";
- return (file_put_contents($this->lSettingsFile,$lContent) === false);
- }
- /**
- * This method will load the vlc server settings file. This file contains information about the current stream that is being transcoded. When the file is not available, notting will be set. Return true when all settings are loadded!
- * @return boolean
- */
- public function getCurrentStream() {
- if ($this->isServerRunning()) {
- $lSettingsContent = @file_get_contents($this->lSettingsFile);
- $lSettingsContent = explode("\n",$lSettingsContent);
- $lTeller = 0;
- foreach ($lSettingsContent as $lSettingsLine) {
- $lSettingsLine = explode("=",$lSettingsLine);
- switch($lSettingsLine[0]) {
- case "resolution":
- $this->lDimensions = explode("x",trim($lSettingsLine[1]));
- $lTeller++;
- break;
- case "fps":
- $this->lFPS = trim($lSettingsLine[1]);
- $lTeller++;
- break;
- case "bitrate":
- $this->lBitrate = trim($lSettingsLine[1]);
- $lTeller++;
- break;
- case "videocodec":
- $this->lVideoCodec = trim($lSettingsLine[1]);
- $lTeller++;
- break;
- case "audiocodec":
- $this->lAudioCodec = trim($lSettingsLine[1]);
- $lTeller++;
- break;
- case "transport":
- $this->lStreamType = trim($lSettingsLine[1]);
- $lTeller++;
- break;
- case "channel":
- $this->lChannel = trim($lSettingsLine[1]);
- $lTeller++;
- break;
- }
- }
- }
- return ($lTeller == 7);
- }
- /**
- * Get the HTML code for the embeded player. This code is based on the chosen protocols, and encoding settings.
- * @return string
- */
- public function getEmbeddedPlayer() {
- $lReturnValue = "";
- $lStreamPort = Settings::getVLCWanStreamPort();
- $lStreamProtocol = Settings::getStreamProtocol();
- if ($this->lStreamType == "mmsh") {
- $lReturnValue = "<object id = \"MediaPlayer\" width = ". $this->lDimensions[0] ." height = ". ($this->lDimensions[1] + 80 )." classid = \"CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95\" codebase = \"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902\" standby = \"Starting restreaming... please wait\" type = \"application/x-oleobject\">\n";
- $lReturnValue .= "<param name = \"FileName\" value = \"" . $this->getStreamingUrl() ." \">\n";
- $lReturnValue .= "<param name = \"animationatStart\" value = \"true\">\n";
- $lReturnValue .= "<param name = \"transparentatStart\" value = \"true\">\n";
- $lReturnValue .= "<param name = \"autoStart\" value = \"true\">\n";
- $lReturnValue .= "<param name = \"showControls\" value = \"1\">\n";
- $lReturnValue .= "<param name = \"ShowDisplay\" value = \"0\">\n";
- $lReturnValue .= "<param name = \"ShowStatusBar\" value = \"1\">\n";
- $lReturnValue .= "<embed type=\"application/x-mplayer2\" pluginspage = \"http://www.microsoft.com/Windows/MediaPlayer/\" src=\"" . $this->getStreamingUrl() . "\" name=\"MediaPlayer\" width = ". $this->lDimensions[0] ." height = ". ($this->lDimensions[1] + 80 ) ." autostart = \"true\" showcontrols = \"1\" showdisplay = \"0\" showstatusbar = \"1\" style=\"z-index: 1;\"></embed>\n";
- $lReturnValue .= "</object>\n";
- //} elseif ($this->lVideoCodec == "FLV1") {
- // $lReturnValue = "var so = new SWFObject('flash/flvplayer.swf','player','300','210','8','#ffffff');
- // so.addParam('allowfullscreen','true');
- // so.addVariable('file','" . $this->getStreamingUrl() . "');
- // so.write('PlayerDiv');";
- } elseif ($this->lStreamType == "rtmp") {
- $lReturnValue = "var so = new SWFObject('mediaplayer.swf','player','" . $this->lDimensions[0] . "','" . ($this->lDimensions[1] + 20) . "','8','#ffffff');
- so.addParam('allowfullscreen','true');
- so.addParam('allowscriptaccess','always');
- so.addVariable('height','" . ($this->lDimensions[1] + 20) . "');
- so.addVariable('width','" . $this->lDimensions[0] . "');
- so.addVariable('file','rtmp://". Settings::getVLCWanIP() . "/rtplive');
- so.addVariable('id','rtplive: " . $this->getStreamingUrl() . "');
- so.addVariable('searchbar','false');
- so.addVariable('autostart','true');
- so.write('PlayerDiv');";
- } elseif (Utils::isFF()) {
- $lReturnValue = "<embed type=\"application/x-vlc-plugin\" name=\"vlc\" autoplay=\"yes\" loop=\"no\" height=\"". $this->lDimensions[1] ."\" width=\"". $this->lDimensions[0] ."\" target=\"". $this->getStreamingUrl() ."\" id=\"vlcplayer\" />";
- } else {
- $lReturnValue = "ERROR";
- }
- return trim($lReturnValue);
- }
- /**
- * Get the external stream data. This will return a m3u playlist file of a asx file for Media Player. This is based on the input streaming url.
- * @param string $pStreamingUrl
- * @return string
- */
- public function openExternal($pStreamingUrl) {
- // Start the current running stream in an external player
- if ((stripos($pStreamingUrl,"http://") !== false) || (stripos($pStreamingUrl,"rtsp://") !== false) || (stripos($pStreamingUrl,"udp://") !== false)) {
- header("Content-type: application/octet-stream");
- header("Content-Disposition: attachment; filename=externalstream.m3u");
- header("Content-Transfer-Encoding: binary");
- echo $pStreamingUrl;
- }elseif(stripos($pStreamingUrl,"mms://") !== false) {
- header("Content-type: video/x-ms-asf");
- header("Content-Disposition: attachment; filename=externalstream.asx");
- echo "<Asx Version = \"3.0\" >
- <Title >" . Settings::getProgramName() . "</Title>
- <Entry>
- <Param Name = \"MediaType\" Value = \"video\" />
- <Param Name = \"SourceURL\" Value = \"" . $pStreamingUrl . "\" />
- <Param Name = \"type\" Value = \"broadcast\" />
- <Title >" . Settings::getProgramName() . "</Title>
- <Ref href = \"" . $pStreamingUrl . "\"/>
- </Entry>
- </Asx>";
- }
- exit;
- }
- /**
- * Check if we are running on Linux.
- * @return boolean
- */
- static private function isLinux() {
- return stripos(php_uname(),"Linux") !== false;
- }
- }
- ?>
Documentation generated on Tue, 24 Jun 2008 18:59:41 +0200 by phpDocumentor 1.3.0RC3