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

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