Reputation: 55
In my webrtc client program (javascript, debugging both peers on 1 localhost) negotiationneeded
event is called 2 times.
Every time, geeting that event, I call getUserMedia
and then addtrack()
to send audio and video track to a remote, although I always use only 1 microphone and 1 camera and does not change their parameters.
So my program send total 4 tracks = (2 negotiationneeded
events) * (2 tracks on every event).
I have noticed, that all the tracks of one kind, I have sent, were with different id's.
So when I scan all sent tracks (peerConnection.getSenders().track), I discover, that there are 4 track = 2 video + 2 audio.
The questions are:
getSenders()
return 4 tracks ? Does it means that the remote will get 2 times more data (2audio + 2video), than it really needed (1 + 1) ?replacetrack()
instead of addtrack()
to eliminate duplicating ?PS: in my question by streams
I mean not a MediaStream
object, but a real data stream with video or audio, flowing between webrtc-peers.
Upvotes: 0
Views: 211
Reputation: 778
Short answer - you don't need to add the tracks again after you got negotiationneeded
event.
The negotiationneeded
just indicates that peers require an SDP offer-answer exchange so they understand how to transmit new tracks. This exhange is necessary for peers to agree on the codecs parameters to use. The tracks that you've already added to the connection will remain in place.
Therefore, in onnegotiationneeded
callback you would typically have a new createOffer -> setLocalDescription -> send offer
sequence.
Tracks not only represent a media source, but also provdie media flow control. They can be stopped, temporary disabled, etc. That's why each time you get a new object with a new ID, so you have individual control over over tracks created even if they represent the same physical source. But keep in mind that under the hood these tracks might be encoded differently for different peers. So the data flowing between peers might not be the same even if it's from the same physical source.
It is a consequence of adding the same tracks twice. Yes, you are probably sending the same data twice, you might check this on chrome://webrtc-internals page.
Regarding media transmission, the sender side is responsible for it. The receiving side has limited control over the media being sent; it can ignore it, but the media will still flow.
Upvotes: 1