Web UI for ARGUS server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
5.4 KiB

6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <?php
  2. /**
  3. * Description of VideoStream
  4. *
  5. * @author Rana
  6. * @link http://codesamplez.com/programming/php-html5-video-streaming-tutorial
  7. */
  8. class VideoStream{
  9. private $path = "";
  10. private $stream = "";
  11. private $buffer = 102400;
  12. private $start = -1;
  13. private $end = -1;
  14. private $size = 0;
  15. private $live_stream = false;
  16. private $videoFormats = array("mp4"=>"video/mp4", "webm"=>"video/webm", "ogg"=>"video/ogg");
  17. function __construct($filePath)
  18. {
  19. $this->path = $filePath;
  20. }
  21. /**
  22. * Open stream
  23. */
  24. private function open()
  25. {
  26. if (!($this->stream = fopen($this->path, 'rb'))) {
  27. die('Could not open stream for reading');
  28. }
  29. }
  30. private function ffmpeg_open()
  31. {
  32. //$CMD = '/srv/toogler/bin/ffmpeg -i "' . $this->path . '" -codec:v libtheora -qscale:v 7 -codec:a libvorbis -qscale:a 5 - 2> /dev/null';
  33. //$CMD = '/srv/toogler/bin/ffmpeg -y -rtsp_transport tcp -i "'. $this->path .'" -c:v copy -an - 2>/dev/null';
  34. //$CMD = '/srv/toogler/bin/ffmpeg -y -i "rtsp://gagua:4GetMyID@192.168.200.116:554/Streaming/Channels/102?transportmode=unicast&profile=Profile_2" -codec:v libtheora -qscale:v 7 -r 10 -an - 2> /dev/null';
  35. //$CMD = '/srv/toogler/bin/ffmpeg -i "rtsp://gagua:4GetMyID@192.168.200.116:554/Streaming/Channels/102?transportmode=unicast&profile=Profile_2" -vf "scale=320:240" -r 5 -vcodec libvpx -an -f webm - 2>./logerror';
  36. $CMD = 'ffmpeg -i "rtsp://gagua:4GetMyID@192.168.0.109:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1" -r 5 -bsf:v h264_mp4toannexb -c copy -f ismv - 2>./logerror';
  37. if (!($this->stream = popen( $CMD, 'rb'))) {
  38. die('Could not open stream for reading');
  39. }
  40. }
  41. /**
  42. * Set proper header to serve the video content
  43. */
  44. private function setHeader()
  45. {
  46. ob_get_clean();
  47. //header("Content-Type: video/mp4");
  48. if( !$this->live_stream ){
  49. header("Content-Type: ".($this->videoFormats[strtolower(pathinfo($this->path, PATHINFO_EXTENSION))]));
  50. } else {
  51. // Reconvert Video to a OGG
  52. //header("Content-Type: video/webm");
  53. header("Content-Type: video/mp4");
  54. };
  55. header('X-Accel-Buffering: no');
  56. header("Cache-Control: no-cache, must-revalidate");
  57. //header("Expires: ".gmdate('D, d M Y H:i:s', 0 ) . 'GMT' ); //time()+2592000) . ' GMT');
  58. //header("Last-Modified: ".gmdate('D, d M Y H:i:s', @filemtime($this->path)) . ' GMT' );
  59. if( !$this->live_stream ){
  60. $this->start = 0;
  61. $this->size = filesize($this->path);
  62. $this->end = $this->size - 1;
  63. header("Accept-Ranges: bytes 0-".$this->end);
  64. if (isset($_SERVER['HTTP_RANGE'])) {
  65. $c_start = $this->start;
  66. $c_end = $this->end;
  67. list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
  68. if (strpos($range, ',') !== false) {
  69. header('HTTP/1.1 416 Requested Range Not Satisfiable');
  70. header("Content-Range: bytes $this->start-$this->end/$this->size");
  71. exit;
  72. };
  73. if ($range == '-') {
  74. $c_start = $this->size - substr($range, 1);
  75. }else{
  76. $range = explode('-', $range);
  77. $c_start = $range[0];
  78. $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_end;
  79. };
  80. $c_end = ($c_end > $this->end) ? $this->end : $c_end;
  81. if ($c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size) {
  82. header('HTTP/1.1 416 Requested Range Not Satisfiable');
  83. header("Content-Range: bytes $this->start-$this->end/$this->size");
  84. exit;
  85. };
  86. $this->start = $c_start;
  87. $this->end = $c_end;
  88. $length = $this->end - $this->start + 1;
  89. fseek($this->stream, $this->start);
  90. header('HTTP/1.1 206 Partial Content');
  91. header("Content-Length: ".$length);
  92. header("Content-Range: bytes $this->start-$this->end/".$this->size);
  93. } else {
  94. header("Content-Length: ".$this->size);
  95. };
  96. };
  97. }
  98. /**
  99. * close curretly opened stream
  100. */
  101. private function end()
  102. {
  103. fclose($this->stream);
  104. exit;
  105. }
  106. /**
  107. * perform the streaming of calculated range
  108. */
  109. private function stream()
  110. {
  111. flush();
  112. set_time_limit(0);
  113. if( $this->live_stream ){
  114. while(!feof($this->stream) && !connection_aborted() ) {
  115. $data = fread($this->stream, $this->buffer );
  116. echo $data;
  117. flush();
  118. };
  119. } else {
  120. $i = $this->start;
  121. while(!feof($this->stream) && $i <= $this->end && !connection_aborted() ) {
  122. $bytesToRead = $this->buffer;
  123. if(($i+$bytesToRead ) > $this->end) {
  124. $bytesToRead = $this->end - $i + 1;
  125. };
  126. $data = fread($this->stream, $bytesToRead);
  127. echo $data;
  128. flush();
  129. $i += $bytesToRead;
  130. };
  131. if(!feof($this->stream)){
  132. pclose( $this->stream );
  133. }
  134. };
  135. }
  136. /**
  137. * Start streaming video content
  138. */
  139. function start()
  140. {
  141. $this->live_stream = false;
  142. $this->open();
  143. $this->setHeader();
  144. $this->stream();
  145. $this->end();
  146. }
  147. function startStream()
  148. {
  149. $this->live_stream = true;
  150. $this->ffmpeg_open();
  151. $this->setHeader();
  152. $this->stream();
  153. $this->end();
  154. }
  155. }
  156. ?>