You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

desktopsharing.bundle.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.desktopsharing=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. /* global $, alert, changeLocalVideo, chrome, config, getConferenceHandler, getUserMediaWithConstraints */
  3. /**
  4. * Indicates that desktop stream is currently in use(for toggle purpose).
  5. * @type {boolean}
  6. */
  7. var isUsingScreenStream = false;
  8. /**
  9. * Indicates that switch stream operation is in progress and prevent from triggering new events.
  10. * @type {boolean}
  11. */
  12. var switchInProgress = false;
  13. /**
  14. * Method used to get screen sharing stream.
  15. *
  16. * @type {function (stream_callback, failure_callback}
  17. */
  18. var obtainDesktopStream = null;
  19. /**
  20. * Flag used to cache desktop sharing enabled state. Do not use directly as it can be <tt>null</tt>.
  21. * @type {null|boolean}
  22. */
  23. var _desktopSharingEnabled = null;
  24. var EventEmitter = require("events");
  25. var eventEmitter = new EventEmitter();
  26. /**
  27. * Method obtains desktop stream from WebRTC 'screen' source.
  28. * Flag 'chrome://flags/#enable-usermedia-screen-capture' must be enabled.
  29. */
  30. function obtainWebRTCScreen(streamCallback, failCallback) {
  31. RTC.getUserMediaWithConstraints(
  32. ['screen'],
  33. streamCallback,
  34. failCallback
  35. );
  36. }
  37. /**
  38. * Constructs inline install URL for Chrome desktop streaming extension.
  39. * The 'chromeExtensionId' must be defined in config.js.
  40. * @returns {string}
  41. */
  42. function getWebStoreInstallUrl()
  43. {
  44. return "https://chrome.google.com/webstore/detail/" + config.chromeExtensionId;
  45. }
  46. /**
  47. * Checks whether extension update is required.
  48. * @param minVersion minimal required version
  49. * @param extVersion current extension version
  50. * @returns {boolean}
  51. */
  52. function isUpdateRequired(minVersion, extVersion)
  53. {
  54. try
  55. {
  56. var s1 = minVersion.split('.');
  57. var s2 = extVersion.split('.');
  58. var len = Math.max(s1.length, s2.length);
  59. for (var i = 0; i < len; i++)
  60. {
  61. var n1 = 0,
  62. n2 = 0;
  63. if (i < s1.length)
  64. n1 = parseInt(s1[i]);
  65. if (i < s2.length)
  66. n2 = parseInt(s2[i]);
  67. if (isNaN(n1) || isNaN(n2))
  68. {
  69. return true;
  70. }
  71. else if (n1 !== n2)
  72. {
  73. return n1 > n2;
  74. }
  75. }
  76. // will happen if boths version has identical numbers in
  77. // their components (even if one of them is longer, has more components)
  78. return false;
  79. }
  80. catch (e)
  81. {
  82. console.error("Failed to parse extension version", e);
  83. UI.messageHandler.showError('Error',
  84. 'Error when trying to detect desktopsharing extension.');
  85. return true;
  86. }
  87. }
  88. function checkExtInstalled(isInstalledCallback) {
  89. if (!chrome.runtime) {
  90. // No API, so no extension for sure
  91. isInstalledCallback(false);
  92. return;
  93. }
  94. chrome.runtime.sendMessage(
  95. config.chromeExtensionId,
  96. { getVersion: true },
  97. function (response) {
  98. if (!response || !response.version) {
  99. // Communication failure - assume that no endpoint exists
  100. console.warn("Extension not installed?: " + chrome.runtime.lastError);
  101. isInstalledCallback(false);
  102. } else {
  103. // Check installed extension version
  104. var extVersion = response.version;
  105. console.log('Extension version is: ' + extVersion);
  106. var updateRequired = isUpdateRequired(config.minChromeExtVersion, extVersion);
  107. if (updateRequired) {
  108. alert(
  109. 'Jitsi Desktop Streamer requires update. ' +
  110. 'Changes will take effect after next Chrome restart.');
  111. }
  112. isInstalledCallback(!updateRequired);
  113. }
  114. }
  115. );
  116. }
  117. function doGetStreamFromExtension(streamCallback, failCallback) {
  118. // Sends 'getStream' msg to the extension. Extension id must be defined in the config.
  119. chrome.runtime.sendMessage(
  120. config.chromeExtensionId,
  121. { getStream: true, sources: config.desktopSharingSources },
  122. function (response) {
  123. if (!response) {
  124. failCallback(chrome.runtime.lastError);
  125. return;
  126. }
  127. console.log("Response from extension: " + response);
  128. if (response.streamId) {
  129. RTC.getUserMediaWithConstraints(
  130. ['desktop'],
  131. function (stream) {
  132. streamCallback(stream);
  133. },
  134. failCallback,
  135. null, null, null,
  136. response.streamId);
  137. } else {
  138. failCallback("Extension failed to get the stream");
  139. }
  140. }
  141. );
  142. }
  143. /**
  144. * Asks Chrome extension to call chooseDesktopMedia and gets chrome 'desktop' stream for returned stream token.
  145. */
  146. function obtainScreenFromExtension(streamCallback, failCallback) {
  147. checkExtInstalled(
  148. function (isInstalled) {
  149. if (isInstalled) {
  150. doGetStreamFromExtension(streamCallback, failCallback);
  151. } else {
  152. chrome.webstore.install(
  153. getWebStoreInstallUrl(),
  154. function (arg) {
  155. console.log("Extension installed successfully", arg);
  156. // We need to reload the page in order to get the access to chrome.runtime
  157. window.location.reload(false);
  158. },
  159. function (arg) {
  160. console.log("Failed to install the extension", arg);
  161. failCallback(arg);
  162. UI.messageHandler.showError('Error',
  163. 'Failed to install desktop sharing extension');
  164. }
  165. );
  166. }
  167. }
  168. );
  169. }
  170. /**
  171. * Call this method to toggle desktop sharing feature.
  172. * @param method pass "ext" to use chrome extension for desktop capture(chrome extension required),
  173. * pass "webrtc" to use WebRTC "screen" desktop source('chrome://flags/#enable-usermedia-screen-capture'
  174. * must be enabled), pass any other string or nothing in order to disable this feature completely.
  175. */
  176. function setDesktopSharing(method) {
  177. // Check if we are running chrome
  178. if (!navigator.webkitGetUserMedia) {
  179. obtainDesktopStream = null;
  180. console.info("Desktop sharing disabled");
  181. } else if (method == "ext") {
  182. obtainDesktopStream = obtainScreenFromExtension;
  183. console.info("Using Chrome extension for desktop sharing");
  184. } else if (method == "webrtc") {
  185. obtainDesktopStream = obtainWebRTCScreen;
  186. console.info("Using Chrome WebRTC for desktop sharing");
  187. }
  188. // Reset enabled cache
  189. _desktopSharingEnabled = null;
  190. }
  191. /**
  192. * Initializes <link rel=chrome-webstore-item /> with extension id set in config.js to support inline installs.
  193. * Host site must be selected as main website of published extension.
  194. */
  195. function initInlineInstalls()
  196. {
  197. $("link[rel=chrome-webstore-item]").attr("href", getWebStoreInstallUrl());
  198. }
  199. function getSwitchStreamFailed(error) {
  200. console.error("Failed to obtain the stream to switch to", error);
  201. switchInProgress = false;
  202. }
  203. function streamSwitchDone() {
  204. switchInProgress = false;
  205. eventEmitter.emit(
  206. DesktopSharingEventTypes.SWITCHING_DONE,
  207. isUsingScreenStream);
  208. }
  209. function newStreamCreated(stream)
  210. {
  211. eventEmitter.emit(DesktopSharingEventTypes.NEW_STREAM_CREATED,
  212. stream, isUsingScreenStream, streamSwitchDone);
  213. }
  214. module.exports = {
  215. isUsingScreenStream: function () {
  216. return isUsingScreenStream;
  217. },
  218. /**
  219. * @returns {boolean} <tt>true</tt> if desktop sharing feature is available and enabled.
  220. */
  221. isDesktopSharingEnabled: function () {
  222. if (_desktopSharingEnabled === null) {
  223. if (obtainDesktopStream === obtainScreenFromExtension) {
  224. // Parse chrome version
  225. var userAgent = navigator.userAgent.toLowerCase();
  226. // We can assume that user agent is chrome, because it's enforced when 'ext' streaming method is set
  227. var ver = parseInt(userAgent.match(/chrome\/(\d+)\./)[1], 10);
  228. console.log("Chrome version" + userAgent, ver);
  229. _desktopSharingEnabled = ver >= 34;
  230. } else {
  231. _desktopSharingEnabled = obtainDesktopStream === obtainWebRTCScreen;
  232. }
  233. }
  234. return _desktopSharingEnabled;
  235. },
  236. init: function () {
  237. setDesktopSharing(config.desktopSharing);
  238. // Initialize Chrome extension inline installs
  239. if (config.chromeExtensionId) {
  240. initInlineInstalls();
  241. }
  242. eventEmitter.emit(DesktopSharingEventTypes.INIT);
  243. },
  244. addListener: function(listener, type)
  245. {
  246. eventEmitter.on(type, listener);
  247. },
  248. removeListener: function (listener,type) {
  249. eventEmitter.removeListener(type, listener);
  250. },
  251. /*
  252. * Toggles screen sharing.
  253. */
  254. toggleScreenSharing: function () {
  255. if (switchInProgress || !obtainDesktopStream) {
  256. console.warn("Switch in progress or no method defined");
  257. return;
  258. }
  259. switchInProgress = true;
  260. if (!isUsingScreenStream)
  261. {
  262. // Switch to desktop stream
  263. obtainDesktopStream(
  264. function (stream) {
  265. // We now use screen stream
  266. isUsingScreenStream = true;
  267. // Hook 'ended' event to restore camera when screen stream stops
  268. stream.addEventListener('ended',
  269. function (e) {
  270. if (!switchInProgress && isUsingScreenStream) {
  271. toggleScreenSharing();
  272. }
  273. }
  274. );
  275. newStreamCreated(stream);
  276. },
  277. getSwitchStreamFailed);
  278. } else {
  279. // Disable screen stream
  280. RTC.getUserMediaWithConstraints(
  281. ['video'],
  282. function (stream) {
  283. // We are now using camera stream
  284. isUsingScreenStream = false;
  285. newStreamCreated(stream);
  286. },
  287. getSwitchStreamFailed, config.resolution || '360'
  288. );
  289. }
  290. }
  291. };
  292. },{"events":2}],2:[function(require,module,exports){
  293. // Copyright Joyent, Inc. and other Node contributors.
  294. //
  295. // Permission is hereby granted, free of charge, to any person obtaining a
  296. // copy of this software and associated documentation files (the
  297. // "Software"), to deal in the Software without restriction, including
  298. // without limitation the rights to use, copy, modify, merge, publish,
  299. // distribute, sublicense, and/or sell copies of the Software, and to permit
  300. // persons to whom the Software is furnished to do so, subject to the
  301. // following conditions:
  302. //
  303. // The above copyright notice and this permission notice shall be included
  304. // in all copies or substantial portions of the Software.
  305. //
  306. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  307. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  308. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  309. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  310. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  311. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  312. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  313. function EventEmitter() {
  314. this._events = this._events || {};
  315. this._maxListeners = this._maxListeners || undefined;
  316. }
  317. module.exports = EventEmitter;
  318. // Backwards-compat with node 0.10.x
  319. EventEmitter.EventEmitter = EventEmitter;
  320. EventEmitter.prototype._events = undefined;
  321. EventEmitter.prototype._maxListeners = undefined;
  322. // By default EventEmitters will print a warning if more than 10 listeners are
  323. // added to it. This is a useful default which helps finding memory leaks.
  324. EventEmitter.defaultMaxListeners = 10;
  325. // Obviously not all Emitters should be limited to 10. This function allows
  326. // that to be increased. Set to zero for unlimited.
  327. EventEmitter.prototype.setMaxListeners = function(n) {
  328. if (!isNumber(n) || n < 0 || isNaN(n))
  329. throw TypeError('n must be a positive number');
  330. this._maxListeners = n;
  331. return this;
  332. };
  333. EventEmitter.prototype.emit = function(type) {
  334. var er, handler, len, args, i, listeners;
  335. if (!this._events)
  336. this._events = {};
  337. // If there is no 'error' event listener then throw.
  338. if (type === 'error') {
  339. if (!this._events.error ||
  340. (isObject(this._events.error) && !this._events.error.length)) {
  341. er = arguments[1];
  342. if (er instanceof Error) {
  343. throw er; // Unhandled 'error' event
  344. }
  345. throw TypeError('Uncaught, unspecified "error" event.');
  346. }
  347. }
  348. handler = this._events[type];
  349. if (isUndefined(handler))
  350. return false;
  351. if (isFunction(handler)) {
  352. switch (arguments.length) {
  353. // fast cases
  354. case 1:
  355. handler.call(this);
  356. break;
  357. case 2:
  358. handler.call(this, arguments[1]);
  359. break;
  360. case 3:
  361. handler.call(this, arguments[1], arguments[2]);
  362. break;
  363. // slower
  364. default:
  365. len = arguments.length;
  366. args = new Array(len - 1);
  367. for (i = 1; i < len; i++)
  368. args[i - 1] = arguments[i];
  369. handler.apply(this, args);
  370. }
  371. } else if (isObject(handler)) {
  372. len = arguments.length;
  373. args = new Array(len - 1);
  374. for (i = 1; i < len; i++)
  375. args[i - 1] = arguments[i];
  376. listeners = handler.slice();
  377. len = listeners.length;
  378. for (i = 0; i < len; i++)
  379. listeners[i].apply(this, args);
  380. }
  381. return true;
  382. };
  383. EventEmitter.prototype.addListener = function(type, listener) {
  384. var m;
  385. if (!isFunction(listener))
  386. throw TypeError('listener must be a function');
  387. if (!this._events)
  388. this._events = {};
  389. // To avoid recursion in the case that type === "newListener"! Before
  390. // adding it to the listeners, first emit "newListener".
  391. if (this._events.newListener)
  392. this.emit('newListener', type,
  393. isFunction(listener.listener) ?
  394. listener.listener : listener);
  395. if (!this._events[type])
  396. // Optimize the case of one listener. Don't need the extra array object.
  397. this._events[type] = listener;
  398. else if (isObject(this._events[type]))
  399. // If we've already got an array, just append.
  400. this._events[type].push(listener);
  401. else
  402. // Adding the second element, need to change to array.
  403. this._events[type] = [this._events[type], listener];
  404. // Check for listener leak
  405. if (isObject(this._events[type]) && !this._events[type].warned) {
  406. var m;
  407. if (!isUndefined(this._maxListeners)) {
  408. m = this._maxListeners;
  409. } else {
  410. m = EventEmitter.defaultMaxListeners;
  411. }
  412. if (m && m > 0 && this._events[type].length > m) {
  413. this._events[type].warned = true;
  414. console.error('(node) warning: possible EventEmitter memory ' +
  415. 'leak detected. %d listeners added. ' +
  416. 'Use emitter.setMaxListeners() to increase limit.',
  417. this._events[type].length);
  418. if (typeof console.trace === 'function') {
  419. // not supported in IE 10
  420. console.trace();
  421. }
  422. }
  423. }
  424. return this;
  425. };
  426. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  427. EventEmitter.prototype.once = function(type, listener) {
  428. if (!isFunction(listener))
  429. throw TypeError('listener must be a function');
  430. var fired = false;
  431. function g() {
  432. this.removeListener(type, g);
  433. if (!fired) {
  434. fired = true;
  435. listener.apply(this, arguments);
  436. }
  437. }
  438. g.listener = listener;
  439. this.on(type, g);
  440. return this;
  441. };
  442. // emits a 'removeListener' event iff the listener was removed
  443. EventEmitter.prototype.removeListener = function(type, listener) {
  444. var list, position, length, i;
  445. if (!isFunction(listener))
  446. throw TypeError('listener must be a function');
  447. if (!this._events || !this._events[type])
  448. return this;
  449. list = this._events[type];
  450. length = list.length;
  451. position = -1;
  452. if (list === listener ||
  453. (isFunction(list.listener) && list.listener === listener)) {
  454. delete this._events[type];
  455. if (this._events.removeListener)
  456. this.emit('removeListener', type, listener);
  457. } else if (isObject(list)) {
  458. for (i = length; i-- > 0;) {
  459. if (list[i] === listener ||
  460. (list[i].listener && list[i].listener === listener)) {
  461. position = i;
  462. break;
  463. }
  464. }
  465. if (position < 0)
  466. return this;
  467. if (list.length === 1) {
  468. list.length = 0;
  469. delete this._events[type];
  470. } else {
  471. list.splice(position, 1);
  472. }
  473. if (this._events.removeListener)
  474. this.emit('removeListener', type, listener);
  475. }
  476. return this;
  477. };
  478. EventEmitter.prototype.removeAllListeners = function(type) {
  479. var key, listeners;
  480. if (!this._events)
  481. return this;
  482. // not listening for removeListener, no need to emit
  483. if (!this._events.removeListener) {
  484. if (arguments.length === 0)
  485. this._events = {};
  486. else if (this._events[type])
  487. delete this._events[type];
  488. return this;
  489. }
  490. // emit removeListener for all listeners on all events
  491. if (arguments.length === 0) {
  492. for (key in this._events) {
  493. if (key === 'removeListener') continue;
  494. this.removeAllListeners(key);
  495. }
  496. this.removeAllListeners('removeListener');
  497. this._events = {};
  498. return this;
  499. }
  500. listeners = this._events[type];
  501. if (isFunction(listeners)) {
  502. this.removeListener(type, listeners);
  503. } else {
  504. // LIFO order
  505. while (listeners.length)
  506. this.removeListener(type, listeners[listeners.length - 1]);
  507. }
  508. delete this._events[type];
  509. return this;
  510. };
  511. EventEmitter.prototype.listeners = function(type) {
  512. var ret;
  513. if (!this._events || !this._events[type])
  514. ret = [];
  515. else if (isFunction(this._events[type]))
  516. ret = [this._events[type]];
  517. else
  518. ret = this._events[type].slice();
  519. return ret;
  520. };
  521. EventEmitter.listenerCount = function(emitter, type) {
  522. var ret;
  523. if (!emitter._events || !emitter._events[type])
  524. ret = 0;
  525. else if (isFunction(emitter._events[type]))
  526. ret = 1;
  527. else
  528. ret = emitter._events[type].length;
  529. return ret;
  530. };
  531. function isFunction(arg) {
  532. return typeof arg === 'function';
  533. }
  534. function isNumber(arg) {
  535. return typeof arg === 'number';
  536. }
  537. function isObject(arg) {
  538. return typeof arg === 'object' && arg !== null;
  539. }
  540. function isUndefined(arg) {
  541. return arg === void 0;
  542. }
  543. },{}]},{},[1])(1)
  544. });