File: media_stream_audio_processing_layout.cc

package info (click to toggle)
chromium 141.0.7390.107-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,254,812 kB
  • sloc: cpp: 35,264,957; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,636; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,319; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (326 lines) | stat: -rw-r--r-- 12,252 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/mediastream/media_stream_audio_processing_layout.h"

#include "build/build_config.h"
#include "media/base/audio_parameters.h"
#include "media/base/media_switches.h"

namespace blink {

namespace {

static constexpr media::AudioProcessingSettings kDummyWebrtcSettings{
    .echo_cancellation = false,
    .noise_suppression = false,
    .automatic_gain_control = false,
    .multi_channel_capture_processing = false,
    .use_loopback_aec_reference = false};

// Returns whether system noise suppression is allowed to be used regardless of
// whether the noise suppression constraint is set, or whether a browser-based
// AEC is active. This is currently the default on at least MacOS but is not
// allowed for ChromeOS or Windows setups. On Windows, the system effects AEC,
// NS and AGC always come as a "package" and it it not possible to enable or
// disable the system NS independently. TODO(crbug.com/417413190): delete if not
// relevant any more.
constexpr bool IsIndependentSystemNsAllowed() {
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN)
  return false;
#else
  return true;
#endif
}

// Returns `enabled_platform_effects` adjusted based on the requested
// processing.
int ConfigureEchoCancellationEffects(const EchoCanceller& echo_canceller,
                                     bool ns_requested,
                                     bool agc_requested,
                                     int enabled_platform_effects) {
  if (!echo_canceller.IsPlatformProvided()) {
    // No platform processing if platform AEC is not requested.
    enabled_platform_effects &= ~media::AudioParameters::ECHO_CANCELLER;
    enabled_platform_effects &= ~media::AudioParameters::AUTOMATIC_GAIN_CONTROL;
    if (!IsIndependentSystemNsAllowed()) {
      // Special case for NS. TODO(crbug.com/417413190): Rethink.
      enabled_platform_effects &= ~media::AudioParameters::NOISE_SUPPRESSION;
    }
    return enabled_platform_effects;
  }

  // Platform echo cancellation is requested.
  // TODO(crbug.com/405165917): CHECK(platform_effects &
  // media::AudioParameters::ECHO_CANCELLER);

#if !BUILDFLAG(IS_WIN)
  // On Windows  can only disable platform NS and AGC effects if platform
  // AEC effect is disabled.

  // Disable platform NS effect if it's not requested.
  if (!ns_requested) {
    if (!IsIndependentSystemNsAllowed()) {
      // Special case for NS. TODO(crbug.com/417413190): Rethink.
      enabled_platform_effects &= ~media::AudioParameters::NOISE_SUPPRESSION;
    }
  }

  // Disable platform AGC effect if not requested.
  if (!agc_requested) {
    enabled_platform_effects &= ~media::AudioParameters::AUTOMATIC_GAIN_CONTROL;
  }
#endif

  return enabled_platform_effects;
}

#if BUILDFLAG(IS_CHROMEOS)
// Adjusts voice processing bits of `enabled_platform_effects` based on what
// is requested and returns the adjusted value.
int UpdateVoiceIsolationEffects(
    bool use_chrome_aec,
    AudioProcessingProperties::VoiceIsolationType voice_isolation,
    int enabled_platform_effects) {
  if (!(base::FeatureList::IsEnabled(media::kCrOSSystemVoiceIsolationOption) &&
        enabled_platform_effects &
            media::AudioParameters::VOICE_ISOLATION_SUPPORTED)) {
    return enabled_platform_effects;
  }

  if (use_chrome_aec || voice_isolation ==
                            AudioProcessingProperties::VoiceIsolationType::
                                kVoiceIsolationDisabled) {
    // Force voice isolation effect to be disabled if disabled in the
    // properties, or if browser-based AEC is enabled (platform voice
    // isolation would break browser-based AEC).
    enabled_platform_effects |=
        media::AudioParameters::CLIENT_CONTROLLED_VOICE_ISOLATION;
    enabled_platform_effects &= ~media::AudioParameters::VOICE_ISOLATION;
  } else if (voice_isolation == AudioProcessingProperties::VoiceIsolationType::
                                    kVoiceIsolationEnabled) {
    // No browser-based AEC involved; voice isolation is enabled in the
    // properties: force voice isolation to be enabled in the effects.
    enabled_platform_effects |=
        media::AudioParameters::CLIENT_CONTROLLED_VOICE_ISOLATION;

    enabled_platform_effects |= media::AudioParameters::VOICE_ISOLATION;
  } else {
    // Turn off voice isolation control.
    enabled_platform_effects &=
        ~media::AudioParameters::CLIENT_CONTROLLED_VOICE_ISOLATION;
  }

  return enabled_platform_effects;
}
#endif

int ApplyPropertiesToEffects(const EchoCanceller& echo_canceller,
                             const AudioProcessingProperties& properties,
                             int enabled_platform_effects) {
  enabled_platform_effects = ConfigureEchoCancellationEffects(
      echo_canceller,
      /*ns_requested=*/properties.noise_suppression,
      /*agc_requested=*/properties.auto_gain_control, enabled_platform_effects);

#if BUILDFLAG(IS_CHROMEOS)
  enabled_platform_effects = UpdateVoiceIsolationEffects(
      /*use_chrome_aec=*/echo_canceller.IsChromeProvided(),
      properties.voice_isolation, enabled_platform_effects);
  if (base::FeatureList::IsEnabled(media::kIgnoreUiGains)) {
    // Ignore UI Gains if AGC is running in either browser or system
    if (properties.auto_gain_control) {
      return enabled_platform_effects | media::AudioParameters::IGNORE_UI_GAINS;
    }
  }
#endif

  return enabled_platform_effects;
}

media::AudioProcessingSettings ComputeWebrtcProcessingSettings(
    const EchoCanceller& echo_canceller,
    const AudioProcessingProperties& properties,
    int enabled_platform_effects,
    bool multichannel_processing) {
  media::AudioProcessingSettings out;

  out.echo_cancellation = echo_canceller.IsChromeProvided();

  out.noise_suppression =
      properties.noise_suppression &&
      (media::IsSystemEchoCancellationEnforcedAndAllowNsInTandem() ||
       !(enabled_platform_effects & media::AudioParameters::NOISE_SUPPRESSION));

  out.automatic_gain_control =
      properties.auto_gain_control &&
      (media::IsSystemEchoCancellationEnforcedAndAllowAgcInTandem() ||
       !(enabled_platform_effects &
         media::AudioParameters::AUTOMATIC_GAIN_CONTROL));

  out.multi_channel_capture_processing = multichannel_processing;

  out.use_loopback_aec_reference = echo_canceller.NeedSystemLoopback();
  return out;
}

}  // namespace

