Jump to content

Interrupt a PHP stream


pizzaguy
 Share

Recommended Posts

Hello. I'm making a script to stream an mjpeg image stream from my webcam. So far it's working quite well, but I was wondering if it was possible to interrupt the stream. Currently, this is the code which is responsible for streaming it (I got it off of another site)

# To stop apache from killing the scriptset_time_limit(0);# Sending the correct header# The boundary=ipcamera is important.# You will have to change "ipcamera" to whatever your camera uses to seperate the# images. Mine uses "--ipcamera" as seperator. You can omit the leading --, but have# to use the rest.header('Content-Type: multipart/x-mixed-replace;boundary=ThisRandomString');# Sending the images# You probably have to adapt the url to the video screen of your cam.//readfile('http://user:pass@ip_adresse_of_camera/video.cgi') readfile('http://192.168.1.90/img/mjpeg.cgi');

What I want to know is, is it possible to interrupt the readfile stream? For example, I want to have the webcam stream be disabled and inaccessible if it's after 3 PM. (I could easily make it check before the page loads, but I want to know if I can actually stop anyone who may be currently viewing the stream).

Edited by Pizzaguy
Link to comment
Share on other sites

You could use Apache.Access control with mod_rewriteThe [F] RewriteRule flag causes a 403 Forbidden response to be sent. Using this, you can deny access to a resource based on arbitrary criteria.For example, if you wish to block access to a resource between 8pm and 6am, you can do this using mod_rewrite.RewriteEngine OnRewriteCond %{TIME_HOUR} >20 [OR]RewriteCond %{TIME_HOUR} <07RewriteRule ^/fridge - [F]This will return a 403 Forbidden response for any request after 8pm or before 7am. This technique can be used for any criteria that you wish to check. You can also redirect, or otherwise rewrite these requests, if that approach is preferred.From: http://httpd.apache.org/docs/2.2/howto/access.htmlThis is worth a try.

Link to comment
Share on other sites

You could use Apache.Access control with mod_rewriteThe [F] RewriteRule flag causes a 403 Forbidden response to be sent. Using this, you can deny access to a resource based on arbitrary criteria.For example, if you wish to block access to a resource between 8pm and 6am, you can do this using mod_rewrite.RewriteEngine OnRewriteCond %{TIME_HOUR} >20 [OR]RewriteCond %{TIME_HOUR} <07RewriteRule ^/fridge - [F]This will return a 403 Forbidden response for any request after 8pm or before 7am. This technique can be used for any criteria that you wish to check. You can also redirect, or otherwise rewrite these requests, if that approach is preferred.From: http://httpd.apache.org/docs/2.2/howto/access.htmlThis is worth a try.
Well, that might work, unless Ingolme is correct and it won't block streams already running. However, I suppose I should have been more specific regarding what I wanted to do, because it only complicates things further. What I'm trying to do is set this up so there are temporary "users" so that the webcam can be viewed. Each of these users may have a specific time which it is set to expire (im saving it in a UNIX timestamp). So, the problem with this solution is that, even if it does work, what I am really looking for is a way to only stop one account's stream, but allow others if they have not yet expired.
Link to comment
Share on other sites

Alright. I was able to figure out how to do it. This is the code I used (plus a little extra, just in case someone happens to be looking for something like this)

# To stop apache from killing the scriptset_time_limit(0);# Sending the correct header# The boundary=ipcamera is important.# You will have to change "ipcamera" to whatever your camera uses to seperate the# images. Mine uses "--ipcamera" as seperator. You can omit the leading --, but have# to use the rest.header('Content-Type: multipart/x-mixed-replace;boundary=ThisRandomString');# Sending the images# You probably have to adapt the url to the video screen of your cam.//readfile('http://user:pass@ip_adresse_of_camera/video.cgi') //readfile('http://192.168.1.90/img/mjpeg.cgi');$fp = fopen("http://192.168.1.90/img/mjpeg.cgi", "r");$buffer = '';while(!feof($fp)){	$buffer = fgets($fp,300000); //just an arbitrary limit size which will be larger than any line given. 	if(!is_null($expiration) && time() > $expiration && trim($buffer) == "--ThisRandomString") break; //if the time has expired, and the stream has finished sending a frame	echo $buffer;}if(!is_null($expiration) && time() > $expiration){ //if expiration has occurred	echo "--ThisRandomString\n";	echo "Content-Type: image/jpeg\n";	echo "Content-Length: " . filesize("expired.jpg") . "\n";	echo "X-TimeStamp: 99999999999999\n\n"; //arbitary timestamp 	echo file_get_contents("expired.jpg");	echo "\n--ThisRandomString--\n";}

The while loop successfully emulates the readfile. The if statement inside will stop the loop if this specific stream has expired, but it will only do so once the stream has finished sending a frame. Then afterwords, a final image containing a message is shown as the last frame which will remain shown, saying that the stream has expired.

Edited by Pizzaguy
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...