gpt4 book ai didi

javascript - FireFox webrtc createoffer 没有调用任何回调

转载 作者:行者123 更新时间:2023-12-03 07:08:24 25 4
gpt4 key购买 nike

var PeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var iceServers = [];
var optional = {
optional: []
};
var constraints = {
'offerToReceiveAudio': true,
'offerToReceiveVideo': true
};
var promiseCreateOffer = function() {
var peer ;
try{
peer = new PeerConnection(iceServers, optional);
}
catch(err){
console.log('error ',err);
return ;
}
peer.createOffer(constraints).then(
function(offer) {
return peer.setLocalDescription(offer);
})
.then(function() {
console.log('ok ',peer.localDescription);
},
function(){
// when promise rejected state,called
console.log('rejected ');
},function(){
//when promise progress state become rejected,called
console.log('progress ');
}
)
.catch(function(reason) {
// An error occurred
console.log('onSdpError: ', reason);
});
}
promiseCreateOffer();

调用promiseCreateOffer()时,共有20%的用户没有任何响应,包括错误事件

//这是我完整的js代码

函数 TESTRTCPeerConnection(config) { 变量选项= { 冰服务器:空, onOfferSDP:空, onOfferSDPError:空, onICE:空, onAnswerSdpSucess:空, onAnswerSdpError:空, onRemoteStreamEnded:空, onRemoteStream: 空 }; var 对等体; window.moz = !!navigator.mozGetUserMedia; var w = 窗口; var PeerConnection = w.mozRTCPeerConnection || w.webkitRTCPeerConnection || w.RTCPeerConnection; var SessionDescription = w.mozRTCSessionDescription || w.RTCSessionDescription; var IceCandidate = w.mozRTCIceCandidate || w.RTCIceCandidate; variceServers = []; IceServers.push({ 网址: 'stun:stun.l.google.com:19302', 网址:'stun:stun.l.google.com:19302' }); var self = this; var getCandidate = false; 冰服务器 = { 冰服务器: 冰服务器 }; 变量可选 = { 选修的: [] }; var nowCreatSdpTime;

self.sendUT = function (msg) {
msg.liveuuid = config.liveuuid;
console.log('TestWebrtcSendUT=', msg);
//static function
};

try {
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestNewPeerConnection'
});
peer = new PeerConnection(iceServers, optional);
if (!peer) {
console.error('[TESTRTCPeerConnection]peer new fail');
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestNewPeerConnectionFail'
});
return;
}
} catch (err) {
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'CatchTestNewPeerConnectionFail',
error: err
});
return;
}
peer.onicecandidate = function (event) {
if (event.candidate) {
if (options.onICE) {
options.onICE(event.candidate);
}
if (getCandidate === false) {
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestOnIceCandidateEvent',
time: new Date().getTime() - nowCreatSdpTime
});
getCandidate = true;
}
console.log('[TESTRTCPeerConnection] candidate:', JSON.stringify(event.candidate));
}
};

peer.onsignalingstatechange = function (state) {
console.log('[TESTRTCPeerConnection] onsignalingstatechange', state);
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestOnSignalingstatechange',
state: peer.signalingState,
time: new Date().getTime() - nowCreatSdpTime
});
};

peer.onaddstream = function (event) {
var remoteMediaStream = event.stream;

// onRemoteStreamEnded(MediaStream)
remoteMediaStream.onended = function () {
if (options.onRemoteStreamEnded) {
options.onRemoteStreamEnded(remoteMediaStream);
}
};
// onRemoteStream(MediaStream)
if (options.onRemoteStream) {
options.onRemoteStream(remoteMediaStream);
}

console.log('[TESTRTCPeerConnection] on:add:stream', remoteMediaStream);
};

var constraints = {
offerToReceiveAudio: true,
offerToReceiveVideo: true
};

self.createOffer = function () {
peer.createOffer(function (sessionDescription) {

self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestCreateOfferSucess',
time: new Date().getTime() - nowCreatSdpTime
});
try {
peer.setLocalDescription(sessionDescription);

} catch (error) {
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'CatchTestCreateOfferSucessError',
time: new Date().getTime() - nowCreatSdpTime
});
}
console.log('[TESTRTCPeerConnection] offer-sdp', sessionDescription.sdp);
},
function (message) {
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestCreateOfferFail',
time: new Date().getTime() - nowCreatSdpTime
});
console.error('[TESTRTCPeerConnection] onSdpError:', message);
},
constraints);
};

