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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 = obtainScreenFromExtension;
  17. /**
  18. * Desktop sharing must be enabled in config and works on chrome only.
  19. */
  20. var desktopSharingEnabled = config.desktopSharing;
  21. /**
  22. * @returns {boolean} <tt>true</tt> if desktop sharing feature is available and enabled.
  23. */
  24. function isDesktopSharingEnabled() {
  25. return desktopSharingEnabled;
  26. }
  27. /**
  28. * Call this method to toggle desktop sharing feature.
  29. * @param method pass "ext" to use chrome extension for desktop capture(chrome extension required),
  30. * pass "webrtc" to use WebRTC "screen" desktop source('chrome://flags/#enable-usermedia-screen-capture'
  31. * must be enabled), pass any other string or nothing in order to disable this feature completely.
  32. */
  33. function setDesktopSharing(method) {
  34. if(method == "ext") {
  35. obtainDesktopStream = obtainScreenFromExtension;
  36. desktopSharingEnabled = true;
  37. } else if(method == "webrtc") {
  38. obtainDesktopStream = obtainWebRTCScreen;
  39. desktopSharingEnabled = true;
  40. } else {
  41. obtainDesktopStream = null;
  42. desktopSharingEnabled = false;
  43. }
  44. showDesktopSharingButton();
  45. }
  46. /*
  47. * Toggles screen sharing.
  48. */
  49. function toggleScreenSharing() {
  50. if (!(connection && connection.connected
  51. && !switchInProgress
  52. && getConferenceHandler().peerconnection.signalingState == 'stable'
  53. && getConferenceHandler().peerconnection.iceConnectionState == 'connected'
  54. && obtainDesktopStream )) {
  55. return;
  56. }
  57. switchInProgress = true;
  58. // Only the focus is able to set a shared key.
  59. if(!isUsingScreenStream)
  60. {
  61. obtainDesktopStream(
  62. function(stream) {
  63. // We now use screen stream
  64. isUsingScreenStream = true;
  65. // Hook 'ended' event to restore camera when screen stream stops
  66. stream.addEventListener('ended',
  67. function(e) {
  68. if(!switchInProgress) {
  69. toggleScreenSharing();
  70. }
  71. }
  72. );
  73. newStreamCreated(stream);
  74. },
  75. getSwitchStreamFailed );
  76. } else {
  77. // Disable screen stream
  78. getUserMediaWithConstraints(
  79. ['video'],
  80. function(stream) {
  81. // We are now using camera stream
  82. isUsingScreenStream = false;
  83. newStreamCreated(stream);
  84. },
  85. getSwitchStreamFailed, config.resolution || '360'
  86. );
  87. }
  88. }
  89. function getSwitchStreamFailed(error) {
  90. console.error("Failed to obtain the stream to switch to", error);
  91. switchInProgress = false;
  92. }
  93. function newStreamCreated(stream) {
  94. var oldStream = connection.jingle.localVideo;
  95. change_local_video(stream, !isUsingScreenStream);
  96. // FIXME: will block switchInProgress on true value in case of exception
  97. getConferenceHandler().switchStreams(
  98. stream, oldStream,
  99. function() {
  100. // Switch operation has finished
  101. switchInProgress = false;
  102. });
  103. }
  104. /**
  105. * Method obtains desktop stream from WebRTC 'screen' source.
  106. * Flag 'chrome://flags/#enable-usermedia-screen-capture' must be enabled.
  107. */
  108. function obtainWebRTCScreen(streamCallback, failCallback) {
  109. getUserMediaWithConstraints(
  110. ['screen'],
  111. streamCallback,
  112. failCallback
  113. );
  114. }
  115. /**
  116. * Asks Chrome extension to call chooseDesktopMedia and gets chrome 'desktop' stream for returned stream token.
  117. */
  118. function obtainScreenFromExtension(streamCallback, failCallback) {
  119. // Check for extension API
  120. if(!chrome || !chrome.runtime) {
  121. failCallback("Failed to communicate with extension - no API available");
  122. return;
  123. }
  124. // Sends 'getStream' msg to the extension. Extension id must be defined in the config.
  125. chrome.runtime.sendMessage(
  126. config.chromeExtensionId,
  127. { getStream: true},
  128. function(response) {
  129. if(!response) {
  130. failCallback(chrome.runtime.lastError);
  131. return;
  132. }
  133. console.log("Response from extension: "+response);
  134. if(response.streamId) {
  135. getUserMediaWithConstraints(
  136. ['desktop'],
  137. function(stream) {
  138. streamCallback(stream);
  139. },
  140. failCallback,
  141. null, null, null,
  142. response.streamId);
  143. } else {
  144. failCallback("Extension failed to get the stream");
  145. }
  146. }
  147. );
  148. }