Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

middleware.js 4.9KB

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