|
| 1 | +/* |
| 2 | + * Copyright 2026 LiveKit |
| 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 <cstdint> |
| 20 | +#include <memory> |
| 21 | +#include <stdexcept> |
| 22 | +#include <string> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +#include "livekit/ffi_handle.h" |
| 26 | +#include "livekit/visibility.h" |
| 27 | + |
| 28 | +namespace livekit { |
| 29 | + |
| 30 | +/// Internal shared state for platform audio handles. |
| 31 | +/// |
| 32 | +/// This forward declaration is exposed only so public wrapper types can hold a |
| 33 | +/// shared implementation pointer. |
| 34 | +/// |
| 35 | +/// @note This object is thread-safe. |
| 36 | +struct PlatformAudioState; |
| 37 | + |
| 38 | +/// Information about a platform audio device. |
| 39 | +/// |
| 40 | +/// @note Device indices may change when audio hardware is added or removed. Use |
| 41 | +/// the stable `id` value when selecting a device. |
| 42 | +struct AudioDeviceInfo { |
| 43 | + /// Current device index. |
| 44 | + std::uint32_t index = 0; |
| 45 | + |
| 46 | + /// Device name reported by the operating system. |
| 47 | + std::string name; |
| 48 | + |
| 49 | + /// Platform-specific stable device identifier. |
| 50 | + std::string id; |
| 51 | +}; |
| 52 | + |
| 53 | +/// Audio processing options for platform microphone capture. |
| 54 | +/// |
| 55 | +/// The default values enable WebRTC's voice processing path for typical |
| 56 | +/// microphone publishing. |
| 57 | +struct PlatformAudioOptions { |
| 58 | + /// Enable acoustic echo cancellation. |
| 59 | + bool echo_cancellation = true; |
| 60 | + |
| 61 | + /// Enable background noise suppression. |
| 62 | + bool noise_suppression = true; |
| 63 | + |
| 64 | + /// Enable automatic gain control. |
| 65 | + bool auto_gain_control = true; |
| 66 | + |
| 67 | + /// Prefer hardware audio processing when the platform provides it. |
| 68 | + bool prefer_hardware = false; |
| 69 | +}; |
| 70 | + |
| 71 | +/// Error raised when platform audio setup or device operations fail. |
| 72 | +class LIVEKIT_API PlatformAudioError : public std::runtime_error { |
| 73 | +public: |
| 74 | + /// Create a platform audio error. |
| 75 | + /// |
| 76 | + /// @param message Human-readable error message. |
| 77 | + explicit PlatformAudioError(const std::string& message) : std::runtime_error(message) {} |
| 78 | +}; |
| 79 | + |
| 80 | +/// Audio source backed by WebRTC's platform Audio Device Module. |
| 81 | +/// |
| 82 | +/// A PlatformAudioSource captures microphone audio automatically. Unlike |
| 83 | +/// AudioSource, callers do not push frames with captureFrame(). |
| 84 | +class LIVEKIT_API PlatformAudioSource { |
| 85 | +public: |
| 86 | + /// Copy construction is disabled. |
| 87 | + PlatformAudioSource(const PlatformAudioSource& other) = delete; |
| 88 | + |
| 89 | + /// Copy assignment is disabled. |
| 90 | + PlatformAudioSource& operator=(const PlatformAudioSource& other) = delete; |
| 91 | + |
| 92 | + /// Move the platform audio source. |
| 93 | + /// |
| 94 | + /// @param other Source to move from. |
| 95 | + /// |
| 96 | + /// @note This operation is not thread-safe. |
| 97 | + PlatformAudioSource(PlatformAudioSource&& other) noexcept = default; |
| 98 | + |
| 99 | + /// Move-assign the platform audio source. |
| 100 | + /// |
| 101 | + /// @param other Source to move from. |
| 102 | + /// @return Reference to this source. |
| 103 | + /// |
| 104 | + /// @note This operation is not thread-safe. |
| 105 | + PlatformAudioSource& operator=(PlatformAudioSource&& other) noexcept = default; |
| 106 | + |
| 107 | + /// Return the underlying FFI handle ID used in FFI requests. |
| 108 | + /// |
| 109 | + /// @note This operation is thread-safe. |
| 110 | + std::uint64_t ffiHandleId() const noexcept { return static_cast<std::uint64_t>(handle_.get()); } |
| 111 | + |
| 112 | +private: |
| 113 | + friend class PlatformAudio; |
| 114 | + |
| 115 | + PlatformAudioSource(FfiHandle handle, std::shared_ptr<PlatformAudioState> platform_audio) noexcept; |
| 116 | + |
| 117 | + FfiHandle handle_; |
| 118 | + std::shared_ptr<PlatformAudioState> platform_audio_; |
| 119 | +}; |
| 120 | + |
| 121 | +/// Platform audio device manager backed by WebRTC's Audio Device Module. |
| 122 | +/// |
| 123 | +/// Use PlatformAudio for microphone capture when built-in echo |
| 124 | +/// cancellation, noise suppression, automatic gain control, and speaker playout |
| 125 | +/// are desired. Use AudioSource instead when the application needs direct access |
| 126 | +/// to raw PCM frames or custom audio generation. |
| 127 | +class LIVEKIT_API PlatformAudio { |
| 128 | +public: |
| 129 | + /// Create a platform audio manager. |
| 130 | + /// |
| 131 | + /// Enables WebRTC's platform Audio Device Module for microphone capture and |
| 132 | + /// speaker playout. |
| 133 | + /// |
| 134 | + /// @throws PlatformAudioError If the FFI response is malformed or the |
| 135 | + /// platform Audio Device Module cannot be created. |
| 136 | + /// |
| 137 | + /// @note This operation is thread-safe. |
| 138 | + PlatformAudio(); |
| 139 | + |
| 140 | + /// Copy the platform audio manager. |
| 141 | + /// |
| 142 | + /// The copy shares the same underlying platform audio handle. |
| 143 | + /// |
| 144 | + /// @param other Manager to copy from. |
| 145 | + /// |
| 146 | + /// @note This operation is not thread-safe. |
| 147 | + PlatformAudio(const PlatformAudio& other) = default; |
| 148 | + |
| 149 | + /// Copy-assign the platform audio manager. |
| 150 | + /// |
| 151 | + /// The assigned instance shares the same underlying platform audio handle. |
| 152 | + /// |
| 153 | + /// @param other Manager to copy from. |
| 154 | + /// @return Reference to this manager. |
| 155 | + /// |
| 156 | + /// @note This operation is not thread-safe. |
| 157 | + PlatformAudio& operator=(const PlatformAudio& other) = default; |
| 158 | + |
| 159 | + /// Move the platform audio manager. |
| 160 | + /// |
| 161 | + /// @param other Manager to move from. |
| 162 | + /// |
| 163 | + /// @note This operation is not thread-safe. |
| 164 | + PlatformAudio(PlatformAudio&& other) noexcept = default; |
| 165 | + |
| 166 | + /// Move-assign the platform audio manager. |
| 167 | + /// |
| 168 | + /// @param other Manager to move from. |
| 169 | + /// @return Reference to this manager. |
| 170 | + /// |
| 171 | + /// @note This operation is not thread-safe. |
| 172 | + PlatformAudio& operator=(PlatformAudio&& other) noexcept = default; |
| 173 | + |
| 174 | + /// Return the current number of recording devices. |
| 175 | + /// |
| 176 | + /// @throws PlatformAudioError If the FFI response is malformed or device |
| 177 | + /// enumeration fails. |
| 178 | + /// |
| 179 | + /// @note This operation is thread-safe. |
| 180 | + std::int32_t recordingDeviceCount() const; |
| 181 | + |
| 182 | + /// Return the current number of playout devices. |
| 183 | + /// |
| 184 | + /// @throws PlatformAudioError If the FFI response is malformed or device |
| 185 | + /// enumeration fails. |
| 186 | + /// |
| 187 | + /// @note This operation is thread-safe. |
| 188 | + std::int32_t playoutDeviceCount() const; |
| 189 | + |
| 190 | + /// Enumerate available microphones. |
| 191 | + /// |
| 192 | + /// @return List of available recording devices. |
| 193 | + /// @throws PlatformAudioError If the FFI response is malformed or device |
| 194 | + /// enumeration fails. |
| 195 | + /// |
| 196 | + /// @note This operation is thread-safe. |
| 197 | + std::vector<AudioDeviceInfo> recordingDevices() const; |
| 198 | + |
| 199 | + /// Enumerate available speakers/headphones. |
| 200 | + /// |
| 201 | + /// @return List of available playout devices. |
| 202 | + /// @throws PlatformAudioError If the FFI response is malformed or device |
| 203 | + /// enumeration fails. |
| 204 | + /// |
| 205 | + /// @note This operation is thread-safe. |
| 206 | + std::vector<AudioDeviceInfo> playoutDevices() const; |
| 207 | + |
| 208 | + /// Select the microphone by device ID. |
| 209 | + /// |
| 210 | + /// @param device_id Stable device identifier from AudioDeviceInfo::id. |
| 211 | + /// @throws PlatformAudioError If the FFI response is malformed or device |
| 212 | + /// selection fails. |
| 213 | + /// |
| 214 | + /// @note This operation is thread-safe. |
| 215 | + void setRecordingDevice(const std::string& device_id) const; |
| 216 | + |
| 217 | + /// Select the speaker/headphones by device ID. |
| 218 | + /// |
| 219 | + /// @param device_id Stable device identifier from AudioDeviceInfo::id. |
| 220 | + /// @throws PlatformAudioError If the FFI response is malformed or device |
| 221 | + /// selection fails. |
| 222 | + /// |
| 223 | + /// @note This operation is thread-safe. |
| 224 | + void setPlayoutDevice(const std::string& device_id) const; |
| 225 | + |
| 226 | + /// Create an automatically captured microphone source for LocalAudioTrack. |
| 227 | + /// |
| 228 | + /// Each call returns a new track source handle backed by the shared platform |
| 229 | + /// Audio Device Module; it does not create a separate ADM instance. |
| 230 | + /// |
| 231 | + /// @param options Audio processing options for the platform microphone path. |
| 232 | + /// @return Platform-backed audio source suitable for LocalAudioTrack. |
| 233 | + /// @throws PlatformAudioError If the FFI response is malformed or source |
| 234 | + /// creation fails. |
| 235 | + /// |
| 236 | + /// @note This operation is thread-safe. |
| 237 | + std::shared_ptr<PlatformAudioSource> createAudioSource(const PlatformAudioOptions& options = {}) const; |
| 238 | + |
| 239 | +private: |
| 240 | + std::shared_ptr<PlatformAudioState> state_; |
| 241 | +}; |
| 242 | + |
| 243 | +} // namespace livekit |
0 commit comments