oximedia-packager 0.1.8

Adaptive streaming packaging (HLS/DASH) 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
#![allow(dead_code)]
//! Variant stream management for multi-bitrate adaptive packaging.
//!
//! This module provides structures for tracking variant streams (renditions)
//! in an adaptive bitrate ladder, including video, audio, and subtitle
//! alternate renditions used in HLS and DASH packaging.

use crate::config::SegmentFormat;
use crate::error::{PackagerError, PackagerResult};

/// Codec identifier for a variant stream.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StreamCodec {
    /// AV1 video codec.
    Av1,
    /// VP9 video codec.
    Vp9,
    /// VP8 video codec.
    Vp8,
    /// Opus audio codec.
    Opus,
    /// Vorbis audio codec.
    Vorbis,
    /// FLAC audio codec.
    Flac,
    /// WebVTT subtitle format.
    WebVtt,
}

impl StreamCodec {
    /// MIME codecs string for HLS/DASH manifests.
    #[must_use]
    pub const fn codecs_string(&self) -> &'static str {
        match self {
            Self::Av1 => "av01.0.08M.08",
            Self::Vp9 => "vp09.00.31.08",
            Self::Vp8 => "vp8",
            Self::Opus => "opus",
            Self::Vorbis => "vorbis",
            Self::Flac => "flac",
            Self::WebVtt => "wvtt",
        }
    }

    /// Whether this codec represents video.
    #[must_use]
    pub const fn is_video(&self) -> bool {
        matches!(self, Self::Av1 | Self::Vp9 | Self::Vp8)
    }

    /// Whether this codec represents audio.
    #[must_use]
    pub const fn is_audio(&self) -> bool {
        matches!(self, Self::Opus | Self::Vorbis | Self::Flac)
    }

    /// Whether this codec represents subtitles.
    #[must_use]
    pub const fn is_subtitle(&self) -> bool {
        matches!(self, Self::WebVtt)
    }
}

/// A single variant stream in the adaptive ladder.
#[derive(Debug, Clone)]
pub struct VariantStream {
    /// Unique identifier for this variant.
    pub id: String,
    /// Video codec (if video variant).
    pub video_codec: Option<StreamCodec>,
    /// Audio codec (if audio variant or muxed).
    pub audio_codec: Option<StreamCodec>,
    /// Width in pixels (video only).
    pub width: Option<u32>,
    /// Height in pixels (video only).
    pub height: Option<u32>,
    /// Frame rate (video only).
    pub frame_rate: Option<f64>,
    /// Peak video bitrate in bits/s.
    pub video_bitrate: u64,
    /// Audio bitrate in bits/s.
    pub audio_bitrate: u64,
    /// Segment format for this variant.
    pub segment_format: SegmentFormat,
    /// Language tag (BCP 47) for audio/subtitle variants.
    pub language: Option<String>,
    /// Whether this variant is the default selection.
    pub is_default: bool,
}

impl VariantStream {
    /// Create a new video variant.
    #[must_use]
    pub fn video(id: &str, codec: StreamCodec, width: u32, height: u32, bitrate: u64) -> Self {
        Self {
            id: id.to_string(),
            video_codec: Some(codec),
            audio_codec: None,
            width: Some(width),
            height: Some(height),
            frame_rate: None,
            video_bitrate: bitrate,
            audio_bitrate: 0,
            segment_format: SegmentFormat::Fmp4,
            language: None,
            is_default: false,
        }
    }

    /// Create a new audio-only variant.
    #[must_use]
    pub fn audio(id: &str, codec: StreamCodec, bitrate: u64, language: &str) -> Self {
        Self {
            id: id.to_string(),
            video_codec: None,
            audio_codec: Some(codec),
            width: None,
            height: None,
            frame_rate: None,
            video_bitrate: 0,
            audio_bitrate: bitrate,
            segment_format: SegmentFormat::Fmp4,
            language: Some(language.to_string()),
            is_default: false,
        }
    }

