I wrote a simple relay script that connects to a web camera and reads from the socket, and outputs this data using the print function. The data is MJPG data with boundaries already setup. I just output the data that is read.
The problem is PHP seems to be buffering this data. When I set the camera to 1 FPS, the feed will freeze for 7-8 seconds, then quickly display 8 frames. If I set the resolution to a huge size, the camera move at more or less 1 frame per second. I assume then some buffering is happening (since huge sizes fill the buffer quickly, and low sizes don't), and I can't figure out how to disable this buffering. Does anyone know how to?
Code:
ignore_user_abort(false);
$boundary = "myboundary";
//Set this so PHP doesn't timeout during a long stream
set_time_limit(0);
$socketConn = @fsockopen ("192.168.1.6", 1989, $errno, $errstr, 2);
if (!$socketConn)
exit();
stream_set_timeout($socketConn, 10);
fputs ($socketConn, "GET /mjpeg HTTP/1.0rnrn");
//Setup Header Information
header("Cache-Control: no-cache");
header("Cache-Control: private");
header("Pragma: no-cache");
header("Content-type: multipart/x-mixed-replace; boundary=$boundary");
@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++)
ob_end_flush();
ob_implicit_flush(1);
stream_set_blocking($f2, false);
//Send data to client
while (connection_status() == CONNECTION_NORMAL)
{
$chunk = fread($socketConn, 128);
print $chunk;
}
fclose($socketConn);
tl;dr version
Do two things:
Disable the userspace output buffer, either...
Globally, by either...
output_buffering
in your php.ini, orTurning off
output_buffering
in your Apache config usingor for just the script you care about, by either...
ob_end_flush()
, orob_end_clean()
Also, disable the server-level output buffer as much as you possibly can, by either:
ob_implicit_flush()
at the start of your script, orflush()
after everyecho
statement or other statement that adds output to the response bodyLonger version
Confusingly, there are two layers of buffering that may be relevant and the PHP documentation does a poor job of distinguishing between the two.
The output buffer
The first layer is usually referred to by the PHP docs as the 'output buffer'. This layer of buffering only affects output to the body of the HTTP response, not the headers. You can turn on output buffering with
ob_start()
, and turn it off withob_end_flush()
orob_end_clean()
. You can also have all your scripts automatically start with output buffering on using theoutput_buffering
option in php.ini.The default value of this option for production versions of php.ini is 4096, which means that the first 4096 bytes of output will be buffered in the output buffer, at which point it will be flushed and output buffering is turned off.
You can disable this layer of buffering globally by setting
output_buffering
toOff
in your php.ini file (or usingin your Apache config, if you're using Apache). Alternatively, you can disable it for a single script by calling
ob_end_clean()
orob_end_flush()
at the start of the script.The write buffer, and the webserver buffer
Beyond the output buffer is what the PHP manual refers to as the 'write buffer', plus any buffering system your web server has. If you're using PHP with Apache through
mod_php
, and are not usingmod_gzip
, you can callflush()
to flush these; with other backends, it might work too, although the manual is cagey about giving assurances:There are also a couple of ways you can make PHP automatically call
flush()
every time youecho
anything (or do anything else that echoes output to the response body).The first is to call
ob_implicit_flush()
. Note that this function is deceptively named; given itsob_
prefix, any reasonable person would expect that it would affect the 'output buffer', as doob_start
,ob_flush
etc. However, this is not the case;ob_implicit_flush()
, likeflush()
, affects the server-level output buffer and does not interact in any way with the output buffer controlled by the otherob_
functions.The second is to globally enable implicit flushing by setting the
implicit_flush
flag toOn
in your php.ini. This is equivalent to callingob_implicit_flush()
at the start of every script. Note that the manual advises against this, cryptically citing "serious performance implications", some of which I explore in this tangentially related answer.