我正在尝试遵循“入门”WebRTC 指南: http://www.html5rocks.com/en/tutorials/webrtc/basics/
我想要实现的是在两个对等点之间建立视频连接。但是,当我共享网络摄像头(按 chrome 中的允许按钮)时,remoteVideo 在远程端是黑色的。
我的脚本:
var pc;
var sdpConstraints = {'mandatory': { 'OfferToReceiveAudio':true, 'OfferToReceiveVideo':true }};
var constraints = {video: true, audio: true};
var socket = io.connect();
function start(isCaller) {
var servers = {"iceServers": [{ "url": "stun:stun.l.google.com:19302"}]};
pc = new RTCPeerConnection(servers);
pc.onicecandidate = function(event) {
if(event.candidate) {
socket.emit('candidate', {'candidate': event.candidate});
}
}
pc.onaddstream = function(event) {
attachMediaStream(remoteVideo, event.stream);
}
getUserMedia(constraints, function(stream) {
attachMediaStream(localVideo, stream);
pc.addStream(stream);
if(isCaller) {
pc.createOffer(gotDescription, null, sdpConstraints);
} else {
pc.createAnswer(pc.remoteDescription, gotDescription);
}
function gotDescription(desc) {
pc.setLocalDescription(desc);
socket.emit('sdpMessage', {'sdpMessage': desc});
}
}, printError);
}
socket.on('gotMessage', function(data) {
if(!pc) { start(false); }
if(data.remoteSDP) {
pc.setRemoteDescription(new RTCSessionDescription(data.remoteSDP));
} else {
pc.addIceCandidate(new RTCIceCandidate(data.remoteCandidate), onAddIceCandidateSuccess, onAddIceCandidateError);
}
});
HTML 包含:
<button onclick="start(true)">HIT ME</button>
<video id="localVideo" autoplay></video>
<video id="remoteVideo" autoplay></video>
部分server.js
serv_io.sockets.on('connection', function(socket) {
socket.on('candidate', function(data) {
socket.broadcast.emit('gotMessage', {'remoteCandidate': data.candidate});
});
socket.on('sdpMessage', function(data) {
socket.broadcast.emit('gotMessage', {'remoteSDP': data.sdpMessage});
});
}
控制台确实记录了 addIceCandidate 成功,当我在接收端记录媒体流时,它的 ID 和标签对应于发送者的 ID 和标签...
我做错了什么?
我也遇到了这个错误: “无法在‘RTCPeerConnection’上执行‘createAnswer’:作为参数 1 提供的回调不是函数。”
最佳答案
您对 API 的使用不正确。你提到的博客也有同样的错误。请查看正确的API here并查看 apprtc example .
createAnswer 签名是:
pc.createAnswer(successCallback, failureCallback, constraints);
关于javascript - WebRTC远程视频黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22147378/