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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! DASH MPD (Media Presentation Description) generation.

use crate::config::BitrateEntry;
use crate::error::{PackagerError, PackagerResult};
use crate::manifest::{CodecStringBuilder, DurationFormatter};
use chrono::Utc;
use quick_xml::events::{BytesDecl, BytesEnd, BytesStart, BytesText, Event};
use quick_xml::Writer;
use std::io::Cursor;
use std::time::Duration;

/// MPD type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MpdType {
    /// Static MPD (VOD).
    Static,
    /// Dynamic MPD (live).
    Dynamic,
}

impl MpdType {
    /// Convert to string for MPD.
    #[must_use]
    pub fn as_str(&self) -> &str {
        match self {
            Self::Static => "static",
            Self::Dynamic => "dynamic",
        }
    }
}

/// DASH profile.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DashProfile {
    /// Live profile.
    Live,
    /// On-demand profile.
    OnDemand,
    /// Main profile.
    Main,
}

impl DashProfile {
    /// Convert to URN string.
    #[must_use]
    pub fn as_urn(&self) -> &str {
        match self {
            Self::Live => "urn:mpeg:dash:profile:isoff-live:2011",
            Self::OnDemand => "urn:mpeg:dash:profile:isoff-on-demand:2011",
            Self::Main => "urn:mpeg:dash:profile:isoff-main:2011",
        }
    }
}

/// Representation in an adaptation set.
#[derive(Debug, Clone)]
pub struct Representation {
    /// Representation ID.
    pub id: String,
    /// Bandwidth in bits per second.
    pub bandwidth: u32,
    /// Codec string.
    pub codecs: String,
    /// Width (for video).
    pub width: Option<u32>,
    /// Height (for video).
    pub height: Option<u32>,
    /// Frame rate (for video).
    pub frame_rate: Option<String>,
    /// Audio sampling rate.
    pub audio_sampling_rate: Option<u32>,
    /// Segment template.
    pub segment_template: Option<SegmentTemplate>,
    /// Base URL.
    pub base_url: Option<String>,
}

impl Representation {
    /// Create a new representation.
    #[must_use]
    pub fn new(id: String, bandwidth: u32, codecs: String) -> Self {
        Self {
            id,
            bandwidth,
            codecs,
            width: None,
            height: None,
            frame_rate: None,
            audio_sampling_rate: None,
            segment_template: None,
            base_url: None,
        }
    }

    /// Set dimensions.
    #[must_use]
    pub fn with_dimensions(mut self, width: u32, height: u32) -> Self {
        self.width = Some(width);
        self.height = Some(height);
        self
    }

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

    /// Set segment template.
    #[must_use]
    pub fn with_segment_template(mut self, template: SegmentTemplate) -> Self {
        self.segment_template = Some(template);
        self
    }
}

/// Segment template for DASH.
#[derive(Debug, Clone)]
pub struct SegmentTemplate {
    /// Initialization segment URL template.
    pub initialization: String,
    /// Media segment URL template.
    pub media: String,
    /// Segment duration (in timescale units).
    pub duration: u64,
    /// Timescale.
    pub timescale: u32,
    /// Start number.
    pub start_number: u32,
}

impl SegmentTemplate {
    /// Create a new segment template.
    #[must_use]
    pub fn new(initialization: String, media: String, duration: u64, timescale: u32) -> Self {
        Self {
            initialization,
            media,
            duration,
            timescale,
            start_number: 1,
        }
    }

    /// Set start number.
    #[must_use]
    pub fn with_start_number(mut self, start: u32) -> Self {
        self.start_number = start;
        self
    }
}

/// Adaptation set in MPD.
#[derive(Debug, Clone)]
pub struct AdaptationSet {
    /// Adaptation set ID.
    pub id: u32,
    /// Content type (video, audio, text).
    pub content_type: String,
    /// MIME type.
    pub mime_type: String,
    /// Segment alignment flag.
    pub segment_alignment: bool,
    /// Representations in this set.
    pub representations: Vec<Representation>,
    /// Language (for audio/subtitle).
    pub lang: Option<String>,
}

