我使用VCL广播到我的localhost,127.0.0.1使用UDP(传统)方法。为了捕获流量,我使用以下代码:
$address = '127.0.0.1';
$port = 1234;
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($sock, $address, $port) or die('Could not bind to address');
$f = fopen ('output', 'w');
fclose ($f);
$sock = stream_socket_server('udp://127.0.0.1:1234', $errno, $errstr, STREAM_SERVER_BIND);
while(1)
{
$a = stream_socket_recvfrom($sock, 65536);
$f = fopen('output', 'a');
fwrite ($f, $a);
fclose ($f);
@ob_flush();
}
这会记录数据包并保存,我将其重命名为.MP4并打开 - 好吧,结果有点乱。我可以识别输出,顶部屏幕可见,下部不好。我试图用另一个VCL播放器捕获它,没有问题。
答案 0 :(得分:2)
这是你的代码,删除了许多无用的东西,并提高了效率。试一试,看看会发生什么。它可能会或可能不会解决问题,但会报告发生的情况,我们会从那里接收它。
// Settings
$address = '127.0.0.1';
$port = 1234;
$outfile = "output.mp4";
// Open pointers
if (!$ofp = fopen($outfile, 'w'))
exit("Could not open output file for writing");
if (!$ifp = stream_socket_server("udp://$address:$port", $errno, $errstr, STREAM_SERVER_BIND))
exit("Could not create listen socket ($errno: $errstr)");
// Loop and fetch data
// This method of looping is flawed and will cause problems because you are using
// UDP. The socket will never be "closed", so the loop will never exit. But you
// were looping infinitely before, so this is no different - we can address this
// later
while (!feof($ifp)) {
if (!strlen($chunk = fread($ifp, 8192))) continue;
fwrite($ofp, $chunk);
}
// Close file pointers
fclose($ofp);
@fclose($ifp);