Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebRTC mix local and remote audio steams and record

So far i've found a way only to record either local or remote using MediaRecorder API but is it possible to mix and record both steams and get a blob?

Please note its audio steam only and i don't want to mix/record in server side.

I've a RTCPeerConnection as 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();

Even i tried adding multiple tracks in MediaStream API but it still gives only first track audio. Any help or insight 'd be appreciated!

like image 205
Sasi Varunan Avatar asked Feb 09 '17 13:02

Sasi Varunan


People also ask

Can you record WebRTC?

First of all, the videos are recorded and stored in local storage since WebRTC allows recording and storing video streams locally. The recorded media is then uploaded to the servers.

Is WebRTC frontend?

WebRTC gives us a way to do real-time, peer-to-peer communication on the web.

How do I play WebRTC?

To Play with WebRTC, go to http://<server-address>:5080/WebRTCAppEE/player.html and enter the stream key parameter and click "Start Playing" button.


1 Answers

The WebAudio API can do mixing for you. Consider this code if you want to record all the audio tracks in the array 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);
like image 130
pehrsons Avatar answered Sep 28 '22 00:09

pehrsons