impl AdaptationSet {
    /// Create a new adaptation set.
    #[must_use]
    pub fn new(id: u32, content_type: String, mime_type: String) -> Self {
        Self {
            id,
            content_type,
            mime_type,
            segment_alignment: true,
            representations: Vec::new(),
            lang: None,
        }
    }

    /// Add a representation.
    pub fn add_representation(&mut self, representation: Representation) {
        self.representations.push(representation);
    }

    /// Set language.
    #[must_use]
    pub fn with_language(mut self, lang: String) -> Self {
        self.lang = Some(lang);
        self
    }
}

/// Period in MPD.
#[derive(Debug, Clone)]
pub struct Period {
    /// Period ID.
    pub id: String,
    /// Duration.
    pub duration: Option<Duration>,
    /// Adaptation sets.
    pub adaptation_sets: Vec<AdaptationSet>,
}

impl Period {
    /// Create a new period.
    #[must_use]
    pub fn new(id: String) -> Self {
        Self {
            id,
            duration: None,
            adaptation_sets: Vec::new(),
        }
    }

    /// Set duration.
    #[must_use]
    pub fn with_duration(mut self, duration: Duration) -> Self {
        self.duration = Some(duration);
        self
    }

    /// Add an adaptation set.
    pub fn add_adaptation_set(&mut self, set: AdaptationSet) {
        self.adaptation_sets.push(set);
    }
}

/// MPD builder for generating DASH manifests.
pub struct MpdBuilder {
    mpd_type: MpdType,
    profile: DashProfile,
    min_buffer_time: Duration,
    media_presentation_duration: Option<Duration>,
    periods: Vec<Period>,
    base_url: Option<String>,
}

impl MpdBuilder {
    /// Create a new MPD builder.
    #[must_use]
    pub fn new(mpd_type: MpdType, profile: DashProfile) -> Self {
        Self {
            mpd_type,
            profile,
            min_buffer_time: Duration::from_secs(2),
            media_presentation_duration: None,
            periods: Vec::new(),
            base_url: None,
        }
    }

    /// Set minimum buffer time.
    #[must_use]
    pub fn with_min_buffer_time(mut self, duration: Duration) -> Self {
        self.min_buffer_time = duration;
        self
    }

    /// Set media presentation duration.
    #[must_use]
    pub fn with_duration(mut self, duration: Duration) -> Self {
        self.media_presentation_duration = Some(duration);
        self
    }

    /// Set base URL.
    #[must_use]
    pub fn with_base_url(mut self, url: String) -> Self {
        self.base_url = Some(url);
        self
    }

    /// Add a period.
    pub fn add_period(&mut self, period: Period) {
        self.periods.push(period);
    }

    /// Build the MPD XML.
    pub fn build(&self) -> PackagerResult<String> {
        let mut writer = Writer::new_with_indent(Cursor::new(Vec::new()), b' ', 2);

        // XML declaration
        writer
            .write_event(Event::Decl(BytesDecl::new("1.0", Some("UTF-8"), None)))
            .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;

        // MPD element
        let mut mpd_elem = BytesStart::new("MPD");
        mpd_elem.push_attribute(("xmlns", "urn:mpeg:dash:schema:mpd:2011"));
        mpd_elem.push_attribute(("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"));
        mpd_elem.push_attribute((
            "xsi:schemaLocation",
            "urn:mpeg:dash:schema:mpd:2011 DASH-MPD.xsd",
        ));
        mpd_elem.push_attribute(("type", self.mpd_type.as_str()));
        mpd_elem.push_attribute(("profiles", self.profile.as_urn()));

        // Min buffer time
        let min_buffer = DurationFormatter::format_iso8601_duration(self.min_buffer_time);
        mpd_elem.push_attribute(("minBufferTime", min_buffer.as_str()));

        // Media presentation duration (for static MPD)
        if let Some(duration) = self.media_presentation_duration {
            let duration_str = DurationFormatter::format_iso8601_duration(duration);
            mpd_elem.push_attribute(("mediaPresentationDuration", duration_str.as_str()));
        }

        // Publish time (for dynamic MPD)
        if self.mpd_type == MpdType::Dynamic {
            let now = Utc::now().to_rfc3339();
            mpd_elem.push_attribute(("publishTime", now.as_str()));
        }

        writer
            .write_event(Event::Start(mpd_elem))
            .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;

        // Base URL
        if let Some(base) = &self.base_url {
            writer
                .write_event(Event::Start(BytesStart::new("BaseURL")))
                .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;
            writer
                .write_event(Event::Text(BytesText::new(base)))
                .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;
            writer
                .write_event(Event::End(BytesEnd::new("BaseURL")))
                .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;
        }

        // Periods
        for period in &self.periods {
            self.write_period(&mut writer, period)?;
        }

        // Close MPD
        writer
            .write_event(Event::End(BytesEnd::new("MPD")))
            .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;

        let xml_bytes = writer.into_inner().into_inner();
        String::from_utf8(xml_bytes)
            .map_err(|e| PackagerError::manifest_failed(format!("UTF-8 error: {e}")))
    }

    /// Write a period element.
    fn write_period<W: std::io::Write>(
        &self,
        writer: &mut Writer<W>,
        period: &Period,
    ) -> PackagerResult<()> {
        let mut period_elem = BytesStart::new("Period");
        period_elem.push_attribute(("id", period.id.as_str()));

        if let Some(duration) = period.duration {
            let duration_str = DurationFormatter::format_iso8601_duration(duration);
            period_elem.push_attribute(("duration", duration_str.as_str()));
        }

        writer
            .write_event(Event::Start(period_elem))
            .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;

        // Adaptation sets
        for set in &period.adaptation_sets {
            self.write_adaptation_set(writer, set)?;
        }

        writer
            .write_event(Event::End(BytesEnd::new("Period")))
            .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;

        Ok(())
    }

    /// Write an adaptation set element.
    fn write_adaptation_set<W: std::io::Write>(
        &self,
        writer: &mut Writer<W>,
        set: &AdaptationSet,
    ) -> PackagerResult<()> {
        let mut set_elem = BytesStart::new("AdaptationSet");
        set_elem.push_attribute(("id", set.id.to_string().as_str()));
        set_elem.push_attribute(("contentType", set.content_type.as_str()));
        set_elem.push_attribute(("mimeType", set.mime_type.as_str()));

        if set.segment_alignment {
            set_elem.push_attribute(("segmentAlignment", "true"));
        }

        if let Some(lang) = &set.lang {
            set_elem.push_attribute(("lang", lang.as_str()));
        }

        writer
            .write_event(Event::Start(set_elem))
            .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;

        // Representations
        for repr in &set.representations {
            self.write_representation(writer, repr)?;
        }

        writer
            .write_event(Event::End(BytesEnd::new("AdaptationSet")))
            .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;

        Ok(())
    }

