Jump to content

Tricky: Getting PHP to read Webcam JPG stream.. Plz help.


roel018

Recommended Posts

Hi everybody!

 

I'm struggling with a serious issue for days now.

I have a so called PHP proxy file which I'm using to bypass javascript's Same-Domain policy. This way, I'm reading the status of my public webcam (online or offline). I'm calling a file integrated in this (very advanced, build-in webserver enabled) webcam of mine to accomplish this, and it works great, without any flaws (I even manage to bypass the Basic Authentication protection this way).

 

Anyway, you'll find my code below.

 

The problem I'm facing, is that I would like to catch the webcam's image stream using the exact same method. The webcam's image stream, which is in fact a series of JPG's (mJPG), broadcasted by a file called GetData.cgi using the "multipart/x-mixed-replace" mime-type. When I type "http://192.x.x.4/GetData.cgi?1234" into my browser's address bar, after providing the webcam's username and password, I instantly get the JPG stream. So it's really there.. When I check the page's properties in my browser, it says it's an image/jpeg document.

 

Now, I would really like to know HOW I could get that stream of JPG's, and echo them onto the screen. That's all I want. Because I would like to create an <IMG> tag in my page, have the SRC pointed to this PHP proxy file, and have the image returned. All attempts so far have ended in PHP throwing errors at me that the provided data is NO valid image data... :( Bummer.

 

Hope someone can help me out here. I'm posting the GetData.cgi contents as well just below my proxy file's code.

 

Thanks,

Kind regards,

Roel

 

 

I use the code below (proxy_get_binary.php) to get the images and to access other files on the cam's build-in webserver (the later works perfectly). I'm calling this script with the following javascript command:  return ('<img id="oCamCtl" src="proxy_get_binary.php?remotefile=/GetData.cgi&status=true' + '&keycode=3748845" width="' + width + 'px" height="' + height + 'px" />');

 

<?php
error_reporting(-1);
$masteruser = 'myloginname';
$masterpass = 'mypassword';

$status= ($_POST['status']);
$keycode = ($_POST['keycode']);
$remotefile = ($_POST['remotefile']);

$remoteserver = '192.xxx.xxx.104';


$urlstring = '?status='.$status;
$url = 'http://'.$remoteserver.$remotefile.$urlstring;

$url = parse_url($url); 
$port = 80;

//just some checks to see if user is allowed to access this page:
if($keycode == '3748845') {
$host = $url['host'];
} else {
$host = "127.0.0.1";
}

$fh = fsockopen( $host, $port );
if( $fh ) {
fwrite( $fh, "GET ".$url['path'].$urlstring." HTTP/1.0\r\n" );
// in case we deal with virtual hosts pass the domain we are interested in.
fwrite( $fh, "Host: ".$url['host']."\r\n" );
// rfc2617: basic authorization header plus base64 encoded username:password
fwrite( $fh, "Authorization: Basic ".base64_encode( $masteruser.":".$masterpass )."\r\n" );
// end of the request header
fwrite( $fh, "\r\n" );
 // practically ignore all answering headers send by the server
while( ! feof( $fh ) && ($debug = fgets( $fh )) != "\r\n" ) /*echo $debug*/;
// display only the body of the message
while( ! feof( $fh ) ) {
	echo fgets( $fh );
}
fclose( $fh );
}

?>

 

 

Here's some info (from the manufacturer) about the cgi script that gives me headaches:

GetData.cgi is designed for "server-push". "Server-push" means that user need not always detect camera's state, and the camera server transfer the camera data on its own.
GetData.cgi[?Status=false] returns:

HTTP/1.0 200 OK
Date: Wed, 19 Feb 2003 03:40:16 GMT
Server: WYM/1.0
Connection: close
Content-Type: multipart/x-mixed-replace;boundary=WINBONDBOUDARY
Last-Modified: Wed, 19 Feb 2003 03:40:16 GMT
Pragma: no-cache
Cache-Control: no-cache
Expires: 01 Jan 1970 00:00:00 GMT

--WINBONDBOUDARY
Content-Type: image/jpeg
<content of jpeg file>

--WINBONDBOUDARY
Content-Type: image/jpeg
<content of jpeg file>

--WINBONDBOUDARY
...
GetData.cgi?Status=true returns:

HTTP/1.0 200 OK
Date: Wed, 19 Feb 2003 03:40:16 GMT
Server: WYM/1.0
Connection: close
Content-Type: multipart/x-mixed-replace;boundary=WINBONDBOUDARY
Last-Modified: Wed, 19 Feb 2003 03:40:16 GMT
Pragma: no-cache
Cache-Control: no-cache
Expires: 01 Jan 1970 00:00:00 GMT

--WINBONDBOUDARY
Content-Type: image/jpeg
<content of jpeg file>

--WINBONDBOUDARY
Content-Type: text/plain
<same as what's returned by GetStatus.cgi>

--WINBONDBOUDARY
Content-Type: image/jpeg
<content of jpeg file>

--WINBONDBOUDARY
Content-Type: text/plain
<same as what's returned by GetStatus.cgi>

--WINBONDBOUDARY
...


THNX!!!!!!

Link to comment
Share on other sites

An <img> tag is not able in any browser I know of to display a JPG stream.

 

You could grab the most recent JPG and show it in the <img>.

 

In that case, you would just want to make the request, parse the content and return the first image/jpeg content.

 

 

That would involve customizing your proxy quite a bit to the camera though....

Link to comment
Share on other sites

An <img> tag is not able in any browser I know of to display a JPG stream.

 

You could grab the most recent JPG and show it in the <img>.

 

In that case, you would just want to make the request, parse the content and return the first image/jpeg content.

 

 

That would involve customizing your proxy quite a bit to the camera though....

 

Yeah but how do I grab the most recent JPG from the cam and output it into the IMG tag? Because all I'm getting back are error messages that GetData.cgi is no valid JPG image....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.