简体   繁体   English

使用具有带宽控制和动态音轨切换的Node.js流音频

[英]Streaming audio with Node.js with bandwidth control and dynamic track switching

Let's assume I have different bitrates mp3 of the same song on my server. 假设我的服务器上同一首歌曲的比特率mp3不同。 Is it possibile to make my node server detect the speed at which the client is receiving the chunks and thus switching dynamically between the files? 是否可以让我的节点服务器检测客户端接收数据块的速度,从而在文件之间动态切换? For example, the server receives a request for a track, and then starts streaming to the client the 192kbps version of it. 例如,服务器接收到对轨道的请求,然后开始将其192kbps版本的流传输到客户端。 After a couple of chunks, it detects that the client isn't receiving them fast enough, and so switches the data source to the 128 kbps version, and so on. 经过几个块之后,它检测到客户端没有足够快地接收到它们,因此将数据源切换到128 kbps版本,依此类推。 Currently I've only managed to do this as a simple test, but it comes with no control options at all: 目前,我只能通过一个简单的测试来做到这一点,但是它根本没有任何控制选项:

const mediaServer = require('mediaserver');
router.get('/play', function(req, res, next){
    mediaServer.pipe(req, res, appRoot + 'private/media/musica/m.mp3')
});

The approach you describe is commonly used in video delivery - the term usually used is Adaptive Bit Rate (ABR) streaming. 您描述的方法通常用于视频交付中-通常使用的术语是自适应比特率(ABR)流。

The key difference between ABR and what you describe is that the decision of which bit rate to use for the next chunk is taken by the client not the server. ABR与您所描述的内容之间的主要区别在于,下一个块使用哪个比特率的决定由客户端而不是服务器决定。

This works well in practice as it is the client that is requesting the chunks anyway, and it is also best placed to know if it can handle a higher not rate or needs a lower one, by examining the inout buffer for example. 这在实践中效果很好,因为无论如何,都是客户端在请求块,并且最好通过例如检查inout缓冲区来了解其是否可以处理更高的速率或需要更低的速率。

There are several open source ABR implementations which you can examine to get an idea how they work - as mentioned most are video focused and as audio is typically very small compared to video you will probably find they don't use ABR for audio but the principles will be the same. 您可以检查几种开源ABR实施方案,以了解它们的工作原理-如前所述,大多数是针对视频的,并且与视频相比,音频通常很小,您可能会发现它们不使用ABR进行音频,但是原理会一样。

Probably the easiest ones to look at for you are video.js and dash.js: video.js和dash.js可能是最容易找到的代码:

In partiular if you look at the ABR rules in dash.js (dash.js/src/streaming/rules/abr/ at the time of writing) it helps get a quick feel for how they have implemented it - there is quite a bit of experience and logic so you may find it easiest to leverage something exciting like this also. 特别是,如果您查看dash.js中的ABR规则(撰写本文时为dash.js / src / streaming / rules / abr /),则有助于快速了解其实施方式-相当多经验和逻辑,因此您可能会发现最容易利用这种激动人心的东西。

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

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