[php-src] Issue #15521: stream_get_contents/fread timeouts
| From: | 6562680 | Date: | Wed, 21 Aug 2024 13:04:42 +0000 |
| Subject: | [php-src] Issue #15521: stream_get_contents/fread timeouts | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247560@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/15521
Author: 6562680
### Description
Actually i have not so much experience with resources or sockets, but tomorrow i tried to create
videostream using PHP and
ffmpeg tool.
There's mode that allows to pass data with PIPE to input and receive data with PIPE from the
output.
Guess, it works great on Unix systems, but still works problematic with Windows.
Short example:
```php
function _sigstop(?bool $sigstop)
{
static $val;
$val = $val ?? boolval($sigstop) ?? false;
return $val;
}
$ffmpegPath = implode(DIRECTORY_SEPARATOR, [ "c:", 'Program Files',
'ffmpeg', 'bin', 'ffmpeg.exe' ]);
$videoOutputPath = implode(DIRECTORY_SEPARATOR, [ __DIR__, 'video.mp4' ]);
$cmd = [
'"' . $ffmpegPath. '"',
// "-y", // overwrite existing file
//
// > tried this one, nothing changed
// "-flush_packets 1",
//
// > input
//
"-framerate 10",
"-i -", // input from PIPE
//
// output
//
"-b:v 2500k", // output bitrate
"-c:v libx264", // output codec
"-pix_fmt yuvj420p", // output color palette
"-f rawvideo -", // output format and mark to PIPE
//
// > i succeeded to create file, but failed with pipes...
//
// '"' . $videoOutputPath . '"', // output FILE format
];
$cmd = implode(' ', $cmd);
$cmdQuoted = "{$cmd}";
$pipesSpec = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w'],
];
$ph = proc_open($cmd, $pipesSpec, $pipes);
$limit = 20;
while (true
&& ($limit > 0)
&& ! _sigstop();
) {
// create image.... (resource) $imageHandler
$imageHandler = imagecreatetruecolor($width, $height);
$bgColor = imagecolorallocate(imageHandler, rand(0, 255), rand(0, 255), rand(0, 255));
imagefill(imageHandler, 0, 0, $bgColor);
$circleColor = imagecolorallocate(imageHandler, rand(0, 255), rand(0, 255), rand(0, 255));
imagefilledellipse(imageHandler, rand(0, $width), rand(0, $height), 100, 100, $circleColor);
ob_start();
imagejpeg($imageHandler);
$imageContent = ob_get_clean();
imageclose($imageHandler);
fwrite($pipes[0], $imageContent); // > using input pipe to pass data to the process
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
}
fclose($pipes[0]); // close input pipe on process end
// there is 2 cases, you have set of images and you can sent all images, then close the pipe (code
will be different, but will work)
// my case is sending images non-stop, like creating unlimited video stream, thats why closing
reading pipe outside the loop.
proc_close($ph);
```
The problem is in that line never reach first byte because reading pipe never closed:
```
$stdout = stream_get_contents($pipes[1]);
```
But once i closed that pipe - i cannot organize unlimited stream - only one bulk of
data can be passed.
It seems a very old and known bug, but maybe after that years some solution was found by php devs
team?