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

functions.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 the stored local video track.
  183. *
  184. * @param {Object} state - The redux state.
  185. * @returns {Object}
  186. */
  187. export function getLocalJitsiVideoTrack(state) {
  188. const track = getLocalVideoTrack(state['features/base/tracks']);
  189. return track?.jitsiTrack;
  190. }
  191. /**
  192. * Returns track of specified media type for specified participant id.
  193. *
  194. * @param {Track[]} tracks - List of all tracks.
  195. * @param {MEDIA_TYPE} mediaType - Media type.
  196. * @param {string} participantId - Participant ID.
  197. * @returns {(Track|undefined)}
  198. */
  199. export function getTrackByMediaTypeAndParticipant(
  200. tracks,
  201. mediaType,
  202. participantId) {
  203. return tracks.find(
  204. t => t.participantId === participantId && t.mediaType === mediaType
  205. );
  206. }
  207. /**
  208. * Returns the track if any which corresponds to a specific instance
  209. * of JitsiLocalTrack or JitsiRemoteTrack.
  210. *
  211. * @param {Track[]} tracks - List of all tracks.
  212. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} jitsiTrack - JitsiTrack instance.
  213. * @returns {(Track|undefined)}
  214. */
  215. export function getTrackByJitsiTrack(tracks, jitsiTrack) {
  216. return tracks.find(t => t.jitsiTrack === jitsiTrack);
  217. }
  218. /**
  219. * Returns tracks of specified media type.
  220. *
  221. * @param {Track[]} tracks - List of all tracks.
  222. * @param {MEDIA_TYPE} mediaType - Media type.
  223. * @returns {Track[]}
  224. */
  225. export function getTracksByMediaType(tracks, mediaType) {
  226. return tracks.filter(t => t.mediaType === mediaType);
  227. }
  228. /**
  229. * Checks if the local video track in the given set of tracks is muted.
  230. *
  231. * @param {Track[]} tracks - List of all tracks.
  232. * @returns {Track[]}
  233. */
  234. export function isLocalVideoTrackMuted(tracks) {
  235. const presenterTrack = getLocalTrack(tracks, MEDIA_TYPE.PRESENTER);
  236. const videoTrack = getLocalTrack(tracks, MEDIA_TYPE.VIDEO);
  237. // Make sure we check the mute status of only camera tracks, i.e.,
  238. // presenter track when it exists, camera track when the presenter
  239. // track doesn't exist.
  240. if (presenterTrack) {
  241. return isLocalTrackMuted(tracks, MEDIA_TYPE.PRESENTER);
  242. } else if (videoTrack) {
  243. return videoTrack.videoType === 'camera'
  244. ? isLocalTrackMuted(tracks, MEDIA_TYPE.VIDEO) : true;
  245. }
  246. return true;
  247. }
  248. /**
  249. * Checks if the first local track in the given tracks set is muted.
  250. *
  251. * @param {Track[]} tracks - List of all tracks.
  252. * @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
  253. * @returns {boolean} True if local track is muted or false if the track is
  254. * unmuted or if there are no local tracks of the given media type in the given
  255. * set of tracks.
  256. */
  257. export function isLocalTrackMuted(tracks, mediaType) {
  258. const track = getLocalTrack(tracks, mediaType);
  259. return !track || track.muted;
  260. }
  261. /**
  262. * Returns true if the remote track of the given media type and the given
  263. * participant is muted, false otherwise.
  264. *
  265. * @param {Track[]} tracks - List of all tracks.
  266. * @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
  267. * @param {*} participantId - Participant ID.
  268. * @returns {boolean}
  269. */
  270. export function isRemoteTrackMuted(tracks, mediaType, participantId) {
  271. const track = getTrackByMediaTypeAndParticipant(
  272. tracks, mediaType, participantId);
  273. return !track || track.muted;
  274. }
  275. /**
  276. * Returns whether or not the current environment needs a user interaction with
  277. * the page before any unmute can occur.
  278. *
  279. * @param {Object} state - The redux state.
  280. * @returns {boolean}
  281. */
  282. export function isUserInteractionRequiredForUnmute(state) {
  283. return browser.isUserInteractionRequiredForUnmute()
  284. && window
  285. && window.self !== window.top
  286. && !state['features/base/user-interaction'].interacted;
  287. }
  288. /**
  289. * Mutes or unmutes a specific {@code JitsiLocalTrack}. If the muted state of
  290. * the specified {@code track} is already in accord with the specified
  291. * {@code muted} value, then does nothing.
  292. *
  293. * @param {JitsiLocalTrack} track - The {@code JitsiLocalTrack} to mute or
  294. * unmute.
  295. * @param {boolean} muted - If the specified {@code track} is to be muted, then
  296. * {@code true}; otherwise, {@code false}.
  297. * @returns {Promise}
  298. */
  299. export function setTrackMuted(track, muted) {
  300. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  301. if (track.isMuted() === muted) {
  302. return Promise.resolve();
  303. }
  304. const f = muted ? 'mute' : 'unmute';
  305. return track[f]().catch(error => {
  306. // Track might be already disposed so ignore such an error.
  307. if (error.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  308. // FIXME Emit mute failed, so that the app can show error dialog.
  309. logger.error(`set track ${f} failed`, error);
  310. }
  311. });
  312. }