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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 tracks.find(t => t.local && t.mediaType === mediaType);
  97. }
  98. /**
  99. * Returns local video track.
  100. *
  101. * @param {Track[]} tracks - List of all tracks.
  102. * @returns {(Track|undefined)}
  103. */
  104. export function getLocalVideoTrack(tracks) {
  105. return getLocalTrack(tracks, MEDIA_TYPE.VIDEO);
  106. }
  107. /**
  108. * Returns track of specified media type for specified participant id.
  109. *
  110. * @param {Track[]} tracks - List of all tracks.
  111. * @param {MEDIA_TYPE} mediaType - Media type.
  112. * @param {string} participantId - Participant ID.
  113. * @returns {(Track|undefined)}
  114. */
  115. export function getTrackByMediaTypeAndParticipant(
  116. tracks,
  117. mediaType,
  118. participantId) {
  119. return tracks.find(
  120. t => t.participantId === participantId && t.mediaType === mediaType
  121. );
  122. }
  123. /**
  124. * Returns the track if any which corresponds to a specific instance
  125. * of JitsiLocalTrack or JitsiRemoteTrack.
  126. *
  127. * @param {Track[]} tracks - List of all tracks.
  128. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} jitsiTrack - JitsiTrack instance.
  129. * @returns {(Track|undefined)}
  130. */
  131. export function getTrackByJitsiTrack(tracks, jitsiTrack) {
  132. return tracks.find(t => t.jitsiTrack === jitsiTrack);
  133. }
  134. /**
  135. * Returns tracks of specified media type.
  136. *
  137. * @param {Track[]} tracks - List of all tracks.
  138. * @param {MEDIA_TYPE} mediaType - Media type.
  139. * @returns {Track[]}
  140. */
  141. export function getTracksByMediaType(tracks, mediaType) {
  142. return tracks.filter(t => t.mediaType === mediaType);
  143. }
  144. /**
  145. * Checks if the first local track in the given tracks set is muted.
  146. *
  147. * @param {Track[]} tracks - List of all tracks.
  148. * @param {MEDIA_TYPE} mediaType - The media type of tracks to be checked.
  149. * @returns {boolean} True if local track is muted or false if the track is
  150. * unmuted or if there are no local tracks of the given media type in the given
  151. * set of tracks.
  152. */
  153. export function isLocalTrackMuted(tracks, mediaType) {
  154. const track = getLocalTrack(tracks, mediaType);
  155. return !track || track.muted;
  156. }
  157. /**
  158. * Mutes or unmutes a specific {@code JitsiLocalTrack}. If the muted state of
  159. * the specified {@code track} is already in accord with the specified
  160. * {@code muted} value, then does nothing.
  161. *
  162. * @param {JitsiLocalTrack} track - The {@code JitsiLocalTrack} to mute or
  163. * unmute.
  164. * @param {boolean} muted - If the specified {@code track} is to be muted, then
  165. * {@code true}; otherwise, {@code false}.
  166. * @returns {Promise}
  167. */
  168. export function setTrackMuted(track, muted) {
  169. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  170. if (track.isMuted() === muted) {
  171. return Promise.resolve();
  172. }
  173. const f = muted ? 'mute' : 'unmute';
  174. return track[f]().catch(error => {
  175. // FIXME emit mute failed, so that the app can show error dialog
  176. console.error(`set track ${f} failed`, error);
  177. });
  178. }