summaryrefslogtreecommitdiff
path: root/js/lib/janus.nojquery.js
blob: 2343ac6cd667dd72e9ab99d207ea1ea30359a27e (plain)
  1. /*
  2.     The MIT License (MIT)
  3. Copyright (c) 2016 Meetecho
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the "Software"),
  6. to deal in the Software without restriction, including without limitation
  7. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. and/or sell copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  16. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  17. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  18. OTHER DEALINGS IN THE SOFTWARE.
  19. */
  20. // List of sessions
  21. Janus.sessions = {};
  22. // Screensharing Chrome Extension ID
  23. Janus.extensionId = "hapfgfdkleiggjjpfpenajgdnfckjpaj";
  24. Janus.isExtensionEnabled = function() {
  25. if(window.navigator.userAgent.match('Chrome')) {
  26. var chromever = parseInt(window.navigator.userAgent.match(/Chrome\/(.*) /)[1], 10);
  27. var maxver = 33;
  28. if(window.navigator.userAgent.match('Linux'))
  29. maxver = 35; // "known" crash in chrome 34 and 35 on linux
  30. if(chromever >= 26 && chromever <= maxver) {
  31. // Older versions of Chrome don't support this extension-based approach, so lie
  32. return true;
  33. }
  34. return (document.getElementById('janus-extension-installed') !== null);
  35. } else {
  36. // Firefox of others, no need for the extension (but this doesn't mean it will work)
  37. return true;
  38. }
  39. };
  40. Janus.noop = function() {};
  41. // Initialization
  42. Janus.init = function(options) {
  43. options = options || {};
  44. options.callback = (typeof options.callback == "function") ? options.callback : Janus.noop;
  45. if(Janus.initDone === true) {
  46. // Already initialized
  47. options.callback();
  48. } else {
  49. if(typeof console == "undefined" || typeof console.log == "undefined")
  50. console = { log: function() {} };
  51. // Console logging (all debugging disabled by default)
  52. Janus.trace = Janus.noop;
  53. Janus.debug = Janus.noop;
  54. Janus.vdebug = Janus.noop;
  55. Janus.log = Janus.noop;
  56. Janus.warn = Janus.noop;
  57. Janus.error = Janus.noop;
  58. if(options.debug === true || options.debug === "all") {
  59. // Enable all debugging levels
  60. Janus.trace = console.trace.bind(console);
  61. Janus.debug = console.debug.bind(console);
  62. Janus.vdebug = console.debug.bind(console);
  63. Janus.log = console.log.bind(console);
  64. Janus.warn = console.warn.bind(console);
  65. Janus.error = console.error.bind(console);
  66. } else if(Array.isArray(options.debug)) {
  67. for(var i in options.debug) {
  68. var d = options.debug[i];
  69. switch(d) {
  70. case "trace":
  71. Janus.trace = console.trace.bind(console);
  72. break;
  73. case "debug":
  74. Janus.debug = console.debug.bind(console);
  75. break;
  76. case "vdebug":
  77. Janus.vdebug = console.debug.bind(console);
  78. break;
  79. case "log":
  80. Janus.log = console.log.bind(console);
  81. break;
  82. case "warn":
  83. Janus.warn = console.warn.bind(console);
  84. break;
  85. case "error":
  86. Janus.error = console.error.bind(console);
  87. break;
  88. default:
  89. console.error("Unknown debugging option '" + d + "' (supported: 'trace', 'debug', 'vdebug', 'log', warn', 'error')");
  90. break;
  91. }
  92. }
  93. }
  94. Janus.log("Initializing library");
  95. // Helper method to enumerate devices
  96. Janus.listDevices = function(callback) {
  97. callback = (typeof callback == "function") ? callback : Janus.noop;
  98. if(navigator.mediaDevices) {
  99. navigator.mediaDevices.getUserMedia({ audio: true, video: true })
  100. .then(function(stream) {
  101. navigator.mediaDevices.enumerateDevices().then(function(devices) {
  102. Janus.debug(devices);
  103. callback(devices);
  104. // Get rid of the now useless stream
  105. try {
  106. stream.stop();
  107. } catch(e) {}
  108. try {
  109. var tracks = stream.getTracks();
  110. for(var i in tracks) {
  111. var mst = tracks[i];
  112. if(mst !== null && mst !== undefined)
  113. mst.stop();
  114. }
  115. } catch(e) {}
  116. });
  117. })
  118. .catch(function(err) {
  119. Janus.error(err);
  120. callback([]);
  121. });
  122. } else {
  123. Janus.warn("navigator.mediaDevices unavailable");
  124. callback([]);
  125. }
  126. }
  127. // Helper methods to attach/reattach a stream to a video element (previously part of adapter.js)
  128. Janus.attachMediaStream = function(element, stream) {
  129. if(adapter.browserDetails.browser === 'chrome') {
  130. var chromever = adapter.browserDetails.version;
  131. if(chromever >= 43) {
  132. element.srcObject = stream;
  133. } else if(typeof element.src !== 'undefined') {
  134. element.src = URL.createObjectURL(stream);
  135. } else {
  136. Janus.error("Error attaching stream to element");
  137. }
  138. } else if(adapter.browserDetails.browser === 'safari' || window.navigator.userAgent.match(/iPad/i) || window.navigator.userAgent.match(/iPhone/i)) {
  139. element.src = URL.createObjectURL(stream);
  140. }
  141. else {
  142. element.srcObject = stream;
  143. }
  144. };
  145. Janus.reattachMediaStream = function(to, from) {
  146. if(adapter.browserDetails.browser === 'chrome') {
  147. var chromever = adapter.browserDetails.version;
  148. if(chromever >= 43) {
  149. to.srcObject = from.srcObject;
  150. } else if(typeof to.src !== 'undefined') {
  151. to.src = from.src;
  152. }
  153. } else if(adapter.browserDetails.browser === 'safari' || window.navigator.userAgent.match(/iPad/i) || window.navigator.userAgent.match(/iPhone/i)) {
  154. to.src = from.src;
  155. }
  156. else {
  157. to.srcObject = from.srcObject;
  158. }
  159. };
  160. // Prepare a helper method to send AJAX requests in a syntax similar to jQuery (at least for what we care)
  161. Janus.ajax = function(params) {
  162. // Check params
  163. if(params === null || params === undefined)
  164. return;
  165. params.success = (typeof params.success == "function") ? params.success : Janus.noop;
  166. params.error = (typeof params.error == "function") ? params.error : Janus.noop;
  167. // Make sure there's an URL
  168. if(params.url === null || params.url === undefined) {
  169. Janus.error('Missing url', params.url);
  170. params.error(null, -1, 'Missing url');
  171. return;
  172. }
  173. // Validate async
  174. params.async = (params.async === null || params.async === undefined) ? true : (params.async === true);
  175. Janus.log(params);
  176. // IE doesn't even know what WebRTC is, so no polyfill needed
  177. var XHR = new XMLHttpRequest();
  178. XHR.open(params.type, params.url, params.async);
  179. if(params.contentType !== null && params.contentType !== undefined)
  180. XHR.setRequestHeader('Content-type', params.contentType);
  181. if(params.withCredentials !== null && params.withCredentials !== undefined)
  182. XHR.withCredentials = params.withCredentials;
  183. if(params.async) {
  184. XHR.onreadystatechange = function () {
  185. if(XHR.readyState != 4)
  186. return;
  187. if(XHR.status !== 200) {
  188. // Got an error?
  189. params.error(XHR, XHR.status !== 0 ? XHR.status : 'error', "");
  190. return;
  191. }
  192. // Got payload
  193. try {
  194. params.success(JSON.parse(XHR.responseText));
  195. } catch(e) {
  196. params.error(XHR, XHR.status, 'Could not parse response, error: ' + e + ', text: ' + XHR.responseText);
  197. }
  198. };
  199. }
  200. try {
  201. XHR.send(params.data);
  202. if(!params.async) {
  203. if(XHR.status !== 200) {
  204. // Got an error?
  205. params.error(XHR, XHR.status !== 0 ? XHR.status : 'error', "");
  206. return;
  207. }
  208. // Got payload
  209. try {
  210. params.success(JSON.parse(XHR.responseText));
  211. } catch(e) {
  212. params.error(XHR, XHR.status, 'Could not parse response, error: ' + e + ', text: ' + XHR.responseText);
  213. }
  214. }
  215. } catch(e) {
  216. // Something broke up
  217. params.error(XHR, 'error', '');
  218. };
  219. };
  220. // Detect tab close: make sure we don't loose existing onbeforeunload handlers
  221. var oldOBF = window.onbeforeunload;
  222. window.onbeforeunload = function() {
  223. Janus.log("Closing window");
  224. for(var s in Janus.sessions) {
  225. if(Janus.sessions[s] !== null && Janus.sessions[s] !== undefined &&
  226. Janus.sessions[s].destroyOnUnload) {
  227. Janus.log("Destroying session " + s);
  228. Janus.sessions[s].destroy({asyncRequest: false});
  229. }
  230. }
  231. if(oldOBF && typeof oldOBF == "function")
  232. oldOBF();
  233. }
  234. Janus.initDone = true;
  235. options.callback();
  236. }
  237. };
  238. // Helper method to check whether WebRTC is supported by this browser
  239. Janus.isWebrtcSupported = function() {
  240. return window.RTCPeerConnection !== undefined && window.RTCPeerConnection !== null &&
  241. navigator.getUserMedia !== undefined && navigator.getUserMedia !== null;
  242. };
  243. // Helper method to create random identifiers (e.g., transaction)
  244. Janus.randomString = function(len) {
  245. var charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  246. var randomString = '';
  247. for (var i = 0; i < len; i++) {
  248. var randomPoz = Math.floor(Math.random() * charSet.length);
  249. randomString += charSet.substring(randomPoz,randomPoz+1);
  250. }
  251. return randomString;
  252. }
  253. // Janus session object
  254. function Janus(gatewayCallbacks) {
  255. if(Janus.initDone === undefined) {
  256. gatewayCallbacks.error("Library not initialized");
  257. return {};
  258. }
  259. if(!Janus.isWebrtcSupported()) {
  260. gatewayCallbacks.error("WebRTC not supported by this browser");
  261. return {};
  262. }
  263. Janus.log("Library initialized: " + Janus.initDone);
  264. gatewayCallbacks = gatewayCallbacks || {};
  265. gatewayCallbacks.success = (typeof gatewayCallbacks.success == "function") ? gatewayCallbacks.success : Janus.noop;
  266. gatewayCallbacks.error = (typeof gatewayCallbacks.error == "function") ? gatewayCallbacks.error : Janus.noop;
  267. gatewayCallbacks.destroyed = (typeof gatewayCallbacks.destroyed == "function") ? gatewayCallbacks.destroyed : Janus.noop;
  268. if(gatewayCallbacks.server === null || gatewayCallbacks.server === undefined) {
  269. gatewayCallbacks.error("Invalid gateway url");
  270. return {};
  271. }
  272. var websockets = false;
  273. var ws = null;
  274. var wsHandlers = {};
  275. var wsKeepaliveTimeoutId = null;
  276. var servers = null, serversIndex = 0;
  277. var server = gatewayCallbacks.server;
  278. if(Array.isArray(server)) {
  279. Janus.log("Multiple servers provided (" + server.length + "), will use the first that works");
  280. server = null;
  281. servers = gatewayCallbacks.server;
  282. Janus.debug(servers);
  283. } else {
  284. if(server.indexOf("ws") === 0) {
  285. websockets = true;
  286. Janus.log("Using WebSockets to contact Janus: " + server);
  287. } else {
  288. websockets = false;
  289. Janus.log("Using REST API to contact Janus: " + server);
  290. }
  291. }
  292. var iceServers = gatewayCallbacks.iceServers;
  293. if(iceServers === undefined || iceServers === null)
  294. iceServers = [{urls: "stun:stun.l.google.com:19302"}];
  295. var iceTransportPolicy = gatewayCallbacks.iceTransportPolicy;
  296. // Whether IPv6 candidates should be gathered
  297. var ipv6Support = gatewayCallbacks.ipv6;
  298. if(ipv6Support === undefined || ipv6Support === null)
  299. ipv6Support = false;
  300. // Whether we should enable the withCredentials flag for XHR requests
  301. var withCredentials = false;
  302. if(gatewayCallbacks.withCredentials !== undefined && gatewayCallbacks.withCredentials !== null)
  303. withCredentials = gatewayCallbacks.withCredentials === true;
  304. // Optional max events
  305. var maxev = null;
  306. if(gatewayCallbacks.max_poll_events !== undefined && gatewayCallbacks.max_poll_events !== null)
  307. maxev = gatewayCallbacks.max_poll_events;
  308. if(maxev < 1)
  309. maxev = 1;
  310. // Token to use (only if the token based authentication mechanism is enabled)
  311. var token = null;
  312. if(gatewayCallbacks.token !== undefined && gatewayCallbacks.token !== null)
  313. token = gatewayCallbacks.token;
  314. // API secret to use (only if the shared API secret is enabled)
  315. var apisecret = null;
  316. if(gatewayCallbacks.apisecret !== undefined && gatewayCallbacks.apisecret !== null)
  317. apisecret = gatewayCallbacks.apisecret;
  318. // Whether we should destroy this session when onbeforeunload is called
  319. this.destroyOnUnload = true;
  320. if(gatewayCallbacks.destroyOnUnload !== undefined && gatewayCallbacks.destroyOnUnload !== null)
  321. this.destroyOnUnload = (gatewayCallbacks.destroyOnUnload === true);
  322. var connected = false;
  323. var sessionId = null;
  324. var pluginHandles = {};
  325. var that = this;
  326. var retries = 0;
  327. var transactions = {};
  328. createSession(gatewayCallbacks);
  329. // Public methods
  330. this.getServer = function() { return server; };
  331. this.isConnected = function() { return connected; };
  332. this.getSessionId = function() { return sessionId; };
  333. this.destroy = function(callbacks) { destroySession(callbacks, true); };
  334. this.attach = function(callbacks) { createHandle(callbacks); };
  335. function eventHandler() {
  336. if(sessionId == null)
  337. return;
  338. Janus.debug('Long poll...');
  339. if(!connected) {
  340. Janus.warn("Is the gateway down? (connected=false)");
  341. return;
  342. }
  343. var longpoll = server + "/" + sessionId + "?rid=" + new Date().getTime();
  344. if(maxev !== undefined && maxev !== null)
  345. longpoll = longpoll + "&maxev=" + maxev;
  346. if(token !== null && token !== undefined)
  347. longpoll = longpoll + "&token=" + token;
  348. if(apisecret !== null && apisecret !== undefined)
  349. longpoll = longpoll + "&apisecret=" + apisecret;
  350. Janus.ajax({
  351. type: 'GET',
  352. url: longpoll,
  353. withCredentials: withCredentials,
  354. cache: false,
  355. timeout: 60000, // FIXME
  356. success: handleEvent,
  357. error: function(XMLHttpRequest, textStatus, errorThrown) {
  358. Janus.error(textStatus + ": " + errorThrown);
  359. retries++;
  360. if(retries > 3) {
  361. // Did we just lose the gateway? :-(
  362. connected = false;
  363. gatewayCallbacks.error("Lost connection to the gateway (is it down?)");
  364. return;
  365. }
  366. eventHandler();
  367. },
  368. dataType: "json"
  369. });
  370. }
  371. // Private event handler: this will trigger plugin callbacks, if set
  372. function handleEvent(json) {
  373. retries = 0;
  374. if(!websockets && sessionId !== undefined && sessionId !== null)
  375. setTimeout(eventHandler, 200);
  376. if(!websockets && Array.isArray(json)) {
  377. // We got an array: it means we passed a maxev > 1, iterate on all objects
  378. for(var i=0; i<json.length; i++) {
  379. handleEvent(json[i]);
  380. }
  381. return;
  382. }
  383. if(json["janus"] === "keepalive") {
  384. // Nothing happened
  385. Janus.vdebug("Got a keepalive on session " + sessionId);
  386. return;
  387. } else if(json["janus"] === "ack") {
  388. // Just an ack, we can probably ignore
  389. Janus.debug("Got an ack on session " + sessionId);
  390. Janus.debug(json);
  391. var transaction = json["transaction"];
  392. if(transaction !== null && transaction !== undefined) {
  393. var reportSuccess = transactions[transaction];
  394. if(reportSuccess !== null && reportSuccess !== undefined) {
  395. reportSuccess(json);
  396. }
  397. delete transactions[transaction];
  398. }
  399. return;
  400. } else if(json["janus"] === "success") {
  401. // Success!
  402. Janus.debug("Got a success on session " + sessionId);
  403. Janus.debug(json);
  404. var transaction = json["transaction"];
  405. if(transaction !== null && transaction !== undefined) {
  406. var reportSuccess = transactions[transaction];
  407. if(reportSuccess !== null && reportSuccess !== undefined) {
  408. reportSuccess(json);
  409. }
  410. delete transactions[transaction];
  411. }
  412. return;
  413. } else if(json["janus"] === "webrtcup") {
  414. // The PeerConnection with the gateway is up! Notify this
  415. Janus.debug("Got a webrtcup event on session " + sessionId);
  416. Janus.debug(json);
  417. var sender = json["sender"];
  418. if(sender === undefined || sender === null) {
  419. Janus.warn("Missing sender...");
  420. return;
  421. }
  422. var pluginHandle = pluginHandles[sender];
  423. if(pluginHandle === undefined || pluginHandle === null) {
  424. Janus.debug("This handle is not attached to this session");
  425. return;
  426. }
  427. pluginHandle.webrtcState(true);
  428. return;
  429. } else if(json["janus"] === "hangup") {
  430. // A plugin asked the core to hangup a PeerConnection on one of our handles
  431. Janus.debug("Got a hangup event on session " + sessionId);
  432. Janus.debug(json);
  433. var sender = json["sender"];
  434. if(sender === undefined || sender === null) {
  435. Janus.warn("Missing sender...");
  436. return;
  437. }
  438. var pluginHandle = pluginHandles[sender];
  439. if(pluginHandle === undefined || pluginHandle === null) {
  440. Janus.debug("This handle is not attached to this session");
  441. return;
  442. }
  443. pluginHandle.webrtcState(false, json["reason"]);
  444. pluginHandle.hangup();
  445. } else if(json["janus"] === "detached") {
  446. // A plugin asked the core to detach one of our handles
  447. Janus.debug("Got a detached event on session " + sessionId);
  448. Janus.debug(json);
  449. var sender = json["sender"];
  450. if(sender === undefined || sender === null) {
  451. Janus.warn("Missing sender...");
  452. return;
  453. }
  454. var pluginHandle = pluginHandles[sender];
  455. if(pluginHandle === undefined || pluginHandle === null) {
  456. // Don't warn here because destroyHandle causes this situation.
  457. return;
  458. }
  459. pluginHandle.detached = true;
  460. pluginHandle.ondetached();
  461. pluginHandle.detach();
  462. } else if(json["janus"] === "media") {
  463. // Media started/stopped flowing
  464. Janus.debug("Got a media event on session " + sessionId);
  465. Janus.debug(json);
  466. var sender = json["sender"];
  467. if(sender === undefined || sender === null) {
  468. Janus.warn("Missing sender...");
  469. return;
  470. }
  471. var pluginHandle = pluginHandles[sender];
  472. if(pluginHandle === undefined || pluginHandle === null) {
  473. Janus.debug("This handle is not attached to this session");
  474. return;
  475. }
  476. pluginHandle.mediaState(json["type"], json["receiving"]);
  477. } else if(json["janus"] === "slowlink") {
  478. Janus.debug("Got a slowlink event on session " + sessionId);
  479. Janus.debug(json);
  480. // Trouble uplink or downlink
  481. var sender = json["sender"];
  482. if(sender === undefined || sender === null) {
  483. Janus.warn("Missing sender...");
  484. return;
  485. }
  486. var pluginHandle = pluginHandles[sender];
  487. if(pluginHandle === undefined || pluginHandle === null) {
  488. Janus.debug("This handle is not attached to this session");
  489. return;
  490. }
  491. pluginHandle.slowLink(json["uplink"], json["nacks"]);
  492. } else if(json["janus"] === "error") {
  493. // Oops, something wrong happened
  494. Janus.error("Ooops: " + json["error"].code + " " + json["error"].reason); // FIXME
  495. Janus.debug(json);
  496. var transaction = json["transaction"];
  497. if(transaction !== null && transaction !== undefined) {
  498. var reportSuccess = transactions[transaction];
  499. if(reportSuccess !== null && reportSuccess !== undefined) {
  500. reportSuccess(json);
  501. }
  502. delete transactions[transaction];
  503. }
  504. return;
  505. } else if(json["janus"] === "event") {
  506. Janus.debug("Got a plugin event on session " + sessionId);
  507. Janus.debug(json);
  508. var sender = json["sender"];
  509. if(sender === undefined || sender === null) {
  510. Janus.warn("Missing sender...");
  511. return;
  512. }
  513. var plugindata = json["plugindata"];
  514. if(plugindata === undefined || plugindata === null) {
  515. Janus.warn("Missing plugindata...");
  516. return;
  517. }
  518. Janus.debug(" -- Event is coming from " + sender + " (" + plugindata["plugin"] + ")");
  519. var data = plugindata["data"];
  520. Janus.debug(data);
  521. var pluginHandle = pluginHandles[sender];
  522. if(pluginHandle === undefined || pluginHandle === null) {
  523. Janus.warn("This handle is not attached to this session");
  524. return;
  525. }
  526. var jsep = json["jsep"];
  527. if(jsep !== undefined && jsep !== null) {
  528. Janus.debug("Handling SDP as well...");
  529. Janus.debug(jsep);
  530. }
  531. var callback = pluginHandle.onmessage;
  532. if(callback !== null && callback !== undefined) {
  533. Janus.debug("Notifying application...");
  534. // Send to callback specified when attaching plugin handle
  535. callback(data, jsep);
  536. } else {
  537. // Send to generic callback (?)
  538. Janus.debug("No provided notification callback");
  539. }
  540. } else {
  541. Janus.warn("Unkown message/event '" + json["janus"] + "' on session " + sessionId);
  542. Janus.debug(json);
  543. }
  544. }
  545. // Private helper to send keep-alive messages on WebSockets
  546. function keepAlive() {
  547. if(server === null || !websockets || !connected)
  548. return;
  549. wsKeepaliveTimeoutId = setTimeout(keepAlive, 30000);
  550. var request = { "janus": "keepalive", "session_id": sessionId, "transaction": Janus.randomString(12) };
  551. if(token !== null && token !== undefined)
  552. request["token"] = token;
  553. if(apisecret !== null && apisecret !== undefined)
  554. request["apisecret"] = apisecret;
  555. ws.send(JSON.stringify(request));
  556. }
  557. // Private method to create a session
  558. function createSession(callbacks) {
  559. var transaction = Janus.randomString(12);
  560. var request = { "janus": "create", "transaction": transaction };
  561. if(token !== null && token !== undefined)
  562. request["token"] = token;
  563. if(apisecret !== null && apisecret !== undefined)
  564. request["apisecret"] = apisecret;
  565. if(server === null && Array.isArray(servers)) {
  566. // We still need to find a working server from the list we were given
  567. server = servers[serversIndex];
  568. if(server.indexOf("ws") === 0) {
  569. websockets = true;
  570. Janus.log("Server #" + (serversIndex+1) + ": trying WebSockets to contact Janus (" + server + ")");
  571. } else {
  572. websockets = false;
  573. Janus.log("Server #" + (serversIndex+1) + ": trying REST API to contact Janus (" + server + ")");
  574. }
  575. }
  576. if(websockets) {
  577. ws = new WebSocket(server, 'janus-protocol');
  578. wsHandlers = {
  579. 'error': function() {
  580. Janus.error("Error connecting to the Janus WebSockets server... " + server);
  581. if (Array.isArray(servers)) {
  582. serversIndex++;
  583. if (serversIndex == servers.length) {
  584. // We tried all the servers the user gave us and they all failed
  585. callbacks.error("Error connecting to any of the provided Janus servers: Is the gateway down?");
  586. return;
  587. }
  588. // Let's try the next server
  589. server = null;
  590. setTimeout(function() {
  591. createSession(callbacks);
  592. }, 200);
  593. return;
  594. }
  595. callbacks.error("Error connecting to the Janus WebSockets server: Is the gateway down?");
  596. },
  597. 'open': function() {
  598. // We need to be notified about the success
  599. transactions[transaction] = function(json) {
  600. Janus.debug(json);
  601. if (json["janus"] !== "success") {
  602. Janus.error("Ooops: " + json["error"].code + " " + json["error"].reason); // FIXME
  603. callbacks.error(json["error"].reason);
  604. return;
  605. }
  606. wsKeepaliveTimeoutId = setTimeout(keepAlive, 30000);
  607. connected = true;
  608. sessionId = json.data["id"];
  609. Janus.log("Created session: " + sessionId);
  610. Janus.sessions[sessionId] = that;
  611. callbacks.success();
  612. };
  613. ws.send(JSON.stringify(request));
  614. },
  615. 'message': function(event) {
  616. try {
  617. handleEvent(JSON.parse(event.data));
  618. } catch(e) {
  619. Janus.error('Error processing event:', e);
  620. }
  621. },
  622. 'close': function() {
  623. if (server === null || !connected) {
  624. return;
  625. }
  626. connected = false;
  627. // FIXME What if this is called when the page is closed?
  628. gatewayCallbacks.error("Lost connection to the gateway (is it down?)");
  629. }
  630. };
  631. for(var eventName in wsHandlers) {
  632. ws.addEventListener(eventName, wsHandlers[eventName]);
  633. }
  634. return;
  635. }
  636. Janus.ajax({
  637. type: 'POST',
  638. url: server,
  639. withCredentials: withCredentials,
  640. cache: false,
  641. contentType: "application/json",
  642. data: JSON.stringify(request),
  643. success: function(json) {
  644. Janus.debug(json);
  645. if(json["janus"] !== "success") {
  646. Janus.error("Ooops: " + json["error"].code + " " + json["error"].reason); // FIXME
  647. callbacks.error(json["error"].reason);
  648. return;
  649. }
  650. connected = true;
  651. sessionId = json.data["id"];
  652. Janus.log("Created session: " + sessionId);
  653. Janus.sessions[sessionId] = that;
  654. eventHandler();
  655. callbacks.success();
  656. },
  657. error: function(XMLHttpRequest, textStatus, errorThrown) {
  658. Janus.error(textStatus + ": " + errorThrown); // FIXME
  659. if(Array.isArray(servers)) {
  660. serversIndex++;
  661. if(serversIndex == servers.length) {
  662. // We tried all the servers the user gave us and they all failed
  663. callbacks.error("Error connecting to any of the provided Janus servers: Is the gateway down?");
  664. return;
  665. }
  666. // Let's try the next server
  667. server = null;
  668. setTimeout(function() { createSession(callbacks); }, 200);
  669. return;
  670. }
  671. if(errorThrown === "")
  672. callbacks.error(textStatus + ": Is the gateway down?");
  673. else
  674. callbacks.error(textStatus + ": " + errorThrown);
  675. },
  676. dataType: "json"
  677. });
  678. }
  679. // Private method to destroy a session
  680. function destroySession(callbacks) {
  681. callbacks = callbacks || {};
  682. // FIXME This method triggers a success even when we fail
  683. callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop;
  684. var asyncRequest = true;
  685. if(callbacks.asyncRequest !== undefined && callbacks.asyncRequest !== null)
  686. asyncRequest = (callbacks.asyncRequest === true);
  687. Janus.log("Destroying session " + sessionId + " (async=" + asyncRequest + ")");
  688. if(!connected) {
  689. Janus.warn("Is the gateway down? (connected=false)");
  690. callbacks.success();
  691. return;
  692. }
  693. if(sessionId === undefined || sessionId === null) {
  694. Janus.warn("No session to destroy");
  695. callbacks.success();
  696. gatewayCallbacks.destroyed();
  697. return;
  698. }
  699. delete Janus.sessions[sessionId];
  700. // No need to destroy all handles first, Janus will do that itself
  701. var request = { "janus": "destroy", "transaction": Janus.randomString(12) };
  702. if(token !== null && token !== undefined)
  703. request["token"] = token;
  704. if(apisecret !== null && apisecret !== undefined)
  705. request["apisecret"] = apisecret;
  706. if(websockets) {
  707. request["session_id"] = sessionId;
  708. var unbindWebSocket = function() {
  709. for(var eventName in wsHandlers) {
  710. ws.removeEventListener(eventName, wsHandlers[eventName]);
  711. }
  712. ws.removeEventListener('message', onUnbindMessage);
  713. ws.removeEventListener('error', onUnbindError);
  714. if(wsKeepaliveTimeoutId) {
  715. clearTimeout(wsKeepaliveTimeoutId);
  716. }
  717. };
  718. var onUnbindMessage = function(event){
  719. var data = JSON.parse(event.data);
  720. if(data.session_id == request.session_id && data.transaction == request.transaction) {
  721. unbindWebSocket();
  722. callbacks.success();
  723. gatewayCallbacks.destroyed();
  724. }
  725. };
  726. var onUnbindError = function(event) {
  727. unbindWebSocket();
  728. callbacks.error("Failed to destroy the gateway: Is the gateway down?");
  729. gatewayCallbacks.destroyed();
  730. };
  731. ws.addEventListener('message', onUnbindMessage);
  732. ws.addEventListener('error', onUnbindError);
  733. ws.send(JSON.stringify(request));
  734. return;
  735. }
  736. Janus.ajax({
  737. type: 'POST',
  738. url: server + "/" + sessionId,
  739. async: asyncRequest, // Sometimes we need false here, or destroying in onbeforeunload won't work
  740. withCredentials: withCredentials,
  741. cache: false,
  742. contentType: "application/json",
  743. data: JSON.stringify(request),
  744. success: function(json) {
  745. Janus.log("Destroyed session:");
  746. Janus.debug(json);
  747. sessionId = null;
  748. connected = false;
  749. if(json["janus"] !== "success") {
  750. Janus.error("Ooops: " + json["error"].code + " " + json["error"].reason); // FIXME
  751. }
  752. callbacks.success();
  753. gatewayCallbacks.destroyed();
  754. },
  755. error: function(XMLHttpRequest, textStatus, errorThrown) {
  756. Janus.error(textStatus + ": " + errorThrown); // FIXME
  757. // Reset everything anyway
  758. sessionId = null;
  759. connected = false;
  760. callbacks.success();
  761. gatewayCallbacks.destroyed();
  762. },
  763. dataType: "json"
  764. });
  765. }
  766. // Private method to create a plugin handle
  767. function createHandle(callbacks) {
  768. callbacks = callbacks || {};
  769. callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop;
  770. callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : Janus.noop;
  771. callbacks.consentDialog = (typeof callbacks.consentDialog == "function") ? callbacks.consentDialog : Janus.noop;
  772. callbacks.iceState = (typeof callbacks.iceState == "function") ? callbacks.iceState : Janus.noop;
  773. callbacks.mediaState = (typeof callbacks.mediaState == "function") ? callbacks.mediaState : Janus.noop;
  774. callbacks.webrtcState = (typeof callbacks.webrtcState == "function") ? callbacks.webrtcState : Janus.noop;
  775. callbacks.slowLink = (typeof callbacks.slowLink == "function") ? callbacks.slowLink : Janus.noop;
  776. callbacks.onmessage = (typeof callbacks.onmessage == "function") ? callbacks.onmessage : Janus.noop;
  777. callbacks.onlocalstream = (typeof callbacks.onlocalstream == "function") ? callbacks.onlocalstream : Janus.noop;
  778. callbacks.onremotestream = (typeof callbacks.onremotestream == "function") ? callbacks.onremotestream : Janus.noop;
  779. callbacks.ondata = (typeof callbacks.ondata == "function") ? callbacks.ondata : Janus.noop;
  780. callbacks.ondataopen = (typeof callbacks.ondataopen == "function") ? callbacks.ondataopen : Janus.noop;
  781. callbacks.oncleanup = (typeof callbacks.oncleanup == "function") ? callbacks.oncleanup : Janus.noop;
  782. callbacks.ondetached = (typeof callbacks.ondetached == "function") ? callbacks.ondetached : Janus.noop;
  783. if(!connected) {
  784. Janus.warn("Is the gateway down? (connected=false)");
  785. callbacks.error("Is the gateway down? (connected=false)");
  786. return;
  787. }
  788. var plugin = callbacks.plugin;
  789. if(plugin === undefined || plugin === null) {
  790. Janus.error("Invalid plugin");
  791. callbacks.error("Invalid plugin");
  792. return;
  793. }
  794. var opaqueId = callbacks.opaqueId;
  795. var transaction = Janus.randomString(12);
  796. var request = { "janus": "attach", "plugin": plugin, "opaque_id": opaqueId, "transaction": transaction };
  797. if(token !== null && token !== undefined)
  798. request["token"] = token;
  799. if(apisecret !== null && apisecret !== undefined)
  800. request["apisecret"] = apisecret;
  801. if(websockets) {
  802. transactions[transaction] = function(json) {
  803. Janus.debug(json);
  804. if(json["janus"] !== "success") {
  805. Janus.error("Ooops: " + json["error"].code + " " + json["error"].reason); // FIXME
  806. callbacks.error("Ooops: " + json["error"].code + " " + json["error"].reason);
  807. return;
  808. }
  809. var handleId = json.data["id"];
  810. Janus.log("Created handle: " + handleId);
  811. var pluginHandle =
  812. {
  813. session : that,
  814. plugin : plugin,
  815. id : handleId,
  816. detached : false,
  817. webrtcStuff : {
  818. started : false,
  819. myStream : null,
  820. streamExternal : false,
  821. remoteStream : null,
  822. mySdp : null,
  823. pc : null,
  824. dataChannel : null,
  825. dtmfSender : null,
  826. trickle : true,
  827. iceDone : false,
  828. sdpSent : false,
  829. volume : {
  830. value : null,
  831. timer : null
  832. },
  833. bitrate : {
  834. value : null,
  835. bsnow : null,
  836. bsbefore : null,
  837. tsnow : null,
  838. tsbefore : null,
  839. timer : null
  840. }
  841. },
  842. getId : function() { return handleId; },
  843. getPlugin : function() { return plugin; },
  844. getVolume : function() { return getVolume(handleId); },
  845. isAudioMuted : function() { return isMuted(handleId, false); },
  846. muteAudio : function() { return mute(handleId, false, true); },
  847. unmuteAudio : function() { return mute(handleId, false, false); },
  848. isVideoMuted : function() { return isMuted(handleId, true); },
  849. muteVideo : function() { return mute(handleId, true, true); },
  850. unmuteVideo : function() { return mute(handleId, true, false); },
  851. getBitrate : function() { return getBitrate(handleId); },
  852. send : function(callbacks) { sendMessage(handleId, callbacks); },
  853. data : function(callbacks) { sendData(handleId, callbacks); },
  854. dtmf : function(callbacks) { sendDtmf(handleId, callbacks); },
  855. consentDialog : callbacks.consentDialog,
  856. iceState : callbacks.iceState,
  857. mediaState : callbacks.mediaState,
  858. webrtcState : callbacks.webrtcState,
  859. slowLink : callbacks.slowLink,
  860. onmessage : callbacks.onmessage,
  861. createOffer : function(callbacks) { prepareWebrtc(handleId, callbacks); },
  862. createAnswer : function(callbacks) { prepareWebrtc(handleId, callbacks); },
  863. handleRemoteJsep : function(callbacks) { prepareWebrtcPeer(handleId, callbacks); },
  864. onlocalstream : callbacks.onlocalstream,
  865. onremotestream : callbacks.onremotestream,
  866. ondata : callbacks.ondata,
  867. ondataopen : callbacks.ondataopen,
  868. oncleanup : callbacks.oncleanup,
  869. ondetached : callbacks.ondetached,
  870. hangup : function(sendRequest) { cleanupWebrtc(handleId, sendRequest === true); },
  871. detach : function(callbacks) { destroyHandle(handleId, callbacks); }
  872. }
  873. pluginHandles[handleId] = pluginHandle;
  874. callbacks.success(pluginHandle);
  875. };
  876. request["session_id"] = sessionId;
  877. ws.send(JSON.stringify(request));
  878. return;
  879. }
  880. Janus.ajax({
  881. type: 'POST',
  882. url: server + "/" + sessionId,
  883. withCredentials: withCredentials,
  884. cache: false,
  885. contentType: "application/json",
  886. data: JSON.stringify(request),
  887. success: function(json) {
  888. Janus.debug(json);
  889. if(json["janus"] !== "success") {
  890. Janus.error("Ooops: " + json["error"].code + " " + json["error"].reason); // FIXME
  891. callbacks.error("Ooops: " + json["error"].code + " " + json["error"].reason);
  892. return;
  893. }
  894. var handleId = json.data["id"];
  895. Janus.log("Created handle: " + handleId);
  896. var pluginHandle =
  897. {
  898. session : that,
  899. plugin : plugin,
  900. id : handleId,
  901. detached : false,
  902. webrtcStuff : {
  903. started : false,
  904. myStream : null,
  905. streamExternal : false,
  906. remoteStream : null,
  907. mySdp : null,
  908. pc : null,
  909. dataChannel : null,
  910. dtmfSender : null,
  911. trickle : true,
  912. iceDone : false,
  913. sdpSent : false,
  914. volume : {
  915. value : null,
  916. timer : null
  917. },
  918. bitrate : {
  919. value : null,
  920. bsnow : null,
  921. bsbefore : null,
  922. tsnow : null,
  923. tsbefore : null,
  924. timer : null
  925. }
  926. },
  927. getId : function() { return handleId; },
  928. getPlugin : function() { return plugin; },
  929. getVolume : function() { return getVolume(handleId); },
  930. isAudioMuted : function() { return isMuted(handleId, false); },
  931. muteAudio : function() { return mute(handleId, false, true); },
  932. unmuteAudio : function() { return mute(handleId, false, false); },
  933. isVideoMuted : function() { return isMuted(handleId, true); },
  934. muteVideo : function() { return mute(handleId, true, true); },
  935. unmuteVideo : function() { return mute(handleId, true, false); },
  936. getBitrate : function() { return getBitrate(handleId); },
  937. send : function(callbacks) { sendMessage(handleId, callbacks); },
  938. data : function(callbacks) { sendData(handleId, callbacks); },
  939. dtmf : function(callbacks) { sendDtmf(handleId, callbacks); },
  940. consentDialog : callbacks.consentDialog,
  941. iceState : callbacks.iceState,
  942. mediaState : callbacks.mediaState,
  943. webrtcState : callbacks.webrtcState,
  944. slowLink : callbacks.slowLink,
  945. onmessage : callbacks.onmessage,
  946. createOffer : function(callbacks) { prepareWebrtc(handleId, callbacks); },
  947. createAnswer : function(callbacks) { prepareWebrtc(handleId, callbacks); },
  948. handleRemoteJsep : function(callbacks) { prepareWebrtcPeer(handleId, callbacks); },
  949. onlocalstream : callbacks.onlocalstream,
  950. onremotestream : callbacks.onremotestream,
  951. ondata : callbacks.ondata,
  952. ondataopen : callbacks.ondataopen,
  953. oncleanup : callbacks.oncleanup,
  954. ondetached : callbacks.ondetached,
  955. hangup : function(sendRequest) { cleanupWebrtc(handleId, sendRequest === true); },
  956. detach : function(callbacks) { destroyHandle(handleId, callbacks); }
  957. }
  958. pluginHandles[handleId] = pluginHandle;
  959. callbacks.success(pluginHandle);
  960. },
  961. error: function(XMLHttpRequest, textStatus, errorThrown) {
  962. Janus.error(textStatus + ": " + errorThrown); // FIXME
  963. },
  964. dataType: "json"
  965. });
  966. }
  967. // Private method to send a message
  968. function sendMessage(handleId, callbacks) {
  969. callbacks = callbacks || {};
  970. callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop;
  971. callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : Janus.noop;
  972. if(!connected) {
  973. Janus.warn("Is the gateway down? (connected=false)");
  974. callbacks.error("Is the gateway down? (connected=false)");
  975. return;
  976. }
  977. var message = callbacks.message;
  978. var jsep = callbacks.jsep;
  979. var transaction = Janus.randomString(12);
  980. var request = { "janus": "message", "body": message, "transaction": transaction };
  981. if(token !== null && token !== undefined)
  982. request["token"] = token;
  983. if(apisecret !== null && apisecret !== undefined)
  984. request["apisecret"] = apisecret;
  985. if(jsep !== null && jsep !== undefined)
  986. request.jsep = jsep;
  987. Janus.debug("Sending message to plugin (handle=" + handleId + "):");
  988. Janus.debug(request);
  989. if(websockets) {
  990. request["session_id"] = sessionId;
  991. request["handle_id"] = handleId;
  992. transactions[transaction] = function(json) {
  993. Janus.debug("Message sent!");
  994. Janus.debug(json);
  995. if(json["janus"] === "success") {
  996. // We got a success, must have been a synchronous transaction
  997. var plugindata = json["plugindata"];
  998. if(plugindata === undefined || plugindata === null) {
  999. Janus.warn("Request succeeded, but missing plugindata...");
  1000. callbacks.success();
  1001. return;
  1002. }
  1003. Janus.log("Synchronous transaction successful (" + plugindata["plugin"] + ")");
  1004. var data = plugindata["data"];
  1005. Janus.debug(data);
  1006. callbacks.success(data);
  1007. return;
  1008. } else if(json["janus"] !== "ack") {
  1009. // Not a success and not an ack, must be an error
  1010. if(json["error"] !== undefined && json["error"] !== null) {
  1011. Janus.error("Ooops: " + json["error"].code + " " + json["error"].reason); // FIXME
  1012. callbacks.error(json["error"].code + " " + json["error"].reason);
  1013. } else {
  1014. Janus.error("Unknown error"); // FIXME
  1015. callbacks.error("Unknown error");
  1016. }
  1017. return;
  1018. }
  1019. // If we got here, the plugin decided to handle the request asynchronously
  1020. callbacks.success();
  1021. };
  1022. ws.send(JSON.stringify(request));
  1023. return;
  1024. }
  1025. Janus.ajax({
  1026. type: 'POST',
  1027. url: server + "/" + sessionId + "/" + handleId,
  1028. withCredentials: withCredentials,
  1029. cache: false,
  1030. contentType: "application/json",
  1031. data: JSON.stringify(request),
  1032. success: function(json) {
  1033. Janus.debug("Message sent!");
  1034. Janus.debug(json);
  1035. if(json["janus"] === "success") {
  1036. // We got a success, must have been a synchronous transaction
  1037. var plugindata = json["plugindata"];
  1038. if(plugindata === undefined || plugindata === null) {
  1039. Janus.warn("Request succeeded, but missing plugindata...");
  1040. callbacks.success();
  1041. return;
  1042. }
  1043. Janus.log("Synchronous transaction successful (" + plugindata["plugin"] + ")");
  1044. var data = plugindata["data"];
  1045. Janus.debug(data);
  1046. callbacks.success(data);
  1047. return;
  1048. } else if(json["janus"] !== "ack") {
  1049. // Not a success and not an ack, must be an error
  1050. if(json["error"] !== undefined && json["error"] !== null) {
  1051. Janus.error("Ooops: " + json["error"].code + " " + json["error"].reason); // FIXME
  1052. callbacks.error(json["error"].code + " " + json["error"].reason);
  1053. } else {
  1054. Janus.error("Unknown error"); // FIXME
  1055. callbacks.error("Unknown error");
  1056. }
  1057. return;
  1058. }
  1059. // If we got here, the plugin decided to handle the request asynchronously
  1060. callbacks.success();
  1061. },
  1062. error: function(XMLHttpRequest, textStatus, errorThrown) {
  1063. Janus.error(textStatus + ": " + errorThrown); // FIXME
  1064. callbacks.error(textStatus + ": " + errorThrown);
  1065. },
  1066. dataType: "json"
  1067. });
  1068. }
  1069. // Private method to send a trickle candidate
  1070. function sendTrickleCandidate(handleId, candidate) {
  1071. if(!connected) {
  1072. Janus.warn("Is the gateway down? (connected=false)");
  1073. return;
  1074. }
  1075. var request = { "janus": "trickle", "candidate": candidate, "transaction": Janus.randomString(12) };
  1076. if(token !== null && token !== undefined)
  1077. request["token"] = token;
  1078. if(apisecret !== null && apisecret !== undefined)
  1079. request["apisecret"] = apisecret;
  1080. Janus.vdebug("Sending trickle candidate (handle=" + handleId + "):");
  1081. Janus.vdebug(request);
  1082. if(websockets) {
  1083. request["session_id"] = sessionId;
  1084. request["handle_id"] = handleId;
  1085. ws.send(JSON.stringify(request));
  1086. return;
  1087. }
  1088. Janus.ajax({
  1089. type: 'POST',
  1090. url: server + "/" + sessionId + "/" + handleId,
  1091. withCredentials: withCredentials,
  1092. cache: false,
  1093. contentType: "application/json",
  1094. data: JSON.stringify(request),
  1095. success: function(json) {
  1096. Janus.vdebug("Candidate sent!");
  1097. Janus.vdebug(json);
  1098. if(json["janus"] !== "ack") {
  1099. Janus.error("Ooops: " + json["error"].code + " " + json["error"].reason); // FIXME
  1100. return;
  1101. }
  1102. },
  1103. error: function(XMLHttpRequest, textStatus, errorThrown) {
  1104. Janus.error(textStatus + ": " + errorThrown); // FIXME
  1105. },
  1106. dataType: "json"
  1107. });
  1108. }
  1109. // Private method to send a data channel message
  1110. function sendData(handleId, callbacks) {
  1111. callbacks = callbacks || {};
  1112. callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop;
  1113. callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : Janus.noop;
  1114. var pluginHandle = pluginHandles[handleId];
  1115. if(pluginHandle === null || pluginHandle === undefined ||
  1116. pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) {
  1117. Janus.warn("Invalid handle");
  1118. callbacks.error("Invalid handle");
  1119. return;
  1120. }
  1121. var config = pluginHandle.webrtcStuff;
  1122. var text = callbacks.text;
  1123. if(text === null || text === undefined) {
  1124. Janus.warn("Invalid text");
  1125. callbacks.error("Invalid text");
  1126. return;
  1127. }
  1128. Janus.log("Sending string on data channel: " + text);
  1129. config.dataChannel.send(text);
  1130. callbacks.success();
  1131. }
  1132. // Private method to send a DTMF tone
  1133. function sendDtmf(handleId, callbacks) {
  1134. callbacks = callbacks || {};
  1135. callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop;
  1136. callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : Janus.noop;
  1137. var pluginHandle = pluginHandles[handleId];
  1138. if(pluginHandle === null || pluginHandle === undefined ||
  1139. pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) {
  1140. Janus.warn("Invalid handle");
  1141. callbacks.error("Invalid handle");
  1142. return;
  1143. }
  1144. var config = pluginHandle.webrtcStuff;
  1145. if(config.dtmfSender === null || config.dtmfSender === undefined) {
  1146. // Create the DTMF sender, if possible
  1147. if(config.myStream !== undefined && config.myStream !== null) {
  1148. var tracks = config.myStream.getAudioTracks();
  1149. if(tracks !== null && tracks !== undefined && tracks.length > 0) {
  1150. var local_audio_track = tracks[0];
  1151. config.dtmfSender = config.pc.createDTMFSender(local_audio_track);
  1152. Janus.log("Created DTMF Sender");
  1153. config.dtmfSender.ontonechange = function(tone) { Janus.debug("Sent DTMF tone: " + tone.tone); };
  1154. }
  1155. }
  1156. if(config.dtmfSender === null || config.dtmfSender === undefined) {
  1157. Janus.warn("Invalid DTMF configuration");
  1158. callbacks.error("Invalid DTMF configuration");
  1159. return;
  1160. }
  1161. }
  1162. var dtmf = callbacks.dtmf;
  1163. if(dtmf === null || dtmf === undefined) {
  1164. Janus.warn("Invalid DTMF parameters");
  1165. callbacks.error("Invalid DTMF parameters");
  1166. return;
  1167. }
  1168. var tones = dtmf.tones;
  1169. if(tones === null || tones === undefined) {
  1170. Janus.warn("Invalid DTMF string");
  1171. callbacks.error("Invalid DTMF string");
  1172. return;
  1173. }
  1174. var duration = dtmf.duration;
  1175. if(duration === null || duration === undefined)
  1176. duration = 500; // We choose 500ms as the default duration for a tone
  1177. var gap = dtmf.gap;
  1178. if(gap === null || gap === undefined)
  1179. gap = 50; // We choose 50ms as the default gap between tones
  1180. Janus.debug("Sending DTMF string " + tones + " (duration " + duration + "ms, gap " + gap + "ms)");
  1181. config.dtmfSender.insertDTMF(tones, duration, gap);
  1182. }
  1183. // Private method to destroy a plugin handle
  1184. function destroyHandle(handleId, callbacks) {
  1185. callbacks = callbacks || {};
  1186. callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop;
  1187. callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : Janus.noop;
  1188. var asyncRequest = true;
  1189. if(callbacks.asyncRequest !== undefined && callbacks.asyncRequest !== null)
  1190. asyncRequest = (callbacks.asyncRequest === true);
  1191. Janus.log("Destroying handle " + handleId + " (sync=" + asyncRequest + ")");
  1192. cleanupWebrtc(handleId);
  1193. if (pluginHandles[handleId].detached) {
  1194. // Plugin was already detached by Janus, calling detach again will return a handle not found error, so just exit here
  1195. delete pluginHandles[handleId];
  1196. callbacks.success();
  1197. return;
  1198. }
  1199. if(!connected) {
  1200. Janus.warn("Is the gateway down? (connected=false)");
  1201. callbacks.error("Is the gateway down? (connected=false)");
  1202. return;
  1203. }
  1204. var request = { "janus": "detach", "transaction": Janus.randomString(12) };
  1205. if(token !== null && token !== undefined)
  1206. request["token"] = token;
  1207. if(apisecret !== null && apisecret !== undefined)
  1208. request["apisecret"] = apisecret;
  1209. if(websockets) {
  1210. request["session_id"] = sessionId;
  1211. request["handle_id"] = handleId;
  1212. ws.send(JSON.stringify(request));
  1213. delete pluginHandles[handleId];
  1214. callbacks.success();
  1215. return;
  1216. }
  1217. Janus.ajax({
  1218. type: 'POST',
  1219. url: server + "/" + sessionId + "/" + handleId,
  1220. async: asyncRequest, // Sometimes we need false here, or destroying in onbeforeunload won't work
  1221. withCredentials: withCredentials,
  1222. cache: false,
  1223. contentType: "application/json",
  1224. data: JSON.stringify(request),
  1225. success: function(json) {
  1226. Janus.log("Destroyed handle:");
  1227. Janus.debug(json);
  1228. if(json["janus"] !== "success") {
  1229. Janus.error("Ooops: " + json["error"].code + " " + json["error"].reason); // FIXME
  1230. }
  1231. delete pluginHandles[handleId];
  1232. callbacks.success();
  1233. },
  1234. error: function(XMLHttpRequest, textStatus, errorThrown) {
  1235. Janus.error(textStatus + ": " + errorThrown); // FIXME
  1236. // We cleanup anyway
  1237. delete pluginHandles[handleId];
  1238. callbacks.success();
  1239. },
  1240. dataType: "json"
  1241. });
  1242. }
  1243. // WebRTC stuff
  1244. function streamsDone(handleId, jsep, media, callbacks, stream) {
  1245. var pluginHandle = pluginHandles[handleId];
  1246. if(pluginHandle === null || pluginHandle === undefined ||
  1247. pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) {
  1248. Janus.warn("Invalid handle");
  1249. callbacks.error("Invalid handle");
  1250. return;
  1251. }
  1252. var config = pluginHandle.webrtcStuff;
  1253. Janus.debug("streamsDone:", stream);
  1254. config.myStream = stream;
  1255. var pc_config = {"iceServers": iceServers, "iceTransportPolicy": iceTransportPolicy};
  1256. //~ var pc_constraints = {'mandatory': {'MozDontOfferDataChannel':true}};
  1257. var pc_constraints = {
  1258. "optional": [{"DtlsSrtpKeyAgreement": true}]
  1259. };
  1260. if(ipv6Support === true) {
  1261. // FIXME This is only supported in Chrome right now
  1262. // For support in Firefox track this: https://bugzilla.mozilla.org/show_bug.cgi?id=797262
  1263. pc_constraints.optional.push({"googIPv6":true});
  1264. }
  1265. if(adapter.browserDetails.browser === "edge") {
  1266. // This is Edge, enable BUNDLE explicitly
  1267. pc_config.bundlePolicy = "max-bundle";
  1268. }
  1269. Janus.log("Creating PeerConnection");
  1270. Janus.debug(pc_constraints);
  1271. config.pc = new RTCPeerConnection(pc_config, pc_constraints);
  1272. Janus.debug(config.pc);
  1273. if(config.pc.getStats) { // FIXME
  1274. config.volume.value = 0;
  1275. config.bitrate.value = "0 kbits/sec";
  1276. }
  1277. Janus.log("Preparing local SDP and gathering candidates (trickle=" + config.trickle + ")");
  1278. config.pc.oniceconnectionstatechange = function(e) {
  1279. if(config.pc)
  1280. pluginHandle.iceState(config.pc.iceConnectionState);
  1281. };
  1282. config.pc.onicecandidate = function(event) {
  1283. if (event.candidate == null ||
  1284. (adapter.browserDetails.browser === 'edge' && event.candidate.candidate.indexOf('endOfCandidates') > 0)) {
  1285. Janus.log("End of candidates.");
  1286. config.iceDone = true;
  1287. if(config.trickle === true) {
  1288. // Notify end of candidates
  1289. sendTrickleCandidate(handleId, {"completed": true});
  1290. } else {
  1291. // No trickle, time to send the complete SDP (including all candidates)
  1292. sendSDP(handleId, callbacks);
  1293. }
  1294. } else {
  1295. // JSON.stringify doesn't work on some WebRTC objects anymore
  1296. // See https://code.google.com/p/chromium/issues/detail?id=467366
  1297. var candidate = {
  1298. "candidate": event.candidate.candidate,
  1299. "sdpMid": event.candidate.sdpMid,
  1300. "sdpMLineIndex": event.candidate.sdpMLineIndex
  1301. };
  1302. if(config.trickle === true) {
  1303. // Send candidate
  1304. sendTrickleCandidate(handleId, candidate);
  1305. }
  1306. }
  1307. };
  1308. if(stream !== null && stream !== undefined) {
  1309. Janus.log('Adding local stream');
  1310. config.pc.addStream(stream);
  1311. pluginHandle.onlocalstream(stream);
  1312. }
  1313. config.pc.ontrack = function(remoteStream) {
  1314. Janus.log("Handling Remote Stream");
  1315. Janus.debug(remoteStream);
  1316. config.remoteStream = remoteStream;
  1317. pluginHandle.onremotestream(remoteStream.streams[0]);
  1318. };
  1319. // Any data channel to create?
  1320. if(isDataEnabled(media)) {
  1321. Janus.log("Creating data channel");
  1322. var onDataChannelMessage = function(event) {
  1323. Janus.log('Received message on data channel: ' + event.data);
  1324. pluginHandle.ondata(event.data); // FIXME
  1325. }
  1326. var onDataChannelStateChange = function() {
  1327. var dcState = config.dataChannel !== null ? config.dataChannel.readyState : "null";
  1328. Janus.log('State change on data channel: ' + dcState);
  1329. if(dcState === 'open') {
  1330. pluginHandle.ondataopen(); // FIXME
  1331. }
  1332. }
  1333. var onDataChannelError = function(error) {
  1334. Janus.error('Got error on data channel:', error);
  1335. // TODO
  1336. }
  1337. // Until we implement the proxying of open requests within the Janus core, we open a channel ourselves whatever the case
  1338. config.dataChannel = config.pc.createDataChannel("JanusDataChannel", {ordered:false}); // FIXME Add options (ordered, maxRetransmits, etc.)
  1339. config.dataChannel.onmessage = onDataChannelMessage;
  1340. config.dataChannel.onopen = onDataChannelStateChange;
  1341. config.dataChannel.onclose = onDataChannelStateChange;
  1342. config.dataChannel.onerror = onDataChannelError;
  1343. }
  1344. // Create offer/answer now
  1345. if(jsep === null || jsep === undefined) {
  1346. createOffer(handleId, media, callbacks);
  1347. } else {
  1348. if(adapter.browserDetails.browser === "edge") {
  1349. // This is Edge, add an a=end-of-candidates at the end
  1350. jsep.sdp += "a=end-of-candidates\r\n";
  1351. }
  1352. config.pc.setRemoteDescription(
  1353. new RTCSessionDescription(jsep),
  1354. function() {
  1355. Janus.log("Remote description accepted!");
  1356. createAnswer(handleId, media, callbacks);
  1357. }, callbacks.error);
  1358. }
  1359. }
  1360. function prepareWebrtc(handleId, callbacks) {
  1361. callbacks = callbacks || {};
  1362. callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop;
  1363. callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : webrtcError;
  1364. var jsep = callbacks.jsep;
  1365. var media = callbacks.media;
  1366. var pluginHandle = pluginHandles[handleId];
  1367. if(pluginHandle === null || pluginHandle === undefined ||
  1368. pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) {
  1369. Janus.warn("Invalid handle");
  1370. callbacks.error("Invalid handle");
  1371. return;
  1372. }
  1373. var config = pluginHandle.webrtcStuff;
  1374. // Are we updating a session?
  1375. if(config.pc !== undefined && config.pc !== null) {
  1376. Janus.log("Updating existing media session");
  1377. // Create offer/answer now
  1378. if(jsep === null || jsep === undefined) {
  1379. createOffer(handleId, media, callbacks);
  1380. } else {
  1381. if(adapter.browserDetails.browser === "edge") {
  1382. // This is Edge, add an a=end-of-candidates at the end
  1383. jsep.sdp += "a=end-of-candidates\r\n";
  1384. }
  1385. config.pc.setRemoteDescription(
  1386. new RTCSessionDescription(jsep),
  1387. function() {
  1388. Janus.log("Remote description accepted!");
  1389. createAnswer(handleId, media, callbacks);
  1390. }, callbacks.error);
  1391. }
  1392. return;
  1393. }
  1394. // Was a MediaStream object passed, or do we need to take care of that?
  1395. if(callbacks.stream !== null && callbacks.stream !== undefined) {
  1396. var stream = callbacks.stream;
  1397. Janus.log("MediaStream provided by the application");
  1398. Janus.debug(stream);
  1399. // Skip the getUserMedia part
  1400. config.streamExternal = true;
  1401. streamsDone(handleId, jsep, media, callbacks, stream);
  1402. return;
  1403. }
  1404. config.trickle = isTrickleEnabled(callbacks.trickle);
  1405. if(isAudioSendEnabled(media) || isVideoSendEnabled(media)) {
  1406. var constraints = { mandatory: {}, optional: []};
  1407. pluginHandle.consentDialog(true);
  1408. var audioSupport = isAudioSendEnabled(media);
  1409. if(audioSupport === true && media != undefined && media != null) {
  1410. if(typeof media.audio === 'object') {
  1411. audioSupport = media.audio;
  1412. }
  1413. }
  1414. var videoSupport = isVideoSendEnabled(media);
  1415. if(videoSupport === true && media != undefined && media != null) {
  1416. if(media.video && media.video != 'screen' && media.video != 'window') {
  1417. var width = 0;
  1418. var height = 0, maxHeight = 0;
  1419. if(media.video === 'lowres') {
  1420. // Small resolution, 4:3
  1421. height = 240;
  1422. maxHeight = 240;
  1423. width = 320;
  1424. } else if(media.video === 'lowres-16:9') {
  1425. // Small resolution, 16:9
  1426. height = 180;
  1427. maxHeight = 180;
  1428. width = 320;
  1429. } else if(media.video === 'hires' || media.video === 'hires-16:9' ) {
  1430. // High resolution is only 16:9
  1431. height = 720;
  1432. maxHeight = 720;
  1433. width = 1280;
  1434. if(navigator.mozGetUserMedia) {
  1435. var firefoxVer = parseInt(window.navigator.userAgent.match(/Firefox\/(.*)/)[1], 10);
  1436. if(firefoxVer < 38) {
  1437. // Unless this is and old Firefox, which doesn't support it
  1438. Janus.warn(media.video + " unsupported, falling back to stdres (old Firefox)");
  1439. height = 480;
  1440. maxHeight = 480;
  1441. width = 640;
  1442. }
  1443. }
  1444. } else if(media.video === 'stdres') {
  1445. // Normal resolution, 4:3
  1446. height = 480;
  1447. maxHeight = 480;
  1448. width = 640;
  1449. } else if(media.video === 'stdres-16:9') {
  1450. // Normal resolution, 16:9
  1451. height = 360;
  1452. maxHeight = 360;
  1453. width = 640;
  1454. } else {
  1455. Janus.log("Default video setting (" + media.video + ") is stdres 4:3");
  1456. height = 480;
  1457. maxHeight = 480;
  1458. width = 640;
  1459. }
  1460. Janus.log("Adding media constraint " + media.video);
  1461. if(navigator.mozGetUserMedia) {
  1462. var firefoxVer = parseInt(window.navigator.userAgent.match(/Firefox\/(.*)/)[1], 10);
  1463. if(firefoxVer < 38) {
  1464. videoSupport = {
  1465. 'require': ['height', 'width'],
  1466. 'height': {'max': maxHeight, 'min': height},
  1467. 'width': {'max': width, 'min': width}
  1468. };
  1469. } else {
  1470. // http://stackoverflow.com/questions/28282385/webrtc-firefox-constraints/28911694#28911694
  1471. // https://github.com/meetecho/janus-gateway/pull/246
  1472. videoSupport = {
  1473. 'height': {'ideal': height},
  1474. 'width': {'ideal': width}
  1475. };
  1476. }
  1477. } else {
  1478. videoSupport = {
  1479. 'mandatory': {
  1480. 'maxHeight': maxHeight,
  1481. 'minHeight': height,
  1482. 'maxWidth': width,
  1483. 'minWidth': width
  1484. },
  1485. 'optional': []
  1486. };
  1487. }
  1488. if(typeof media.video === 'object') {
  1489. videoSupport = media.video;
  1490. }
  1491. Janus.debug(videoSupport);
  1492. } else if(media.video === 'screen' || media.video === 'window') {
  1493. if (!media.screenshareFrameRate) {
  1494. media.screenshareFrameRate = 3;
  1495. }
  1496. // Not a webcam, but screen capture
  1497. if(window.location.protocol !== 'https:') {
  1498. // Screen sharing mandates HTTPS
  1499. Janus.warn("Screen sharing only works on HTTPS, try the https:// version of this page");
  1500. pluginHandle.consentDialog(false);
  1501. callbacks.error("Screen sharing only works on HTTPS, try the https:// version of this page");
  1502. return;
  1503. }
  1504. // We're going to try and use the extension for Chrome 34+, the old approach
  1505. // for older versions of Chrome, or the experimental support in Firefox 33+
  1506. var cache = {};
  1507. function callbackUserMedia (error, stream) {
  1508. pluginHandle.consentDialog(false);
  1509. if(error) {
  1510. callbacks.error({code: error.code, name: error.name, message: error.message});
  1511. } else {
  1512. streamsDone(handleId, jsep, media, callbacks, stream);
  1513. }
  1514. };
  1515. function getScreenMedia(constraints, gsmCallback) {
  1516. Janus.log("Adding media constraint (screen capture)");
  1517. Janus.debug(constraints);
  1518. navigator.mediaDevices.getUserMedia(constraints)
  1519. .then(function(stream) { gsmCallback(null, stream); })
  1520. .catch(function(error) { pluginHandle.consentDialog(false); gsmCallback(error); });
  1521. };
  1522. if(adapter.browserDetails.browser === 'chrome') {
  1523. var chromever = adapter.browserDetails.version;
  1524. var maxver = 33;
  1525. if(window.navigator.userAgent.match('Linux'))
  1526. maxver = 35; // "known" crash in chrome 34 and 35 on linux
  1527. if(chromever >= 26 && chromever <= maxver) {
  1528. // Chrome 26->33 requires some awkward chrome://flags manipulation
  1529. constraints = {
  1530. video: {
  1531. mandatory: {
  1532. googLeakyBucket: true,
  1533. maxWidth: window.screen.width,
  1534. maxHeight: window.screen.height,
  1535. minFrameRate: media.screenshareFrameRate,
  1536. maxFrameRate: media.screenshareFrameRate,
  1537. chromeMediaSource: 'screen'
  1538. }
  1539. },
  1540. audio: isAudioSendEnabled(media)
  1541. };
  1542. getScreenMedia(constraints, callbackUserMedia);
  1543. } else {
  1544. // Chrome 34+ requires an extension
  1545. var pending = window.setTimeout(
  1546. function () {
  1547. error = new Error('NavigatorUserMediaError');
  1548. error.name = 'The required Chrome extension is not installed: click <a href="#">here</a> to install it. (NOTE: this will need you to refresh the page)';
  1549. pluginHandle.consentDialog(false);
  1550. return callbacks.error(error);
  1551. }, 1000);
  1552. cache[pending] = [callbackUserMedia, null];
  1553. window.postMessage({ type: 'janusGetScreen', id: pending }, '*');
  1554. }
  1555. } else if (window.navigator.userAgent.match('Firefox')) {
  1556. var ffver = parseInt(window.navigator.userAgent.match(/Firefox\/(.*)/)[1], 10);
  1557. if(ffver >= 33) {
  1558. // Firefox 33+ has experimental support for screen sharing
  1559. constraints = {
  1560. video: {
  1561. mozMediaSource: media.video,
  1562. mediaSource: media.video
  1563. },
  1564. audio: isAudioSendEnabled(media)
  1565. };
  1566. getScreenMedia(constraints, function (err, stream) {
  1567. callbackUserMedia(err, stream);
  1568. // Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=1045810
  1569. if (!err) {
  1570. var lastTime = stream.currentTime;
  1571. var polly = window.setInterval(function () {
  1572. if(!stream)
  1573. window.clearInterval(polly);
  1574. if(stream.currentTime == lastTime) {
  1575. window.clearInterval(polly);
  1576. if(stream.onended) {
  1577. stream.onended();
  1578. }
  1579. }
  1580. lastTime = stream.currentTime;
  1581. }, 500);
  1582. }
  1583. });
  1584. } else {
  1585. var error = new Error('NavigatorUserMediaError');
  1586. error.name = 'Your version of Firefox does not support screen sharing, please install Firefox 33 (or more recent versions)';
  1587. pluginHandle.consentDialog(false);
  1588. callbacks.error(error);
  1589. return;
  1590. }
  1591. }
  1592. // Wait for events from the Chrome Extension
  1593. window.addEventListener('message', function (event) {
  1594. if(event.origin != window.location.origin)
  1595. return;
  1596. if(event.data.type == 'janusGotScreen' && cache[event.data.id]) {
  1597. var data = cache[event.data.id];
  1598. var callback = data[0];
  1599. delete cache[event.data.id];
  1600. if (event.data.sourceId === '') {
  1601. // user canceled
  1602. var error = new Error('NavigatorUserMediaError');
  1603. error.name = 'You cancelled the request for permission, giving up...';
  1604. pluginHandle.consentDialog(false);
  1605. callbacks.error(error);
  1606. } else {
  1607. constraints = {
  1608. audio: isAudioSendEnabled(media),
  1609. video: {
  1610. mandatory: {
  1611. chromeMediaSource: 'desktop',
  1612. maxWidth: window.screen.width,
  1613. maxHeight: window.screen.height,
  1614. minFrameRate: media.screenshareFrameRate,
  1615. maxFrameRate: media.screenshareFrameRate,
  1616. },
  1617. optional: [
  1618. {googLeakyBucket: true},
  1619. {googTemporalLayeredScreencast: true}
  1620. ]
  1621. }
  1622. };
  1623. constraints.video.mandatory.chromeMediaSourceId = event.data.sourceId;
  1624. getScreenMedia(constraints, callback);
  1625. }
  1626. } else if (event.data.type == 'janusGetScreenPending') {
  1627. window.clearTimeout(event.data.id);
  1628. }
  1629. });
  1630. return;
  1631. }
  1632. }
  1633. // If we got here, we're not screensharing
  1634. if(media === null || media === undefined || media.video !== 'screen') {
  1635. // Check whether all media sources are actually available or not
  1636. navigator.mediaDevices.enumerateDevices().then(function(devices) {
  1637. var audioExist = devices.some(function(device) {
  1638. return device.kind === 'audioinput';
  1639. }),
  1640. videoExist = devices.some(function(device) {
  1641. return device.kind === 'videoinput';
  1642. });
  1643. // Check whether a missing device is really a problem
  1644. var audioSend = isAudioSendEnabled(media);
  1645. var videoSend = isVideoSendEnabled(media);
  1646. var needAudioDevice = isAudioSendRequired(media);
  1647. var needVideoDevice = isVideoSendRequired(media);
  1648. if(audioSend || videoSend || needAudioDevice || needVideoDevice) {
  1649. // We need to send either audio or video
  1650. var haveAudioDevice = audioSend ? audioExist : false;
  1651. var haveVideoDevice = videoSend ? videoExist : false;
  1652. if(!haveAudioDevice && !haveVideoDevice) {
  1653. // FIXME Should we really give up, or just assume recvonly for both?
  1654. pluginHandle.consentDialog(false);
  1655. callbacks.error('No capture device found');
  1656. return false;
  1657. } else if(!haveAudioDevice && needAudioDevice) {
  1658. pluginHandle.consentDialog(false);
  1659. callbacks.error('Audio capture is required, but no capture device found');
  1660. return false;
  1661. } else if(!haveVideoDevice && needVideoDevice) {
  1662. pluginHandle.consentDialog(false);
  1663. callbacks.error('Video capture is required, but no capture device found');
  1664. return false;
  1665. }
  1666. }
  1667. navigator.mediaDevices.getUserMedia({
  1668. audio: audioExist ? audioSupport : false,
  1669. video: videoExist ? videoSupport : false
  1670. })
  1671. .then(function(stream) { pluginHandle.consentDialog(false); streamsDone(handleId, jsep, media, callbacks, stream); })
  1672. .catch(function(error) { pluginHandle.consentDialog(false); callbacks.error({code: error.code, name: error.name, message: error.message}); });
  1673. })
  1674. .catch(function(error) {
  1675. pluginHandle.consentDialog(false);
  1676. callbacks.error('enumerateDevices error', error);
  1677. });
  1678. }
  1679. } else {
  1680. // No need to do a getUserMedia, create offer/answer right away
  1681. streamsDone(handleId, jsep, media, callbacks);
  1682. }
  1683. }
  1684. function prepareWebrtcPeer(handleId, callbacks) {
  1685. callbacks = callbacks || {};
  1686. callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop;
  1687. callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : webrtcError;
  1688. var jsep = callbacks.jsep;
  1689. var pluginHandle = pluginHandles[handleId];
  1690. if(pluginHandle === null || pluginHandle === undefined ||
  1691. pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) {
  1692. Janus.warn("Invalid handle");
  1693. callbacks.error("Invalid handle");
  1694. return;
  1695. }
  1696. var config = pluginHandle.webrtcStuff;
  1697. if(jsep !== undefined && jsep !== null) {
  1698. if(config.pc === null) {
  1699. Janus.warn("Wait, no PeerConnection?? if this is an answer, use createAnswer and not handleRemoteJsep");
  1700. callbacks.error("No PeerConnection: if this is an answer, use createAnswer and not handleRemoteJsep");
  1701. return;
  1702. }
  1703. if(adapter.browserDetails.browser === "edge") {
  1704. // This is Edge, add an a=end-of-candidates at the end
  1705. jsep.sdp += "a=end-of-candidates\r\n";
  1706. }
  1707. config.pc.setRemoteDescription(
  1708. new RTCSessionDescription(jsep),
  1709. function() {
  1710. Janus.log("Remote description accepted!");
  1711. callbacks.success();
  1712. }, callbacks.error);
  1713. } else {
  1714. callbacks.error("Invalid JSEP");
  1715. }
  1716. }
  1717. function createOffer(handleId, media, callbacks) {
  1718. callbacks = callbacks || {};
  1719. callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop;
  1720. callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : Janus.noop;
  1721. var pluginHandle = pluginHandles[handleId];
  1722. if(pluginHandle === null || pluginHandle === undefined ||
  1723. pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) {
  1724. Janus.warn("Invalid handle");
  1725. callbacks.error("Invalid handle");
  1726. return;
  1727. }
  1728. var config = pluginHandle.webrtcStuff;
  1729. Janus.log("Creating offer (iceDone=" + config.iceDone + ")");
  1730. // https://code.google.com/p/webrtc/issues/detail?id=3508
  1731. var mediaConstraints = null;
  1732. if(adapter.browserDetails.browser == "firefox" || adapter.browserDetails.browser == "edge") {
  1733. mediaConstraints = {
  1734. 'offerToReceiveAudio':isAudioRecvEnabled(media),
  1735. 'offerToReceiveVideo':isVideoRecvEnabled(media)
  1736. };
  1737. } else {
  1738. mediaConstraints = {
  1739. 'mandatory': {
  1740. 'OfferToReceiveAudio':isAudioRecvEnabled(media),
  1741. 'OfferToReceiveVideo':isVideoRecvEnabled(media)
  1742. }
  1743. };
  1744. }
  1745. Janus.debug(mediaConstraints);
  1746. config.pc.createOffer(
  1747. function(offer) {
  1748. Janus.debug(offer);
  1749. if(config.mySdp === null || config.mySdp === undefined) {
  1750. Janus.log("Setting local description");
  1751. config.mySdp = offer.sdp;
  1752. config.pc.setLocalDescription(offer);
  1753. }
  1754. if(!config.iceDone && !config.trickle) {
  1755. // Don't do anything until we have all candidates
  1756. Janus.log("Waiting for all candidates...");
  1757. return;
  1758. }
  1759. if(config.sdpSent) {
  1760. Janus.log("Offer already sent, not sending it again");
  1761. return;
  1762. }
  1763. Janus.log("Offer ready");
  1764. Janus.debug(callbacks);
  1765. config.sdpSent = true;
  1766. // JSON.stringify doesn't work on some WebRTC objects anymore
  1767. // See https://code.google.com/p/chromium/issues/detail?id=467366
  1768. var jsep = {
  1769. "type": offer.type,
  1770. "sdp": offer.sdp
  1771. };
  1772. callbacks.success(jsep);
  1773. }, callbacks.error, mediaConstraints);
  1774. }
  1775. function createAnswer(handleId, media, callbacks) {
  1776. callbacks = callbacks || {};
  1777. callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop;
  1778. callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : Janus.noop;
  1779. var pluginHandle = pluginHandles[handleId];
  1780. if(pluginHandle === null || pluginHandle === undefined ||
  1781. pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) {
  1782. Janus.warn("Invalid handle");
  1783. callbacks.error("Invalid handle");
  1784. return;
  1785. }
  1786. var config = pluginHandle.webrtcStuff;
  1787. Janus.log("Creating answer (iceDone=" + config.iceDone + ")");
  1788. var mediaConstraints = null;
  1789. if(adapter.browserDetails.browser == "firefox" || adapter.browserDetails.browser == "edge") {
  1790. mediaConstraints = {
  1791. 'offerToReceiveAudio':isAudioRecvEnabled(media),
  1792. 'offerToReceiveVideo':isVideoRecvEnabled(media)
  1793. };
  1794. } else {
  1795. mediaConstraints = {
  1796. 'mandatory': {
  1797. 'OfferToReceiveAudio':isAudioRecvEnabled(media),
  1798. 'OfferToReceiveVideo':isVideoRecvEnabled(media)
  1799. }
  1800. };
  1801. }
  1802. Janus.debug(mediaConstraints);
  1803. config.pc.createAnswer(
  1804. function(answer) {
  1805. Janus.debug(answer);
  1806. if(config.mySdp === null || config.mySdp === undefined) {
  1807. Janus.log("Setting local description");
  1808. config.mySdp = answer.sdp;
  1809. config.pc.setLocalDescription(answer);
  1810. }
  1811. if(!config.iceDone && !config.trickle) {
  1812. // Don't do anything until we have all candidates
  1813. Janus.log("Waiting for all candidates...");
  1814. return;
  1815. }
  1816. if(config.sdpSent) { // FIXME badly
  1817. Janus.log("Answer already sent, not sending it again");
  1818. return;
  1819. }
  1820. config.sdpSent = true;
  1821. // JSON.stringify doesn't work on some WebRTC objects anymore
  1822. // See https://code.google.com/p/chromium/issues/detail?id=467366
  1823. var jsep = {
  1824. "type": answer.type,
  1825. "sdp": answer.sdp
  1826. };
  1827. callbacks.success(jsep);
  1828. }, callbacks.error, mediaConstraints);
  1829. }
  1830. function sendSDP(handleId, callbacks) {
  1831. callbacks = callbacks || {};
  1832. callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop;
  1833. callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : Janus.noop;
  1834. var pluginHandle = pluginHandles[handleId];
  1835. if(pluginHandle === null || pluginHandle === undefined ||
  1836. pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) {
  1837. Janus.warn("Invalid handle, not sending anything");
  1838. return;
  1839. }
  1840. var config = pluginHandle.webrtcStuff;
  1841. Janus.log("Sending offer/answer SDP...");
  1842. if(config.mySdp === null || config.mySdp === undefined) {
  1843. Janus.warn("Local SDP instance is invalid, not sending anything...");
  1844. return;
  1845. }
  1846. config.mySdp = {
  1847. "type": config.pc.localDescription.type,
  1848. "sdp": config.pc.localDescription.sdp
  1849. };
  1850. if(config.sdpSent) {
  1851. Janus.log("Offer/Answer SDP already sent, not sending it again");
  1852. return;
  1853. }
  1854. if(config.trickle === false)
  1855. config.mySdp["trickle"] = false;
  1856. Janus.debug(callbacks);
  1857. config.sdpSent = true;
  1858. callbacks.success(config.mySdp);
  1859. }
  1860. function getVolume(handleId) {
  1861. var pluginHandle = pluginHandles[handleId];
  1862. if(pluginHandle === null || pluginHandle === undefined ||
  1863. pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) {
  1864. Janus.warn("Invalid handle");
  1865. return 0;
  1866. }
  1867. var config = pluginHandle.webrtcStuff;
  1868. // Start getting the volume, if getStats is supported
  1869. if(config.pc.getStats && adapter.browserDetails.browser == "chrome") { // FIXME
  1870. if(config.remoteStream === null || config.remoteStream === undefined) {
  1871. Janus.warn("Remote stream unavailable");
  1872. return 0;
  1873. }
  1874. // http://webrtc.googlecode.com/svn/trunk/samples/js/demos/html/constraints-and-stats.html
  1875. if(config.volume.timer === null || config.volume.timer === undefined) {
  1876. Janus.log("Starting volume monitor");
  1877. config.volume.timer = setInterval(function() {
  1878. config.pc.getStats(function(stats) {
  1879. var results = stats.result();
  1880. for(var i=0; i<results.length; i++) {
  1881. var res = results[i];
  1882. if(res.type == 'ssrc' && res.stat('audioOutputLevel')) {
  1883. config.volume.value = res.stat('audioOutputLevel');
  1884. }
  1885. }
  1886. });
  1887. }, 200);
  1888. return 0; // We don't have a volume to return yet
  1889. }
  1890. return config.volume.value;
  1891. } else {
  1892. Janus.log("Getting the remote volume unsupported by browser");
  1893. return 0;
  1894. }
  1895. }
  1896. function isMuted(handleId, video) {
  1897. var pluginHandle = pluginHandles[handleId];
  1898. if(pluginHandle === null || pluginHandle === undefined ||
  1899. pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) {
  1900. Janus.warn("Invalid handle");
  1901. return true;
  1902. }
  1903. var config = pluginHandle.webrtcStuff;
  1904. if(config.pc === null || config.pc === undefined) {
  1905. Janus.warn("Invalid PeerConnection");
  1906. return true;
  1907. }
  1908. if(config.myStream === undefined || config.myStream === null) {
  1909. Janus.warn("Invalid local MediaStream");
  1910. return true;
  1911. }
  1912. if(video) {
  1913. // Check video track
  1914. if(config.myStream.getVideoTracks() === null
  1915. || config.myStream.getVideoTracks() === undefined
  1916. || config.myStream.getVideoTracks().length === 0) {
  1917. Janus.warn("No video track");
  1918. return true;
  1919. }
  1920. return !config.myStream.getVideoTracks()[0].enabled;
  1921. } else {
  1922. // Check audio track
  1923. if(config.myStream.getAudioTracks() === null
  1924. || config.myStream.getAudioTracks() === undefined
  1925. || config.myStream.getAudioTracks().length === 0) {
  1926. Janus.warn("No audio track");
  1927. return true;
  1928. }
  1929. return !config.myStream.getAudioTracks()[0].enabled;
  1930. }
  1931. }
  1932. function mute(handleId, video, mute) {
  1933. var pluginHandle = pluginHandles[handleId];
  1934. if(pluginHandle === null || pluginHandle === undefined ||
  1935. pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) {
  1936. Janus.warn("Invalid handle");
  1937. return false;
  1938. }
  1939. var config = pluginHandle.webrtcStuff;
  1940. if(config.pc === null || config.pc === undefined) {
  1941. Janus.warn("Invalid PeerConnection");
  1942. return false;
  1943. }
  1944. if(config.myStream === undefined || config.myStream === null) {
  1945. Janus.warn("Invalid local MediaStream");
  1946. return false;
  1947. }
  1948. if(video) {
  1949. // Mute/unmute video track
  1950. if(config.myStream.getVideoTracks() === null
  1951. || config.myStream.getVideoTracks() === undefined
  1952. || config.myStream.getVideoTracks().length === 0) {
  1953. Janus.warn("No video track");
  1954. return false;
  1955. }
  1956. config.myStream.getVideoTracks()[0].enabled = mute ? false : true;
  1957. return true;
  1958. } else {
  1959. // Mute/unmute audio track
  1960. if(config.myStream.getAudioTracks() === null
  1961. || config.myStream.getAudioTracks() === undefined
  1962. || config.myStream.getAudioTracks().length === 0) {
  1963. Janus.warn("No audio track");
  1964. return false;
  1965. }
  1966. config.myStream.getAudioTracks()[0].enabled = mute ? false : true;
  1967. return true;
  1968. }
  1969. }
  1970. function getBitrate(handleId) {
  1971. var pluginHandle = pluginHandles[handleId];
  1972. if(pluginHandle === null || pluginHandle === undefined ||
  1973. pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) {
  1974. Janus.warn("Invalid handle");
  1975. return "Invalid handle";
  1976. }
  1977. var config = pluginHandle.webrtcStuff;
  1978. if(config.pc === null || config.pc === undefined)
  1979. return "Invalid PeerConnection";
  1980. // Start getting the bitrate, if getStats is supported
  1981. if(config.pc.getStats && adapter.browserDetails.browser == "chrome") {
  1982. // Do it the Chrome way
  1983. if(config.remoteStream === null || config.remoteStream === undefined) {
  1984. Janus.warn("Remote stream unavailable");
  1985. return "Remote stream unavailable";
  1986. }
  1987. // http://webrtc.googlecode.com/svn/trunk/samples/js/demos/html/constraints-and-stats.html
  1988. if(config.bitrate.timer === null || config.bitrate.timer === undefined) {
  1989. Janus.log("Starting bitrate timer (Chrome)");
  1990. config.bitrate.timer = setInterval(function() {
  1991. config.pc.getStats(function(stats) {
  1992. var results = stats.result();
  1993. for(var i=0; i<results.length; i++) {
  1994. var res = results[i];
  1995. if(res.type == 'ssrc' && res.stat('googFrameHeightReceived')) {
  1996. config.bitrate.bsnow = res.stat('bytesReceived');
  1997. config.bitrate.tsnow = res.timestamp;
  1998. if(config.bitrate.bsbefore === null || config.bitrate.tsbefore === null) {
  1999. // Skip this round
  2000. config.bitrate.bsbefore = config.bitrate.bsnow;
  2001. config.bitrate.tsbefore = config.bitrate.tsnow;
  2002. } else {
  2003. // Calculate bitrate
  2004. var bitRate = Math.round((config.bitrate.bsnow - config.bitrate.bsbefore) * 8 / (config.bitrate.tsnow - config.bitrate.tsbefore));
  2005. config.bitrate.value = bitRate + ' kbits/sec';
  2006. //~ Janus.log("Estimated bitrate is " + config.bitrate.value);
  2007. config.bitrate.bsbefore = config.bitrate.bsnow;
  2008. config.bitrate.tsbefore = config.bitrate.tsnow;
  2009. }
  2010. }
  2011. }
  2012. });
  2013. }, 1000);
  2014. return "0 kbits/sec"; // We don't have a bitrate value yet
  2015. }
  2016. return config.bitrate.value;
  2017. } else if(config.pc.getStats && adapter.browserDetails.browser == "firefox") {
  2018. // Do it the Firefox way
  2019. if(config.remoteStream === null || config.remoteStream === undefined
  2020. || config.remoteStream.streams[0] === null || config.remoteStream.streams[0] === undefined) {
  2021. Janus.warn("Remote stream unavailable");
  2022. return "Remote stream unavailable";
  2023. }
  2024. var videoTracks = config.remoteStream.streams[0].getVideoTracks();
  2025. if(videoTracks === null || videoTracks === undefined || videoTracks.length < 1) {
  2026. Janus.warn("No video track");
  2027. return "No video track";
  2028. }
  2029. // https://github.com/muaz-khan/getStats/blob/master/getStats.js
  2030. if(config.bitrate.timer === null || config.bitrate.timer === undefined) {
  2031. Janus.log("Starting bitrate timer (Firefox)");
  2032. config.bitrate.timer = setInterval(function() {
  2033. // We need a helper callback
  2034. var cb = function(res) {
  2035. if(res === null || res === undefined ||
  2036. res.inbound_rtp_video_1 == null || res.inbound_rtp_video_1 == null) {
  2037. config.bitrate.value = "Missing inbound_rtp_video_1";
  2038. return;
  2039. }
  2040. config.bitrate.bsnow = res.inbound_rtp_video_1.bytesReceived;
  2041. config.bitrate.tsnow = res.inbound_rtp_video_1.timestamp;
  2042. if(config.bitrate.bsbefore === null || config.bitrate.tsbefore === null) {
  2043. // Skip this round
  2044. config.bitrate.bsbefore = config.bitrate.bsnow;
  2045. config.bitrate.tsbefore = config.bitrate.tsnow;
  2046. } else {
  2047. // Calculate bitrate
  2048. var bitRate = Math.round((config.bitrate.bsnow - config.bitrate.bsbefore) * 8 / (config.bitrate.tsnow - config.bitrate.tsbefore));
  2049. config.bitrate.value = bitRate + ' kbits/sec';
  2050. config.bitrate.bsbefore = config.bitrate.bsnow;
  2051. config.bitrate.tsbefore = config.bitrate.tsnow;
  2052. }
  2053. };
  2054. // Actually get the stats
  2055. config.pc.getStats(videoTracks[0], function(stats) {
  2056. cb(stats);
  2057. }, cb);
  2058. }, 1000);
  2059. return "0 kbits/sec"; // We don't have a bitrate value yet
  2060. }
  2061. return config.bitrate.value;
  2062. } else {
  2063. Janus.warn("Getting the video bitrate unsupported by browser");
  2064. return "Feature unsupported by browser";
  2065. }
  2066. }
  2067. function webrtcError(error) {
  2068. Janus.error("WebRTC error:", error);
  2069. }
  2070. function cleanupWebrtc(handleId, hangupRequest) {
  2071. Janus.log("Cleaning WebRTC stuff");
  2072. var pluginHandle = pluginHandles[handleId];
  2073. if(pluginHandle === null || pluginHandle === undefined) {
  2074. // Nothing to clean
  2075. return;
  2076. }
  2077. var config = pluginHandle.webrtcStuff;
  2078. if(config !== null && config !== undefined) {
  2079. if(hangupRequest === true) {
  2080. // Send a hangup request (we don't really care about the response)
  2081. var request = { "janus": "hangup", "transaction": Janus.randomString(12) };
  2082. if(token !== null && token !== undefined)
  2083. request["token"] = token;
  2084. if(apisecret !== null && apisecret !== undefined)
  2085. request["apisecret"] = apisecret;
  2086. Janus.debug("Sending hangup request (handle=" + handleId + "):");
  2087. Janus.debug(request);
  2088. if(websockets) {
  2089. request["session_id"] = sessionId;
  2090. request["handle_id"] = handleId;
  2091. ws.send(JSON.stringify(request));
  2092. } else {
  2093. Janus.ajax({
  2094. type: 'POST',
  2095. url: server + "/" + sessionId + "/" + handleId,
  2096. withCredentials: withCredentials,
  2097. cache: false,
  2098. contentType: "application/json",
  2099. data: JSON.stringify(request),
  2100. dataType: "json"
  2101. });
  2102. }
  2103. }
  2104. // Cleanup stack
  2105. config.remoteStream = null;
  2106. if(config.volume.timer)
  2107. clearInterval(config.volume.timer);
  2108. config.volume.value = null;
  2109. if(config.bitrate.timer)
  2110. clearInterval(config.bitrate.timer);
  2111. config.bitrate.timer = null;
  2112. config.bitrate.bsnow = null;
  2113. config.bitrate.bsbefore = null;
  2114. config.bitrate.tsnow = null;
  2115. config.bitrate.tsbefore = null;
  2116. config.bitrate.value = null;
  2117. try {
  2118. // Try a MediaStream.stop() first
  2119. if(!config.streamExternal && config.myStream !== null && config.myStream !== undefined) {
  2120. Janus.log("Stopping local stream");
  2121. config.myStream.stop();
  2122. }
  2123. } catch(e) {
  2124. // Do nothing if this fails
  2125. }
  2126. try {
  2127. // Try a MediaStreamTrack.stop() for each track as well
  2128. if(!config.streamExternal && config.myStream !== null && config.myStream !== undefined) {
  2129. Janus.log("Stopping local stream tracks");
  2130. var tracks = config.myStream.getTracks();
  2131. for(var i in tracks) {
  2132. var mst = tracks[i];
  2133. Janus.log(mst);
  2134. if(mst !== null && mst !== undefined)
  2135. mst.stop();
  2136. }
  2137. }
  2138. } catch(e) {
  2139. // Do nothing if this fails
  2140. }
  2141. config.streamExternal = false;
  2142. config.myStream = null;
  2143. // Close PeerConnection
  2144. try {
  2145. config.pc.close();
  2146. } catch(e) {
  2147. // Do nothing
  2148. }
  2149. config.pc = null;
  2150. config.mySdp = null;
  2151. config.iceDone = false;
  2152. config.sdpSent = false;
  2153. config.dataChannel = null;
  2154. config.dtmfSender = null;
  2155. }
  2156. pluginHandle.oncleanup();
  2157. }
  2158. // Helper methods to parse a media object
  2159. function isAudioSendEnabled(media) {
  2160. Janus.debug("isAudioSendEnabled:", media);
  2161. if(media === undefined || media === null)
  2162. return true; // Default
  2163. if(media.audio === false)
  2164. return false; // Generic audio has precedence
  2165. if(media.audioSend === undefined || media.audioSend === null)
  2166. return true; // Default
  2167. return (media.audioSend === true);
  2168. }
  2169. function isAudioSendRequired(media) {
  2170. Janus.debug("isAudioSendRequired:", media);
  2171. if(media === undefined || media === null)
  2172. return false; // Default
  2173. if(media.audio === false || media.audioSend === false)
  2174. return false; // If we're not asking to capture audio, it's not required
  2175. if(media.failIfNoAudio === undefined || media.failIfNoAudio === null)
  2176. return false; // Default
  2177. return (media.failIfNoAudio === true);
  2178. }
  2179. function isAudioRecvEnabled(media) {
  2180. Janus.debug("isAudioRecvEnabled:", media);
  2181. if(media === undefined || media === null)
  2182. return true; // Default
  2183. if(media.audio === false)
  2184. return false; // Generic audio has precedence
  2185. if(media.audioRecv === undefined || media.audioRecv === null)
  2186. return true; // Default
  2187. return (media.audioRecv === true);
  2188. }
  2189. function isVideoSendEnabled(media) {
  2190. Janus.debug("isVideoSendEnabled:", media);
  2191. if(media === undefined || media === null)
  2192. return true; // Default
  2193. if(media.video === false)
  2194. return false; // Generic video has precedence
  2195. if(media.videoSend === undefined || media.videoSend === null)
  2196. return true; // Default
  2197. return (media.videoSend === true);
  2198. }
  2199. function isVideoSendRequired(media) {
  2200. Janus.debug("isVideoSendRequired:", media);
  2201. if(media === undefined || media === null)
  2202. return false; // Default
  2203. if(media.video === false || media.videoSend === false)
  2204. return false; // If we're not asking to capture video, it's not required
  2205. if(media.failIfNoVideo === undefined || media.failIfNoVideo === null)
  2206. return false; // Default
  2207. return (media.failIfNoVideo === true);
  2208. }
  2209. function isVideoRecvEnabled(media) {
  2210. Janus.debug("isVideoRecvEnabled:", media);
  2211. if(media === undefined || media === null)
  2212. return true; // Default
  2213. if(media.video === false)
  2214. return false; // Generic video has precedence
  2215. if(media.videoRecv === undefined || media.videoRecv === null)
  2216. return true; // Default
  2217. return (media.videoRecv === true);
  2218. }
  2219. function isDataEnabled(media) {
  2220. Janus.debug("isDataEnabled:", media);
  2221. if(adapter.browserDetails.browser == "edge") {
  2222. Janus.warn("Edge doesn't support data channels yet");
  2223. return false;
  2224. }
  2225. if(media === undefined || media === null)
  2226. return false; // Default
  2227. return (media.data === true);
  2228. }
  2229. function isTrickleEnabled(trickle) {
  2230. Janus.debug("isTrickleEnabled:", trickle);
  2231. if(trickle === undefined || trickle === null)
  2232. return true; // Default is true
  2233. return (trickle === true);
  2234. }
  2235. };