简体   繁体   English

Node.js直播:避免缓冲

[英]Node.js Live Streaming: Avoid buffering

I've written a small nodeJS server that outputs system audio captured by ffmpeg on Windows (using DirectShow) to the browser as a streaming MP3 file. 我写了一个小nodeJS服务器,它将Windows上的ffmpeg捕获的系统音频(使用DirectShow)作为流式MP3文件输出到浏览器。 The audio needs to be as live as possible, with minimum/no buffering, and a "skipping" effect in the audio is perfectly acceptable. 音频需要尽可能的生动,最小/无缓冲,音频中的“跳过”效果是完全可以接受的。

When I play the audio in Chrome using the HTML5 audio tag, there's a delay of about 8-10 secs over a low-latency LAN connection. 当我使用HTML5音频标签在Chrome中播放音频时,在低延迟LAN连接上延迟大约8-10秒。 I suspected this to be a client-side buffer, and used a Flash MP3 player on the client-side, which brought down the delay to 2-3 secs. 我怀疑这是一个客户端缓冲区,并在客户端使用Flash MP3播放器,这将延迟降低到2-3秒。

Now, the buffering seems to taking place on the server-side. 现在,缓冲似乎发生在服务器端。 The documentation for NodeJS's response.write mentions that the data is written kernel buffers. NodeJS的response.write文档提到数据是内核缓冲区。 How do I go about avoiding any buffering altogether or at least getting around it, so that the client always gets the latest audio data? 我如何完全避免任何缓冲或至少绕过它,以便客户端始终获取最新的音频数据? Strategies for handling 'drain' events to always push live data? 处理“排水”事件以便始终推送实时数据的策略?

On the request object, I've used setNoDelay(true) to avoid the use of Nagle's algorithm. 在请求对象上,我使用了setNoDelay(true)来避免使用Nagle算法。 Following is a snippet of how data is written when the spawned ffmpeg process emits data. 以下是生成的ffmpeg进程发出数据时如何写入数据的片段。

var clients = []; //List of client connections currently being served
ffmpeg.stdout.on('data', function(data) {
    for(var i = 0; i < clients.length; i++){
        clients[i].res.write(data);
    }
});

There are a few places where delay/buffering occurs: 有几个地方发生延迟/缓冲:

  1. DirectShow Capturing (~100ms or so) DirectShow捕获(约100ms左右)
  2. FFMPEG MPEG Encoding Buffer (1-10 seconds, depending on configuration) FFMPEG MPEG编码缓冲区(1-10秒,具体取决于配置)
  3. Network Writes and Transmission (near 0 in your setup) 网络写入和传输(设置中接近0)
  4. Client-Side Buffering (varies by client, as you have seen - most clients buffer ~2 seconds for decoding) 客户端缓冲(因客户端而异,如您所见 - 大多数客户端缓冲约2秒进行解码)

I suspect the buffer you need to look at is the one for FFMPEG encoding. 我怀疑你需要看的缓冲区是FFMPEG编码的缓冲区。 I've been able to reduce this by making sure the input format is configured explicitly when executing FFMPEG. 我已经能够通过确保在执行FFMPEG时明确配置输入格式来减少这种情况。 Also, make sure to drop the first chunks of data for encoding, as the first bits will undoubtedly be delayed more than later. 此外,请确保删除第一块数据以进行编码,因为第一位无疑会比以后延迟更多。

Once you've done this, you will find that your delay is a second or two. 一旦你完成了这个,你会发现你的延迟是一两秒钟。 At least, that's what I'm getting with a similar setup. 至少,这就是我通过类似的设置获得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM