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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. import logger from './logger';
  10. /**
  11. * Creates a local video track for presenter. The constraints are computed based
  12. * on the height of the desktop that is being shared.
  13. *
  14. * @param {Object} options - The options with which the local presenter track
  15. * is to be created.
  16. * @param {string|null} [options.cameraDeviceId] - Camera device id or
  17. * {@code undefined} to use app's settings.
  18. * @param {number} desktopHeight - The height of the desktop that is being
  19. * shared.
  20. * @returns {Promise<JitsiLocalTrack>}
  21. */
  22. export async function createLocalPresenterTrack(options, desktopHeight) {
  23. const { cameraDeviceId } = options;
  24. // compute the constraints of the camera track based on the resolution
  25. // of the desktop screen that is being shared.
  26. const cameraHeights = [ 180, 270, 360, 540, 720 ];
  27. const proportion = 4;
  28. const result = cameraHeights.find(
  29. height => (desktopHeight / proportion) < height);
  30. const constraints = {
  31. video: {
  32. aspectRatio: 4 / 3,
  33. height: {
  34. ideal: result
  35. }
  36. }
  37. };
  38. const [ videoTrack ] = await JitsiMeetJS.createLocalTracks(
  39. {
  40. cameraDeviceId,
  41. constraints,
  42. devices: [ 'video' ]
  43. });
  44. videoTrack.type = MEDIA_TYPE.PRESENTER;
  45. return videoTrack;
  46. }
  47. /**
  48. * Create local tracks of specific types.
  49. *
  50. * @param {Object} options - The options with which the local tracks are to be
  51. * created.
  52. * @param {string|null} [options.cameraDeviceId] - Camera device id or
  53. * {@code undefined} to use app's settings.
  54. * @param {string[]} options.devices - Required track types such as 'audio'
  55. * and/or 'video'.
  56. * @param {string|null} [options.micDeviceId] - Microphone device id or
  57. * {@code undefined} to use app's settings.
  58. * @param {boolean} [firePermissionPromptIsShownEvent] - Whether lib-jitsi-meet
  59. * should check for a {@code getUserMedia} permission prompt and fire a
  60. * corresponding event.
  61. * @param {Object} store - The redux store in the context of which the function
  62. * is to execute and from which state such as {@code config} is to be retrieved.
  63. * @returns {Promise<JitsiLocalTrack[]>}
  64. */
  65. export function createLocalTracksF(
  66. options,
  67. firePermissionPromptIsShownEvent,
  68. store) {
  69. options || (options = {}); // eslint-disable-line no-param-reassign
  70. let { cameraDeviceId, micDeviceId } = options;
  71. if (typeof APP !== 'undefined') {
  72. // TODO The app's settings should go in the redux store and then the
  73. // reliance on the global variable APP will go away.
  74. store || (store = APP.store); // eslint-disable-line no-param-reassign
  75. const state = store.getState();
  76. if (typeof cameraDeviceId === 'undefined' || cameraDeviceId === null) {
  77. cameraDeviceId = getUserSelectedCameraDeviceId(state);
  78. }
  79. if (typeof micDeviceId === 'undefined' || micDeviceId === null) {
  80. micDeviceId = getUserSelectedMicDeviceId(state);
  81. }
  82. }
  83. const state = store.getState();
  84. const {
  85. desktopSharingFrameRate,
  86. firefox_fake_device, // eslint-disable-line camelcase
  87. resolution
  88. } = state['features/base/config'];
  89. const constraints = options.constraints
  90. ?? state['features/base/config'].constraints;
  91. // Do not load blur effect if option for ignoring effects is present.
  92. // This is needed when we are creating a video track for presenter mode.
  93. const loadEffectsPromise = state['features/blur'].blurEnabled
  94. ? getBlurEffect()
  95. .then(blurEffect => [ blurEffect ])
  96. .catch(error => {
  97. logger.error('Failed to obtain the blur effect instance with error: ', error);
  98. return Promise.resolve([]);
  99. })
  100. : Promise.resolve([]);
  101. return (
  102. loadEffectsPromise.then(effects =>
  103. JitsiMeetJS.createLocalTracks(
  104. {
  105. cameraDeviceId,
  106. constraints,
  107. desktopSharingExtensionExternalInstallation:
  108. options.desktopSharingExtensionExternalInstallation,
  109. desktopSharingFrameRate,
  110. desktopSharingSourceDevice:
  111. options.desktopSharingSourceDevice,
  112. desktopSharingSources: options.desktopSharingSources,
  113. // Copy array to avoid mutations inside library.
  114. devices: options.devices.slice(0),
  115. effects,
  116. firefox_fake_device, // eslint-disable-line camelcase
  117. micDeviceId,
  118. resolution
  119. },
  120. firePermissionPromptIsShownEvent)
  121. .catch(err => {
  122. logger.error('Failed to create local tracks', options.devices, err);
  123. return Promise.reject(err);
  124. })));
  125. }
  126. /**
  127. * Returns local audio track.
  128. *
  129. * @param {Track[]} tracks - List of all tracks.
  130. * @returns {(Track|undefined)}
  131. */
  132. export function getLocalAudioTrack(tracks) {
  133. return getLocalTrack(tracks, MEDIA_TYPE.AUDIO);
  134. }
  135. /**
  136. * Returns local track by media type.
  137. *
  138. * @param {Track[]} tracks - List of all tracks.
  139. * @param {MEDIA_TYPE} mediaType - Media type.
  140. * @param {boolean} [includePending] - Indicates whether a local track is to be
  141. * returned if it is still pending. A local track is pending if
  142. * {@code getUserMedia} is still executing to create it and, consequently, its
  143. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  144. * track is not returned.
  145. * @returns {(Track|undefined)}
  146. */
  147. export function getLocalTrack(tracks, mediaType, includePending = false) {
  148. return (
  149. getLocalTracks(tracks, includePending)
  150. .find(t => t.mediaType === mediaType));
  151. }
  152. /**
  153. * Returns an array containing the local tracks with or without a (valid)
  154. * {@code JitsiTrack}.
  155. *
  156. * @param {Track[]} tracks - An array containing all local tracks.
  157. * @param {boolean} [includePending] - Indicates whether a local track is to be
  158. * returned if it is still pending. A local track is pending if
  159. * {@code getUserMedia} is still executing to create it and, consequently, its
  160. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  161. * track is not returned.
  162. * @returns {Track[]}
  163. */
  164. export function getLocalTracks(tracks, includePending = false) {
  165. // XXX A local track is considered ready only once it has its `jitsiTrack`
  166. // property set by the `TRACK_ADDED` action. Until then there is a stub
  167. // added just before the `getUserMedia` call with a cancellable
  168. // `gumInProgress` property which then can be used to destroy the track that
  169. // has not yet been added to the redux store. Once GUM is cancelled, it will
  170. // never make it to the store nor there will be any
  171. // `TRACK_ADDED`/`TRACK_REMOVED` actions dispatched for it.
  172. return tracks.filter(t => t.local && (t.jitsiTrack || includePending));
  173. }
  174. /**
  175. * Returns local video track.
  176. *
  177. * @param {Track[]} tracks - List of all tracks.
  178. * @returns {(Track|undefined)}
  179. */
  180. export function getLocalVideoTrack(tracks) {
  181. return getLocalTrack(tracks, MEDIA_TYPE.VIDEO);
  182. }
  183. /**
  184. * Returns the media type of the local video, presenter or video.
  185. *
  186. * @param {Track[]} tracks - List of all tracks.
  187. * @returns {MEDIA_TYPE}
  188. */
  189. export function getLocalVideoType(tracks) {
  190. const presenterTrack = getLocalTrack(tracks, MEDIA_TYPE.PRESENTER);
  191. return presenterTrack ? MEDIA_TYPE.PRESENTER : MEDIA_TYPE.VIDEO;
  192. }
  193. /**
  194. * Returns track of specified media type for specified participant id.
  195. *
  196. * @param {Track[]} tracks - List of all tracks.
  197. * @param {MEDIA_TYPE} mediaType - Media type.
  198. * @param {string} participantId - Participant ID.
  199. * @returns {(Track|undefined)}
  200. */
  201. export function getTrackByMediaTypeAndParticipant(
  202. tracks,
  203. mediaType,
  204. participantId) {
  205. return tracks.find(
  206. t => t.participantId === participantId && t.mediaType === mediaType
  207. );
  208. }
  209. /**
  210. * Returns the track if any which corresponds to a specific instance
  211. * of JitsiLocalTrack or JitsiRemoteTrack.
  212. *
  213. * @param {Track[]} tracks - List of all tracks.
  214. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} jitsiTrack - JitsiTrack instance.
  215. * @returns {(Track|undefined)}
  216. */
  217. export function getTrackByJitsiTrack(tracks, jitsiTrack) {
  218. return tracks.find(t => t.jitsiTrack === jitsiTrack);
  219. }
  220. /**
  221. * Returns tracks of specified media type.
  222. *
  223. * @param {Track[]} tracks - List of all tracks.
  224. * @param {MEDIA_TYPE} mediaType - Media type.
  225. * @returns {Track[]}
  226. */
  227. export function getTracksByMediaType(tracks, mediaType) {
  228. return tracks.filter(t => t.mediaType === mediaType);
  229. }
  230. /**
  231. * Checks if the local video track in the given set of tracks is muted.
  232. *
  233. * @param {Track[]} tracks - List of all tracks.
  234. * @returns {Track[]}
  235. */
  236. export function isLocalVideoTrackMuted(tracks) {
  237. const presenterTrack = getLocalTrack(tracks, MEDIA_TYPE.PRESENTER);
  238. const videoTrack = getLocalTrack(tracks, MEDIA_TYPE.VIDEO);
  239. // Make sure we check the mute status of only camera tracks, i.e.,
  240. // presenter track when it exists, camera track when the presenter
  241. // track doesn't exist.
  242. if (presenterTrack) {
  243. return isLocalTrackMuted(tracks, MEDIA_TYPE.PRESENTER);
  244. } else if (videoTrack) {
  245. return videoTrack.videoType === 'camera'
  246. ? isLocalTrackMuted(tracks, MEDIA_TYPE.VIDEO) : true;
  247. }
  248. return true;
  249. }
  250. /**
  251. * Checks if the first local track in the given tracks set is muted.
  252. *
  253. * @param {Track[]} tracks - List of all tracks.
  254. * @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
  255. * @returns {boolean} True if local track is muted or false if the track is
  256. * unmuted or if there are no local tracks of the given media type in the given
  257. * set of tracks.
  258. */
  259. export function isLocalTrackMuted(tracks, mediaType) {
  260. const track = getLocalTrack(tracks, mediaType);
  261. return !track || track.muted;
  262. }
  263. /**
  264. * Returns true if the remote track of the given media type and the given
  265. * participant is muted, false otherwise.
  266. *
  267. * @param {Track[]} tracks - List of all tracks.
  268. * @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
  269. * @param {*} participantId - Participant ID.
  270. * @returns {boolean}
  271. */
  272. export function isRemoteTrackMuted(tracks, mediaType, participantId) {
  273. const track = getTrackByMediaTypeAndParticipant(
  274. tracks, mediaType, participantId);
  275. return !track || track.muted;
  276. }
  277. /**
  278. * Returns whether or not the current environment needs a user interaction with
  279. * the page before any unmute can occur.
  280. *
  281. * @param {Object} state - The redux state.
  282. * @returns {boolean}
  283. */
  284. export function isUserInteractionRequiredForUnmute(state) {
  285. return browser.isUserInteractionRequiredForUnmute()
  286. && window
  287. && window.self !== window.top
  288. && !state['features/base/user-interaction'].interacted;
  289. }
  290. /**
  291. * Mutes or unmutes a specific {@code JitsiLocalTrack}. If the muted state of
  292. * the specified {@code track} is already in accord with the specified
  293. * {@code muted} value, then does nothing.
  294. *
  295. * @param {JitsiLocalTrack} track - The {@code JitsiLocalTrack} to mute or
  296. * unmute.
  297. * @param {boolean} muted - If the specified {@code track} is to be muted, then
  298. * {@code true}; otherwise, {@code false}.
  299. * @returns {Promise}
  300. */
  301. export function setTrackMuted(track, muted) {
  302. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  303. if (track.isMuted() === muted) {
  304. return Promise.resolve();
  305. }
  306. const f = muted ? 'mute' : 'unmute';
  307. return track[f]().catch(error => {
  308. // Track might be already disposed so ignore such an error.
  309. if (error.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  310. // FIXME Emit mute failed, so that the app can show error dialog.
  311. logger.error(`set track ${f} failed`, error);
  312. }
  313. });
  314. }