2

WebRTC 在本地服务器上运行良好,但是当我将它部署到 VPS 时,我只能看到 localVideo,而 romoteVideo 只是一个黑色方块。

我的 webrtc 代码是:

var socket = io.connect();

var remotevid = document.getElementById('webrtc-remotevid');
var localStream = null;
var peerConn = null;
var started = false;
var channelReady = false;
var mediaConstraints = {'mandatory': {
                        'OfferToReceiveAudio':true, 
                        'OfferToReceiveVideo':true }};
var isVideoMuted = true;

var videoCamera = new tracking.VideoCamera().hide().render().renderVideoCanvas();

function setLocalAndSendMessage(sessionDescription) {
  peerConn.setLocalDescription(sessionDescription);
  console.log("Sending: SDP");
  console.log(sessionDescription);
  socket.json.send(sessionDescription);
}

function createOfferFailed() {
  console.log("Create Answer failed");
}

// start the connection upon user request
   function connectvid() {

    console.log(localStream);
    if (!started && localStream && channelReady) {
      createPeerConnection();
      started = true;
      peerConn.createOffer(setLocalAndSendMessage, createOfferFailed, mediaConstraints);
    } else {
      alert("Local stream not running yet - try again.");
    }

}

// stop the connection upon user request
function hangUp() {
  console.log("Hang up.");    
  socket.json.send({type: "bye"});
  stop();
}

function stop() {
  peerConn.close();
  peerConn = null;
  started = false;    
}

// socket: channel connected
socket.on('connect', onChannelOpened)
      .on('message', onMessage);

function onChannelOpened(evt) {
  console.log('Channel opened.');
  channelReady = true;
}

function createAnswerFailed() {
  console.log("Create Answer failed");
}
// socket: accept connection request
function onMessage(evt) {
  if (evt.type === 'offer') {
    console.log("Received offer...")
    if (!started) {
      createPeerConnection();
      started = true;
    }
    console.log('Creating remote session description...' );
    peerConn.setRemoteDescription(new RTCSessionDescription(evt));
    console.log('Sending answer...');
    peerConn.createAnswer(setLocalAndSendMessage, createAnswerFailed, mediaConstraints);

  } else if (evt.type === 'answer' && started) {
    console.log('Received answer...');
    console.log('Setting remote session description...' );
    peerConn.setRemoteDescription(new RTCSessionDescription(evt));

  } else if (evt.type === 'candidate' && started) {
    console.log('Received ICE candidate...');
    var candidate = new RTCIceCandidate({sdpMLineIndex:evt.sdpMLineIndex, sdpMid:evt.sdpMid, candidate:evt.candidate});
    console.log(candidate);
    peerConn.addIceCandidate(candidate);

  } else if (evt.type === 'bye' && started) {
    console.log("Received bye");
    stop();
  }
}

function createPeerConnection() {
  console.log("Creating peer connection");
  RTCPeerConnection = webkitRTCPeerConnection || mozRTCPeerConnection;
  var pc_config = {"iceServers":[]};
  try {
    peerConn = new RTCPeerConnection(pc_config);

  } catch (e) {
    console.log("Failed to create PeerConnection, exception: " + e.message);
  }
  // send any ice candidates to the other peer
  peerConn.onicecandidate = function (evt) {
    if (event.candidate) {
      console.log('Sending ICE candidate...');
      console.log(evt.candidate);
      socket.json.send({type: "candidate",
                        sdpMLineIndex: evt.candidate.sdpMLineIndex,
                        sdpMid: evt.candidate.sdpMid,
                        candidate: evt.candidate.candidate});
    } else {
      console.log("End of candidates.");
    }
  };
  console.log('Adding local stream...');
  console.log(localStream);
  peerConn.addStream(localStream);

  // peerConn.addStream(localScreen);

  peerConn.addEventListener("addstream", onRemoteStreamAdded, false);

  // when remote adds a stream, hand it on to the local video element
  function onRemoteStreamAdded(event) {
    console.log("Added remote stream");
    remotevid.src = window.URL.createObjectURL(event.stream);
  }

  // when remote removes a stream, remove it from the local video element
}

socket.on('disconnect', function() {
  remotevid.src = "";
  started = false;
})

我不知道它有什么问题,它是否与 chrome 标志有问题?

4

1 回答 1

0

它在本地工作,因为您可能只在本地网络中尝试过。当它公开时,IP 差异、代理都会浮现出来。您应该首先设置一个 stun 服务器。有几个公共眩晕服务器可用。

一个例如是 stun.l.google.com:19302

尽管如此,如果您遇到问题,请检查您遇到的确切错误。

于 2014-03-13T11:15:41.623 回答