    /// Set this variant as the default.
    #[must_use]
    pub fn as_default(mut self) -> Self {
        self.is_default = true;
        self
    }

    /// Set the frame rate.
    #[must_use]
    pub fn with_frame_rate(mut self, fps: f64) -> Self {
        self.frame_rate = Some(fps);
        self
    }

    /// Total bandwidth for this variant.
    #[must_use]
    pub fn total_bandwidth(&self) -> u64 {
        self.video_bitrate + self.audio_bitrate
    }

    /// Build the combined codecs string for manifests.
    #[must_use]
    pub fn combined_codecs(&self) -> String {
        let mut parts = Vec::new();
        if let Some(vc) = &self.video_codec {
            parts.push(vc.codecs_string().to_string());
        }
        if let Some(ac) = &self.audio_codec {
            parts.push(ac.codecs_string().to_string());
        }
        parts.join(",")
    }

    /// Resolution string (e.g. "1920x1080").
    #[must_use]
    pub fn resolution_string(&self) -> Option<String> {
        match (self.width, self.height) {
            (Some(w), Some(h)) => Some(format!("{w}x{h}")),
            _ => None,
        }
    }

    /// Validate the variant stream.
    ///
    /// # Errors
    ///
    /// Returns an error if required fields are missing.
    pub fn validate(&self) -> PackagerResult<()> {
        if self.id.is_empty() {
            return Err(PackagerError::InvalidConfig(
                "Variant stream ID must not be empty".into(),
            ));
        }
        if self.video_codec.is_none() && self.audio_codec.is_none() {
            return Err(PackagerError::InvalidConfig(
                "Variant must have at least one codec".into(),
            ));
        }
        if self.video_codec.is_some() && (self.width.is_none() || self.height.is_none()) {
            return Err(PackagerError::InvalidConfig(
                "Video variant must specify width and height".into(),
            ));
        }
        Ok(())
    }
}

/// A collection of variant streams forming an adaptive set.
#[derive(Debug, Clone)]
pub struct VariantSet {
    /// The variant streams.
    pub variants: Vec<VariantStream>,
}

impl VariantSet {
    /// Create a new empty variant set.
    #[must_use]
    pub fn new() -> Self {
        Self {
            variants: Vec::new(),
        }
    }

    /// Add a variant stream.
    pub fn add(&mut self, variant: VariantStream) {
        self.variants.push(variant);
    }

    /// Number of variants.
    #[must_use]
    pub fn len(&self) -> usize {
        self.variants.len()
    }

    /// Whether the set is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.variants.is_empty()
    }

    /// Get all video variants, sorted by bandwidth (ascending).
    #[must_use]
    pub fn video_variants(&self) -> Vec<&VariantStream> {
        let mut vids: Vec<&VariantStream> = self
            .variants
            .iter()
            .filter(|v| v.video_codec.is_some())
            .collect();
        vids.sort_by_key(|v| v.video_bitrate);
        vids
    }

    /// Get all audio variants.
    #[must_use]
    pub fn audio_variants(&self) -> Vec<&VariantStream> {
        self.variants
            .iter()
            .filter(|v| v.video_codec.is_none() && v.audio_codec.is_some())
            .collect()
    }

    /// Validate all variants.
    ///
    /// # Errors
    ///
    /// Returns the first validation error encountered.
    pub fn validate(&self) -> PackagerResult<()> {
        if self.variants.is_empty() {
            return Err(PackagerError::InvalidConfig(
                "Variant set must have at least one variant".into(),
            ));
        }
        for v in &self.variants {
            v.validate()?;
        }
        Ok(())
    }
}

impl Default for VariantSet {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_stream_codec_properties() {
        assert!(StreamCodec::Av1.is_video());
        assert!(!StreamCodec::Av1.is_audio());
        assert!(StreamCodec::Opus.is_audio());
        assert!(StreamCodec::WebVtt.is_subtitle());
    }

