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.

actions.ts 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { IStore } from '../app/types';
  2. import { getLocalJitsiAudioTrack } from '../base/tracks/functions';
  3. import { showErrorNotification } from '../notifications/actions';
  4. import { NOTIFICATION_TIMEOUT_TYPE } from '../notifications/constants';
  5. import { NoiseSuppressionEffect } from '../stream-effects/noise-suppression/NoiseSuppressionEffect';
  6. import { SET_NOISE_SUPPRESSION_ENABLED } from './actionTypes';
  7. import { canEnableNoiseSuppression, isNoiseSuppressionEnabled } from './functions';
  8. import logger from './logger';
  9. /**
  10. * Updates the noise suppression active state.
  11. *
  12. * @param {boolean} enabled - Is noise suppression enabled.
  13. * @returns {{
  14. * type: SET_NOISE_SUPPRESSION_STATE,
  15. * enabled: boolean
  16. * }}
  17. */
  18. export function setNoiseSuppressionEnabledState(enabled: boolean): any {
  19. return {
  20. type: SET_NOISE_SUPPRESSION_ENABLED,
  21. enabled
  22. };
  23. }
  24. /**
  25. * Enabled/disable noise suppression depending on the current state.
  26. *
  27. * @returns {Function}
  28. */
  29. export function toggleNoiseSuppression(): any {
  30. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  31. if (isNoiseSuppressionEnabled(getState())) {
  32. dispatch(setNoiseSuppressionEnabled(false));
  33. } else {
  34. dispatch(setNoiseSuppressionEnabled(true));
  35. }
  36. };
  37. }
  38. /**
  39. * Attempt to enable or disable noise suppression using the {@link NoiseSuppressionEffect}.
  40. *
  41. * @param {boolean} enabled - Enable or disable noise suppression.
  42. *
  43. * @returns {Function}
  44. */
  45. export function setNoiseSuppressionEnabled(enabled: boolean): any {
  46. return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  47. const state = getState();
  48. const { noiseSuppression: nsOptions } = state['features/base/config'];
  49. const localAudio = getLocalJitsiAudioTrack(state);
  50. const noiseSuppressionEnabled = isNoiseSuppressionEnabled(state);
  51. logger.info(`Attempting to set noise suppression enabled state: ${enabled}`);
  52. if (enabled === noiseSuppressionEnabled) {
  53. logger.warn(`Noise suppression enabled state already: ${enabled}`);
  54. return;
  55. }
  56. // If there is no local audio, simply set the enabled state. Once an audio track is created
  57. // the effects list will be applied.
  58. if (!localAudio) {
  59. dispatch(setNoiseSuppressionEnabledState(enabled));
  60. return;
  61. }
  62. try {
  63. if (enabled) {
  64. if (!canEnableNoiseSuppression(state, dispatch, localAudio)) {
  65. return;
  66. }
  67. await localAudio.setEffect(new NoiseSuppressionEffect(nsOptions));
  68. dispatch(setNoiseSuppressionEnabledState(true));
  69. logger.info('Noise suppression enabled.');
  70. } else {
  71. await localAudio.setEffect(undefined);
  72. dispatch(setNoiseSuppressionEnabledState(false));
  73. logger.info('Noise suppression disabled.');
  74. }
  75. } catch (error) {
  76. logger.error(
  77. `Failed to set noise suppression enabled to: ${enabled}`,
  78. error
  79. );
  80. dispatch(showErrorNotification({
  81. titleKey: 'notify.noiseSuppressionFailedTitle'
  82. }, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  83. }
  84. };
  85. }