self.promiseCreateOffer = function () {
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'promiseTestCreateOffer',
time: new Date().getTime() - nowCreatSdpTime
});
peer.createOffer(constraints).then(
function (offer) {
console.log('[TESTRTCPeerConnection] promiseCreateOffer onSdp sucess:', offer);
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'promiseTestCreateOfferSucess',
time: new Date().getTime() - nowCreatSdpTime
});
return peer.setLocalDescription(offer);
})
.then(
function () {
console.log('[TESTRTCPeerConnection] promiseCreateOffer onSdp: ', peer.localDescription);
},
function () {
// rejected
console.log('[TESTRTCPeerConnection] promiseCreateOffer rejected ');
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'promiseTestCreateOfferReject',
time: new Date().getTime() - nowCreatSdpTime
});
},
function () {
// progress
console.log('[TESTRTCPeerConnection] promiseCreateOffer progress ');
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'promiseTestCreateOfferProgress',
time: new Date().getTime() - nowCreatSdpTime
});
})
.catch(function (reason) {
// An error occurred, so handle the failure to connect
console.log('[TESTRTCPeerConnection] promiseCreateOffer onSdpError: ', reason);
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'promiseTestCreateOfferCatchFail',
time: new Date().getTime() - nowCreatSdpTime,
error: reason
});
});
};

self.addAnswerSDP = function (sdp) {
var answer = new SessionDescription({
type: 'answer',
sdp: sdp
});
console.log('[TESTRTCPeerConnection] adding answer-sdp', sdp);
peer.setRemoteDescription(answer, self.onAnswerSdpSuccess, self.onAnswerSdpError);
};

self.onAnswerSdpSuccess = function (msg) {
console.log('[TESTRTCPeerConnection] onSdpSuccess', msg);
if (options.onAnswerSdpSuccess) {
options.onAnswerSdpSuccess(msg);
}
};

self.onAnswerSdpError = function (error) {
console.log('[TESTRTCPeerConnection] onAnswerSdpError', error);
if (options.onAnswerSdpError) {
options.onAnswerSdpError(error);
}
};

self.addICE = function (candidate) {
peer.addIceCandidate(new IceCandidate({
sdpMLineIndex: candidate.sdpMLineIndex,
candidate: candidate.candidate
}));
console.log('[TESTRTCPeerConnection] adding-ice', candidate.candidate);
};

nowCreatSdpTime = new Date().getTime();
console.log('[TESTRTCPeerConnection] createoffer start ', nowCreatSdpTime);
self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestCreateOfferStart'
});

if (window.moz) {
self.promiseCreateOffer();
} else {
self.createOffer();
}

console.log('[TESTRTCPeerConnection] createoffer end ', (new Date().getTime()) - nowCreatSdpTime);

self.sendUT({
type: 'others',
modId: 'webrtc',
country: 'country',
position: 'TestCreateOfferEnd',
time: (new Date().getTime()) - nowCreatSdpTime
});

}var mywebrtc = 新 TESTRTCPeerConnection({ liveuuid:"123456"});

最佳答案

我应该说你的代码在 Firefox 中对我来说工作得很好(我看到“ok”)。也就是说,您的代码有问题。

我意识到这是一个测试函数,但是 peerpromiseCreateOffer 内的局部变量,因此一旦 promiseCreateOffer 返回(它会立即执行) ),您对 peer 的引用为零,那么什么阻止它被垃圾收集呢?

垃圾收集发生在后台,所以这是我能想到的唯一可以解释像你所说的情况发生在 20% 的用户身上的事情(尽管我自己没有观察到)。

尝试将引用移出,看看是否有帮助。

其他尼特:

  • 您将三个函数传递给 then,它们接受两个参数。
  • 不要在函数内启动 Promise 链而不返回相应的 Promise。

关于javascript - FireFox webrtc createoffer 没有调用任何回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36714152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com