2012-10-06 22:34:30 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2013-01-23 11:21:25 -08:00
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
2012-10-06 22:34:30 -07:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
|
|
|
const PC_CONTRACT = "@mozilla.org/dom/peerconnection;1";
|
|
|
|
const PC_ICE_CONTRACT = "@mozilla.org/dom/rtcicecandidate;1";
|
|
|
|
const PC_SESSION_CONTRACT = "@mozilla.org/dom/rtcsessiondescription;1";
|
2012-11-09 21:04:27 -08:00
|
|
|
const PC_MANAGER_CONTRACT = "@mozilla.org/dom/peerconnectionmanager;1";
|
2012-10-06 22:34:30 -07:00
|
|
|
|
|
|
|
const PC_CID = Components.ID("{7cb2b368-b1ce-4560-acac-8e0dbda7d3d0}");
|
|
|
|
const PC_ICE_CID = Components.ID("{8c5dbd70-2c8e-4ecb-a5ad-2fc919099f01}");
|
|
|
|
const PC_SESSION_CID = Components.ID("{5f21ffd9-b73f-4ba0-a685-56b4667aaf1c}");
|
2012-11-09 21:04:27 -08:00
|
|
|
const PC_MANAGER_CID = Components.ID("{7293e901-2be3-4c02-b4bd-cbef6fc24f78}");
|
2012-10-06 22:34:30 -07:00
|
|
|
|
|
|
|
// Global list of PeerConnection objects, so they can be cleaned up when
|
|
|
|
// a page is torn down. (Maps inner window ID to an array of PC objects).
|
|
|
|
function GlobalPCList() {
|
2012-11-18 20:53:14 -08:00
|
|
|
this._list = [];
|
2012-12-11 04:58:12 -08:00
|
|
|
this._networkdown = false; // XXX Need to query current state somehow
|
2012-10-06 22:34:30 -07:00
|
|
|
Services.obs.addObserver(this, "inner-window-destroyed", true);
|
2012-11-18 20:53:14 -08:00
|
|
|
Services.obs.addObserver(this, "profile-change-net-teardown", true);
|
|
|
|
Services.obs.addObserver(this, "network:offline-about-to-go-offline", true);
|
2012-12-11 04:58:12 -08:00
|
|
|
Services.obs.addObserver(this, "network:offline-status-changed", true);
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
|
|
|
GlobalPCList.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
|
2012-11-09 21:04:27 -08:00
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
Ci.IPeerConnectionManager]),
|
|
|
|
|
|
|
|
classID: PC_MANAGER_CID,
|
|
|
|
classInfo: XPCOMUtils.generateCI({classID: PC_MANAGER_CID,
|
|
|
|
contractID: PC_MANAGER_CONTRACT,
|
|
|
|
classDescription: "PeerConnectionManager",
|
|
|
|
interfaces: [
|
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
Ci.IPeerConnectionManager
|
|
|
|
]}),
|
|
|
|
|
|
|
|
_xpcom_factory: {
|
|
|
|
createInstance: function(outer, iid) {
|
|
|
|
if (outer) {
|
2013-01-23 11:21:25 -08:00
|
|
|
throw Cr.NS_ERROR_NO_AGGREGATION;
|
2012-11-09 21:04:27 -08:00
|
|
|
}
|
|
|
|
return _globalPCList.QueryInterface(iid);
|
|
|
|
}
|
|
|
|
},
|
2012-10-06 22:34:30 -07:00
|
|
|
|
|
|
|
addPC: function(pc) {
|
|
|
|
let winID = pc._winID;
|
|
|
|
if (this._list[winID]) {
|
2013-01-17 15:11:14 -08:00
|
|
|
this._list[winID].push(Components.utils.getWeakReference(pc));
|
2012-10-06 22:34:30 -07:00
|
|
|
} else {
|
2013-01-17 15:11:14 -08:00
|
|
|
this._list[winID] = [Components.utils.getWeakReference(pc)];
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
2013-01-17 15:11:14 -08:00
|
|
|
this.removeNullRefs(winID);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeNullRefs: function(winID) {
|
|
|
|
if (this._list === undefined || this._list[winID] === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._list[winID] = this._list[winID].filter(
|
|
|
|
function (e,i,a) { return e.get() !== null; });
|
2012-10-06 22:34:30 -07:00
|
|
|
},
|
|
|
|
|
2012-11-09 21:04:27 -08:00
|
|
|
hasActivePeerConnection: function(winID) {
|
2013-01-17 15:11:14 -08:00
|
|
|
this.removeNullRefs(winID);
|
2012-11-09 21:04:27 -08:00
|
|
|
return this._list[winID] ? true : false;
|
|
|
|
},
|
|
|
|
|
2012-10-06 22:34:30 -07:00
|
|
|
observe: function(subject, topic, data) {
|
2012-11-18 20:53:14 -08:00
|
|
|
if (topic == "inner-window-destroyed") {
|
|
|
|
let winID = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
|
|
|
|
if (this._list[winID]) {
|
2013-01-17 15:11:14 -08:00
|
|
|
this._list[winID].forEach(function(pcref) {
|
|
|
|
let pc = pcref.get();
|
|
|
|
if (pc !== null) {
|
|
|
|
pc._pc.close(false);
|
|
|
|
delete pc._observer;
|
|
|
|
pc._pc = null;
|
|
|
|
}
|
2012-11-18 20:53:14 -08:00
|
|
|
});
|
|
|
|
delete this._list[winID];
|
|
|
|
}
|
|
|
|
} else if (topic == "profile-change-net-teardown" ||
|
|
|
|
topic == "network:offline-about-to-go-offline") {
|
|
|
|
// Delete all peerconnections on shutdown - synchronously (we need
|
|
|
|
// them to be done deleting transports before we return)!
|
2013-01-08 10:40:15 -08:00
|
|
|
// Also kill them if "Work Offline" is selected - more can be created
|
2012-11-18 20:53:14 -08:00
|
|
|
// while offline, but attempts to connect them should fail.
|
|
|
|
let array;
|
|
|
|
while ((array = this._list.pop()) != undefined) {
|
2013-01-17 15:11:14 -08:00
|
|
|
array.forEach(function(pcref) {
|
|
|
|
let pc = pcref.get();
|
|
|
|
if (pc !== null) {
|
|
|
|
pc._pc.close(true);
|
|
|
|
delete pc._observer;
|
|
|
|
pc._pc = null;
|
|
|
|
}
|
2012-11-18 20:53:14 -08:00
|
|
|
});
|
|
|
|
};
|
2012-12-11 04:58:12 -08:00
|
|
|
this._networkdown = true;
|
|
|
|
}
|
|
|
|
else if (topic == "network:offline-status-changed") {
|
|
|
|
if (data == "offline") {
|
|
|
|
// this._list shold be empty here
|
|
|
|
this._networkdown = true;
|
|
|
|
} else if (data == "online") {
|
|
|
|
this._networkdown = false;
|
|
|
|
}
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
2012-11-18 20:53:14 -08:00
|
|
|
},
|
2012-10-06 22:34:30 -07:00
|
|
|
};
|
|
|
|
let _globalPCList = new GlobalPCList();
|
|
|
|
|
|
|
|
function IceCandidate(candidate) {
|
|
|
|
this.candidate = candidate;
|
|
|
|
this.sdpMid = null;
|
|
|
|
this.sdpMLineIndex = null;
|
|
|
|
}
|
|
|
|
IceCandidate.prototype = {
|
|
|
|
classID: PC_ICE_CID,
|
|
|
|
|
|
|
|
classInfo: XPCOMUtils.generateCI({classID: PC_ICE_CID,
|
|
|
|
contractID: PC_ICE_CONTRACT,
|
|
|
|
classDescription: "IceCandidate",
|
|
|
|
interfaces: [
|
|
|
|
Ci.nsIDOMRTCIceCandidate
|
|
|
|
],
|
|
|
|
flags: Ci.nsIClassInfo.DOM_OBJECT}),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
|
|
|
Ci.nsIDOMRTCIceCandidate, Ci.nsIDOMGlobalObjectConstructor
|
|
|
|
]),
|
|
|
|
|
2012-11-21 06:52:43 -08:00
|
|
|
constructor: function(win, candidateInitDict) {
|
2012-10-06 22:34:30 -07:00
|
|
|
if (this._win) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception("Constructor already called");
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
|
|
|
this._win = win;
|
2012-11-21 06:52:43 -08:00
|
|
|
if (candidateInitDict !== undefined) {
|
|
|
|
this.candidate = candidateInitDict.candidate || null;
|
2013-01-23 13:00:22 -08:00
|
|
|
this.sdpMid = candidateInitDict.sdpMid || null;
|
2012-11-21 10:48:32 -08:00
|
|
|
this.sdpMLineIndex = candidateInitDict.sdpMLineIndex === null ?
|
|
|
|
null : candidateInitDict.sdpMLineIndex + 1;
|
2012-11-21 06:52:43 -08:00
|
|
|
} else {
|
|
|
|
this.candidate = this.sdpMid = this.sdpMLineIndex = null;
|
|
|
|
}
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function SessionDescription(type, sdp) {
|
|
|
|
this.type = type;
|
|
|
|
this.sdp = sdp;
|
|
|
|
}
|
|
|
|
SessionDescription.prototype = {
|
|
|
|
classID: PC_SESSION_CID,
|
|
|
|
|
|
|
|
classInfo: XPCOMUtils.generateCI({classID: PC_SESSION_CID,
|
|
|
|
contractID: PC_SESSION_CONTRACT,
|
|
|
|
classDescription: "SessionDescription",
|
|
|
|
interfaces: [
|
|
|
|
Ci.nsIDOMRTCSessionDescription
|
|
|
|
],
|
|
|
|
flags: Ci.nsIClassInfo.DOM_OBJECT}),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
|
|
|
Ci.nsIDOMRTCSessionDescription, Ci.nsIDOMGlobalObjectConstructor
|
|
|
|
]),
|
|
|
|
|
2012-11-20 23:59:02 -08:00
|
|
|
constructor: function(win, descriptionInitDict) {
|
2012-10-06 22:34:30 -07:00
|
|
|
if (this._win) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception("Constructor already called");
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
|
|
|
this._win = win;
|
2012-11-20 23:59:02 -08:00
|
|
|
if (descriptionInitDict !== undefined) {
|
|
|
|
this.type = descriptionInitDict.type || null;
|
|
|
|
this.sdp = descriptionInitDict.sdp || null;
|
|
|
|
} else {
|
|
|
|
this.type = this.sdp = null;
|
|
|
|
}
|
2012-10-06 22:34:30 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
toString: function() {
|
|
|
|
return JSON.stringify({
|
|
|
|
type: this.type, sdp: this.sdp
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function PeerConnection() {
|
|
|
|
this._queue = [];
|
|
|
|
|
|
|
|
this._pc = null;
|
|
|
|
this._observer = null;
|
2012-10-20 09:43:57 -07:00
|
|
|
this._closed = false;
|
2012-10-06 22:34:30 -07:00
|
|
|
|
|
|
|
this._onCreateOfferSuccess = null;
|
|
|
|
this._onCreateOfferFailure = null;
|
|
|
|
this._onCreateAnswerSuccess = null;
|
|
|
|
this._onCreateAnswerFailure = null;
|
|
|
|
|
2013-01-14 14:25:54 -08:00
|
|
|
this._pendingType = null;
|
|
|
|
this._localType = null;
|
|
|
|
this._remoteType = null;
|
|
|
|
|
2012-10-06 22:34:30 -07:00
|
|
|
/**
|
|
|
|
* Everytime we get a request from content, we put it in the queue. If
|
|
|
|
* there are no pending operations though, we will execute it immediately.
|
|
|
|
* In PeerConnectionObserver, whenever we are notified that an operation
|
|
|
|
* has finished, we will check the queue for the next operation and execute
|
|
|
|
* if neccesary. The _pending flag indicates whether an operation is currently
|
|
|
|
* in progress.
|
|
|
|
*/
|
|
|
|
this._pending = false;
|
|
|
|
|
|
|
|
// Public attributes.
|
|
|
|
this.onaddstream = null;
|
2012-11-21 08:55:25 -08:00
|
|
|
this.onopen = null;
|
2012-10-06 22:34:30 -07:00
|
|
|
this.onremovestream = null;
|
|
|
|
this.onicecandidate = null;
|
|
|
|
this.onstatechange = null;
|
|
|
|
this.ongatheringchange = null;
|
|
|
|
this.onicechange = null;
|
2012-10-06 22:34:30 -07:00
|
|
|
|
|
|
|
// Data channel.
|
|
|
|
this.ondatachannel = null;
|
|
|
|
this.onconnection = null;
|
|
|
|
this.onclosedconnection = null;
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
|
|
|
PeerConnection.prototype = {
|
|
|
|
classID: PC_CID,
|
|
|
|
|
2013-02-15 19:41:31 -08:00
|
|
|
classInfo: Cu.getDOMClassInfo("RTCPeerConnection"),
|
2012-10-06 22:34:30 -07:00
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
2013-01-17 15:11:14 -08:00
|
|
|
Ci.nsIDOMRTCPeerConnection,
|
|
|
|
Ci.nsIDOMGlobalObjectConstructor,
|
|
|
|
Ci.nsISupportsWeakReference
|
2012-10-06 22:34:30 -07:00
|
|
|
]),
|
|
|
|
|
|
|
|
// Constructor is an explicit function, because of nsIDOMGlobalObjectConstructor.
|
2013-01-23 11:21:25 -08:00
|
|
|
constructor: function(win, rtcConfig) {
|
2012-10-06 22:34:30 -07:00
|
|
|
if (!Services.prefs.getBoolPref("media.peerconnection.enabled")) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception("PeerConnection not enabled (did you set the pref?)");
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
|
|
|
if (this._win) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception("RTCPeerConnection constructor already called");
|
2013-01-23 11:21:25 -08:00
|
|
|
}
|
2013-02-18 05:49:14 -08:00
|
|
|
if (!rtcConfig ||
|
|
|
|
!Services.prefs.getBoolPref("media.peerconnection.use_document_iceservers")) {
|
|
|
|
rtcConfig = {iceServers:
|
|
|
|
JSON.parse(Services.prefs.getCharPref("media.peerconnection.default_iceservers"))};
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
2013-01-23 11:21:25 -08:00
|
|
|
this._mustValidateRTCConfiguration(rtcConfig,
|
|
|
|
"RTCPeerConnection constructor passed invalid RTCConfiguration");
|
2012-12-11 04:58:12 -08:00
|
|
|
if (_globalPCList._networkdown) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception("Can't create RTCPeerConnections when the network is down");
|
2012-12-11 04:58:12 -08:00
|
|
|
}
|
2012-10-06 22:34:30 -07:00
|
|
|
|
|
|
|
this._pc = Cc["@mozilla.org/peerconnection;1"].
|
|
|
|
createInstance(Ci.IPeerConnection);
|
|
|
|
this._observer = new PeerConnectionObserver(this);
|
|
|
|
|
|
|
|
// Nothing starts until ICE gathering completes.
|
|
|
|
this._queueOrRun({
|
2013-04-16 22:16:02 -07:00
|
|
|
func: this._getPC().initialize,
|
2013-01-23 11:21:25 -08:00
|
|
|
args: [this._observer, win, rtcConfig, Services.tm.currentThread],
|
2012-10-06 22:34:30 -07:00
|
|
|
wait: true
|
|
|
|
});
|
|
|
|
|
|
|
|
this._win = win;
|
|
|
|
this._winID = this._win.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID;
|
|
|
|
|
|
|
|
// Add a reference to the PeerConnection to global list.
|
|
|
|
_globalPCList.addPC(this);
|
|
|
|
},
|
|
|
|
|
2013-04-16 22:16:02 -07:00
|
|
|
_getPC: function() {
|
|
|
|
if (!this._pc) {
|
|
|
|
throw new Components.Exception("PeerConnection is gone (did you turn on Offline mode?)");
|
|
|
|
}
|
|
|
|
return this._pc;
|
|
|
|
},
|
|
|
|
|
2012-10-06 22:34:30 -07:00
|
|
|
/**
|
|
|
|
* Add a function to the queue or run it immediately if the queue is empty.
|
|
|
|
* Argument is an object with the func, args and wait properties; wait should
|
|
|
|
* be set to true if the function has a success/error callback that will
|
|
|
|
* call _executeNext, false if it doesn't have a callback.
|
|
|
|
*/
|
|
|
|
_queueOrRun: function(obj) {
|
2013-01-17 12:25:17 -08:00
|
|
|
this._checkClosed();
|
2012-10-06 22:34:30 -07:00
|
|
|
if (!this._pending) {
|
2013-01-14 14:25:54 -08:00
|
|
|
if (obj.type !== undefined) {
|
|
|
|
this._pendingType = obj.type;
|
|
|
|
}
|
2012-10-06 22:34:30 -07:00
|
|
|
obj.func.apply(this, obj.args);
|
|
|
|
if (obj.wait) {
|
|
|
|
this._pending = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._queue.push(obj);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Pick the next item from the queue and run it.
|
|
|
|
_executeNext: function() {
|
|
|
|
if (this._queue.length) {
|
|
|
|
let obj = this._queue.shift();
|
2013-01-14 14:25:54 -08:00
|
|
|
if (obj.type !== undefined) {
|
|
|
|
this._pendingType = obj.type;
|
|
|
|
}
|
2012-10-06 22:34:30 -07:00
|
|
|
obj.func.apply(this, obj.args);
|
|
|
|
if (!obj.wait) {
|
|
|
|
this._executeNext();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._pending = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-01-23 11:21:25 -08:00
|
|
|
/**
|
|
|
|
* An RTCConfiguration looks like this:
|
|
|
|
*
|
2013-01-24 14:58:29 -08:00
|
|
|
* { "iceServers": [ { url:"stun:23.21.150.121" },
|
|
|
|
* { url:"turn:user@turn.example.org", credential:"mypass"} ] }
|
2013-01-23 11:21:25 -08:00
|
|
|
*
|
|
|
|
* We check for basic structure and well-formed stun/turn urls, but not
|
|
|
|
* validity of servers themselves, before passing along to C++.
|
|
|
|
* ErrorMsg is passed in to detail which array-entry failed, if any.
|
|
|
|
*/
|
|
|
|
_mustValidateRTCConfiguration: function(rtcConfig, errorMsg) {
|
|
|
|
function isObject(obj) {
|
|
|
|
return obj && (typeof obj === "object");
|
|
|
|
}
|
2013-02-13 23:08:56 -08:00
|
|
|
function isArraylike(obj) {
|
|
|
|
return isObject(obj) && ("length" in obj);
|
2013-01-23 11:21:25 -08:00
|
|
|
}
|
|
|
|
function nicerNewURI(uriStr, errorMsg) {
|
|
|
|
let ios = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService);
|
|
|
|
try {
|
|
|
|
return ios.newURI(uriStr, null, null);
|
|
|
|
} catch (e if (e.result == Cr.NS_ERROR_MALFORMED_URI)) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception(errorMsg + " - malformed URI: " + uriStr,
|
|
|
|
Cr.NS_ERROR_MALFORMED_URI);
|
2013-01-23 11:21:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function mustValidateServer(server) {
|
|
|
|
let url = nicerNewURI(server.url, errorMsg);
|
2013-01-24 14:58:29 -08:00
|
|
|
if (!(url.scheme in { stun:1, stuns:1, turn:1, turns:1 })) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception(errorMsg + " - improper scheme: " + url.scheme,
|
|
|
|
Cr.NS_ERROR_MALFORMED_URI);
|
2013-01-23 11:21:25 -08:00
|
|
|
}
|
|
|
|
if (server.credential && isObject(server.credential)) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception(errorMsg + " - invalid credential");
|
2013-01-23 11:21:25 -08:00
|
|
|
}
|
|
|
|
}
|
2013-01-24 14:58:29 -08:00
|
|
|
if (!isObject(rtcConfig)) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception(errorMsg);
|
2013-01-23 11:21:25 -08:00
|
|
|
}
|
2013-02-13 23:08:56 -08:00
|
|
|
if (!isArraylike(rtcConfig.iceServers)) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception(errorMsg +
|
|
|
|
" - iceServers [] property not present");
|
2013-01-24 14:58:29 -08:00
|
|
|
}
|
2013-02-13 23:08:56 -08:00
|
|
|
let len = rtcConfig.iceServers.length;
|
|
|
|
for (let i=0; i < len; i++) {
|
2013-01-24 14:58:29 -08:00
|
|
|
mustValidateServer (rtcConfig.iceServers[i], errorMsg);
|
2013-01-23 11:21:25 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-25 12:24:30 -07:00
|
|
|
/**
|
|
|
|
* Constraints look like this:
|
|
|
|
*
|
|
|
|
* {
|
2013-02-13 23:08:56 -08:00
|
|
|
* mandatory: {"OfferToReceiveAudio": true, "OfferToReceiveVideo": true },
|
|
|
|
* optional: [{"VoiceActivityDetection": true}, {"FooBar": 10}]
|
2012-10-25 12:24:30 -07:00
|
|
|
* }
|
|
|
|
*
|
2013-02-13 23:08:56 -08:00
|
|
|
* We check for basic structure of constraints and the validity of
|
|
|
|
* mandatory constraints against those we support (fail if we don't).
|
|
|
|
* Unknown optional constraints may be of any type.
|
2012-10-25 12:24:30 -07:00
|
|
|
*/
|
2013-02-13 23:08:56 -08:00
|
|
|
_mustValidateConstraints: function(constraints, errorMsg) {
|
2012-10-25 12:24:30 -07:00
|
|
|
function isObject(obj) {
|
|
|
|
return obj && (typeof obj === "object");
|
|
|
|
}
|
2013-02-13 23:08:56 -08:00
|
|
|
function isArraylike(obj) {
|
|
|
|
return isObject(obj) && ("length" in obj);
|
|
|
|
}
|
|
|
|
const SUPPORTED_CONSTRAINTS = {
|
|
|
|
OfferToReceiveAudio:1,
|
|
|
|
OfferToReceiveVideo:1,
|
|
|
|
MozDontOfferDataChannel:1
|
|
|
|
};
|
|
|
|
const OTHER_KNOWN_CONSTRAINTS = {
|
|
|
|
VoiceActivityDetection:1,
|
|
|
|
IceTransports:1,
|
|
|
|
RequestIdentity:1
|
|
|
|
};
|
|
|
|
// Parse-aid: Testing for pilot error of missing outer block avoids
|
|
|
|
// otherwise silent no-op since both mandatory and optional are optional
|
|
|
|
if (!isObject(constraints) || Array.isArray(constraints)) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception(errorMsg);
|
2013-02-13 23:08:56 -08:00
|
|
|
}
|
|
|
|
if (constraints.mandatory) {
|
|
|
|
// Testing for pilot error of using [] on mandatory here throws nicer msg
|
|
|
|
// (arrays would throw in loop below regardless but with more cryptic msg)
|
|
|
|
if (!isObject(constraints.mandatory) || Array.isArray(constraints.mandatory)) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception(errorMsg + " - malformed mandatory constraints");
|
2013-02-13 23:08:56 -08:00
|
|
|
}
|
|
|
|
for (let constraint in constraints.mandatory) {
|
|
|
|
if (!(constraint in SUPPORTED_CONSTRAINTS) &&
|
|
|
|
constraints.mandatory.hasOwnProperty(constraint)) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception(errorMsg + " - " +
|
|
|
|
((constraint in OTHER_KNOWN_CONSTRAINTS)?
|
|
|
|
"unsupported" : "unknown") +
|
|
|
|
" mandatory constraint: " + constraint);
|
2013-02-13 23:08:56 -08:00
|
|
|
}
|
|
|
|
}
|
2012-10-25 12:24:30 -07:00
|
|
|
}
|
2013-02-13 23:08:56 -08:00
|
|
|
if (constraints.optional) {
|
|
|
|
if (!isArraylike(constraints.optional)) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception(errorMsg +
|
|
|
|
" - malformed optional constraint array");
|
2013-02-13 23:08:56 -08:00
|
|
|
}
|
|
|
|
let len = constraints.optional.length;
|
|
|
|
for (let i = 0; i < len; i += 1) {
|
|
|
|
if (!isObject(constraints.optional[i])) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception(errorMsg +
|
|
|
|
" - malformed optional constraint: " +
|
|
|
|
constraints.optional[i]);
|
2013-02-13 23:08:56 -08:00
|
|
|
}
|
|
|
|
let constraints_per_entry = 0;
|
|
|
|
for (let constraint in constraints.optional[i]) {
|
|
|
|
if (constraints.optional[i].hasOwnProperty(constraint)) {
|
|
|
|
if (constraints_per_entry) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception(errorMsg +
|
|
|
|
" - optional constraint must be single key/value pair");
|
2013-02-13 23:08:56 -08:00
|
|
|
}
|
|
|
|
constraints_per_entry += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-25 12:24:30 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-01-17 12:25:17 -08:00
|
|
|
// Ideally, this should be of the form _checkState(state),
|
|
|
|
// where the state is taken from an enumeration containing
|
|
|
|
// the valid peer connection states defined in the WebRTC
|
|
|
|
// spec. See Bug 831756.
|
|
|
|
_checkClosed: function() {
|
|
|
|
if (this._closed) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception("Peer connection is closed");
|
2013-01-17 12:25:17 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-06 22:34:30 -07:00
|
|
|
createOffer: function(onSuccess, onError, constraints) {
|
2012-10-26 15:31:17 -07:00
|
|
|
if (!constraints) {
|
|
|
|
constraints = {};
|
|
|
|
}
|
|
|
|
|
2013-02-13 23:08:56 -08:00
|
|
|
this._mustValidateConstraints(constraints, "createOffer passed invalid constraints");
|
2012-10-06 22:34:30 -07:00
|
|
|
this._onCreateOfferSuccess = onSuccess;
|
|
|
|
this._onCreateOfferFailure = onError;
|
|
|
|
|
|
|
|
this._queueOrRun({
|
2013-04-16 22:16:02 -07:00
|
|
|
func: this._getPC().createOffer,
|
2012-10-06 22:34:30 -07:00
|
|
|
args: [constraints],
|
|
|
|
wait: true
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-01-24 19:44:30 -08:00
|
|
|
_createAnswer: function(onSuccess, onError, constraints, provisional) {
|
|
|
|
this._onCreateAnswerSuccess = onSuccess;
|
|
|
|
this._onCreateAnswerFailure = onError;
|
|
|
|
|
2012-10-15 11:35:21 -07:00
|
|
|
if (!this.remoteDescription) {
|
2013-01-24 19:44:30 -08:00
|
|
|
|
2013-03-29 13:30:16 -07:00
|
|
|
this._observer.onCreateAnswerError(Ci.IPeerConnection.kInvalidState,
|
2013-01-24 19:44:30 -08:00
|
|
|
"setRemoteDescription not called");
|
|
|
|
return;
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
|
|
|
|
2012-10-15 11:35:21 -07:00
|
|
|
if (this.remoteDescription.type != "offer") {
|
2013-01-24 19:44:30 -08:00
|
|
|
|
2013-03-29 13:30:16 -07:00
|
|
|
this._observer.onCreateAnswerError(Ci.IPeerConnection.kInvalidState,
|
2013-01-24 19:44:30 -08:00
|
|
|
"No outstanding offer");
|
|
|
|
return;
|
2012-10-25 12:24:30 -07:00
|
|
|
}
|
|
|
|
|
2013-01-24 19:44:30 -08:00
|
|
|
// TODO: Implement provisional answer.
|
|
|
|
|
2013-04-16 22:16:02 -07:00
|
|
|
this._getPC().createAnswer(constraints);
|
2013-01-24 19:44:30 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
createAnswer: function(onSuccess, onError, constraints, provisional) {
|
2012-10-26 15:31:17 -07:00
|
|
|
if (!constraints) {
|
|
|
|
constraints = {};
|
|
|
|
}
|
|
|
|
|
2013-02-13 23:08:56 -08:00
|
|
|
this._mustValidateConstraints(constraints, "createAnswer passed invalid constraints");
|
2012-10-06 22:34:30 -07:00
|
|
|
|
|
|
|
if (!provisional) {
|
|
|
|
provisional = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._queueOrRun({
|
2013-01-24 19:44:30 -08:00
|
|
|
func: this._createAnswer,
|
|
|
|
args: [onSuccess, onError, constraints, provisional],
|
2012-10-06 22:34:30 -07:00
|
|
|
wait: true
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
setLocalDescription: function(desc, onSuccess, onError) {
|
2013-01-17 12:25:17 -08:00
|
|
|
// TODO -- if we have two setLocalDescriptions in the
|
|
|
|
// queue,this code overwrites the callbacks for the first
|
|
|
|
// one with the callbacks for the second one. See Bug 831759.
|
2012-10-06 22:34:30 -07:00
|
|
|
this._onSetLocalDescriptionSuccess = onSuccess;
|
|
|
|
this._onSetLocalDescriptionFailure = onError;
|
|
|
|
|
|
|
|
let type;
|
|
|
|
switch (desc.type) {
|
|
|
|
case "offer":
|
|
|
|
type = Ci.IPeerConnection.kActionOffer;
|
|
|
|
break;
|
|
|
|
case "answer":
|
|
|
|
type = Ci.IPeerConnection.kActionAnswer;
|
|
|
|
break;
|
|
|
|
default:
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception("Invalid type " + desc.type +
|
|
|
|
" provided to setLocalDescription");
|
2012-10-06 22:34:30 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._queueOrRun({
|
2013-04-16 22:16:02 -07:00
|
|
|
func: this._getPC().setLocalDescription,
|
2012-10-06 22:34:30 -07:00
|
|
|
args: [type, desc.sdp],
|
2013-01-14 14:25:54 -08:00
|
|
|
wait: true,
|
|
|
|
type: desc.type
|
2012-10-06 22:34:30 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
setRemoteDescription: function(desc, onSuccess, onError) {
|
2013-01-17 12:25:17 -08:00
|
|
|
// TODO -- if we have two setRemoteDescriptions in the
|
|
|
|
// queue, this code overwrites the callbacks for the first
|
|
|
|
// one with the callbacks for the second one. See Bug 831759.
|
2012-10-06 22:34:30 -07:00
|
|
|
this._onSetRemoteDescriptionSuccess = onSuccess;
|
|
|
|
this._onSetRemoteDescriptionFailure = onError;
|
|
|
|
|
|
|
|
let type;
|
|
|
|
switch (desc.type) {
|
|
|
|
case "offer":
|
|
|
|
type = Ci.IPeerConnection.kActionOffer;
|
|
|
|
break;
|
|
|
|
case "answer":
|
|
|
|
type = Ci.IPeerConnection.kActionAnswer;
|
|
|
|
break;
|
|
|
|
default:
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception("Invalid type " + desc.type +
|
|
|
|
" provided to setRemoteDescription");
|
2012-10-06 22:34:30 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._queueOrRun({
|
2013-04-16 22:16:02 -07:00
|
|
|
func: this._getPC().setRemoteDescription,
|
2012-10-06 22:34:30 -07:00
|
|
|
args: [type, desc.sdp],
|
2013-01-14 14:25:54 -08:00
|
|
|
wait: true,
|
|
|
|
type: desc.type
|
2012-10-06 22:34:30 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
updateIce: function(config, constraints, restart) {
|
|
|
|
return Cr.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
},
|
|
|
|
|
2013-01-31 12:43:03 -08:00
|
|
|
addIceCandidate: function(cand, onSuccess, onError) {
|
2012-10-06 22:34:30 -07:00
|
|
|
if (!cand) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception("NULL candidate passed to addIceCandidate!");
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
2012-12-02 14:10:19 -08:00
|
|
|
|
|
|
|
if (!cand.candidate || !cand.sdpMLineIndex) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception("Invalid candidate passed to addIceCandidate!");
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
|
|
|
|
2013-01-31 12:43:03 -08:00
|
|
|
this._onAddIceCandidateSuccess = onSuccess;
|
|
|
|
this._onAddIceCandidateError = onError;
|
|
|
|
|
2012-10-06 22:34:30 -07:00
|
|
|
this._queueOrRun({
|
2013-04-16 22:16:02 -07:00
|
|
|
func: this._getPC().addIceCandidate,
|
2012-12-02 14:10:19 -08:00
|
|
|
args: [cand.candidate, cand.sdpMid || "", cand.sdpMLineIndex],
|
2013-01-31 12:43:03 -08:00
|
|
|
wait: true
|
2012-10-06 22:34:30 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
addStream: function(stream, constraints) {
|
|
|
|
// TODO: Implement constraints.
|
|
|
|
this._queueOrRun({
|
2013-04-16 22:16:02 -07:00
|
|
|
func: this._getPC().addStream,
|
2012-10-06 22:34:30 -07:00
|
|
|
args: [stream],
|
|
|
|
wait: false
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
removeStream: function(stream) {
|
2013-03-29 08:29:28 -07:00
|
|
|
//Bug844295: Not implemeting this functionality.
|
|
|
|
return Cr.NS_ERROR_NOT_IMPLEMENTED;
|
2012-10-06 22:34:30 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
close: function() {
|
|
|
|
this._queueOrRun({
|
2013-04-16 22:16:02 -07:00
|
|
|
func: this._getPC().close,
|
2012-11-18 20:53:14 -08:00
|
|
|
args: [false],
|
2012-10-06 22:34:30 -07:00
|
|
|
wait: false
|
|
|
|
});
|
2012-10-20 09:43:57 -07:00
|
|
|
this._closed = true;
|
2012-10-06 22:34:30 -07:00
|
|
|
},
|
|
|
|
|
2012-09-13 09:04:31 -07:00
|
|
|
get localStreams() {
|
2013-01-17 12:25:17 -08:00
|
|
|
this._checkClosed();
|
2013-04-16 22:16:02 -07:00
|
|
|
return this._getPC().localStreams;
|
2012-09-13 09:04:31 -07:00
|
|
|
},
|
2013-01-14 14:25:54 -08:00
|
|
|
|
2012-09-13 09:04:31 -07:00
|
|
|
get remoteStreams() {
|
2013-01-17 12:25:17 -08:00
|
|
|
this._checkClosed();
|
2013-04-16 22:16:02 -07:00
|
|
|
return this._getPC().remoteStreams;
|
2012-09-13 09:04:31 -07:00
|
|
|
},
|
|
|
|
|
2013-01-14 14:25:54 -08:00
|
|
|
get localDescription() {
|
2013-01-17 12:25:17 -08:00
|
|
|
this._checkClosed();
|
2013-04-16 22:16:02 -07:00
|
|
|
let sdp = this._getPC().localDescription;
|
2013-01-14 14:25:54 -08:00
|
|
|
if (sdp.length == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
type: this._localType, sdp: sdp,
|
|
|
|
__exposedProps__: { type: "rw", sdp: "rw" }
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
get remoteDescription() {
|
2013-01-17 12:25:17 -08:00
|
|
|
this._checkClosed();
|
2013-04-16 22:16:02 -07:00
|
|
|
let sdp = this._getPC().remoteDescription;
|
2013-01-14 14:25:54 -08:00
|
|
|
if (sdp.length == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
type: this._remoteType, sdp: sdp,
|
|
|
|
__exposedProps__: { type: "rw", sdp: "rw" }
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2013-03-27 17:48:52 -07:00
|
|
|
get readyState() {
|
|
|
|
// checking for our local pc closed indication
|
|
|
|
// before invoking the pc methods.
|
|
|
|
if(this._closed) {
|
|
|
|
return "closed";
|
|
|
|
}
|
|
|
|
|
|
|
|
var state="undefined";
|
2013-04-16 22:16:02 -07:00
|
|
|
switch (this._getPC().readyState) {
|
2013-03-27 17:48:52 -07:00
|
|
|
case Ci.IPeerConnection.kNew:
|
|
|
|
state = "new";
|
|
|
|
break;
|
|
|
|
case Ci.IPeerConnection.kNegotiating:
|
|
|
|
state = "negotiating";
|
|
|
|
break;
|
|
|
|
case Ci.IPeerConnection.kActive:
|
|
|
|
state = "active";
|
|
|
|
break;
|
|
|
|
case Ci.IPeerConnection.kClosing:
|
|
|
|
state = "closing";
|
|
|
|
break;
|
|
|
|
case Ci.IPeerConnection.kClosed:
|
|
|
|
state = "closed";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
},
|
|
|
|
|
2012-10-06 22:34:30 -07:00
|
|
|
createDataChannel: function(label, dict) {
|
2013-01-17 12:25:17 -08:00
|
|
|
this._checkClosed();
|
2013-03-31 18:09:25 -07:00
|
|
|
if (dict == undefined) {
|
|
|
|
dict = {};
|
|
|
|
}
|
|
|
|
if (dict.maxRetransmitTime != undefined &&
|
2012-10-06 22:34:30 -07:00
|
|
|
dict.maxRetransmitNum != undefined) {
|
2013-03-14 14:36:50 -07:00
|
|
|
throw new Components.Exception("Both maxRetransmitTime and maxRetransmitNum cannot be provided");
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
2013-03-31 18:09:25 -07:00
|
|
|
let protocol;
|
|
|
|
if (dict.protocol == undefined) {
|
|
|
|
protocol = "";
|
|
|
|
} else {
|
|
|
|
protocol = dict.protocol;
|
|
|
|
}
|
2012-10-06 22:34:30 -07:00
|
|
|
|
|
|
|
// Must determine the type where we still know if entries are undefined.
|
|
|
|
let type;
|
|
|
|
if (dict.maxRetransmitTime != undefined) {
|
2012-11-07 11:07:22 -08:00
|
|
|
type = Ci.IPeerConnection.kDataChannelPartialReliableTimed;
|
2012-10-06 22:34:30 -07:00
|
|
|
} else if (dict.maxRetransmitNum != undefined) {
|
2012-11-07 11:07:22 -08:00
|
|
|
type = Ci.IPeerConnection.kDataChannelPartialReliableRexmit;
|
2012-10-06 22:34:30 -07:00
|
|
|
} else {
|
2012-11-07 11:07:22 -08:00
|
|
|
type = Ci.IPeerConnection.kDataChannelReliable;
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Synchronous since it doesn't block.
|
2013-04-16 22:16:02 -07:00
|
|
|
let channel = this._getPC().createDataChannel(
|
2013-03-31 18:09:25 -07:00
|
|
|
label, protocol, type, dict.outOfOrderAllowed, dict.maxRetransmitTime,
|
|
|
|
dict.maxRetransmitNum, dict.preset ? true : false,
|
|
|
|
dict.stream != undefined ? dict.stream : 0xFFFF
|
2012-10-06 22:34:30 -07:00
|
|
|
);
|
|
|
|
return channel;
|
|
|
|
},
|
|
|
|
|
|
|
|
connectDataConnection: function(localport, remoteport, numstreams) {
|
|
|
|
if (numstreams == undefined || numstreams <= 0) {
|
|
|
|
numstreams = 16;
|
|
|
|
}
|
|
|
|
this._queueOrRun({
|
2013-04-16 22:16:02 -07:00
|
|
|
func: this._getPC().connectDataConnection,
|
2012-10-06 22:34:30 -07:00
|
|
|
args: [localport, remoteport, numstreams],
|
|
|
|
wait: false
|
|
|
|
});
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-16 16:23:07 -07:00
|
|
|
function RTCError(code, message) {
|
|
|
|
this.name = this.reasonName[Math.min(code, this.reasonName.length - 1)];
|
|
|
|
this.message = (typeof message === "string")? message : this.name;
|
|
|
|
this.__exposedProps__ = { name: "rw", message: "rw" };
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
2013-04-16 16:23:07 -07:00
|
|
|
RTCError.prototype = {
|
2013-03-29 13:30:16 -07:00
|
|
|
// These strings must match those defined in the WebRTC spec.
|
|
|
|
reasonName: [
|
|
|
|
"NO_ERROR", // Should never happen -- only used for testing
|
|
|
|
"INVALID_CONSTRAINTS_TYPE",
|
|
|
|
"INVALID_CANDIDATE_TYPE",
|
|
|
|
"INVALID_MEDIASTREAM_TRACK",
|
|
|
|
"INVALID_STATE",
|
|
|
|
"INVALID_SESSION_DESCRIPTION",
|
|
|
|
"INCOMPATIBLE_SESSION_DESCRIPTION",
|
|
|
|
"INCOMPATIBLE_CONSTRAINTS",
|
|
|
|
"INCOMPATIBLE_MEDIASTREAMTRACK",
|
|
|
|
"INTERNAL_ERROR"
|
2013-04-16 16:23:07 -07:00
|
|
|
]
|
|
|
|
};
|
2013-03-29 13:30:16 -07:00
|
|
|
|
2013-04-16 16:23:07 -07:00
|
|
|
// This is a separate object because we don't want to expose it to DOM.
|
|
|
|
function PeerConnectionObserver(dompc) {
|
|
|
|
this._dompc = dompc;
|
|
|
|
}
|
|
|
|
PeerConnectionObserver.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.IPeerConnectionObserver,
|
|
|
|
Ci.nsISupportsWeakReference]),
|
2013-03-29 13:30:16 -07:00
|
|
|
|
2013-04-16 16:23:07 -07:00
|
|
|
callCB: function(callback, arg) {
|
2013-03-29 13:30:16 -07:00
|
|
|
if (callback) {
|
|
|
|
try {
|
2013-04-16 16:23:07 -07:00
|
|
|
callback.onCallback(arg);
|
|
|
|
} catch(e) {
|
|
|
|
// A content script (user-provided) callback threw an error. We don't
|
|
|
|
// want this to take down peerconnection, but we still want the user
|
|
|
|
// to see it, so we catch it, report it, and move on.
|
|
|
|
//
|
|
|
|
// We do stack parsing in two different places for different reasons:
|
|
|
|
|
|
|
|
var msg;
|
|
|
|
if (e.result == Cr.NS_ERROR_XPC_JS_THREW_JS_OBJECT) {
|
|
|
|
// TODO(jib@mozilla.com): Revisit once bug 862153 is fixed.
|
|
|
|
//
|
|
|
|
// The actual content script frame is unavailable due to bug 862153,
|
|
|
|
// so users see file and line # into this file, which is not helpful.
|
|
|
|
//
|
|
|
|
// 1) Fix up the error message itself to differentiate between the
|
|
|
|
// 22 places we call callCB() in this file, using plain JS stack.
|
|
|
|
//
|
|
|
|
// Tweak the existing NS_ERROR_XPC_JS_THREW_JS_OBJECT message:
|
|
|
|
// -'Error: x' when calling method: [RTCPeerConCallback::onCallback]
|
|
|
|
// +'Error: x' when calling method: [RTCPeerConCallback::onCreateOfferError]
|
|
|
|
|
|
|
|
let caller = Error().stack.split("\n")[1].split("@")[0];
|
|
|
|
// caller ~= "PeerConnectionObserver.prototype.onCreateOfferError"
|
|
|
|
|
|
|
|
msg = e.message.replace("::onCallback", "::" + caller.split(".")[2]);
|
|
|
|
} else {
|
|
|
|
msg = e.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log error message to web console and window.onerror, if present.
|
|
|
|
//
|
|
|
|
// 2) nsIScriptError doesn't understand the nsIStackFrame format, so
|
|
|
|
// do the translation by extracting file and line from XPCOM stack:
|
|
|
|
//
|
|
|
|
// e.location ~= "JS frame :: file://.js :: RTCPCCb::onCallback :: line 1"
|
|
|
|
|
|
|
|
let stack = e.location.toString().split(" :: ");
|
|
|
|
let file = stack[1];
|
|
|
|
let line = parseInt(stack[3].split(" ")[1]);
|
|
|
|
|
2013-04-17 16:03:39 -07:00
|
|
|
let scriptErrorClass = Cc["@mozilla.org/scripterror;1"];
|
|
|
|
let scriptError = scriptErrorClass.createInstance(Ci.nsIScriptError);
|
2013-04-16 16:23:07 -07:00
|
|
|
scriptError.initWithWindowID(msg, file, null, line, 0,
|
|
|
|
Ci.nsIScriptError.exceptionFlag,
|
|
|
|
"content javascript",
|
|
|
|
this._dompc._winID);
|
2013-04-17 16:03:39 -07:00
|
|
|
let console = Cc["@mozilla.org/consoleservice;1"].
|
|
|
|
getService(Ci.nsIConsoleService);
|
|
|
|
console.logMessage(scriptError);
|
|
|
|
|
|
|
|
// Safely call onerror directly if present (necessary for testing)
|
|
|
|
try {
|
|
|
|
if (typeof this._dompc._win.onerror === "function") {
|
|
|
|
this._dompc._win.onerror(msg, file, line);
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
// If onerror itself throws, service it.
|
|
|
|
try {
|
|
|
|
let scriptError = scriptErrorClass.createInstance(Ci.nsIScriptError);
|
|
|
|
scriptError.initWithWindowID(e.message, e.fileName, null,
|
|
|
|
e.lineNumber, 0,
|
|
|
|
Ci.nsIScriptError.exceptionFlag,
|
|
|
|
"content javascript",
|
|
|
|
this._dompc._winID);
|
|
|
|
console.logMessage(scriptError);
|
|
|
|
} catch(e) {}
|
2013-04-16 16:23:07 -07:00
|
|
|
}
|
|
|
|
}
|
2013-03-29 13:30:16 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-06 22:34:30 -07:00
|
|
|
onCreateOfferSuccess: function(offer) {
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc._onCreateOfferSuccess,
|
|
|
|
{ type: "offer", sdp: offer,
|
|
|
|
__exposedProps__: { type: "rw", sdp: "rw" } });
|
2012-10-06 22:34:30 -07:00
|
|
|
this._dompc._executeNext();
|
|
|
|
},
|
|
|
|
|
2013-03-29 13:30:16 -07:00
|
|
|
onCreateOfferError: function(code, message) {
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc._onCreateOfferFailure, new RTCError(code, message));
|
2012-10-06 22:34:30 -07:00
|
|
|
this._dompc._executeNext();
|
|
|
|
},
|
|
|
|
|
|
|
|
onCreateAnswerSuccess: function(answer) {
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB (this._dompc._onCreateAnswerSuccess,
|
|
|
|
{ type: "answer", sdp: answer,
|
|
|
|
__exposedProps__: { type: "rw", sdp: "rw" } });
|
2012-10-06 22:34:30 -07:00
|
|
|
this._dompc._executeNext();
|
|
|
|
},
|
|
|
|
|
2013-03-29 13:30:16 -07:00
|
|
|
onCreateAnswerError: function(code, message) {
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc._onCreateAnswerFailure, new RTCError(code, message));
|
2012-10-06 22:34:30 -07:00
|
|
|
this._dompc._executeNext();
|
|
|
|
},
|
|
|
|
|
2013-03-29 13:30:16 -07:00
|
|
|
onSetLocalDescriptionSuccess: function() {
|
2013-01-14 14:25:54 -08:00
|
|
|
this._dompc._localType = this._dompc._pendingType;
|
|
|
|
this._dompc._pendingType = null;
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc._onSetLocalDescriptionSuccess);
|
2013-04-09 14:18:32 -07:00
|
|
|
|
|
|
|
// Until we support generating trickle ICE candidates,
|
|
|
|
// we go ahead and trigger a call of onicecandidate here.
|
|
|
|
// This is to provide some level of compatibility with
|
|
|
|
// scripts that expect this behavior (which is how Chrome
|
|
|
|
// signals that no further trickle candidates will be sent).
|
|
|
|
// TODO: This needs to be removed when Bug 842459 lands.
|
|
|
|
this.foundIceCandidate(null);
|
|
|
|
|
2012-10-06 22:34:30 -07:00
|
|
|
this._dompc._executeNext();
|
|
|
|
},
|
|
|
|
|
2013-03-29 13:30:16 -07:00
|
|
|
onSetRemoteDescriptionSuccess: function() {
|
2013-01-14 14:25:54 -08:00
|
|
|
this._dompc._remoteType = this._dompc._pendingType;
|
|
|
|
this._dompc._pendingType = null;
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc._onSetRemoteDescriptionSuccess);
|
2012-10-06 22:34:30 -07:00
|
|
|
this._dompc._executeNext();
|
|
|
|
},
|
|
|
|
|
2013-03-29 13:30:16 -07:00
|
|
|
onSetLocalDescriptionError: function(code, message) {
|
2013-01-14 14:25:54 -08:00
|
|
|
this._dompc._pendingType = null;
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc._onSetLocalDescriptionFailure,
|
|
|
|
new RTCError(code, message));
|
2012-10-06 22:34:30 -07:00
|
|
|
this._dompc._executeNext();
|
|
|
|
},
|
|
|
|
|
2013-03-29 13:30:16 -07:00
|
|
|
onSetRemoteDescriptionError: function(code, message) {
|
2013-01-14 14:25:54 -08:00
|
|
|
this._dompc._pendingType = null;
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc._onSetRemoteDescriptionFailure,
|
|
|
|
new RTCError(code, message));
|
2012-10-06 22:34:30 -07:00
|
|
|
this._dompc._executeNext();
|
|
|
|
},
|
|
|
|
|
2013-03-29 13:30:16 -07:00
|
|
|
onAddIceCandidateSuccess: function() {
|
2013-01-31 12:43:03 -08:00
|
|
|
this._dompc._pendingType = null;
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc._onAddIceCandidateSuccess);
|
2013-01-31 12:43:03 -08:00
|
|
|
this._dompc._executeNext();
|
|
|
|
},
|
|
|
|
|
2013-03-29 13:30:16 -07:00
|
|
|
onAddIceCandidateError: function(code, message) {
|
2013-01-31 12:43:03 -08:00
|
|
|
this._dompc._pendingType = null;
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc._onAddIceCandidateError, new RTCError(code, message));
|
2013-01-31 12:43:03 -08:00
|
|
|
this._dompc._executeNext();
|
|
|
|
},
|
|
|
|
|
2012-10-06 22:34:30 -07:00
|
|
|
onStateChange: function(state) {
|
|
|
|
if (state != Ci.IPeerConnectionObserver.kIceState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (this._dompc._pc.iceState) {
|
|
|
|
case Ci.IPeerConnection.kIceGathering:
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc.ongatheringchange, "gathering");
|
2012-10-06 22:34:30 -07:00
|
|
|
break;
|
|
|
|
case Ci.IPeerConnection.kIceWaiting:
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc.onicechange, "starting");
|
2012-10-06 22:34:30 -07:00
|
|
|
this._dompc._executeNext();
|
|
|
|
break;
|
|
|
|
case Ci.IPeerConnection.kIceChecking:
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc.onicechange, "checking");
|
2012-10-06 22:34:30 -07:00
|
|
|
break;
|
|
|
|
case Ci.IPeerConnection.kIceConnected:
|
|
|
|
// ICE gathering complete.
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc.onicechange, "connected");
|
|
|
|
this.callCB(this._dompc.ongatheringchange, "complete");
|
2012-10-06 22:34:30 -07:00
|
|
|
break;
|
|
|
|
case Ci.IPeerConnection.kIceFailed:
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc.onicechange, "failed");
|
2012-10-06 22:34:30 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Unknown state!
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onAddStream: function(stream, type) {
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc.onaddstream,
|
|
|
|
{ stream: stream, type: type,
|
|
|
|
__exposedProps__: { stream: "r", type: "r" } });
|
2012-10-06 22:34:30 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
onRemoveStream: function(stream, type) {
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc.onremovestream,
|
|
|
|
{ stream: stream, type: type,
|
|
|
|
__exposedProps__: { stream: "r", type: "r" } });
|
2012-10-06 22:34:30 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
foundIceCandidate: function(cand) {
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc.onicecandidate,
|
|
|
|
{candidate: cand, __exposedProps__: { candidate: "rw" } });
|
2012-10-06 22:34:30 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
notifyDataChannel: function(channel) {
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB(this._dompc.ondatachannel,
|
|
|
|
{ channel: channel, __exposedProps__: { channel: "r" } });
|
2012-10-06 22:34:30 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
notifyConnection: function() {
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB (this._dompc.onconnection);
|
2012-10-06 22:34:30 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
notifyClosedConnection: function() {
|
2013-04-16 16:23:07 -07:00
|
|
|
this.callCB (this._dompc.onclosedconnection);
|
2012-10-06 22:34:30 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory(
|
2012-11-09 21:04:27 -08:00
|
|
|
[GlobalPCList, IceCandidate, SessionDescription, PeerConnection]
|
2012-10-06 22:34:30 -07:00
|
|
|
);
|