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.

functions.js 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* global APP */
  2. import { getBlurEffect } from '../../blur';
  3. import JitsiMeetJS, { JitsiTrackErrors, browser } from '../lib-jitsi-meet';
  4. import { MEDIA_TYPE } from '../media';
  5. import {
  6. getUserSelectedCameraDeviceId,
  7. getUserSelectedMicDeviceId
  8. } from '../settings';
  9. const logger = require('jitsi-meet-logger').getLogger(__filename);
  10. /**
  11. * Create local tracks of specific types.
  12. *
  13. * @param {Object} options - The options with which the local tracks are to be
  14. * created.
  15. * @param {string|null} [options.cameraDeviceId] - Camera device id or
  16. * {@code undefined} to use app's settings.
  17. * @param {string[]} options.devices - Required track types such as 'audio'
  18. * and/or 'video'.
  19. * @param {string|null} [options.micDeviceId] - Microphone device id or
  20. * {@code undefined} to use app's settings.
  21. * @param {boolean} [firePermissionPromptIsShownEvent] - Whether lib-jitsi-meet
  22. * should check for a {@code getUserMedia} permission prompt and fire a
  23. * corresponding event.
  24. * @param {Object} store - The redux store in the context of which the function
  25. * is to execute and from which state such as {@code config} is to be retrieved.
  26. * @returns {Promise<JitsiLocalTrack[]>}
  27. */
  28. export function createLocalTracksF(
  29. options,
  30. firePermissionPromptIsShownEvent,
  31. store) {
  32. options || (options = {}); // eslint-disable-line no-param-reassign
  33. let { cameraDeviceId, micDeviceId } = options;
  34. if (typeof APP !== 'undefined') {
  35. // TODO The app's settings should go in the redux store and then the
  36. // reliance on the global variable APP will go away.
  37. store || (store = APP.store); // eslint-disable-line no-param-reassign
  38. const state = store.getState();
  39. if (typeof cameraDeviceId === 'undefined' || cameraDeviceId === null) {
  40. cameraDeviceId = getUserSelectedCameraDeviceId(state);
  41. }
  42. if (typeof micDeviceId === 'undefined' || micDeviceId === null) {
  43. micDeviceId = getUserSelectedMicDeviceId(state);
  44. }
  45. }
  46. const state = store.getState();
  47. const {
  48. constraints,
  49. desktopSharingFrameRate,
  50. firefox_fake_device, // eslint-disable-line camelcase
  51. resolution
  52. } = state['features/base/config'];
  53. const loadEffectsPromise = state['features/blur'].blurEnabled
  54. ? getBlurEffect()
  55. .then(blurEffect => [ blurEffect ])
  56. .catch(error => {
  57. logger.error('Failed to obtain the blur effect instance with error: ', error);
  58. return Promise.resolve([]);
  59. })
  60. : Promise.resolve([]);
  61. return (
  62. loadEffectsPromise.then(effects =>
  63. JitsiMeetJS.createLocalTracks(
  64. {
  65. cameraDeviceId,
  66. constraints,
  67. desktopSharingExtensionExternalInstallation:
  68. options.desktopSharingExtensionExternalInstallation,
  69. desktopSharingFrameRate,
  70. desktopSharingSourceDevice:
  71. options.desktopSharingSourceDevice,
  72. desktopSharingSources: options.desktopSharingSources,
  73. // Copy array to avoid mutations inside library.
  74. devices: options.devices.slice(0),
  75. effects,
  76. firefox_fake_device, // eslint-disable-line camelcase
  77. micDeviceId,
  78. resolution
  79. },
  80. firePermissionPromptIsShownEvent)
  81. .catch(err => {
  82. logger.error('Failed to create local tracks', options.devices, err);
  83. return Promise.reject(err);
  84. })));
  85. }
  86. /**
  87. * Returns local audio track.
  88. *
  89. * @param {Track[]} tracks - List of all tracks.
  90. * @returns {(Track|undefined)}
  91. */
  92. export function getLocalAudioTrack(tracks) {
  93. return getLocalTrack(tracks, MEDIA_TYPE.AUDIO);
  94. }
  95. /**
  96. * Returns local track by media type.
  97. *
  98. * @param {Track[]} tracks - List of all tracks.
  99. * @param {MEDIA_TYPE} mediaType - Media type.
  100. * @param {boolean} [includePending] - Indicates whether a local track is to be
  101. * returned if it is still pending. A local track is pending if
  102. * {@code getUserMedia} is still executing to create it and, consequently, its
  103. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  104. * track is not returned.
  105. * @returns {(Track|undefined)}
  106. */
  107. export function getLocalTrack(tracks, mediaType, includePending = false) {
  108. return (
  109. getLocalTracks(tracks, includePending)
  110. .find(t => t.mediaType === mediaType));
  111. }
  112. /**
  113. * Returns an array containing the local tracks with or without a (valid)
  114. * {@code JitsiTrack}.
  115. *
  116. * @param {Track[]} tracks - An array containing all local tracks.
  117. * @param {boolean} [includePending] - Indicates whether a local track is to be
  118. * returned if it is still pending. A local track is pending if
  119. * {@code getUserMedia} is still executing to create it and, consequently, its
  120. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  121. * track is not returned.
  122. * @returns {Track[]}
  123. */
  124. export function getLocalTracks(tracks, includePending = false) {
  125. // XXX A local track is considered ready only once it has its `jitsiTrack`
  126. // property set by the `TRACK_ADDED` action. Until then there is a stub
  127. // added just before the `getUserMedia` call with a cancellable
  128. // `gumInProgress` property which then can be used to destroy the track that
  129. // has not yet been added to the redux store. Once GUM is cancelled, it will
  130. // never make it to the store nor there will be any
  131. // `TRACK_ADDED`/`TRACK_REMOVED` actions dispatched for it.
  132. return tracks.filter(t => t.local && (t.jitsiTrack || includePending));
  133. }
  134. /**
  135. * Returns local video track.
  136. *
  137. * @param {Track[]} tracks - List of all tracks.
  138. * @returns {(Track|undefined)}
  139. */
  140. export function getLocalVideoTrack(tracks) {
  141. return getLocalTrack(tracks, MEDIA_TYPE.VIDEO);
  142. }
  143. /**
  144. * Returns track of specified media type for specified participant id.
  145. *
  146. * @param {Track[]} tracks - List of all tracks.
  147. * @param {MEDIA_TYPE} mediaType - Media type.
  148. * @param {string} participantId - Participant ID.
  149. * @returns {(Track|undefined)}
  150. */
  151. export function getTrackByMediaTypeAndParticipant(
  152. tracks,
  153. mediaType,
  154. participantId) {
  155. return tracks.find(
  156. t => t.participantId === participantId && t.mediaType === mediaType
  157. );
  158. }
  159. /**
  160. * Returns the track if any which corresponds to a specific instance
  161. * of JitsiLocalTrack or JitsiRemoteTrack.
  162. *
  163. * @param {Track[]} tracks - List of all tracks.
  164. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} jitsiTrack - JitsiTrack instance.
  165. * @returns {(Track|undefined)}
  166. */
  167. export function getTrackByJitsiTrack(tracks, jitsiTrack) {
  168. return tracks.find(t => t.jitsiTrack === jitsiTrack);
  169. }
  170. /**
  171. * Returns tracks of specified media type.
  172. *
  173. * @param {Track[]} tracks - List of all tracks.
  174. * @param {MEDIA_TYPE} mediaType - Media type.
  175. * @returns {Track[]}
  176. */
  177. export function getTracksByMediaType(tracks, mediaType) {
  178. return tracks.filter(t => t.mediaType === mediaType);
  179. }
  180. /**
  181. * Checks if the first local track in the given tracks set is muted.
  182. *
  183. * @param {Track[]} tracks - List of all tracks.
  184. * @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
  185. * @returns {boolean} True if local track is muted or false if the track is
  186. * unmuted or if there are no local tracks of the given media type in the given
  187. * set of tracks.
  188. */
  189. export function isLocalTrackMuted(tracks, mediaType) {
  190. const track = getLocalTrack(tracks, mediaType);
  191. return !track || track.muted;
  192. }
  193. /**
  194. * Returns true if the remote track of the given media type and the given
  195. * participant is muted, false otherwise.
  196. *
  197. * @param {Track[]} tracks - List of all tracks.
  198. * @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
  199. * @param {*} participantId - Participant ID.
  200. * @returns {boolean}
  201. */
  202. export function isRemoteTrackMuted(tracks, mediaType, participantId) {
  203. const track = getTrackByMediaTypeAndParticipant(
  204. tracks, mediaType, participantId);
  205. return !track || track.muted;
  206. }
  207. /**
  208. * Returns whether or not the current environment needs a user interaction with
  209. * the page before any unmute can occur.
  210. *
  211. * @param {Object} state - The redux state.
  212. * @returns {boolean}
  213. */
  214. export function isUserInteractionRequiredForUnmute(state) {
  215. return browser.isUserInteractionRequiredForUnmute()
  216. && window
  217. && window.self !== window.top
  218. && !state['features/base/user-interaction'].interacted;
  219. }
  220. /**
  221. * Mutes or unmutes a specific {@code JitsiLocalTrack}. If the muted state of
  222. * the specified {@code track} is already in accord with the specified
  223. * {@code muted} value, then does nothing.
  224. *
  225. * @param {JitsiLocalTrack} track - The {@code JitsiLocalTrack} to mute or
  226. * unmute.
  227. * @param {boolean} muted - If the specified {@code track} is to be muted, then
  228. * {@code true}; otherwise, {@code false}.
  229. * @returns {Promise}
  230. */
  231. export function setTrackMuted(track, muted) {
  232. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  233. if (track.isMuted() === muted) {
  234. return Promise.resolve();
  235. }
  236. const f = muted ? 'mute' : 'unmute';
  237. return track[f]().catch(error => {
  238. // Track might be already disposed so ignore such an error.
  239. if (error.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  240. // FIXME Emit mute failed, so that the app can show error dialog.
  241. logger.error(`set track ${f} failed`, error);
  242. }
  243. });
  244. }