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.

middleware.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // @flow
  2. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../base/app';
  3. import { CONFERENCE_JOINED } from '../base/conference';
  4. import {
  5. formatDeviceLabel,
  6. setAudioInputDevice
  7. } from '../base/devices';
  8. import JitsiMeetJS, { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
  9. import { MiddlewareRegistry } from '../base/redux';
  10. import { updateSettings } from '../base/settings';
  11. import { playSound, registerSound, unregisterSound } from '../base/sounds';
  12. import { hideNotification, showNotification } from '../notifications';
  13. import { setNoAudioSignalNotificationUid } from './actions';
  14. import { NO_AUDIO_SIGNAL_SOUND_ID } from './constants';
  15. import { NO_AUDIO_SIGNAL_SOUND_FILE } from './sounds';
  16. MiddlewareRegistry.register(store => next => async action => {
  17. const result = next(action);
  18. const { dispatch } = store;
  19. switch (action.type) {
  20. case APP_WILL_MOUNT:
  21. dispatch(registerSound(NO_AUDIO_SIGNAL_SOUND_ID, NO_AUDIO_SIGNAL_SOUND_FILE));
  22. break;
  23. case APP_WILL_UNMOUNT:
  24. dispatch(unregisterSound(NO_AUDIO_SIGNAL_SOUND_ID));
  25. break;
  26. case CONFERENCE_JOINED:
  27. _handleNoAudioSignalNotification(store, action);
  28. break;
  29. }
  30. return result;
  31. });
  32. /**
  33. * Handles the logic of displaying the no audio input detected notification as well as finding a valid device on the
  34. * system.
  35. *
  36. * @param {Store} store - The redux store in which the specified action is being dispatched.
  37. * @param {Action} action - The redux action {@code CONFERENCE_JOINED} which is being dispatched in the specified redux
  38. * store.
  39. * @private
  40. * @returns {void}
  41. */
  42. async function _handleNoAudioSignalNotification({ dispatch, getState }, action) {
  43. const { conference } = action;
  44. conference.on(JitsiConferenceEvents.AUDIO_INPUT_STATE_CHANGE, hasAudioInput => {
  45. const { noAudioSignalNotificationUid } = getState()['features/no-audio-signal'];
  46. // In case the notification is displayed but the conference detected audio input signal we hide it.
  47. if (noAudioSignalNotificationUid && hasAudioInput) {
  48. dispatch(hideNotification(noAudioSignalNotificationUid));
  49. dispatch(setNoAudioSignalNotificationUid());
  50. }
  51. });
  52. conference.on(JitsiConferenceEvents.NO_AUDIO_INPUT, async () => {
  53. const { noSrcDataNotificationUid } = getState()['features/base/no-src-data'];
  54. // In case the 'no data detected from source' notification was already shown, we prevent the
  55. // no audio signal notification as it's redundant i.e. it's clear that the users microphone is
  56. // muted from system settings.
  57. if (noSrcDataNotificationUid) {
  58. return;
  59. }
  60. const activeDevice = await JitsiMeetJS.getActiveAudioDevice();
  61. // In case there is a previous notification displayed just hide it.
  62. const { noAudioSignalNotificationUid } = getState()['features/no-audio-signal'];
  63. if (noAudioSignalNotificationUid) {
  64. dispatch(hideNotification(noAudioSignalNotificationUid));
  65. dispatch(setNoAudioSignalNotificationUid());
  66. }
  67. let descriptionKey = 'toolbar.noAudioSignalDesc';
  68. let customActionNameKey;
  69. let customActionHandler;
  70. // In case the detector picked up a device show a notification with a device suggestion
  71. if (activeDevice.deviceLabel !== '') {
  72. descriptionKey = 'toolbar.noAudioSignalDescSuggestion';
  73. // Preferably the label should be passed as an argument paired with a i18next string, however
  74. // at the point of the implementation the showNotification function only supports doing that for
  75. // the description.
  76. // TODO Add support for arguments to showNotification title and customAction strings.
  77. customActionNameKey = `Use ${formatDeviceLabel(activeDevice.deviceLabel)}`;
  78. customActionHandler = () => {
  79. // Select device callback
  80. dispatch(
  81. updateSettings({
  82. userSelectedMicDeviceId: activeDevice.deviceId,
  83. userSelectedMicDeviceLabel: activeDevice.deviceLabel
  84. })
  85. );
  86. dispatch(setAudioInputDevice(activeDevice.deviceId));
  87. };
  88. }
  89. const notification = showNotification({
  90. titleKey: 'toolbar.noAudioSignalTitle',
  91. descriptionKey,
  92. customActionNameKey,
  93. customActionHandler
  94. });
  95. dispatch(notification);
  96. dispatch(playSound(NO_AUDIO_SIGNAL_SOUND_ID));
  97. // Store the current notification uid so we can check for this state and hide it in case
  98. // a new track was added, thus changing the context of the notification
  99. dispatch(setNoAudioSignalNotificationUid(notification.uid));
  100. });
  101. }