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.js 9.6KB

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