简体   繁体   English

WebRTC 远程视频是黑色的

[英]WebRTC remote video is black

I am trying to follow the 'getting started' WebRTC guide on: http://www.html5rocks.com/en/tutorials/webrtc/basics/我正在尝试遵循“入门”WebRTC 指南: http : //www.html5rocks.com/en/tutorials/webrtc/basics/

What I am trying to achieve is to have a video connection between two peers.我想要实现的是在两个对等点之间建立视频连接。 However when I share the webcam (press the allow button in chrome) the remoteVideo is black on the remote side.但是,当我共享网络摄像头(按 chrome 中的允许按钮)时,远程视频在远程端为黑色。

My script:我的脚本:

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

The HTML contains: HTML 包含:

<button onclick="start(true)">HIT ME</button>
<video id="localVideo" autoplay></video>
<video id="remoteVideo" autoplay></video>

part of the server.js 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});
    });
}

The console does log an addIceCandidate succes, and when I log the media stream on the receiving end it's id and label correspond to the sender's id & label...控制台确实记录了 addIceCandidate 成功,当我在接收端记录媒体流时,它的 id 和标签对应于发件人的 id 和标签......

What am I doing wrong?我究竟做错了什么?

I also get this error: "Failed to execute 'createAnswer' on 'RTCPeerConnection': The callback provided as parameter 1 is not a function."我也收到此错误:“无法在‘RTCPeerConnection’上执行‘createAnswer’:作为参数 1 提供的回调不是函数。”

Your usage of the API is incorrect.您对 API 的使用不正确。 The blog you have mentioned has the same mistakes.你提到的博客也有同样的错误。 Please see the correct API here and also take a look at the apprtc example .请在此处查看正确的API,并查看apprtc 示例

The createAnswer signature is: createAnswer 签名是:

pc.createAnswer(successCallback, failureCallback, constraints);

You just need to right click in the video section and select show controls, and the video will show.您只需要在视频部分右键单击并选择显示控件,视频就会显示出来。 It's really simple!真的很简单!

在此处输入图片说明

This happened to me when my computer was behind the proxy server.当我的计算机位于代理服务器后面时,这发生在我身上。 I solved this problem by using TURN server.我通过使用 TURN 服务器解决了这个问题。 Also list TURN server in iceServers.还要在 iceServers 中列出 TURN 服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM