简体   繁体   中英

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. 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. 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. 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.

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.

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.

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.

Probably the easiest ones to look at for you are video.js and 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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