我有一个问题,我的onicecandidate事件在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)
});
})
我的错误是声明了常量RTCPeerConnection,因为我知道在Opera浏览器中RTCPeerConnection是一个全局类(它不在window.RTCPeerConnection等中),我需要替换这一行。
const RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
对此
const RTCPeerConnection = RTCPeerConnection || window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;