Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transceiver.mid to onTrack() event payload #628

Open
jeremija opened this issue Apr 24, 2020 · 5 comments · May be fixed by #930
Open

Add transceiver.mid to onTrack() event payload #628

jeremija opened this issue Apr 24, 2020 · 5 comments · May be fixed by #930

Comments

@jeremija
Copy link
Contributor

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!

@franciscoreis
Copy link

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
Copy link

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

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

@jeremija
Copy link
Contributor Author

@franciscoreis that's almost exactly what I ended up doing in my fork: jeremija@f3eaec5

@franciscoreis
Copy link

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.

@razvanphp
Copy link

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 razvanphp linked a pull request Feb 11, 2024 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants