Changing the track of a producer

Is it possible to change the track of a producer (and a consumer) or if my client’s track is ended, I have to make a new producer (and all the consumers on the server)?

With webrtc since the “unified plan”, a transceiver’s receiver track exists for its whole lifetime and is independent of the existence of the track of the sender.

So with vanilla webrtc, if my sender’s track is closed or replaced, nothing really needs to be done on the receiver end. Is it similar with the mediasoup producer API?

You can use replaceTrack() method of producer to change the track, nothing needs to be done on consumer side, once producer changes the track, it is reflected to consumer side automatically.

1 Like

Great thanks, and if I need one producer for audio and one producer for video. Does it make sense to create both of them immediately? Or should I wait that I have a track to create it?

(is it even possible to create a producer without a track).

The use case is a typical conference where people don’t necessarily stream a video, but with webRTC I would create a transceiver for the video track when creating the peer connection and would change it with RTCRtpSender.replaceTrack(), with null if the user does not stream a video, or with the track if they start sharing.

From the doc you linked, it seems that replaceTrack() takes a mandatory track argument. Whereas the replaceTrack() of WebRTC’s RTCRtpSender only take the track as an optional argument (null/undefined = removing it).

I don’t think you can produce without a track. I think you are concerned about the time to reach video to other people. If you produce it when the video track is available the video will take around 1-5 seconds to reach to consumers. I think this is fine, for video user doesn’t care about few seconds delay. Delay in audio matters.

But still if you want to produce video even if video track is not available then there is a workaround. What you can do is you can create a dummy video track and produce that track and when original video track is available you can replace that video track with dummy track, problem solved. Here is how to create dummy track:

But I will go with the first approach which is to produce when user turns on video otherwise not. That 1-5 seconds delay is acceptable.

1 Like

I see, I will only create the producer when needed then. I just didn’t like the idea of creating a producer (with all the boiler plate that it entails) at any arbitrary moment.

I would have prefered to have them setup at the “peer” creation, with only needing to do a simple “replaceTrack()” whenever needed afterwards.

Although in the end, this producer creation overhead can be abstracted away anyways. Thanks for the answers!

1 Like