我开始测试WebRTC,并开始使用https://github.com/samdutton/simpl/中的示例
我正在测试这个例子:https://github.com/samdutton/simpl/blob/master/rtcpeerconnection/
我运行python的简单ran服务器: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:我还以为getUserMedia可能在铬中被禁用了(我使用的是34.0.1847.116Ubuntu14.04光环(260972))
所以我在铬设置中启用了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);
}
}发布于 2014-07-16 09:33:52
这样的事情麻烦太多了。当我测试铬的时候,我没有注意到我在firefox上运行相同的页面。所以我猜这是一场冲突。第一件意外的事情发生时铬堵塞了我的相机,我没有注意到。
然后我尝试用firefox运行相同的程序(版本30),但我想它没有打开firefox,因为它不识别javascript文件中的一些参数,并抛出了一些错误。然后,当我的相机运行时,我把这个页面挂在firefox上,甚至当我打开铬上的相机时,我也不能运行它,因为firefox。
https://stackoverflow.com/questions/24777002
复制相似问题