首页 > 解决方案 > 歌剧浏览器上未调用 webrtc onicecandidate

问题描述

我有一个问题,我的 onececandidate 事件没有在 Opera 浏览器中被触发,这是我的代码

我试过getUserMedia以不同的方式(通过addStream和addTrack)将流添加到RTCPeerConnection,设置offerToReceiveAudio和offerToReceiveVideo,这些都没有帮助

代码

const RTCPeerConnection =  window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
const RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.RTCSessionDescription;
const RTCIceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate;

const mediaConstraints = {
    offerToReceiveAudio: true,
    offerToReceiveVideo: false
}

const options = {
    iceServers: [
        {
          urls: 'turn:74.11.232.11:3478',
          credential: 'password',
          username: 'username'
        }
    ],
};

function getUserMedia(callback) {
    navigator.mediaDevices.getUserMedia({
        audio: true,
        video: false
    }).then(callback);
}

getUserMedia((stream) => {
     localStream = stream

     localStream.getAudioTracks()[0].enabled = false

     WebRtcPeerConnection = new RTCPeerConnection(options)

     WebRtcPeerConnection.addStream(stream)

     WebRtcPeerConnection.createOffer(mediaConstraints).then((offer) => {
         console.log(offer.sdp)
         WebRtcPeerConnection.setLocalDescription(offer);
         WebRtcPeerConnection.onicecandidate = onRoomIceCandidate
         onRoomOffer(null, offer)
     });
})

标签: javascriptwebrtc

解决方案


我的错误是声明了常量 RTCPeerConnection,正如我在 Opera 浏览器中理解的那样 RTCPeerConnection 是一个全局类(不在 window.RTCPeerConnection 等中)我需要替换这一行

const RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;

对此

const RTCPeerConnection = RTCPeerConnection || window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;


推荐阅读