您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 4.8KB

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