您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.js 12KB

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