Separate remote audio in different devices

166 views
Skip to first unread message

Joel Bermudez

unread,
Mar 12, 2021, 4:15:53 PM3/12/21
to discuss-webrtc
Hi everyone,

we need to separate the audio from two peerconnections into two different output devices.
Each peerconnection receives the corresponding audio track.
we have two audio elements, one for each peerconnection.
By making use of setsinkid of an audio element assigned to peerconnection1, it also affects the audio output element assigned to peerconnection2 preventing each stream to be played by a different output device from the other peerconnection.
For each ontrack event of each peerconnection, we create a new mediastream and add the track to the mediastream.
Checking the browser's webrtc-internals viewer, I check that both peerconnection share the same RTCMediaStream element but each one with its corresponding track.

We impliemented for each peerConnection like the below:

var localStream = await navigator.mediaDevices.getUserMedia({vide: true, audio: true});
peerConnection1 = new RTCPeerConnection(config);
localStream.getTracks().forEach(track => {
peerConnection1.addTrack(track, localStream);
});

var remoteStream = new MediaStream();
var remote = document.querySelector('#audio1');
remoteVideo.srcObject = remoteStream;

peerConnection1.addEventListener('track', async (event) => {
remoteStream.addTrack(event.track, remoteStream);
});

peerConnection1.addEventListener('icecandidate', event => {
    if (event.candidate) {
        ws.sendMessage({
                id: 'ADD_ICE_CANDIDATE_SELECT',
                candidate: event.candidate
        });
    }});

var sdpOffer = await peerConnection1.createOffer();
await peerConnection1.setLocalDescription(sdpOffer);

ws.onmessage = function(message)
{

        switch (jsonMessage.id) {
        case 'PROCESS_SDP_ANSWER_PEER1':
            var remoteDesc = new RTCSessionDescription({type:"answer",sdp:jsonMessage.sdpAnswer});
await peerConnection1.setRemoteDescription(remoteDesc);
            break;
        
        case 'ADD_ICE_CANDIDATE_PEER1':
            await peerConnection1.addIceCandidate(jsonMessage.candidate);;
            break;
};
ws.sendMessage({
id: 'PROCESS_SDP_OFFER_PEER1',
sdpOffer: sdpOffer.sdp
});

Thanks in advance!



guest271314

unread,
Mar 13, 2021, 10:53:01 AM3/13/21
to discuss-webrtc
> we need to separate the audio from two peerconnections into two different output devices.

Re setSinkId(), 'audiooutput' does not really mean an actual audio output devices, see https://github.com/guest271314/SpeechSynthesisRecorder/issues/18. On Linux on Chrome it is not possible to actually select an audio output device without workarounds.

> to be played by a different output device

The first step is clearly defining what you mean by output device? Two different sets of speakers connected to the machine? Headphones and speakers?

Joel Bermudez

unread,
Aug 30, 2021, 1:32:46 PM8/30/21
to discuss-webrtc
Yes, two different sets of devices, one peerconnection stream via headphones and one peerconnection stream via speakers.

guest271314

unread,
Aug 30, 2021, 10:03:28 PM8/30/21
to discuss...@googlegroups.com
AFAIK there is no specified means to select headphones https://github.com/WebAudio/web-audio-api-v2/issues/89. You can try to use PulseAudio or whatever the system audio controller is to select and stream to specific devices, or virtual devices, e.g., https://github.com/guest271314/setUserMediaAudioSourcehttps://github.com/guest271314/SpeechSynthesisRecorder/issues/17#issuecomment-749875748.

--

---
You received this message because you are subscribed to a topic in the Google Groups "discuss-webrtc" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/discuss-webrtc/lMH3hS5wY_8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to discuss-webrt...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/2269a559-7680-4082-a468-578acfb4da83n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages