3

我正在尝试使用 PeerJS 进行简单的视频聊天。我想在 pc 上的 Firefox 和 Android 上的 Firefox 之间发送音频。我可以让呼叫运行 (call.on),我可以查看本地视频,但由于某种原因,媒体无法传递给其他用户。目前我收到错误:

ICE 失败,详情见 about:webrtc

我有一个服务器,它的简单版本是这样的:

    var ip = require('ip');
    var PeerServer = require('peer').PeerServer;

    var port = 9000;
    var server = new PeerServer({port: port, allow_discovery: true});

然后我有两个客户端,一个用于拨打电话的电脑:

var SERVER_IP = window.location.hostname;
var SERVER_PORT = 9000;
var localStream = "";
var peerID = "pc"
var peerConnectionID = "and"

var remoteVideo = document.querySelector('#rremote-video');
var localVideo = document.querySelector('#llocal-video');

var peer = new Peer(peerID, {host: SERVER_IP, port: SERVER_PORT});
var conn = peer.connect(peerConnectionID);


var getUserMedia = navigator.mediaDevices.getUserMedia({ video: true, audio: true })
                    .then(stream => localVideo.srcObject = stream)
                    .then(stream => localStream = stream)
                    .catch(e => console.log(e.name + ": "+ e.message));



waitForElement();

function waitForElement(){
    if(localStream != ""){

        conn.on('open', function(){
          conn.send('hi from PC!');
        });

        peer.on('connection', function(conn) {
          conn.on('data', function(data){
            console.log(data);
          });
        });

        console.log("we have a stream: "+localStream);
        var call = peer.call(peerConnectionID, localStream);
        console.log("Calling "+peerConnectionID);
        call.on('stream', function(remotestream) {
            console.log("Call on.");
            remoteVideo.srcObject = remotestream;
        });
    }
    else{
        setTimeout(function(){
            waitForElement();
        },750);
    }
}

接听电话的是:

var SERVER_IP = window.location.hostname;
var SERVER_PORT = 9000;
var localStream = "";
var peerID = "and"
var peerConnectionID = "pc"

var remoteVideo = document.querySelector('#rremote-video');
var localVideo = document.querySelector('#llocal-video');
var remoteAudio = document.querySelector('#remote-audio');
var localAudio = document.querySelector('#local-audio');


var peer = new Peer(peerID, {host: SERVER_IP, port: SERVER_PORT});
var conn = peer.connect(peerConnectionID);


var getUserMedia = navigator.mediaDevices.getUserMedia({ video: true, audio: true })
                                                          .then(stream => localAudio.srcObject = stream)
                                                          .then(stream => localVideo.srcObject = stream)
                                                          .then(stream => localStream = stream)
                                                          .catch(e =>     console.log(e.name + ": "+ e.message));

    waitForElement();

    function waitForElement(){
        if(localStream != ""){

        conn.on('open', function(){
          conn.send('hi from android!');
        });

        peer.on('connection', function(conn) {
          conn.on('data', function(data){
            console.log(data);
          });
        });

        peer.on('call', function(call) {
            console.log("Picking up call.");
            call.answer(localStream); 
            call.on('stream', function(remotestream) {
                console.log("Call on.");
                remoteVideo.srcObject = remotestream;
            });
        });
    }
    else{
        setTimeout(function(){
            waitForElement();
        },750);
    }
}

我认为这是我弄错了一些小调整,我主要按照 PeerJS 网站上的说明进行操作:http ://peerjs.com/如果有人可以看到需要更改的内容,欢迎提供任何帮助!

4

2 回答 2

0

听起来您的 ICE 候选人无法相互交流。您将不得不使用 STUN 服务器,如果它仍然不起作用,您将需要一个 TURN 服务器。

来自 PeerJS 文档:

var peer = new Peer({
  config: {'iceServers': [
    { url: 'stun:stun.l.google.com:19302' },
    { url: 'turn:homeo@turn.bistri.com:80', credential: 'homeo' }
  ]} /* Sample servers, please use appropriate ones */
});

链接将为您提供一种部署您自己的 TURN 服务器的方法。

于 2016-11-23T07:51:15.760 回答
0

你用的是https吗?浏览器不再允许调用非本地机器。

要对此进行测试,请在本地计算机上运行两组代码。如果你能做到这一点,这意味着你的代码是好的。

不幸的是,要进行远程连接,您将需要 https。这意味着您还需要自己的 peerjs 服务器(以 https 方式运行)。

另一种选择是使用端口转发使其中一台机器认为它正在与本地主机通信

于 2016-11-22T20:36:56.287 回答