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

functions.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* global APP */
  2. import JitsiMeetJS, { JitsiTrackErrors } from '../lib-jitsi-meet';
  3. import { MEDIA_TYPE } from '../media';
  4. import {
  5. getUserSelectedCameraDeviceId,
  6. getUserSelectedMicDeviceId
  7. } from '../settings';
  8. const logger = require('jitsi-meet-logger').getLogger(__filename);
  9. /**
  10. * Create local tracks of specific types.
  11. *
  12. * @param {Object} options - The options with which the local tracks are to be
  13. * created.
  14. * @param {string|null} [options.cameraDeviceId] - Camera device id or
  15. * {@code undefined} to use app's settings.
  16. * @param {string[]} options.devices - Required track types such as 'audio'
  17. * and/or 'video'.
  18. * @param {string|null} [options.micDeviceId] - Microphone device id or
  19. * {@code undefined} to use app's settings.
  20. * @param {boolean} [firePermissionPromptIsShownEvent] - Whether lib-jitsi-meet
  21. * should check for a {@code getUserMedia} permission prompt and fire a
  22. * corresponding event.
  23. * @param {Object} store - The redux store in the context of which the function
  24. * is to execute and from which state such as {@code config} is to be retrieved.
  25. * @returns {Promise<JitsiLocalTrack[]>}
  26. */
  27. export function createLocalTracksF(
  28. options,
  29. firePermissionPromptIsShownEvent,
  30. store) {
  31. options || (options = {}); // eslint-disable-line no-param-reassign
  32. let { cameraDeviceId, micDeviceId } = options;
  33. if (typeof APP !== 'undefined') {
  34. // TODO The app's settings should go in the redux store and then the
  35. // reliance on the global variable APP will go away.
  36. store || (store = APP.store); // eslint-disable-line no-param-reassign
  37. const state = store.getState();
  38. if (typeof cameraDeviceId === 'undefined' || cameraDeviceId === null) {
  39. cameraDeviceId = getUserSelectedCameraDeviceId(state);
  40. }
  41. if (typeof micDeviceId === 'undefined' || micDeviceId === null) {
  42. micDeviceId = getUserSelectedMicDeviceId(state);
  43. }
  44. }
  45. const {
  46. constraints,
  47. desktopSharingFrameRate,
  48. firefox_fake_device, // eslint-disable-line camelcase
  49. resolution
  50. } = store.getState()['features/base/config'];
  51. return (
  52. JitsiMeetJS.createLocalTracks(
  53. {
  54. cameraDeviceId,
  55. constraints,
  56. desktopSharingExtensionExternalInstallation:
  57. options.desktopSharingExtensionExternalInstallation,
  58. desktopSharingFrameRate,
  59. desktopSharingSourceDevice:
  60. options.desktopSharingSourceDevice,
  61. desktopSharingSources: options.desktopSharingSources,
  62. // Copy array to avoid mutations inside library.
  63. devices: options.devices.slice(0),
  64. firefox_fake_device, // eslint-disable-line camelcase
  65. micDeviceId,
  66. resolution
  67. },
  68. firePermissionPromptIsShownEvent)
  69. .catch(err => {
  70. logger.error('Failed to create local tracks', options.devices, err);
  71. return Promise.reject(err);
  72. }));
  73. }
  74. /**
  75. * Returns local audio track.
  76. *
  77. * @param {Track[]} tracks - List of all tracks.
  78. * @returns {(Track|undefined)}
  79. */
  80. export function getLocalAudioTrack(tracks) {
  81. return getLocalTrack(tracks, MEDIA_TYPE.AUDIO);
  82. }
  83. /**
  84. * Returns local track by media type.
  85. *
  86. * @param {Track[]} tracks - List of all tracks.
  87. * @param {MEDIA_TYPE} mediaType - Media type.
  88. * @param {boolean} [includePending] - Indicates whether a local track is to be
  89. * returned if it is still pending. A local track is pending if
  90. * {@code getUserMedia} is still executing to create it and, consequently, its
  91. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  92. * track is not returned.
  93. * @returns {(Track|undefined)}
  94. */
  95. export function getLocalTrack(tracks, mediaType, includePending = false) {
  96. return (
  97. getLocalTracks(tracks, includePending)
  98. .find(t => t.mediaType === mediaType));
  99. }
  100. /**
  101. * Returns an array containing the local tracks with or without a (valid)
  102. * {@code JitsiTrack}.
  103. *
  104. * @param {Track[]} tracks - An array containing all local tracks.
  105. * @param {boolean} [includePending] - Indicates whether a local track is to be
  106. * returned if it is still pending. A local track is pending if
  107. * {@code getUserMedia} is still executing to create it and, consequently, its
  108. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  109. * track is not returned.
  110. * @returns {Track[]}
  111. */
  112. export function getLocalTracks(tracks, includePending = false) {
  113. // XXX A local track is considered ready only once it has its `jitsiTrack`
  114. // property set by the `TRACK_ADDED` action. Until then there is a stub
  115. // added just before the `getUserMedia` call with a cancellable
  116. // `gumInProgress` property which then can be used to destroy the track that
  117. // has not yet been added to the redux store. Once GUM is cancelled, it will
  118. // never make it to the store nor there will be any
  119. // `TRACK_ADDED`/`TRACK_REMOVED` actions dispatched for it.
  120. return tracks.filter(t => t.local && (t.jitsiTrack || includePending));
  121. }
  122. /**
  123. * Returns local video track.
  124. *
  125. * @param {Track[]} tracks - List of all tracks.
  126. * @returns {(Track|undefined)}
  127. */
  128. export function getLocalVideoTrack(tracks) {
  129. return getLocalTrack(tracks, MEDIA_TYPE.VIDEO);
  130. }
  131. /**
  132. * Returns track of specified media type for specified participant id.
  133. *
  134. * @param {Track[]} tracks - List of all tracks.
  135. * @param {MEDIA_TYPE} mediaType - Media type.
  136. * @param {string} participantId - Participant ID.
  137. * @returns {(Track|undefined)}
  138. */
  139. export function getTrackByMediaTypeAndParticipant(
  140. tracks,
  141. mediaType,
  142. participantId) {
  143. return tracks.find(
  144. t => t.participantId === participantId && t.mediaType === mediaType
  145. );
  146. }
  147. /**
  148. * Returns the track if any which corresponds to a specific instance
  149. * of JitsiLocalTrack or JitsiRemoteTrack.
  150. *
  151. * @param {Track[]} tracks - List of all tracks.
  152. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} jitsiTrack - JitsiTrack instance.
  153. * @returns {(Track|undefined)}
  154. */
  155. export function getTrackByJitsiTrack(tracks, jitsiTrack) {
  156. return tracks.find(t => t.jitsiTrack === jitsiTrack);
  157. }
  158. /**
  159. * Returns tracks of specified media type.
  160. *
  161. * @param {Track[]} tracks - List of all tracks.
  162. * @param {MEDIA_TYPE} mediaType - Media type.
  163. * @returns {Track[]}
  164. */
  165. export function getTracksByMediaType(tracks, mediaType) {
  166. return tracks.filter(t => t.mediaType === mediaType);
  167. }
  168. /**
  169. * Checks if the first local track in the given tracks set is muted.
  170. *
  171. * @param {Track[]} tracks - List of all tracks.
  172. * @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
  173. * @returns {boolean} True if local track is muted or false if the track is
  174. * unmuted or if there are no local tracks of the given media type in the given
  175. * set of tracks.
  176. */
  177. export function isLocalTrackMuted(tracks, mediaType) {
  178. const track = getLocalTrack(tracks, mediaType);
  179. return !track || track.muted;
  180. }
  181. /**
  182. * Returns true if the remote track of the given media type and the given
  183. * participant is muted, false otherwise.
  184. *
  185. * @param {Track[]} tracks - List of all tracks.
  186. * @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
  187. * @param {*} participantId - Participant ID.
  188. * @returns {boolean}
  189. */
  190. export function isRemoteTrackMuted(tracks, mediaType, participantId) {
  191. const track = getTrackByMediaTypeAndParticipant(
  192. tracks, mediaType, participantId);
  193. return !track || track.muted;
  194. }
  195. /**
  196. * Mutes or unmutes a specific {@code JitsiLocalTrack}. If the muted state of
  197. * the specified {@code track} is already in accord with the specified
  198. * {@code muted} value, then does nothing.
  199. *
  200. * @param {JitsiLocalTrack} track - The {@code JitsiLocalTrack} to mute or
  201. * unmute.
  202. * @param {boolean} muted - If the specified {@code track} is to be muted, then
  203. * {@code true}; otherwise, {@code false}.
  204. * @returns {Promise}
  205. */
  206. export function setTrackMuted(track, muted) {
  207. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  208. if (track.isMuted() === muted) {
  209. return Promise.resolve();
  210. }
  211. const f = muted ? 'mute' : 'unmute';
  212. return track[f]().catch(error => {
  213. // Track might be already disposed so ignore such an error.
  214. if (error.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  215. // FIXME Emit mute failed, so that the app can show error dialog.
  216. logger.error(`set track ${f} failed`, error);
  217. }
  218. });
  219. }