BlueGit

ict rusty-teams

Files

rusty-teams / vendor / webrtc-sys / src / adm_proxy.cpp

adm_proxy.cpp

adm_proxy.cpp 26.8 KiB Download Raw
1/*
2 * Copyright 2026 LiveKit, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "livekit/adm_proxy.h"
18
19#include "api/audio/audio_device.h"
20#include "api/audio/create_audio_device_module.h"
21#include "api/make_ref_counted.h"
22#include "rtc_base/checks.h"
23#include "rtc_base/logging.h"
24
25#if defined(__ANDROID__)
26#include <jni.h>
27#include "sdk/android/native_api/audio_device_module/audio_device_android.h"
28#include "sdk/android/native_api/base/init.h"
29#endif
30
31namespace livekit_ffi {
32
33AdmProxy::AdmProxy(const webrtc::Environment& env, webrtc::Thread* worker_thread)
34 : env_(env),
35 worker_thread_(worker_thread) {
36 RTC_DCHECK(worker_thread_);
37 // The proxy must be constructed on the worker thread so the synthetic ADM
38 // created below binds to it.
39 RTC_DCHECK_RUN_ON(worker_thread_);
40
41 // Create the synthetic ADM for synthetic mode. SyntheticAudioDevice pumps
42 // the WebRTC audio pipeline without platform audio, allowing FFI callbacks
43 // to receive decoded remote audio.
44 synthetic_adm_ = webrtc::make_ref_counted<SyntheticAudioDevice>(env_);
45 if (synthetic_adm_->Init() != 0) {
46 RTC_LOG(LS_ERROR) << "AdmProxy: Failed to initialize synthetic ADM";
47 }
48
49 // The Platform ADM is created lazily in EnsurePlatformAdmCreated() on the
50 // first AcquirePlatformAdm() call. Applications that never use PlatformAudio
51 // never construct the platform ADM at all, so factory creation has no audio
52 // side effects beyond the synthetic ADM. On Android laziness is also a hard
53 // requirement: CreateAndroidAudioDeviceModule needs JNI (the application
54 // context) which may not be initialized when this constructor runs.
55 // State that WebRTC or the application configures before the first acquire
56 // (audio transport, device selection, channel config) is cached below and
57 // replayed onto the fresh ADM.
58}
59
60AdmProxy::~AdmProxy() {
61 RTC_LOG(LS_VERBOSE) << "AdmProxy::~AdmProxy()";
62
63 // The last reference may be dropped on any thread. Tear down on the worker
64 // so the sub ADMs are terminated and destroyed on their owning thread.
65 RunOnWorker([this] {
66 RTC_DCHECK_RUN_ON(worker_thread_);
67 if (synthetic_adm_) {
68 synthetic_adm_->Terminate();
69 synthetic_adm_ = nullptr;
70 }
71 if (platform_adm_) {
72 platform_adm_->Terminate();
73 platform_adm_ = nullptr;
74 }
75 });
76}
77
78// =============================================================================
79// Helper Methods (worker thread only)
80// =============================================================================
81
82bool AdmProxy::IsPlatformPlayoutActive() const {
83 // Platform playout is active when: ref_count > 0 AND playout explicitly enabled.
84 // Otherwise, synthetic mode handles playout via the internal pumping task.
85 return platform_adm_ && platform_adm_ref_count_ > 0 && playout_enabled_;
86}
87
88webrtc::AudioDeviceModule* AdmProxy::RecordingAdm() const {
89 // Recording only available through platform ADM when enabled.
90 // Synthetic mode doesn't support recording (no microphone).
91 if (platform_adm_ && platform_adm_ref_count_ > 0 && recording_enabled_) {
92 return platform_adm_.get();
93 }
94 return nullptr;
95}
96
97// =============================================================================
98// Platform ADM Lifecycle Management
99// =============================================================================
100
101bool AdmProxy::EnsurePlatformAdmCreated() {
102 if (platform_adm_) {
103 return platform_adm_->Initialized() || InitPlatformAdm();
104 }
105
106 // This runs on the worker thread (see the RTC_RUN_ON annotation), so the
107 // fresh ADM binds its sequence checker to the worker thread, exactly as an
108 // eagerly constructed one would.
109#if defined(__ANDROID__)
110 // Use CreateAndroidAudioDeviceModule which properly uses GetAppContext()
111 // to get the application context set via ContextUtils.initialize().
112 platform_adm_ = webrtc::CreateAndroidAudioDeviceModule(
113 env_, webrtc::AudioDeviceModule::kPlatformDefaultAudio);
114
115 if (!platform_adm_) {
116 RTC_LOG(LS_ERROR) << "AdmProxy: CreateAndroidAudioDeviceModule returned nullptr. "
117 << "Ensure ContextUtils.initialize() was called.";
118 return false;
119 }
120#else
121 platform_adm_ = webrtc::CreateAudioDeviceModule(
122 env_, webrtc::AudioDeviceModule::kPlatformDefaultAudio);
123
124 if (!platform_adm_) {
125 RTC_LOG(LS_ERROR) << "AdmProxy: CreateAudioDeviceModule returned nullptr";
126 return false;
127 }
128#endif
129
130 if (!InitPlatformAdm()) {
131 platform_adm_ = nullptr;
132 return false;
133 }
134 return true;
135}
136
137// WebRtcVoiceEngine::Terminate() terminates the ADM at the end of every call,
138// so the next call re-inits the same instance and needs its state again.
139bool AdmProxy::InitPlatformAdm() {
140 int32_t init_result = platform_adm_->Init();
141 if (init_result != 0) {
142 RTC_LOG(LS_ERROR) << "AdmProxy: Platform ADM Init() failed with error=" << init_result;
143 return false;
144 }
145
146 // WebRTC registered its audio transport before the ADM existed,
147 // pass it along so recorded audio actually reaches the pipeline
148 if (audio_transport_) {
149 platform_adm_->RegisterAudioCallback(audio_transport_);
150 }
151
152 // Re-apply any device selection made before the ADM existed
153 if (selected_playout_device_.has_value()) {
154 if (auto* index = std::get_if<uint16_t>(&*selected_playout_device_)) {
155 platform_adm_->SetPlayoutDevice(*index);
156 } else {
157 platform_adm_->SetPlayoutDevice(
158 std::get<WindowsDeviceType>(*selected_playout_device_));
159 }
160 }
161 if (selected_recording_device_.has_value()) {
162 if (auto* index = std::get_if<uint16_t>(&*selected_recording_device_)) {
163 platform_adm_->SetRecordingDevice(*index);
164 } else {
165 platform_adm_->SetRecordingDevice(
166 std::get<WindowsDeviceType>(*selected_recording_device_));
167 }
168 }
169
170 // Re-register an observer set before the ADM existed
171 if (audio_observer_) {
172 platform_adm_->SetObserver(audio_observer_);
173 }
174
175 // Re-apply the channel configuration the voice engine requested at factory
176 // init, before the ADM existed
177 if (selected_stereo_playout_.has_value()) {
178 platform_adm_->SetStereoPlayout(*selected_stereo_playout_);
179 }
180 if (selected_stereo_recording_.has_value()) {
181 platform_adm_->SetStereoRecording(*selected_stereo_recording_);
182 }
183
184 return true;
185}
186
187bool AdmProxy::AcquirePlatformAdm() {
188 return RunOnWorker([this] {
189 RTC_DCHECK_RUN_ON(worker_thread_);
190
191 // Lazily create the Platform ADM on first acquire. Apps that never use
192 // PlatformAudio never construct it.
193 if (!EnsurePlatformAdmCreated()) {
194 RTC_LOG(LS_ERROR) << "AdmProxy::AcquirePlatformAdm() - Failed to create Platform ADM";
195 return false;
196 }
197
198 int old_ref_count = platform_adm_ref_count_;
199 platform_adm_ref_count_++;
200
201 // If this is the first acquisition and playout/recording is enabled,
202 // we may need to switch from synthetic mode to platform ADM
203 if (old_ref_count == 0) {
204 SwitchPlayoutMode();
205 SwitchRecordingAdm();
206 }
207
208 return true;
209 });
210}
211
212void AdmProxy::ReleasePlatformAdm() {
213 RunOnWorker([this] {
214 RTC_DCHECK_RUN_ON(worker_thread_);
215
216 if (platform_adm_ref_count_ <= 0) {
217 RTC_LOG(LS_WARNING) << "AdmProxy::ReleasePlatformAdm() called with ref_count="
218 << platform_adm_ref_count_;
219 return;
220 }
221
222 platform_adm_ref_count_--;
223
224 // If ref_count reaches 0, switch back from platform ADM to synthetic mode
225 // Note: We do NOT terminate the Platform ADM - it stays alive until destructor.
226 // This avoids iOS KVO race conditions from re-creating the ADM.
227 if (platform_adm_ref_count_ == 0) {
228 SwitchPlayoutMode();
229 SwitchRecordingAdm();
230 }
231 });
232}
233
234int AdmProxy::platform_adm_ref_count() const {
235 return RunOnWorker([this] {
236 RTC_DCHECK_RUN_ON(worker_thread_);
237 return platform_adm_ref_count_;
238 });
239}
240
241bool AdmProxy::is_platform_adm_active() const {
242 return RunOnWorker([this] {
243 RTC_DCHECK_RUN_ON(worker_thread_);
244 return platform_adm_ != nullptr && platform_adm_ref_count_ > 0;
245 });
246}
247
248// =============================================================================
249// Recording/Playout Control
250// =============================================================================
251
252void AdmProxy::set_recording_enabled(bool enabled) {
253 RunOnWorker([this, enabled] {
254 RTC_DCHECK_RUN_ON(worker_thread_);
255 if (recording_enabled_ == enabled) {
256 return;
257 }
258 recording_enabled_ = enabled;
259 SwitchRecordingAdm();
260 });
261}
262
263bool AdmProxy::recording_enabled() const {
264 return RunOnWorker([this] {
265 RTC_DCHECK_RUN_ON(worker_thread_);
266 return recording_enabled_;
267 });
268}
269
270void AdmProxy::set_playout_enabled(bool enabled) {
271 RunOnWorker([this, enabled] {
272 RTC_DCHECK_RUN_ON(worker_thread_);
273 if (playout_enabled_ == enabled) {
274 return;
275 }
276 playout_enabled_ = enabled;
277 SwitchPlayoutMode();
278 });
279}
280
281bool AdmProxy::playout_enabled() const {
282 return RunOnWorker([this] {
283 RTC_DCHECK_RUN_ON(worker_thread_);
284 return playout_enabled_;
285 });
286}
287
288// =============================================================================
289// Mode Switching Helpers (worker thread only)
290// =============================================================================
291
292void AdmProxy::SwitchPlayoutMode() {
293 if (!playing_) return;
294
295 if (IsPlatformPlayoutActive()) {
296 // Switch to platform mode - stop synthetic, start platform ADM
297 if (synthetic_adm_) {
298 synthetic_adm_->StopPlayout();
299 }
300 if (platform_adm_) {
301 platform_adm_->InitPlayout();
302 platform_adm_->StartPlayout();
303 }
304 } else {
305 // Switch to synthetic mode - stop platform ADM, start synthetic ADM
306 if (platform_adm_) {
307 platform_adm_->StopPlayout();
308 }
309 if (synthetic_adm_) {
310 synthetic_adm_->StartPlayout();
311 }
312 }
313}
314
315void AdmProxy::SwitchRecordingAdm() {
316 if (!recording_) return;
317
318 // Stop platform ADM recording (only one that supports recording)
319 if (platform_adm_) platform_adm_->StopRecording();
320
321 // Start if new ADM supports recording
322 auto* adm = RecordingAdm();
323 if (adm) {
324 adm->InitRecording();
325 adm->StartRecording();
326 } else {
327 recording_ = false;
328 }
329}
330
331// =============================================================================
332// AudioDeviceModule Interface Implementation
333// =============================================================================
334
335int32_t AdmProxy::ActiveAudioLayer(AudioLayer* audioLayer) const {
336 return RunOnWorker([this, audioLayer] {
337 RTC_DCHECK_RUN_ON(worker_thread_);
338 if (platform_adm_) {
339 return platform_adm_->ActiveAudioLayer(audioLayer);
340 }
341 *audioLayer = AudioLayer::kDummyAudio;
342 return 0;
343 });
344}
345
346int32_t AdmProxy::RegisterAudioCallback(webrtc::AudioTransport* transport) {
347 return RunOnWorker([this, transport] {
348 RTC_DCHECK_RUN_ON(worker_thread_);
349 audio_transport_ = transport;
350
351 // Register with both ADMs so they're ready when we switch modes
352 if (synthetic_adm_) {
353 synthetic_adm_->RegisterAudioCallback(transport);
354 }
355 if (platform_adm_) {
356 platform_adm_->RegisterAudioCallback(transport);
357 }
358 return 0;
359 });
360}
361
362int32_t AdmProxy::Init() {
363 return RunOnWorker([this] {
364 RTC_DCHECK_RUN_ON(worker_thread_);
365 if (synthetic_adm_ && !synthetic_adm_->Initialized() &&
366 synthetic_adm_->Init() != 0) {
367 return -1;
368 }
369 if (platform_adm_ && !platform_adm_->Initialized() && !InitPlatformAdm()) {
370 return -1;
371 }
372 return 0;
373 });
374}
375
376int32_t AdmProxy::Terminate() {
377 return RunOnWorker([this] {
378 RTC_DCHECK_RUN_ON(worker_thread_);
379 int32_t result = 0;
380 if (synthetic_adm_) {
381 synthetic_adm_->StopPlayout();
382 result = synthetic_adm_->Terminate();
383 }
384 if (platform_adm_) {
385 int32_t platform_result = platform_adm_->Terminate();
386 if (result == 0) result = platform_result;
387 }
388 playing_ = false;
389 recording_ = false;
390 return result;
391 });
392}
393
394bool AdmProxy::Initialized() const {
395 return RunOnWorker([this] {
396 RTC_DCHECK_RUN_ON(worker_thread_);
397 // We're initialized if at least one ADM is initialized
398 bool synthetic_init = synthetic_adm_ && synthetic_adm_->Initialized();
399 bool platform_init = platform_adm_ && platform_adm_->Initialized();
400 return synthetic_init || platform_init;
401 });
402}
403
404int16_t AdmProxy::PlayoutDevices() {
405 // In synthetic mode, there are no platform devices
406 return WithPlatformAdm<int16_t>(0, [](webrtc::AudioDeviceModule& adm) {
407 return adm.PlayoutDevices();
408 });
409}
410
411int16_t AdmProxy::RecordingDevices() {
412 // In synthetic mode, there are no platform devices
413 return WithPlatformAdm<int16_t>(0, [](webrtc::AudioDeviceModule& adm) {
414 return adm.RecordingDevices();
415 });
416}
417
418int32_t AdmProxy::PlayoutDeviceName(uint16_t index,
419 char name[webrtc::kAdmMaxDeviceNameSize],
420 char guid[webrtc::kAdmMaxGuidSize]) {
421 return WithPlatformAdm<int32_t>(-1, [&](webrtc::AudioDeviceModule& adm) {
422 return adm.PlayoutDeviceName(index, name, guid);
423 });
424}
425
426int32_t AdmProxy::RecordingDeviceName(uint16_t index,
427 char name[webrtc::kAdmMaxDeviceNameSize],
428 char guid[webrtc::kAdmMaxGuidSize]) {
429 return WithPlatformAdm<int32_t>(-1, [&](webrtc::AudioDeviceModule& adm) {
430 return adm.RecordingDeviceName(index, name, guid);
431 });
432}
433
434int32_t AdmProxy::SetPlayoutDevice(uint16_t index) {
435 return RunOnWorker([this, index] {
436 RTC_DCHECK_RUN_ON(worker_thread_);
437 selected_playout_device_ = DeviceSelection(index);
438 if (platform_adm_) {
439 return platform_adm_->SetPlayoutDevice(index);
440 }
441 return 0;
442 });
443}
444
445int32_t AdmProxy::SetPlayoutDevice(WindowsDeviceType device) {
446 return RunOnWorker([this, device] {
447 RTC_DCHECK_RUN_ON(worker_thread_);
448 selected_playout_device_ = DeviceSelection(device);
449 if (platform_adm_) {
450 return platform_adm_->SetPlayoutDevice(device);
451 }
452 return 0;
453 });
454}
455
456int32_t AdmProxy::SetRecordingDevice(uint16_t index) {
457 return RunOnWorker([this, index] {
458 RTC_DCHECK_RUN_ON(worker_thread_);
459 selected_recording_device_ = DeviceSelection(index);
460 if (platform_adm_) {
461 return platform_adm_->SetRecordingDevice(index);
462 }
463 return 0;
464 });
465}
466
467int32_t AdmProxy::SetRecordingDevice(WindowsDeviceType device) {
468 return RunOnWorker([this, device] {
469 RTC_DCHECK_RUN_ON(worker_thread_);
470 selected_recording_device_ = DeviceSelection(device);
471 if (platform_adm_) {
472 return platform_adm_->SetRecordingDevice(device);
473 }
474 return 0;
475 });
476}
477
478int32_t AdmProxy::PlayoutIsAvailable(bool* available) {
479 return RunOnWorker([this, available] {
480 RTC_DCHECK_RUN_ON(worker_thread_);
481 if (platform_adm_) {
482 return platform_adm_->PlayoutIsAvailable(available);
483 }
484 *available = true; // Synthetic playout is always available
485 return 0;
486 });
487}
488
489int32_t AdmProxy::InitPlayout() {
490 return RunOnWorker([this] {
491 RTC_DCHECK_RUN_ON(worker_thread_);
492 if (IsPlatformPlayoutActive()) {
493 return platform_adm_->InitPlayout();
494 }
495 // Synthetic mode
496 if (synthetic_adm_) {
497 return synthetic_adm_->InitPlayout();
498 }
499 return -1;
500 });
501}
502
503bool AdmProxy::PlayoutIsInitialized() const {
504 return RunOnWorker([this] {
505 RTC_DCHECK_RUN_ON(worker_thread_);
506 if (IsPlatformPlayoutActive()) {
507 return platform_adm_->PlayoutIsInitialized();
508 }
509 // Synthetic mode
510 return synthetic_adm_ && synthetic_adm_->PlayoutIsInitialized();
511 });
512}
513
514int32_t AdmProxy::RecordingIsAvailable(bool* available) {
515 return RunOnWorker([this, available] {
516 RTC_DCHECK_RUN_ON(worker_thread_);
517 if (platform_adm_) {
518 return platform_adm_->RecordingIsAvailable(available);
519 }
520 *available = false; // Recording not available in synthetic mode
521 return 0;
522 });
523}
524
525int32_t AdmProxy::InitRecording() {
526 return RunOnWorker([this] {
527 RTC_DCHECK_RUN_ON(worker_thread_);
528 auto* adm = RecordingAdm();
529 if (!adm) {
530 // Recording not available (no platform ADM or recording disabled)
531 // Return success to avoid breaking WebRTC's initialization flow
532 return 0;
533 }
534 return adm->InitRecording();
535 });
536}
537
538bool AdmProxy::RecordingIsInitialized() const {
539 return RunOnWorker([this] {
540 RTC_DCHECK_RUN_ON(worker_thread_);
541 auto* adm = RecordingAdm();
542 if (adm) {
543 return adm->RecordingIsInitialized();
544 }
545 return false; // Recording not available
546 });
547}
548
549int32_t AdmProxy::StartPlayout() {
550 return RunOnWorker([this] {
551 RTC_DCHECK_RUN_ON(worker_thread_);
552 playing_ = true;
553
554 if (IsPlatformPlayoutActive()) {
555 return platform_adm_->StartPlayout();
556 }
557 // Synthetic mode
558 if (synthetic_adm_) {
559 return synthetic_adm_->StartPlayout();
560 }
561 return -1;
562 });
563}
564
565int32_t AdmProxy::StopPlayout() {
566 return RunOnWorker([this] {
567 RTC_DCHECK_RUN_ON(worker_thread_);
568 playing_ = false;
569
570 // Stop both ADMs
571 if (synthetic_adm_) {
572 synthetic_adm_->StopPlayout();
573 }
574 if (platform_adm_) {
575 platform_adm_->StopPlayout();
576 }
577 return 0;
578 });
579}
580
581bool AdmProxy::Playing() const {
582 return RunOnWorker([this] {
583 RTC_DCHECK_RUN_ON(worker_thread_);
584 if (IsPlatformPlayoutActive()) {
585 return platform_adm_->Playing();
586 }
587 return synthetic_adm_ && synthetic_adm_->Playing();
588 });
589}
590
591int32_t AdmProxy::StartRecording() {
592 return RunOnWorker([this] {
593 RTC_DCHECK_RUN_ON(worker_thread_);
594 auto* adm = RecordingAdm();
595 if (!adm) {
596 // Recording not available - return success to avoid breaking WebRTC
597 return 0;
598 }
599 recording_ = true;
600 return adm->StartRecording();
601 });
602}
603
604int32_t AdmProxy::StopRecording() {
605 return RunOnWorker([this] {
606 RTC_DCHECK_RUN_ON(worker_thread_);
607 recording_ = false;
608
609 auto* adm = RecordingAdm();
610 if (adm) {
611 return adm->StopRecording();
612 }
613 return 0;
614 });
615}
616
617bool AdmProxy::Recording() const {
618 return RunOnWorker([this] {
619 RTC_DCHECK_RUN_ON(worker_thread_);
620 auto* adm = RecordingAdm();
621 if (adm) {
622 return adm->Recording();
623 }
624 return false;
625 });
626}
627
628int32_t AdmProxy::InitSpeaker() {
629 return WithPlatformAdm<int32_t>(0, [](webrtc::AudioDeviceModule& adm) {
630 return adm.InitSpeaker();
631 });
632}
633
634bool AdmProxy::SpeakerIsInitialized() const {
635 return WithPlatformAdm<bool>(true, [](webrtc::AudioDeviceModule& adm) {
636 return adm.SpeakerIsInitialized();
637 });
638}
639
640int32_t AdmProxy::InitMicrophone() {
641 return WithPlatformAdm<int32_t>(0, [](webrtc::AudioDeviceModule& adm) {
642 return adm.InitMicrophone();
643 });
644}
645
646bool AdmProxy::MicrophoneIsInitialized() const {
647 return WithPlatformAdm<bool>(false, [](webrtc::AudioDeviceModule& adm) {
648 return adm.MicrophoneIsInitialized();
649 });
650}
651
652int32_t AdmProxy::SpeakerVolumeIsAvailable(bool* available) {
653 return RunOnWorker([this, available] {
654 RTC_DCHECK_RUN_ON(worker_thread_);
655 if (platform_adm_) {
656 return platform_adm_->SpeakerVolumeIsAvailable(available);
657 }
658 *available = false;
659 return 0;
660 });
661}
662
663int32_t AdmProxy::SetSpeakerVolume(uint32_t volume) {
664 return WithPlatformAdm<int32_t>(-1, [volume](webrtc::AudioDeviceModule& adm) {
665 return adm.SetSpeakerVolume(volume);
666 });
667}
668
669int32_t AdmProxy::SpeakerVolume(uint32_t* volume) const {
670 return WithPlatformAdm<int32_t>(-1, [volume](webrtc::AudioDeviceModule& adm) {
671 return adm.SpeakerVolume(volume);
672 });
673}
674
675int32_t AdmProxy::MaxSpeakerVolume(uint32_t* maxVolume) const {
676 return WithPlatformAdm<int32_t>(-1, [maxVolume](webrtc::AudioDeviceModule& adm) {
677 return adm.MaxSpeakerVolume(maxVolume);
678 });
679}
680
681int32_t AdmProxy::MinSpeakerVolume(uint32_t* minVolume) const {
682 return WithPlatformAdm<int32_t>(-1, [minVolume](webrtc::AudioDeviceModule& adm) {
683 return adm.MinSpeakerVolume(minVolume);
684 });
685}
686
687int32_t AdmProxy::MicrophoneVolumeIsAvailable(bool* available) {
688 return RunOnWorker([this, available] {
689 RTC_DCHECK_RUN_ON(worker_thread_);
690 if (platform_adm_) {
691 return platform_adm_->MicrophoneVolumeIsAvailable(available);
692 }
693 *available = false;
694 return 0;
695 });
696}
697
698int32_t AdmProxy::SetMicrophoneVolume(uint32_t volume) {
699 return WithPlatformAdm<int32_t>(-1, [volume](webrtc::AudioDeviceModule& adm) {
700 return adm.SetMicrophoneVolume(volume);
701 });
702}
703
704int32_t AdmProxy::MicrophoneVolume(uint32_t* volume) const {
705 return WithPlatformAdm<int32_t>(-1, [volume](webrtc::AudioDeviceModule& adm) {
706 return adm.MicrophoneVolume(volume);
707 });
708}
709
710int32_t AdmProxy::MaxMicrophoneVolume(uint32_t* maxVolume) const {
711 return WithPlatformAdm<int32_t>(-1, [maxVolume](webrtc::AudioDeviceModule& adm) {
712 return adm.MaxMicrophoneVolume(maxVolume);
713 });
714}
715
716int32_t AdmProxy::MinMicrophoneVolume(uint32_t* minVolume) const {
717 return WithPlatformAdm<int32_t>(-1, [minVolume](webrtc::AudioDeviceModule& adm) {
718 return adm.MinMicrophoneVolume(minVolume);
719 });
720}
721
722int32_t AdmProxy::SpeakerMuteIsAvailable(bool* available) {
723 return RunOnWorker([this, available] {
724 RTC_DCHECK_RUN_ON(worker_thread_);
725 if (platform_adm_) {
726 return platform_adm_->SpeakerMuteIsAvailable(available);
727 }
728 *available = false;
729 return 0;
730 });
731}
732
733int32_t AdmProxy::SetSpeakerMute(bool enable) {
734 return WithPlatformAdm<int32_t>(-1, [enable](webrtc::AudioDeviceModule& adm) {
735 return adm.SetSpeakerMute(enable);
736 });
737}
738
739int32_t AdmProxy::SpeakerMute(bool* enabled) const {
740 return WithPlatformAdm<int32_t>(-1, [enabled](webrtc::AudioDeviceModule& adm) {
741 return adm.SpeakerMute(enabled);
742 });
743}
744
745int32_t AdmProxy::MicrophoneMuteIsAvailable(bool* available) {
746 return RunOnWorker([this, available] {
747 RTC_DCHECK_RUN_ON(worker_thread_);
748 if (platform_adm_) {
749 return platform_adm_->MicrophoneMuteIsAvailable(available);
750 }
751 *available = false;
752 return 0;
753 });
754}
755
756int32_t AdmProxy::SetMicrophoneMute(bool enable) {
757 return WithPlatformAdm<int32_t>(-1, [enable](webrtc::AudioDeviceModule& adm) {
758 return adm.SetMicrophoneMute(enable);
759 });
760}
761
762int32_t AdmProxy::MicrophoneMute(bool* enabled) const {
763 return WithPlatformAdm<int32_t>(-1, [enabled](webrtc::AudioDeviceModule& adm) {
764 return adm.MicrophoneMute(enabled);
765 });
766}
767
768int32_t AdmProxy::StereoPlayoutIsAvailable(bool* available) const {
769 return RunOnWorker([this, available] {
770 RTC_DCHECK_RUN_ON(worker_thread_);
771 if (platform_adm_) {
772 return platform_adm_->StereoPlayoutIsAvailable(available);
773 }
774 *available = true;
775 return 0;
776 });
777}
778
779int32_t AdmProxy::SetStereoPlayout(bool enable) {
780 return RunOnWorker([this, enable] {
781 RTC_DCHECK_RUN_ON(worker_thread_);
782 // Stored so a selection made before the Platform ADM exists can be
783 // re-applied once it is created
784 selected_stereo_playout_ = enable;
785 if (platform_adm_) {
786 return platform_adm_->SetStereoPlayout(enable);
787 }
788 return 0;
789 });
790}
791
792int32_t AdmProxy::StereoPlayout(bool* enabled) const {
793 return RunOnWorker([this, enabled] {
794 RTC_DCHECK_RUN_ON(worker_thread_);
795 if (platform_adm_) {
796 return platform_adm_->StereoPlayout(enabled);
797 }
798 // Report a pending pre-acquire configuration, or the default
799 *enabled = selected_stereo_playout_.value_or(true);
800 return 0;
801 });
802}
803
804int32_t AdmProxy::StereoRecordingIsAvailable(bool* available) const {
805 return RunOnWorker([this, available] {
806 RTC_DCHECK_RUN_ON(worker_thread_);
807 if (platform_adm_) {
808 return platform_adm_->StereoRecordingIsAvailable(available);
809 }
810 *available = false;
811 return 0;
812 });
813}
814
815int32_t AdmProxy::SetStereoRecording(bool enable) {
816 return RunOnWorker([this, enable] {
817 RTC_DCHECK_RUN_ON(worker_thread_);
818 // Stored so a selection made before the Platform ADM exists can be
819 // re-applied once it is created
820 selected_stereo_recording_ = enable;
821 if (platform_adm_) {
822 return platform_adm_->SetStereoRecording(enable);
823 }
824 return 0;
825 });
826}
827
828int32_t AdmProxy::StereoRecording(bool* enabled) const {
829 return RunOnWorker([this, enabled] {
830 RTC_DCHECK_RUN_ON(worker_thread_);
831 if (platform_adm_) {
832 return platform_adm_->StereoRecording(enabled);
833 }
834 // Report a pending pre-acquire configuration, or the default
835 *enabled = selected_stereo_recording_.value_or(false);
836 return 0;
837 });
838}
839
840int32_t AdmProxy::PlayoutDelay(uint16_t* delayMS) const {
841 return RunOnWorker([this, delayMS] {
842 RTC_DCHECK_RUN_ON(worker_thread_);
843 if (IsPlatformPlayoutActive()) {
844 return platform_adm_->PlayoutDelay(delayMS);
845 }
846 if (synthetic_adm_) {
847 return synthetic_adm_->PlayoutDelay(delayMS);
848 }
849 *delayMS = 0;
850 return 0;
851 });
852}
853
854bool AdmProxy::BuiltInAECIsAvailable() const {
855#if defined(WEBRTC_WIN)
856 // The Windows voice capture DMO fails StartRecording unless playout runs first,
857 // which WebRTC does not guarantee; AEC3 in software is what Chrome uses too.
858 return false;
859#else
860 return WithPlatformAdm<bool>(false, [](webrtc::AudioDeviceModule& adm) {
861 return adm.BuiltInAECIsAvailable();
862 });
863#endif
864}
865
866bool AdmProxy::BuiltInAGCIsAvailable() const {
867 return WithPlatformAdm<bool>(false, [](webrtc::AudioDeviceModule& adm) {
868 return adm.BuiltInAGCIsAvailable();
869 });
870}
871
872bool AdmProxy::BuiltInNSIsAvailable() const {
873 return WithPlatformAdm<bool>(false, [](webrtc::AudioDeviceModule& adm) {
874 return adm.BuiltInNSIsAvailable();
875 });
876}
877
878int32_t AdmProxy::EnableBuiltInAEC(bool enable) {
879 return WithPlatformAdm<int32_t>(-1, [enable](webrtc::AudioDeviceModule& adm) {
880 return adm.EnableBuiltInAEC(enable);
881 });
882}
883
884int32_t AdmProxy::EnableBuiltInAGC(bool enable) {
885 return WithPlatformAdm<int32_t>(-1, [enable](webrtc::AudioDeviceModule& adm) {
886 return adm.EnableBuiltInAGC(enable);
887 });
888}
889
890int32_t AdmProxy::EnableBuiltInNS(bool enable) {
891 return WithPlatformAdm<int32_t>(-1, [enable](webrtc::AudioDeviceModule& adm) {
892 return adm.EnableBuiltInNS(enable);
893 });
894}
895
896#if defined(WEBRTC_IOS)
897int AdmProxy::GetPlayoutAudioParameters(webrtc::AudioParameters* params) const {
898 return WithPlatformAdm<int>(-1, [params](webrtc::AudioDeviceModule& adm) {
899 return adm.GetPlayoutAudioParameters(params);
900 });
901}
902
903int AdmProxy::GetRecordAudioParameters(webrtc::AudioParameters* params) const {
904 return WithPlatformAdm<int>(-1, [params](webrtc::AudioDeviceModule& adm) {
905 return adm.GetRecordAudioParameters(params);
906 });
907}
908#endif
909
910int32_t AdmProxy::SetObserver(webrtc::AudioDeviceObserver* observer) {
911 return RunOnWorker([this, observer] {
912 RTC_DCHECK_RUN_ON(worker_thread_);
913 // Stored (including nullptr to clear) so a registration made before the
914 // Platform ADM exists is re-applied once it is created
915 audio_observer_ = observer;
916 if (platform_adm_) {
917 return platform_adm_->SetObserver(observer);
918 }
919 return 0;
920 });
921}
922
923} // namespace livekit_ffi