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

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