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

标签 javascript google-chrome firefox webrtc

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

我的 RTCPeerConnection

        myConnection = new RTCPeerConnection();

建立对等连接

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);
    });

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

Firefox output

在 Chrome 上,输出为空 Chrome output

最佳答案

在调用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    

或者,您可以在创建报价之前指定 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 

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

关于javascript - 当我在 google chrome 浏览器上运行我的本地 webRTC 应用程序时,没有生成 ICE 候选人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55572712/

相关文章:

javascript - 如何使用 d3.js 从外部 json 文件读取特定属性?

php - 注册表单在 Chrome 和移动设备上无法使用

html - anchor 元素在 Firefox 和 Chrome 中不起作用

javascript - 如何将 puppeteer-core 与 Electron 一起使用?

javascript - Jquery 追加 div 到新的 div

javascript - 如何每5秒更改一次背景

html - Firefox/Chrome 上的字体大小和缩进不一致

html - Chrome 40 中的@Font-Face 渲染

javascript - 火狐插件: How can I paste clipboard to a textarea?

css - 无法获得响应式 SVG 图像掩码以在 Firefox 中工作