简体   繁体   中英

Broadcast Live Streaming Audio Server using Node.js

My goal is to create a web-radio station hosted on localhost using JavaScript with node.js server for the backend part and HTML/CSS/JavaScript for the frontend part. My radio is going to be simple, and the goal is to create a server which is going to constantly broadcast a single song (or many songs using the.mp3 format) so they can be consumable from each client connected.

The streaming part is not so hard. The part I am struggling so far, is to achieve the "broadcast" transmission simultaneously to all consumers. The image below explains better my thoughts:

广播传输

Does anyone have a code example so I can easily understand it and at the same time implement it for my project's use?

Has anyone faced a similar situation like this?

This is exactly what SHOUTcast/Icecast and compatible servers do. You basically just need to copy the audio data to each client.

You can use normal HTTP for the streaming protocol. The clients don't need to know or care that they're streaming live. A simple audio element works:

<audio src="https://stream.example.com/some-stream" preload="none" controls></audio>

On the server-side, you do need to ensure that you're sending a stream aligned in such a way that the client can just pick right up and play it. The good news is that for raw MP3 and ADTS (which normally wraps AAC) streams, all the data they need for playback is in each frame, so you can just "needle drop", stream from an arbitrary position, and the client will figure it out.

Inside your Node.js app, it will look a bit like this:

Input Stream -> Buffer(s) -> Clients HTTP Responses

And in fact, you can even drop the buffer part if you want. As data comes in from the codec, you can just write it to all the clients. The buffer is useful to ensure a fast starting playback. Most clients are going to need some buffered data to sniff stream type and compatibility, as well as to fill their own playback buffer for smooth streaming.

That's really all there is to it!

im looking for this, although i want to deploy my node server, to firebase. how are you getting on??

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