事件回调在 Mozilla Firefox 中未触发

event callback not firing in Mozilla Firefox

本文关键字:Firefox 回调 Mozilla 事件      更新时间:2023-09-26

我正在使用WebRTC为音频会议创建一个示例应用程序。我正在尝试做的是创建一个RTCpeerconnection对象并向其传递从调用方收到的远程描述。设置远程描述后,将为对等连接对象触发"onaddstream"事件,我们可以将远程描述中收到的流设置为某些音频/视频控件,如以下示例代码所示:

function call() {        
    chatHub.server.connect('receiver');        
    pc2 = new RTCPeerConnection(null, pcConstraints);
    pc2.onicecandidate = iceCallback2;
    pc2.onaddstream = gotRemoteStream; 
}
function gotDescription1(desc) { 
    var dessc = new RTCSessionDescription({ type: 'offer', sdp: desc });    
    pc2.setRemoteDescription(dessc);
}
function gotRemoteStream(e) { 
    //attaching the stream to UI controls
    audio2 = attachMediaStream(audio2, e.stream);
    audio2.play();  
    pc2.createAnswer(gotDescription2, onCreateSessionDescriptionError,sdpConstraints);
    callButton.disabled = true;
    hangupButton.disabled = false;
}
function gotDescription2(desc) {
    pc2.setLocalDescription(desc);      
}
function iceCallback2(event) {
    //---foo----
}

从示例代码中清楚的那样,过程从调用方法开始,该方法设置了一个PeerConnection对象并设置其事件回调,然后gotDescription1由某个代码元素调用,现在这是我们设置远程描述的地方,它应该在内部触发gotRemoteStream

所有这些都在除Firefox以外的主要浏览器中运行良好,为对象设置了远程描述,但没有回调gotStream

检查此内容以获取可能的解释。

https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection#Initializing_the_call

规范对onaddstream说:

每当远程对等方添加媒体流时,都会调用它。这只会作为 setRemoteTDescription 的结果触发。Onnaddstream在setRemoteDescription之后尽可能早地发生。此回调不会等待通过 SDP 协商接受或拒绝给定的媒体流。

Firefox 目前在协商完成之前不会触发 onaddstream;请参阅 Bug 1122306

无论如何,在你执行 SetLocalDescription() 之前,媒体不应该流动,所以只要让它不让你接受