3

I am trying to get a simple video chat working with PeerJS. I want to send audio between Firefox on a pc and Firefox on Android. I can get the call running (call.on) and I can view local video but for some reason the media just doesn't come through to the other user. Currently I am getting the error:

ICE failed, see about:webrtc for more details

I have a server which in its simple version is as such:

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

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

Then I have two clients, one for the pc that makes the call:

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

And the one that answers the call is:

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

I think it is some little tweak that I'm getting wrong, I have mainly followed instructions on PeerJS website: http://peerjs.com/ Please if anyone can see something that needs to change, any help is welcome!

suppp111
  • 93
  • 3
  • 9

2 Answers2

0

Are you using https? Making calls to non-local machines is no longer allowed by the browsers.

To test this out, run both sets of code on your local machine. If you can do that connection, it means your code is ok.

To do a remote connection you will unfortunately need https. This means you will also need your own peerjs server (to run as https).

The other option is to use port forwarding to make one of the machines think it is talking to the localhost

Mikkel
  • 7,693
  • 3
  • 17
  • 31
  • I have my own https server for signaling. Still fails with this message: "ICE failed, add a TURN server and see about:webrtc for more details" – Morris Dec 06 '19 at 09:32
0

It sounds like your ICE Candidates cannot communicate one to each other. You will have to use a STUN server and, if it still doesnt work, you will need a TURN server.

From PeerJS Documentation:

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

This link will provide you a method to deploy your own TURN server.

Antonin M.
  • 1,744
  • 1
  • 18
  • 29
  • Thanks for the reply, hopefully I can get somewhere with this. So I set up a TURN server on my ubuntu and I'm running the demon. I changed the new Peer to: `var peer = new Peer({ config: {'iceServers': [ { urls: 'stun:stun.l.google.com:19302' }, { urls: 'turn:https://myurl.com:3478', credential: 'test', username: 'test' } ]} });` and I swapped my local peer.js to https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.14/peer.js as it was complaining about https/http inconsistancy, but now I'm getting a "ReferenceError: Peer is not defined". – suppp111 Nov 23 '16 at 08:59
  • Do I need to still run my own Peer server? I thought I would not as I'm not really calling it anymore...another option is that this peer.js is actually not working anymore as I'm getting the "change to urls" error. – suppp111 Nov 23 '16 at 08:59
  • Unless you are operating entirely within your own network, the peerjs, stun and turn servers all need to be on the internet, not on your own machine – Mikkel Nov 23 '16 at 09:39
  • well this is running on an EC2 – suppp111 Nov 23 '16 at 10:09
  • There is an error in the URL syntax of your turn. It should be something like : `turn:myurl.com:3478`. You will have to remove the `https://` part. – Antonin M. Dec 02 '16 at 16:23