Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* jshint moz:true, browser:true */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | "use strict"; |
michael@0 | 7 | |
michael@0 | 8 | const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
michael@0 | 9 | |
michael@0 | 10 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 11 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 12 | XPCOMUtils.defineLazyModuleGetter(this, "PeerConnectionIdp", |
michael@0 | 13 | "resource://gre/modules/media/PeerConnectionIdp.jsm"); |
michael@0 | 14 | |
michael@0 | 15 | const PC_CONTRACT = "@mozilla.org/dom/peerconnection;1"; |
michael@0 | 16 | const PC_OBS_CONTRACT = "@mozilla.org/dom/peerconnectionobserver;1"; |
michael@0 | 17 | const PC_ICE_CONTRACT = "@mozilla.org/dom/rtcicecandidate;1"; |
michael@0 | 18 | const PC_SESSION_CONTRACT = "@mozilla.org/dom/rtcsessiondescription;1"; |
michael@0 | 19 | const PC_MANAGER_CONTRACT = "@mozilla.org/dom/peerconnectionmanager;1"; |
michael@0 | 20 | const PC_STATS_CONTRACT = "@mozilla.org/dom/rtcstatsreport;1"; |
michael@0 | 21 | const PC_IDENTITY_CONTRACT = "@mozilla.org/dom/rtcidentityassertion;1"; |
michael@0 | 22 | |
michael@0 | 23 | const PC_CID = Components.ID("{00e0e20d-1494-4776-8e0e-0f0acbea3c79}"); |
michael@0 | 24 | const PC_OBS_CID = Components.ID("{d1748d4c-7f6a-4dc5-add6-d55b7678537e}"); |
michael@0 | 25 | const PC_ICE_CID = Components.ID("{02b9970c-433d-4cc2-923d-f7028ac66073}"); |
michael@0 | 26 | const PC_SESSION_CID = Components.ID("{1775081b-b62d-4954-8ffe-a067bbf508a7}"); |
michael@0 | 27 | const PC_MANAGER_CID = Components.ID("{7293e901-2be3-4c02-b4bd-cbef6fc24f78}"); |
michael@0 | 28 | const PC_STATS_CID = Components.ID("{7fe6e18b-0da3-4056-bf3b-440ef3809e06}"); |
michael@0 | 29 | const PC_IDENTITY_CID = Components.ID("{1abc7499-3c54-43e0-bd60-686e2703f072}"); |
michael@0 | 30 | |
michael@0 | 31 | // Global list of PeerConnection objects, so they can be cleaned up when |
michael@0 | 32 | // a page is torn down. (Maps inner window ID to an array of PC objects). |
michael@0 | 33 | function GlobalPCList() { |
michael@0 | 34 | this._list = {}; |
michael@0 | 35 | this._networkdown = false; // XXX Need to query current state somehow |
michael@0 | 36 | Services.obs.addObserver(this, "inner-window-destroyed", true); |
michael@0 | 37 | Services.obs.addObserver(this, "profile-change-net-teardown", true); |
michael@0 | 38 | Services.obs.addObserver(this, "network:offline-about-to-go-offline", true); |
michael@0 | 39 | Services.obs.addObserver(this, "network:offline-status-changed", true); |
michael@0 | 40 | } |
michael@0 | 41 | GlobalPCList.prototype = { |
michael@0 | 42 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, |
michael@0 | 43 | Ci.nsISupportsWeakReference, |
michael@0 | 44 | Ci.IPeerConnectionManager]), |
michael@0 | 45 | classID: PC_MANAGER_CID, |
michael@0 | 46 | _xpcom_factory: { |
michael@0 | 47 | createInstance: function(outer, iid) { |
michael@0 | 48 | if (outer) { |
michael@0 | 49 | throw Cr.NS_ERROR_NO_AGGREGATION; |
michael@0 | 50 | } |
michael@0 | 51 | return _globalPCList.QueryInterface(iid); |
michael@0 | 52 | } |
michael@0 | 53 | }, |
michael@0 | 54 | |
michael@0 | 55 | addPC: function(pc) { |
michael@0 | 56 | let winID = pc._winID; |
michael@0 | 57 | if (this._list[winID]) { |
michael@0 | 58 | this._list[winID].push(Cu.getWeakReference(pc)); |
michael@0 | 59 | } else { |
michael@0 | 60 | this._list[winID] = [Cu.getWeakReference(pc)]; |
michael@0 | 61 | } |
michael@0 | 62 | this.removeNullRefs(winID); |
michael@0 | 63 | }, |
michael@0 | 64 | |
michael@0 | 65 | removeNullRefs: function(winID) { |
michael@0 | 66 | if (this._list[winID] === undefined) { |
michael@0 | 67 | return; |
michael@0 | 68 | } |
michael@0 | 69 | this._list[winID] = this._list[winID].filter( |
michael@0 | 70 | function (e,i,a) { return e.get() !== null; }); |
michael@0 | 71 | |
michael@0 | 72 | if (this._list[winID].length === 0) { |
michael@0 | 73 | delete this._list[winID]; |
michael@0 | 74 | } |
michael@0 | 75 | }, |
michael@0 | 76 | |
michael@0 | 77 | hasActivePeerConnection: function(winID) { |
michael@0 | 78 | this.removeNullRefs(winID); |
michael@0 | 79 | return this._list[winID] ? true : false; |
michael@0 | 80 | }, |
michael@0 | 81 | |
michael@0 | 82 | observe: function(subject, topic, data) { |
michael@0 | 83 | let cleanupPcRef = function(pcref) { |
michael@0 | 84 | let pc = pcref.get(); |
michael@0 | 85 | if (pc) { |
michael@0 | 86 | pc._pc.close(); |
michael@0 | 87 | delete pc._observer; |
michael@0 | 88 | pc._pc = null; |
michael@0 | 89 | } |
michael@0 | 90 | }; |
michael@0 | 91 | |
michael@0 | 92 | let cleanupWinId = function(list, winID) { |
michael@0 | 93 | if (list.hasOwnProperty(winID)) { |
michael@0 | 94 | list[winID].forEach(cleanupPcRef); |
michael@0 | 95 | delete list[winID]; |
michael@0 | 96 | } |
michael@0 | 97 | }; |
michael@0 | 98 | |
michael@0 | 99 | if (topic == "inner-window-destroyed") { |
michael@0 | 100 | cleanupWinId(this._list, subject.QueryInterface(Ci.nsISupportsPRUint64).data); |
michael@0 | 101 | } else if (topic == "profile-change-net-teardown" || |
michael@0 | 102 | topic == "network:offline-about-to-go-offline") { |
michael@0 | 103 | // Delete all peerconnections on shutdown - mostly synchronously (we |
michael@0 | 104 | // need them to be done deleting transports and streams before we |
michael@0 | 105 | // return)! All socket operations must be queued to STS thread |
michael@0 | 106 | // before we return to here. |
michael@0 | 107 | // Also kill them if "Work Offline" is selected - more can be created |
michael@0 | 108 | // while offline, but attempts to connect them should fail. |
michael@0 | 109 | for (let winId in this._list) { |
michael@0 | 110 | cleanupWinId(this._list, winId); |
michael@0 | 111 | } |
michael@0 | 112 | this._networkdown = true; |
michael@0 | 113 | } |
michael@0 | 114 | else if (topic == "network:offline-status-changed") { |
michael@0 | 115 | if (data == "offline") { |
michael@0 | 116 | // this._list shold be empty here |
michael@0 | 117 | this._networkdown = true; |
michael@0 | 118 | } else if (data == "online") { |
michael@0 | 119 | this._networkdown = false; |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | }, |
michael@0 | 123 | |
michael@0 | 124 | }; |
michael@0 | 125 | let _globalPCList = new GlobalPCList(); |
michael@0 | 126 | |
michael@0 | 127 | function RTCIceCandidate() { |
michael@0 | 128 | this.candidate = this.sdpMid = this.sdpMLineIndex = null; |
michael@0 | 129 | } |
michael@0 | 130 | RTCIceCandidate.prototype = { |
michael@0 | 131 | classDescription: "mozRTCIceCandidate", |
michael@0 | 132 | classID: PC_ICE_CID, |
michael@0 | 133 | contractID: PC_ICE_CONTRACT, |
michael@0 | 134 | QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, |
michael@0 | 135 | Ci.nsIDOMGlobalPropertyInitializer]), |
michael@0 | 136 | |
michael@0 | 137 | init: function(win) { this._win = win; }, |
michael@0 | 138 | |
michael@0 | 139 | __init: function(dict) { |
michael@0 | 140 | this.candidate = dict.candidate; |
michael@0 | 141 | this.sdpMid = dict.sdpMid; |
michael@0 | 142 | this.sdpMLineIndex = ("sdpMLineIndex" in dict)? dict.sdpMLineIndex : null; |
michael@0 | 143 | } |
michael@0 | 144 | }; |
michael@0 | 145 | |
michael@0 | 146 | function RTCSessionDescription() { |
michael@0 | 147 | this.type = this.sdp = null; |
michael@0 | 148 | } |
michael@0 | 149 | RTCSessionDescription.prototype = { |
michael@0 | 150 | classDescription: "mozRTCSessionDescription", |
michael@0 | 151 | classID: PC_SESSION_CID, |
michael@0 | 152 | contractID: PC_SESSION_CONTRACT, |
michael@0 | 153 | QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, |
michael@0 | 154 | Ci.nsIDOMGlobalPropertyInitializer]), |
michael@0 | 155 | |
michael@0 | 156 | init: function(win) { this._win = win; }, |
michael@0 | 157 | |
michael@0 | 158 | __init: function(dict) { |
michael@0 | 159 | this.type = dict.type; |
michael@0 | 160 | this.sdp = dict.sdp; |
michael@0 | 161 | } |
michael@0 | 162 | }; |
michael@0 | 163 | |
michael@0 | 164 | function RTCStatsReport(win, dict) { |
michael@0 | 165 | function appendStats(stats, report) { |
michael@0 | 166 | stats.forEach(function(stat) { |
michael@0 | 167 | report[stat.id] = stat; |
michael@0 | 168 | }); |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | this._win = win; |
michael@0 | 172 | this._pcid = dict.pcid; |
michael@0 | 173 | this._report = {}; |
michael@0 | 174 | appendStats(dict.inboundRTPStreamStats, this._report); |
michael@0 | 175 | appendStats(dict.outboundRTPStreamStats, this._report); |
michael@0 | 176 | appendStats(dict.mediaStreamTrackStats, this._report); |
michael@0 | 177 | appendStats(dict.mediaStreamStats, this._report); |
michael@0 | 178 | appendStats(dict.transportStats, this._report); |
michael@0 | 179 | appendStats(dict.iceComponentStats, this._report); |
michael@0 | 180 | appendStats(dict.iceCandidatePairStats, this._report); |
michael@0 | 181 | appendStats(dict.iceCandidateStats, this._report); |
michael@0 | 182 | appendStats(dict.codecStats, this._report); |
michael@0 | 183 | } |
michael@0 | 184 | RTCStatsReport.prototype = { |
michael@0 | 185 | classDescription: "RTCStatsReport", |
michael@0 | 186 | classID: PC_STATS_CID, |
michael@0 | 187 | contractID: PC_STATS_CONTRACT, |
michael@0 | 188 | QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), |
michael@0 | 189 | |
michael@0 | 190 | // TODO: Change to use webidl getters once available (Bug 952122) |
michael@0 | 191 | // |
michael@0 | 192 | // Since webidl getters are not available, we make the stats available as |
michael@0 | 193 | // enumerable read-only properties directly on our content-facing object. |
michael@0 | 194 | // Must be called after our webidl sandwich is made. |
michael@0 | 195 | |
michael@0 | 196 | makeStatsPublic: function() { |
michael@0 | 197 | let props = {}; |
michael@0 | 198 | this.forEach(function(stat) { |
michael@0 | 199 | props[stat.id] = { enumerable: true, configurable: false, |
michael@0 | 200 | writable: false, value: stat }; |
michael@0 | 201 | }); |
michael@0 | 202 | Object.defineProperties(this.__DOM_IMPL__.wrappedJSObject, props); |
michael@0 | 203 | }, |
michael@0 | 204 | |
michael@0 | 205 | forEach: function(cb, thisArg) { |
michael@0 | 206 | for (var key in this._report) { |
michael@0 | 207 | cb.call(thisArg || this._report, this.get(key), key, this._report); |
michael@0 | 208 | } |
michael@0 | 209 | }, |
michael@0 | 210 | |
michael@0 | 211 | get: function(key) { |
michael@0 | 212 | function publifyReadonly(win, obj) { |
michael@0 | 213 | let props = {}; |
michael@0 | 214 | for (let k in obj) { |
michael@0 | 215 | props[k] = {enumerable:true, configurable:false, writable:false, value:obj[k]}; |
michael@0 | 216 | } |
michael@0 | 217 | let pubobj = Cu.createObjectIn(win); |
michael@0 | 218 | Object.defineProperties(pubobj, props); |
michael@0 | 219 | return pubobj; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | // Return a content object rather than a wrapped chrome one. |
michael@0 | 223 | return publifyReadonly(this._win, this._report[key]); |
michael@0 | 224 | }, |
michael@0 | 225 | |
michael@0 | 226 | has: function(key) { |
michael@0 | 227 | return this._report[key] !== undefined; |
michael@0 | 228 | }, |
michael@0 | 229 | |
michael@0 | 230 | get mozPcid() { return this._pcid; } |
michael@0 | 231 | }; |
michael@0 | 232 | |
michael@0 | 233 | function RTCIdentityAssertion() {} |
michael@0 | 234 | RTCIdentityAssertion.prototype = { |
michael@0 | 235 | classDescription: "RTCIdentityAssertion", |
michael@0 | 236 | classID: PC_IDENTITY_CID, |
michael@0 | 237 | contractID: PC_IDENTITY_CONTRACT, |
michael@0 | 238 | QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, |
michael@0 | 239 | Ci.nsIDOMGlobalPropertyInitializer]), |
michael@0 | 240 | |
michael@0 | 241 | init: function(win) { this._win = win; }, |
michael@0 | 242 | |
michael@0 | 243 | __init: function(idp, name) { |
michael@0 | 244 | this.idp = idp; |
michael@0 | 245 | this.name = name; |
michael@0 | 246 | } |
michael@0 | 247 | }; |
michael@0 | 248 | |
michael@0 | 249 | function RTCPeerConnection() { |
michael@0 | 250 | this._queue = []; |
michael@0 | 251 | |
michael@0 | 252 | this._pc = null; |
michael@0 | 253 | this._observer = null; |
michael@0 | 254 | this._closed = false; |
michael@0 | 255 | |
michael@0 | 256 | this._onCreateOfferSuccess = null; |
michael@0 | 257 | this._onCreateOfferFailure = null; |
michael@0 | 258 | this._onCreateAnswerSuccess = null; |
michael@0 | 259 | this._onCreateAnswerFailure = null; |
michael@0 | 260 | this._onGetStatsSuccess = null; |
michael@0 | 261 | this._onGetStatsFailure = null; |
michael@0 | 262 | |
michael@0 | 263 | this._pendingType = null; |
michael@0 | 264 | this._localType = null; |
michael@0 | 265 | this._remoteType = null; |
michael@0 | 266 | this._trickleIce = false; |
michael@0 | 267 | this._peerIdentity = null; |
michael@0 | 268 | |
michael@0 | 269 | /** |
michael@0 | 270 | * Everytime we get a request from content, we put it in the queue. If there |
michael@0 | 271 | * are no pending operations though, we will execute it immediately. In |
michael@0 | 272 | * PeerConnectionObserver, whenever we are notified that an operation has |
michael@0 | 273 | * finished, we will check the queue for the next operation and execute if |
michael@0 | 274 | * neccesary. The _pending flag indicates whether an operation is currently in |
michael@0 | 275 | * progress. |
michael@0 | 276 | */ |
michael@0 | 277 | this._pending = false; |
michael@0 | 278 | |
michael@0 | 279 | // States |
michael@0 | 280 | this._iceGatheringState = this._iceConnectionState = "new"; |
michael@0 | 281 | } |
michael@0 | 282 | RTCPeerConnection.prototype = { |
michael@0 | 283 | classDescription: "mozRTCPeerConnection", |
michael@0 | 284 | classID: PC_CID, |
michael@0 | 285 | contractID: PC_CONTRACT, |
michael@0 | 286 | QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, |
michael@0 | 287 | Ci.nsIDOMGlobalPropertyInitializer]), |
michael@0 | 288 | init: function(win) { this._win = win; }, |
michael@0 | 289 | |
michael@0 | 290 | __init: function(rtcConfig) { |
michael@0 | 291 | this._trickleIce = Services.prefs.getBoolPref("media.peerconnection.trickle_ice"); |
michael@0 | 292 | if (!rtcConfig.iceServers || |
michael@0 | 293 | !Services.prefs.getBoolPref("media.peerconnection.use_document_iceservers")) { |
michael@0 | 294 | rtcConfig = {iceServers: |
michael@0 | 295 | JSON.parse(Services.prefs.getCharPref("media.peerconnection.default_iceservers"))}; |
michael@0 | 296 | } |
michael@0 | 297 | this._mustValidateRTCConfiguration(rtcConfig, |
michael@0 | 298 | "RTCPeerConnection constructor passed invalid RTCConfiguration"); |
michael@0 | 299 | if (_globalPCList._networkdown) { |
michael@0 | 300 | throw new this._win.DOMError("", |
michael@0 | 301 | "Can't create RTCPeerConnections when the network is down"); |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | this.makeGetterSetterEH("onaddstream"); |
michael@0 | 305 | this.makeGetterSetterEH("onicecandidate"); |
michael@0 | 306 | this.makeGetterSetterEH("onnegotiationneeded"); |
michael@0 | 307 | this.makeGetterSetterEH("onsignalingstatechange"); |
michael@0 | 308 | this.makeGetterSetterEH("onremovestream"); |
michael@0 | 309 | this.makeGetterSetterEH("ondatachannel"); |
michael@0 | 310 | this.makeGetterSetterEH("onconnection"); |
michael@0 | 311 | this.makeGetterSetterEH("onclosedconnection"); |
michael@0 | 312 | this.makeGetterSetterEH("oniceconnectionstatechange"); |
michael@0 | 313 | this.makeGetterSetterEH("onidentityresult"); |
michael@0 | 314 | this.makeGetterSetterEH("onpeeridentity"); |
michael@0 | 315 | this.makeGetterSetterEH("onidpassertionerror"); |
michael@0 | 316 | this.makeGetterSetterEH("onidpvalidationerror"); |
michael@0 | 317 | |
michael@0 | 318 | this._pc = new this._win.PeerConnectionImpl(); |
michael@0 | 319 | |
michael@0 | 320 | this.__DOM_IMPL__._innerObject = this; |
michael@0 | 321 | this._observer = new this._win.PeerConnectionObserver(this.__DOM_IMPL__); |
michael@0 | 322 | |
michael@0 | 323 | // Add a reference to the PeerConnection to global list (before init). |
michael@0 | 324 | this._winID = this._win.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 325 | .getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID; |
michael@0 | 326 | _globalPCList.addPC(this); |
michael@0 | 327 | |
michael@0 | 328 | this._queueOrRun({ |
michael@0 | 329 | func: this._initialize, |
michael@0 | 330 | args: [rtcConfig], |
michael@0 | 331 | // If not trickling, suppress start. |
michael@0 | 332 | wait: !this._trickleIce |
michael@0 | 333 | }); |
michael@0 | 334 | }, |
michael@0 | 335 | |
michael@0 | 336 | _initialize: function(rtcConfig) { |
michael@0 | 337 | this._impl.initialize(this._observer, this._win, rtcConfig, |
michael@0 | 338 | Services.tm.currentThread); |
michael@0 | 339 | this._initIdp(); |
michael@0 | 340 | }, |
michael@0 | 341 | |
michael@0 | 342 | get _impl() { |
michael@0 | 343 | if (!this._pc) { |
michael@0 | 344 | throw new this._win.DOMError("", |
michael@0 | 345 | "RTCPeerConnection is gone (did you enter Offline mode?)"); |
michael@0 | 346 | } |
michael@0 | 347 | return this._pc; |
michael@0 | 348 | }, |
michael@0 | 349 | |
michael@0 | 350 | _initIdp: function() { |
michael@0 | 351 | let prefName = "media.peerconnection.identity.timeout"; |
michael@0 | 352 | let idpTimeout = Services.prefs.getIntPref(prefName); |
michael@0 | 353 | let warningFunc = this.logWarning.bind(this); |
michael@0 | 354 | this._localIdp = new PeerConnectionIdp(this._win, idpTimeout, warningFunc, |
michael@0 | 355 | this.dispatchEvent.bind(this)); |
michael@0 | 356 | this._remoteIdp = new PeerConnectionIdp(this._win, idpTimeout, warningFunc, |
michael@0 | 357 | this.dispatchEvent.bind(this)); |
michael@0 | 358 | }, |
michael@0 | 359 | |
michael@0 | 360 | /** |
michael@0 | 361 | * Add a function to the queue or run it immediately if the queue is empty. |
michael@0 | 362 | * Argument is an object with the func, args and wait properties; wait should |
michael@0 | 363 | * be set to true if the function has a success/error callback that will call |
michael@0 | 364 | * _executeNext, false if it doesn't have a callback. |
michael@0 | 365 | */ |
michael@0 | 366 | _queueOrRun: function(obj) { |
michael@0 | 367 | this._checkClosed(); |
michael@0 | 368 | if (!this._pending) { |
michael@0 | 369 | if (obj.type !== undefined) { |
michael@0 | 370 | this._pendingType = obj.type; |
michael@0 | 371 | } |
michael@0 | 372 | obj.func.apply(this, obj.args); |
michael@0 | 373 | if (obj.wait) { |
michael@0 | 374 | this._pending = true; |
michael@0 | 375 | } |
michael@0 | 376 | } else { |
michael@0 | 377 | this._queue.push(obj); |
michael@0 | 378 | } |
michael@0 | 379 | }, |
michael@0 | 380 | |
michael@0 | 381 | // Pick the next item from the queue and run it. |
michael@0 | 382 | _executeNext: function() { |
michael@0 | 383 | if (this._queue.length) { |
michael@0 | 384 | let obj = this._queue.shift(); |
michael@0 | 385 | if (obj.type !== undefined) { |
michael@0 | 386 | this._pendingType = obj.type; |
michael@0 | 387 | } |
michael@0 | 388 | obj.func.apply(this, obj.args); |
michael@0 | 389 | if (!obj.wait) { |
michael@0 | 390 | this._executeNext(); |
michael@0 | 391 | } |
michael@0 | 392 | } else { |
michael@0 | 393 | this._pending = false; |
michael@0 | 394 | } |
michael@0 | 395 | }, |
michael@0 | 396 | |
michael@0 | 397 | /** |
michael@0 | 398 | * An RTCConfiguration looks like this: |
michael@0 | 399 | * |
michael@0 | 400 | * { "iceServers": [ { url:"stun:stun.example.org" }, |
michael@0 | 401 | * { url:"turn:turn.example.org", |
michael@0 | 402 | * username:"jib", credential:"mypass"} ] } |
michael@0 | 403 | * |
michael@0 | 404 | * WebIDL normalizes structure for us, so we test well-formed stun/turn urls, |
michael@0 | 405 | * but not validity of servers themselves, before passing along to C++. |
michael@0 | 406 | * ErrorMsg is passed in to detail which array-entry failed, if any. |
michael@0 | 407 | */ |
michael@0 | 408 | _mustValidateRTCConfiguration: function(rtcConfig, errorMsg) { |
michael@0 | 409 | var errorCtor = this._win.DOMError; |
michael@0 | 410 | function nicerNewURI(uriStr, errorMsg) { |
michael@0 | 411 | let ios = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService); |
michael@0 | 412 | try { |
michael@0 | 413 | return ios.newURI(uriStr, null, null); |
michael@0 | 414 | } catch (e if (e.result == Cr.NS_ERROR_MALFORMED_URI)) { |
michael@0 | 415 | throw new errorCtor("", errorMsg + " - malformed URI: " + uriStr); |
michael@0 | 416 | } |
michael@0 | 417 | } |
michael@0 | 418 | function mustValidateServer(server) { |
michael@0 | 419 | if (!server.url) { |
michael@0 | 420 | throw new errorCtor("", errorMsg + " - missing url"); |
michael@0 | 421 | } |
michael@0 | 422 | let url = nicerNewURI(server.url, errorMsg); |
michael@0 | 423 | if (url.scheme in { turn:1, turns:1 }) { |
michael@0 | 424 | if (!server.username) { |
michael@0 | 425 | throw new errorCtor("", errorMsg + " - missing username: " + server.url); |
michael@0 | 426 | } |
michael@0 | 427 | if (!server.credential) { |
michael@0 | 428 | throw new errorCtor("", errorMsg + " - missing credential: " + |
michael@0 | 429 | server.url); |
michael@0 | 430 | } |
michael@0 | 431 | } |
michael@0 | 432 | else if (!(url.scheme in { stun:1, stuns:1 })) { |
michael@0 | 433 | throw new errorCtor("", errorMsg + " - improper scheme: " + url.scheme); |
michael@0 | 434 | } |
michael@0 | 435 | } |
michael@0 | 436 | if (rtcConfig.iceServers) { |
michael@0 | 437 | let len = rtcConfig.iceServers.length; |
michael@0 | 438 | for (let i=0; i < len; i++) { |
michael@0 | 439 | mustValidateServer (rtcConfig.iceServers[i], errorMsg); |
michael@0 | 440 | } |
michael@0 | 441 | } |
michael@0 | 442 | }, |
michael@0 | 443 | |
michael@0 | 444 | /** |
michael@0 | 445 | * MediaConstraints look like this: |
michael@0 | 446 | * |
michael@0 | 447 | * { |
michael@0 | 448 | * mandatory: {"OfferToReceiveAudio": true, "OfferToReceiveVideo": true }, |
michael@0 | 449 | * optional: [{"VoiceActivityDetection": true}, {"FooBar": 10}] |
michael@0 | 450 | * } |
michael@0 | 451 | * |
michael@0 | 452 | * WebIDL normalizes the top structure for us, but the mandatory constraints |
michael@0 | 453 | * member comes in as a raw object so we can detect unknown constraints. We |
michael@0 | 454 | * compare its members against ones we support, and fail if not found. |
michael@0 | 455 | */ |
michael@0 | 456 | _mustValidateConstraints: function(constraints, errorMsg) { |
michael@0 | 457 | if (constraints.mandatory) { |
michael@0 | 458 | let supported; |
michael@0 | 459 | try { |
michael@0 | 460 | // Passing the raw constraints.mandatory here validates its structure |
michael@0 | 461 | supported = this._observer.getSupportedConstraints(constraints.mandatory); |
michael@0 | 462 | } catch (e) { |
michael@0 | 463 | throw new this._win.DOMError("", errorMsg + " - " + e.message); |
michael@0 | 464 | } |
michael@0 | 465 | |
michael@0 | 466 | for (let constraint of Object.keys(constraints.mandatory)) { |
michael@0 | 467 | if (!(constraint in supported)) { |
michael@0 | 468 | throw new this._win.DOMError("", |
michael@0 | 469 | errorMsg + " - unknown mandatory constraint: " + constraint); |
michael@0 | 470 | } |
michael@0 | 471 | } |
michael@0 | 472 | } |
michael@0 | 473 | if (constraints.optional) { |
michael@0 | 474 | let len = constraints.optional.length; |
michael@0 | 475 | for (let i = 0; i < len; i++) { |
michael@0 | 476 | let constraints_per_entry = 0; |
michael@0 | 477 | for (let constraint in Object.keys(constraints.optional[i])) { |
michael@0 | 478 | if (constraints_per_entry) { |
michael@0 | 479 | throw new this._win.DOMError("", errorMsg + |
michael@0 | 480 | " - optional constraint must be single key/value pair"); |
michael@0 | 481 | } |
michael@0 | 482 | constraints_per_entry += 1; |
michael@0 | 483 | } |
michael@0 | 484 | } |
michael@0 | 485 | } |
michael@0 | 486 | }, |
michael@0 | 487 | |
michael@0 | 488 | // Ideally, this should be of the form _checkState(state), |
michael@0 | 489 | // where the state is taken from an enumeration containing |
michael@0 | 490 | // the valid peer connection states defined in the WebRTC |
michael@0 | 491 | // spec. See Bug 831756. |
michael@0 | 492 | _checkClosed: function() { |
michael@0 | 493 | if (this._closed) { |
michael@0 | 494 | throw new this._win.DOMError("", "Peer connection is closed"); |
michael@0 | 495 | } |
michael@0 | 496 | }, |
michael@0 | 497 | |
michael@0 | 498 | dispatchEvent: function(event) { |
michael@0 | 499 | this.__DOM_IMPL__.dispatchEvent(event); |
michael@0 | 500 | }, |
michael@0 | 501 | |
michael@0 | 502 | // Log error message to web console and window.onerror, if present. |
michael@0 | 503 | logErrorAndCallOnError: function(msg, file, line) { |
michael@0 | 504 | this.logMsg(msg, file, line, Ci.nsIScriptError.exceptionFlag); |
michael@0 | 505 | |
michael@0 | 506 | // Safely call onerror directly if present (necessary for testing) |
michael@0 | 507 | try { |
michael@0 | 508 | if (typeof this._win.onerror === "function") { |
michael@0 | 509 | this._win.onerror(msg, file, line); |
michael@0 | 510 | } |
michael@0 | 511 | } catch(e) { |
michael@0 | 512 | // If onerror itself throws, service it. |
michael@0 | 513 | try { |
michael@0 | 514 | this.logError(e.message, e.fileName, e.lineNumber); |
michael@0 | 515 | } catch(e) {} |
michael@0 | 516 | } |
michael@0 | 517 | }, |
michael@0 | 518 | |
michael@0 | 519 | logError: function(msg, file, line) { |
michael@0 | 520 | this.logMsg(msg, file, line, Ci.nsIScriptError.errorFlag); |
michael@0 | 521 | }, |
michael@0 | 522 | |
michael@0 | 523 | logWarning: function(msg, file, line) { |
michael@0 | 524 | this.logMsg(msg, file, line, Ci.nsIScriptError.warningFlag); |
michael@0 | 525 | }, |
michael@0 | 526 | |
michael@0 | 527 | logMsg: function(msg, file, line, flag) { |
michael@0 | 528 | let scriptErrorClass = Cc["@mozilla.org/scripterror;1"]; |
michael@0 | 529 | let scriptError = scriptErrorClass.createInstance(Ci.nsIScriptError); |
michael@0 | 530 | scriptError.initWithWindowID(msg, file, null, line, 0, flag, |
michael@0 | 531 | "content javascript", this._winID); |
michael@0 | 532 | let console = Cc["@mozilla.org/consoleservice;1"]. |
michael@0 | 533 | getService(Ci.nsIConsoleService); |
michael@0 | 534 | console.logMessage(scriptError); |
michael@0 | 535 | }, |
michael@0 | 536 | |
michael@0 | 537 | getEH: function(type) { |
michael@0 | 538 | return this.__DOM_IMPL__.getEventHandler(type); |
michael@0 | 539 | }, |
michael@0 | 540 | |
michael@0 | 541 | setEH: function(type, handler) { |
michael@0 | 542 | this.__DOM_IMPL__.setEventHandler(type, handler); |
michael@0 | 543 | }, |
michael@0 | 544 | |
michael@0 | 545 | makeGetterSetterEH: function(name) { |
michael@0 | 546 | Object.defineProperty(this, name, |
michael@0 | 547 | { |
michael@0 | 548 | get:function() { return this.getEH(name); }, |
michael@0 | 549 | set:function(h) { return this.setEH(name, h); } |
michael@0 | 550 | }); |
michael@0 | 551 | }, |
michael@0 | 552 | |
michael@0 | 553 | createOffer: function(onSuccess, onError, constraints) { |
michael@0 | 554 | if (!constraints) { |
michael@0 | 555 | constraints = {}; |
michael@0 | 556 | } |
michael@0 | 557 | this._mustValidateConstraints(constraints, "createOffer passed invalid constraints"); |
michael@0 | 558 | |
michael@0 | 559 | this._queueOrRun({ |
michael@0 | 560 | func: this._createOffer, |
michael@0 | 561 | args: [onSuccess, onError, constraints], |
michael@0 | 562 | wait: true |
michael@0 | 563 | }); |
michael@0 | 564 | }, |
michael@0 | 565 | |
michael@0 | 566 | _createOffer: function(onSuccess, onError, constraints) { |
michael@0 | 567 | this._onCreateOfferSuccess = onSuccess; |
michael@0 | 568 | this._onCreateOfferFailure = onError; |
michael@0 | 569 | this._impl.createOffer(constraints); |
michael@0 | 570 | }, |
michael@0 | 571 | |
michael@0 | 572 | _createAnswer: function(onSuccess, onError, constraints, provisional) { |
michael@0 | 573 | this._onCreateAnswerSuccess = onSuccess; |
michael@0 | 574 | this._onCreateAnswerFailure = onError; |
michael@0 | 575 | |
michael@0 | 576 | if (!this.remoteDescription) { |
michael@0 | 577 | |
michael@0 | 578 | this._observer.onCreateAnswerError(Ci.IPeerConnection.kInvalidState, |
michael@0 | 579 | "setRemoteDescription not called"); |
michael@0 | 580 | return; |
michael@0 | 581 | } |
michael@0 | 582 | |
michael@0 | 583 | if (this.remoteDescription.type != "offer") { |
michael@0 | 584 | |
michael@0 | 585 | this._observer.onCreateAnswerError(Ci.IPeerConnection.kInvalidState, |
michael@0 | 586 | "No outstanding offer"); |
michael@0 | 587 | return; |
michael@0 | 588 | } |
michael@0 | 589 | |
michael@0 | 590 | // TODO: Implement provisional answer. |
michael@0 | 591 | |
michael@0 | 592 | this._impl.createAnswer(constraints); |
michael@0 | 593 | }, |
michael@0 | 594 | |
michael@0 | 595 | createAnswer: function(onSuccess, onError, constraints, provisional) { |
michael@0 | 596 | if (!constraints) { |
michael@0 | 597 | constraints = {}; |
michael@0 | 598 | } |
michael@0 | 599 | |
michael@0 | 600 | this._mustValidateConstraints(constraints, "createAnswer passed invalid constraints"); |
michael@0 | 601 | |
michael@0 | 602 | if (!provisional) { |
michael@0 | 603 | provisional = false; |
michael@0 | 604 | } |
michael@0 | 605 | |
michael@0 | 606 | this._queueOrRun({ |
michael@0 | 607 | func: this._createAnswer, |
michael@0 | 608 | args: [onSuccess, onError, constraints, provisional], |
michael@0 | 609 | wait: true |
michael@0 | 610 | }); |
michael@0 | 611 | }, |
michael@0 | 612 | |
michael@0 | 613 | setLocalDescription: function(desc, onSuccess, onError) { |
michael@0 | 614 | let type; |
michael@0 | 615 | switch (desc.type) { |
michael@0 | 616 | case "offer": |
michael@0 | 617 | type = Ci.IPeerConnection.kActionOffer; |
michael@0 | 618 | break; |
michael@0 | 619 | case "answer": |
michael@0 | 620 | type = Ci.IPeerConnection.kActionAnswer; |
michael@0 | 621 | break; |
michael@0 | 622 | case "pranswer": |
michael@0 | 623 | throw new this._win.DOMError("", "pranswer not yet implemented"); |
michael@0 | 624 | default: |
michael@0 | 625 | throw new this._win.DOMError("", |
michael@0 | 626 | "Invalid type " + desc.type + " provided to setLocalDescription"); |
michael@0 | 627 | } |
michael@0 | 628 | |
michael@0 | 629 | this._queueOrRun({ |
michael@0 | 630 | func: this._setLocalDescription, |
michael@0 | 631 | args: [type, desc.sdp, onSuccess, onError], |
michael@0 | 632 | wait: true, |
michael@0 | 633 | type: desc.type |
michael@0 | 634 | }); |
michael@0 | 635 | }, |
michael@0 | 636 | |
michael@0 | 637 | _setLocalDescription: function(type, sdp, onSuccess, onError) { |
michael@0 | 638 | this._onSetLocalDescriptionSuccess = onSuccess; |
michael@0 | 639 | this._onSetLocalDescriptionFailure = onError; |
michael@0 | 640 | this._impl.setLocalDescription(type, sdp); |
michael@0 | 641 | }, |
michael@0 | 642 | |
michael@0 | 643 | setRemoteDescription: function(desc, onSuccess, onError) { |
michael@0 | 644 | let type; |
michael@0 | 645 | switch (desc.type) { |
michael@0 | 646 | case "offer": |
michael@0 | 647 | type = Ci.IPeerConnection.kActionOffer; |
michael@0 | 648 | break; |
michael@0 | 649 | case "answer": |
michael@0 | 650 | type = Ci.IPeerConnection.kActionAnswer; |
michael@0 | 651 | break; |
michael@0 | 652 | case "pranswer": |
michael@0 | 653 | throw new this._win.DOMError("", "pranswer not yet implemented"); |
michael@0 | 654 | default: |
michael@0 | 655 | throw new this._win.DOMError("", |
michael@0 | 656 | "Invalid type " + desc.type + " provided to setRemoteDescription"); |
michael@0 | 657 | } |
michael@0 | 658 | |
michael@0 | 659 | try { |
michael@0 | 660 | let processIdentity = this._processIdentity.bind(this); |
michael@0 | 661 | this._remoteIdp.verifyIdentityFromSDP(desc.sdp, processIdentity); |
michael@0 | 662 | } catch (e) { |
michael@0 | 663 | this.logWarning(e.message, e.fileName, e.lineNumber); |
michael@0 | 664 | // only happens if processing the SDP for identity doesn't work |
michael@0 | 665 | // let _setRemoteDescription do the error reporting |
michael@0 | 666 | } |
michael@0 | 667 | |
michael@0 | 668 | this._queueOrRun({ |
michael@0 | 669 | func: this._setRemoteDescription, |
michael@0 | 670 | args: [type, desc.sdp, onSuccess, onError], |
michael@0 | 671 | wait: true, |
michael@0 | 672 | type: desc.type |
michael@0 | 673 | }); |
michael@0 | 674 | }, |
michael@0 | 675 | |
michael@0 | 676 | _processIdentity: function(message) { |
michael@0 | 677 | if (message) { |
michael@0 | 678 | this._peerIdentity = new this._win.RTCIdentityAssertion( |
michael@0 | 679 | this._remoteIdp.provider, message.identity); |
michael@0 | 680 | |
michael@0 | 681 | let args = { peerIdentity: this._peerIdentity }; |
michael@0 | 682 | this.dispatchEvent(new this._win.Event("peeridentity")); |
michael@0 | 683 | } |
michael@0 | 684 | }, |
michael@0 | 685 | |
michael@0 | 686 | _setRemoteDescription: function(type, sdp, onSuccess, onError) { |
michael@0 | 687 | this._onSetRemoteDescriptionSuccess = onSuccess; |
michael@0 | 688 | this._onSetRemoteDescriptionFailure = onError; |
michael@0 | 689 | this._impl.setRemoteDescription(type, sdp); |
michael@0 | 690 | }, |
michael@0 | 691 | |
michael@0 | 692 | setIdentityProvider: function(provider, protocol, username) { |
michael@0 | 693 | this._checkClosed(); |
michael@0 | 694 | this._localIdp.setIdentityProvider(provider, protocol, username); |
michael@0 | 695 | }, |
michael@0 | 696 | |
michael@0 | 697 | _gotIdentityAssertion: function(assertion){ |
michael@0 | 698 | let args = { assertion: assertion }; |
michael@0 | 699 | let ev = new this._win.RTCPeerConnectionIdentityEvent("identityresult", args); |
michael@0 | 700 | this.dispatchEvent(ev); |
michael@0 | 701 | }, |
michael@0 | 702 | |
michael@0 | 703 | getIdentityAssertion: function() { |
michael@0 | 704 | this._checkClosed(); |
michael@0 | 705 | |
michael@0 | 706 | function gotAssertion(assertion) { |
michael@0 | 707 | if (assertion) { |
michael@0 | 708 | this._gotIdentityAssertion(assertion); |
michael@0 | 709 | } |
michael@0 | 710 | } |
michael@0 | 711 | |
michael@0 | 712 | this._localIdp.getIdentityAssertion(this._impl.fingerprint, |
michael@0 | 713 | gotAssertion.bind(this)); |
michael@0 | 714 | }, |
michael@0 | 715 | |
michael@0 | 716 | updateIce: function(config, constraints) { |
michael@0 | 717 | throw new this._win.DOMError("", "updateIce not yet implemented"); |
michael@0 | 718 | }, |
michael@0 | 719 | |
michael@0 | 720 | addIceCandidate: function(cand, onSuccess, onError) { |
michael@0 | 721 | if (!cand.candidate && !cand.sdpMLineIndex) { |
michael@0 | 722 | throw new this._win.DOMError("", |
michael@0 | 723 | "Invalid candidate passed to addIceCandidate!"); |
michael@0 | 724 | } |
michael@0 | 725 | this._onAddIceCandidateSuccess = onSuccess || null; |
michael@0 | 726 | this._onAddIceCandidateError = onError || null; |
michael@0 | 727 | |
michael@0 | 728 | this._queueOrRun({ func: this._addIceCandidate, args: [cand], wait: true }); |
michael@0 | 729 | }, |
michael@0 | 730 | |
michael@0 | 731 | _addIceCandidate: function(cand) { |
michael@0 | 732 | this._impl.addIceCandidate(cand.candidate, cand.sdpMid || "", |
michael@0 | 733 | (cand.sdpMLineIndex === null) ? 0 : |
michael@0 | 734 | cand.sdpMLineIndex + 1); |
michael@0 | 735 | }, |
michael@0 | 736 | |
michael@0 | 737 | addStream: function(stream, constraints) { |
michael@0 | 738 | if (!constraints) { |
michael@0 | 739 | constraints = {}; |
michael@0 | 740 | } |
michael@0 | 741 | this._mustValidateConstraints(constraints, |
michael@0 | 742 | "addStream passed invalid constraints"); |
michael@0 | 743 | if (stream.currentTime === undefined) { |
michael@0 | 744 | throw new this._win.DOMError("", "Invalid stream passed to addStream!"); |
michael@0 | 745 | } |
michael@0 | 746 | this._queueOrRun({ func: this._addStream, |
michael@0 | 747 | args: [stream, constraints], |
michael@0 | 748 | wait: false }); |
michael@0 | 749 | }, |
michael@0 | 750 | |
michael@0 | 751 | _addStream: function(stream, constraints) { |
michael@0 | 752 | this._impl.addStream(stream, constraints); |
michael@0 | 753 | }, |
michael@0 | 754 | |
michael@0 | 755 | removeStream: function(stream) { |
michael@0 | 756 | // Bug 844295: Not implementing this functionality. |
michael@0 | 757 | throw new this._win.DOMError("", "removeStream not yet implemented"); |
michael@0 | 758 | }, |
michael@0 | 759 | |
michael@0 | 760 | getStreamById: function(id) { |
michael@0 | 761 | throw new this._win.DOMError("", "getStreamById not yet implemented"); |
michael@0 | 762 | }, |
michael@0 | 763 | |
michael@0 | 764 | close: function() { |
michael@0 | 765 | if (this._closed) { |
michael@0 | 766 | return; |
michael@0 | 767 | } |
michael@0 | 768 | this._queueOrRun({ func: this._close, args: [false], wait: false }); |
michael@0 | 769 | this._closed = true; |
michael@0 | 770 | this.changeIceConnectionState("closed"); |
michael@0 | 771 | }, |
michael@0 | 772 | |
michael@0 | 773 | _close: function() { |
michael@0 | 774 | this._localIdp.close(); |
michael@0 | 775 | this._remoteIdp.close(); |
michael@0 | 776 | this._impl.close(); |
michael@0 | 777 | }, |
michael@0 | 778 | |
michael@0 | 779 | getLocalStreams: function() { |
michael@0 | 780 | this._checkClosed(); |
michael@0 | 781 | return this._impl.getLocalStreams(); |
michael@0 | 782 | }, |
michael@0 | 783 | |
michael@0 | 784 | getRemoteStreams: function() { |
michael@0 | 785 | this._checkClosed(); |
michael@0 | 786 | return this._impl.getRemoteStreams(); |
michael@0 | 787 | }, |
michael@0 | 788 | |
michael@0 | 789 | get localDescription() { |
michael@0 | 790 | this._checkClosed(); |
michael@0 | 791 | let sdp = this._impl.localDescription; |
michael@0 | 792 | if (sdp.length == 0) { |
michael@0 | 793 | return null; |
michael@0 | 794 | } |
michael@0 | 795 | |
michael@0 | 796 | sdp = this._localIdp.wrapSdp(sdp); |
michael@0 | 797 | return new this._win.mozRTCSessionDescription({ type: this._localType, |
michael@0 | 798 | sdp: sdp }); |
michael@0 | 799 | }, |
michael@0 | 800 | |
michael@0 | 801 | get remoteDescription() { |
michael@0 | 802 | this._checkClosed(); |
michael@0 | 803 | let sdp = this._impl.remoteDescription; |
michael@0 | 804 | if (sdp.length == 0) { |
michael@0 | 805 | return null; |
michael@0 | 806 | } |
michael@0 | 807 | return new this._win.mozRTCSessionDescription({ type: this._remoteType, |
michael@0 | 808 | sdp: sdp }); |
michael@0 | 809 | }, |
michael@0 | 810 | |
michael@0 | 811 | get peerIdentity() { return this._peerIdentity; }, |
michael@0 | 812 | get iceGatheringState() { return this._iceGatheringState; }, |
michael@0 | 813 | get iceConnectionState() { return this._iceConnectionState; }, |
michael@0 | 814 | |
michael@0 | 815 | get signalingState() { |
michael@0 | 816 | // checking for our local pc closed indication |
michael@0 | 817 | // before invoking the pc methods. |
michael@0 | 818 | if (this._closed) { |
michael@0 | 819 | return "closed"; |
michael@0 | 820 | } |
michael@0 | 821 | return { |
michael@0 | 822 | "SignalingInvalid": "", |
michael@0 | 823 | "SignalingStable": "stable", |
michael@0 | 824 | "SignalingHaveLocalOffer": "have-local-offer", |
michael@0 | 825 | "SignalingHaveRemoteOffer": "have-remote-offer", |
michael@0 | 826 | "SignalingHaveLocalPranswer": "have-local-pranswer", |
michael@0 | 827 | "SignalingHaveRemotePranswer": "have-remote-pranswer", |
michael@0 | 828 | "SignalingClosed": "closed" |
michael@0 | 829 | }[this._impl.signalingState]; |
michael@0 | 830 | }, |
michael@0 | 831 | |
michael@0 | 832 | changeIceGatheringState: function(state) { |
michael@0 | 833 | this._iceGatheringState = state; |
michael@0 | 834 | }, |
michael@0 | 835 | |
michael@0 | 836 | changeIceConnectionState: function(state) { |
michael@0 | 837 | this._iceConnectionState = state; |
michael@0 | 838 | this.dispatchEvent(new this._win.Event("iceconnectionstatechange")); |
michael@0 | 839 | }, |
michael@0 | 840 | |
michael@0 | 841 | getStats: function(selector, onSuccess, onError) { |
michael@0 | 842 | this._queueOrRun({ |
michael@0 | 843 | func: this._getStats, |
michael@0 | 844 | args: [selector, onSuccess, onError], |
michael@0 | 845 | wait: true |
michael@0 | 846 | }); |
michael@0 | 847 | }, |
michael@0 | 848 | |
michael@0 | 849 | _getStats: function(selector, onSuccess, onError) { |
michael@0 | 850 | this._onGetStatsSuccess = onSuccess; |
michael@0 | 851 | this._onGetStatsFailure = onError; |
michael@0 | 852 | |
michael@0 | 853 | this._impl.getStats(selector); |
michael@0 | 854 | }, |
michael@0 | 855 | |
michael@0 | 856 | createDataChannel: function(label, dict) { |
michael@0 | 857 | this._checkClosed(); |
michael@0 | 858 | if (dict == undefined) { |
michael@0 | 859 | dict = {}; |
michael@0 | 860 | } |
michael@0 | 861 | if (dict.maxRetransmitNum != undefined) { |
michael@0 | 862 | dict.maxRetransmits = dict.maxRetransmitNum; |
michael@0 | 863 | this.logWarning("Deprecated RTCDataChannelInit dictionary entry maxRetransmitNum used!", null, 0); |
michael@0 | 864 | } |
michael@0 | 865 | if (dict.outOfOrderAllowed != undefined) { |
michael@0 | 866 | dict.ordered = !dict.outOfOrderAllowed; // the meaning is swapped with |
michael@0 | 867 | // the name change |
michael@0 | 868 | this.logWarning("Deprecated RTCDataChannelInit dictionary entry outOfOrderAllowed used!", null, 0); |
michael@0 | 869 | } |
michael@0 | 870 | if (dict.preset != undefined) { |
michael@0 | 871 | dict.negotiated = dict.preset; |
michael@0 | 872 | this.logWarning("Deprecated RTCDataChannelInit dictionary entry preset used!", null, 0); |
michael@0 | 873 | } |
michael@0 | 874 | if (dict.stream != undefined) { |
michael@0 | 875 | dict.id = dict.stream; |
michael@0 | 876 | this.logWarning("Deprecated RTCDataChannelInit dictionary entry stream used!", null, 0); |
michael@0 | 877 | } |
michael@0 | 878 | |
michael@0 | 879 | if (dict.maxRetransmitTime != undefined && |
michael@0 | 880 | dict.maxRetransmits != undefined) { |
michael@0 | 881 | throw new this._win.DOMError("", |
michael@0 | 882 | "Both maxRetransmitTime and maxRetransmits cannot be provided"); |
michael@0 | 883 | } |
michael@0 | 884 | let protocol; |
michael@0 | 885 | if (dict.protocol == undefined) { |
michael@0 | 886 | protocol = ""; |
michael@0 | 887 | } else { |
michael@0 | 888 | protocol = dict.protocol; |
michael@0 | 889 | } |
michael@0 | 890 | |
michael@0 | 891 | // Must determine the type where we still know if entries are undefined. |
michael@0 | 892 | let type; |
michael@0 | 893 | if (dict.maxRetransmitTime != undefined) { |
michael@0 | 894 | type = Ci.IPeerConnection.kDataChannelPartialReliableTimed; |
michael@0 | 895 | } else if (dict.maxRetransmits != undefined) { |
michael@0 | 896 | type = Ci.IPeerConnection.kDataChannelPartialReliableRexmit; |
michael@0 | 897 | } else { |
michael@0 | 898 | type = Ci.IPeerConnection.kDataChannelReliable; |
michael@0 | 899 | } |
michael@0 | 900 | |
michael@0 | 901 | // Synchronous since it doesn't block. |
michael@0 | 902 | let channel = this._impl.createDataChannel( |
michael@0 | 903 | label, protocol, type, !dict.ordered, dict.maxRetransmitTime, |
michael@0 | 904 | dict.maxRetransmits, dict.negotiated ? true : false, |
michael@0 | 905 | dict.id != undefined ? dict.id : 0xFFFF |
michael@0 | 906 | ); |
michael@0 | 907 | return channel; |
michael@0 | 908 | }, |
michael@0 | 909 | |
michael@0 | 910 | connectDataConnection: function(localport, remoteport, numstreams) { |
michael@0 | 911 | if (numstreams == undefined || numstreams <= 0) { |
michael@0 | 912 | numstreams = 16; |
michael@0 | 913 | } |
michael@0 | 914 | this._queueOrRun({ |
michael@0 | 915 | func: this._connectDataConnection, |
michael@0 | 916 | args: [localport, remoteport, numstreams], |
michael@0 | 917 | wait: false |
michael@0 | 918 | }); |
michael@0 | 919 | }, |
michael@0 | 920 | |
michael@0 | 921 | _connectDataConnection: function(localport, remoteport, numstreams) { |
michael@0 | 922 | this._impl.connectDataConnection(localport, remoteport, numstreams); |
michael@0 | 923 | } |
michael@0 | 924 | }; |
michael@0 | 925 | |
michael@0 | 926 | function RTCError(code, message) { |
michael@0 | 927 | this.name = this.reasonName[Math.min(code, this.reasonName.length - 1)]; |
michael@0 | 928 | this.message = (typeof message === "string")? message : this.name; |
michael@0 | 929 | this.__exposedProps__ = { name: "rw", message: "rw" }; |
michael@0 | 930 | } |
michael@0 | 931 | RTCError.prototype = { |
michael@0 | 932 | // These strings must match those defined in the WebRTC spec. |
michael@0 | 933 | reasonName: [ |
michael@0 | 934 | "NO_ERROR", // Should never happen -- only used for testing |
michael@0 | 935 | "INVALID_CONSTRAINTS_TYPE", |
michael@0 | 936 | "INVALID_CANDIDATE_TYPE", |
michael@0 | 937 | "INVALID_MEDIASTREAM_TRACK", |
michael@0 | 938 | "INVALID_STATE", |
michael@0 | 939 | "INVALID_SESSION_DESCRIPTION", |
michael@0 | 940 | "INCOMPATIBLE_SESSION_DESCRIPTION", |
michael@0 | 941 | "INCOMPATIBLE_CONSTRAINTS", |
michael@0 | 942 | "INCOMPATIBLE_MEDIASTREAMTRACK", |
michael@0 | 943 | "INTERNAL_ERROR" |
michael@0 | 944 | ] |
michael@0 | 945 | }; |
michael@0 | 946 | |
michael@0 | 947 | // This is a separate object because we don't want to expose it to DOM. |
michael@0 | 948 | function PeerConnectionObserver() { |
michael@0 | 949 | this._dompc = null; |
michael@0 | 950 | } |
michael@0 | 951 | PeerConnectionObserver.prototype = { |
michael@0 | 952 | classDescription: "PeerConnectionObserver", |
michael@0 | 953 | classID: PC_OBS_CID, |
michael@0 | 954 | contractID: PC_OBS_CONTRACT, |
michael@0 | 955 | QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, |
michael@0 | 956 | Ci.nsIDOMGlobalPropertyInitializer]), |
michael@0 | 957 | init: function(win) { this._win = win; }, |
michael@0 | 958 | |
michael@0 | 959 | __init: function(dompc) { |
michael@0 | 960 | this._dompc = dompc._innerObject; |
michael@0 | 961 | }, |
michael@0 | 962 | |
michael@0 | 963 | dispatchEvent: function(event) { |
michael@0 | 964 | this._dompc.dispatchEvent(event); |
michael@0 | 965 | }, |
michael@0 | 966 | |
michael@0 | 967 | callCB: function(callback, arg) { |
michael@0 | 968 | if (callback) { |
michael@0 | 969 | try { |
michael@0 | 970 | callback(arg); |
michael@0 | 971 | } catch(e) { |
michael@0 | 972 | // A content script (user-provided) callback threw an error. We don't |
michael@0 | 973 | // want this to take down peerconnection, but we still want the user |
michael@0 | 974 | // to see it, so we catch it, report it, and move on. |
michael@0 | 975 | this._dompc.logErrorAndCallOnError(e.message, |
michael@0 | 976 | e.fileName, |
michael@0 | 977 | e.lineNumber); |
michael@0 | 978 | } |
michael@0 | 979 | } |
michael@0 | 980 | }, |
michael@0 | 981 | |
michael@0 | 982 | onCreateOfferSuccess: function(sdp) { |
michael@0 | 983 | let pc = this._dompc; |
michael@0 | 984 | let fp = pc._impl.fingerprint; |
michael@0 | 985 | pc._localIdp.appendIdentityToSDP(sdp, fp, function(sdp, assertion) { |
michael@0 | 986 | if (assertion) { |
michael@0 | 987 | pc._gotIdentityAssertion(assertion); |
michael@0 | 988 | } |
michael@0 | 989 | this.callCB(pc._onCreateOfferSuccess, |
michael@0 | 990 | new pc._win.mozRTCSessionDescription({ type: "offer", |
michael@0 | 991 | sdp: sdp })); |
michael@0 | 992 | pc._executeNext(); |
michael@0 | 993 | }.bind(this)); |
michael@0 | 994 | }, |
michael@0 | 995 | |
michael@0 | 996 | onCreateOfferError: function(code, message) { |
michael@0 | 997 | this.callCB(this._dompc._onCreateOfferFailure, new RTCError(code, message)); |
michael@0 | 998 | this._dompc._executeNext(); |
michael@0 | 999 | }, |
michael@0 | 1000 | |
michael@0 | 1001 | onCreateAnswerSuccess: function(sdp) { |
michael@0 | 1002 | let pc = this._dompc; |
michael@0 | 1003 | let fp = pc._impl.fingerprint; |
michael@0 | 1004 | pc._localIdp.appendIdentityToSDP(sdp, fp, function(sdp, assertion) { |
michael@0 | 1005 | if (assertion) { |
michael@0 | 1006 | pc._gotIdentityAssertion(assertion); |
michael@0 | 1007 | } |
michael@0 | 1008 | this.callCB (pc._onCreateAnswerSuccess, |
michael@0 | 1009 | new pc._win.mozRTCSessionDescription({ type: "answer", |
michael@0 | 1010 | sdp: sdp })); |
michael@0 | 1011 | pc._executeNext(); |
michael@0 | 1012 | }.bind(this)); |
michael@0 | 1013 | }, |
michael@0 | 1014 | |
michael@0 | 1015 | onCreateAnswerError: function(code, message) { |
michael@0 | 1016 | this.callCB(this._dompc._onCreateAnswerFailure, new RTCError(code, message)); |
michael@0 | 1017 | this._dompc._executeNext(); |
michael@0 | 1018 | }, |
michael@0 | 1019 | |
michael@0 | 1020 | onSetLocalDescriptionSuccess: function() { |
michael@0 | 1021 | this._dompc._localType = this._dompc._pendingType; |
michael@0 | 1022 | this._dompc._pendingType = null; |
michael@0 | 1023 | this.callCB(this._dompc._onSetLocalDescriptionSuccess); |
michael@0 | 1024 | |
michael@0 | 1025 | if (this._dompc._iceGatheringState == "complete") { |
michael@0 | 1026 | // If we are not trickling or we completed gathering prior |
michael@0 | 1027 | // to setLocal, then trigger a call of onicecandidate here. |
michael@0 | 1028 | this.foundIceCandidate(null); |
michael@0 | 1029 | } |
michael@0 | 1030 | |
michael@0 | 1031 | this._dompc._executeNext(); |
michael@0 | 1032 | }, |
michael@0 | 1033 | |
michael@0 | 1034 | onSetRemoteDescriptionSuccess: function() { |
michael@0 | 1035 | this._dompc._remoteType = this._dompc._pendingType; |
michael@0 | 1036 | this._dompc._pendingType = null; |
michael@0 | 1037 | this.callCB(this._dompc._onSetRemoteDescriptionSuccess); |
michael@0 | 1038 | this._dompc._executeNext(); |
michael@0 | 1039 | }, |
michael@0 | 1040 | |
michael@0 | 1041 | onSetLocalDescriptionError: function(code, message) { |
michael@0 | 1042 | this._dompc._pendingType = null; |
michael@0 | 1043 | this.callCB(this._dompc._onSetLocalDescriptionFailure, |
michael@0 | 1044 | new RTCError(code, message)); |
michael@0 | 1045 | this._dompc._executeNext(); |
michael@0 | 1046 | }, |
michael@0 | 1047 | |
michael@0 | 1048 | onSetRemoteDescriptionError: function(code, message) { |
michael@0 | 1049 | this._dompc._pendingType = null; |
michael@0 | 1050 | this.callCB(this._dompc._onSetRemoteDescriptionFailure, |
michael@0 | 1051 | new RTCError(code, message)); |
michael@0 | 1052 | this._dompc._executeNext(); |
michael@0 | 1053 | }, |
michael@0 | 1054 | |
michael@0 | 1055 | onAddIceCandidateSuccess: function() { |
michael@0 | 1056 | this._dompc._pendingType = null; |
michael@0 | 1057 | this.callCB(this._dompc._onAddIceCandidateSuccess); |
michael@0 | 1058 | this._dompc._executeNext(); |
michael@0 | 1059 | }, |
michael@0 | 1060 | |
michael@0 | 1061 | onAddIceCandidateError: function(code, message) { |
michael@0 | 1062 | this._dompc._pendingType = null; |
michael@0 | 1063 | this.callCB(this._dompc._onAddIceCandidateError, new RTCError(code, message)); |
michael@0 | 1064 | this._dompc._executeNext(); |
michael@0 | 1065 | }, |
michael@0 | 1066 | |
michael@0 | 1067 | onIceCandidate: function(level, mid, candidate) { |
michael@0 | 1068 | this.foundIceCandidate(new this._dompc._win.mozRTCIceCandidate( |
michael@0 | 1069 | { |
michael@0 | 1070 | candidate: candidate, |
michael@0 | 1071 | sdpMid: mid, |
michael@0 | 1072 | sdpMLineIndex: level - 1 |
michael@0 | 1073 | } |
michael@0 | 1074 | )); |
michael@0 | 1075 | }, |
michael@0 | 1076 | |
michael@0 | 1077 | |
michael@0 | 1078 | // This method is primarily responsible for updating iceConnectionState. |
michael@0 | 1079 | // This state is defined in the WebRTC specification as follows: |
michael@0 | 1080 | // |
michael@0 | 1081 | // iceConnectionState: |
michael@0 | 1082 | // ------------------- |
michael@0 | 1083 | // new The ICE Agent is gathering addresses and/or waiting for |
michael@0 | 1084 | // remote candidates to be supplied. |
michael@0 | 1085 | // |
michael@0 | 1086 | // checking The ICE Agent has received remote candidates on at least |
michael@0 | 1087 | // one component, and is checking candidate pairs but has not |
michael@0 | 1088 | // yet found a connection. In addition to checking, it may |
michael@0 | 1089 | // also still be gathering. |
michael@0 | 1090 | // |
michael@0 | 1091 | // connected The ICE Agent has found a usable connection for all |
michael@0 | 1092 | // components but is still checking other candidate pairs to |
michael@0 | 1093 | // see if there is a better connection. It may also still be |
michael@0 | 1094 | // gathering. |
michael@0 | 1095 | // |
michael@0 | 1096 | // completed The ICE Agent has finished gathering and checking and found |
michael@0 | 1097 | // a connection for all components. Open issue: it is not |
michael@0 | 1098 | // clear how the non controlling ICE side knows it is in the |
michael@0 | 1099 | // state. |
michael@0 | 1100 | // |
michael@0 | 1101 | // failed The ICE Agent is finished checking all candidate pairs and |
michael@0 | 1102 | // failed to find a connection for at least one component. |
michael@0 | 1103 | // Connections may have been found for some components. |
michael@0 | 1104 | // |
michael@0 | 1105 | // disconnected Liveness checks have failed for one or more components. |
michael@0 | 1106 | // This is more aggressive than failed, and may trigger |
michael@0 | 1107 | // intermittently (and resolve itself without action) on a |
michael@0 | 1108 | // flaky network. |
michael@0 | 1109 | // |
michael@0 | 1110 | // closed The ICE Agent has shut down and is no longer responding to |
michael@0 | 1111 | // STUN requests. |
michael@0 | 1112 | |
michael@0 | 1113 | handleIceConnectionStateChange: function(iceConnectionState) { |
michael@0 | 1114 | var histogram = Services.telemetry.getHistogramById("WEBRTC_ICE_SUCCESS_RATE"); |
michael@0 | 1115 | |
michael@0 | 1116 | if (iceConnectionState === 'failed') { |
michael@0 | 1117 | histogram.add(false); |
michael@0 | 1118 | this._dompc.logError("ICE failed, see about:webrtc for more details", null, 0); |
michael@0 | 1119 | } |
michael@0 | 1120 | if (this._dompc.iceConnectionState === 'checking' && |
michael@0 | 1121 | (iceConnectionState === 'completed' || |
michael@0 | 1122 | iceConnectionState === 'connected')) { |
michael@0 | 1123 | histogram.add(true); |
michael@0 | 1124 | } |
michael@0 | 1125 | this._dompc.changeIceConnectionState(iceConnectionState); |
michael@0 | 1126 | }, |
michael@0 | 1127 | |
michael@0 | 1128 | // This method is responsible for updating iceGatheringState. This |
michael@0 | 1129 | // state is defined in the WebRTC specification as follows: |
michael@0 | 1130 | // |
michael@0 | 1131 | // iceGatheringState: |
michael@0 | 1132 | // ------------------ |
michael@0 | 1133 | // new The object was just created, and no networking has occurred |
michael@0 | 1134 | // yet. |
michael@0 | 1135 | // |
michael@0 | 1136 | // gathering The ICE engine is in the process of gathering candidates for |
michael@0 | 1137 | // this RTCPeerConnection. |
michael@0 | 1138 | // |
michael@0 | 1139 | // complete The ICE engine has completed gathering. Events such as adding |
michael@0 | 1140 | // a new interface or a new TURN server will cause the state to |
michael@0 | 1141 | // go back to gathering. |
michael@0 | 1142 | // |
michael@0 | 1143 | handleIceGatheringStateChange: function(gatheringState) { |
michael@0 | 1144 | this._dompc.changeIceGatheringState(gatheringState); |
michael@0 | 1145 | |
michael@0 | 1146 | if (gatheringState === "complete") { |
michael@0 | 1147 | if (!this._dompc._trickleIce) { |
michael@0 | 1148 | // If we are not trickling, then the queue is in a pending state |
michael@0 | 1149 | // waiting for ICE gathering and executeNext frees it |
michael@0 | 1150 | this._dompc._executeNext(); |
michael@0 | 1151 | } |
michael@0 | 1152 | else if (this._dompc.localDescription) { |
michael@0 | 1153 | // If we are trickling but we have already done setLocal, |
michael@0 | 1154 | // then we need to send a final foundIceCandidate(null) to indicate |
michael@0 | 1155 | // that we are done gathering. |
michael@0 | 1156 | this.foundIceCandidate(null); |
michael@0 | 1157 | } |
michael@0 | 1158 | } |
michael@0 | 1159 | }, |
michael@0 | 1160 | |
michael@0 | 1161 | onStateChange: function(state) { |
michael@0 | 1162 | switch (state) { |
michael@0 | 1163 | case "SignalingState": |
michael@0 | 1164 | this.callCB(this._dompc.onsignalingstatechange, |
michael@0 | 1165 | this._dompc.signalingState); |
michael@0 | 1166 | break; |
michael@0 | 1167 | |
michael@0 | 1168 | case "IceConnectionState": |
michael@0 | 1169 | this.handleIceConnectionStateChange(this._dompc._pc.iceConnectionState); |
michael@0 | 1170 | break; |
michael@0 | 1171 | |
michael@0 | 1172 | case "IceGatheringState": |
michael@0 | 1173 | this.handleIceGatheringStateChange(this._dompc._pc.iceGatheringState); |
michael@0 | 1174 | break; |
michael@0 | 1175 | |
michael@0 | 1176 | case "SdpState": |
michael@0 | 1177 | // No-op |
michael@0 | 1178 | break; |
michael@0 | 1179 | |
michael@0 | 1180 | case "ReadyState": |
michael@0 | 1181 | // No-op |
michael@0 | 1182 | break; |
michael@0 | 1183 | |
michael@0 | 1184 | case "SipccState": |
michael@0 | 1185 | // No-op |
michael@0 | 1186 | break; |
michael@0 | 1187 | |
michael@0 | 1188 | default: |
michael@0 | 1189 | this._dompc.logWarning("Unhandled state type: " + state, null, 0); |
michael@0 | 1190 | break; |
michael@0 | 1191 | } |
michael@0 | 1192 | }, |
michael@0 | 1193 | |
michael@0 | 1194 | onGetStatsSuccess: function(dict) { |
michael@0 | 1195 | let chromeobj = new RTCStatsReport(this._dompc._win, dict); |
michael@0 | 1196 | let webidlobj = this._dompc._win.RTCStatsReport._create(this._dompc._win, |
michael@0 | 1197 | chromeobj); |
michael@0 | 1198 | chromeobj.makeStatsPublic(); |
michael@0 | 1199 | this.callCB(this._dompc._onGetStatsSuccess, webidlobj); |
michael@0 | 1200 | this._dompc._executeNext(); |
michael@0 | 1201 | }, |
michael@0 | 1202 | |
michael@0 | 1203 | onGetStatsError: function(code, message) { |
michael@0 | 1204 | this.callCB(this._dompc._onGetStatsFailure, new RTCError(code, message)); |
michael@0 | 1205 | this._dompc._executeNext(); |
michael@0 | 1206 | }, |
michael@0 | 1207 | |
michael@0 | 1208 | onAddStream: function(stream) { |
michael@0 | 1209 | this.dispatchEvent(new this._dompc._win.MediaStreamEvent("addstream", |
michael@0 | 1210 | { stream: stream })); |
michael@0 | 1211 | }, |
michael@0 | 1212 | |
michael@0 | 1213 | onRemoveStream: function(stream, type) { |
michael@0 | 1214 | this.dispatchEvent(new this._dompc._win.MediaStreamEvent("removestream", |
michael@0 | 1215 | { stream: stream })); |
michael@0 | 1216 | }, |
michael@0 | 1217 | |
michael@0 | 1218 | foundIceCandidate: function(cand) { |
michael@0 | 1219 | this.dispatchEvent(new this._dompc._win.RTCPeerConnectionIceEvent("icecandidate", |
michael@0 | 1220 | { candidate: cand } )); |
michael@0 | 1221 | }, |
michael@0 | 1222 | |
michael@0 | 1223 | notifyDataChannel: function(channel) { |
michael@0 | 1224 | this.dispatchEvent(new this._dompc._win.RTCDataChannelEvent("datachannel", |
michael@0 | 1225 | { channel: channel })); |
michael@0 | 1226 | }, |
michael@0 | 1227 | |
michael@0 | 1228 | notifyConnection: function() { |
michael@0 | 1229 | this.dispatchEvent(new this._dompc._win.Event("connection")); |
michael@0 | 1230 | }, |
michael@0 | 1231 | |
michael@0 | 1232 | notifyClosedConnection: function() { |
michael@0 | 1233 | this.dispatchEvent(new this._dompc._win.Event("closedconnection")); |
michael@0 | 1234 | }, |
michael@0 | 1235 | |
michael@0 | 1236 | getSupportedConstraints: function(dict) { |
michael@0 | 1237 | return dict; |
michael@0 | 1238 | }, |
michael@0 | 1239 | }; |
michael@0 | 1240 | |
michael@0 | 1241 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory( |
michael@0 | 1242 | [GlobalPCList, |
michael@0 | 1243 | RTCIceCandidate, |
michael@0 | 1244 | RTCSessionDescription, |
michael@0 | 1245 | RTCPeerConnection, |
michael@0 | 1246 | RTCStatsReport, |
michael@0 | 1247 | RTCIdentityAssertion, |
michael@0 | 1248 | PeerConnectionObserver] |
michael@0 | 1249 | ); |