and sorry if it seems like this post is duplicated, but actually any of the similar post helped me, so I decided to ask it again hoping to solve this problem.
var local;
var remote;
var localStream;
var remoteStream;
var localPeerConnection;
var configuration = { "iceServers": [ {"url": "stun:provserver.televolution.net"} ] };
var mediaConstraints = {
'mandatory': {
'OfferToReceiveAudio': true,
'OfferToReceiveVideo': true
}
};
var socket = io.connect('http:/xxx/');
var RTCPeerConnection = webkitRTCPeerConnection || mozRTCPeerConnection;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
local = document.getElementById('person1');
remote = document.createElement('video');
localPeerConnection = new RTCPeerConnection( configuration );
navigator.webkitGetUserMedia({ audio: true, video: true }, function ( stream ) {
localStream = stream;
localPeerConnection.addStream( stream );
local.src = URL.createObjectURL( stream );
local.play();
});
localPeerConnection.onaddstream = function ( stream ) {
console.log('stream received');
remoteStream = stream.stream;
document.getElementsByTagName('body')[0].appendChild( remote );
remote.src = URL.createObjectURL( stream.stream );
remote.play();
}
localPeerConnection.onicecandidate = function ( info ) {
console.log('ICE candidate created');
if ( info.candidate ) {
socket.emit('candidate', info.candidate );
} else {
console.log('ICE candidate finished');
}
}
socket.on('newUser', function ( data ) {
console.log('Call received an accepted');
localPeerConnection.setRemoteDescription( new RTCSessionDescription( data.description ));
localPeerConnection.createAnswer(function( desc ) {
console.log('sending answer');
localPeerConnection.setLocalDescription( desc );
socket.emit('accepted', {
desc: desc
});
}, null, mediaConstraints);
});
socket.on('callAccepted', function ( data ) {
console.log('Call accepted');
localPeerConnection.setRemoteDescription( new RTCSessionDescription( data.desc ) );
});
socket.on('newCandidate', function ( data ) {
var candidate = new RTCIceCandidate({
sdpMLineIndex: data.sdpMLineIndex,
candidate: data.candidate
});
localPeerConnection.addIceCandidate( candidate, function () {
}, function ( err ) {
console.log( err );
});
});
function start() {
console.log('Call created');
localPeerConnection.createOffer( function ( desc ) {
localPeerConnection.setLocalDescription( desc );
console.log('Local desc setted');
socket.emit('newConnection', {
description: desc
});
}, null, mediaConstraints);
}
function waitToVideo () {
if ( remote.currentTime > 0 ) {
console.log(2);
document.getElementsByTagName('body')[0].appendChild( remote );
remote.play();
} else {
setTimeout( waitToVideo, 100 );
}
}
The problem is that I don't receive any error in console, everything seems to be correct, but the remote stream video is black. I've read that maybe it's a problem releated to ICE packages, but they are sended just fine, and my code works when the peers are connected to the same network.
I tried to change the STUN server, but it stills no working. I've also attached the stream to the video element after all the ICE packages were received, and stills no working.
I don't now what to do, I've seem some examples, and the code is very similar, and they work, so I don't know what's the problem!
Thank's advanced