使用WebRTC和WebAudio(WebSockets)实时流式传输音频

时间:2014-10-08 20:11:04

标签: javascript audio webrtc live-streaming web-audio

我正在尝试建立一个实时音频流系统,客户端将从麦克风(使用getUserMedia访问)的音频广播到一个或多个对等方。 为此,音频流的块通过WebSocket发送到服务器,服务器然后将该信息中继到连接到WebSocket的所有对等体。

我的主要问题来自于如何播放网站上同行收到的大量数据。

首先,这就是我在客户端广播JS脚本上发送音频数据的方式:

var context = new AudioContext();
var audioStream = context.createMediaStreamSource(stream);
// Create a processor node of buffer size, with one input channel, and one output channel
var node = context.createScriptProcessor(2048, 1, 1);
// listen to the audio data, and record into the buffer
node.onaudioprocess = function(e){
        var inputData = e.inputBuffer.getChannelData(0);
        ws.send(JSON.stringify({sound: _arrayBufferToBase64(convertoFloat32ToInt16(inputData))}));
}
audioStream.connect(node);
node.connect(context.destination);

arrayBufferToBase64和convertoFloat32ToInt16是我用来分别以base64格式发送流,并将inputData转换为Int16的方法,而不是那个花哨的Float32表示(我使用在SO上找到的方法,应该可以工作)。

然后,在数据通过WebSocket之后,我在另一个脚本中收集数据,该脚本将在每个对等方的网站上执行:

var audioCtx = new AudioContext();
var arrayBuffer = _base64ToArrayBuffer(mediaJSON.sound);
audioCtx.decodeAudioData(arrayBuffer, function(buffer) {
     playSound(buffer);
});

我还需要将收到的base64数据转换为ArrayBuffer,然后由decodeAudioData解码,以生成AudioBuffer类型的audioBuffer。 playSound函数就像这样简单:

function playSound(arrBuff) {
    var src = audioCtx.createBufferSource();
    src.buffer = arrBuff;
    src.looping = false;
    src.connect(audioCtx.destination);
    src.start();
}

但由于某些原因,我无法在这个剧本上播放任何声音。我很确定广播脚本是正确的,但不是“监听器”脚本。谁可以帮我这个事 ?

谢谢!

0 个答案:

没有答案
相关问题