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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. * @returns {(Track|undefined)}
  97. */
  98. export function getLocalTrack(tracks, mediaType) {
  99. return getLocalTracks(tracks).find(t => t.mediaType === mediaType);
  100. }
  101. /**
  102. * Returns an array containing the local tracks with a (valid)
  103. * {@code JitsiTrack}.
  104. *
  105. * @param {Track[]} tracks - An array containing all local tracks.
  106. * @returns {Track[]}
  107. */
  108. export function getLocalTracks(tracks) {
  109. // XXX A local track is considered ready only once it has its `jitsiTrack`
  110. // property set by the `TRACK_ADDED` action. Until then there is a stub
  111. // added just before the `getUserMedia` call with a cancellable
  112. // `gumInProgress` property which then can be used to destroy the track that
  113. // has not yet been added to the redux store. Once GUM is cancelled, it will
  114. // never make it to the store nor there will be any
  115. // `TRACK_ADDED`/`TRACK_REMOVED` actions dispatched for it.
  116. return tracks.filter(t => t.local && t.jitsiTrack);
  117. }
  118. /**
  119. * Returns local video track.
  120. *
  121. * @param {Track[]} tracks - List of all tracks.
  122. * @returns {(Track|undefined)}
  123. */
  124. export function getLocalVideoTrack(tracks) {
  125. return getLocalTrack(tracks, MEDIA_TYPE.VIDEO);
  126. }
  127. /**
  128. * Returns track of specified media type for specified participant id.
  129. *
  130. * @param {Track[]} tracks - List of all tracks.
  131. * @param {MEDIA_TYPE} mediaType - Media type.
  132. * @param {string} participantId - Participant ID.
  133. * @returns {(Track|undefined)}
  134. */
  135. export function getTrackByMediaTypeAndParticipant(
  136. tracks,
  137. mediaType,
  138. participantId) {
  139. return tracks.find(
  140. t => t.participantId === participantId && t.mediaType === mediaType
  141. );
  142. }
  143. /**
  144. * Returns the track if any which corresponds to a specific instance
  145. * of JitsiLocalTrack or JitsiRemoteTrack.
  146. *
  147. * @param {Track[]} tracks - List of all tracks.
  148. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} jitsiTrack - JitsiTrack instance.
  149. * @returns {(Track|undefined)}
  150. */
  151. export function getTrackByJitsiTrack(tracks, jitsiTrack) {
  152. return tracks.find(t => t.jitsiTrack === jitsiTrack);
  153. }
  154. /**
  155. * Returns tracks of specified media type.
  156. *
  157. * @param {Track[]} tracks - List of all tracks.
  158. * @param {MEDIA_TYPE} mediaType - Media type.
  159. * @returns {Track[]}
  160. */
  161. export function getTracksByMediaType(tracks, mediaType) {
  162. return tracks.filter(t => t.mediaType === mediaType);
  163. }
  164. /**
  165. * Checks if the first local track in the given tracks set is muted.
  166. *
  167. * @param {Track[]} tracks - List of all tracks.
  168. * @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
  169. * @returns {boolean} True if local track is muted or false if the track is
  170. * unmuted or if there are no local tracks of the given media type in the given
  171. * set of tracks.
  172. */
  173. export function isLocalTrackMuted(tracks, mediaType) {
  174. const track = getLocalTrack(tracks, mediaType);
  175. return !track || track.muted;
  176. }
  177. /**
  178. * Mutes or unmutes a specific {@code JitsiLocalTrack}. If the muted state of
  179. * the specified {@code track} is already in accord with the specified
  180. * {@code muted} value, then does nothing.
  181. *
  182. * @param {JitsiLocalTrack} track - The {@code JitsiLocalTrack} to mute or
  183. * unmute.
  184. * @param {boolean} muted - If the specified {@code track} is to be muted, then
  185. * {@code true}; otherwise, {@code false}.
  186. * @returns {Promise}
  187. */
  188. export function setTrackMuted(track, muted) {
  189. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  190. if (track.isMuted() === muted) {
  191. return Promise.resolve();
  192. }
  193. const f = muted ? 'mute' : 'unmute';
  194. return track[f]().catch(error => {
  195. // Track might be already disposed so ignore such an error.
  196. if (error.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  197. // FIXME Emit mute failed, so that the app can show error dialog.
  198. console.error(`set track ${f} failed`, error);
  199. }
  200. });
  201. }