    /// Write a representation element.
    fn write_representation<W: std::io::Write>(
        &self,
        writer: &mut Writer<W>,
        repr: &Representation,
    ) -> PackagerResult<()> {
        let mut repr_elem = BytesStart::new("Representation");
        repr_elem.push_attribute(("id", repr.id.as_str()));
        repr_elem.push_attribute(("bandwidth", repr.bandwidth.to_string().as_str()));
        repr_elem.push_attribute(("codecs", repr.codecs.as_str()));

        if let Some(width) = repr.width {
            repr_elem.push_attribute(("width", width.to_string().as_str()));
        }

        if let Some(height) = repr.height {
            repr_elem.push_attribute(("height", height.to_string().as_str()));
        }

        if let Some(fps) = &repr.frame_rate {
            repr_elem.push_attribute(("frameRate", fps.as_str()));
        }

        if let Some(sample_rate) = repr.audio_sampling_rate {
            repr_elem.push_attribute(("audioSamplingRate", sample_rate.to_string().as_str()));
        }

        writer
            .write_event(Event::Start(repr_elem))
            .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;

        // Base URL
        if let Some(base_url) = &repr.base_url {
            writer
                .write_event(Event::Start(BytesStart::new("BaseURL")))
                .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;
            writer
                .write_event(Event::Text(BytesText::new(base_url)))
                .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;
            writer
                .write_event(Event::End(BytesEnd::new("BaseURL")))
                .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;
        }

        // Segment template
        if let Some(template) = &repr.segment_template {
            self.write_segment_template(writer, template)?;
        }

        writer
            .write_event(Event::End(BytesEnd::new("Representation")))
            .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;

        Ok(())
    }

    /// Write segment template element.
    fn write_segment_template<W: std::io::Write>(
        &self,
        writer: &mut Writer<W>,
        template: &SegmentTemplate,
    ) -> PackagerResult<()> {
        let mut template_elem = BytesStart::new("SegmentTemplate");
        template_elem.push_attribute(("initialization", template.initialization.as_str()));
        template_elem.push_attribute(("media", template.media.as_str()));
        template_elem.push_attribute(("duration", template.duration.to_string().as_str()));
        template_elem.push_attribute(("timescale", template.timescale.to_string().as_str()));
        template_elem.push_attribute(("startNumber", template.start_number.to_string().as_str()));

        writer
            .write_event(Event::Empty(template_elem))
            .map_err(|e| PackagerError::manifest_failed(format!("XML write error: {e}")))?;

        Ok(())
    }
}

/// Create representation from bitrate entry.
pub fn representation_from_bitrate_entry(
    entry: &BitrateEntry,
    id: String,
) -> PackagerResult<Representation> {
    let codec_str = match entry.codec.as_str() {
        "av1" => CodecStringBuilder::av1(0, 4, 8),
        "vp9" => CodecStringBuilder::vp9(0, 40, 8),
        "vp8" => CodecStringBuilder::vp8(),
        _ => {
            return Err(PackagerError::unsupported_codec(format!(
                "Unsupported codec: {}",
                entry.codec
            )))
        }
    };

    let mut repr = Representation::new(id, entry.bitrate, codec_str)
        .with_dimensions(entry.width, entry.height);

    if let Some(fps) = entry.framerate {
        repr = repr.with_frame_rate(fps);
    }

    Ok(repr)
}

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

    #[test]
    fn test_mpd_type_conversion() {
        assert_eq!(MpdType::Static.as_str(), "static");
        assert_eq!(MpdType::Dynamic.as_str(), "dynamic");
    }

    #[test]
    fn test_dash_profile_urn() {
        assert!(DashProfile::OnDemand.as_urn().contains("on-demand"));
    }

    #[test]
    fn test_representation_creation() {
        let repr =
            Representation::new("video1".to_string(), 1_000_000, "av01.0.04M.08".to_string());

        assert_eq!(repr.id, "video1");
        assert_eq!(repr.bandwidth, 1_000_000);
    }

    #[test]
    fn test_mpd_builder() {
        let mut builder = MpdBuilder::new(MpdType::Static, DashProfile::OnDemand);

        let mut period = Period::new("0".to_string());
        let mut adaptation_set =
            AdaptationSet::new(0, "video".to_string(), "video/mp4".to_string());

        let repr = Representation::new("1".to_string(), 1_000_000, "av01.0.04M.08".to_string());

        adaptation_set.add_representation(repr);
        period.add_adaptation_set(adaptation_set);
        builder.add_period(period);

        let mpd = builder.build().expect("should succeed in test");

        assert!(mpd.contains("<MPD"));
        assert!(mpd.contains("type=\"static\""));
        assert!(mpd.contains("Representation"));
    }
}