Javascript 未调用WebRTC ontrack

Javascript 未调用WebRTC ontrack,javascript,webrtc,Javascript,Webrtc,我觉得被WebRTC卡住了。我只是在尝试一个虚拟的点对点连接,但是远程连接没有收到任何东西。问题是ontrack函数没有被激发,我不知道为什么?我怎样才能让它工作 它在Chromium和Firefox上都不起作用 var localVideo = document.querySelector('#local'), remoteVideo = document.querySelector('#remote'), localConnection,remoteConnection;

我觉得被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);
    });
  });
}

我已经对你的代码做了一些修改,现在它可以正常工作了。我还用承诺更改了回调

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)
        });
}