rvoip_rtp_core/api/common/buffer/
mod.rs1use std::sync::Arc;
6use std::time::Duration;
7use async_trait::async_trait;
8
9use crate::api::common::frame::MediaFrame;
10use crate::api::common::error::BufferError;
11
12mod media_buffer_impl;
13pub use media_buffer_impl::DefaultMediaBuffer;
14
15#[derive(Debug, Clone)]
17pub struct MediaBufferConfig {
18 pub min_delay_ms: u32,
20
21 pub max_delay_ms: u32,
23
24 pub adaptive: bool,
26
27 pub target_occupancy: u8,
29
30 pub max_packet_count: usize,
32
33 pub transmit_max_latency_ms: u32,
35
36 pub prioritize_keyframes: bool,
38}
39
40impl Default for MediaBufferConfig {
41 fn default() -> Self {
42 Self {
43 min_delay_ms: 20,
44 max_delay_ms: 120,
45 adaptive: true,
46 target_occupancy: 50,
47 max_packet_count: 1000,
48 transmit_max_latency_ms: 100,
49 prioritize_keyframes: true,
50 }
51 }
52}
53
54#[derive(Debug, Clone)]
56pub struct BufferStats {
57 pub current_delay_ms: u32,
59
60 pub packet_count: usize,
62
63 pub max_delay_seen_ms: u32,
65
66 pub min_delay_seen_ms: u32,
68
69 pub late_packet_count: u64,
71
72 pub overflow_discard_count: u64,
74
75 pub average_occupancy: f32,
77
78 pub underrun_count: u64,
80}
81
82#[async_trait]
84pub trait MediaBuffer: Send + Sync {
85 async fn put_frame(&self, frame: MediaFrame) -> Result<(), BufferError>;
87
88 async fn get_frame(&self, timeout: Duration) -> Result<MediaFrame, BufferError>;
90
91 async fn get_stats(&self) -> BufferStats;
93
94 async fn reset(&self) -> Result<(), BufferError>;
96
97 async fn flush(&self) -> Result<Vec<MediaFrame>, BufferError>;
99
100 async fn update_config(&self, config: MediaBufferConfig) -> Result<(), BufferError>;
102}
103
104pub struct MediaBufferConfigBuilder {
106 config: MediaBufferConfig,
107}
108
109impl MediaBufferConfigBuilder {
110 pub fn new() -> Self {
112 Self {
113 config: MediaBufferConfig::default(),
114 }
115 }
116
117 pub fn preset(mut self, preset: crate::api::common::config::NetworkPreset) -> Self {
119 use crate::api::common::config::NetworkPreset;
120 match preset {
121 NetworkPreset::LowLatency => {
122 self.config.min_delay_ms = 10;
123 self.config.max_delay_ms = 50;
124 self.config.adaptive = false;
125 self.config.target_occupancy = 30;
126 },
127 NetworkPreset::Balanced => {
128 },
130 NetworkPreset::Resilient => {
131 self.config.min_delay_ms = 50;
132 self.config.max_delay_ms = 200;
133 self.config.adaptive = true;
134 self.config.target_occupancy = 70;
135 },
136 NetworkPreset::HighProtection => {
137 self.config.min_delay_ms = 100;
138 self.config.max_delay_ms = 500;
139 self.config.adaptive = true;
140 self.config.target_occupancy = 80;
141 },
142 }
143 self
144 }
145
146 pub fn audio(mut self) -> Self {
148 self.config.min_delay_ms = 20;
150 self.config.max_delay_ms = 120;
151 self.config.target_occupancy = 50;
152 self
153 }
154
155 pub fn video(mut self) -> Self {
157 self.config.min_delay_ms = 50;
159 self.config.max_delay_ms = 300;
160 self.config.target_occupancy = 60;
161 self.config.max_packet_count = 5000;
162 self.config.prioritize_keyframes = true;
163 self
164 }
165
166 pub fn min_delay_ms(mut self, delay: u32) -> Self {
168 self.config.min_delay_ms = delay;
169 self
170 }
171
172 pub fn max_delay_ms(mut self, delay: u32) -> Self {
174 self.config.max_delay_ms = delay;
175 self
176 }
177
178 pub fn adaptive(mut self, adaptive: bool) -> Self {
180 self.config.adaptive = adaptive;
181 self
182 }
183
184 pub fn target_occupancy(mut self, occupancy: u8) -> Self {
186 self.config.target_occupancy = occupancy.min(100);
187 self
188 }
189
190 pub fn max_packet_count(mut self, count: usize) -> Self {
192 self.config.max_packet_count = count;
193 self
194 }
195
196 pub fn transmit_max_latency_ms(mut self, latency: u32) -> Self {
198 self.config.transmit_max_latency_ms = latency;
199 self
200 }
201
202 pub fn prioritize_keyframes(mut self, prioritize: bool) -> Self {
204 self.config.prioritize_keyframes = prioritize;
205 self
206 }
207
208 pub fn build(self) -> Result<MediaBufferConfig, BufferError> {
210 if self.config.min_delay_ms > self.config.max_delay_ms {
212 return Err(BufferError::ConfigurationError(
213 "Minimum delay must be less than or equal to maximum delay".to_string()
214 ));
215 }
216
217 if self.config.max_packet_count == 0 {
218 return Err(BufferError::ConfigurationError(
219 "Maximum packet count must be greater than zero".to_string()
220 ));
221 }
222
223 Ok(self.config)
224 }
225}
226
227pub struct MediaBufferFactory;
229
230impl MediaBufferFactory {
231 pub fn create_buffer(
233 config: MediaBufferConfig,
234 ) -> Result<Arc<dyn MediaBuffer>, BufferError> {
235 let buffer = media_buffer_impl::DefaultMediaBuffer::new(config)?;
236 Ok(buffer as Arc<dyn MediaBuffer>)
237 }
238}