BlueGit

ict rusty-teams

Files

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

adm_proxy.cpp

adm_proxy.cpp 27.2 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
434// WebRtcVoiceEngine::Init asks for the communications default at every call start; the app's default is the Windows default device.
435AdmProxy::WindowsDeviceType AdmProxy::SystemDefaultFor(WindowsDeviceType requested) {
436 return requested == WindowsDeviceType::kDefaultCommunicationDevice
437 ? WindowsDeviceType::kDefaultDevice
438 : requested;
439}
440
441int32_t AdmProxy::SetPlayoutDevice(uint16_t index) {
442 return RunOnWorker([this, index] {
443 RTC_DCHECK_RUN_ON(worker_thread_);
444 selected_playout_device_ = DeviceSelection(index);
445 if (platform_adm_) {
446 return platform_adm_->SetPlayoutDevice(index);
447 }
448 return 0;
449 });
450}
451
452int32_t AdmProxy::SetPlayoutDevice(WindowsDeviceType requested) {
453 const WindowsDeviceType device = SystemDefaultFor(requested);
454 return RunOnWorker([this, device] {
455 RTC_DCHECK_RUN_ON(worker_thread_);
456 selected_playout_device_ = DeviceSelection(device);
457 if (platform_adm_) {
458 return platform_adm_->SetPlayoutDevice(device);
459 }
460 return 0;
461 });
462}
463
464int32_t AdmProxy::SetRecordingDevice(uint16_t index) {
465 return RunOnWorker([this, index] {
466 RTC_DCHECK_RUN_ON(worker_thread_);
467 selected_recording_device_ = DeviceSelection(index);
468 if (platform_adm_) {
469 return platform_adm_->SetRecordingDevice(index);
470 }
471 return 0;
472 });
473}
474
475int32_t AdmProxy::SetRecordingDevice(WindowsDeviceType requested) {
476 const WindowsDeviceType device = SystemDefaultFor(requested);
477 return RunOnWorker([this, device] {
478 RTC_DCHECK_RUN_ON(worker_thread_);
479 selected_recording_device_ = DeviceSelection(device);
480 if (platform_adm_) {
481 return platform_adm_->SetRecordingDevice(device);
482 }
483 return 0;
484 });
485}
486
487int32_t AdmProxy::PlayoutIsAvailable(bool* available) {
488 return RunOnWorker([this, available] {
489 RTC_DCHECK_RUN_ON(worker_thread_);
490 if (platform_adm_) {
491 return platform_adm_->PlayoutIsAvailable(available);
492 }
493 *available = true; // Synthetic playout is always available
494 return 0;
495 });
496}
497
498int32_t AdmProxy::InitPlayout() {
499 return RunOnWorker([this] {
500 RTC_DCHECK_RUN_ON(worker_thread_);
501 if (IsPlatformPlayoutActive()) {
502 return platform_adm_->InitPlayout();
503 }
504 // Synthetic mode
505 if (synthetic_adm_) {
506 return synthetic_adm_->InitPlayout();
507 }
508 return -1;
509 });
510}
511
512bool AdmProxy::PlayoutIsInitialized() const {
513 return RunOnWorker([this] {
514 RTC_DCHECK_RUN_ON(worker_thread_);
515 if (IsPlatformPlayoutActive()) {
516 return platform_adm_->PlayoutIsInitialized();
517 }
518 // Synthetic mode
519 return synthetic_adm_ && synthetic_adm_->PlayoutIsInitialized();
520 });
521}
522
523int32_t AdmProxy::RecordingIsAvailable(bool* available) {
524 return RunOnWorker([this, available] {
525 RTC_DCHECK_RUN_ON(worker_thread_);
526 if (platform_adm_) {
527 return platform_adm_->RecordingIsAvailable(available);
528 }
529 *available = false; // Recording not available in synthetic mode
530 return 0;
531 });
532}
533
534int32_t AdmProxy::InitRecording() {
535 return RunOnWorker([this] {
536 RTC_DCHECK_RUN_ON(worker_thread_);
537 auto* adm = RecordingAdm();
538 if (!adm) {
539 // Recording not available (no platform ADM or recording disabled)
540 // Return success to avoid breaking WebRTC's initialization flow
541 return 0;
542 }
543 return adm->InitRecording();
544 });
545}
546
547bool AdmProxy::RecordingIsInitialized() const {
548 return RunOnWorker([this] {
549 RTC_DCHECK_RUN_ON(worker_thread_);
550 auto* adm = RecordingAdm();
551 if (adm) {
552 return adm->RecordingIsInitialized();
553 }
554 return false; // Recording not available
555 });
556}
557
558int32_t AdmProxy::StartPlayout() {
559 return RunOnWorker([this] {
560 RTC_DCHECK_RUN_ON(worker_thread_);
561 playing_ = true;
562
563 if (IsPlatformPlayoutActive()) {
564 return platform_adm_->StartPlayout();
565 }
566 // Synthetic mode
567 if (synthetic_adm_) {
568 return synthetic_adm_->StartPlayout();
569 }
570 return -1;
571 });
572}
573
574int32_t AdmProxy::StopPlayout() {
575 return RunOnWorker([this] {
576 RTC_DCHECK_RUN_ON(worker_thread_);
577 playing_ = false;
578
579 // Stop both ADMs
580 if (synthetic_adm_) {
581 synthetic_adm_->StopPlayout();
582 }
583 if (platform_adm_) {
584 platform_adm_->StopPlayout();
585 }
586 return 0;
587 });
588}
589
590bool AdmProxy::Playing() const {
591 return RunOnWorker([this] {
592 RTC_DCHECK_RUN_ON(worker_thread_);
593 if (IsPlatformPlayoutActive()) {
594 return platform_adm_->Playing();
595 }
596 return synthetic_adm_ && synthetic_adm_->Playing();
597 });
598}
599
600int32_t AdmProxy::StartRecording() {
601 return RunOnWorker([this] {
602 RTC_DCHECK_RUN_ON(worker_thread_);
603 auto* adm = RecordingAdm();
604 if (!adm) {
605 // Recording not available - return success to avoid breaking WebRTC
606 return 0;
607 }
608 recording_ = true;
609 return adm->StartRecording();
610 });
611}
612
613int32_t AdmProxy::StopRecording() {
614 return RunOnWorker([this] {
615 RTC_DCHECK_RUN_ON(worker_thread_);
616 recording_ = false;
617
618 auto* adm = RecordingAdm();
619 if (adm) {
620 return adm->StopRecording();
621 }
622 return 0;
623 });
624}
625
626bool AdmProxy::Recording() const {
627 return RunOnWorker([this] {
628 RTC_DCHECK_RUN_ON(worker_thread_);
629 auto* adm = RecordingAdm();
630 if (adm) {
631 return adm->Recording();
632 }
633 return false;
634 });
635}
636
637int32_t AdmProxy::InitSpeaker() {
638 return WithPlatformAdm<int32_t>(0, [](webrtc::AudioDeviceModule& adm) {
639 return adm.InitSpeaker();
640 });
641}
642
643bool AdmProxy::SpeakerIsInitialized() const {
644 return WithPlatformAdm<bool>(true, [](webrtc::AudioDeviceModule& adm) {
645 return adm.SpeakerIsInitialized();
646 });
647}
648
649int32_t AdmProxy::InitMicrophone() {
650 return WithPlatformAdm<int32_t>(0, [](webrtc::AudioDeviceModule& adm) {
651 return adm.InitMicrophone();
652 });
653}
654
655bool AdmProxy::MicrophoneIsInitialized() const {
656 return WithPlatformAdm<bool>(false, [](webrtc::AudioDeviceModule& adm) {
657 return adm.MicrophoneIsInitialized();
658 });
659}
660
661int32_t AdmProxy::SpeakerVolumeIsAvailable(bool* available) {
662 return RunOnWorker([this, available] {
663 RTC_DCHECK_RUN_ON(worker_thread_);
664 if (platform_adm_) {
665 return platform_adm_->SpeakerVolumeIsAvailable(available);
666 }
667 *available = false;
668 return 0;
669 });
670}
671
672int32_t AdmProxy::SetSpeakerVolume(uint32_t volume) {
673 return WithPlatformAdm<int32_t>(-1, [volume](webrtc::AudioDeviceModule& adm) {
674 return adm.SetSpeakerVolume(volume);
675 });
676}
677
678int32_t AdmProxy::SpeakerVolume(uint32_t* volume) const {
679 return WithPlatformAdm<int32_t>(-1, [volume](webrtc::AudioDeviceModule& adm) {
680 return adm.SpeakerVolume(volume);
681 });
682}
683
684int32_t AdmProxy::MaxSpeakerVolume(uint32_t* maxVolume) const {
685 return WithPlatformAdm<int32_t>(-1, [maxVolume](webrtc::AudioDeviceModule& adm) {
686 return adm.MaxSpeakerVolume(maxVolume);
687 });
688}
689
690int32_t AdmProxy::MinSpeakerVolume(uint32_t* minVolume) const {
691 return WithPlatformAdm<int32_t>(-1, [minVolume](webrtc::AudioDeviceModule& adm) {
692 return adm.MinSpeakerVolume(minVolume);
693 });
694}
695
696int32_t AdmProxy::MicrophoneVolumeIsAvailable(bool* available) {
697 return RunOnWorker([this, available] {
698 RTC_DCHECK_RUN_ON(worker_thread_);
699 if (platform_adm_) {
700 return platform_adm_->MicrophoneVolumeIsAvailable(available);
701 }
702 *available = false;
703 return 0;
704 });
705}
706
707int32_t AdmProxy::SetMicrophoneVolume(uint32_t volume) {
708 return WithPlatformAdm<int32_t>(-1, [volume](webrtc::AudioDeviceModule& adm) {
709 return adm.SetMicrophoneVolume(volume);
710 });
711}
712
713int32_t AdmProxy::MicrophoneVolume(uint32_t* volume) const {
714 return WithPlatformAdm<int32_t>(-1, [volume](webrtc::AudioDeviceModule& adm) {
715 return adm.MicrophoneVolume(volume);
716 });
717}
718
719int32_t AdmProxy::MaxMicrophoneVolume(uint32_t* maxVolume) const {
720 return WithPlatformAdm<int32_t>(-1, [maxVolume](webrtc::AudioDeviceModule& adm) {
721 return adm.MaxMicrophoneVolume(maxVolume);
722 });
723}
724
725int32_t AdmProxy::MinMicrophoneVolume(uint32_t* minVolume) const {
726 return WithPlatformAdm<int32_t>(-1, [minVolume](webrtc::AudioDeviceModule& adm) {
727 return adm.MinMicrophoneVolume(minVolume);
728 });
729}
730
731int32_t AdmProxy::SpeakerMuteIsAvailable(bool* available) {
732 return RunOnWorker([this, available] {
733 RTC_DCHECK_RUN_ON(worker_thread_);
734 if (platform_adm_) {
735 return platform_adm_->SpeakerMuteIsAvailable(available);
736 }
737 *available = false;
738 return 0;
739 });
740}
741
742int32_t AdmProxy::SetSpeakerMute(bool enable) {
743 return WithPlatformAdm<int32_t>(-1, [enable](webrtc::AudioDeviceModule& adm) {
744 return adm.SetSpeakerMute(enable);
745 });
746}
747
748int32_t AdmProxy::SpeakerMute(bool* enabled) const {
749 return WithPlatformAdm<int32_t>(-1, [enabled](webrtc::AudioDeviceModule& adm) {
750 return adm.SpeakerMute(enabled);
751 });
752}
753
754int32_t AdmProxy::MicrophoneMuteIsAvailable(bool* available) {
755 return RunOnWorker([this, available] {
756 RTC_DCHECK_RUN_ON(worker_thread_);
757 if (platform_adm_) {
758 return platform_adm_->MicrophoneMuteIsAvailable(available);
759 }
760 *available = false;
761 return 0;
762 });
763}
764
765int32_t AdmProxy::SetMicrophoneMute(bool enable) {
766 return WithPlatformAdm<int32_t>(-1, [enable](webrtc::AudioDeviceModule& adm) {
767 return adm.SetMicrophoneMute(enable);
768 });
769}
770
771int32_t AdmProxy::MicrophoneMute(bool* enabled) const {
772 return WithPlatformAdm<int32_t>(-1, [enabled](webrtc::AudioDeviceModule& adm) {
773 return adm.MicrophoneMute(enabled);
774 });
775}
776
777int32_t AdmProxy::StereoPlayoutIsAvailable(bool* available) const {
778 return RunOnWorker([this, available] {
779 RTC_DCHECK_RUN_ON(worker_thread_);
780 if (platform_adm_) {
781 return platform_adm_->StereoPlayoutIsAvailable(available);
782 }
783 *available = true;
784 return 0;
785 });
786}
787
788int32_t AdmProxy::SetStereoPlayout(bool enable) {
789 return RunOnWorker([this, enable] {
790 RTC_DCHECK_RUN_ON(worker_thread_);
791 // Stored so a selection made before the Platform ADM exists can be
792 // re-applied once it is created
793 selected_stereo_playout_ = enable;
794 if (platform_adm_) {
795 return platform_adm_->SetStereoPlayout(enable);
796 }
797 return 0;
798 });
799}
800
801int32_t AdmProxy::StereoPlayout(bool* enabled) const {
802 return RunOnWorker([this, enabled] {
803 RTC_DCHECK_RUN_ON(worker_thread_);
804 if (platform_adm_) {
805 return platform_adm_->StereoPlayout(enabled);
806 }
807 // Report a pending pre-acquire configuration, or the default
808 *enabled = selected_stereo_playout_.value_or(true);
809 return 0;
810 });
811}
812
813int32_t AdmProxy::StereoRecordingIsAvailable(bool* available) const {
814 return RunOnWorker([this, available] {
815 RTC_DCHECK_RUN_ON(worker_thread_);
816 if (platform_adm_) {
817 return platform_adm_->StereoRecordingIsAvailable(available);
818 }
819 *available = false;
820 return 0;
821 });
822}
823
824int32_t AdmProxy::SetStereoRecording(bool enable) {
825 return RunOnWorker([this, enable] {
826 RTC_DCHECK_RUN_ON(worker_thread_);
827 // Stored so a selection made before the Platform ADM exists can be
828 // re-applied once it is created
829 selected_stereo_recording_ = enable;
830 if (platform_adm_) {
831 return platform_adm_->SetStereoRecording(enable);
832 }
833 return 0;
834 });
835}
836
837int32_t AdmProxy::StereoRecording(bool* enabled) const {
838 return RunOnWorker([this, enabled] {
839 RTC_DCHECK_RUN_ON(worker_thread_);
840 if (platform_adm_) {
841 return platform_adm_->StereoRecording(enabled);
842 }
843 // Report a pending pre-acquire configuration, or the default
844 *enabled = selected_stereo_recording_.value_or(false);
845 return 0;
846 });
847}
848
849int32_t AdmProxy::PlayoutDelay(uint16_t* delayMS) const {
850 return RunOnWorker([this, delayMS] {
851 RTC_DCHECK_RUN_ON(worker_thread_);
852 if (IsPlatformPlayoutActive()) {
853 return platform_adm_->PlayoutDelay(delayMS);
854 }
855 if (synthetic_adm_) {
856 return synthetic_adm_->PlayoutDelay(delayMS);
857 }
858 *delayMS = 0;
859 return 0;
860 });
861}
862
863bool AdmProxy::BuiltInAECIsAvailable() const {
864#if defined(WEBRTC_WIN)
865 // The Windows voice capture DMO fails StartRecording unless playout runs first,
866 // which WebRTC does not guarantee; AEC3 in software is what Chrome uses too.
867 return false;
868#else
869 return WithPlatformAdm<bool>(false, [](webrtc::AudioDeviceModule& adm) {
870 return adm.BuiltInAECIsAvailable();
871 });
872#endif
873}
874
875bool AdmProxy::BuiltInAGCIsAvailable() const {
876 return WithPlatformAdm<bool>(false, [](webrtc::AudioDeviceModule& adm) {
877 return adm.BuiltInAGCIsAvailable();
878 });
879}
880
881bool AdmProxy::BuiltInNSIsAvailable() const {
882 return WithPlatformAdm<bool>(false, [](webrtc::AudioDeviceModule& adm) {
883 return adm.BuiltInNSIsAvailable();
884 });
885}
886
887int32_t AdmProxy::EnableBuiltInAEC(bool enable) {
888 return WithPlatformAdm<int32_t>(-1, [enable](webrtc::AudioDeviceModule& adm) {
889 return adm.EnableBuiltInAEC(enable);
890 });
891}
892
893int32_t AdmProxy::EnableBuiltInAGC(bool enable) {
894 return WithPlatformAdm<int32_t>(-1, [enable](webrtc::AudioDeviceModule& adm) {
895 return adm.EnableBuiltInAGC(enable);
896 });
897}
898
899int32_t AdmProxy::EnableBuiltInNS(bool enable) {
900 return WithPlatformAdm<int32_t>(-1, [enable](webrtc::AudioDeviceModule& adm) {
901 return adm.EnableBuiltInNS(enable);
902 });
903}
904
905#if defined(WEBRTC_IOS)
906int AdmProxy::GetPlayoutAudioParameters(webrtc::AudioParameters* params) const {
907 return WithPlatformAdm<int>(-1, [params](webrtc::AudioDeviceModule& adm) {
908 return adm.GetPlayoutAudioParameters(params);
909 });
910}
911
912int AdmProxy::GetRecordAudioParameters(webrtc::AudioParameters* params) const {
913 return WithPlatformAdm<int>(-1, [params](webrtc::AudioDeviceModule& adm) {
914 return adm.GetRecordAudioParameters(params);
915 });
916}
917#endif
918
919int32_t AdmProxy::SetObserver(webrtc::AudioDeviceObserver* observer) {
920 return RunOnWorker([this, observer] {
921 RTC_DCHECK_RUN_ON(worker_thread_);
922 // Stored (including nullptr to clear) so a registration made before the
923 // Platform ADM exists is re-applied once it is created
924 audio_observer_ = observer;
925 if (platform_adm_) {
926 return platform_adm_->SetObserver(observer);
927 }
928 return 0;
929 });
930}
931
932} // namespace livekit_ffi