WebRTC - peer2peer连接停止工作

时间:2014-07-16 09:23:06

标签: javascript webrtc

我开始测试WebRTC并开始使用https://github.com/samdutton/simpl/

中的示例

我正在测试这个例子: https://github.com/samdutton/simpl/blob/master/rtcpeerconnection/

我运行了python的简单网络服务器:python -m SimpleHTTPServer

然后尝试了这个例子在我的结尾。它确实如此。然后我修改了代码的一部分(-表示删除了行,+添加了行代码:

function start() {
  trace("Requesting local stream");
  startButton.disabled = true;
  navigator.getUserMedia = navigator.getUserMedia ||
      navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
  - navigator.getUserMedia({video:true}, gotStream,
  + navigator.getUserMedia({video:true, audio:true}, gotStream,
    function(error) {
      trace("navigator.getUserMedia error: ", error);
    });
}

当我跑步时,视频停止工作,但我能听到我的声音。然后我改变了原来的样子,但由于某种原因,它一直停止工作。

后来我注意到我的相机由于某种原因被阻挡了,然后我解锁了它,但它仍然无效。我甚至用github中的原始代码替换了整个代码(即使它是相同的)。

我得到错误没有任何错误指示,所以我不知道什么是错的:

463.069: Requesting local stream main.js:31
465.550: navigator.getUserMedia error:

我也想过可能以某种方式在Chromium中禁用了getUserMedia(我使用 - 版本34.0.1847.116 Ubuntu 14.04 aura(260972))

所以我在Chromium设置中启用了getUserMedia。但仍然没有。

您可以查看此链接的工作方式:http://www.simpl.info/rtcpeerconnection/

指出错误的行(31行):

console.log((performance.now() / 1000).toFixed(3) + ": " + text);

得到这一行的功能:

var total = '';
function trace(text) {
  total += text;
  console.log((performance.now() / 1000).toFixed(3) + ": " + text);
}

整个javascript代码:

var localStream, localPeerConnection, remotePeerConnection;

var localVideo = document.getElementById("localVideo");
var remoteVideo = document.getElementById("remoteVideo");

localVideo.addEventListener("loadedmetadata", function(){
trace("Local video currentSrc: " + this.currentSrc +
        ", videoWidth: " + this.videoWidth +
        "px,  videoHeight: " + this.videoHeight + "px");
});

remoteVideo.addEventListener("loadedmetadata", function(){
trace("Remote video currentSrc: " + this.currentSrc +
        ", videoWidth: " + this.videoWidth +
        "px,  videoHeight: " + this.videoHeight + "px");
});

var startButton = document.getElementById("startButton");
var callButton = document.getElementById("callButton");
var hangupButton = document.getElementById("hangupButton");
startButton.disabled = false;
callButton.disabled = true;
hangupButton.disabled = true;
startButton.onclick = start;
callButton.onclick = call;
hangupButton.onclick = hangup;

var total = '';
function trace(text) {
  total += text;
  console.log((performance.now() / 1000).toFixed(3) + ": " + text);
}

function gotStream(stream){
  trace("Received local stream");
  localVideo.src = URL.createObjectURL(stream);
  localStream = stream;
  callButton.disabled = false;
}

function start() {
  trace("Requesting local stream");
  startButton.disabled = true;
  navigator.getUserMedia = navigator.getUserMedia ||
    navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
  navigator.getUserMedia({video:true}, gotStream,
    function(error) {
      trace("navigator.getUserMedia error: ", error);
    });
}

function call() {
  callButton.disabled = true;
  hangupButton.disabled = false;
  trace("Starting call");

  if (localStream.getVideoTracks().length > 0) {
    trace('Using video device: ' + localStream.getVideoTracks()[0].label);
  }
  if (localStream.getAudioTracks().length > 0) {
    trace('Using audio device: ' + localStream.getAudioTracks()[0].label);
  }

  var servers = null;

  localPeerConnection = new webkitRTCPeerConnection(servers);
  trace("Created local peer connection object localPeerConnection");
  localPeerConnection.onicecandidate = gotLocalIceCandidate;

  remotePeerConnection = new webkitRTCPeerConnection(servers);
  trace("Created remote peer connection object remotePeerConnection");
  remotePeerConnection.onicecandidate = gotRemoteIceCandidate;
  remotePeerConnection.onaddstream = gotRemoteStream;

  localPeerConnection.addStream(localStream);
  trace("Added localStream to localPeerConnection");
  localPeerConnection.createOffer(gotLocalDescription);
}

function gotLocalDescription(description){
  localPeerConnection.setLocalDescription(description);
  trace("Offer from localPeerConnection: \n" + description.sdp);
  remotePeerConnection.setRemoteDescription(description);
  remotePeerConnection.createAnswer(gotRemoteDescription);
}

function gotRemoteDescription(description){
  remotePeerConnection.setLocalDescription(description);
  trace("Answer from remotePeerConnection: \n" + description.sdp);
  localPeerConnection.setRemoteDescription(description);
}

function hangup() {
  trace("Ending call");
  localPeerConnection.close();
  remotePeerConnection.close();
  localPeerConnection = null;
  remotePeerConnection = null;
  hangupButton.disabled = true;
  callButton.disabled = false;
}

function gotRemoteStream(event){
  remoteVideo.src = URL.createObjectURL(event.stream);
  trace("Received remote stream");
}

function gotLocalIceCandidate(event){
  if (event.candidate) {
    remotePeerConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
    trace("Local ICE candidate: \n" + event.candidate.candidate);
  }
}

function gotRemoteIceCandidate(event){
  if (event.candidate) {
    localPeerConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
    trace("Remote ICE candidate: \n " + event.candidate.candidate);
  }
}

1 个答案:

答案 0 :(得分:0)

这样的事情太麻烦了。当我测试铬时,我没有注意到我在firefox上运行相同的页面。所以我猜测它陷入了冲突。当铬阻挡我的相机时我发现了第一个意外的事情,我没有注意到。

然后我尝试用firefox(版本30)运行相同的程序,但我猜它没有在firefox上打开,因为它没有识别javascript文件中的一些参数并抛出一些错误。然后我在相机运行的时候把那个页面挂在firefox上,即使我用铬解锁了我的相机,我还是因为firefox而无法运行它。