Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

desktopsharing.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* global $, alert, APP, changeLocalVideo, chrome, config, getConferenceHandler,
  2. 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
  10. * triggering new events.
  11. * @type {boolean}
  12. */
  13. var switchInProgress = false;
  14. /**
  15. * Method used to get screen sharing stream.
  16. *
  17. * @type {function (stream_callback, failure_callback}
  18. */
  19. var obtainDesktopStream = null;
  20. /**
  21. * Indicates whether desktop sharing extension is installed.
  22. * @type {boolean}
  23. */
  24. var extInstalled = false;
  25. /**
  26. * Indicates whether update of desktop sharing extension is required.
  27. * @type {boolean}
  28. */
  29. var extUpdateRequired = false;
  30. /**
  31. * Flag used to cache desktop sharing enabled state. Do not use directly as
  32. * it can be <tt>null</tt>.
  33. *
  34. * @type {null|boolean}
  35. */
  36. var _desktopSharingEnabled = null;
  37. var EventEmitter = require("events");
  38. var eventEmitter = new EventEmitter();
  39. var DesktopSharingEventTypes
  40. = require("../../service/desktopsharing/DesktopSharingEventTypes");
  41. /**
  42. * Method obtains desktop stream from WebRTC 'screen' source.
  43. * Flag 'chrome://flags/#enable-usermedia-screen-capture' must be enabled.
  44. */
  45. function obtainWebRTCScreen(streamCallback, failCallback) {
  46. APP.RTC.getUserMediaWithConstraints(
  47. ['screen'],
  48. streamCallback,
  49. failCallback
  50. );
  51. }
  52. /**
  53. * Constructs inline install URL for Chrome desktop streaming extension.
  54. * The 'chromeExtensionId' must be defined in config.js.
  55. * @returns {string}
  56. */
  57. function getWebStoreInstallUrl()
  58. {
  59. return "https://chrome.google.com/webstore/detail/" +
  60. config.chromeExtensionId;
  61. }
  62. /**
  63. * Checks whether extension update is required.
  64. * @param minVersion minimal required version
  65. * @param extVersion current extension version
  66. * @returns {boolean}
  67. */
  68. function isUpdateRequired(minVersion, extVersion)
  69. {
  70. try
  71. {
  72. var s1 = minVersion.split('.');
  73. var s2 = extVersion.split('.');
  74. var len = Math.max(s1.length, s2.length);
  75. for (var i = 0; i < len; i++)
  76. {
  77. var n1 = 0,
  78. n2 = 0;
  79. if (i < s1.length)
  80. n1 = parseInt(s1[i]);
  81. if (i < s2.length)
  82. n2 = parseInt(s2[i]);
  83. if (isNaN(n1) || isNaN(n2))
  84. {
  85. return true;
  86. }
  87. else if (n1 !== n2)
  88. {
  89. return n1 > n2;
  90. }
  91. }
  92. // will happen if boths version has identical numbers in
  93. // their components (even if one of them is longer, has more components)
  94. return false;
  95. }
  96. catch (e)
  97. {
  98. console.error("Failed to parse extension version", e);
  99. APP.UI.messageHandler.showError("dialog.error",
  100. "dialog.detectext");
  101. return true;
  102. }
  103. }
  104. function checkExtInstalled(callback) {
  105. if (!chrome.runtime) {
  106. // No API, so no extension for sure
  107. callback(false, false);
  108. return;
  109. }
  110. chrome.runtime.sendMessage(
  111. config.chromeExtensionId,
  112. { getVersion: true },
  113. function (response) {
  114. if (!response || !response.version) {
  115. // Communication failure - assume that no endpoint exists
  116. console.warn(
  117. "Extension not installed?: ", chrome.runtime.lastError);
  118. callback(false, false);
  119. return;
  120. }
  121. // Check installed extension version
  122. var extVersion = response.version;
  123. console.log('Extension version is: ' + extVersion);
  124. var updateRequired
  125. = isUpdateRequired(config.minChromeExtVersion, extVersion);
  126. callback(!updateRequired, updateRequired);
  127. }
  128. );
  129. }
  130. function doGetStreamFromExtension(streamCallback, failCallback) {
  131. // Sends 'getStream' msg to the extension.
  132. // Extension id must be defined in the config.
  133. chrome.runtime.sendMessage(
  134. config.chromeExtensionId,
  135. { getStream: true, sources: config.desktopSharingSources },
  136. function (response) {
  137. if (!response) {
  138. failCallback(chrome.runtime.lastError);
  139. return;
  140. }
  141. console.log("Response from extension: " + response);
  142. if (response.streamId) {
  143. APP.RTC.getUserMediaWithConstraints(
  144. ['desktop'],
  145. function (stream) {
  146. streamCallback(stream);
  147. },
  148. failCallback,
  149. null, null, null,
  150. response.streamId);
  151. } else {
  152. failCallback("Extension failed to get the stream");
  153. }
  154. }
  155. );
  156. }
  157. /**
  158. * Asks Chrome extension to call chooseDesktopMedia and gets chrome 'desktop'
  159. * stream for returned stream token.
  160. */
  161. function obtainScreenFromExtension(streamCallback, failCallback) {
  162. if (extInstalled) {
  163. doGetStreamFromExtension(streamCallback, failCallback);
  164. } else {
  165. if (extUpdateRequired) {
  166. alert(
  167. 'Jitsi Desktop Streamer requires update. ' +
  168. 'Changes will take effect after next Chrome restart.');
  169. }
  170. chrome.webstore.install(
  171. getWebStoreInstallUrl(),
  172. function (arg) {
  173. console.log("Extension installed successfully", arg);
  174. // We need to reload the page in order to get the access to
  175. // chrome.runtime
  176. window.location.reload(false);
  177. },
  178. function (arg) {
  179. console.log("Failed to install the extension", arg);
  180. failCallback(arg);
  181. APP.UI.messageHandler.showError("dialog.error",
  182. "dialog.failtoinstall");
  183. }
  184. );
  185. }
  186. }
  187. /**
  188. * Call this method to toggle desktop sharing feature.
  189. * @param method pass "ext" to use chrome extension for desktop capture(chrome
  190. * extension required), pass "webrtc" to use WebRTC "screen" desktop
  191. * source('chrome://flags/#enable-usermedia-screen-capture' must be
  192. * enabled), pass any other string or nothing in order to disable this
  193. * feature completely.
  194. */
  195. function setDesktopSharing(method) {
  196. // Check if we are running chrome
  197. if (!navigator.webkitGetUserMedia) {
  198. obtainDesktopStream = null;
  199. console.info("Desktop sharing disabled");
  200. } else if (method == "ext") {
  201. obtainDesktopStream = obtainScreenFromExtension;
  202. console.info("Using Chrome extension for desktop sharing");
  203. } else if (method == "webrtc") {
  204. obtainDesktopStream = obtainWebRTCScreen;
  205. console.info("Using Chrome WebRTC for desktop sharing");
  206. }
  207. // Reset enabled cache
  208. _desktopSharingEnabled = null;
  209. }
  210. /**
  211. * Initializes <link rel=chrome-webstore-item /> with extension id set in
  212. * config.js to support inline installs. Host site must be selected as main
  213. * website of published extension.
  214. */
  215. function initInlineInstalls()
  216. {
  217. $("link[rel=chrome-webstore-item]").attr("href", getWebStoreInstallUrl());
  218. }
  219. function getVideoStreamFailed(error) {
  220. console.error("Failed to obtain the stream to switch to", error);
  221. switchInProgress = false;
  222. isUsingScreenStream = false;
  223. newStreamCreated(null);
  224. }
  225. function getDesktopStreamFailed(error) {
  226. console.error("Failed to obtain the stream to switch to", error);
  227. switchInProgress = false;
  228. }
  229. function streamSwitchDone() {
  230. switchInProgress = false;
  231. eventEmitter.emit(
  232. DesktopSharingEventTypes.SWITCHING_DONE,
  233. isUsingScreenStream);
  234. }
  235. function newStreamCreated(stream)
  236. {
  237. eventEmitter.emit(DesktopSharingEventTypes.NEW_STREAM_CREATED,
  238. stream, isUsingScreenStream, streamSwitchDone);
  239. }
  240. module.exports = {
  241. isUsingScreenStream: function () {
  242. return isUsingScreenStream;
  243. },
  244. /**
  245. * @returns {boolean} <tt>true</tt> if desktop sharing feature is available
  246. * and enabled.
  247. */
  248. isDesktopSharingEnabled: function () {
  249. if (_desktopSharingEnabled === null) {
  250. if (obtainDesktopStream === obtainScreenFromExtension) {
  251. // Parse chrome version
  252. var userAgent = navigator.userAgent.toLowerCase();
  253. // We can assume that user agent is chrome, because it's
  254. // enforced when 'ext' streaming method is set
  255. var ver = parseInt(userAgent.match(/chrome\/(\d+)\./)[1], 10);
  256. console.log("Chrome version" + userAgent, ver);
  257. _desktopSharingEnabled = ver >= 34;
  258. } else {
  259. _desktopSharingEnabled =
  260. obtainDesktopStream === obtainWebRTCScreen;
  261. }
  262. }
  263. return _desktopSharingEnabled;
  264. },
  265. init: function () {
  266. setDesktopSharing(config.desktopSharing);
  267. // Initialize Chrome extension inline installs
  268. if (config.chromeExtensionId) {
  269. initInlineInstalls();
  270. // Check if extension is installed
  271. checkExtInstalled(function (installed, updateRequired) {
  272. extInstalled = installed;
  273. extUpdateRequired = updateRequired;
  274. console.info(
  275. "Chrome extension installed: " + extInstalled +
  276. " updateRequired: " + extUpdateRequired);
  277. });
  278. }
  279. eventEmitter.emit(DesktopSharingEventTypes.INIT);
  280. },
  281. addListener: function (listener, type)
  282. {
  283. eventEmitter.on(type, listener);
  284. },
  285. removeListener: function (listener, type) {
  286. eventEmitter.removeListener(type, listener);
  287. },
  288. /*
  289. * Toggles screen sharing.
  290. */
  291. toggleScreenSharing: function () {
  292. if (switchInProgress || !obtainDesktopStream) {
  293. console.warn("Switch in progress or no method defined");
  294. return;
  295. }
  296. switchInProgress = true;
  297. if (!isUsingScreenStream)
  298. {
  299. // Switch to desktop stream
  300. obtainDesktopStream(
  301. function (stream) {
  302. // We now use screen stream
  303. isUsingScreenStream = true;
  304. // Hook 'ended' event to restore camera
  305. // when screen stream stops
  306. stream.addEventListener('ended',
  307. function (e) {
  308. if (!switchInProgress && isUsingScreenStream) {
  309. APP.desktopsharing.toggleScreenSharing();
  310. }
  311. }
  312. );
  313. newStreamCreated(stream);
  314. },
  315. getDesktopStreamFailed);
  316. } else {
  317. // Disable screen stream
  318. APP.RTC.getUserMediaWithConstraints(
  319. ['video'],
  320. function (stream) {
  321. // We are now using camera stream
  322. isUsingScreenStream = false;
  323. newStreamCreated(stream);
  324. },
  325. getVideoStreamFailed, config.resolution || '360'
  326. );
  327. }
  328. }
  329. };