| 1 | /* |
| 2 | * Copyright 2025 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/synthetic_audio_device.h" |
| 18 | |
| 19 | const int kSampleRate = 48000; |
| 20 | const int kChannels = 2; |
| 21 | const int kBytesPerSample = kChannels * sizeof(int16_t); |
| 22 | const int kSamplesPer10Ms = kSampleRate / 100; |
| 23 | |
| 24 | namespace livekit_ffi { |
| 25 | |
| 26 | SyntheticAudioDevice::SyntheticAudioDevice(const webrtc::Environment& env) |
| 27 | : env_(env), |
| 28 | data_(kSamplesPer10Ms * kChannels) {} |
| 29 | |
| 30 | SyntheticAudioDevice::~SyntheticAudioDevice() { |
| 31 | Terminate(); |
| 32 | } |
| 33 | |
| 34 | int32_t SyntheticAudioDevice::ActiveAudioLayer(AudioLayer* audioLayer) const { |
| 35 | *audioLayer = AudioLayer::kDummyAudio; |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | int32_t SyntheticAudioDevice::RegisterAudioCallback(webrtc::AudioTransport* transport) { |
| 40 | webrtc::MutexLock lock(&mutex_); |
| 41 | audio_transport_ = transport; |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | int32_t SyntheticAudioDevice::Init() { |
| 46 | webrtc::MutexLock lock(&mutex_); |
| 47 | if (initialized_) |
| 48 | return 0; |
| 49 | |
| 50 | audio_queue_ = |
| 51 | env_.task_queue_factory().CreateTaskQueue( |
| 52 | "SyntheticAudioDevice", webrtc::TaskQueueFactory::Priority::NORMAL); |
| 53 | |
| 54 | audio_task_ = |
| 55 | webrtc::RepeatingTaskHandle::Start(audio_queue_.get(), [this]() { |
| 56 | webrtc::MutexLock lock(&mutex_); |
| 57 | |
| 58 | if (playing_) { |
| 59 | int64_t elapsed_time_ms = -1; |
| 60 | int64_t ntp_time_ms = -1; |
| 61 | size_t n_samples_out = 0; |
| 62 | void* data = data_.data(); |
| 63 | |
| 64 | // Request the AudioData, otherwise WebRTC will ignore the packets. |
| 65 | // 10ms of audio data. |
| 66 | audio_transport_->NeedMorePlayData( |
| 67 | kSamplesPer10Ms, kBytesPerSample, kChannels, kSampleRate, data, |
| 68 | n_samples_out, &elapsed_time_ms, &ntp_time_ms); |
| 69 | } |
| 70 | |
| 71 | return webrtc::TimeDelta::Millis(10); |
| 72 | }); |
| 73 | |
| 74 | initialized_ = true; |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | int32_t SyntheticAudioDevice::Terminate() { |
| 79 | { |
| 80 | webrtc::MutexLock lock(&mutex_); |
| 81 | if (!initialized_) |
| 82 | return 0; |
| 83 | |
| 84 | initialized_ = false; |
| 85 | } |
| 86 | audio_queue_ = nullptr; |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | bool SyntheticAudioDevice::Initialized() const { |
| 91 | webrtc::MutexLock lock(&mutex_); |
| 92 | return initialized_; |
| 93 | } |
| 94 | |
| 95 | int16_t SyntheticAudioDevice::PlayoutDevices() { |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | int16_t SyntheticAudioDevice::RecordingDevices() { |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | int32_t SyntheticAudioDevice::PlayoutDeviceName(uint16_t index, |
| 104 | char name[webrtc::kAdmMaxDeviceNameSize], |
| 105 | char guid[webrtc::kAdmMaxGuidSize]) { |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | int32_t SyntheticAudioDevice::RecordingDeviceName( |
| 110 | uint16_t index, |
| 111 | char name[webrtc::kAdmMaxDeviceNameSize], |
| 112 | char guid[webrtc::kAdmMaxGuidSize]) { |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int32_t SyntheticAudioDevice::SetPlayoutDevice(uint16_t index) { |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | int32_t SyntheticAudioDevice::SetPlayoutDevice(WindowsDeviceType device) { |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | int32_t SyntheticAudioDevice::SetRecordingDevice(uint16_t index) { |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int32_t SyntheticAudioDevice::SetRecordingDevice(WindowsDeviceType device) { |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | int32_t SyntheticAudioDevice::PlayoutIsAvailable(bool* available) { |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | int32_t SyntheticAudioDevice::InitPlayout() { |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | bool SyntheticAudioDevice::PlayoutIsInitialized() const { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | int32_t SyntheticAudioDevice::RecordingIsAvailable(bool* available) { |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | int32_t SyntheticAudioDevice::InitRecording() { |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | bool SyntheticAudioDevice::RecordingIsInitialized() const { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | int32_t SyntheticAudioDevice::StartPlayout() { |
| 157 | webrtc::MutexLock lock(&mutex_); |
| 158 | playing_ = true; |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | int32_t SyntheticAudioDevice::StopPlayout() { |
| 163 | webrtc::MutexLock lock(&mutex_); |
| 164 | playing_ = false; |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | bool SyntheticAudioDevice::Playing() const { |
| 169 | webrtc::MutexLock lock(&mutex_); |
| 170 | return playing_; |
| 171 | } |
| 172 | |
| 173 | int32_t SyntheticAudioDevice::StartRecording() { |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | int32_t SyntheticAudioDevice::StopRecording() { |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | bool SyntheticAudioDevice::Recording() const { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | int32_t SyntheticAudioDevice::InitSpeaker() { |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | bool SyntheticAudioDevice::SpeakerIsInitialized() const { |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | int32_t SyntheticAudioDevice::InitMicrophone() { |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | bool SyntheticAudioDevice::MicrophoneIsInitialized() const { |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | int32_t SyntheticAudioDevice::SpeakerVolumeIsAvailable(bool* available) { |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | int32_t SyntheticAudioDevice::SetSpeakerVolume(uint32_t volume) { |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | int32_t SyntheticAudioDevice::SpeakerVolume(uint32_t* volume) const { |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | int32_t SyntheticAudioDevice::MaxSpeakerVolume(uint32_t* maxVolume) const { |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | int32_t SyntheticAudioDevice::MinSpeakerVolume(uint32_t* minVolume) const { |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | int32_t SyntheticAudioDevice::MicrophoneVolumeIsAvailable(bool* available) { |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | int32_t SyntheticAudioDevice::SetMicrophoneVolume(uint32_t volume) { |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | int32_t SyntheticAudioDevice::MicrophoneVolume(uint32_t* volume) const { |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | int32_t SyntheticAudioDevice::MaxMicrophoneVolume(uint32_t* maxVolume) const { |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | int32_t SyntheticAudioDevice::MinMicrophoneVolume(uint32_t* minVolume) const { |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | int32_t SyntheticAudioDevice::SpeakerMuteIsAvailable(bool* available) { |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | int32_t SyntheticAudioDevice::SetSpeakerMute(bool enable) { |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | int32_t SyntheticAudioDevice::SpeakerMute(bool* enabled) const { |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | int32_t SyntheticAudioDevice::MicrophoneMuteIsAvailable(bool* available) { |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | int32_t SyntheticAudioDevice::SetMicrophoneMute(bool enable) { |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | int32_t SyntheticAudioDevice::MicrophoneMute(bool* enabled) const { |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | int32_t SyntheticAudioDevice::StereoPlayoutIsAvailable(bool* available) const { |
| 266 | *available = true; |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | int32_t SyntheticAudioDevice::SetStereoPlayout(bool enable) { |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | int32_t SyntheticAudioDevice::StereoPlayout(bool* enabled) const { |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | int32_t SyntheticAudioDevice::StereoRecordingIsAvailable(bool* available) const { |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | int32_t SyntheticAudioDevice::SetStereoRecording(bool enable) { |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | int32_t SyntheticAudioDevice::StereoRecording(bool* enabled) const { |
| 287 | *enabled = true; |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | int32_t SyntheticAudioDevice::PlayoutDelay(uint16_t* delayMS) const { |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | bool SyntheticAudioDevice::BuiltInAECIsAvailable() const { |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | bool SyntheticAudioDevice::BuiltInAGCIsAvailable() const { |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | bool SyntheticAudioDevice::BuiltInNSIsAvailable() const { |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | int32_t SyntheticAudioDevice::EnableBuiltInAEC(bool enable) { |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | int32_t SyntheticAudioDevice::EnableBuiltInAGC(bool enable) { |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | int32_t SyntheticAudioDevice::EnableBuiltInNS(bool enable) { |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | #if defined(WEBRTC_IOS) |
| 320 | int SyntheticAudioDevice::GetPlayoutAudioParameters( |
| 321 | webrtc::AudioParameters* params) const { |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | int SyntheticAudioDevice::GetRecordAudioParameters( |
| 326 | webrtc::AudioParameters* params) const { |
| 327 | return 0; |
| 328 | } |
| 329 | #endif // WEBRTC_IOS |
| 330 | |
| 331 | int32_t SyntheticAudioDevice::SetObserver(webrtc::AudioDeviceObserver* observer) { |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | } // namespace livekit_ffi |