// static
bool MediaStreamAudioProcessingLayout::IsIndependentSystemNsAllowedForTests() {
  return IsIndependentSystemNsAllowed();
}

// static
media::AudioProcessingSettings
MediaStreamAudioProcessingLayout::ComputeWebrtcProcessingSettingsForTests(
    const AudioProcessingProperties& properties,
    int enabled_platform_effects,
    bool multichannel_processing) {
  return ComputeWebrtcProcessingSettings(
      EchoCanceller::From(properties, enabled_platform_effects), properties,
      enabled_platform_effects, multichannel_processing);
}

// static
MediaStreamAudioProcessingLayout MediaStreamAudioProcessingLayout::None() {
  return MediaStreamAudioProcessingLayout(
      AudioProcessingProperties::Disabled(),
      EchoCanceller(EchoCanceller::Type::kNone), /*platform_effects=*/0,
      kDummyWebrtcSettings);
}

// static
// TODO(crbug.com://40247860, crbug.com://415952276): retire this
// logic when restrictOwnAudio is launched.
std::optional<MediaStreamAudioProcessingLayout>
MediaStreamAudioProcessingLayout::MaybeMakeForProcessedDisplayCapture(
    const AudioProcessingProperties& properties,
    int channels) {
  if (!EchoCanceller::From(properties, /*available_platform_effects=*/0)
           .IsEnabled()) {
    return std::nullopt;
  }

  // Run APM locally to only remove PeerConnection playout.
  return MediaStreamAudioProcessingLayout(
      properties, EchoCanceller(EchoCanceller::Type::kPeerConnection),
      /*available_platform_effects=*/0, channels);
}

