[英]WebRTC: how to detect when a stream or track gets removed from a PeerConnection, in Firefox?
onremovestream
has been deprecated (and removed from Firefox), while onremovetrack
is not yet implemented in Firefox. onremovestream
已被弃用(并从 Firefox 中删除),而onremovetrack
尚未在 Firefox 中实现。
How do I detect when a stream or track is being removed in Firefox?如何检测 Firefox 中的流或轨道何时被删除?
You use onremovetrack
on the receiving stream :您在接收流上使用onremovetrack
:
pc.ontrack = ({track, streams: [stream]}) => {
track.onunmute = () => {
if (!video.srcObject) video.srcObject = stream;
};
stream.onremovetrack = ({track}) => {
console.log(`${track.kind} track was removed.`);
if (!stream.getTracks().length) {
console.log(`stream ${stream.id} emptied (effectively removed).`);
}
};
};
The above ontrack
will run when eg the other side adds a track (and negotiates):当例如另一方添加轨道(并协商)时,上述ontrack
将运行:
const sender = pc.addTrack(track, stream);
Now, whenever that other side calls either pc.removeTrack(sender)
or sets transceiver.direction = "recvonly"
(and negotiates), you should see the removetrack
event fire.现在,每当另一方调用pc.removeTrack(sender)
或设置transceiver.direction = "recvonly"
(并协商)时,您应该看到removetrack
事件触发。
Here's an example that should work in all browsers.这是一个适用于所有浏览器的示例。
In standard WebRTC ("unified-plan") our transceiver.receiver.track
isn't ended
when this happens, because it is wired to the other side's transceiver.sender
, not the other side's transceiver.sender.track
.在标准的 WebRTC(“统一计划”)中,当发生这种情况时,我们的transceiver.receiver.track
并没有ended
,因为它连接到另一端的transceiver.sender
,而不是另一端的transceiver.sender.track
。
Instead of ending, our receiving track is muted
and removed from its stream(s).我们的接收轨道没有结束,而是muted
并从其流中删除。
This is because pc.removeTrack(sender)
only sets the sender.track
to null
and transceiver.direction
to recvonly
(requiring negotiation).这是因为pc.removeTrack(sender)
只将sender.track
设置为null
并将transceiver.direction
recvonly
设置为recvonly
(需要协商)。
A sender may thus resume sending data using sender.replaceTrack(newTrack)
and setting transceiver.direction = "sendrecv"
again.因此,发送方可以使用sender.replaceTrack(newTrack)
并再次设置transceiver.direction = "sendrecv"
sender.replaceTrack(newTrack)
transceiver.direction = "sendrecv"
来恢复发送数据。 On this happening, our receiver.track
would be unmuted
again and reinserted into the stream(s), firing the addtrack
events on the stream(s).在这种情况下,我们的receiver.track
将再次unmuted
并重新插入到流中,在流上触发addtrack
事件。 This also fires the track
event again.这也会再次触发track
事件。 Explore all the events in this blog 's interactive section. 在此博客的互动部分探索所有事件。
A receiving track is only truly ended
by transceiver.stop()
(locally or through negotiation), or pc.close()
.的接收轨道只有真正ended
由transceiver.stop()
本地或通过协商),或pc.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.