2013-02-28 00:22:10 +01:00
|
|
|
var RTCPeerConnection = null;
|
|
|
|
var getUserMedia = null;
|
|
|
|
var attachMediaStream = null;
|
|
|
|
var reattachMediaStream = null;
|
|
|
|
var webrtcDetectedBrowser = null;
|
2013-05-30 00:13:19 +02:00
|
|
|
var webrtcDetectedVersion = null;
|
2013-02-28 00:22:10 +01:00
|
|
|
|
2013-03-05 04:35:26 +01:00
|
|
|
function trace(text) {
|
|
|
|
// This function is used for logging.
|
|
|
|
if (text[text.length - 1] == '\n') {
|
|
|
|
text = text.substring(0, text.length - 1);
|
|
|
|
}
|
|
|
|
console.log((performance.now() / 1000).toFixed(3) + ": " + text);
|
|
|
|
}
|
2014-02-24 04:43:03 +01:00
|
|
|
function maybeFixConfiguration(pcConfig) {
|
2014-02-24 22:51:58 +01:00
|
|
|
if (pcConfig == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-24 04:43:03 +01:00
|
|
|
for (var i = 0; i < pcConfig.iceServers.length; i++) {
|
|
|
|
if (pcConfig.iceServers[i].hasOwnProperty('urls')){
|
|
|
|
pcConfig.iceServers[i]['url'] = pcConfig.iceServers[i]['urls'];
|
|
|
|
delete pcConfig.iceServers[i]['urls'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-05 04:35:26 +01:00
|
|
|
|
2013-02-28 00:22:10 +01:00
|
|
|
if (navigator.mozGetUserMedia) {
|
|
|
|
console.log("This appears to be Firefox");
|
|
|
|
|
|
|
|
webrtcDetectedBrowser = "firefox";
|
|
|
|
|
2013-06-25 16:52:51 +02:00
|
|
|
webrtcDetectedVersion =
|
2013-10-22 20:02:41 +02:00
|
|
|
parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
|
2013-06-25 16:52:51 +02:00
|
|
|
|
2013-02-28 00:22:10 +01:00
|
|
|
// The RTCPeerConnection object.
|
2014-02-24 04:43:03 +01:00
|
|
|
var RTCPeerConnection = function(pcConfig, pcConstraints) {
|
|
|
|
// .urls is not supported in FF yet.
|
2014-02-24 22:51:58 +01:00
|
|
|
maybeFixConfiguration(pcConfig);
|
2014-02-24 04:43:03 +01:00
|
|
|
return new mozRTCPeerConnection(pcConfig, pcConstraints);
|
|
|
|
}
|
2013-02-28 00:22:10 +01:00
|
|
|
|
|
|
|
// The RTCSessionDescription object.
|
|
|
|
RTCSessionDescription = mozRTCSessionDescription;
|
|
|
|
|
|
|
|
// The RTCIceCandidate object.
|
|
|
|
RTCIceCandidate = mozRTCIceCandidate;
|
|
|
|
|
|
|
|
// Get UserMedia (only difference is the prefix).
|
|
|
|
// Code from Adam Barth.
|
|
|
|
getUserMedia = navigator.mozGetUserMedia.bind(navigator);
|
2014-01-31 00:38:44 +01:00
|
|
|
navigator.getUserMedia = getUserMedia;
|
2013-02-28 00:22:10 +01:00
|
|
|
|
2013-06-25 16:52:51 +02:00
|
|
|
// Creates iceServer from the url for FF.
|
|
|
|
createIceServer = function(url, username, password) {
|
|
|
|
var iceServer = null;
|
|
|
|
var url_parts = url.split(':');
|
|
|
|
if (url_parts[0].indexOf('stun') === 0) {
|
|
|
|
// Create iceServer with stun url.
|
|
|
|
iceServer = { 'url': url };
|
2013-10-24 22:31:57 +02:00
|
|
|
} else if (url_parts[0].indexOf('turn') === 0) {
|
|
|
|
if (webrtcDetectedVersion < 27) {
|
|
|
|
// Create iceServer with turn url.
|
|
|
|
// Ignore the transport parameter from TURN url for FF version <=27.
|
|
|
|
var turn_url_parts = url.split("?");
|
|
|
|
// Return null for createIceServer if transport=tcp.
|
2013-12-20 23:10:17 +01:00
|
|
|
if (turn_url_parts.length === 1 ||
|
|
|
|
turn_url_parts[1].indexOf('transport=udp') === 0) {
|
2014-02-24 04:43:03 +01:00
|
|
|
iceServer = {'url': turn_url_parts[0],
|
|
|
|
'credential': password,
|
|
|
|
'username': username};
|
2013-10-24 22:31:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// FF 27 and above supports transport parameters in TURN url,
|
|
|
|
// So passing in the full url to create iceServer.
|
2014-02-24 04:43:03 +01:00
|
|
|
iceServer = {'url': url,
|
|
|
|
'credential': password,
|
|
|
|
'username': username};
|
2013-10-24 22:31:57 +02:00
|
|
|
}
|
2013-06-25 16:52:51 +02:00
|
|
|
}
|
2013-05-30 00:13:19 +02:00
|
|
|
return iceServer;
|
|
|
|
};
|
|
|
|
|
2014-02-24 04:43:03 +01:00
|
|
|
createIceServers = function(urls, username, password) {
|
|
|
|
var iceServers = [];
|
|
|
|
// Use .url for FireFox.
|
|
|
|
for (i = 0; i < urls.length; i++) {
|
|
|
|
var iceServer = createIceServer(urls[i],
|
|
|
|
username,
|
|
|
|
password);
|
|
|
|
if (iceServer !== null) {
|
|
|
|
iceServers.push(iceServer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return iceServers;
|
|
|
|
}
|
|
|
|
|
2013-02-28 00:22:10 +01:00
|
|
|
// Attach a media stream to an element.
|
|
|
|
attachMediaStream = function(element, stream) {
|
|
|
|
console.log("Attaching media stream");
|
|
|
|
element.mozSrcObject = stream;
|
|
|
|
element.play();
|
|
|
|
};
|
|
|
|
|
|
|
|
reattachMediaStream = function(to, from) {
|
|
|
|
console.log("Reattaching media stream");
|
|
|
|
to.mozSrcObject = from.mozSrcObject;
|
|
|
|
to.play();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fake get{Video,Audio}Tracks
|
2013-12-11 09:36:16 +01:00
|
|
|
if (!MediaStream.prototype.getVideoTracks) {
|
|
|
|
MediaStream.prototype.getVideoTracks = function() {
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
}
|
2013-02-28 00:22:10 +01:00
|
|
|
|
2013-12-11 09:36:16 +01:00
|
|
|
if (!MediaStream.prototype.getAudioTracks) {
|
|
|
|
MediaStream.prototype.getAudioTracks = function() {
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
}
|
2013-02-28 00:22:10 +01:00
|
|
|
} else if (navigator.webkitGetUserMedia) {
|
|
|
|
console.log("This appears to be Chrome");
|
|
|
|
|
|
|
|
webrtcDetectedBrowser = "chrome";
|
2013-05-30 00:13:19 +02:00
|
|
|
webrtcDetectedVersion =
|
2013-10-22 20:02:41 +02:00
|
|
|
parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10);
|
2013-05-30 00:13:19 +02:00
|
|
|
|
2014-02-24 04:43:03 +01:00
|
|
|
// Creates iceServer from the url for Chrome M33 and earlier.
|
2013-06-25 16:52:51 +02:00
|
|
|
createIceServer = function(url, username, password) {
|
|
|
|
var iceServer = null;
|
|
|
|
var url_parts = url.split(':');
|
|
|
|
if (url_parts[0].indexOf('stun') === 0) {
|
|
|
|
// Create iceServer with stun url.
|
|
|
|
iceServer = { 'url': url };
|
|
|
|
} else if (url_parts[0].indexOf('turn') === 0) {
|
2013-10-24 22:31:57 +02:00
|
|
|
// Chrome M28 & above uses below TURN format.
|
2014-02-24 04:43:03 +01:00
|
|
|
iceServer = {'url': url,
|
|
|
|
'credential': password,
|
|
|
|
'username': username};
|
|
|
|
}
|
|
|
|
return iceServer;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Creates iceServers from the urls for Chrome M34 and above.
|
|
|
|
createIceServers = function(urls, username, password) {
|
|
|
|
var iceServers = [];
|
|
|
|
if (webrtcDetectedVersion >= 34) {
|
|
|
|
// .urls is supported since Chrome M34.
|
|
|
|
iceServers = {'urls': urls,
|
2013-10-24 22:31:57 +02:00
|
|
|
'credential': password,
|
|
|
|
'username': username };
|
2014-02-24 04:43:03 +01:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < urls.length; i++) {
|
|
|
|
var iceServer = createIceServer(urls[i],
|
|
|
|
username,
|
|
|
|
password);
|
|
|
|
if (iceServer !== null) {
|
|
|
|
iceServers.push(iceServer);
|
|
|
|
}
|
|
|
|
}
|
2013-06-25 16:52:51 +02:00
|
|
|
}
|
2014-02-24 04:43:03 +01:00
|
|
|
return iceServers;
|
2013-06-25 16:52:51 +02:00
|
|
|
};
|
2013-02-28 00:22:10 +01:00
|
|
|
|
|
|
|
// The RTCPeerConnection object.
|
2014-02-24 04:43:03 +01:00
|
|
|
var RTCPeerConnection = function(pcConfig, pcConstraints) {
|
|
|
|
// .urls is supported since Chrome M34.
|
|
|
|
if (webrtcDetectedVersion < 34) {
|
2014-02-24 22:51:58 +01:00
|
|
|
maybeFixConfiguration(pcConfig);
|
2014-02-24 04:43:03 +01:00
|
|
|
}
|
|
|
|
return new webkitRTCPeerConnection(pcConfig, pcConstraints);
|
|
|
|
}
|
2013-03-06 04:35:03 +01:00
|
|
|
|
2013-02-28 00:22:10 +01:00
|
|
|
// Get UserMedia (only difference is the prefix).
|
|
|
|
// Code from Adam Barth.
|
|
|
|
getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
|
2014-01-31 00:38:44 +01:00
|
|
|
navigator.getUserMedia = getUserMedia;
|
2013-02-28 00:22:10 +01:00
|
|
|
|
|
|
|
// Attach a media stream to an element.
|
|
|
|
attachMediaStream = function(element, stream) {
|
2013-04-26 01:22:03 +02:00
|
|
|
if (typeof element.srcObject !== 'undefined') {
|
|
|
|
element.srcObject = stream;
|
|
|
|
} else if (typeof element.mozSrcObject !== 'undefined') {
|
|
|
|
element.mozSrcObject = stream;
|
|
|
|
} else if (typeof element.src !== 'undefined') {
|
|
|
|
element.src = URL.createObjectURL(stream);
|
|
|
|
} else {
|
|
|
|
console.log('Error attaching stream to element.');
|
|
|
|
}
|
2013-02-28 00:22:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
reattachMediaStream = function(to, from) {
|
|
|
|
to.src = from.src;
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
console.log("Browser does not appear to be WebRTC-capable");
|
|
|
|
}
|