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.0KB

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