简体   繁体   English

节点(套接字)直播音频流/广播

[英]node (socket) live audio stream / broadcast

Please, is there any easy way to stream (broadcast) media file (ogg, mp3, spx..) from server to client (browser) via NODE.js and possibly SOCKET.IO? 请问有没有简单的方法可以通过NODE.js和可能的SOCKET.IO从服务器到客户端(浏览器)传输(广播)媒体文件(ogg,mp3,spx ..)?

I have to record audio input on the server side and then be able to play it realtime for many clients. 我必须在服务器端录制音频输入,然后才能为许多客户端实时播放。 I've been messing with binary.js or socket.io streams but wasnt able to get it right. 我一直在搞乱binary.js或socket.io流,但是无法正确使用它。

I've tried to encode audio input with speex, vorbis or lame and then load it by FS to client but I havent been successful. 我试图用speex,vorbis或lame对音频输入进行编码,然后通过FS将其加载到客户端,但我还没有成功。 Or do i have to capture PCM and then decode it in browser? 或者我必须捕获PCM然后在浏览器中解码它?

Any suggestion on this, nothing Ive found ever helped me. 对此有任何建议,我发现没有任何帮助过我。

Many thanks for any tips, links and ideas. 非常感谢任何提示,链接和想法。

You'll want to look for packages that work on Streams and from there it's just about piping your streams to output as necessary. 您将需要查找适用于Streams的软件包,并从那里开始根据需要将流输出到输出。 Using Express or just the built-in HTTP you can accomplish this quite easily. 使用Express或仅使用内置HTTP,您可以非常轻松地完成此任务。 Here's an example built around osx-audio which provides a PCM stream, lame which can encode a stream to mp3, and Express: 这是一个围绕osx-audio构建的示例,它提供了一个PCM流, lame可以将流编码为mp3,而Express:

var Webcast = function(options) {

  var lame = require('lame');
  var audio = require('osx-audio');
  var fs = require('fs');

  // create the Encoder instance
  var encoder = new lame.Encoder({
    // input
    channels: 2,        // 2 channels (left and right)
    bitDepth: 16,       // 16-bit samples
    sampleRate: 44100,  // 44,100 Hz sample rate

    // output
    bitRate: options.bitrate,
    outSampleRate: options.samplerate,
    mode: (options.mono ? lame.MONO : lame.STEREO) // STEREO (default), JOINTSTEREO, DUALCHANNEL or MONO
  });

  var input = new audio.Input();
  input.pipe(encoder);

  // set up an express app
  var express = require('express')
  var app = express()

  app.get('/stream.mp3', function (req, res) {
    res.set({
      'Content-Type': 'audio/mpeg3',
      'Transfer-Encoding': 'chunked'
    });
    encoder.pipe(res);
  });

  var server = app.listen(options.port);
}

module.exports = Webcast;

How you get your input stream might be the most interesting part, but that will depend on your implementation. 如何获得输入流可能是最有趣的部分,但这取决于您的实现。 The popular request package is built around Streams as well though, so it might just be an HTTP request away! 流行的request包也是围绕Streams构建的,所以它可能只是一个HTTP请求!

On the web browser you have the HTML5 video element and the audio element . 在Web浏览器上,您有HTML5视频元素和音频元素 Both of them have sources. 他们俩都有消息来源。 Each web browser supports different codecs natively. 每个Web浏览器本身都支持不同的编解码器。 So you'll want to watch out for that if you're trying to stream mp3. 所以如果你想尝试流式传输MP3,你会想要注意的。

You don't need socket.io, you only need HTTP. 你不需要socket.io,你只需要HTTP。 Your app is reading a file, music.ogg , and for each chunk it reads, it will send it through the http server. 您的应用正在读取文件, music.ogg ,并且对于它读取的每个块,它将通过http服务器发送它。 It will be one single HTTP request that's kept open until the file is transferred. 它将是一个单独的HTTP请求,在文件传输之前一直保持打开状态。

Here's how your html will look: 以下是您的html的外观:

<audio src="http://example.com/music.ogg"></audio>

And your nodejs code will be something like this (haven't tested this): 你的nodejs代码将是这样的(没有测试过):

var http = require('http');
var fs = require('fs');

http.on('request', function(request, response) {
    var inputStream = fs.open('/path/to/music_file.ogg');
    inputStream.pipe(response);
})

I'm only using the ReadableStream.pipe method on the inputStream and the http and fs modules for the above code. 我只在inputStream上使用ReadableStream.pipe方法,在上面的代码中使用http和fs模块。 If you want to transcode the audio file (for example, from mp3 to ogg) you'll want to find a module that does that and pipe the data from the file into the transcoder then into response : 如果要对音频文件进行转码(例如,从mp3转换为ogg),您将需要找到一个模块来执行该操作,并将数据从文件传输到转码器然后进入response

// using some magical transcoder
inputStream.pipe(transcoder).pipe(response);

The method will call end on the stream whenever it's finished writing so that the HTTP request will be finished as soon as the file is done being read (and transcoded). 只要文件完成写入,该方法就会在流上调用end ,这样一旦文件被读取(并进行转码),HTTP请求就会完成。

You can do this with node and RTC. 您可以使用node和RTC执行此操作。 There is some tools ready to use like SimpleWebRTC or EasyRTC . 有一些工具可以使用,如SimpleWebRTCEasyRTC For what I already tested video is still a trouble, but audio works great. 对于我已经测试过的视频仍然是一个麻烦,但音频效果很好。

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

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