rvoip_audio_core/lib.rs
1//! RVOIP Audio Core Library
2//!
3//! Comprehensive audio handling for VoIP applications, providing device management,
4//! format conversion, codec processing, and RTP audio stream integration.
5//!
6//! # Architecture
7//!
8//! The audio-core library is organized into several key modules:
9//!
10//! - **Device Management**: Cross-platform audio device access and control
11//! - **Format Bridge**: Audio format conversion and resampling
12//! - **Pipeline**: High-level audio streaming pipelines
13//! - **RTP Integration**: RTP payload encoding/decoding
14//! - **Processing**: Audio signal processing (AEC, AGC, etc.)
15//!
16//! # Quick Start
17//!
18//! ## Device Enumeration
19//!
20//! ```rust,no_run
21//! use rvoip_audio_core::device::AudioDeviceManager;
22//! use rvoip_audio_core::types::AudioDirection;
23//!
24//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
25//! let device_manager = AudioDeviceManager::new().await?;
26//!
27//! // List available devices
28//! let input_devices = device_manager.list_devices(AudioDirection::Input).await?;
29//! let output_devices = device_manager.list_devices(AudioDirection::Output).await?;
30//!
31//! println!("Found {} input devices", input_devices.len());
32//! println!("Found {} output devices", output_devices.len());
33//! # Ok(())
34//! # }
35//! ```
36//!
37//! ## Audio Pipeline
38//!
39//! ```rust,no_run
40//! use rvoip_audio_core::pipeline::AudioPipeline;
41//! use rvoip_audio_core::types::{AudioFormat, AudioDirection};
42//!
43//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
44//! // Create a basic audio pipeline
45//! let mut pipeline = AudioPipeline::builder()
46//! .input_format(AudioFormat::pcm_8khz_mono())
47//! .output_format(AudioFormat::pcm_48khz_stereo())
48//! .build()
49//! .await?;
50//!
51//! // Start the pipeline
52//! pipeline.start().await?;
53//! # Ok(())
54//! # }
55//! ```
56//!
57//! # Features
58//!
59//! This crate supports multiple optional features:
60//!
61//! - `device-cpal`: CPAL-based audio device support (default)
62//! - `format-conversion`: Audio format conversion and resampling
63//! - `processing-*`: Audio signal processing features
64//!
65//! Enable features in your `Cargo.toml`:
66//!
67//! ```toml
68//! [dependencies]
69//! rvoip-audio-core = { version = "0.1", features = ["full"] }
70//! ```
71
72#![warn(missing_docs)]
73#![doc(html_root_url = "https://docs.rs/rvoip-audio-core/0.1.0")]
74
75// Core modules
76pub mod error;
77pub mod types;
78
79// Device management
80pub mod device;
81
82// Format conversion and processing
83#[cfg(feature = "format-conversion")]
84pub mod format;
85
86
87// Audio pipeline
88pub mod pipeline;
89
90// RTP integration
91pub mod rtp;
92
93// Audio signal processing
94#[cfg(any(
95 feature = "processing-aec",
96 feature = "processing-agc",
97 feature = "processing-noise",
98 feature = "processing-vad"
99))]
100pub mod processing;
101
102// Re-export commonly used types
103pub use error::{AudioError, AudioResult};
104pub use types::{
105 AudioFormat, AudioFrame, AudioDirection,
106 AudioDeviceInfo, AudioStreamConfig, AudioQualityMetrics
107};
108
109// Re-export device management
110pub use device::{AudioDeviceManager, AudioDevice};
111
112// Re-export pipeline
113pub use pipeline::AudioPipeline;
114
115
116// Re-export integration types from session-core and rtp-core
117pub use rvoip_session_core::api::types::SessionId;
118pub use rvoip_session_core::api::MediaControl;
119pub use rvoip_rtp_core::packet::RtpPacket;
120
121/// Audio-core library version
122pub const VERSION: &str = env!("CARGO_PKG_VERSION");
123
124/// Default audio configuration constants
125pub mod defaults {
126 use crate::types::AudioFormat;
127
128 /// Default sample rate for VoIP (8kHz narrowband)
129 pub const SAMPLE_RATE_NARROWBAND: u32 = 8000;
130
131 /// Default sample rate for wideband VoIP (16kHz)
132 pub const SAMPLE_RATE_WIDEBAND: u32 = 16000;
133
134 /// Default sample rate for high-quality audio (48kHz)
135 pub const SAMPLE_RATE_HIFI: u32 = 48000;
136
137 /// Default frame size in milliseconds
138 pub const FRAME_SIZE_MS: u32 = 20;
139
140 /// Default number of channels (mono)
141 pub const CHANNELS: u16 = 1;
142
143 /// Default bit depth
144 pub const BIT_DEPTH: u16 = 16;
145
146 /// Default VoIP audio format (8kHz, mono, 16-bit, 20ms frames)
147 pub fn voip_format() -> AudioFormat {
148 AudioFormat::new(
149 SAMPLE_RATE_NARROWBAND,
150 CHANNELS,
151 BIT_DEPTH,
152 FRAME_SIZE_MS,
153 )
154 }
155
156 /// Default wideband VoIP audio format (16kHz, mono, 16-bit, 20ms frames)
157 pub fn wideband_format() -> AudioFormat {
158 AudioFormat::new(
159 SAMPLE_RATE_WIDEBAND,
160 CHANNELS,
161 BIT_DEPTH,
162 FRAME_SIZE_MS,
163 )
164 }
165
166 /// Default high-quality audio format (48kHz, stereo, 16-bit, 20ms frames)
167 pub fn hifi_format() -> AudioFormat {
168 AudioFormat::new(
169 SAMPLE_RATE_HIFI,
170 2, // stereo
171 BIT_DEPTH,
172 FRAME_SIZE_MS,
173 )
174 }
175}
176
177#[cfg(test)]
178mod tests {
179 use super::*;
180
181 #[test]
182 fn test_version_defined() {
183 assert!(!VERSION.is_empty());
184 }
185
186 #[test]
187 fn test_default_formats() {
188 let voip = defaults::voip_format();
189 assert_eq!(voip.sample_rate, 8000);
190 assert_eq!(voip.channels, 1);
191
192 let wideband = defaults::wideband_format();
193 assert_eq!(wideband.sample_rate, 16000);
194 assert_eq!(wideband.channels, 1);
195
196 let hifi = defaults::hifi_format();
197 assert_eq!(hifi.sample_rate, 48000);
198 assert_eq!(hifi.channels, 2);
199 }
200}