oximedia-net 0.1.2

Network streaming for OxiMedia
Documentation
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Segment generation for live streaming.
//!
//! This module handles the generation of media segments for HLS and DASH,
//! including segmentation, packaging, and caching.

use super::{MediaPacket, MediaType};
use crate::error::NetResult;
use bytes::{Bytes, BytesMut};
use parking_lot::RwLock;
use std::collections::VecDeque;
use std::sync::Arc;
use std::time::Duration;
use uuid::Uuid;

/// Segment configuration.
#[derive(Debug, Clone)]
pub struct SegmentConfig {
    /// Target segment duration.
    pub duration: Duration,

    /// Keyframe interval (for forcing segmentation).
    pub keyframe_interval: u64,
}

impl Default for SegmentConfig {
    fn default() -> Self {
        Self {
            duration: Duration::from_secs(2),
            keyframe_interval: 60,
        }
    }
}

/// Media segment.
#[derive(Debug, Clone)]
pub struct MediaSegment {
    /// Segment ID.
    pub id: Uuid,

    /// Segment sequence number.
    pub sequence: u64,

    /// Segment start timestamp (milliseconds).
    pub start_timestamp: u64,

    /// Segment duration (milliseconds).
    pub duration: u64,

    /// Segment data.
    pub data: Bytes,

    /// Is initialization segment.
    pub is_init: bool,

    /// Contains keyframe.
    pub has_keyframe: bool,

    /// Variant ID.
    pub variant_id: Option<String>,

    /// Media type.
    pub media_type: MediaType,

    /// Byte range offset (for packed segments).
    pub byte_offset: Option<u64>,

    /// Byte range length.
    pub byte_length: Option<u64>,
}

impl MediaSegment {
    /// Creates a new media segment.
    #[must_use]
    pub fn new(
        sequence: u64,
        start_timestamp: u64,
        duration: u64,
        data: Bytes,
        media_type: MediaType,
    ) -> Self {
        Self {
            id: Uuid::new_v4(),
            sequence,
            start_timestamp,
            duration,
            data,
            is_init: false,
            has_keyframe: false,
            variant_id: None,
            media_type,
            byte_offset: None,
            byte_length: None,
        }
    }

    /// Creates an initialization segment.
    #[must_use]
    pub fn init_segment(data: Bytes, media_type: MediaType) -> Self {
        Self {
            id: Uuid::new_v4(),
            sequence: 0,
            start_timestamp: 0,
            duration: 0,
            data,
            is_init: true,
            has_keyframe: false,
            variant_id: None,
            media_type,
            byte_offset: None,
            byte_length: None,
        }
    }

    /// Returns the segment filename for HLS.
    #[must_use]
    pub fn hls_filename(&self) -> String {
        if self.is_init {
            format!("init_{}.mp4", self.id)
        } else {
            format!("seg_{}_{}.m4s", self.sequence, self.id)
        }
    }

    /// Returns the segment filename for DASH.
    #[must_use]
    pub fn dash_filename(&self) -> String {
        let media_str = match self.media_type {
            MediaType::Video => "video",
            MediaType::Audio => "audio",
            MediaType::Metadata => "metadata",
        };

        if self.is_init {
            format!("init_{media_str}.mp4")
        } else {
            format!("{media_str}_{}_{}.m4s", self.sequence, self.id)
        }
    }

    /// Returns the duration in seconds.
    #[must_use]
    pub fn duration_secs(&self) -> f64 {
        self.duration as f64 / 1000.0
    }
}

/// Segmentation state for a stream.
struct SegmentationState {
    /// Current sequence number.
    sequence: u64,

    /// Packets accumulated for current segment.
    current_packets: Vec<MediaPacket>,

    /// Current segment start timestamp.
    current_start_ts: Option<u64>,

    /// Last keyframe timestamp.
    last_keyframe_ts: u64,

    /// Completed segments.
    segments: VecDeque<Arc<MediaSegment>>,

    /// Maximum segments to keep.
    max_segments: usize,
}

