不调用WebRTC ontrack

时间:2018-07-03 00:33:02

标签: javascript webrtc

我对WebRTC感到困惑。我只是在尝试虚拟对等连接,但远程连接什么也没收到。问题是函数ontrack没有被触发,我也不知道为什么?我该如何运作?

在Chromium和Firefox上均不起作用

var localVideo = document.querySelector('#local'),
    remoteVideo = document.querySelector('#remote'),
    localConnection,remoteConnection;

if (hasUserMedia()){
   navigator.getUserMedia({video: true, audio:false},function(stream){
      localVideo.srcObject = stream;

      if (hasRTCPeerConnection()){
        startPeerConnection(stream);
      } else {
        alert("WebRTC not supported!");
      }
   },function(error){
     alert("Camera capture failed!")
   });
}  else {
  alert("WebRTC not supported!");
}

function startPeerConnection(stream){
  var configuration ={
    offerToReceiveAudio: true,
    offerToReceiveVideo: true
  }
  localConnection = new RTCPeerConnection(configuration);
  remoteConnection = new RTCPeerConnection(configuration);
   stream.getTracks().forEach(
   function(track) {
     localConnection.addTrack(
       track,
       stream
     );
   }
);

  remoteConnection.ontrack = function(e){
    remoteVideo.srcObject = e.streams[0];
  };

  localConnection.onicecandidate = function(event){
    if (event.candidate){
      remoteConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
    }
  };

  remoteConnection.onicecandidate = function(event){
    if (event.candidate){
      localConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
    }
  };

  localConnection.createOffer(function(offer){
    localConnection.setLocalDescription(offer);
    remoteConnection.setRemoteDescription(offer);

    remoteConnection.createAnswer(function(offer){
      remoteConnection.setLocalDescription(offer);
      localConnection.setRemoteDescription(offer);
    });
  });
}

1 个答案:

答案 0 :(得分:2)

我已经对您的代码进行了一些更正,现在可以了。此外,我还更改了带有promises的回调。

var localVideo = document.querySelector('#local'),
remoteVideo = document.querySelector('#remote'),
localConnection, remoteConnection;

navigator.getUserMedia({video: true, audio: false}, function (stream) {
    localVideo.srcObject = stream;
    startPeerConnection(stream);
}, function (error) {
    alert("Camera capture failed!")
});


function startPeerConnection(stream) {
    var configuration = {
        offerToReceiveAudio: true,
        offerToReceiveVideo: true
    }
    localConnection = new RTCPeerConnection({configuration: configuration, iceServers: []});
    remoteConnection = new RTCPeerConnection(configuration);
    stream.getTracks().forEach(
        function (track) {
            localConnection.addTrack(
                track,
                stream
            );
        }
    );

    remoteConnection.ontrack = function (e) {
        remoteVideo.srcObject = e.streams[0];
    };


    // Set up the ICE candidates for the two peers

    localConnection.onicecandidate = e => !e.candidate
        || remoteConnection.addIceCandidate(e.candidate)
            .catch(e => {
                console.error(e)
            });

    remoteConnection.onicecandidate = e => !e.candidate
        || localConnection.addIceCandidate(e.candidate)
            .catch(e => {
                console.error(e)
            });

    localConnection.createOffer()
        .then(offer => localConnection.setLocalDescription(offer))
        .then(() => remoteConnection.setRemoteDescription(localConnection.localDescription))
        .then(() => remoteConnection.createAnswer())
        .then(answer => remoteConnection.setLocalDescription(answer))
        .then(() => localConnection.setRemoteDescription(remoteConnection.localDescription))
        .catch(e => {
            console.error(e)
        });
}