Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

desktopsharing.js 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. }
  88. chrome.runtime.sendMessage(
  89. config.chromeExtensionId,
  90. { getVersion: true },
  91. function (response) {
  92. if (!response || !response.version) {
  93. // Communication failure - assume that no endpoint exists
  94. console.warn("Extension not installed?: " + chrome.runtime.lastError);
  95. isInstalledCallback(false);
  96. } else {
  97. // Check installed extension version
  98. var extVersion = response.version;
  99. console.log('Extension version is: ' + extVersion);
  100. var updateRequired = isUpdateRequired(config.minChromeExtVersion, extVersion);
  101. if (updateRequired) {
  102. alert(
  103. 'Jitsi Desktop Streamer requires update. ' +
  104. 'Changes will take effect after next Chrome restart.');
  105. }
  106. isInstalledCallback(!updateRequired);
  107. }
  108. }
  109. );
  110. }
  111. function doGetStreamFromExtension(streamCallback, failCallback) {
  112. // Sends 'getStream' msg to the extension. Extension id must be defined in the config.
  113. chrome.runtime.sendMessage(
  114. config.chromeExtensionId,
  115. { getStream: true},
  116. function (response) {
  117. if (!response) {
  118. failCallback(chrome.runtime.lastError);
  119. return;
  120. }
  121. console.log("Response from extension: " + response);
  122. if (response.streamId) {
  123. getUserMediaWithConstraints(
  124. ['desktop'],
  125. function (stream) {
  126. streamCallback(stream);
  127. },
  128. failCallback,
  129. null, null, null,
  130. response.streamId);
  131. } else {
  132. failCallback("Extension failed to get the stream");
  133. }
  134. }
  135. );
  136. }
  137. /**
  138. * Asks Chrome extension to call chooseDesktopMedia and gets chrome 'desktop' stream for returned stream token.
  139. */
  140. function obtainScreenFromExtension(streamCallback, failCallback) {
  141. checkExtInstalled(
  142. function (isInstalled) {
  143. if (isInstalled) {
  144. doGetStreamFromExtension(streamCallback, failCallback);
  145. } else {
  146. chrome.webstore.install(
  147. getWebStoreInstallUrl(),
  148. function (arg) {
  149. console.log("Extension installed successfully", arg);
  150. // We need to reload the page in order to get the access to chrome.runtime
  151. window.location.reload(false);
  152. },
  153. function (arg) {
  154. console.log("Failed to install the extension", arg);
  155. failCallback(arg);
  156. }
  157. );
  158. }
  159. }
  160. );
  161. }
  162. /**
  163. * @returns {boolean} <tt>true</tt> if desktop sharing feature is available and enabled.
  164. */
  165. function isDesktopSharingEnabled() {
  166. if (_desktopSharingEnabled === null) {
  167. if (obtainDesktopStream === obtainScreenFromExtension) {
  168. // Parse chrome version
  169. var userAgent = navigator.userAgent.toLowerCase();
  170. // We can assume that user agent is chrome, because it's enforced when 'ext' streaming method is set
  171. var ver = parseInt(userAgent.match(/chrome\/(\d+)\./)[1], 10);
  172. console.log("Chrome version" + userAgent, ver);
  173. _desktopSharingEnabled = ver >= 34;
  174. } else {
  175. _desktopSharingEnabled = obtainDesktopStream === obtainWebRTCScreen;
  176. }
  177. }
  178. return _desktopSharingEnabled;
  179. }
  180. function showDesktopSharingButton() {
  181. if (isDesktopSharingEnabled()) {
  182. $('#desktopsharing').css({display: "inline"});
  183. } else {
  184. $('#desktopsharing').css({display: "none"});
  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 extension required),
  190. * pass "webrtc" to use WebRTC "screen" desktop source('chrome://flags/#enable-usermedia-screen-capture'
  191. * must be enabled), pass any other string or nothing in order to disable this feature completely.
  192. */
  193. function setDesktopSharing(method) {
  194. // Check if we are running chrome
  195. if (!navigator.webkitGetUserMedia) {
  196. obtainDesktopStream = null;
  197. console.info("Desktop sharing disabled");
  198. } else if (method == "ext") {
  199. obtainDesktopStream = obtainScreenFromExtension;
  200. console.info("Using Chrome extension for desktop sharing");
  201. } else if (method == "webrtc") {
  202. obtainDesktopStream = obtainWebRTCScreen;
  203. console.info("Using Chrome WebRTC for desktop sharing");
  204. }
  205. // Reset enabled cache
  206. _desktopSharingEnabled = null;
  207. showDesktopSharingButton();
  208. }
  209. /**
  210. * Initializes <link rel=chrome-webstore-item /> with extension id set in config.js to support inline installs.
  211. * Host site must be selected as main website of published extension.
  212. */
  213. function initInlineInstalls()
  214. {
  215. $("link[rel=chrome-webstore-item]").attr("href", getWebStoreInstallUrl());
  216. }
  217. function getSwitchStreamFailed(error) {
  218. console.error("Failed to obtain the stream to switch to", error);
  219. switchInProgress = false;
  220. }
  221. function streamSwitchDone() {
  222. //window.setTimeout(
  223. // function () {
  224. switchInProgress = false;
  225. // }, 100
  226. //);
  227. }
  228. function newStreamCreated(stream) {
  229. var oldStream = connection.jingle.localVideo;
  230. change_local_video(stream, !isUsingScreenStream);
  231. var conferenceHandler = getConferenceHandler();
  232. if (conferenceHandler) {
  233. // FIXME: will block switchInProgress on true value in case of exception
  234. conferenceHandler.switchStreams(stream, oldStream, streamSwitchDone);
  235. } else {
  236. // We are done immediately
  237. console.error("No conference handler");
  238. streamSwitchDone();
  239. }
  240. }
  241. /*
  242. * Toggles screen sharing.
  243. */
  244. function toggleScreenSharing() {
  245. if (switchInProgress || !obtainDesktopStream) {
  246. console.warn("Switch in progress or no method defined");
  247. return;
  248. }
  249. switchInProgress = true;
  250. // Only the focus is able to set a shared key.
  251. if (!isUsingScreenStream)
  252. {
  253. obtainDesktopStream(
  254. function (stream) {
  255. // We now use screen stream
  256. isUsingScreenStream = true;
  257. // Hook 'ended' event to restore camera when screen stream stops
  258. stream.addEventListener('ended',
  259. function (e) {
  260. if (!switchInProgress && isUsingScreenStream) {
  261. toggleScreenSharing();
  262. }
  263. }
  264. );
  265. newStreamCreated(stream);
  266. },
  267. getSwitchStreamFailed);
  268. } else {
  269. // Disable screen stream
  270. getUserMediaWithConstraints(
  271. ['video'],
  272. function (stream) {
  273. // We are now using camera stream
  274. isUsingScreenStream = false;
  275. newStreamCreated(stream);
  276. },
  277. getSwitchStreamFailed, config.resolution || '360'
  278. );
  279. }
  280. }