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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
ash / system / audio / audio_effects_controller.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/audio/audio_effects_controller.h"
#include <memory>
#include "ash/accessibility/accessibility_controller.h"
#include "ash/constants/ash_features.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/video_conference/effects/video_conference_tray_effects_manager.h"
#include "ash/system/video_conference/effects/video_conference_tray_effects_manager_types.h"
#include "ash/system/video_conference/video_conference_tray_controller.h"
#include "base/notreached.h"
#include "chromeos/ash/components/audio/cras_audio_handler.h"
#include "components/live_caption/caption_util.h"
#include "ui/base/l10n/l10n_util.h"
namespace ash {
bool IsStyleTransferSupportedByVc() {
// The toggle is added only if it's supported. After added, dlc progress will
// be queried. To avoid getting `nullopt` when querying dlcs, we export
// `false` here if it's `nullopt`.
if (CrasAudioHandler::Get()->GetAudioEffectDlcs() == std::nullopt) {
return false;
}
return CrasAudioHandler::Get()->style_transfer_supported();
}
// Vc can only support either noise cancellation or style transfer. So we skip
// noise cancellation if style transfer is supported already.
bool IsNoiseCancellationSupportedByVc() {
// The toggle is added only if it's supported. After added, dlc progress will
// be queried. To avoid getting `nullopt` when querying dlcs, we export
// `false` here if it's `nullopt`.
if (CrasAudioHandler::Get()->GetAudioEffectDlcs() == std::nullopt) {
return false;
}
return CrasAudioHandler::Get()->IsNoiseCancellationSupportedForDevice(
CrasAudioHandler::Get()->GetPrimaryActiveInputNode()) &&
!IsStyleTransferSupportedByVc();
}
AudioEffectsController::AudioEffectsController() {
auto* session_controller = Shell::Get()->session_controller();
DCHECK(session_controller);
session_observation_.Observe(session_controller);
CrasAudioHandler::Get()->AddAudioObserver(this);
}
AudioEffectsController::~AudioEffectsController() {
CrasAudioHandler::Get()->RemoveAudioObserver(this);
VideoConferenceTrayEffectsManager& effects_manager =
VideoConferenceTrayController::Get()->GetEffectsManager();
if (effects_manager.IsDelegateRegistered(this)) {
effects_manager.UnregisterDelegate(this);
}
}
bool AudioEffectsController::IsEffectSupported(VcEffectId effect_id) {
switch (effect_id) {
case VcEffectId::kNoiseCancellation:
return IsNoiseCancellationSupportedByVc();
case VcEffectId::kStyleTransfer:
return IsStyleTransferSupportedByVc();
case VcEffectId::kLiveCaption:
return base::FeatureList::IsEnabled(
features::kShowLiveCaptionInVideoConferenceTray) &&
captions::IsLiveCaptionFeatureSupported();
case VcEffectId::kBackgroundBlur:
case VcEffectId::kPortraitRelighting:
case VcEffectId::kCameraFraming:
case VcEffectId::kTestEffect:
case VcEffectId::kFaceRetouch:
case VcEffectId::kStudioLook:
NOTREACHED();
}
}
std::optional<int> AudioEffectsController::GetEffectState(
VcEffectId effect_id) {
switch (effect_id) {
case VcEffectId::kNoiseCancellation:
return CrasAudioHandler::Get()->GetNoiseCancellationState() ? 1 : 0;
case VcEffectId::kStyleTransfer:
return CrasAudioHandler::Get()->GetStyleTransferState() ? 1 : 0;
case VcEffectId::kLiveCaption:
return Shell::Get()->accessibility_controller()->live_caption().enabled()
? 1
: 0;
case VcEffectId::kBackgroundBlur:
case VcEffectId::kPortraitRelighting:
case VcEffectId::kCameraFraming:
case VcEffectId::kTestEffect:
case VcEffectId::kFaceRetouch:
case VcEffectId::kStudioLook:
NOTREACHED();
}
}
void AudioEffectsController::OnEffectControlActivated(
VcEffectId effect_id,
std::optional<int> value) {
switch (effect_id) {
case VcEffectId::kNoiseCancellation: {
// Toggle noise cancellation.
CrasAudioHandler::Get()->SetNoiseCancellationState(
!CrasAudioHandler::Get()->GetNoiseCancellationState(),
CrasAudioHandler::AudioSettingsChangeSource::kVideoConferenceTray);
return;
}
case VcEffectId::kStyleTransfer: {
// Toggle studio mic.
CrasAudioHandler::Get()->SetStyleTransferState(
!CrasAudioHandler::Get()->GetStyleTransferState());
return;
}
case VcEffectId::kLiveCaption: {
// Toggle live caption.
AccessibilityController* controller =
Shell::Get()->accessibility_controller();
controller->live_caption().SetEnabled(
!controller->live_caption().enabled());
return;
}
case VcEffectId::kBackgroundBlur:
case VcEffectId::kPortraitRelighting:
case VcEffectId::kCameraFraming:
case VcEffectId::kTestEffect:
case VcEffectId::kFaceRetouch:
case VcEffectId::kStudioLook:
NOTREACHED();
}
}
void AudioEffectsController::OnActiveUserPrefServiceChanged(
PrefService* pref_service) {
if (IsEffectSupported(VcEffectId::kNoiseCancellation)) {
AddNoiseCancellationEffect();
}
if (IsEffectSupported(VcEffectId::kStyleTransfer)) {
AddStyleTransferEffect();
}
if (IsEffectSupported(VcEffectId::kLiveCaption)) {
AddLiveCaptionEffect();
}
}
void AudioEffectsController::OnActiveInputNodeChanged() {
RefreshNoiseCancellationOrStyleTransferSupported();
}
void AudioEffectsController::OnAudioNodesChanged() {
RefreshNoiseCancellationOrStyleTransferSupported();
}
void AudioEffectsController::OnActiveOutputNodeChanged() {
RefreshNoiseCancellationOrStyleTransferSupported();
}
void AudioEffectsController::OnNoiseCancellationStateChanged() {
RefreshNoiseCancellationOrStyleTransferSupported();
}
void AudioEffectsController::OnStyleTransferStateChanged() {
RefreshNoiseCancellationOrStyleTransferSupported();
}
void AudioEffectsController::
RefreshNoiseCancellationOrStyleTransferSupported() {
const bool noise_cancellation_supported =
IsEffectSupported(VcEffectId::kNoiseCancellation);
const bool style_transfer_supported =
IsEffectSupported(VcEffectId::kStyleTransfer);
const bool noise_cancellation_added =
IsEffectsAdded(VcEffectId::kNoiseCancellation);
const bool style_transfer_added = IsEffectsAdded(VcEffectId::kStyleTransfer);
// If effects added match effects supported, then no action required.
if (noise_cancellation_supported == noise_cancellation_added &&
style_transfer_supported == style_transfer_added) {
return;
}
if (style_transfer_supported != style_transfer_added) {
if (style_transfer_supported) {
AddStyleTransferEffect();
} else {
RemoveEffect(VcEffectId::kStyleTransfer);
}
VideoConferenceTrayController::Get()
->GetEffectsManager()
.NotifyEffectSupportStateChanged(VcEffectId::kStyleTransfer,
style_transfer_supported);
}
if (noise_cancellation_supported != noise_cancellation_added) {
if (noise_cancellation_supported) {
AddNoiseCancellationEffect();
} else {
RemoveEffect(VcEffectId::kNoiseCancellation);
}
VideoConferenceTrayController::Get()
->GetEffectsManager()
.NotifyEffectSupportStateChanged(VcEffectId::kNoiseCancellation,
noise_cancellation_supported);
}
}
void AudioEffectsController::AddNoiseCancellationEffect() {
const auto noise_cancellation_id = VcEffectId::kNoiseCancellation;
// Do nothing if the effect was already added.
if (IsEffectsAdded(noise_cancellation_id)) {
return;
}
std::unique_ptr<VcHostedEffect> effect = std::make_unique<VcHostedEffect>(
/*type=*/VcEffectType::kToggle,
/*get_state_callback=*/
base::BindRepeating(&AudioEffectsController::GetEffectState,
base::Unretained(this), noise_cancellation_id),
/*effect_id=*/noise_cancellation_id);
auto effect_state = std::make_unique<VcEffectState>(
/*icon=*/&kVideoConferenceNoiseCancellationOnIcon,
/*label_text=*/
l10n_util::GetStringUTF16(
IDS_ASH_STATUS_TRAY_AUDIO_INPUT_NOISE_CANCELLATION),
/*accessible_name_id=*/
IDS_ASH_STATUS_TRAY_AUDIO_INPUT_NOISE_CANCELLATION,
/*button_callback=*/
base::BindRepeating(&AudioEffectsController::OnEffectControlActivated,
weak_factory_.GetWeakPtr(),
/*effect_id=*/noise_cancellation_id,
/*value=*/0));
effect->AddState(std::move(effect_state));
effect->set_dependency_flags(VcHostedEffect::ResourceDependency::kMicrophone);
AddEffect(std::move(effect));
// Register this delegate if needed so that the effect is added to the UI.
// Note that other functions might register this delegate already and we need
// to avoid registering twice.
VideoConferenceTrayEffectsManager& effects_manager =
VideoConferenceTrayController::Get()->GetEffectsManager();
if (!effects_manager.IsDelegateRegistered(this)) {
effects_manager.RegisterDelegate(this);
}
}
void AudioEffectsController::AddStyleTransferEffect() {
const auto style_transfer_id = VcEffectId::kStyleTransfer;
// Do nothing if the effect was already added.
if (IsEffectsAdded(style_transfer_id)) {
return;
}
std::unique_ptr<VcHostedEffect> effect = std::make_unique<VcHostedEffect>(
/*type=*/VcEffectType::kToggle,
/*get_state_callback=*/
base::BindRepeating(&AudioEffectsController::GetEffectState,
base::Unretained(this), style_transfer_id),
/*effect_id=*/style_transfer_id);
auto effect_state = std::make_unique<VcEffectState>(
/*icon=*/&kUnifiedMenuMicStyleTransferIcon,
/*label_text=*/
l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO_INPUT_STYLE_TRANSFER),
/*accessible_name_id=*/
IDS_ASH_STATUS_TRAY_AUDIO_INPUT_STYLE_TRANSFER,
/*button_callback=*/
base::BindRepeating(&AudioEffectsController::OnEffectControlActivated,
weak_factory_.GetWeakPtr(),
/*effect_id=*/style_transfer_id,
/*value=*/0));
effect->AddState(std::move(effect_state));
effect->set_dependency_flags(VcHostedEffect::ResourceDependency::kMicrophone);
AddEffect(std::move(effect));
// Register this delegate if needed so that the effect is added to the UI.
// Note that other functions might register this delegate already and we need
// to avoid registering twice.
VideoConferenceTrayEffectsManager& effects_manager =
VideoConferenceTrayController::Get()->GetEffectsManager();
if (!effects_manager.IsDelegateRegistered(this)) {
effects_manager.RegisterDelegate(this);
}
}
void AudioEffectsController::AddLiveCaptionEffect() {
const auto live_caption_id = VcEffectId::kLiveCaption;
// Do nothing if the effect was already added.
if (IsEffectsAdded(live_caption_id)) {
return;
}
std::unique_ptr<VcHostedEffect> effect = std::make_unique<VcHostedEffect>(
/*type=*/VcEffectType::kToggle,
/*get_state_callback=*/
base::BindRepeating(&AudioEffectsController::GetEffectState,
base::Unretained(this), live_caption_id),
/*effect_id=*/live_caption_id);
auto effect_state = std::make_unique<VcEffectState>(
/*icon=*/&kVideoConferenceLiveCaptionOnIcon,
/*label_text=*/
l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_LIVE_CAPTION),
/*accessible_name_id=*/
IDS_ASH_STATUS_TRAY_LIVE_CAPTION,
/*button_callback=*/
base::BindRepeating(&AudioEffectsController::OnEffectControlActivated,
weak_factory_.GetWeakPtr(),
/*effect_id=*/live_caption_id,
/*value=*/0));
effect->AddState(std::move(effect_state));
AddEffect(std::move(effect));
// Register this delegate if needed so that the effect is added to the UI.
// Note that other functions might register this delegate already and we need
// to avoid registering twice.
VideoConferenceTrayEffectsManager& effects_manager =
VideoConferenceTrayController::Get()->GetEffectsManager();
if (!effects_manager.IsDelegateRegistered(this)) {
effects_manager.RegisterDelegate(this);
}
}
bool AudioEffectsController::IsEffectsAdded(VcEffectId id) {
return GetEffectById(id);
}
} // namespace ash