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

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