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
//! HLS playlist generation (M3U8).

use crate::config::BitrateEntry;
use crate::encryption::EncryptionHandler;
use crate::error::{PackagerError, PackagerResult};
use crate::manifest::{CodecStringBuilder, DurationFormatter};
use crate::segment::SegmentInfo;
use std::fmt::Write as FmtWrite;
use std::time::Duration;

/// HLS playlist type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlaylistType {
    /// Master playlist (multi-variant).
    Master,
    /// Media playlist (single variant).
    Media,
}

/// Media type for HLS.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MediaType {
    /// Video.
    Video,
    /// Audio.
    Audio,
    /// Subtitles.
    Subtitles,
}

impl MediaType {
    /// Convert to HLS media type string.
    #[must_use]
    pub fn as_str(&self) -> &str {
        match self {
            Self::Video => "VIDEO",
            Self::Audio => "AUDIO",
            Self::Subtitles => "SUBTITLES",
        }
    }
}

/// HLS variant stream information.
#[derive(Debug, Clone)]
pub struct VariantStream {
    /// Bandwidth in bits per second.
    pub bandwidth: u32,
    /// Average bandwidth.
    pub average_bandwidth: Option<u32>,
    /// Codec string.
    pub codecs: String,
    /// Resolution (width x height).
    pub resolution: Option<(u32, u32)>,
    /// Frame rate.
    pub frame_rate: Option<f64>,
    /// URI to media playlist.
    pub uri: String,
    /// Audio group ID.
    pub audio: Option<String>,
    /// Subtitles group ID.
    pub subtitles: Option<String>,
}

impl VariantStream {
    /// Create a new variant stream.
    #[must_use]
    pub fn new(bandwidth: u32, codecs: String, uri: String) -> Self {
        Self {
            bandwidth,
            average_bandwidth: None,
            codecs,
            resolution: None,
            frame_rate: None,
            uri,
            audio: None,
            subtitles: None,
        }
    }

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

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

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

    /// Set average bandwidth.
    #[must_use]
    pub fn with_average_bandwidth(mut self, avg: u32) -> Self {
        self.average_bandwidth = Some(avg);
        self
    }
}

/// HLS media group (for alternate audio/subtitles).
#[derive(Debug, Clone)]
pub struct MediaGroup {
    /// Media type.
    pub media_type: MediaType,
    /// Group ID.
    pub group_id: String,
    /// Media name.
    pub name: String,
    /// Language.
    pub language: Option<String>,
    /// Is default.
    pub is_default: bool,
    /// Auto-select.
    pub autoselect: bool,
    /// URI to media playlist.
    pub uri: Option<String>,
}

impl MediaGroup {
    /// Create a new media group.
    #[must_use]
    pub fn new(media_type: MediaType, group_id: String, name: String) -> Self {
        Self {
            media_type,
            group_id,
            name,
            language: None,
            is_default: false,
            autoselect: false,
            uri: None,
        }
    }

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

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

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

/// Master playlist builder.
pub struct MasterPlaylistBuilder {
    variants: Vec<VariantStream>,
    media_groups: Vec<MediaGroup>,
    version: u32,
    independent_segments: bool,
}

impl MasterPlaylistBuilder {
    /// Create a new master playlist builder.
    #[must_use]
    pub fn new() -> Self {
        Self {
            variants: Vec::new(),
            media_groups: Vec::new(),
            version: 6,
            independent_segments: true,
        }
    }

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

    /// Add a media group.
    pub fn add_media_group(&mut self, group: MediaGroup) {
        self.media_groups.push(group);
    }

    /// Set HLS version.
    #[must_use]
    pub fn with_version(mut self, version: u32) -> Self {
        self.version = version;
        self
    }