    #[test]
    fn test_codecs_string() {
        assert_eq!(StreamCodec::Av1.codecs_string(), "av01.0.08M.08");
        assert_eq!(StreamCodec::Opus.codecs_string(), "opus");
    }

    #[test]
    fn test_video_variant_creation() {
        let v = VariantStream::video("v1", StreamCodec::Av1, 1920, 1080, 5_000_000);
        assert_eq!(v.width, Some(1920));
        assert_eq!(v.height, Some(1080));
        assert!(v.validate().is_ok());
    }

    #[test]
    fn test_audio_variant_creation() {
        let v = VariantStream::audio("a1", StreamCodec::Opus, 128_000, "en");
        assert_eq!(v.language, Some("en".to_string()));
        assert!(v.validate().is_ok());
    }

    #[test]
    fn test_variant_total_bandwidth() {
        let mut v = VariantStream::video("v1", StreamCodec::Vp9, 1280, 720, 3_000_000);
        v.audio_bitrate = 128_000;
        assert_eq!(v.total_bandwidth(), 3_128_000);
    }

    #[test]
    fn test_combined_codecs() {
        let mut v = VariantStream::video("v1", StreamCodec::Av1, 1920, 1080, 5_000_000);
        v.audio_codec = Some(StreamCodec::Opus);
        let codecs = v.combined_codecs();
        assert!(codecs.contains("av01"));
        assert!(codecs.contains("opus"));
    }

    #[test]
    fn test_resolution_string() {
        let v = VariantStream::video("v1", StreamCodec::Av1, 1920, 1080, 5_000_000);
        assert_eq!(v.resolution_string(), Some("1920x1080".to_string()));
    }

    #[test]
    fn test_audio_variant_no_resolution() {
        let v = VariantStream::audio("a1", StreamCodec::Opus, 128_000, "en");
        assert_eq!(v.resolution_string(), None);
    }

    #[test]
    fn test_variant_validate_empty_id() {
        let v = VariantStream::video("", StreamCodec::Av1, 1920, 1080, 5_000_000);
        assert!(v.validate().is_err());
    }

    #[test]
    fn test_variant_validate_no_codec() {
        let v = VariantStream {
            id: "x".to_string(),
            video_codec: None,
            audio_codec: None,
            width: None,
            height: None,
            frame_rate: None,
            video_bitrate: 0,
            audio_bitrate: 0,
            segment_format: SegmentFormat::Fmp4,
            language: None,
            is_default: false,
        };
        assert!(v.validate().is_err());
    }

    #[test]
    fn test_variant_set() {
        let mut set = VariantSet::new();
        set.add(VariantStream::video(
            "v1",
            StreamCodec::Av1,
            1920,
            1080,
            5_000_000,
        ));
        set.add(VariantStream::video(
            "v2",
            StreamCodec::Av1,
            1280,
            720,
            3_000_000,
        ));
        assert_eq!(set.len(), 2);
        assert!(set.validate().is_ok());
    }

    #[test]
    fn test_variant_set_video_sorted() {
        let mut set = VariantSet::new();
        set.add(VariantStream::video(
            "hi",
            StreamCodec::Av1,
            1920,
            1080,
            5_000_000,
        ));
        set.add(VariantStream::video(
            "lo",
            StreamCodec::Av1,
            640,
            360,
            500_000,
        ));
        let vids = set.video_variants();
        assert!(vids[0].video_bitrate < vids[1].video_bitrate);
    }

    #[test]
    fn test_variant_set_empty_validation() {
        let set = VariantSet::new();
        assert!(set.validate().is_err());
    }

    #[test]
    fn test_default_variant() {
        let v = VariantStream::video("v1", StreamCodec::Av1, 1920, 1080, 5_000_000).as_default();
        assert!(v.is_default);
    }
}