| 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 | #pragma once |
| 18 | |
| 19 | #include <atomic> |
| 20 | |
| 21 | #include "api/environment/environment.h" |
| 22 | #include "api/task_queue/task_queue_base.h" |
| 23 | #include "modules/audio_device/include/audio_device.h" |
| 24 | #include "rtc_base/synchronization/mutex.h" |
| 25 | #include "rtc_base/task_utils/repeating_task.h" |
| 26 | |
| 27 | namespace livekit_ffi { |
| 28 | |
| 29 | class SyntheticAudioDevice : public webrtc::AudioDeviceModule { |
| 30 | public: |
| 31 | SyntheticAudioDevice(const webrtc::Environment& env); |
| 32 | ~SyntheticAudioDevice() override; |
| 33 | |
| 34 | int32_t ActiveAudioLayer(AudioLayer* audioLayer) const override; |
| 35 | int32_t RegisterAudioCallback(webrtc::AudioTransport* transport) override; |
| 36 | |
| 37 | int32_t Init() override; |
| 38 | int32_t Terminate() override; |
| 39 | bool Initialized() const override; |
| 40 | |
| 41 | int16_t PlayoutDevices() override; |
| 42 | int16_t RecordingDevices() override; |
| 43 | int32_t PlayoutDeviceName(uint16_t index, |
| 44 | char name[webrtc::kAdmMaxDeviceNameSize], |
| 45 | char guid[webrtc::kAdmMaxGuidSize]) override; |
| 46 | |
| 47 | int32_t RecordingDeviceName(uint16_t index, |
| 48 | char name[webrtc::kAdmMaxDeviceNameSize], |
| 49 | char guid[webrtc::kAdmMaxGuidSize]) override; |
| 50 | |
| 51 | int32_t SetPlayoutDevice(uint16_t index) override; |
| 52 | int32_t SetPlayoutDevice(WindowsDeviceType device) override; |
| 53 | int32_t SetRecordingDevice(uint16_t index) override; |
| 54 | int32_t SetRecordingDevice(WindowsDeviceType device) override; |
| 55 | |
| 56 | int32_t PlayoutIsAvailable(bool* available) override; |
| 57 | int32_t InitPlayout() override; |
| 58 | bool PlayoutIsInitialized() const override; |
| 59 | int32_t RecordingIsAvailable(bool* available) override; |
| 60 | int32_t InitRecording() override; |
| 61 | bool RecordingIsInitialized() const override; |
| 62 | |
| 63 | int32_t StartPlayout() override; |
| 64 | int32_t StopPlayout() override; |
| 65 | bool Playing() const override; |
| 66 | int32_t StartRecording() override; |
| 67 | int32_t StopRecording() override; |
| 68 | bool Recording() const override; |
| 69 | |
| 70 | int32_t InitSpeaker() override; |
| 71 | bool SpeakerIsInitialized() const override; |
| 72 | int32_t InitMicrophone() override; |
| 73 | bool MicrophoneIsInitialized() const override; |
| 74 | |
| 75 | int32_t SpeakerVolumeIsAvailable(bool* available) override; |
| 76 | int32_t SetSpeakerVolume(uint32_t volume) override; |
| 77 | int32_t SpeakerVolume(uint32_t* volume) const override; |
| 78 | int32_t MaxSpeakerVolume(uint32_t* maxVolume) const override; |
| 79 | int32_t MinSpeakerVolume(uint32_t* minVolume) const override; |
| 80 | |
| 81 | int32_t MicrophoneVolumeIsAvailable(bool* available) override; |
| 82 | int32_t SetMicrophoneVolume(uint32_t volume) override; |
| 83 | int32_t MicrophoneVolume(uint32_t* volume) const override; |
| 84 | int32_t MaxMicrophoneVolume(uint32_t* maxVolume) const override; |
| 85 | int32_t MinMicrophoneVolume(uint32_t* minVolume) const override; |
| 86 | |
| 87 | int32_t SpeakerMuteIsAvailable(bool* available) override; |
| 88 | int32_t SetSpeakerMute(bool enable) override; |
| 89 | int32_t SpeakerMute(bool* enabled) const override; |
| 90 | |
| 91 | int32_t MicrophoneMuteIsAvailable(bool* available) override; |
| 92 | int32_t SetMicrophoneMute(bool enable) override; |
| 93 | int32_t MicrophoneMute(bool* enabled) const override; |
| 94 | |
| 95 | int32_t StereoPlayoutIsAvailable(bool* available) const override; |
| 96 | int32_t SetStereoPlayout(bool enable) override; |
| 97 | int32_t StereoPlayout(bool* enabled) const override; |
| 98 | int32_t StereoRecordingIsAvailable(bool* available) const override; |
| 99 | int32_t SetStereoRecording(bool enable) override; |
| 100 | int32_t StereoRecording(bool* enabled) const override; |
| 101 | |
| 102 | int32_t PlayoutDelay(uint16_t* delayMS) const override; |
| 103 | |
| 104 | bool BuiltInAECIsAvailable() const override; |
| 105 | bool BuiltInAGCIsAvailable() const override; |
| 106 | bool BuiltInNSIsAvailable() const override; |
| 107 | |
| 108 | int32_t EnableBuiltInAEC(bool enable) override; |
| 109 | int32_t EnableBuiltInAGC(bool enable) override; |
| 110 | int32_t EnableBuiltInNS(bool enable) override; |
| 111 | |
| 112 | #if defined(WEBRTC_IOS) |
| 113 | int GetPlayoutAudioParameters(webrtc::AudioParameters* params) const override; |
| 114 | int GetRecordAudioParameters(webrtc::AudioParameters* params) const override; |
| 115 | #endif // WEBRTC_IOS |
| 116 | |
| 117 | int32_t SetObserver(webrtc::AudioDeviceObserver* sink) override; |
| 118 | |
| 119 | private: |
| 120 | mutable webrtc::Mutex mutex_; |
| 121 | std::vector<int16_t> data_; |
| 122 | std::unique_ptr<webrtc::TaskQueueBase, webrtc::TaskQueueDeleter> audio_queue_; |
| 123 | webrtc::RepeatingTaskHandle audio_task_; |
| 124 | webrtc::AudioTransport* audio_transport_; |
| 125 | const webrtc::Environment& env_; |
| 126 | bool playing_{false}; |
| 127 | bool initialized_{false}; |
| 128 | }; |
| 129 | } // namespace livekit_ffi |