| 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/audio_device_controller.h" |
| 18 | |
| 19 | #include <string> |
| 20 | #include <utility> |
| 21 | |
| 22 | namespace livekit_ffi { |
| 23 | |
| 24 | AudioDeviceController::AudioDeviceController( |
| 25 | std::shared_ptr<RtcRuntime> rtc_runtime, |
| 26 | webrtc::scoped_refptr<AdmProxy> adm_proxy) |
| 27 | : rtc_runtime_(std::move(rtc_runtime)), adm_proxy_(std::move(adm_proxy)) {} |
| 28 | |
| 29 | int16_t AudioDeviceController::playout_devices() const { |
| 30 | return adm_proxy_->PlayoutDevices(); |
| 31 | } |
| 32 | |
| 33 | int16_t AudioDeviceController::recording_devices() const { |
| 34 | return adm_proxy_->RecordingDevices(); |
| 35 | } |
| 36 | |
| 37 | rust::String AudioDeviceController::playout_device_name(uint16_t index) const { |
| 38 | char name[webrtc::kAdmMaxDeviceNameSize] = {0}; |
| 39 | char guid[webrtc::kAdmMaxGuidSize] = {0}; |
| 40 | adm_proxy_->PlayoutDeviceName(index, name, guid); |
| 41 | return rust::String(name); |
| 42 | } |
| 43 | |
| 44 | rust::String AudioDeviceController::recording_device_name(uint16_t index) const { |
| 45 | char name[webrtc::kAdmMaxDeviceNameSize] = {0}; |
| 46 | char guid[webrtc::kAdmMaxGuidSize] = {0}; |
| 47 | adm_proxy_->RecordingDeviceName(index, name, guid); |
| 48 | return rust::String(name); |
| 49 | } |
| 50 | |
| 51 | rust::String AudioDeviceController::playout_device_guid(uint16_t index) const { |
| 52 | char name[webrtc::kAdmMaxDeviceNameSize] = {0}; |
| 53 | char guid[webrtc::kAdmMaxGuidSize] = {0}; |
| 54 | adm_proxy_->PlayoutDeviceName(index, name, guid); |
| 55 | return rust::String(guid); |
| 56 | } |
| 57 | |
| 58 | rust::String AudioDeviceController::recording_device_guid(uint16_t index) const { |
| 59 | char name[webrtc::kAdmMaxDeviceNameSize] = {0}; |
| 60 | char guid[webrtc::kAdmMaxGuidSize] = {0}; |
| 61 | adm_proxy_->RecordingDeviceName(index, name, guid); |
| 62 | return rust::String(guid); |
| 63 | } |
| 64 | |
| 65 | bool AudioDeviceController::set_playout_device(uint16_t index) const { |
| 66 | return adm_proxy_->SetPlayoutDevice(index) == 0; |
| 67 | } |
| 68 | |
| 69 | bool AudioDeviceController::set_recording_device(uint16_t index) const { |
| 70 | return adm_proxy_->SetRecordingDevice(index) == 0; |
| 71 | } |
| 72 | |
| 73 | bool AudioDeviceController::set_playout_device_by_guid(rust::String guid) const { |
| 74 | int16_t count = adm_proxy_->PlayoutDevices(); |
| 75 | |
| 76 | // Try to find a device matching the GUID |
| 77 | for (int16_t i = 0; i < count; i++) { |
| 78 | char name[webrtc::kAdmMaxDeviceNameSize] = {0}; |
| 79 | char device_guid[webrtc::kAdmMaxGuidSize] = {0}; |
| 80 | if (adm_proxy_->PlayoutDeviceName(i, name, device_guid) == 0) { |
| 81 | if (std::string(guid.c_str()) == std::string(device_guid)) { |
| 82 | return adm_proxy_->SetPlayoutDevice(i) == 0; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // No match found - fall back to default device (index 0). |
| 88 | // This handles mobile platforms (iOS/Android) where: |
| 89 | // - GUIDs may be empty or not meaningful |
| 90 | // - Device selection is a no-op (system handles routing) |
| 91 | if (count > 0) { |
| 92 | return adm_proxy_->SetPlayoutDevice(0) == 0; |
| 93 | } |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | bool AudioDeviceController::set_recording_device_by_guid(rust::String guid) const { |
| 98 | int16_t count = adm_proxy_->RecordingDevices(); |
| 99 | |
| 100 | // Try to find a device matching the GUID |
| 101 | for (int16_t i = 0; i < count; i++) { |
| 102 | char name[webrtc::kAdmMaxDeviceNameSize] = {0}; |
| 103 | char device_guid[webrtc::kAdmMaxGuidSize] = {0}; |
| 104 | if (adm_proxy_->RecordingDeviceName(i, name, device_guid) == 0) { |
| 105 | if (std::string(guid.c_str()) == std::string(device_guid)) { |
| 106 | return adm_proxy_->SetRecordingDevice(i) == 0; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // No match found - fall back to default device (index 0). |
| 112 | // This handles mobile platforms (iOS/Android) where: |
| 113 | // - GUIDs may be empty or not meaningful |
| 114 | // - Device selection is a no-op (system handles routing) |
| 115 | if (count > 0) { |
| 116 | return adm_proxy_->SetRecordingDevice(0) == 0; |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | bool AudioDeviceController::stop_recording() const { |
| 122 | return adm_proxy_->StopRecording() == 0; |
| 123 | } |
| 124 | |
| 125 | bool AudioDeviceController::init_recording() const { |
| 126 | return adm_proxy_->InitRecording() == 0; |
| 127 | } |
| 128 | |
| 129 | bool AudioDeviceController::start_recording() const { |
| 130 | return adm_proxy_->StartRecording() == 0; |
| 131 | } |
| 132 | |
| 133 | bool AudioDeviceController::recording_is_initialized() const { |
| 134 | return adm_proxy_->RecordingIsInitialized(); |
| 135 | } |
| 136 | |
| 137 | bool AudioDeviceController::stop_playout() const { |
| 138 | return adm_proxy_->StopPlayout() == 0; |
| 139 | } |
| 140 | |
| 141 | bool AudioDeviceController::init_playout() const { |
| 142 | return adm_proxy_->InitPlayout() == 0; |
| 143 | } |
| 144 | |
| 145 | bool AudioDeviceController::start_playout() const { |
| 146 | return adm_proxy_->StartPlayout() == 0; |
| 147 | } |
| 148 | |
| 149 | bool AudioDeviceController::playout_is_initialized() const { |
| 150 | return adm_proxy_->PlayoutIsInitialized(); |
| 151 | } |
| 152 | |
| 153 | bool AudioDeviceController::builtin_aec_is_available() const { |
| 154 | return adm_proxy_->BuiltInAECIsAvailable(); |
| 155 | } |
| 156 | |
| 157 | bool AudioDeviceController::builtin_agc_is_available() const { |
| 158 | return adm_proxy_->BuiltInAGCIsAvailable(); |
| 159 | } |
| 160 | |
| 161 | bool AudioDeviceController::builtin_ns_is_available() const { |
| 162 | return adm_proxy_->BuiltInNSIsAvailable(); |
| 163 | } |
| 164 | |
| 165 | bool AudioDeviceController::enable_builtin_aec(bool enable) const { |
| 166 | return adm_proxy_->EnableBuiltInAEC(enable) == 0; |
| 167 | } |
| 168 | |
| 169 | bool AudioDeviceController::enable_builtin_agc(bool enable) const { |
| 170 | return adm_proxy_->EnableBuiltInAGC(enable) == 0; |
| 171 | } |
| 172 | |
| 173 | bool AudioDeviceController::enable_builtin_ns(bool enable) const { |
| 174 | return adm_proxy_->EnableBuiltInNS(enable) == 0; |
| 175 | } |
| 176 | |
| 177 | void AudioDeviceController::set_adm_recording_enabled(bool enabled) const { |
| 178 | adm_proxy_->set_recording_enabled(enabled); |
| 179 | } |
| 180 | |
| 181 | bool AudioDeviceController::adm_recording_enabled() const { |
| 182 | return adm_proxy_->recording_enabled(); |
| 183 | } |
| 184 | |
| 185 | void AudioDeviceController::set_adm_playout_enabled(bool enabled) const { |
| 186 | adm_proxy_->set_playout_enabled(enabled); |
| 187 | } |
| 188 | |
| 189 | bool AudioDeviceController::adm_playout_enabled() const { |
| 190 | return adm_proxy_->playout_enabled(); |
| 191 | } |
| 192 | |
| 193 | bool AudioDeviceController::acquire_platform_adm() const { |
| 194 | return adm_proxy_->AcquirePlatformAdm(); |
| 195 | } |
| 196 | |
| 197 | void AudioDeviceController::release_platform_adm() const { |
| 198 | adm_proxy_->ReleasePlatformAdm(); |
| 199 | } |
| 200 | |
| 201 | int AudioDeviceController::platform_adm_ref_count() const { |
| 202 | return adm_proxy_->platform_adm_ref_count(); |
| 203 | } |
| 204 | |
| 205 | bool AudioDeviceController::is_platform_adm_active() const { |
| 206 | return adm_proxy_->is_platform_adm_active(); |
| 207 | } |
| 208 | |
| 209 | } // namespace livekit_ffi |