| 1 | // Copyright 2025 LiveKit, Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | /// Default sample rate used by WebRTC audio pipelines (48kHz). |
| 16 | pub const DEFAULT_SAMPLE_RATE: u32 = 48000; |
| 17 | |
| 18 | /// Default number of audio channels (mono). |
| 19 | pub const DEFAULT_NUM_CHANNELS: u32 = 1; |
| 20 | |
| 21 | #[derive(Default, Debug)] |
| 22 | pub struct AudioSourceOptions { |
| 23 | pub echo_cancellation: bool, |
| 24 | pub noise_suppression: bool, |
| 25 | pub auto_gain_control: bool, |
| 26 | } |
| 27 | |
| 28 | /// Audio source type for creating audio tracks. |
| 29 | /// |
| 30 | /// Choose the appropriate source based on your use case: |
| 31 | /// |
| 32 | /// | Use Case | Source | Description | |
| 33 | /// |----------|--------|-------------| |
| 34 | /// | Manual audio (TTS, files) | `RtcAudioSource::Native(source)` | Push frames manually | |
| 35 | /// | Microphone capture | `RtcAudioSource::Device` | Automatic via Platform ADM | |
| 36 | /// | Both (mic + screen) | Use both types | Multiple tracks supported | |
| 37 | /// |
| 38 | /// # Combining Sources |
| 39 | /// |
| 40 | /// You can have multiple audio tracks with different source types: |
| 41 | /// - Track A: `RtcAudioSource::Device` for microphone (via `PlatformAudio`) |
| 42 | /// - Track B: `RtcAudioSource::Native` for screen capture or TTS |
| 43 | #[non_exhaustive] |
| 44 | #[derive(Debug, Clone)] |
| 45 | pub enum RtcAudioSource { |
| 46 | /// Native audio source for manual audio frame capture. |
| 47 | /// |
| 48 | /// Use this with Synthetic ADM mode (the default). You push audio frames |
| 49 | /// manually via `NativeAudioSource::capture_frame()`. |
| 50 | /// |
| 51 | /// # Example |
| 52 | /// |
| 53 | /// ```rust,ignore |
| 54 | /// use livekit::webrtc::audio_source::native::NativeAudioSource; |
| 55 | /// use livekit::webrtc::audio_source::{AudioSourceOptions, RtcAudioSource}; |
| 56 | /// |
| 57 | /// let source = NativeAudioSource::new( |
| 58 | /// AudioSourceOptions::default(), |
| 59 | /// 48000, 2, 100, |
| 60 | /// ); |
| 61 | /// source.capture_frame(&frame).await?; |
| 62 | /// |
| 63 | /// let track = LocalAudioTrack::create_audio_track( |
| 64 | /// "audio", |
| 65 | /// RtcAudioSource::Native(source), |
| 66 | /// ); |
| 67 | /// ``` |
| 68 | #[cfg(not(target_arch = "wasm32"))] |
| 69 | Native(native::NativeAudioSource), |
| 70 | |
| 71 | /// Device audio source - uses Platform ADM for automatic microphone capture. |
| 72 | /// |
| 73 | /// WebRTC automatically captures audio from the selected recording device |
| 74 | /// (microphone). You do NOT push frames manually. |
| 75 | /// |
| 76 | /// # Usage |
| 77 | /// |
| 78 | /// Use `PlatformAudio` from the `livekit` crate, which manages the Platform ADM |
| 79 | /// lifecycle and provides `RtcAudioSource::Device` via `rtc_source()`: |
| 80 | /// |
| 81 | /// ```rust,ignore |
| 82 | /// use livekit::prelude::*; |
| 83 | /// |
| 84 | /// // Create PlatformAudio (enables Platform ADM) |
| 85 | /// let audio = PlatformAudio::new()?; |
| 86 | /// |
| 87 | /// // Optionally select a specific device |
| 88 | /// if let Some(device) = audio.recording_devices().next() { |
| 89 | /// audio.set_recording_device(&device.id)?; |
| 90 | /// } |
| 91 | /// |
| 92 | /// // Create track using the device source |
| 93 | /// let track = LocalAudioTrack::create_audio_track("mic", audio.rtc_source()); |
| 94 | /// ``` |
| 95 | /// |
| 96 | /// # Combining with NativeAudioSource |
| 97 | /// |
| 98 | /// You CAN use `NativeAudioSource` alongside Platform ADM to have multiple |
| 99 | /// audio tracks with different sources (e.g., microphone + screen capture). |
| 100 | /// |
| 101 | /// # Platform Support |
| 102 | /// |
| 103 | /// - **iOS**: CoreAudio with VPIO (Voice Processing IO) |
| 104 | /// - **macOS**: CoreAudio |
| 105 | /// - **Windows**: WASAPI |
| 106 | /// - **Linux**: PulseAudio / ALSA |
| 107 | /// - **Android**: AAudio / OpenSL ES |
| 108 | #[cfg(not(target_arch = "wasm32"))] |
| 109 | Device, |
| 110 | } |
| 111 | |
| 112 | impl RtcAudioSource { |
| 113 | /// Set audio processing options. |
| 114 | /// Note: For `Device` source, options are controlled by the Platform ADM. |
| 115 | pub fn set_audio_options(&self, options: AudioSourceOptions) { |
| 116 | match self { |
| 117 | #[cfg(not(target_arch = "wasm32"))] |
| 118 | RtcAudioSource::Native(source) => source.set_audio_options(options), |
| 119 | #[cfg(not(target_arch = "wasm32"))] |
| 120 | RtcAudioSource::Device => { |
| 121 | // Device source options are managed by the Platform ADM |
| 122 | // This is a no-op |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /// Get audio processing options. |
| 128 | /// Note: For `Device` source, returns default options (actual options are managed by ADM). |
| 129 | pub fn audio_options(&self) -> AudioSourceOptions { |
| 130 | match self { |
| 131 | #[cfg(not(target_arch = "wasm32"))] |
| 132 | RtcAudioSource::Native(source) => source.audio_options(), |
| 133 | #[cfg(not(target_arch = "wasm32"))] |
| 134 | RtcAudioSource::Device => AudioSourceOptions::default(), |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /// Get the sample rate. |
| 139 | /// Note: For `Device` source, returns [`DEFAULT_SAMPLE_RATE`] (48kHz). |
| 140 | pub fn sample_rate(&self) -> u32 { |
| 141 | match self { |
| 142 | #[cfg(not(target_arch = "wasm32"))] |
| 143 | RtcAudioSource::Native(source) => source.sample_rate(), |
| 144 | #[cfg(not(target_arch = "wasm32"))] |
| 145 | RtcAudioSource::Device => DEFAULT_SAMPLE_RATE, |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /// Get the number of channels. |
| 150 | /// Note: For `Device` source, returns [`DEFAULT_NUM_CHANNELS`] (mono). |
| 151 | pub fn num_channels(&self) -> u32 { |
| 152 | match self { |
| 153 | #[cfg(not(target_arch = "wasm32"))] |
| 154 | RtcAudioSource::Native(source) => source.num_channels(), |
| 155 | #[cfg(not(target_arch = "wasm32"))] |
| 156 | RtcAudioSource::Device => DEFAULT_NUM_CHANNELS, |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | #[cfg(not(target_arch = "wasm32"))] |
| 162 | pub mod native { |
| 163 | use std::fmt::{Debug, Formatter}; |
| 164 | |
| 165 | use super::*; |
| 166 | use crate::imp::audio_source::NativeAudioSource as ImpAudioSource; |
| 167 | use crate::{audio_frame::AudioFrame, RtcError}; |
| 168 | |
| 169 | #[derive(Clone)] |
| 170 | pub struct NativeAudioSource { |
| 171 | pub(crate) handle: ImpAudioSource, |
| 172 | } |
| 173 | |
| 174 | impl Debug for NativeAudioSource { |
| 175 | fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { |
| 176 | f.debug_struct("NativeAudioSource").finish() |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | impl NativeAudioSource { |
| 181 | pub fn new( |
| 182 | options: AudioSourceOptions, |
| 183 | sample_rate: u32, |
| 184 | num_channels: u32, |
| 185 | queue_size_ms: u32, |
| 186 | ) -> NativeAudioSource { |
| 187 | Self { handle: ImpAudioSource::new(options, sample_rate, num_channels, queue_size_ms) } |
| 188 | } |
| 189 | |
| 190 | pub fn clear_buffer(&self) { |
| 191 | self.handle.clear_buffer() |
| 192 | } |
| 193 | |
| 194 | pub async fn capture_frame(&self, frame: &AudioFrame<'_>) -> Result<(), RtcError> { |
| 195 | self.handle.capture_frame(frame).await |
| 196 | } |
| 197 | |
| 198 | pub fn set_audio_options(&self, options: AudioSourceOptions) { |
| 199 | self.handle.set_audio_options(options) |
| 200 | } |
| 201 | |
| 202 | pub fn audio_options(&self) -> AudioSourceOptions { |
| 203 | self.handle.audio_options() |
| 204 | } |
| 205 | |
| 206 | pub fn sample_rate(&self) -> u32 { |
| 207 | self.handle.sample_rate() |
| 208 | } |
| 209 | |
| 210 | pub fn num_channels(&self) -> u32 { |
| 211 | self.handle.num_channels() |
| 212 | } |
| 213 | } |
| 214 | } |