Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ScreenObtainer.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* global config, APP, chrome, $, alert */
  2. /* jshint -W003 */
  3. var RTCBrowserType = require("../RTC/RTCBrowserType");
  4. var AdapterJS = require("../RTC/adapter.screenshare");
  5. /**
  6. * Indicates whether the Chrome desktop sharing extension is installed.
  7. * @type {boolean}
  8. */
  9. var chromeExtInstalled = false;
  10. /**
  11. * Indicates whether an update of the Chrome desktop sharing extension is
  12. * required.
  13. * @type {boolean}
  14. */
  15. var chromeExtUpdateRequired = false;
  16. /**
  17. * Handles obtaining a stream from a screen capture on different browsers.
  18. */
  19. function ScreenObtainer(){
  20. }
  21. /**
  22. * Initializes the function used to obtain a screen capture (this.obtainStream).
  23. *
  24. * If the browser is Chrome, it uses the value of 'config.desktopSharing' to
  25. * decide whether to use the a Chrome extension (if the value is 'ext'), use
  26. * the "screen" media source (if the value is 'webrtc'), or disable screen
  27. * capture (if the value is other).
  28. * Note that for the "screen" media source to work the
  29. * 'chrome://flags/#enable-usermedia-screen-capture' flag must be set.
  30. */
  31. ScreenObtainer.prototype.init = function() {
  32. var obtainDesktopStream = null;
  33. // When TemasysWebRTC plugin is used we always use getUserMedia, so we don't
  34. // care about the value of config.desktopSharing.
  35. if (RTCBrowserType.isTemasysPluginUsed()) {
  36. if (!AdapterJS.WebRTCPlugin.plugin.HasScreensharingFeature) {
  37. console.info("Screensharing not supported by this plugin version");
  38. } else if (!AdapterJS.WebRTCPlugin.plugin.isScreensharingAvailable) {
  39. console.info(
  40. "Screensharing not available with Temasys plugin on this site");
  41. } else {
  42. obtainDesktopStream = obtainWebRTCScreen;
  43. console.info("Using Temasys plugin for desktop sharing");
  44. }
  45. } else if (RTCBrowserType.isChrome()) {
  46. if (config.desktopSharing == "ext") {
  47. if (RTCBrowserType.getChromeVersion() >= 34) {
  48. obtainDesktopStream = obtainScreenFromExtension;
  49. console.info("Using Chrome extension for desktop sharing");
  50. initChromeExtension();
  51. } else {
  52. console.info("Chrome extension not supported until ver 34");
  53. }
  54. } else if (config.desktopSharing == "webrtc") {
  55. obtainDesktopStream = obtainWebRTCScreen;
  56. console.info("Using Chrome WebRTC for desktop sharing");
  57. }
  58. } else if (RTCBrowserType.isFirefox()) {
  59. obtainDesktopStream = obtainWebRTCScreen;
  60. }
  61. if (!obtainDesktopStream) {
  62. console.info("Desktop sharing disabled");
  63. }
  64. ScreenObtainer.prototype.obtainStream = obtainDesktopStream.bind(this);
  65. };
  66. ScreenObtainer.prototype.obtainStream = null;
  67. /**
  68. * Checks whether obtaining a screen capture is supported in the current
  69. * environment.
  70. * @returns {boolean}
  71. */
  72. ScreenObtainer.prototype.isSupported = function() {
  73. return !!this.obtainStream;
  74. };
  75. /**
  76. * Obtains a desktop stream using getUserMedia.
  77. * For this to work on Chrome, the
  78. * 'chrome://flags/#enable-usermedia-screen-capture' flag must be enabled.
  79. *
  80. * On firefox, the document's domain must be white-listed in the
  81. * 'media.getusermedia.screensharing.allowed_domains' preference in
  82. * 'about:config'.
  83. */
  84. function obtainWebRTCScreen(streamCallback, failCallback) {
  85. APP.RTC.getUserMediaWithConstraints(
  86. ['screen'],
  87. streamCallback,
  88. failCallback
  89. );
  90. }
  91. /**
  92. * Constructs inline install URL for Chrome desktop streaming extension.
  93. * The 'chromeExtensionId' must be defined in config.js.
  94. * @returns {string}
  95. */
  96. function getWebStoreInstallUrl()
  97. {
  98. return "https://chrome.google.com/webstore/detail/" +
  99. config.chromeExtensionId;
  100. }
  101. /**
  102. * Checks whether an update of the Chrome extension is required.
  103. * @param minVersion minimal required version
  104. * @param extVersion current extension version
  105. * @returns {boolean}
  106. */
  107. function isUpdateRequired(minVersion, extVersion) {
  108. try {
  109. var s1 = minVersion.split('.');
  110. var s2 = extVersion.split('.');
  111. var len = Math.max(s1.length, s2.length);
  112. for (var i = 0; i < len; i++) {
  113. var n1 = 0,
  114. n2 = 0;
  115. if (i < s1.length)
  116. n1 = parseInt(s1[i]);
  117. if (i < s2.length)
  118. n2 = parseInt(s2[i]);
  119. if (isNaN(n1) || isNaN(n2)) {
  120. return true;
  121. } else if (n1 !== n2) {
  122. return n1 > n2;
  123. }
  124. }
  125. // will happen if both versions have identical numbers in
  126. // their components (even if one of them is longer, has more components)
  127. return false;
  128. }
  129. catch (e) {
  130. console.error("Failed to parse extension version", e);
  131. APP.UI.messageHandler.showError("dialog.error",
  132. "dialog.detectext");
  133. return true;
  134. }
  135. }
  136. function checkChromeExtInstalled(callback) {
  137. if (!chrome || !chrome.runtime) {
  138. // No API, so no extension for sure
  139. callback(false, false);
  140. return;
  141. }
  142. chrome.runtime.sendMessage(
  143. config.chromeExtensionId,
  144. { getVersion: true },
  145. function (response) {
  146. if (!response || !response.version) {
  147. // Communication failure - assume that no endpoint exists
  148. console.warn(
  149. "Extension not installed?: ", chrome.runtime.lastError);
  150. callback(false, false);
  151. return;
  152. }
  153. // Check installed extension version
  154. var extVersion = response.version;
  155. console.log('Extension version is: ' + extVersion);
  156. var updateRequired
  157. = isUpdateRequired(config.minChromeExtVersion, extVersion);
  158. callback(!updateRequired, updateRequired);
  159. }
  160. );
  161. }
  162. function doGetStreamFromExtension(streamCallback, failCallback) {
  163. // Sends 'getStream' msg to the extension.
  164. // Extension id must be defined in the config.
  165. chrome.runtime.sendMessage(
  166. config.chromeExtensionId,
  167. { getStream: true, sources: config.desktopSharingSources },
  168. function (response) {
  169. if (!response) {
  170. failCallback(chrome.runtime.lastError);
  171. return;
  172. }
  173. console.log("Response from extension: " + response);
  174. if (response.streamId) {
  175. APP.RTC.getUserMediaWithConstraints(
  176. ['desktop'],
  177. function (stream) {
  178. streamCallback(stream);
  179. },
  180. failCallback,
  181. null, null, null,
  182. response.streamId);
  183. } else {
  184. failCallback("Extension failed to get the stream");
  185. }
  186. }
  187. );
  188. }
  189. /**
  190. * Asks Chrome extension to call chooseDesktopMedia and gets chrome 'desktop'
  191. * stream for returned stream token.
  192. */
  193. function obtainScreenFromExtension(streamCallback, failCallback) {
  194. if (chromeExtInstalled) {
  195. doGetStreamFromExtension(streamCallback, failCallback);
  196. } else {
  197. if (chromeExtUpdateRequired) {
  198. alert(
  199. 'Jitsi Desktop Streamer requires update. ' +
  200. 'Changes will take effect after next Chrome restart.');
  201. }
  202. chrome.webstore.install(
  203. getWebStoreInstallUrl(),
  204. function (arg) {
  205. console.log("Extension installed successfully", arg);
  206. chromeExtInstalled = true;
  207. // We need to give a moment for the endpoint to become available
  208. window.setTimeout(function () {
  209. doGetStreamFromExtension(streamCallback, failCallback);
  210. }, 500);
  211. },
  212. function (arg) {
  213. console.log("Failed to install the extension", arg);
  214. failCallback(arg);
  215. APP.UI.messageHandler.showError("dialog.error",
  216. "dialog.failtoinstall");
  217. }
  218. );
  219. }
  220. }
  221. /**
  222. * Initializes <link rel=chrome-webstore-item /> with extension id set in
  223. * config.js to support inline installs. Host site must be selected as main
  224. * website of published extension.
  225. */
  226. function initInlineInstalls()
  227. {
  228. $("link[rel=chrome-webstore-item]").attr("href", getWebStoreInstallUrl());
  229. }
  230. function initChromeExtension() {
  231. // Initialize Chrome extension inline installs
  232. initInlineInstalls();
  233. // Check if extension is installed
  234. checkChromeExtInstalled(function (installed, updateRequired) {
  235. chromeExtInstalled = installed;
  236. chromeExtUpdateRequired = updateRequired;
  237. console.info(
  238. "Chrome extension installed: " + chromeExtInstalled +
  239. " updateRequired: " + chromeExtUpdateRequired);
  240. });
  241. }
  242. module.exports = ScreenObtainer;