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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. desktopSharingSources: options.desktopSharingSources,
  57. // Copy array to avoid mutations inside library.
  58. devices: options.devices.slice(0),
  59. firefox_fake_device, // eslint-disable-line camelcase
  60. micDeviceId,
  61. resolution
  62. },
  63. firePermissionPromptIsShownEvent)
  64. .then(tracks => {
  65. // TODO JitsiTrackEvents.NO_DATA_FROM_SOURCE should probably be
  66. // dispatched in the redux store here and then
  67. // APP.UI.showTrackNotWorkingDialog should be in a middleware
  68. // somewhere else.
  69. if (typeof APP !== 'undefined') {
  70. tracks.forEach(track =>
  71. track.on(
  72. JitsiTrackEvents.NO_DATA_FROM_SOURCE,
  73. APP.UI.showTrackNotWorkingDialog.bind(
  74. null, track.isAudioTrack())));
  75. }
  76. return tracks;
  77. })
  78. .catch(err => {
  79. logger.error('Failed to create local tracks', options.devices, err);
  80. return Promise.reject(err);
  81. }));
  82. }
  83. /**
  84. * Returns local audio track.
  85. *
  86. * @param {Track[]} tracks - List of all tracks.
  87. * @returns {(Track|undefined)}
  88. */
  89. export function getLocalAudioTrack(tracks) {
  90. return getLocalTrack(tracks, MEDIA_TYPE.AUDIO);
  91. }
  92. /**
  93. * Returns local track by media type.
  94. *
  95. * @param {Track[]} tracks - List of all tracks.
  96. * @param {MEDIA_TYPE} mediaType - Media type.
  97. * @param {boolean} [includePending] - Indicates whether a local track is to be
  98. * returned if it is still pending. A local track is pending if
  99. * {@code getUserMedia} is still executing to create it and, consequently, its
  100. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  101. * track is not returned.
  102. * @returns {(Track|undefined)}
  103. */
  104. export function getLocalTrack(tracks, mediaType, includePending = false) {
  105. return (
  106. getLocalTracks(tracks, includePending)
  107. .find(t => t.mediaType === mediaType));
  108. }
  109. /**
  110. * Returns an array containing the local tracks with or without a (valid)
  111. * {@code JitsiTrack}.
  112. *
  113. * @param {Track[]} tracks - An array containing all local tracks.
  114. * @param {boolean} [includePending] - Indicates whether a local track is to be
  115. * returned if it is still pending. A local track is pending if
  116. * {@code getUserMedia} is still executing to create it and, consequently, its
  117. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  118. * track is not returned.
  119. * @returns {Track[]}
  120. */
  121. export function getLocalTracks(tracks, includePending = false) {
  122. // XXX A local track is considered ready only once it has its `jitsiTrack`
  123. // property set by the `TRACK_ADDED` action. Until then there is a stub
  124. // added just before the `getUserMedia` call with a cancellable
  125. // `gumInProgress` property which then can be used to destroy the track that
  126. // has not yet been added to the redux store. Once GUM is cancelled, it will
  127. // never make it to the store nor there will be any
  128. // `TRACK_ADDED`/`TRACK_REMOVED` actions dispatched for it.
  129. return tracks.filter(t => t.local && (t.jitsiTrack || includePending));
  130. }
  131. /**
  132. * Returns local video track.
  133. *
  134. * @param {Track[]} tracks - List of all tracks.
  135. * @returns {(Track|undefined)}
  136. */
  137. export function getLocalVideoTrack(tracks) {
  138. return getLocalTrack(tracks, MEDIA_TYPE.VIDEO);
  139. }
  140. /**
  141. * Returns track of specified media type for specified participant id.
  142. *
  143. * @param {Track[]} tracks - List of all tracks.
  144. * @param {MEDIA_TYPE} mediaType - Media type.
  145. * @param {string} participantId - Participant ID.
  146. * @returns {(Track|undefined)}
  147. */
  148. export function getTrackByMediaTypeAndParticipant(
  149. tracks,
  150. mediaType,
  151. participantId) {
  152. return tracks.find(
  153. t => t.participantId === participantId && t.mediaType === mediaType
  154. );
  155. }
  156. /**
  157. * Returns the track if any which corresponds to a specific instance
  158. * of JitsiLocalTrack or JitsiRemoteTrack.
  159. *
  160. * @param {Track[]} tracks - List of all tracks.
  161. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} jitsiTrack - JitsiTrack instance.
  162. * @returns {(Track|undefined)}
  163. */
  164. export function getTrackByJitsiTrack(tracks, jitsiTrack) {
  165. return tracks.find(t => t.jitsiTrack === jitsiTrack);
  166. }
  167. /**
  168. * Returns tracks of specified media type.
  169. *
  170. * @param {Track[]} tracks - List of all tracks.
  171. * @param {MEDIA_TYPE} mediaType - Media type.
  172. * @returns {Track[]}
  173. */
  174. export function getTracksByMediaType(tracks, mediaType) {
  175. return tracks.filter(t => t.mediaType === mediaType);
  176. }
  177. /**
  178. * Checks if the first local track in the given tracks set is muted.
  179. *
  180. * @param {Track[]} tracks - List of all tracks.
  181. * @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
  182. * @returns {boolean} True if local track is muted or false if the track is
  183. * unmuted or if there are no local tracks of the given media type in the given
  184. * set of tracks.
  185. */
  186. export function isLocalTrackMuted(tracks, mediaType) {
  187. const track = getLocalTrack(tracks, mediaType);
  188. return !track || track.muted;
  189. }
  190. /**
  191. * Returns true if the remote track of the given media type and the given
  192. * participant is muted, false otherwise.
  193. *
  194. * @param {Track[]} tracks - List of all tracks.
  195. * @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
  196. * @param {*} participantId - Participant ID.
  197. * @returns {boolean}
  198. */
  199. export function isRemoteTrackMuted(tracks, mediaType, participantId) {
  200. const track = getTrackByMediaTypeAndParticipant(
  201. tracks, mediaType, participantId);
  202. return !track || track.muted;
  203. }
  204. /**
  205. * Mutes or unmutes a specific {@code JitsiLocalTrack}. If the muted state of
  206. * the specified {@code track} is already in accord with the specified
  207. * {@code muted} value, then does nothing.
  208. *
  209. * @param {JitsiLocalTrack} track - The {@code JitsiLocalTrack} to mute or
  210. * unmute.
  211. * @param {boolean} muted - If the specified {@code track} is to be muted, then
  212. * {@code true}; otherwise, {@code false}.
  213. * @returns {Promise}
  214. */
  215. export function setTrackMuted(track, muted) {
  216. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  217. if (track.isMuted() === muted) {
  218. return Promise.resolve();
  219. }
  220. const f = muted ? 'mute' : 'unmute';
  221. return track[f]().catch(error => {
  222. // Track might be already disposed so ignore such an error.
  223. if (error.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  224. // FIXME Emit mute failed, so that the app can show error dialog.
  225. logger.error(`set track ${f} failed`, error);
  226. }
  227. });
  228. }