    /// Build the master playlist.
    pub fn build(&self) -> PackagerResult<String> {
        let mut output = String::new();

        // Header
        writeln!(output, "#EXTM3U")
            .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        writeln!(output, "#EXT-X-VERSION:{}", self.version)
            .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;

        if self.independent_segments {
            writeln!(output, "#EXT-X-INDEPENDENT-SEGMENTS")
                .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        }

        // Media groups
        for group in &self.media_groups {
            self.write_media_group(&mut output, group)?;
        }

        // Variant streams (sorted by bandwidth)
        let mut sorted_variants = self.variants.clone();
        sorted_variants.sort_by_key(|v| v.bandwidth);

        for variant in &sorted_variants {
            self.write_variant_stream(&mut output, variant)?;
        }

        Ok(output)
    }

    /// Write media group tag.
    fn write_media_group(&self, output: &mut String, group: &MediaGroup) -> PackagerResult<()> {
        write!(
            output,
            "#EXT-X-MEDIA:TYPE={},GROUP-ID=\"{}\",NAME=\"{}\"",
            group.media_type.as_str(),
            group.group_id,
            group.name
        )
        .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;

        if let Some(lang) = &group.language {
            write!(output, ",LANGUAGE=\"{lang}\"")
                .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        }

        if group.is_default {
            write!(output, ",DEFAULT=YES")
                .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        }

        if group.autoselect {
            write!(output, ",AUTOSELECT=YES")
                .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        }

        if let Some(uri) = &group.uri {
            write!(output, ",URI=\"{uri}\"")
                .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        }

        writeln!(output)
            .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;

        Ok(())
    }

    /// Write variant stream tag.
    fn write_variant_stream(
        &self,
        output: &mut String,
        variant: &VariantStream,
    ) -> PackagerResult<()> {
        write!(
            output,
            "#EXT-X-STREAM-INF:BANDWIDTH={},CODECS=\"{}\"",
            variant.bandwidth, variant.codecs
        )
        .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;

        if let Some(avg) = variant.average_bandwidth {
            write!(output, ",AVERAGE-BANDWIDTH={avg}")
                .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        }

        if let Some((width, height)) = variant.resolution {
            write!(output, ",RESOLUTION={width}x{height}")
                .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        }

        if let Some(fps) = variant.frame_rate {
            write!(output, ",FRAME-RATE={fps:.3}")
                .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        }

        if let Some(audio) = &variant.audio {
            write!(output, ",AUDIO=\"{audio}\"")
                .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        }

        if let Some(subtitles) = &variant.subtitles {
            write!(output, ",SUBTITLES=\"{subtitles}\"")
                .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        }

        writeln!(output)
            .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        writeln!(output, "{}", variant.uri)
            .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;

        Ok(())
    }
}

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

/// Media playlist builder.
pub struct MediaPlaylistBuilder {
    segments: Vec<SegmentInfo>,
    version: u32,
    target_duration: Duration,
    media_sequence: u64,
    encryption_handler: Option<EncryptionHandler>,
    playlist_type: Option<String>,
    end_list: bool,
}

impl MediaPlaylistBuilder {
    /// Create a new media playlist builder.
    #[must_use]
    pub fn new(target_duration: Duration) -> Self {
        Self {
            segments: Vec::new(),
            version: 6,
            target_duration,
            media_sequence: 0,
            encryption_handler: None,
            playlist_type: None,
            end_list: false,
        }
    }

    /// Add a segment.
    pub fn add_segment(&mut self, segment: SegmentInfo) {
        self.segments.push(segment);
    }

    /// Set media sequence number.
    #[must_use]
    pub fn with_media_sequence(mut self, seq: u64) -> Self {
        self.media_sequence = seq;
        self
    }

    /// Set encryption handler.
    #[must_use]
    pub fn with_encryption(mut self, handler: EncryptionHandler) -> Self {
        self.encryption_handler = Some(handler);
        self
    }

    /// Set playlist type (VOD or EVENT).
    #[must_use]
    pub fn with_playlist_type(mut self, playlist_type: String) -> Self {
        self.playlist_type = Some(playlist_type);
        self
    }

