FireFox webrtc createoffer没有任何回调

时间:2016-04-19 09:24:51

标签: javascript webrtc

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代码

function TESTRTCPeerConnection(config){     var options = {         iceServers:null,         onOfferSDP:null,         onOfferSDPError:null,         onICE:null,         onAnswerSdpSucess:null,         onAnswerSdpError:null,         onRemoteStreamEnded:null,         onRemoteStream:null     };     var peer;     window.moz = !! navigator.mozGetUserMedia;     var w = window;     var PeerConnection = w.mozRTCPeerConnection || w.webkitRTCPeerConnection || w.RTCPeerConnection;     var SessionDescription = w.mozRTCSessionDescription || w.RTCSessionDescription;     var IceCandidate = w.mozRTCIceCandidate || w.RTCIceCandidate;     var iceServers = [];     iceServers.push({         url:' stun:stun.l.google.com:19302',         网址:' stun:stun.l.google.com:19302'     });     var self = this;     var getCandidate = false;     iceServers = {         iceServers:iceServers     };     var optional = {         可选的: []     };     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 = new TESTRTCPeerConnection({     liveuuid:" 123456" });

1 个答案:

答案 0 :(得分:1)

我应该说你的代码在我的Firefox中工作正常(我看到'确定')。也就是说,您的代码存在问题。

我意识到这是一个测试函数,但是peerpromiseCreateOffer中的局部变量,所以一旦promiseCreateOffer返回(它立即执行),你就没有{{1}的引用1}},那么是什么阻止它被垃圾收集?

垃圾收集在后台发生,所以这是我唯一可以想到的,这可能解释了你所说的20%的用户正在发生的事情(虽然我自己没有观察到它)。

尝试移动引用以查看它是否有帮助。

其他尼特:

  • 你将三个函数传递给peer,它们有两个参数。
  • 不要在函数内部启动promise链而不返回相应的promise。
相关问题