I encountered a peculiar issue with the abstraction of the WebRTC offer generation process. It appears that the incoming ice candidates fail to reach the null candidate. While I have been able to generate offers successfully using similar code in the past, my abstracted version only reaches 12 candidates compared to the original 20. This discrepancy is baffling, as the code seems nearly identical but the abstracted version fails even on the same browser.
Here is the original working code:
var localConn = new webkitRTCPeerConnection({'iceServers':[{...}]});
var remoteConn = new webkitRTCPeerConnection({'iceServers':[{...}]});
function initMedia(localView, callback){
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
var constraints = {audio:true,video:true};
navigator.getUserMedia(constraints, successStream, errorStream);
//onSuccess and Error functions
function successStream(stream){
window.stream = stream;
if(window.URL){
$('#'+localView).attr('src',window.URL.createObjectURL(stream));
} else {
$('#'+localView).attr('src',stream);
}
localConn.addStream(stream);
remoteConn.addStream(stream);
console.log('local Stream: '+ stream.id);
callback(); //-> goes on to create new offer
}
function errorStream(error){
console.log('navigator.getUserMedia error: ', error);
}
}
//function that generates offer and sends it out to a callback
function newOffer(callback){
console.log('creating new offer');
localConn.createOffer(function (sessionDescription){
localConn.setLocalDescription(sessionDescription);
}, function(error){
console.log('Error setting local description: '+error);
});
createOffer();
//gather ICE with a callback to handle/send generated offer
function createOffer(){
localConn.onicecandidate = function(iceEvent){
console.log('gathering local ice'); //ice events fired (20 total)
//upon gathering all local ice
if(iceEvent.candidate === null){
console.log('local ice gathered'); //success
var offer = {'type': localConn.localDescription.type,
'sdp': localConn.localDescription.sdp};
offer = JSON.stringify(offer);
console.log('offer created');
callback(offer);
}
}
}
}
Here is the updated version with functions for abstraction (not receiving null ice candidate):
//general rtc vars
var localConn = new webkitRTCPeerConnection({'iceServers':[{'url':'stun:stun.1.google.com:19302'}]});
var remoteConn = new webkitRTCPeerConnection({'iceServers':[{'url':'stun:stun.1.google.com:19302'}]});
//var mediaStream;
var channel;
//creates a stream from webcam
//@params function streamHandle(stream)
function initStream(streamHandle){
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
var constraints = {audio:true,video:true};
navigator.getUserMedia(constraints, successStream, errorStream);
//onSuccess and Error functions
function successStream(stream){
window.stream = stream;
console.log('TreRTC: Local Stream-'+ stream.id);
//mediaStream = stream;
localConn.addStream(stream);
remoteConn.addStream(stream);
streamHandle(stream);
}
function errorStream(error){
console.log('navigator.getUserMedia error: ', error);
}
}
//creates an offer to be sent
//@params Stream stream (from getusermedia)
//@return string offer
function createOffer(stream){
console.log('TreRTC: Creating Offer');
localConn.createOffer(function (sessionDescription){
localConn.setLocalDescription(sessionDescription);
}, function(error){
console.log('Error setting local description: '+error);
});
localConn.onicecandidate = function(iceEvt){
console.log('TreRTC: ICE in');
if(iceEvt.candidate === null){
console.log('TreRTC: ICE gathered');
var offer = {'type': localConn.localDescription.type,
'sdp': localConn.localDescription.sdp};
offer = JSON.stringify(offer);
console.log('TreRTC: Offer initialized');
return offer;
}
}
}