popen打开的stream如何转发

iz

问题描述

觉得pipe也许可用,但是pipe只对TCPConnection有效,无法转发stream

为此你搜索到了哪些方案及不适用的原因

于是尝试使用定时器

stream_set_timeout($stream,0,10);
$timer = Timer::add(0.5,function ($timer) use ($stream,$connection){
    if(feof($stream)) fclose($stream) and Timer::del($timer);
    else $connection->send(fread($stream,1*1024*1024),true);
},[$timer]);

然后意识到,send执行后HTTP请求结束了,于是浏览器拿到了皮毛就返回了
找不到合适的方法,请教HTTP数据流可能吗,该如何实现?
(我知道应该降级到HTTP/1.0)

226 1 0
1个回答

iz

最后尝试使用Chunk解决了流媒体的问题

        $stream = popen("ffmpeg -i '$file' -f '$codec' pipe:",'rb');
        stream_set_blocking($stream, false);
        $connection->send(new Response(200, [
            'Transfer-Encoding' => 'chunked',
            'Content-Type'      =>  $ctype
        ], ''));
        $fd = Worker::$globalEvent->onReadable($stream, function () use ($connection,$stream,$fd){
            // $connection->send(fgets($stream),true);
            $connection->send(new Chunk(fgets($stream)));
            if(feof($stream)) pclose($stream) and Worker::$globalEvent->offReadable($fd);
        });

虽然不是使用文档上的接口,可用就行了

  • 暂无评论
🔝