onremovestream has been deprecated (and removed from Firefox), while onremovetrack is not yet implemented in Firefox.
How do I detect when a stream or track is being removed in Firefox?
onremovestream has been deprecated (and removed from Firefox), while onremovetrack is not yet implemented in Firefox.
How do I detect when a stream or track is being removed in Firefox?
Copyright © 2021 Jogjafile Inc.
You use
onremovetrackon the receiving stream:The above
ontrackwill run when e.g. the other side adds a track (and negotiates):Now, whenever that other side calls either
pc.removeTrack(sender)or setstransceiver.direction = "recvonly"(and negotiates), you should see theremovetrackevent fire.Here's an example that should work in all browsers.
Things to keep in mind
In standard WebRTC ("unified-plan") our
transceiver.receiver.trackisn'tendedwhen this happens, because it is wired to the other side'stransceiver.sender, not the other side'stransceiver.sender.track.Instead of ending, our receiving track is
mutedand removed from its stream(s).This is because
pc.removeTrack(sender)only sets thesender.tracktonullandtransceiver.directiontorecvonly(requiring negotiation).A sender may thus resume sending data using
sender.replaceTrack(newTrack)and settingtransceiver.direction = "sendrecv"again. On this happening, ourreceiver.trackwould beunmutedagain and reinserted into the stream(s), firing theaddtrackevents on the stream(s). This also fires thetrackevent again. Explore all the events in this blog's interactive section.A receiving track is only truly
endedbytransceiver.stop()(locally or through negotiation), orpc.close().