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 9.1KB

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