// static
// Keep only echo cancellation info and apply only it to platform effects.
// TODO(http://crbug.com/428837201)
// This is how the code works historically. Needs to be reconsidered.
MediaStreamAudioProcessingLayout
MediaStreamAudioProcessingLayout::MakeForUnprocessedLocalSource(
    const AudioProcessingProperties& properties,
    int platform_effects) {
  AudioProcessingProperties properties_aec_only(
      AudioProcessingProperties::Disabled());
  properties_aec_only.echo_cancellation_mode =
      properties.echo_cancellation_mode;

  EchoCanceller echo_canceller =
      EchoCanceller::From(properties_aec_only, platform_effects);

  CHECK(!echo_canceller.IsChromeProvided());

  if (echo_canceller.IsPlatformProvided()) {
    CHECK(platform_effects & media::AudioParameters::ECHO_CANCELLER);
  } else {
    platform_effects &= ~media::AudioParameters::ECHO_CANCELLER;
  }

  return MediaStreamAudioProcessingLayout(properties_aec_only, echo_canceller,
                                          platform_effects,
                                          kDummyWebrtcSettings);
}

// static
MediaStreamAudioProcessingLayout
MediaStreamAudioProcessingLayout::MakeForUnprocessedLocalSourceForTests(
    bool platform_aec,
    int available_platform_effects) {
  AudioProcessingProperties properties(AudioProcessingProperties::Disabled());
  if (platform_aec) {
    CHECK(available_platform_effects & media::AudioParameters::ECHO_CANCELLER);
    properties.echo_cancellation_mode = EchoCancellationMode::kAll;
  }

  return MakeForUnprocessedLocalSource(properties, available_platform_effects);
}

MediaStreamAudioProcessingLayout::MediaStreamAudioProcessingLayout(
    const AudioProcessingProperties& properties,
    int available_platform_effects,
    int channels)
    : MediaStreamAudioProcessingLayout(
          properties,
          EchoCanceller::From(properties, available_platform_effects),
          available_platform_effects,
          channels) {}

MediaStreamAudioProcessingLayout::MediaStreamAudioProcessingLayout(
    const AudioProcessingProperties& properties,
    const EchoCanceller& echo_canceller,
    int available_platform_effects,
    int channels)
    : properties_(properties),
      echo_canceller_(echo_canceller),
      platform_effects_(ApplyPropertiesToEffects(echo_canceller_,
                                                 properties_,
                                                 available_platform_effects)),
      webrtc_processing_settings_(
          ComputeWebrtcProcessingSettings(echo_canceller_,
                                          properties_,
                                          platform_effects_,
                                          channels > 1)) {}

MediaStreamAudioProcessingLayout::MediaStreamAudioProcessingLayout(
    const AudioProcessingProperties& properties,
    const EchoCanceller& echo_canceller,
    int platform_effects,
    const media::AudioProcessingSettings& webrtc_processing_settings)
    : properties_(properties),
      echo_canceller_(echo_canceller),
      platform_effects_(platform_effects),
      webrtc_processing_settings_(webrtc_processing_settings) {}

bool MediaStreamAudioProcessingLayout::NeedApmInAudioService() const {
  return echo_canceller_.GetApmLocation() ==
         EchoCanceller::ApmLocation::kAudioService;
}

bool MediaStreamAudioProcessingLayout::NeedWebrtcAudioProcessing() const {
  if (webrtc_processing_settings_.NeedWebrtcAudioProcessing()) {
    return true;
  }

// TODO(crbug.com/40205004): reconsider the logic below; see also
// AudioProcessingSettings::NeedWebrtcAudioProcessing().
#if !BUILDFLAG(IS_IOS)
  if (properties_.auto_gain_control) {
    return true;
  }
#endif

  if (properties_.noise_suppression) {
    return true;
  }

  return false;
}

bool MediaStreamAudioProcessingLayout::NoiseSuppressionInTandem() const {
  return (platform_effects_ & media::AudioParameters::NOISE_SUPPRESSION) &&
         webrtc_processing_settings_.noise_suppression;
}

bool MediaStreamAudioProcessingLayout::AutomaticGainControlInTandem() const {
  return (platform_effects_ & media::AudioParameters::AUTOMATIC_GAIN_CONTROL) &&
         webrtc_processing_settings_.automatic_gain_control;
}

}  // namespace blink