RTC Peer Connection - receiving a stream twice
I have an instance RTCPeerConnection
with ontrack
:
newConnection.ontrack = receivedStream // remote
Once the SDP exchange is complete and the peer adds its local stream:
connection.addStream(stream); // local
I can see that it receivedStream
gets called twice for each thread. - Checking e.track
shows me that the first call is for the track audio
and the second is for the track video
.
What's weird is that checking e.streams[0]
and calling getTracks
on this gives me two MediaStreamTracks
- one for audio and another for video:
So, I connect four MediaStreamTracks
to two calls 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
});
}
source to share
You can follow the same procedure for each MediaStreamTrack
, which will add an instance MediaStreamTrack
to MediaStream
, then set .srcObject
toHTMLMediaElement
const mediaStream = new MediaStream();
const video = document.querySelector("video");
for (const track of receivedMediaStream) {
mediaStream.addTrack(track)
}
video.srcObject = mediaStream;
source to share