2016-04-19 14 views
0
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(); 

と呼ばれる)、合計20%のユーザーがない任意の応答を有するエラーイベントのFireFoxのWebRTCのcreateofferは

//この私の完全なjsのコード

関数TESTRTCPeerConnection(設定を含めます){ VARオプション= { iceServers:ヌル、 onOfferSDP:ヌル、 onOfferSDPError:ヌル、 氷上:ヌル、 onAnswerSdpSucess:ヌル、 onAnswerSdpError:ヌル、 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'、 URL: 'stun:stun.l.google.com:19302' }); var self = this; var getCandidate = false; iceServers = { iceServers:iceServers }; varオプション= { オプション:[] }; 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" })。

+0

Firefoxのバージョン42まで最大のバージョンすべてにこの問題があります – duzz

答えて

1

Firefoxで私のコードが正常に機能しているといいはずです(「OK」と表示されます)。つまり、コードには問題があります。

ので、何がガベージコレクトされるのを防ぐことができ、(それはすぐに行います)promiseCreateOffer戻り一度ので、あなたがpeerにゼロの参照を持って、私はこれはテスト機能で実現するが、peerpromiseCreateOffer内のローカル変数でありますか?

ガベージコレクションはバックグラウンドで行われるため、ユーザーの20%に起こっていることを説明できる唯一のことが考えられます(私はそれを自分で観察しませんでしたが)。

参考にして参考にしてください。

その他のニット:

  • 次の2つの引数を取るthenに3つの機能を渡しています。
  • 対応する約束を返さずに関数内で約束を開始しないでください。
+0

ありがとう、これは私のjsコードの一部であり、テストコードとして、var peerは実際にはコード側のエラーです。 – duzz

+0

私は完全なjsを貼り付けて、私はいくつかの他の理由があるかもしれないと思う、ユーザーデータは、FireFoxのバージョン42--47 統計情報は、ユーザーが約15%の応答がないときにこのスクリプトを呼び出すことを示しています、質問を解決する。 FireFoxバージョン<= 41、FireFox> = 48 – duzz

+0

@duzzコードが多すぎます。ごめんなさい。あなたは問題を再現するのに必要な最小限のコードを絞り込むことになっています。また、コードのフォーマットが正しくありません。 – jib

関連する問題