impl SegmentationState {
    fn new() -> Self {
        Self {
            sequence: 0,
            current_packets: Vec::new(),
            current_start_ts: None,
            last_keyframe_ts: 0,
            segments: VecDeque::new(),
            max_segments: 100,
        }
    }

    fn add_packet(&mut self, packet: MediaPacket) {
        if self.current_start_ts.is_none() {
            self.current_start_ts = Some(packet.timestamp);
        }
        self.current_packets.push(packet);
    }

    fn should_finalize(&self, packet: &MediaPacket, target_duration_ms: u64) -> bool {
        if let Some(start_ts) = self.current_start_ts {
            let elapsed = packet.timestamp.saturating_sub(start_ts);
            if packet.keyframe && elapsed >= target_duration_ms {
                return true;
            }
        }
        false
    }

    fn finalize_segment(&mut self, media_type: MediaType) -> Option<Arc<MediaSegment>> {
        if self.current_packets.is_empty() {
            return None;
        }

        let start_ts = self.current_start_ts?;
        let last_ts = self.current_packets.last()?.timestamp;
        let duration = last_ts.saturating_sub(start_ts);

        // Package packets into segment data
        let data = Self::package_packets(&self.current_packets);

        let _has_keyframe = self.current_packets.iter().any(|p| p.keyframe);

        let segment = Arc::new(MediaSegment::new(
            self.sequence,
            start_ts,
            duration,
            data,
            media_type,
        ));

        self.sequence += 1;
        self.current_packets.clear();
        self.current_start_ts = None;

        // Store segment
        self.segments.push_back(segment.clone());
        if self.segments.len() > self.max_segments {
            self.segments.pop_front();
        }

        Some(segment)
    }

    fn package_packets(packets: &[MediaPacket]) -> Bytes {
        // Simple packaging - in production, would use proper MP4/fMP4 packaging
        let mut buf = BytesMut::new();
        for packet in packets {
            buf.extend_from_slice(&packet.data);
        }
        buf.freeze()
    }

    fn get_segments(&self, count: usize) -> Vec<Arc<MediaSegment>> {
        self.segments
            .iter()
            .rev()
            .take(count)
            .rev()
            .cloned()
            .collect()
    }

    fn get_segment_by_sequence(&self, sequence: u64) -> Option<Arc<MediaSegment>> {
        self.segments
            .iter()
            .find(|s| s.sequence == sequence)
            .cloned()
    }
}

/// Segment generator.
pub struct SegmentGenerator {
    /// Configuration.
    config: SegmentConfig,

    /// Video segmentation state.
    video_state: RwLock<SegmentationState>,

    /// Audio segmentation state.
    audio_state: RwLock<SegmentationState>,

    /// Initialization segment for video.
    video_init: RwLock<Option<Arc<MediaSegment>>>,

    /// Initialization segment for audio.
    audio_init: RwLock<Option<Arc<MediaSegment>>>,
}

impl SegmentGenerator {
    /// Creates a new segment generator.
    #[must_use]
    pub fn new(config: SegmentConfig) -> Self {
        Self {
            config,
            video_state: RwLock::new(SegmentationState::new()),
            audio_state: RwLock::new(SegmentationState::new()),
            video_init: RwLock::new(None),
            audio_init: RwLock::new(None),
        }
    }

    /// Adds a media packet.
    pub fn add_packet(&self, packet: &MediaPacket) -> NetResult<()> {
        let target_duration_ms = self.config.duration.as_millis() as u64;

        match packet.media_type {
            MediaType::Video => {
                let mut state = self.video_state.write();

                if state.should_finalize(packet, target_duration_ms) {
                    if let Some(_segment) = state.finalize_segment(MediaType::Video) {
                        // Segment finalized - could emit event here
                    }
                }

                state.add_packet(packet.clone());
            }
            MediaType::Audio => {
                let mut state = self.audio_state.write();

                if state.should_finalize(packet, target_duration_ms) {
                    if let Some(_segment) = state.finalize_segment(MediaType::Audio) {
                        // Segment finalized
                    }
                }

                state.add_packet(packet.clone());
            }
            MediaType::Metadata => {
                // Handle metadata packets
            }
        }

        Ok(())
    }

