Shashidhara
Shashidhara

Reputation: 683

`stream` is undefined on `ontrack` event in WebRTC

In WebRTC peer communication, i am trying to implement Unified Plan where Transceivers can be used. In that when the ontrack is fired, the stream is passed as undefined. However I can accesss the track from transceiver.receiver.track.

-> The problem is that, for video I need the stream instead separate track(audio, video). How to convert this separate track to a single stream? Is it intended feature or whether I need to set something inorder fetch the stream itself.

Upvotes: 2

Views: 2630

Answers (1)

jib
jib

Reputation: 42500

The track event has a streams (plural) argument. If you know there's one stream, use:

pc.ontrack = event => video.srcObject = event.streams[0];

This is because Unified-plan is track-based, and a track may be associated with more than one stream, or no streams at all.

A remote track's stream associations come from the arguments to addTrack or addTransceiver:

pc.addTrack(track, streamMyTrackIsIn, optional2ndStreamMyTrackIsIn);

Only tracks are sent. Remote streams get recreated with ids matching the streams passed in above.

The RTCPeerConnection automatically manages the streams in event.streams for you, in response to things like transceiver.direction changes or the misnomer removeTrack.

Or create your own remote streams to put your tracks in any way you please:

pc.ontrack = event => video.srcObject = new MediaStream([event.track]);

Use the latter only if you want to manage stream associations manually, as it can get complicated.

Upvotes: 3

Related Questions