简体   繁体   English

使用 Node.js 广播直播音频服务器

[英]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.我的目标是使用 JavaScript 和 node.js 服务器为后端部分和 HTML/CSS/JavaScript 为前端部分创建一个托管在 localhost 上的网络广播电台。 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.我的收音机将很简单,目标是创建一个服务器,该服务器将不断播放一首歌曲(或使用 .mp3 格式的许多歌曲),以便可以从每个连接的客户端使用它们。

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.这正是 SHOUTcast/Icecast 和兼容服务器所做的。 You basically just need to copy the audio data to each client.您基本上只需要将音频数据复制到每个客户端。

You can use normal HTTP for the streaming protocol.您可以使用普通的 HTTP 作为流协议。 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.在服务器端,您确实需要确保发送的 stream 以这样一种方式对齐,即客户端可以直接拿起并播放它。 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.好消息是,对于原始 MP3 和 ADTS(通常包装 AAC)流,它们播放所需的所有数据都在每一帧中,因此您可以从任意 position 中“针落”,stream,客户端将计算出来。

Inside your Node.js app, it will look a bit like this:在您的 Node.js 应用程序中,它看起来有点像这样:

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.大多数客户端将需要一些缓冲数据来嗅探 stream 类型和兼容性,以及填充自己的播放缓冲区以实现流畅的流式传输。

That's really all there is to it!这就是它的全部!

im looking for this, although i want to deploy my node server, to firebase.我正在寻找这个,虽然我想将我的节点服务器部署到 firebase。 how are you getting on??你过得怎么样??

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

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