    /// Gets recent video segments.
    #[must_use]
    pub fn get_video_segments(&self, count: usize) -> Vec<Arc<MediaSegment>> {
        let state = self.video_state.read();
        state.get_segments(count)
    }

    /// Gets recent audio segments.
    #[must_use]
    pub fn get_audio_segments(&self, count: usize) -> Vec<Arc<MediaSegment>> {
        let state = self.audio_state.read();
        state.get_segments(count)
    }

    /// Gets video segment by sequence.
    #[must_use]
    pub fn get_video_segment(&self, sequence: u64) -> Option<Arc<MediaSegment>> {
        let state = self.video_state.read();
        state.get_segment_by_sequence(sequence)
    }

    /// Gets audio segment by sequence.
    #[must_use]
    pub fn get_audio_segment(&self, sequence: u64) -> Option<Arc<MediaSegment>> {
        let state = self.audio_state.read();
        state.get_segment_by_sequence(sequence)
    }

    /// Sets video initialization segment.
    pub fn set_video_init(&self, data: Bytes) {
        let segment = Arc::new(MediaSegment::init_segment(data, MediaType::Video));
        *self.video_init.write() = Some(segment);
    }

    /// Sets audio initialization segment.
    pub fn set_audio_init(&self, data: Bytes) {
        let segment = Arc::new(MediaSegment::init_segment(data, MediaType::Audio));
        *self.audio_init.write() = Some(segment);
    }

    /// Gets video initialization segment.
    #[must_use]
    pub fn get_video_init(&self) -> Option<Arc<MediaSegment>> {
        self.video_init.read().clone()
    }

    /// Gets audio initialization segment.
    #[must_use]
    pub fn get_audio_init(&self) -> Option<Arc<MediaSegment>> {
        self.audio_init.read().clone()
    }

    /// Forces finalization of current segments.
    pub fn flush(&self) {
        {
            let mut state = self.video_state.write();
            state.finalize_segment(MediaType::Video);
        }
        {
            let mut state = self.audio_state.write();
            state.finalize_segment(MediaType::Audio);
        }
    }

    /// Resets the segment generator.
    pub fn reset(&self) {
        *self.video_state.write() = SegmentationState::new();
        *self.audio_state.write() = SegmentationState::new();
        *self.video_init.write() = None;
        *self.audio_init.write() = None;
    }

    /// Returns current video sequence number.
    #[must_use]
    pub fn video_sequence(&self) -> u64 {
        self.video_state.read().sequence
    }

    /// Returns current audio sequence number.
    #[must_use]
    pub fn audio_sequence(&self) -> u64 {
        self.audio_state.read().sequence
    }
}

/// Segment cache for efficient serving.
pub struct SegmentCache {
    /// Cached segments.
    cache: RwLock<VecDeque<Arc<MediaSegment>>>,

    /// Maximum cache size.
    max_size: usize,
}

impl SegmentCache {
    /// Creates a new segment cache.
    #[must_use]
    pub fn new(max_size: usize) -> Self {
        Self {
            cache: RwLock::new(VecDeque::with_capacity(max_size)),
            max_size,
        }
    }

    /// Adds a segment to the cache.
    pub fn add(&self, segment: Arc<MediaSegment>) {
        let mut cache = self.cache.write();
        cache.push_back(segment);
        if cache.len() > self.max_size {
            cache.pop_front();
        }
    }

    /// Gets a segment by sequence number.
    #[must_use]
    pub fn get(&self, sequence: u64) -> Option<Arc<MediaSegment>> {
        let cache = self.cache.read();
        cache.iter().find(|s| s.sequence == sequence).cloned()
    }

    /// Gets the most recent segments.
    #[must_use]
    pub fn get_recent(&self, count: usize) -> Vec<Arc<MediaSegment>> {
        let cache = self.cache.read();
        cache.iter().rev().take(count).rev().cloned().collect()
    }

    /// Clears the cache.
    pub fn clear(&self) {
        self.cache.write().clear();
    }

    /// Returns cache size.
    #[must_use]
    pub fn size(&self) -> usize {
        self.cache.read().len()
    }
}