Source for file VLCServer.class.php

Documentation is available at VLCServer.class.php

  1. <?php
  2.  
  3. class VLCServer {
  4.  
  5. /**
  6. * @access private
  7. * @var array
  8. */
  9. private $lDimensions;
  10.  
  11. /**
  12. * @access private
  13. * @var string
  14. */
  15. private $lChannel;
  16.  
  17. /**
  18. * @access private
  19. * @var string
  20. */
  21. private $lAudioCodec;
  22.  
  23. /**
  24. * @access private
  25. * @var string
  26. */
  27. private $lVideoCodec;
  28.  
  29. /**
  30. * @access private
  31. * @var int
  32. */
  33. private $lFPS;
  34.  
  35. /**
  36. * @access private
  37. * @var string
  38. */
  39. private $lStreamType;
  40.  
  41. /**
  42. * @access private
  43. * @var int
  44. */
  45. private $lBitrate;
  46.  
  47. /**
  48. * @access private
  49. * @var int
  50. */
  51. private $lAudioBitrate;
  52.  
  53. /**
  54. * @access private
  55. * @var int
  56. */
  57. private $lVolume;
  58.  
  59. /**
  60. * @access private
  61. * @var int
  62. */
  63. private $lScale;
  64.  
  65. /**
  66. * @access private
  67. * @var string
  68. */
  69. private $lVLCExecutable;
  70.  
  71. /**
  72. * @access private
  73. * @var int
  74. */
  75. private $lChannels;
  76.  
  77. /**
  78. * @access private
  79. * @var string
  80. */
  81. private $lEnigmaVersion;
  82.  
  83. /**
  84. * @access private
  85. * @var array
  86. */
  87. private $lAuthentication;
  88.  
  89. /**
  90. * @access private
  91. * @var string
  92. */
  93. private $lSettingsFile;
  94.  
  95. /**
  96. * 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.
  97. * @return VLCServer
  98. */
  99. function __construct($pEnigmaVersion = "enigma2", $pAuthentication = array()) {
  100. $this->lDimensions = array();
  101. $this->lVLCExecutable = Settings::getVLCLocation(($this->isLinux()? 0 : 1));
  102. $this->lVolume = 60;
  103. $this->lScale = "1";
  104. $this->lChannels = 2;
  105. $this->setEnigmaVersion($pEnigmaVersion);
  106. $this->lAuthentication = $pAuthentication;
  107. $this->lSettingsFile = Settings::getVLCSettingsFile();
  108. }
  109.  
  110. /**
  111. * Set the Enigma type. Valid values are 'enigma1' and 'enigma2'.
  112. * @param string $pType
  113. * @return boolean
  114. */
  115. public function setEnigmaVersion($pType) {
  116. if ( ($pType == trim($pType)) == "") return false;
  117. switch ($pType) {
  118. case "enigma1":
  119. $this->lEnigmaVersion = "enigma1";
  120. break;
  121. case "enigma2";
  122. $this->lEnigmaVersion = "enigma2";
  123. break;
  124. default:
  125. return false;
  126. break;
  127. }
  128. return true;
  129. }
  130.  
  131. /**
  132. * Set the VLC Server encoding dimentions. This is the size of the re-encoded stream.
  133. * @param string $pSizes
  134. */
  135. public function setDimentions($pSizes) {
  136. $pSizes = explode("x",$pSizes);
  137. if (is_array($pSizes) && sizeof($pSizes) == 2 && is_numeric($pSizes[0]) && is_numeric($pSizes[1])) {
  138. $this->lDimensions = $pSizes;
  139. }
  140. }
  141.  
  142. /**
  143. * Set the VLC Server source channel. This is the ID of a Channel Object.
  144. * @param string $pChannel
  145. */
  146. public function setChannel($pChannel) {
  147. // pChannel trimmed and than not empty, set pChannel;
  148. if ( ($pChannel = trim($pChannel)) != "") $this->lChannel = $pChannel;
  149. }
  150.  
  151. /**
  152. * Returns the current playing channel. This is red form the vlc settings file.
  153. * @return string
  154. */
  155. public function getCurrentChannel() {
  156. return $this->lChannel;
  157. }
  158.  
  159. /**
  160. * Set the VLC Server encoding audio codec. This sets the output audio codec.
  161. * @param string $pCodec
  162. */
  163. public function setAudioCodec($pCodec) {
  164. if ( ($pCodec = trim($pCodec)) != "") $this->lAudioCodec = $pCodec;
  165. }
  166.  
  167. /**
  168. * Set the VLC Server encoding video codec. This sets the output video codec.
  169. * @param string $pCodec
  170. */
  171. public function setVideoCodec($pCodec) {
  172. if ( ($pCodec = trim($pCodec)) != "") $this->lVideoCodec = $pCodec;
  173. }
  174.  
  175. /**
  176. * Set the VLC Server frames per second.
  177. * @param string $pFPS
  178. */
  179. public function setFPS($pFPS) {
  180. if ( is_numeric($pFPS = trim($pFPS)) ) $this->lFPS = $pFPS;
  181. }
  182.  
  183. /**
  184. * Set the VLC Server stream type. This sets also the streamplayer type when you need the embedded player.
  185. * @param string $pStreamType
  186. */
  187. public function setStreamType($pStreamType) {
  188. if ( ($pStreamType = trim($pStreamType)) != "") $this->lStreamType = $pStreamType;
  189. }
  190.  
  191. /**
  192. * Set the VLC Server streaming video bitrate. The higher the bitrate, the better the image qualitiy will be.
  193. * @param string $pRate
  194. */
  195. public function setBitrate($pRate) {
  196. if ( is_numeric($pRate = trim($pRate)) ) $this->lBitrate = $pRate;
  197. }
  198.  
  199. /**
  200. * Set the VLC Server streaming audio bitrate. The higher the bitrate, the better the audio qualitiy will be.
  201. * @param string $pRate
  202. */
  203. public function setAudioBitrate($pRate) {
  204. if ( is_numeric($pRate = trim($pRate)) ) $this->lAudioBitrate = $pRate;
  205. }
  206.  
  207. /**
  208. * 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
  209. * @return string
  210. */
  211. public function getEmbbedType() {
  212. if ($this->lStreamType == "rtmp") {
  213. return "javascript";
  214. } else {
  215. return "normal";
  216. }
  217. }
  218.  
  219. /**
  220. * 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.
  221. * @return string
  222. */
  223. public function getJSSettings() {
  224. $lReturnValue = "jQuery('#resolution').val('". $this->lDimensions[0] . "x" . $this->lDimensions[1] . "');
  225. jQuery('#fps').val('". $this->lFPS . "');
  226. jQuery('#bitrate').val('". $this->lBitrate . "');
  227. jQuery('#video').val('". $this->lVideoCodec . "');
  228. jQuery('#audio').val('". $this->lAudioCodec . "');
  229. jQuery('#transport').val('". $this->lStreamType . "');";
  230. return $lReturnValue;
  231. }
  232. /**
  233. * 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.
  234. * @param boolean $pDebug
  235. * @return string
  236. */
  237. public function startServer($pDebug = 0) {
  238. $this->stopServer();
  239.  
  240. $lExtraUrl = "";
  241. if ($this->lAuthentication[0] != "" || $this->lAuthentication[1] != "") {
  242. $lExtraUrl = $this->lAuthentication[0] . ":" . $this->lAuthentication[1] . "@";
  243. }
  244. switch ($this->lEnigmaVersion) {
  245. case "enigma1":
  246. if (substr($this->lChannel,-2) == "ts") { # Recording
  247. $recording = explode(":",$this->lChannel);
  248. $lStreamUrl = Settings::getStreamProtocol() . "://" . $lExtraUrl . Settings::getDreamboxIP() . ":31342". urldecode($recording[10]);
  249.  
  250. } else{
  251. //Zap first
  252. $lZapUrl = Settings::getStreamProtocol() . "://" . $lExtraUrl . Settings::getDreamboxIP() . "/cgi-bin/zapTo?path=". $this->lChannel ."&curBouquet=0&curChannel=0";
  253. $Dummy = @fopen($lZapUrl,"r");
  254. sleep(1); // Wait for the dreambox to change channel
  255.  
  256. $lStreamUrl = trim(file_get_contents(Settings::getStreamProtocol() . "://" . $lExtraUrl . Settings::getDreamboxIP() . "/video.m3u"));
  257. }
  258. break;
  259.  
  260. case "enigma2":
  261. if (substr($this->lChannel,-2) == "ts") {
  262. $lStreamUrl = Settings::getStreamProtocol() . "://" . $lExtraUrl . Settings::getDreamboxIP() . "/file/?file=" . urlencode($this->lChannel);
  263. } else {
  264. $lStreamUrl = Settings::getStreamProtocol() . "://" . $lExtraUrl . Settings::getDreamboxIP() . ":8001/" . $this->lChannel;
  265. }
  266. break;
  267. }
  268.  
  269.  
  270. $lCMD = $this->lVLCExecutable . " -I dummy " . $lStreamUrl;
  271.  
  272. if (Settings::getGgrabLocation() != "") {
  273. $lCMD = Settings::getGgrabLocation() . " -host " . Settings::getDreamboxIP() . " -q -o - -p 0x1006 0x1106 | " . $this->lVLCExecutable . " - ";
  274. }
  275. $lCMD .= " --sout=";
  276.  
  277. if ($this->lStreamType == "rtmp") {
  278. // The order of params is not free. This order is needed!!!
  279. $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}";
  280. } else {
  281. // TheYOSH-Update Should be tested first....
  282. $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];
  283. // $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];
  284.  
  285. if($this->lStreamType == "rtp") {
  286. $lTransCode .= "}:rtp{dest=127.0.0.1,port-video=10000,port-audio=10002,sdp=rtsp://". Settings::getVLCWanIP() .":". Settings::getVLCStreamPort() ."/restream.sdp}";
  287. } else {
  288.  
  289. switch ($this->lVideoCodec) {
  290. case "ASFH":
  291. $mux = "asfh";
  292. break;
  293. /*
  294. Disable VLC flash streaming..... should be able in the future... Now we have Wowza Media
  295. case "FLV1":
  296. $mux = "ffmpeg{mux=flv}";
  297. $lTransCode .= ",samplerate=44100";
  298. break;
  299. */
  300. default:
  301. $mux = "ts";
  302. break;
  303. }
  304. if ($this->lStreamType == "mmsh") {
  305. $mux = "asfh";
  306. }
  307. // Stream on all interfaces
  308. $lTransCode .= "}:std{access=". $this->lStreamType .",mux=". $mux .",url=:". Settings::getVLCStreamPort() . "} ";
  309. }
  310. }
  311.  
  312. if (VLCServer::isLinux()) $lTransCode = "\"" . $lTransCode . "\"";
  313. $lCMD .= $lTransCode;
  314.  
  315. if (VLCServer::isLinux()) {
  316. $lCMD .= " >>/dev/null 2>>/dev/null &";
  317. shell_exec($lCMD);
  318. } else {
  319. $WshShell = new COM("WScript.Shell");
  320. $oExec = $WshShell->Run("$lCMD", 3, false);
  321. }
  322. $this->saveSettingsToFile();
  323. if ($pDebug == 1) return $lCMD;
  324. }
  325.  
  326. /**
  327. * Stop the VLC Server. This is handy to spare some system resources.
  328. */
  329. public function stopServer() {
  330. if (VLCServer::isLinux()) {
  331. $lCMD = "killall -Iq vlc";
  332. shell_exec($lCMD);
  333. } else {
  334. // But you need to download this one: http://www.beyondlogic.org/solutions/processutil/processutil.htm
  335. // And install it in a dir called apps in the restream root dir
  336. exec(getcwd() . "/apps/Process.exe -k vlc.exe");
  337. }
  338. }
  339.  
  340. /**
  341. * Check if the server is still running.
  342. * @return boolean
  343. */
  344. public function isServerRunning() {
  345. if (VLCServer::isLinux()) {
  346. $lData = shell_exec("ps fax | grep vlc | grep -v grep | grep " . Settings::getDreamboxIP());
  347. return (strlen($lData) > 0);
  348. } else {
  349. // TODO Windows process check
  350. }
  351. }
  352.  
  353. /**
  354. * Get the streaming url. This is based on the protocols and ip addresses.
  355. * @return string
  356. */
  357. public function getStreamingUrl() {
  358. $lStreamPort = Settings::getVLCWanStreamPort();
  359. $lStreamProtocol = Settings::getStreamProtocol();
  360. if ($this->lStreamType == "rtp" || $this->lStreamType == "rtmp") {
  361. $lStreamPort = Settings::getVLCStreamPort() . "/restream.sdp";
  362. if ($this->lStreamType == "rtp") $lStreamProtocol = "rtsp";
  363. else $lStreamProtocol = "http";
  364. } elseif ($this->lStreamType == "mmsh") {
  365. $lStreamProtocol = "mms";
  366. } elseif ($this->lVideoCodec == "FLV") {
  367. $lStreamPort .= "/stream.flv";
  368. } elseif ($this->lStreamType == "udp") {
  369. $lStreamProtocol = "udp";
  370. }
  371. return $lStreamProtocol . "://" . Settings::getVLCWanIP() . ":". $lStreamPort;
  372. }
  373.  
  374. /**
  375. * 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
  376. * @return boolean
  377. */
  378. public function saveSettingsToFile() {
  379. $lContent = "resolution=" . $this->lDimensions[0] . "x" . $this->lDimensions[1] . "\n";
  380. $lContent .= "fps=" . $this->lFPS . "\n";
  381. $lContent .= "bitrate=" . $this->lBitrate . "\n";
  382. $lContent .= "videocodec=" . $this->lVideoCodec . "\n";
  383. $lContent .= "audiocodec=" . $this->lAudioCodec . "\n";
  384. $lContent .= "transport=" . $this->lStreamType . "\n";
  385. $lContent .= "channel=" . $this->lChannel . "\n";
  386. return (file_put_contents($this->lSettingsFile,$lContent) === false);
  387. }
  388.  
  389. /**
  390. * 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!
  391. * @return boolean
  392. */
  393. public function getCurrentStream() {
  394. if ($this->isServerRunning()) {
  395. $lSettingsContent = @file_get_contents($this->lSettingsFile);
  396. $lSettingsContent = explode("\n",$lSettingsContent);
  397. $lTeller = 0;
  398. foreach ($lSettingsContent as $lSettingsLine) {
  399. $lSettingsLine = explode("=",$lSettingsLine);
  400. switch($lSettingsLine[0]) {
  401. case "resolution":
  402. $this->lDimensions = explode("x",trim($lSettingsLine[1]));
  403. $lTeller++;
  404. break;
  405. case "fps":
  406. $this->lFPS = trim($lSettingsLine[1]);
  407. $lTeller++;
  408. break;
  409. case "bitrate":
  410. $this->lBitrate = trim($lSettingsLine[1]);
  411. $lTeller++;
  412. break;
  413. case "videocodec":
  414. $this->lVideoCodec = trim($lSettingsLine[1]);
  415. $lTeller++;
  416. break;
  417. case "audiocodec":
  418. $this->lAudioCodec = trim($lSettingsLine[1]);
  419. $lTeller++;
  420. break;
  421. case "transport":
  422. $this->lStreamType = trim($lSettingsLine[1]);
  423. $lTeller++;
  424. break;
  425. case "channel":
  426. $this->lChannel = trim($lSettingsLine[1]);
  427. $lTeller++;
  428. break;
  429.  
  430. }
  431. }
  432. }
  433. return ($lTeller == 7);
  434. }
  435.  
  436. /**
  437. * Get the HTML code for the embeded player. This code is based on the chosen protocols, and encoding settings.
  438. * @return string
  439. */
  440. public function getEmbeddedPlayer() {
  441. $lReturnValue = "";
  442. $lStreamPort = Settings::getVLCWanStreamPort();
  443. $lStreamProtocol = Settings::getStreamProtocol();
  444. if ($this->lStreamType == "mmsh") {
  445. $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";
  446.  
  447. $lReturnValue .= "<param name = \"FileName\" value = \"" . $this->getStreamingUrl() ." \">\n";
  448. $lReturnValue .= "<param name = \"animationatStart\" value = \"true\">\n";
  449. $lReturnValue .= "<param name = \"transparentatStart\" value = \"true\">\n";
  450. $lReturnValue .= "<param name = \"autoStart\" value = \"true\">\n";
  451. $lReturnValue .= "<param name = \"showControls\" value = \"1\">\n";
  452. $lReturnValue .= "<param name = \"ShowDisplay\" value = \"0\">\n";
  453. $lReturnValue .= "<param name = \"ShowStatusBar\" value = \"1\">\n";
  454. $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";
  455. $lReturnValue .= "</object>\n";
  456. //} elseif ($this->lVideoCodec == "FLV1") {
  457. // $lReturnValue = "var so = new SWFObject('flash/flvplayer.swf','player','300','210','8','#ffffff');
  458. // so.addParam('allowfullscreen','true');
  459. // so.addVariable('file','" . $this->getStreamingUrl() . "');
  460. // so.write('PlayerDiv');";
  461. } elseif ($this->lStreamType == "rtmp") {
  462. $lReturnValue = "var so = new SWFObject('mediaplayer.swf','player','" . $this->lDimensions[0] . "','" . ($this->lDimensions[1] + 20) . "','8','#ffffff');
  463. so.addParam('allowfullscreen','true');
  464. so.addParam('allowscriptaccess','always');
  465. so.addVariable('height','" . ($this->lDimensions[1] + 20) . "');
  466. so.addVariable('width','" . $this->lDimensions[0] . "');
  467. so.addVariable('file','rtmp://". Settings::getVLCWanIP() . "/rtplive');
  468. so.addVariable('id','rtplive: " . $this->getStreamingUrl() . "');
  469. so.addVariable('searchbar','false');
  470. so.addVariable('autostart','true');
  471. so.write('PlayerDiv');";
  472. } elseif (Utils::isFF()) {
  473. $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\" />";
  474. } else {
  475. $lReturnValue = "ERROR";
  476. }
  477. return trim($lReturnValue);
  478. }
  479.  
  480. /**
  481. * 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.
  482. * @param string $pStreamingUrl
  483. * @return string
  484. */
  485. public function openExternal($pStreamingUrl) {
  486. // Start the current running stream in an external player
  487. if ((stripos($pStreamingUrl,"http://") !== false) || (stripos($pStreamingUrl,"rtsp://") !== false) || (stripos($pStreamingUrl,"udp://") !== false)) {
  488. header("Content-type: application/octet-stream");
  489. header("Content-Disposition: attachment; filename=externalstream.m3u");
  490. header("Content-Transfer-Encoding: binary");
  491. echo $pStreamingUrl;
  492. }elseif(stripos($pStreamingUrl,"mms://") !== false) {
  493. header("Content-type: video/x-ms-asf");
  494. header("Content-Disposition: attachment; filename=externalstream.asx");
  495. echo "<Asx Version = \"3.0\" >
  496. <Title >" . Settings::getProgramName() . "</Title>
  497. <Entry>
  498. <Param Name = \"MediaType\" Value = \"video\" />
  499. <Param Name = \"SourceURL\" Value = \"" . $pStreamingUrl . "\" />
  500. <Param Name = \"type\" Value = \"broadcast\" />
  501. <Title >" . Settings::getProgramName() . "</Title>
  502. <Ref href = \"" . $pStreamingUrl . "\"/>
  503. </Entry>
  504. </Asx>";
  505. }
  506. exit;
  507. }
  508.  
  509. /**
  510. * Check if we are running on Linux.
  511. * @return boolean
  512. */
  513. static private function isLinux() {
  514. return stripos(php_uname(),"Linux") !== false;
  515. }
  516. }
  517. ?>

Documentation generated on Tue, 24 Jun 2008 18:59:41 +0200 by phpDocumentor 1.3.0RC3