2017-01-29 20 views
1

繼方法here我試圖回答從iPhone模擬器(使用React Native)的Chrome瀏覽器啓動的音頻呼叫。RTCPeerConnection.iceConnectionState從檢查更改爲關閉

事件序列的總結:

  1. 接收到的呼叫信號
  2. 得到本地流
  3. 發送加入
  4. 接收到的遠程描述(報價)呼叫信號,
  5. 創建PeerConnection等
  6. 新增本地流
  7. 收到候選人
  8. 加入候選
  9. 7和8重複15次(即總共16次)
  10. onnegotiationneeded觸發
  11. signalingState改變爲具有遠程要約
  12. onaddstream觸發
  13. 的回調函數setRemoteDescription被觸發,創建答案。
  14. signalingState變成變成穩定
  15. iceconnectionstate檢查
  16. onicecandidate引發的第一次。
  17. 來自15名候選人
  18. onicecandidate第二次觸發。該名候選人是null
  19. iceconnectionstate變成關閉

步驟7,8,9 5月6日之後的不同地方和19

之前,我一直停留在這個問題上相當而。我甚至不知道現在要調試什麼。關閉連接的可能原因是什麼?如果需要,我可以發佈更多日誌。

一個觀察是,對應於iceconnectionstatechange兩個RTCEvent具有以下屬性:

isTrusted:false

目標RTCPeerConnection有

iceConnectionState:"closed" 
iceGatheringState:"complete" 

這裏是我的功能來處理remoteOffer和remoteCandidates:

WebRTCClass.prototype.onRemoteOffer = function(data) { 
    var ref; 
    if (this.active !== true) { 
    return; 
    } 
    var peerConnection = this.getPeerConnection(data.from); 
    console.log('onRemoteOffer', data,peerConnection.signalingState); 

    if (peerConnection.iceConnectionState !== 'new') { 
    return; 
    } 
    var onSuccess = (function(_this){ 
    return function(){ 
     console.log("setRemoteDescription onSuccess function"); 
     _this.getLocalUserMedia((function(_this) { 
      return function(onSuccess,stream) { 
      peerConnection.addStream(_this.localStream); 
      var onAnswer = (function(_this) { 
       return function(answer) { 
       var onLocalDescription = function() { 
        return _this.transport.sendDescription({ 
        to: data.from, 
        type: 'answer', 
        ts: peerConnection.createdAt, 
        description: { 
         sdp: answer.sdp, 
         type: answer.type 
        } 
        }); 
       }; 
       return peerConnection.setLocalDescription(new RTCSessionDescription(answer), onLocalDescription, _this.onError); 
       }; 
      })(_this); 
      return peerConnection.createAnswer(onAnswer, _this.onError); 
      } 
     })(_this) 
    ); 
    } 
    })(this); 
    return peerConnection.setRemoteDescription(new RTCSessionDescription(data.description),onSuccess,console.warn); 
}; 

WebRTCClass.prototype.onRemoteCandidate = function(data) { 
    var peerConnection, ref; 
    if (this.active !== true) { 
    return; 
    } 
    if (data.to !== this.selfId) { 
    return; 
    } 
    console.log('onRemoteCandidate', data); 
    peerConnection = this.getPeerConnection(data.from); 
    if ((ref = peerConnection.iceConnectionState) !== "closed" && ref !== "failed" && ref !== "disconnected" && ref !== "completed") { 
    peerConnection.addIceCandidate(new RTCIceCandidate(data.candidate)); 
    } 
}; 
+0

我懷疑[react-native-webrtc](https://github.com/oney/react-native-webrtc)與原始webrtc軟件包略有不同。但我對此很陌生,所以我也可能不遵循webrtc協議的正確軌道 –

+0

模擬器和瀏覽器是否位於同一網絡中?您是否在createpeerconnection中使用任何STUN或TURN服務器?你有ios或chrome libjingle日誌嗎? – manishg

+0

模擬器和瀏覽器位於同一網絡(127.0.0.1)。對等連接是通過將STUN服務器設置爲'stun:stun.l.google.com:19302'創建的。我不確定什麼是libjingle日誌或如何打開它。你能否指點我一個參考?我用代碼生成了一些更詳細的日誌。 –

回答

0

我發現,如果我叫下面的兩個功能一個接一個,那麼它會工作。

peerConnection.setRemoteDescription(new RTCSessionDescription(data.description),onSuccess,console.warn); 
(...definition of onAnswer ...) 
peerConnection.createAnswer(onAnswer, this.onError); 

我以前的代碼onSuccess回調setRemoteDescription中調用createAnswer。這對react-native-webrtc演示沒有影響,但對Rocket.Chat沒有影響。仍然不完全理解它。但是我的項目現在可以繼續。