WebRTC使用承诺 - 两端都没有看到远程视频

时间:2016-04-28 09:49:27

标签: webrtc

我之前发过一些关于这个问题的问题。那时我有两个独立的呼叫者和接收者节目。我还使用了老式的回调API。感谢@jib对该帖子的帮助,我能够理解一些根本性变化的必要性。我重写了程序,使其成为调用者和接收者的集成程序,并使用了WebRTC promises API。我的问题是我没有从任何一端获得远程视频。我理解的一部分但不知道解决方案:接收器首先不为视频创建SDP,仅用于音频。调用者部分确实为视频和音频创建SDPS,但在接收器端没有为远程流生成事件。

我已通过控制台日志检查核心功能是否有效。提供SDP创建,发送,接收,回答SDP创建,发送,接收等。候选人也得到交换和添加。但永远不会触发.onaddstream事件处理程序。显示了本地视频,但这很简单。

我花了很多时间在这上面。我只需要获得令人兴奋的感觉,即在两端看到远程视频,这让我一直都在前进。任何帮助都将得到满意的评价。

<script>
$(document).ready(function () {

  var iceCandidates = [], countIceCandidates=0;
  var socket = io.connect();
  socket.on('connect',function() { console.log("Socket connected"); });

  var pc = new RTCPeerConnection({"iceServers":[{"url":"stun:stun.l.google.com:19302"}]});

  //If remote video stream comes in, display it in DIV vid2
  pc.onaddStream = function (event) {
     stream = event.stream;
     var video = $('#vid2'); 
     video.attr('src', URL.createObjectURL(stream));
     video.onloadedmetadata = function(e) { video.play(); }
  }

  //Display media in both Caller and Receiver
  navigator.mediaDevices.getUserMedia({ audio: true, video: true })
  .then(function(stream) {
      var video = $('#vid1'); 
      video.attr('src', URL.createObjectURL(stream));
      video.onloadedmetadata = function(e) { video.play(); };
      pc.addStream(stream);
   })
  .catch(function(err) { console.log(err);});


 //INITIATE CALL
 $('#call').click(function() {
   pc.createOffer({ offerToReceiveVideo: true, offerToReceiveAudio: true })
   .then(function(offer) {
     localSessionDescription = new RTCSessionDescription(offer);
     pc.setLocalDescription(localSessionDescription)
     .then (function() { socket.emit('sdpOffer',localSessionDescription); })
     .catch(function(err) { console.log("Error in setLocalDescription"); console.log(err); })
     .catch(function(err) { console.log("Error in createOffer"); console.log(err); })
   });
 })

  pc.onicecandidate = function (event) {
    socket.emit('candidate',event.candidate);
  };

  socket.on('candidate',function (data) {
  if (data != null) {
    pc.addIceCandidate(new RTCIceCandidate(data))
     .then(function() { console.log("peer candidate added");})
     .catch(function(err) {console.log(err); console.log("Error during peer candidate addition");});
   }
  });

  socket.on('disconnect',function() { alert("Disconnected"); });

  function error(err) {
   console.log("The following error occurred: " + err.name);
  }

  socket.on('sdpAnswer',function(data) {  
    sdpAnswer = new RTCSessionDescription(data.sdpAnswer);
    pc.setRemoteDescription(sdpAnswer)
    .then(function() { console.log("Answer SDP Set:"); console.log(sdpAnswer); })
    .catch(function(err) { console.log("Error enountered when setting remote SDP Answer"); console.log(err)});
  });

  socket.on('sdpOffer', function(data) {
    sdpOffer = new RTCSessionDescription(data.sdpOffer);
    pc.setRemoteDescription(sdpOffer)
    .then(function() { console.log("Remote SDP set in receiver"); 
      pc.createAnswer()
        .then(function(sdpAnswer) {
           localSessionDescription = new RTCSessionDescription(sdpAnswer);
       socket.emit('sdpAnswer',localSessionDescription);
       pc.setLocalDescription(localSessionDescription)
         .then(function(){
                 console.log("Local SDP Description set in receiver:"); 
               })
            .catch(function(err) { console.log("Error enountered when setting local SDP in receiver"); console.log(err)});
    })
         .catch(function(err) { console.log("Error enountered when creating answer SDP in receiver"); console.log(err)});
     });
   });
}); //End of document.ready function

</script>

在服务器端(仅限相关代码)。我已经包含在这里以防万一有任何数据类型相关的问题 - 对象类型等在通过服务器发送时会发生变化。

 io.sockets.on('connection', function(socket) {
   socket.on('sdpOffer', function(data) {
     sdpOffer = data.sdp;
     socket.broadcast.emit('sdpOffer',{"sdpOffer":data});
   });
   socket.on('sdpAnswer', function(data) {
     sdpAnswer = data.sdp;
     socket.broadcast.emit('sdpAnswer',{"sdpAnswer":data});
   });
   socket.on('candidate', function(data) {
     socket.broadcast.emit('candidate',data);
   });
 });

1 个答案:

答案 0 :(得分:3)

pc.onaddstream重命名为DSN=Excel Files;DBQ=C:\Temp\Alma.xlsx;DefaultDir=C:\Temp;DriverId=1046;MaxBufferSize=2048;PageTimeout=5;

相关问题