I have a task to make an mp3 player embedded on a page that will play some voice messages stored in database. Some of messages are stored in WAV-format, so they must be converted to mp3. The conversion should be done "on fly". Because of not all messages have to be converted, I desided that it will be a good idea to use a stream filter that will be used when needed.
class LameFilter extends php_user_filter
{
protected $process;
protected $pipes = array();
public function onCreate() {
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
//2 => array("pipe", "w"),
);
$this->process = proc_open('lame --cbr -b 128 - -', $descriptorspec, $this->pipes);
}
public function filter($in, $out, &$consumed, $closing) {
while ($bucket = stream_bucket_make_writeable($in)) {
fwrite($this->pipes[0], $bucket->data);
$data = '';
while (true) {
$line = fread($this->pipes[1], 8192);
if (strlen($line) == 0) {
/* EOF */
break;
}
$data .= $line;
}
$bucket->data = $data;
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
public function onClose() {
//$error = stream_get_contents($this->pipes[2]);
fclose($this->pipes[0]);
fclose($this->pipes[1]);
//fclose($this->pipes[2]);
proc_close($this->process);
}
}
/* Register our filter with PHP */
stream_filter_register("lame", "LameFilter")
or die("Failed to register filter");
$mp3 = fopen("result.mp3", "wb");
/* Attach the registered filter to the stream just opened */
stream_filter_append($mp3, "lame");
$wav = fopen('ir_end.wav', 'rb');
while (!feof($wav)) {
fwrite($mp3, fread($wav, 8192));
}
fclose($wav);
fclose($mp3);
In example I used reading from one file and writing to another one. But actually data is read from OCI-lob and must be written to STDOUT.
The problem is that line "$line = fread($this->pipes[1], 8192);" blocks the script actually independently on expected data length.
Is there any correct way to read from process not closing its STDIN?