simple-peer icon indicating copy to clipboard operation
simple-peer copied to clipboard

Add transceiver.mid to onTrack() event payload

Open jeremija opened this issue 4 years ago • 5 comments

I'd like to propose adding a third parameter, event.transceiver.mid, to the track event:

diff --git a/index.js b/index.js
index 5d0d08b..c274499 100644
--- a/index.js
+++ b/index.js
@@ -958,7 +958,7 @@ class Peer extends stream.Duplex {

     event.streams.forEach(eventStream => {
       this._debug('on track')
-      this.emit('track', event.track, eventStream)
+      this.emit('track', event.track, eventStream, event.transceiver.mid)

       this._remoteTracks.push({
         track: event.track,

Background

The Unified Plan Transition Guide says the following:

Local and Remote Track IDs Do Not Match

In Plan B, a sender is created for each local track that is added and a receiver is created for each remote track that is negotiated, the track IDs of local and remote endpoints match.

In Unified Plan, senders and receivers are created in pairs of transceivers, and a transceiver.receiver.track may have been created long before remote SDP offers a track. As such, the track that is fired in RTCPeerConnection.ontrack is no longer guaranteed to have an ID matching the sender-side version of the track. Furthermore, because of addTransceiver() and replaceTrack(), a track may be sent multiple times. Track IDs are misleading, and shouldn’t be assumed to match. Instead, transceiver.mid should be used to correlate local and remote tracks. You may have to “setLocalDescription(answer)” before the mid is known.

I'm building an SFU, and since a change of track.id or MediaStream.id might not be visible in the browser, I see no other option than to use the mid and send the track information for a particular mid through a different channel.

To be able to do that, I need to know which transceiver a track belongs to, and I was unable to find a public API from simple-peer that would allow me to do that. Thanks!

jeremija avatar Apr 24 '20 13:04 jeremija

Apparently, access to mid (why not to the whole Transceiver object) is essential in streams with multi video tracks. If not available, just have to edit the SimplePeer javascript source replacing this.emit("track", e.track, t) with this.emit("track", e.track, t, e.transceiver)

Congratulations for the excelent job. These improvements are naturally for advanced use. Francisco

franciscoreis avatar Sep 10 '20 13:09 franciscoreis

The following obvious change is also needed: p.on('track', (track, stream, transceiver) => { })

to align with this.emit("track", e.track, t, e.transceiver)

franciscoreis avatar Sep 10 '20 13:09 franciscoreis

@franciscoreis that's almost exactly what I ended up doing in my fork: https://github.com/jeremija/simple-peer/commit/f3eaec5ab28c582e31eef08b324707aa87403b0a

jeremija avatar Sep 10 '20 13:09 jeremija

Sure, your input was essential to find the solution I am looking for since Tuesday, thanks! Safari does not keep tracks original order so I hope (test later) this mid info will do the trick.

franciscoreis avatar Sep 10 '20 14:09 franciscoreis

so do I understand correctly that if one has more tracks like this on the initiator side:

peer.addTransceiver('video', {direction: 'recvonly'});
peer.addTransceiver('video', {direction: 'recvonly'});
peer.addTransceiver('audio', {direction: 'recvonly'});

then when receiving them, there is no way to differentiate on which is which?

@feross, @t-mullen how would you see the API for this? I can push a PR that includes also an updated documentation if you agree with passing the transreceiver to the events too.

Thank you!

razvanphp avatar Oct 09 '20 11:10 razvanphp