【问题标题】:No ICE candidates generated when I run my local webRTC application on google chrome browser当我在 google chrome 浏览器上运行本地 webRTC 应用程序时没有生成 ICE 候选
【发布时间】:2019-08-29 13:27:19
【问题描述】:

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

我的 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 上,您可以在控制台日志中看到在每个“oniceccandidate”事件中收集的所有 ICE 候选者

在 Chrome 上,输出为空

【问题讨论】:

  • 我遇到了同样的问题,你找到解决办法了吗?

标签: javascript google-chrome firefox webrtc


【解决方案1】:

在调用createOffer()方法时需要传递options对象,例如:

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

【讨论】:

    【解决方案2】:

    创建对等连接时,您必须通过 STUN/TURN 服务器。

    否则,您将只能使用本地候选人,因此只能在本地连接

    var STUN = {
        'url': 'stun:stun.l.google.com:19302',
    };
    
    var iceServers = 
    {
        iceServers: [STUN]
    };
    
    var pc = new RTCPeerConnection(iceServers);
    

    【讨论】:

    • 是的,我明白,但我通过 LAN 在本地使用我的应用程序,我不需要使用 STUN 服务器。
    猜你喜欢
    • 2019-08-29
    • 2021-07-20
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多