Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RTC Peer Connection - receiving stream twice

I have an instance of RTCPeerConnection with ontrack defined:

 newConnection.ontrack = receivedStream // remote

Once the SDP exchange is complete and the peer adds their local stream:

 connection.addStream(stream); // local

I see that receivedStream gets invoked twice per stream - Inspecting e.track shows me that the first invocation is for the audio track, and second is for the video track.

What's odd is that inspecting e.streams[0] and calling getTracks on this gives me two MediaStreamTracks - one for audio and another for video:

enter image description here

So I'm netting four MediaStreamTracks across two invocations of receivedStream despite calling addStream once.

receivedStream is here:

 function receivedStream(e) {
        var stream = e.streams[0]; // this gets invoked twice when adding one stream!
        if (!stream) {
            throw new Error("no stream found");
        };
        // this gets me the corresponding connection
        waitForStream(stream.id).then(function (connection) {
            // get element
            targetVideoElement[0].srcObject = stream; // this now gets called twice per stream - once for audio, once for video
        }).catch(function (error) {
           // log
        });
    }
like image 705
SB2055 Avatar asked Jul 30 '17 21:07

SB2055


3 Answers

You can perform the same procedure for each MediaStreamTrack, that is add the MediaStreamTrack to a MediaStream instance, then set .srcObject of HTMLMediaElement

const mediaStream = new MediaStream();

const video = document.querySelector("video");

for (const track of receivedMediaStream) {
  mediaStream.addTrack(track)
}

video.srcObject = mediaStream;
like image 148
guest271314 Avatar answered Nov 03 '22 21:11

guest271314


May be you have added more than one track in remote peer like here:

localStream.getTracks().forEach(track => peer.addTrack(track, localStream));

Each call to peer.addTrack( ) in the remote Peer produces and event in local peer.ontrack = ()

like image 45
Cesar Morillas Avatar answered Nov 03 '22 20:11

Cesar Morillas


the track event is fired once for each MediaStreamTrack. Hence, if you have two different tracks, such as an audio and a video, in a MediaStream, the ontrack event will fire twice. The stream property of the event of both events will be identical and you can attach the stream to the same audio or video element twice without consequences.

like image 1
Philipp Hancke Avatar answered Nov 03 '22 22:11

Philipp Hancke