WebRTC 混合本地和远程音频流并记录

WebRTC mix local and remote audio steams and record

到目前为止,我已经找到了一种使用 MediaRecorder API 仅记录本地或远程记录的方法,但是是否可以混合和记录两种流并得到一个 blob?

请注意它只有音频流,我不想 mix/record 在服务器端。

我有一个 RTCPeerConnection 作为 pc

var local_stream = pc.getLocalStreams()[0];
var remote_stream = pc.getRemoteStreams()[0];
var audioChunks = [];
var rec = new MediaRecorder(local_stream);
rec.ondataavailable = e => {
    audioChunks.push(e.data);
    if (rec.state == "inactive") 
        // Play audio using new blob
}
rec.start();

即使我尝试在 MediaStream API 中添加多个曲目,但它仍然只提供第一首曲目音频。任何帮助或见解都将不胜感激!

WebAudio API可以为您混音。如果您想记录数组 audioTracks:

中的所有音轨,请考虑此代码
const ac = new AudioContext();

// WebAudio MediaStream sources only use the first track.
const sources = audioTracks.map(t => ac.createMediaStreamSource(new MediaStream([t])));

// The destination will output one track of mixed audio.
const dest = ac.createMediaStreamDestination();

// Mixing
sources.forEach(s => s.connect(dest));

// Record 10s of mixed audio as an example
const recorder = new MediaRecorder(dest.stream);
recorder.start();
recorder.ondataavailable = e => console.log("Got data", e.data);
recorder.onstop = () => console.log("stopped");
setTimeout(() => recorder.stop(), 10000);