1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//! Main streaming implementation
use super::priority_queue::MediaPriorityQueue;
use super::types::*;
use crate::stream_performance_metrics::StreamMetrics;
use crate::{auto_backpressure_block, tick, unfold};
use crate::{auto_backpressure_drop_newest, from_iter, throttle, RS2Stream, RS2StreamExt};
use futures_core::Stream;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::fs::File;
use tokio::io::AsyncReadExt;
pub struct MediaStreamingService {
chunk_queue: Arc<MediaPriorityQueue>,
metrics: Arc<tokio::sync::Mutex<StreamMetrics>>,
}
impl MediaStreamingService {
pub fn new(buffer_capacity: usize) -> Self {
Self {
chunk_queue: Arc::new(MediaPriorityQueue::new(buffer_capacity, 64)),
metrics: Arc::new(tokio::sync::Mutex::new(
StreamMetrics::new().with_name("media-stream".to_string()),
)),
}
}
/// Start streaming from a file
pub async fn start_file_stream(
&self,
file_path: PathBuf,
stream_config: MediaStream,
) -> RS2Stream<MediaChunk> {
let file = self.acquire_file_resource(file_path).await;
let chunk_queue = Arc::clone(&self.chunk_queue);
let metrics = Arc::clone(&self.metrics);
self.create_chunk_stream(file, stream_config, chunk_queue, metrics)
}
/// Start streaming from live input (camera, microphone, etc.)
pub async fn start_live_stream(&self, stream_config: MediaStream) -> RS2Stream<MediaChunk> {
let chunk_queue = Arc::clone(&self.chunk_queue);
let metrics = Arc::clone(&self.metrics);
// Create live stream using from_iter with throttling
auto_backpressure_drop_newest(
throttle(
from_iter(0u64..)
.take_rs2(
stream_config
.metadata
.get("max_chunks")
.and_then(|s| s.parse().ok())
.unwrap_or(u64::MAX as usize),
)
.par_eval_map_rs2(4, move |sequence| {
let queue = Arc::clone(&chunk_queue);
let metrics = Arc::clone(&metrics);
let config = stream_config.clone();
async move {
// Simulate live capture - create chunk directly here
tokio::time::sleep(std::time::Duration::from_micros(100)).await;
let chunk = MediaChunk {
stream_id: config.id.clone(),
sequence_number: sequence,
data: vec![0u8; config.chunk_size],
chunk_type: if sequence % 30 == 0 {
ChunkType::VideoIFrame
} else if sequence % 3 == 0 {
ChunkType::VideoBFrame
} else {
ChunkType::VideoPFrame
},
priority: if sequence % 30 == 0 {
MediaPriority::High
} else if sequence % 3 == 0 {
MediaPriority::Low
} else {
MediaPriority::Normal
},
timestamp: std::time::Duration::from_millis(sequence * 33),
is_final: false,
checksum: None,
};
// Update metrics
{
let mut m = metrics.lock().await;
m.items_processed += 1;
m.bytes_processed += chunk.data.len() as u64;
m.average_item_size =
m.bytes_processed as f64 / m.items_processed as f64;
m.last_activity = Some(std::time::Instant::now());
}
// Try to enqueue (don't block for live streaming)
if let Err(_) = queue.try_enqueue(chunk.clone()).await {
let mut m = metrics.lock().await;
m.errors += 1;
}
chunk
}
}),
std::time::Duration::from_millis(33), // ~30fps
),
512,
)
}
async fn acquire_file_resource(&self, path: PathBuf) -> File {
File::open(&path)
.await
.unwrap_or_else(|e| panic!("Failed to open media file {:?}: {}", path, e))
}
fn create_chunk_stream(
&self,
file: File,
config: MediaStream,
queue: Arc<MediaPriorityQueue>,
metrics: Arc<tokio::sync::Mutex<StreamMetrics>>,
) -> RS2Stream<MediaChunk> {
// Use unfold to read file sequentially
auto_backpressure_block(
unfold((file, 0u64), move |state| {
let queue = Arc::clone(&queue);
let metrics = Arc::clone(&metrics);
let config = config.clone();
async move {
let (mut file, sequence) = state;
// Read chunk from file
let mut buffer = vec![0u8; config.chunk_size];
match file.read(&mut buffer).await {
Ok(0) => {
// EOF reached
None
}
Ok(bytes_read) => {
// Truncate buffer to actual bytes read
buffer.truncate(bytes_read);
// Determine chunk type inline
let chunk_type = if sequence % 30 == 0 {
ChunkType::VideoIFrame
} else if sequence % 3 == 0 {
ChunkType::VideoBFrame
} else {
ChunkType::VideoPFrame
};
// Determine priority inline
let priority = if sequence % 30 == 0 {
MediaPriority::High
} else if sequence % 3 == 0 {
MediaPriority::Low
} else {
MediaPriority::Normal
};
let chunk = MediaChunk {
stream_id: config.id.clone(),
sequence_number: sequence,
data: buffer,
chunk_type,
priority,
timestamp: std::time::Duration::from_millis(sequence * 33),
is_final: bytes_read < config.chunk_size,
checksum: None,
};
// Update metrics inline
{
let mut m = metrics.lock().await;
m.items_processed += 1;
m.bytes_processed += chunk.data.len() as u64;
m.average_item_size =
m.bytes_processed as f64 / m.items_processed as f64;
m.last_activity = Some(std::time::Instant::now());
}
// Enqueue with priority
if let Err(_) = queue.enqueue(chunk.clone()).await {
let mut m = metrics.lock().await;
m.errors += 1;
}
Some((chunk, (file, sequence + 1)))
}
Err(e) => {
log::error!("Error reading file: {}", e);
None
}
}
}
}),
256,
)
}
/// Create a chunk for live streaming
async fn create_live_chunk(&self, config: &MediaStream, sequence: u64) -> MediaChunk {
// Simulate capturing from live source
tokio::time::sleep(std::time::Duration::from_micros(100)).await;
MediaChunk {
stream_id: config.id.clone(),
sequence_number: sequence,
data: vec![0u8; config.chunk_size], // Mock data - replace with actual capture
chunk_type: self.determine_chunk_type(sequence),
priority: self.determine_priority(sequence),
timestamp: std::time::Duration::from_millis(sequence * 33),
is_final: false, // Live streams don't end
checksum: None,
}
}
/// Determine chunk type based on sequence
pub fn determine_chunk_type(&self, sequence: u64) -> ChunkType {
if sequence % 30 == 0 {
ChunkType::VideoIFrame // Keyframe every 30 frames
} else if sequence % 3 == 0 {
ChunkType::VideoBFrame // B-frame every 3rd frame
} else {
ChunkType::VideoPFrame // P-frame otherwise
}
}
/// Determine priority based on sequence and chunk type
pub fn determine_priority(&self, sequence: u64) -> MediaPriority {
if sequence % 30 == 0 {
MediaPriority::High // I-frames are high priority
} else if sequence % 3 == 0 {
MediaPriority::Low // B-frames are low priority
} else {
MediaPriority::Normal // P-frames are normal priority
}
}
/// Update metrics efficiently
async fn update_metrics(
&self,
metrics: &Arc<tokio::sync::Mutex<StreamMetrics>>,
chunk: &MediaChunk,
) {
let mut m = metrics.lock().await;
m.items_processed += 1;
m.bytes_processed += chunk.data.len() as u64;
m.average_item_size = m.bytes_processed as f64 / m.items_processed as f64;
m.last_activity = Some(std::time::Instant::now());
}
/// Get stream from priority queue
pub fn get_chunk_stream(&self) -> impl Stream<Item = MediaChunk> + Send + 'static {
self.chunk_queue.dequeue()
}
/// Get current metrics with updated buffer utilization
pub async fn get_metrics(&self) -> StreamMetrics {
let metrics = self.metrics.lock().await;
metrics.clone()
}
/// Create a metrics monitoring stream
pub fn get_metrics_stream(&self) -> RS2Stream<StreamMetrics> {
let metrics = Arc::clone(&self.metrics);
tick(std::time::Duration::from_secs(1), ()).par_eval_map_rs2(1, move |_| {
let metrics = Arc::clone(&metrics);
async move {
let m = metrics.lock().await;
m.clone()
}
})
}
/// Gracefully shutdown the streaming service
pub async fn shutdown(&self) {
log::info!("Shutting down media streaming service");
self.chunk_queue.close().await;
}
}
/// Factory for creating different types of streaming services
pub struct StreamingServiceFactory;
impl StreamingServiceFactory {
/// Create service optimized for live streaming
pub fn create_live_streaming_service() -> MediaStreamingService {
MediaStreamingService::new(2048) // Larger buffer for live
}
/// Create service optimized for file streaming
pub fn create_file_streaming_service() -> MediaStreamingService {
MediaStreamingService::new(512) // Smaller buffer for files
}
/// Create service optimized for low-latency streaming
pub fn create_low_latency_service() -> MediaStreamingService {
MediaStreamingService::new(128) // Very small buffer for low latency
}
}