rvoip_media_core/lib.rs
1//! # Media Core library for the RVOIP project
2//!
3//! `media-core` provides comprehensive media processing capabilities for SIP servers.
4//! It focuses exclusively on media processing, codec management, and media session
5//! coordination while integrating cleanly with `session-core` and `rtp-core`.
6//!
7//! ## Core Components
8//!
9//! - **MediaEngine**: Central orchestrator for all media processing
10//! - **MediaSession**: Per-dialog media management
11//! - **Codec Framework**: Audio codec support (G.711, Opus, etc.)
12//! - **Audio Processing**: AEC, AGC, VAD, noise suppression
13//! - **Quality Monitoring**: Real-time quality metrics and adaptation
14//!
15//! ## Audio Muting
16//!
17//! The media-core library implements silence-based muting that maintains continuous
18//! RTP packet flow. When a session is muted, audio samples are replaced with silence
19//! before encoding, preserving:
20//!
21//! - RTP sequence numbers and timestamps
22//! - NAT traversal and binding keepalive
23//! - Compatibility with all SIP endpoints
24//! - Instant mute/unmute without renegotiation
25//!
26//! Use `MediaSessionController::set_audio_muted()` for production-ready muting.
27//!
28//! ## Quick Start
29//!
30//! ```rust,ignore
31//! use rvoip_media_core::relay::controller::MediaSessionController;
32//! use rvoip_media_core::types::{MediaSessionId, DialogId};
33//!
34//! #[tokio::main]
35//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
36//! // Create media session controller
37//! let controller = MediaSessionController::new();
38//!
39//! // RTP bridge callbacks can be added for external RTP event handling
40//! // This enables integration with session-core for audio frame delivery
41//!
42//! Ok(())
43//! }
44//! ```
45
46// Core modules
47pub mod error;
48pub mod types;
49pub mod engine;
50pub mod session; // New session module
51pub mod processing; // New processing pipeline module
52pub mod quality; // New quality monitoring module
53pub mod integration; // New integration module
54pub mod buffer; // New buffer module
55pub mod performance; // New performance optimization module
56
57// Working modules from old implementation (to be refactored)
58pub mod codec;
59pub mod relay;
60
61// Audio utilities
62pub mod audio;
63
64// Re-export core types
65pub use error::{Error, Result};
66pub use types::*;
67
68// Re-export RTP statistics types from rtp-core
69pub use rvoip_rtp_core::session::{RtpSessionStats, RtpStreamStats};
70
71// Re-export engine components
72pub use engine::{
73 MediaEngine,
74 MediaEngineConfig,
75 EngineCapabilities,
76 MediaSessionParams,
77 MediaSessionHandle,
78 EngineState,
79};
80
81// NEW: Enhanced configuration exports from media_engine
82pub use engine::media_engine::{
83 PerformanceLevel,
84 AdvancedProcessorFactory,
85};
86
87// Re-export session components
88pub use session::{
89 MediaSession,
90 MediaSessionConfig,
91 MediaSessionState,
92 MediaSessionEvent as SessionEvent, // Rename to avoid conflict
93 MediaSessionEventType,
94};
95
96// Re-export integration components
97pub use integration::{
98 RtpBridge,
99 RtpBridgeConfig,
100 SessionBridge,
101 SessionBridgeConfig,
102 IntegrationEvent,
103 IntegrationEventType,
104 RtpEvent,
105 RtpEventCallback,
106};
107
108// Legacy exports (will be replaced in Phase 2)
109pub use codec::{Codec, CodecRegistry};
110pub use relay::{
111 MediaSessionController,
112 MediaConfig,
113 MediaSessionStatus,
114 MediaSessionInfo,
115 G711PcmuCodec,
116 G711PcmaCodec,
117};
118
119// NEW: Enhanced configuration re-exports
120pub use engine::config::{
121 PerformanceConfig,
122 AdvancedProcessingConfig,
123 AudioConfig,
124 CodecConfig,
125 QualityConfig,
126 BufferConfig,
127};
128
129/// Media sample type (raw audio data)
130pub type Sample = i16;
131
132/// Audio format (channels, bit depth, sample rate)
133#[derive(Debug, Clone, Copy, PartialEq, Eq)]
134pub struct AudioFormat {
135 /// Number of channels (1 for mono, 2 for stereo)
136 pub channels: u8,
137 /// Bits per sample (typically 8, 16, or 32)
138 pub bit_depth: u8,
139 /// Sample rate in Hz
140 pub sample_rate: crate::types::SampleRate,
141}
142
143impl Default for AudioFormat {
144 fn default() -> Self {
145 Self {
146 channels: 1, // Default to mono
147 bit_depth: 16, // Default to 16-bit
148 sample_rate: crate::types::SampleRate::default(),
149 }
150 }
151}
152
153impl AudioFormat {
154 /// Create a new audio format
155 pub fn new(channels: u8, bit_depth: u8, sample_rate: crate::types::SampleRate) -> Self {
156 Self {
157 channels,
158 bit_depth,
159 sample_rate,
160 }
161 }
162
163 /// Create a new mono 16-bit format with the given sample rate
164 pub fn mono_16bit(sample_rate: crate::types::SampleRate) -> Self {
165 Self::new(1, 16, sample_rate)
166 }
167
168 /// Create a new stereo 16-bit format with the given sample rate
169 pub fn stereo_16bit(sample_rate: crate::types::SampleRate) -> Self {
170 Self::new(2, 16, sample_rate)
171 }
172
173 /// Standard narrowband telephony format (mono, 16-bit, 8kHz)
174 pub fn telephony() -> Self {
175 Self::mono_16bit(crate::types::SampleRate::Rate8000)
176 }
177}
178
179/// A chunk of audio samples (PCM or encoded)
180#[derive(Debug, Clone)]
181pub struct AudioBuffer {
182 /// Raw audio data
183 pub data: bytes::Bytes,
184 /// Audio format information
185 pub format: AudioFormat,
186}
187
188impl AudioBuffer {
189 /// Create a new audio buffer with the given data and format
190 pub fn new(data: bytes::Bytes, format: AudioFormat) -> Self {
191 Self { data, format }
192 }
193
194 /// Get the duration of the audio in milliseconds
195 pub fn duration_ms(&self) -> u32 {
196 let bytes_per_sample = (self.format.bit_depth / 8) as u32;
197 let samples = (self.data.len() as u32) / bytes_per_sample / (self.format.channels as u32);
198 (samples * 1000) / self.format.sample_rate.as_hz()
199 }
200
201 /// Get the number of samples in the buffer
202 pub fn samples(&self) -> usize {
203 let bytes_per_sample = (self.format.bit_depth / 8) as usize;
204 self.data.len() / bytes_per_sample / (self.format.channels as usize)
205 }
206}
207
208/// Prelude module with commonly used types
209pub mod prelude {
210 // Re-export basic types for convenience
211 pub use crate::types::{
212 // Basic types
213 DialogId,
214 MediaSessionId,
215 PayloadType,
216 MediaType,
217 MediaDirection,
218 SampleRate,
219 AudioFrame,
220 VideoFrame,
221 MediaPacket,
222 // Payload types
223 payload_types::{static_types, dynamic_range},
224 // Statistics types
225 MediaStatistics,
226 MediaProcessingStats,
227 QualityMetrics,
228 };
229
230 // Re-export from codec module
231 pub use crate::codec::{
232 mapping::CodecMapper,
233 audio::AudioCodec,
234 };
235
236 // Re-export from engine module
237 pub use crate::engine::{
238 media_engine::MediaEngine,
239 config::{
240 MediaEngineConfig,
241 AudioConfig,
242 CodecConfig,
243 QualityConfig,
244 BufferConfig,
245 PerformanceConfig,
246 AdvancedProcessingConfig,
247 EngineCapabilities,
248 AudioCodecCapability,
249 AudioProcessingCapabilities,
250 },
251 };
252
253 // Re-export from RTP core
254 pub use rvoip_rtp_core::{
255 RtpHeader,
256 RtpPacket,
257 RtpSession,
258 RtpSessionConfig,
259 };
260
261 // Re-export from error module
262 pub use crate::error::{Error, Result};
263}