当我在 google chrome 浏览器上运行本地 webRTC 应用程序时,没有生成 ICE 候选项

Flu*_*ubs 4 javascript firefox google-chrome webrtc

我有一个基本的 webRTC 应用程序,支持两个对等点之间的视频/音频通信和文件共享,当我在 Mozilla Firefox 上打开它时,该应用程序按预期运行,但当我在 Google Chrome 上运行它时,onicecandidate返回null

我的 RTCPeerConnection

        myConnection = new RTCPeerConnection();
Run Code Online (Sandbox Code Playgroud)

设置对等连接

myConnection.createOffer().then(offer => {
    currentoffer = offer
    myConnection.setLocalDescription(offer);
})
    .then(function () {
        myConnection.onicecandidate = function (event) {
            console.log(event.candidate);

            if (event.candidate) {
                send({
                    type: "candidate",
                    candidate: event.candidate
                });
            }
        };
        send({
            type: "offer",
            offer: currentoffer
        });
    })
    .catch(function (reason) {
        alert("Problem with creating offer. " + reason);
    });
Run Code Online (Sandbox Code Playgroud)

在 Mozilla Firefox 上,您可以在控制台中看到在每个“onicecandidate”事件中收集的所有 ICE 候选者日志

火狐输出

在 Chrome 上,输出为 null 镀铬输出

小智 8

调用方法时应该传递选项对象createOffer(),例如:

myConnection = new RTCPeerConnection();

var mediaConstraints = {
    'offerToReceiveAudio': true,
    'offerToReceiveVideo': true    
};

myConnection.createOffer(mediaConstraints).then(offer => {
        currentoffer = offer
        myConnection.setLocalDescription(offer);
    })
    ...// the rest of you code goes here    
Run Code Online (Sandbox Code Playgroud)

RTCRtpTransceiver或者,您可以在创建报价之前指定:

myConnection = new RTCPeerConnection();

myConnection.addTransceiver("audio");
myConnection.addTransceiver("video");

myConnection.createOffer().then(offer => {
        currentoffer = offer
        myConnection.setLocalDescription(offer);
    })
    ...// the rest of you code goes here 
Run Code Online (Sandbox Code Playgroud)

来源:
WebRTC 1.0
MDN RTCPeerConnection.createOffer()
MDN RTCPeerConnection.addTransceiver()
示例 - GitHub