7

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 :)

Zee
  • 155
  • 1
  • 7

3 Answers3

3

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);
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Tech Consultant
  • 374
  • 1
  • 7
  • DUDE, you just saved me a looooot of time :) Tested on php 5.1 and 5.6 and works as a charm – bksi Apr 28 '18 at 01:28
  • I use this code and cannot stream audio file, the file will be downloaded, not play on browser, how can I fix this? – Tạ Anh Tú May 28 '22 at 12:08
  • tried with both audio and video files, works ok. please share the full php code and also the html code (browser side) on how you are using it ? – Tech Consultant May 29 '22 at 15:22
  • This isn't working for me. Is some html video tag needed or something? I'm not sure how this works. I tried using this example as is in a file called liveVideo.php. I just removed the ** comment lines and defined $real_file_location_path_or_url to be 'http://:/'. My web server is on the same internal network as the camera. when I go to https://.com//liveVideo.php, chrome just spins forever with the status of the GET request in dev tools showing (pending) and size showing 0B. – benino Aug 01 '23 at 04:56
1

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

Community
  • 1
  • 1
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

Try a solution like in this page: Streaming POST data through PHP cURL I'm gonna try it myself to see if it works, but thought I post this here before I get distracted and forget about this question :)

Reza S
  • 9,480
  • 3
  • 54
  • 84