Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Streaming video handler

I'm trying to implement a video streaming solution based on user access.

I have many video streams located on a private network connected to the server (http//192.168.100.101/mpeg4/1/media.amp), and I want to "proxy" that video stream through the web server.

I know how to setup the user access part, but how can I proxy the video stream to the user?

I have tried something like this, but it does not seem to work.

header('Content-type: application/x-rtsp-tunnelled');
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_URL, "http//192.168.100.101/mpeg4/1/media.amp");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
echo $output;
curl_close($ch);

Any ideas on the best way to do this? How do other streaming sites do it?

Thanks :)

like image 261
Zee Avatar asked Dec 06 '11 14:12

Zee


2 Answers

Yes, its easy to do. No need to set those headers manually. Let the server do it automatically.

Heres a working script which I wrote for a video streaming proxy -

ini_set('memory_limit','1024M');

set_time_limit(3600);

ob_start();

**// do any user checks here - authentication / ip restriction / max downloads / whatever**

**// if check fails, return back error message**

**// if check succeeds, proceed with code below**

if( isset($_SERVER['HTTP_RANGE']) )

$opts['http']['header']="Range: ".$_SERVER['HTTP_RANGE'];

$opts['http']['method']= "HEAD";

$conh=stream_context_create($opts);

$opts['http']['method']= "GET";

$cong= stream_context_create($opts);

$out[]= file_get_contents($real_file_location_path_or_url,false,$conh);

$out[]= $http_response_header;

ob_end_clean();

array_map("header",$http_response_header);

readfile($real_file_location_path_or_url,false,$cong);
like image 54
Tech Consultant Avatar answered Oct 15 '22 20:10

Tech Consultant


curl_exec() isn't designed for streaming output. It'll return only when the http request is completed. For a streaming request, that would theoretically be "never", and you'll just be filling up a memory buffer somewhere.

Check this answer for workarounds: Manipulate a string that is 30 million characters long

like image 26
Marc B Avatar answered Oct 15 '22 21:10

Marc B