    /// Mark playlist as ended.
    #[must_use]
    pub fn with_end_list(mut self) -> Self {
        self.end_list = true;
        self
    }

    /// Build the media playlist.
    pub fn build(&self) -> PackagerResult<String> {
        let mut output = String::new();

        // Header
        writeln!(output, "#EXTM3U")
            .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        writeln!(output, "#EXT-X-VERSION:{}", self.version)
            .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;

        // Target duration (rounded up)
        let target_secs = self.target_duration.as_secs() + 1;
        writeln!(output, "#EXT-X-TARGETDURATION:{target_secs}")
            .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;

        // Media sequence
        writeln!(output, "#EXT-X-MEDIA-SEQUENCE:{}", self.media_sequence)
            .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;

        // Playlist type
        if let Some(ptype) = &self.playlist_type {
            writeln!(output, "#EXT-X-PLAYLIST-TYPE:{ptype}")
                .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        }

        // Encryption
        if let Some(handler) = &self.encryption_handler {
            if handler.is_enabled() {
                let key_tag = handler.generate_hls_key_tag()?;
                writeln!(output, "{key_tag}")
                    .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
            }
        }

        // Segments
        for segment in &self.segments {
            self.write_segment(&mut output, segment)?;
        }

        // End list marker
        if self.end_list {
            writeln!(output, "#EXT-X-ENDLIST")
                .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        }

        Ok(output)
    }

    /// Write segment information.
    fn write_segment(&self, output: &mut String, segment: &SegmentInfo) -> PackagerResult<()> {
        let duration_str = DurationFormatter::format_hls_duration(segment.duration);

        writeln!(output, "#EXTINF:{duration_str},")
            .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;
        writeln!(output, "{}", segment.path)
            .map_err(|e| PackagerError::manifest_failed(format!("Write error: {e}")))?;

        Ok(())
    }

    /// Calculate target duration from segments.
    #[must_use]
    pub fn calculate_target_duration(segments: &[SegmentInfo]) -> Duration {
        segments
            .iter()
            .map(|s| s.duration)
            .max()
            .unwrap_or(Duration::from_secs(6))
    }
}

/// Generate HLS variant stream from bitrate entry.
pub fn variant_from_bitrate_entry(
    entry: &BitrateEntry,
    uri: String,
) -> PackagerResult<VariantStream> {
    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 variant = VariantStream::new(entry.bitrate, codec_str, uri)
        .with_resolution(entry.width, entry.height);

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

    Ok(variant)
}

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

    #[test]
    fn test_master_playlist_generation() {
        let mut builder = MasterPlaylistBuilder::new();

        let variant = VariantStream::new(
            1_000_000,
            "av01.0.04M.08".to_string(),
            "variant_1.m3u8".to_string(),
        )
        .with_resolution(1280, 720);

        builder.add_variant(variant);

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

        assert!(playlist.contains("#EXTM3U"));
        assert!(playlist.contains("#EXT-X-VERSION:6"));
        assert!(playlist.contains("BANDWIDTH=1000000"));
        assert!(playlist.contains("variant_1.m3u8"));
    }

    #[test]
    fn test_media_playlist_generation() {
        let builder = MediaPlaylistBuilder::new(Duration::from_secs(6)).with_end_list();

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

        assert!(playlist.contains("#EXTM3U"));
        assert!(playlist.contains("#EXT-X-TARGETDURATION:7"));
        assert!(playlist.contains("#EXT-X-ENDLIST"));
    }

    #[test]
    fn test_variant_from_bitrate_entry() {
        let entry = BitrateEntry::new(1_000_000, 1280, 720, "av1");
        let variant = variant_from_bitrate_entry(&entry, "variant.m3u8".to_string())
            .expect("should succeed in test");

        assert_eq!(variant.bandwidth, 1_000_000);
        assert_eq!(variant.resolution, Some((1280, 720)));
    }
}