WebRTC:Firefox 中的重新协商

WebRTC: Renegotiation in firefox

根据 this article,重新协商在 firefox v38 中实现,我们可以从相同的对等连接中添加删除流而无需创建新的,但我找不到任何工作演示来支持该声明,并且当我尝试时,两个用户以视频模式聊天,我将其中一个流更改为 audio,我收到错误:

NotSupportedError: removeStream not yet implemented

this tells the same, but this 表示支持重新协商事件,但 removeStream 不是重新协商的关键部分吗?我在 windows 7 中使用的是 firefox 版本 39。我很困惑,firefox 还不支持重新协商,对吗?

尝试对单个曲目使用 replaceTrack,而不是替换整个流。此示例假设您有一个对等连接 pc1 和一个要替换的新流 newStream。获取发件人,并用新流中的适当曲目替换曲目。工作样本 here.

Promise.all(pc1.getSenders().map(sender =>
  sender.replaceTrack((sender.track.kind == "audio")?
                      newStream.getAudioTracks()[0] :
                      newStream.getVideoTracks()[0])))
.then(() => log("Flip!"))
.catch(failed);

另请注意,从您的第一个 link:

function screenShare() {
    let screenConstraints = {video: {mediaSource: "screen"}};

    navigator.mediaDevices.getUserMedia(screenConstraints)
    .then(stream) {
        stream.getTracks().forEach(track) {
            screenStream = stream;
            screenSenders.push(pc1.addTrack(track, stream));
        });
    });
}

请注意,此示例调用 pc1.addTrack 而不是 pc1.addStream

反过来,删除 - pc1.removeTrack:

function stopScreenShare() {
    screenStream.stop();
    screenSenders.forEach(sender) {
        pc1.removeTrack(sender);
    });
}

Firefox 支持重新协商

Firefox 从未实现 removeStream,因为在重新协商实现时 the spec 已更改为 addTrackremoveTrack(有些人暗示它的删除过于仓促,所以它可能会回来)。 addStream 仍然适用于向后兼容,因为 Firefox 已经支持它。

请注意,removeTrack 混淆了从 addTrack 返回的 RTCRtpSender,因此 API 不是插入项。

polyfill 看起来像这样:

mozRTCPeerConnection.prototype.removeStream = function(stream) {
  this.getSenders().forEach(sender =>
      stream.getTracks().includes(sender.track) && this.removeTrack(sender));
}

转移到曲目是为了给用户更多的灵活性,因为曲目可能属于多个流,并且并非流中的所有曲目都需要通过 PeerConnection(或相同的 PeerConnection)发送。

请参阅此 answer to a different question 以了解适用于 Firefox 的重新协商示例。