WebRTC连接本地连接,但是通过互联网进行故障转移,有消息交换

时间:2015-12-25 01:48:01

标签: php mysql webrtc

希望一切顺利,

我在两台PC之间实现了一次WebRTC视频(仅限视频)会议,它在语言环境(Chrome和Opera)上运行得很好,虽然它在Firefox上不起作用。此外,它不适用于Chrome和Firefox上的Internet。两个对等体之间有一个信令消息交换(一个进入我家,另一个进入我家外),但视频不能在家外的PC上播放(通过互联网连接到PC进入房屋)。

使用以下变量

navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia;
window.RTCPeerConnection = window.RTCPeerConnection ||  window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
window.RTCIceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate ||  window.webkitRTCIceCandidate;
window.RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription;
var peerConnectionConfig = {'iceServers': [{'url': 'stun:stun.services.mozilla.com'}, {'url': 'stun:stun.l.google.com:19302'}]};            
var pcOptions = {optional: [{DtlsSrtpKeyAgreement: false}]

我通过调用函数创建了peerconnection:

function createPeerConnection(strdata){
//  strdata contains the descreption receved frome the signalings server
  log('createPeerConnection');
  peerConnection = new RTCPeerConnection(peerConnectionConfig,pcOptions);
  peerConnection.onicecandidate = gotIceCandidate;
  peerConnection.onaddstream = gotRemoteStream;
  peerConnection.onsignalingstatechange = function(ev){ 
     log('============================ '+JSON.stringify(ev));
  }
  data=data.replace(/@/gi, "\\r\\n"); 
  /*
    I use '@' to replace '\r\n'  to avaoid probleme with SQL requests, I 
     replaced all ocurences of '\r\n' by '@' (str.replace(/\\r\\n/gi, "@");
    ), I used the same things for answer, offer and candidates.I use PHP and
     MySQL to store all messages: candidates, offer and answer, 
  */

   var description= JSON.parse(data);
   try{
      log('createPeerConnectionDD');
      peerConnection.setRemoteDescription(new RTCSessionDescription(description), function() {
          log('set remote description success');
           //offere=true;
          for(i=0;i<msgss.length;i++){
             var data=msgss[i].data.replace(/@/gi, "\\r\\n");
             var candidate= JSON.parse(data);
             log('cand= '+JSON.stringify(candidate));
             peerConnection.addIceCandidate(new RTCIceCandidate(candidate));
          }
          peerConnection.createAnswer(gotDescription, createAnswerError);
       },function(err) {
          log('set remote description fail');
    });
  }catch(e){
    log(e);
  }
}

0 个答案:

没有答案