Vlc.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /* $Id: Vlc.php 2832 2007-04-08 10:31:44Z b4rt $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. /**
  16. * Vlc
  17. */
  18. class Vlc
  19. {
  20. // public fields
  21. // lists
  22. var $lists = array(
  23. 'vidc' => array(
  24. 'DIV3',
  25. 'DIV4',
  26. 'WMV1',
  27. 'WMV2',
  28. 'RV10',
  29. 'mp1v',
  30. 'mp4v'
  31. ),
  32. 'vbit' => array(
  33. '192',
  34. '256',
  35. '384',
  36. '512',
  37. '768',
  38. '1024',
  39. '1280',
  40. '1536',
  41. '1792',
  42. '2048'
  43. ),
  44. 'audc' => array(
  45. 'mp3',
  46. 'mp4a',
  47. 'mpga',
  48. 'vorb',
  49. 'flac'
  50. ),
  51. 'abit' => array(
  52. '64',
  53. '96',
  54. '128',
  55. '192',
  56. '256',
  57. '384'
  58. )
  59. );
  60. // addr
  61. var $addr = "";
  62. // ports
  63. var $port_default = 0;
  64. var $port = 0;
  65. // private fields
  66. // command
  67. var $_command;
  68. // =========================================================================
  69. // public static methods
  70. // =========================================================================
  71. /**
  72. * initialize Vlc.
  73. */
  74. function initialize() {
  75. global $instanceVlc;
  76. // create instance
  77. if (!isset($instanceVlc))
  78. $instanceVlc = new Vlc();
  79. }
  80. /**
  81. * getPort
  82. *
  83. * @return port
  84. */
  85. function getPort() {
  86. global $instanceVlc;
  87. // initialize if needed
  88. if (!isset($instanceVlc))
  89. Vlc::initialize();
  90. // return
  91. return $instanceVlc->port;
  92. }
  93. /**
  94. * getAddr
  95. *
  96. * @return port
  97. */
  98. function getAddr() {
  99. global $instanceVlc;
  100. // initialize if needed
  101. if (!isset($instanceVlc))
  102. Vlc::initialize();
  103. // return
  104. return $instanceVlc->addr;
  105. }
  106. /**
  107. * getList
  108. *
  109. * @param $type
  110. * @return array
  111. */
  112. function getList($type) {
  113. global $instanceVlc;
  114. // initialize if needed
  115. if (!isset($instanceVlc))
  116. Vlc::initialize();
  117. // return
  118. return (isset($instanceVlc->lists[$type]))
  119. ? $instanceVlc->lists[$type]
  120. : array();
  121. }
  122. /**
  123. * start a stream
  124. *
  125. * @param $file
  126. * @param $vidc
  127. * @param $vbit
  128. * @param $audc
  129. * @param $abit
  130. */
  131. function start($file, $vidc, $vbit, $audc, $abit) {
  132. global $instanceVlc;
  133. // initialize if needed
  134. if (!isset($instanceVlc))
  135. Vlc::initialize();
  136. // call instance-method
  137. $instanceVlc->instance_start($file, $vidc, $vbit, $audc, $abit);
  138. }
  139. /**
  140. * stop a (/all) stream(s)
  141. */
  142. function stop($port = 0) {
  143. global $instanceVlc;
  144. // initialize if needed
  145. if (!isset($instanceVlc))
  146. Vlc::initialize();
  147. // call instance-method
  148. $instanceVlc->instance_stop($port);
  149. }
  150. /**
  151. * get current running stream(s)
  152. *
  153. * @return array
  154. */
  155. function getRunning($port = 0) {
  156. global $instanceVlc;
  157. // initialize if needed
  158. if (!isset($instanceVlc))
  159. Vlc::initialize();
  160. // call instance-method
  161. return $instanceVlc->instance_getRunning($port);
  162. }
  163. /**
  164. * check if a stream is running on host/port
  165. *
  166. * @param $port
  167. * @return boolean
  168. */
  169. function isStreamRunning($port) {
  170. global $instanceVlc;
  171. // initialize if needed
  172. if (!isset($instanceVlc))
  173. Vlc::initialize();
  174. // call instance-method
  175. return $instanceVlc->instance_isStreamRunning($port);
  176. }
  177. // =========================================================================
  178. // ctor
  179. // =========================================================================
  180. /**
  181. * do not use direct, use the factory-methods !
  182. *
  183. * @return Vlc
  184. */
  185. function Vlc() {
  186. global $cfg;
  187. $this->addr = $_SERVER['SERVER_ADDR'];
  188. $this->port_default = $cfg['vlc_port'];
  189. $this->port = $this->port_default;
  190. }
  191. // =========================================================================
  192. // public methods
  193. // =========================================================================
  194. /**
  195. * start a stream
  196. *
  197. * @param $file
  198. * @param $vidc
  199. * @param $vbit
  200. * @param $audc
  201. * @param $abit
  202. */
  203. function instance_start($file, $vidc, $vbit, $audc, $abit) {
  204. global $cfg;
  205. // build command
  206. $this->_command = "nohup";
  207. $this->_command .= " ".$cfg['bin_vlc'];
  208. $this->_command .= " --rc-fake-tty";
  209. $this->_command .= " --sout ".tfb_shellencode("#transcode{vcodec=".$vidc.",vb=".$vbit.",scale=1,acodec=".$audc.",ab=".$abit.",channels=2}:std{access=mmsh,mux=asfh,dst=".$this->addr.":".$this->port."}");
  210. $this->_command .= " ".tfb_shellencode($file);
  211. $this->_command .= " > /dev/null &";
  212. // DEBUG : log the command
  213. if ($cfg['debuglevel'] > 1)
  214. AuditAction($cfg["constants"]["debug"], "vlcStart : ".$this->_command);
  215. // exec command
  216. exec($this->_command);
  217. }
  218. /**
  219. * stop a (/all) stream(s)
  220. */
  221. function instance_stop($port = 0) {
  222. if ($port == 0) { /* all */
  223. @shell_exec("killall -9 vlc > /dev/null");
  224. }
  225. }
  226. /**
  227. * get current running stream(s)
  228. *
  229. * @return array
  230. */
  231. function instance_getRunning($port = 0) {
  232. global $cfg;
  233. $retVal = array();
  234. if ($port > 0) { /* single */
  235. $vlcPS = trim(shell_exec("ps x -o pid='' -o ppid='' -o command='' -ww | ".$cfg['bin_grep']." ". $cfg['bin_vlc'] ." | ".$cfg['bin_grep']." ".$port." | ".$cfg['bin_grep']." -v grep"));
  236. if (strlen($vlcPS > 0)) {
  237. $tempArray = explode("\n", $vlcPS);
  238. if ((count($tempArray)) > 0) {
  239. $streamProcess = array_pop($tempArray);
  240. $processArray = explode(" ", $streamProcess);
  241. if ((count($processArray)) > 0) {
  242. $fileString = array_pop($processArray);
  243. $fileArray = explode("/", $fileString);
  244. if ((count($fileArray)) > 0) {
  245. $tempo = array_pop($fileArray);
  246. array_push($retVal, $tempo);
  247. }
  248. }
  249. }
  250. }
  251. }
  252. return $retVal;
  253. }
  254. /**
  255. * check if a stream is running on addr/port
  256. *
  257. * @param $port
  258. * @return boolean
  259. */
  260. function instance_isStreamRunning($port) {
  261. $fp = false;
  262. $fp = @fsockopen($this->addr, $port, $errno, $errstr, 1);
  263. if ($fp === false)
  264. return false;
  265. @fclose($fp);
  266. return true;
  267. }
  268. }
  269. ?>