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 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * Indicates that desktop stream is currently in use(for toggle purpose).
  3. * @type {boolean}
  4. */
  5. var isUsingScreenStream = false;
  6. /**
  7. * Indicates that switch stream operation is in progress and prevent from triggering new events.
  8. * @type {boolean}
  9. */
  10. var switchInProgress = false;
  11. /**
  12. * Method used to get screen sharing stream.
  13. *
  14. * @type {function(stream_callback, failure_callback}
  15. */
  16. var obtainDesktopStream = null;
  17. /**
  18. * @returns {boolean} <tt>true</tt> if desktop sharing feature is available and enabled.
  19. */
  20. function isDesktopSharingEnabled() {
  21. if(obtainDesktopStream === obtainScreenFromExtension) {
  22. // Parse chrome version
  23. var userAgent = navigator.userAgent.toLowerCase();
  24. // We can assume that user agent is chrome, because it's enforced when 'ext' streaming method is set
  25. var ver = parseInt(userAgent.match(/chrome\/(\d+)\./)[1], 10);
  26. console.log("Chrome version" + userAgent, ver);
  27. return ver >= 35;
  28. } else {
  29. return obtainDesktopStream === obtainWebRTCScreen;
  30. }
  31. }
  32. /**
  33. * Call this method to toggle desktop sharing feature.
  34. * @param method pass "ext" to use chrome extension for desktop capture(chrome extension required),
  35. * pass "webrtc" to use WebRTC "screen" desktop source('chrome://flags/#enable-usermedia-screen-capture'
  36. * must be enabled), pass any other string or nothing in order to disable this feature completely.
  37. */
  38. function setDesktopSharing(method) {
  39. if(method == "ext") {
  40. if(RTC.browser === 'chrome') {
  41. obtainDesktopStream = obtainScreenFromExtension;
  42. } else {
  43. console.log("Chrome is required to use extension method");
  44. obtainDesktopStream = null;
  45. }
  46. } else if(method == "webrtc") {
  47. obtainDesktopStream = obtainWebRTCScreen;
  48. } else {
  49. obtainDesktopStream = null;
  50. }
  51. showDesktopSharingButton();
  52. }
  53. function showDesktopSharingButton() {
  54. if(isDesktopSharingEnabled()) {
  55. $('#desktopsharing').css( {display:"inline"} );
  56. } else {
  57. $('#desktopsharing').css( {display:"none"} );
  58. }
  59. }
  60. /*
  61. * Toggles screen sharing.
  62. */
  63. function toggleScreenSharing() {
  64. if (!(connection && connection.connected
  65. && !switchInProgress
  66. && getConferenceHandler().peerconnection.signalingState == 'stable'
  67. && getConferenceHandler().peerconnection.iceConnectionState == 'connected'
  68. && obtainDesktopStream )) {
  69. return;
  70. }
  71. switchInProgress = true;
  72. // Only the focus is able to set a shared key.
  73. if(!isUsingScreenStream)
  74. {
  75. obtainDesktopStream(
  76. function(stream) {
  77. // We now use screen stream
  78. isUsingScreenStream = true;
  79. // Hook 'ended' event to restore camera when screen stream stops
  80. stream.addEventListener('ended',
  81. function(e) {
  82. if(!switchInProgress) {
  83. toggleScreenSharing();
  84. }
  85. }
  86. );
  87. newStreamCreated(stream);
  88. },
  89. getSwitchStreamFailed );
  90. } else {
  91. // Disable screen stream
  92. getUserMediaWithConstraints(
  93. ['video'],
  94. function(stream) {
  95. // We are now using camera stream
  96. isUsingScreenStream = false;
  97. newStreamCreated(stream);
  98. },
  99. getSwitchStreamFailed, config.resolution || '360'
  100. );
  101. }
  102. }
  103. function getSwitchStreamFailed(error) {
  104. console.error("Failed to obtain the stream to switch to", error);
  105. switchInProgress = false;
  106. }
  107. function newStreamCreated(stream) {
  108. var oldStream = connection.jingle.localVideo;
  109. change_local_video(stream, !isUsingScreenStream);
  110. // FIXME: will block switchInProgress on true value in case of exception
  111. getConferenceHandler().switchStreams(
  112. stream, oldStream,
  113. function() {
  114. // Switch operation has finished
  115. switchInProgress = false;
  116. });
  117. }
  118. /**
  119. * Method obtains desktop stream from WebRTC 'screen' source.
  120. * Flag 'chrome://flags/#enable-usermedia-screen-capture' must be enabled.
  121. */
  122. function obtainWebRTCScreen(streamCallback, failCallback) {
  123. getUserMediaWithConstraints(
  124. ['screen'],
  125. streamCallback,
  126. failCallback
  127. );
  128. }
  129. /**
  130. * Asks Chrome extension to call chooseDesktopMedia and gets chrome 'desktop' stream for returned stream token.
  131. */
  132. function obtainScreenFromExtension(streamCallback, failCallback) {
  133. checkExtInstalled(
  134. function(isInstalled) {
  135. if(isInstalled) {
  136. doGetStreamFromExtension(streamCallback, failCallback);
  137. } else {
  138. chrome.webstore.install(
  139. "https://chrome.google.com/webstore/detail/" + config.chromeExtensionId,
  140. function(arg) {
  141. console.log("Extension installed successfully", arg);
  142. // We need to reload the page in order to get the access to chrome.runtime
  143. window.location.reload(false);
  144. },
  145. function(arg) {
  146. console.log("Failed to install the extension", arg);
  147. failCallback(arg);
  148. }
  149. );
  150. }
  151. }
  152. );
  153. }
  154. function checkExtInstalled(isInstalledCallback) {
  155. if(!chrome.runtime) {
  156. // No API, so no extension for sure
  157. isInstalledCallback(false);
  158. return false;
  159. }
  160. chrome.runtime.sendMessage(
  161. config.chromeExtensionId,
  162. { getVersion: true },
  163. function(response){
  164. if(!response || !response.version) {
  165. // Communication failure - assume that no endpoint exists
  166. console.warn("Extension not installed?: "+chrome.runtime.lastError);
  167. isInstalledCallback(false);
  168. } else {
  169. // Check installed extension version
  170. var extVersion = response.version;
  171. console.log('Extension version is: '+extVersion);
  172. var updateRequired = extVersion < config.minChromeExtVersion;
  173. if(updateRequired) {
  174. alert(
  175. 'Jitsi Desktop Streamer requires update. ' +
  176. 'Changes will take effect after next Chrome restart.' );
  177. }
  178. isInstalledCallback(!updateRequired);
  179. }
  180. }
  181. );
  182. }
  183. function doGetStreamFromExtension(streamCallback, failCallback) {
  184. // Sends 'getStream' msg to the extension. Extension id must be defined in the config.
  185. chrome.runtime.sendMessage(
  186. config.chromeExtensionId,
  187. { getStream: true},
  188. function(response) {
  189. if(!response) {
  190. failCallback(chrome.runtime.lastError);
  191. return;
  192. }
  193. console.log("Response from extension: "+response);
  194. if(response.streamId) {
  195. getUserMediaWithConstraints(
  196. ['desktop'],
  197. function(stream) {
  198. streamCallback(stream);
  199. },
  200. failCallback,
  201. null, null, null,
  202. response.streamId);
  203. } else {
  204. failCallback("Extension failed to get the stream");
  205. }
  206. }
  207. );
  208. }