当我在谷歌浏览器上运行本地WebRTC应用程序时,没有生成任何ICE候选项。

4

我有一个基本的WebRTC应用程序,支持两个对等方之间的视频/音频通信和文件共享。当我在Mozilla Firefox上打开它时,应用程序按预期运行,但是当我在Google Chrome上运行它时,onicecandidate返回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 中,您可以在控制台日志中查看每个“onicecandidate”事件收集的所有ICE候选项。
火狐输出如下图所示: Firefox output 而在 Chrome 中,输出为 null。 Chrome 输出如下图所示: Chrome output

我也遇到了同样的问题,你找到解决办法了吗? - alexward1230
2个回答

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    

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


0

在创建对等连接时,您必须传递STUN/TURN服务器。

否则,您将只能获得本地候选者,因此只能在本地连接。

var STUN = {
    'url': 'stun:stun.l.google.com:19302',
};

var iceServers = 
{
    iceServers: [STUN]
};

var pc = new RTCPeerConnection(iceServers);

3
我了解,但我使用我的应用程序在本地局域网上,并且不需要使用STUN服务器。 - FlubberFlubs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接