Siaran Langsung Node.js: Hindari buffering (Node.js Live Streaming: Avoid buffering)


問題描述

Siaran Langsung Node.js: Hindari buffering (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. 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. 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.

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. 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. Following is a snippet of how data is written when the spawned ffmpeg process emits data. 

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);
    }
});

‑‑‑‑‑

參考解法

方法 1:

There are a few places where delay/buffering occurs:

  1. DirectShow Capturing (~100ms or so)
  2. FFMPEG MPEG Encoding Buffer (1‑10 seconds, depending on configuration)
  3. Network Writes and Transmission (near 0 in your setup)
  4. Client‑Side Buffering (varies by client, as you have seen ‑ most clients buffer ~2 seconds for decoding)

I suspect the buffer you need to look at is the one for FFMPEG encoding.  I've been able to reduce this by making sure the input format is configured explicitly when executing 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.

(by Shirish KamathBrad)

參考文件

  1. Node.js Live Streaming: Avoid buffering (CC BY‑SA 3.0/4.0)

#audio-streaming #low-latency #node.js #FFmpeg






相關問題

在 windows phone 7 應用程序中流式傳輸音頻 (Streaming audio in windows phone 7 app)

從 FFMPEG for android 的命令行版本訪問網絡攝像頭麥克風 (Accessing webcam mic from command line version of FFMPEG for android)

Siaran Langsung Node.js: Hindari buffering (Node.js Live Streaming: Avoid buffering)

Icecast 流媒體無法播放 (Icecast streamming not playing)

如何使用 NAudio 在 C# 中解碼 mp3 文件 (how to decode mp3 file in c# using NAudio)

在 Nodejs 中使用 Kurento 複合媒體元素進行音頻會議需要幫助 (Need help for audio conference using Kurento composite media element in Nodejs)

iPhone - 在顯示圖像時使用 UIWebView 流式傳輸音頻 (iPhone - streaming audio using UIWebView while displaying an image)

jQuery:跨瀏覽器問題——調試 Internet Explorer (jQuery: Cross-browser problem -- Debugging Internet Explorer)

在 xuggler 中轉發和倒帶音頻 (forwarding and rewinding audio in xuggler)

網站上的實時音頻流 (Live audio streaming on a website)

如何通過 NAT 流式傳輸 WebRTC 音頻? (How to stream WebRTC audio through a NAT?)

從 WebAPI 在瀏覽器中播放 WAV 文件 (Play WAV file in Browser from WebAPI)







留言討論