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

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