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 $lVolume;
  52.  
  53. /**
  54. * @access private
  55. * @var int
  56. */
  57. private $lScale;
  58.  
  59. /**
  60. * @access private
  61. * @var string
  62. */
  63. private $lVLCExecutable;
  64.  
  65. /**
  66. * @access private
  67. * @var int
  68. */
  69. private $lChannels;
  70.  
  71. /**
  72. * @access private
  73. * @var string
  74. */
  75. private $lEnigmaVersion;
  76.  
  77. /**
  78. * Constructor. This creates a VLC Server object. Some defaults are already set. These are: volume, scale and audio channels. It alse detects when it is running on Windows or Linux.
  79. * @return VLCServer
  80. */
  81. function __construct($pEnigmaVersion = "enigma2") {
  82. $this->lDimensions = array();
  83. $this->lVLCExecutable = Settings::getVLCLocation();
  84. $this->lVolume = 60;
  85. $this->lScale = "0.50";
  86. $this->lChannels = 2;
  87. $this->setEnigmaVersion($pEnigmaVersion);
  88. }
  89.  
  90. /**
  91. * Set the Enigma type. Valid values are 'enigma1' and 'enigma2'.
  92. * @param string $pType
  93. * @return boolean
  94. */
  95. public function setEnigmaVersion($pType) {
  96. if ( ($pType == trim($pType)) == "") return false;
  97. switch ($pType) {
  98. case "enigma1":
  99. $this->lEnigmaVersion = "enigma1";
  100. break;
  101. case "enigma2";
  102. $this->lEnigmaVersion = "enigma2";
  103. break;
  104. default:
  105. return false;
  106. break;
  107. }
  108. return true;
  109. }
  110.  
  111. /**
  112. * Set the VLC Server encoding dimentions. This is the size of the re-encoded stream.
  113. * @param string $pSizes
  114. */
  115. public function setDimentions($pSizes) {
  116. $pSizes = explode("x",$pSizes);
  117. if (is_array($pSizes) && sizeof($pSizes) == 2 && is_numeric($pSizes[0]) && is_numeric($pSizes[1])) {
  118. $this->lDimensions = $pSizes;
  119. }
  120. }
  121.  
  122. /**
  123. * Set the VLC Server source channel. This is the ID of a Channel Object.
  124. * @param string $pChannel
  125. */
  126. public function setChannel($pChannel) {
  127. // pChannel trimmed and than not empty, set pChannel;
  128. if ( ($pChannel = trim($pChannel)) != "") $this->lChannel = $pChannel;
  129. }
  130.  
  131.  
  132. /**
  133. * Set the VLC Server encoding audio codec. This sets the output audio codec.
  134. * @param string $pCodec
  135. */
  136. public function setAudioCodec($pCodec) {
  137. if ( ($pCodec = trim($pCodec)) != "") $this->lAudioCodec = $pCodec;
  138. }
  139.  
  140. /**
  141. * Set the VLC Server encoding video codec. This sets the output video codec.
  142. * @param string $pCodec
  143. */
  144. public function setVideoCodec($pCodec) {
  145. if ( ($pCodec = trim($pCodec)) != "") $this->lVideoCodec = $pCodec;
  146. }
  147.  
  148. /**
  149. * Set the VLC Server frames per second.
  150. * @param string $pFPS
  151. */
  152. public function setFPS($pFPS) {
  153. if ( is_numeric($pFPS = trim($pFPS)) ) $this->lFPS = $pFPS;
  154. }
  155.  
  156. /**
  157. * Set the VLC Server stream type. This sets also the streamplayer type when you need the embedded player.
  158. * @param string $pStreamType
  159. */
  160. public function setStreamType($pStreamType) {
  161. if ( ($pStreamType = trim($pStreamType)) != "") $this->lStreamType = $pStreamType;
  162. }
  163.  
  164. /**
  165. * Set the VLC Server streaming video bitrate. The higher the bitrate, the better the image qualitiy will be.
  166. * @param string $pRate
  167. */
  168. public function setBitrate($pRate) {
  169. if ( is_numeric($pRate = trim($pRate)) ) $this->lBitrate = $pRate;
  170. }
  171.  
  172. /**
  173. * 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.
  174. * @param boolean $pDebug
  175. * @return string
  176. */
  177. public function startServer($pDebug = 0) {
  178. $this->stopServer();
  179.  
  180.  
  181. switch ($this->lEnigmaVersion) {
  182. case "enigma1":
  183. //Zap first
  184. $lZapUrl = Settings::getStreamProtocol() . "://" . Settings::getDreamboxIP() . "/cgi-bin/zapTo?path=". $this->lChannel ."&curBouquet=0&curChannel=0";
  185. @fopen($lZapUrl,"r");
  186. sleep(1);
  187.  
  188. $lStreamUrl = "/video.m3u";
  189. break;
  190.  
  191. case "enigma2":
  192. $lStreamUrl = ":8001/" . $this->lChannel;
  193. break;
  194. }
  195.  
  196.  
  197. $lCMD = $this->lVLCExecutable . " -I dummy " . Settings::getStreamProtocol() . "://" . Settings::getDreamboxIP() . $lStreamUrl;
  198. if ($this->lStreamType == "rtp") $lCMD .= " --rtsp-tcp";
  199. $lCMD .= " --sout=";
  200.  
  201. $lTransCode = "#transcode{acodec=". $this->lAudioCodec .",vcodec=". $this->lVideoCodec .",ab=64,vb=". $this->lBitrate . ",scale=". $this->lScale .",deinterlace,fps=". $this->lFPS .",channels=". $this->lChannels .",width=". $this->lDimensions[0] .",height=". $this->lDimensions[1];
  202.  
  203. if($this->lStreamType == "rtp") {
  204. $lTransCode .= "}:rtp{port=". Settings::getVLCStreamPort() .",sdp=rtsp://". Settings::getVLCLanIP() .":". Settings::getVLCControlPort() ."/restream.sdp}";
  205. } else {
  206.  
  207. switch ($this->lVideoCodec) {
  208. case "ASFH":
  209. $mux = "asfh";
  210. break;
  211. case "FLV1":
  212. $mux = "ffmpeg{mux=flv}";
  213. $lTransCode .= ",samplerate=44100";
  214. break;
  215. default:
  216. $mux = "ts";
  217. break;
  218. }
  219. if ($this->lStreamType == "mmsh") {
  220. $mux = "asfh";
  221. }
  222.  
  223. $lTransCode .= "}:std{access=". $this->lStreamType .",mux=". $mux .",url=". Settings::getVLCLanIP() .":". Settings::getVLCStreamPort() . "} ";
  224. }
  225.  
  226. if (VLCServer::isLinux()) $lTransCode = "\"" . $lTransCode . "\"";
  227. $lCMD .= $lTransCode;
  228.  
  229. if (VLCServer::isLinux()) {
  230. $lCMD .= " >>/dev/null 2>>/dev/null &";
  231. shell_exec($lCMD);
  232. } else {
  233. $WshShell = new COM("WScript.Shell");
  234. $oExec = $WshShell->Run($lCMD, 7, false);
  235. }
  236. if ($pDebug == 1) return $lCMD;
  237. }
  238.  
  239. /**
  240. * Stop the VLC Server. This is handy to spare some system resources.
  241. */
  242. public function stopServer() {
  243. if (VLCServer::isLinux()) {
  244. $lCMD = "killall -Iq vlc";
  245. shell_exec($lCMD);
  246. } else {
  247. // But you need to download this one: http://www.beyondlogic.org/solutions/processutil/processutil.htm
  248. exec("c:\process.exe -k vlc.exe");
  249. }
  250. }
  251.  
  252. /**
  253. * Check if the server is still running.
  254. * @return boolean
  255. */
  256. public function isServerRunning() {
  257. if (VLCServer::isLinux()) {
  258. $lData = shell_exec("ps fax | grep vlc | grep -v grep | grep " . Settings::getDreamboxIP());
  259. return (strlen($lData) > 0);
  260. } else {
  261. // TODO Windows process check
  262. }
  263. }
  264.  
  265. /**
  266. * Get the streaming url. This is based on the protocols and ip addresses.
  267. * @return string
  268. */
  269. public function getStreamingUrl() {
  270. $lStreamPort = Settings::getVLCWanStreamPort();
  271. $lStreamProtocol = Settings::getStreamProtocol();
  272. if ($this->lStreamType == "rtp") {
  273. $lStreamPort = Settings::getVLCControlPort() . "/restream.sdp";
  274. $lStreamProtocol = "rtsp";
  275. } elseif ($this->lStreamType == "mmsh") {
  276. $lStreamProtocol = "mms";
  277. } elseif ($this->lVideoCodec == "FLV") {
  278. $lStreamPort .= "/stream.flv";
  279. } elseif ($this->lStreamType == "udp") {
  280. $lStreamProtocol = "udp";
  281. }
  282. return $lStreamProtocol . "://" . Settings::getVLCWanIP() . ":". $lStreamPort;
  283. }
  284.  
  285. /**
  286. * Get the HTML code for the embeded player. This code is based on the chosen protocols, and encoding settings.
  287. * @return string
  288. */
  289. public function getEmbeddedPlayer() {
  290. $lReturnValue = "";
  291. $lStreamPort = Settings::getVLCWanStreamPort();
  292. $lStreamProtocol = Settings::getStreamProtocol();
  293. if ($this->lStreamType == "rtp") {
  294. $lStreamPort = Settings::getVLCControlPort() . "/restream.sdp";
  295. $lStreamProtocol = "rtsp";
  296. } elseif ($this->lStreamType == "mmsh") {
  297. $lStreamProtocol = "mms";
  298. }
  299.  
  300. if ($this->lStreamType == "mmsh") {
  301. $lReturnValue = "<object id = \"MediaPlayer\" width = ". $this->lDimensions[0] ." height = ". ($this->lDimensions[1] + 68 )." 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";
  302. $lReturnValue .= "<param name = \"FileName\" value = \"" . $this->getStreamingUrl() ." \">\n";
  303. $lReturnValue .= "<param name = \"animationatStart\" value = \"true\">\n";
  304. $lReturnValue .= "<param name = \"transparentatStart\" value = \"true\">\n";
  305. $lReturnValue .= "<param name = \"autoStart\" value = \"true\">\n";
  306. $lReturnValue .= "<param name = \"showControls\" value = \"1\">\n";
  307. $lReturnValue .= "<param name = \"ShowDisplay\" value = \"0\">\n";
  308. $lReturnValue .= "<param name = \"ShowStatusBar\" value = \"1\">\n";
  309. $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] + 68 ) ." autostart = \"true\" showcontrols = \"1\" showdisplay = \"0\" showstatusbar = \"1\" style=\"z-index: 1;\"></embed>\n";
  310. $lReturnValue .= "</object>\n";
  311. } elseif ($this->lVideoCodec == "FLV1") {
  312. $lReturnValue = "var so = new SWFObject('flash/flvplayer.swf','player','300','210','8','#ffffff');
  313. so.addParam('allowfullscreen','true');
  314. so.addVariable('file','" . $this->getStreamingUrl() . "');
  315. so.write('PlayerDiv');";
  316. } elseif (Utils::isFF()) {
  317. $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\" />";
  318. } else {
  319. $lReturnValue = "ERROR";
  320. }
  321. return trim($lReturnValue);
  322. }
  323.  
  324. /**
  325. * 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.
  326. * @param string $pStreamingUrl
  327. * @return string
  328. */
  329. public function openExternal($pStreamingUrl) {
  330. // Start the current running stream in an external player
  331. if ((stripos($pStreamingUrl,"http://") !== false) || (stripos($pStreamingUrl,"rtsp://") !== false) || (stripos($pStreamingUrl,"udp://") !== false)) {
  332. header("Content-type: application/octet-stream");
  333. header("Content-Disposition: attachment; filename=externalstream.m3u");
  334. header("Content-Transfer-Encoding: binary");
  335. echo $pStreamingUrl;
  336. }elseif(stripos($pStreamingUrl,"mms://") !== false) {
  337. header("Content-type: video/x-ms-asf");
  338. header("Content-Disposition: attachment; filename=externalstream.asx");
  339. echo "<Asx Version = \"3.0\" >
  340. <Title >" . Settings::getProgramName() . "</Title>
  341. <Entry>
  342. <Param Name = \"MediaType\" Value = \"video\" />
  343. <Param Name = \"SourceURL\" Value = \"" . $pStreamingUrl . "\" />
  344. <Param Name = \"type\" Value = \"broadcast\" />
  345. <Title >" . Settings::getProgramName() . "</Title>
  346. <Ref href = \"" . $pStreamingUrl . "\"/>
  347. </Entry>
  348. </Asx>";
  349. }
  350. exit;
  351. }
  352.  
  353. /**
  354. * Check if we are running on Linux.
  355. * @return boolean
  356. */
  357. static private function isLinux() {
  358. return stripos(php_uname(),"Linux") !== false;
  359. }
  360.  
  361. }
  362. ?>

Documentation generated on Sat, 19 Jan 2008 12:56:21 +0100 by phpDocumentor 1.3.0RC3