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.
 
 
 
 

169 lines
5.4 KiB

<?php
/**
* Description of VideoStream
*
* @author Rana
* @link http://codesamplez.com/programming/php-html5-video-streaming-tutorial
*/
class VideoStream{
private $path = "";
private $stream = "";
private $buffer = 102400;
private $start = -1;
private $end = -1;
private $size = 0;
private $live_stream = false;
private $videoFormats = array("mp4"=>"video/mp4", "webm"=>"video/webm", "ogg"=>"video/ogg");
function __construct($filePath)
{
$this->path = $filePath;
}
/**
* Open stream
*/
private function open()
{
if (!($this->stream = fopen($this->path, 'rb'))) {
die('Could not open stream for reading');
}
}
private function ffmpeg_open()
{
//$CMD = '/srv/toogler/bin/ffmpeg -i "' . $this->path . '" -codec:v libtheora -qscale:v 7 -codec:a libvorbis -qscale:a 5 - 2> /dev/null';
//$CMD = '/srv/toogler/bin/ffmpeg -y -rtsp_transport tcp -i "'. $this->path .'" -c:v copy -an - 2>/dev/null';
//$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';
//$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';
$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';
if (!($this->stream = popen( $CMD, 'rb'))) {
die('Could not open stream for reading');
}
}
/**
* Set proper header to serve the video content
*/
private function setHeader()
{
ob_get_clean();
//header("Content-Type: video/mp4");
if( !$this->live_stream ){
header("Content-Type: ".($this->videoFormats[strtolower(pathinfo($this->path, PATHINFO_EXTENSION))]));
} else {
// Reconvert Video to a OGG
//header("Content-Type: video/webm");
header("Content-Type: video/mp4");
};
header('X-Accel-Buffering: no');
header("Cache-Control: no-cache, must-revalidate");
//header("Expires: ".gmdate('D, d M Y H:i:s', 0 ) . 'GMT' ); //time()+2592000) . ' GMT');
//header("Last-Modified: ".gmdate('D, d M Y H:i:s', @filemtime($this->path)) . ' GMT' );
if( !$this->live_stream ){
$this->start = 0;
$this->size = filesize($this->path);
$this->end = $this->size - 1;
header("Accept-Ranges: bytes 0-".$this->end);
if (isset($_SERVER['HTTP_RANGE'])) {
$c_start = $this->start;
$c_end = $this->end;
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
if (strpos($range, ',') !== false) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $this->start-$this->end/$this->size");
exit;
};
if ($range == '-') {
$c_start = $this->size - substr($range, 1);
}else{
$range = explode('-', $range);
$c_start = $range[0];
$c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_end;
};
$c_end = ($c_end > $this->end) ? $this->end : $c_end;
if ($c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $this->start-$this->end/$this->size");
exit;
};
$this->start = $c_start;
$this->end = $c_end;
$length = $this->end - $this->start + 1;
fseek($this->stream, $this->start);
header('HTTP/1.1 206 Partial Content');
header("Content-Length: ".$length);
header("Content-Range: bytes $this->start-$this->end/".$this->size);
} else {
header("Content-Length: ".$this->size);
};
};
}
/**
* close curretly opened stream
*/
private function end()
{
fclose($this->stream);
exit;
}
/**
* perform the streaming of calculated range
*/
private function stream()
{
flush();
set_time_limit(0);
if( $this->live_stream ){
while(!feof($this->stream) && !connection_aborted() ) {
$data = fread($this->stream, $this->buffer );
echo $data;
flush();
};
} else {
$i = $this->start;
while(!feof($this->stream) && $i <= $this->end && !connection_aborted() ) {
$bytesToRead = $this->buffer;
if(($i+$bytesToRead ) > $this->end) {
$bytesToRead = $this->end - $i + 1;
};
$data = fread($this->stream, $bytesToRead);
echo $data;
flush();
$i += $bytesToRead;
};
if(!feof($this->stream)){
pclose( $this->stream );
}
};
}
/**
* Start streaming video content
*/
function start()
{
$this->live_stream = false;
$this->open();
$this->setHeader();
$this->stream();
$this->end();
}
function startStream()
{
$this->live_stream = true;
$this->ffmpeg_open();
$this->setHeader();
$this->stream();
$this->end();
}
}
?>