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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
// Copyright 2024 OxiMedia Project
// Licensed under the Apache License, Version 2.0

//! Multi-audio track support for adaptive streaming packaging.
//!
//! This module provides `AudioTrackDescriptor` with language/accessibility
//! labels, and generates multi-audio variant entries for HLS and DASH
//! manifests.
//!
//! # HLS Multi-Audio
//!
//! HLS uses `EXT-X-MEDIA` tags with `TYPE=AUDIO` in the multivariant playlist
//! to declare alternate audio renditions. Each `EXT-X-STREAM-INF` then
//! references an `AUDIO` group containing the available languages.
//!
//! # DASH Multi-Audio
//!
//! DASH uses separate `<AdaptationSet>` elements for each audio track,
//! distinguished by `@lang` and `<Accessibility>` descriptors.

use crate::error::{PackagerError, PackagerResult};
use std::time::Duration;

// ---------------------------------------------------------------------------
// AudioRole
// ---------------------------------------------------------------------------

/// The role of an audio track, per DASH Role scheme and HLS CHARACTERISTICS.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AudioRole {
    /// Main dialogue track.
    Main,
    /// Alternate language dubbing.
    Dub,
    /// Audio description for visually impaired (AD).
    Description,
    /// Commentary track.
    Commentary,
    /// Emergency information.
    Emergency,
    /// Supplementary audio (e.g. director's commentary).
    Supplementary,
}

impl AudioRole {
    /// DASH `<Role>` scheme value.
    #[must_use]
    pub fn dash_role_value(self) -> &'static str {
        match self {
            Self::Main => "main",
            Self::Dub => "dub",
            Self::Description => "description",
            Self::Commentary => "commentary",
            Self::Emergency => "emergency",
            Self::Supplementary => "supplementary",
        }
    }

    /// HLS `CHARACTERISTICS` attribute value (from RFC 8216).
    #[must_use]
    pub fn hls_characteristics(self) -> Option<&'static str> {
        match self {
            Self::Description => Some("public.accessibility.describes-video"),
            Self::Commentary => Some("public.accessibility.transcribes-spoken-dialog"),
            _ => None,
        }
    }
}

// ---------------------------------------------------------------------------
// AudioCodecInfo
// ---------------------------------------------------------------------------

/// Audio codec information for manifest generation.
#[derive(Debug, Clone)]
pub struct AudioCodecInfo {
    /// Codec identifier string for manifests (e.g. `"opus"`, `"flac"`, `"vorbis"`).
    pub codecs_string: String,
    /// Sample rate in Hz.
    pub sample_rate: u32,
    /// Number of audio channels.
    pub channels: u32,
}

impl AudioCodecInfo {
    /// Create a new audio codec info.
    #[must_use]
    pub fn new(codecs_string: impl Into<String>, sample_rate: u32, channels: u32) -> Self {
        Self {
            codecs_string: codecs_string.into(),
            sample_rate,
            channels,
        }
    }

    /// Create Opus codec info.
    #[must_use]
    pub fn opus(sample_rate: u32, channels: u32) -> Self {
        Self::new("opus", sample_rate, channels)
    }

    /// Create FLAC codec info.
    #[must_use]
    pub fn flac(sample_rate: u32, channels: u32) -> Self {
        Self::new("fLaC", sample_rate, channels)
    }

    /// Create Vorbis codec info.
    #[must_use]
    pub fn vorbis(sample_rate: u32, channels: u32) -> Self {
        Self::new("vorbis", sample_rate, channels)
    }
}

// ---------------------------------------------------------------------------
// AudioTrackDescriptor
// ---------------------------------------------------------------------------

/// Describes a single audio track for multi-audio packaging.
///
/// Each descriptor maps to:
/// - One HLS `EXT-X-MEDIA` tag (TYPE=AUDIO)
/// - One DASH `<AdaptationSet>` with `@lang` and `<Role>`
#[derive(Debug, Clone)]
pub struct AudioTrackDescriptor {
    /// Unique identifier (used in GROUP-ID and @id).
    pub id: String,
    /// BCP 47 language tag (e.g. `"en"`, `"ja"`, `"fr-CA"`).
    pub language: String,
    /// Human-readable label (e.g. `"English"`, `"Japanese"`).
    pub label: String,
    /// Codec information.
    pub codec: AudioCodecInfo,
    /// Bitrate in bits per second.
    pub bitrate: u64,
    /// Role of this audio track.
    pub role: AudioRole,
    /// Whether this is the default audio selection.
    pub is_default: bool,
    /// Whether this track should auto-select.
    pub autoselect: bool,
    /// URI of the media playlist for this audio track.
    pub playlist_uri: String,
    /// Init segment URI (for fMP4/CMAF).
    pub init_uri: Option<String>,
    /// Segment duration.
    pub segment_duration: Duration,
    /// Accessibility labels (for hearing impaired, etc.).
    pub accessibility_labels: Vec<String>,
}

impl AudioTrackDescriptor {
    /// Create a new audio track descriptor.
    #[must_use]
    pub fn new(
        id: impl Into<String>,
        language: impl Into<String>,
        label: impl Into<String>,
        codec: AudioCodecInfo,
        bitrate: u64,
    ) -> Self {
        Self {
            id: id.into(),
            language: language.into(),
            label: label.into(),
            codec,
            bitrate,
            role: AudioRole::Main,
            is_default: false,
            autoselect: true,
            playlist_uri: String::new(),
            init_uri: None,
            segment_duration: Duration::from_secs(6),
            accessibility_labels: Vec::new(),
        }
    }

    /// Set the track role.
    #[must_use]
    pub fn with_role(mut self, role: AudioRole) -> Self {
        self.role = role;
        self
    }

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

    /// Set the playlist URI.
    #[must_use]
    pub fn with_playlist_uri(mut self, uri: impl Into<String>) -> Self {
        self.playlist_uri = uri.into();
        self
    }

    /// Set the init segment URI.
    #[must_use]
    pub fn with_init_uri(mut self, uri: impl Into<String>) -> Self {
        self.init_uri = Some(uri.into());
        self
    }

    /// Add an accessibility label.
    #[must_use]
    pub fn with_accessibility(mut self, label: impl Into<String>) -> Self {
        self.accessibility_labels.push(label.into());
        self
    }

    /// Render as an HLS `EXT-X-MEDIA` tag for the multivariant playlist.
    #[must_use]
    pub fn to_hls_media_tag(&self, group_id: &str) -> String {
        let mut attrs = Vec::new();
        attrs.push("TYPE=AUDIO".to_string());
        attrs.push(format!("GROUP-ID=\"{group_id}\""));
        attrs.push(format!("NAME=\"{}\"", self.label));
        attrs.push(format!("LANGUAGE=\"{}\"", self.language));

        if self.is_default {
            attrs.push("DEFAULT=YES".to_string());
        } else {
            attrs.push("DEFAULT=NO".to_string());
        }

        if self.autoselect {
            attrs.push("AUTOSELECT=YES".to_string());
        }

        if let Some(chars) = self.role.hls_characteristics() {
            attrs.push(format!("CHARACTERISTICS=\"{chars}\""));
        } else if !self.accessibility_labels.is_empty() {
            attrs.push(format!(
                "CHARACTERISTICS=\"{}\"",
                self.accessibility_labels.join(",")
            ));
        }

        attrs.push(format!("CHANNELS=\"{}\"", self.codec.channels));

        if !self.playlist_uri.is_empty() {
            attrs.push(format!("URI=\"{}\"", self.playlist_uri));
        }

        format!("#EXT-X-MEDIA:{}", attrs.join(","))
    }

    /// Render as a DASH `<AdaptationSet>` XML fragment.
    #[must_use]
    pub fn to_dash_adaptation_set(&self) -> String {
        let mut xml = String::new();
        xml.push_str(&format!(
            r#"<AdaptationSet id="{}" contentType="audio" lang="{}" mimeType="audio/mp4">"#,
            self.id, self.language
        ));
        xml.push_str(&format!(
            r#"<Role schemeIdUri="urn:mpeg:dash:role:2011" value="{}"/>"#,
            self.role.dash_role_value()
        ));

        for label in &self.accessibility_labels {
            xml.push_str(&format!(
                r#"<Accessibility schemeIdUri="urn:mpeg:dash:role:2011" value="{label}"/>"#
            ));
        }

        xml.push_str(&format!(
            r#"<Representation id="{}_rep" bandwidth="{}" codecs="{}" audioSamplingRate="{}"><AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="{}"/></Representation>"#,
            self.id,
            self.bitrate,
            self.codec.codecs_string,
            self.codec.sample_rate,
            self.codec.channels
        ));

        xml.push_str("</AdaptationSet>");
        xml
    }

    /// Validate the descriptor.
    ///
    /// # Errors
    ///
    /// Returns an error if required fields are missing or invalid.
    pub fn validate(&self) -> PackagerResult<()> {
        if self.id.is_empty() {
            return Err(PackagerError::InvalidConfig(
                "Audio track ID must not be empty".into(),
            ));
        }
        if self.language.is_empty() {
            return Err(PackagerError::InvalidConfig(
                "Audio track language must not be empty".into(),
            ));
        }
        if self.bitrate == 0 {
            return Err(PackagerError::InvalidConfig(
                "Audio track bitrate must not be zero".into(),
            ));
        }
        if self.codec.sample_rate == 0 {
            return Err(PackagerError::InvalidConfig(
                "Audio track sample rate must not be zero".into(),
            ));
        }
        if self.codec.channels == 0 {
            return Err(PackagerError::InvalidConfig(
                "Audio track channel count must not be zero".into(),
            ));
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// MultiAudioSet
// ---------------------------------------------------------------------------

/// A collection of audio tracks forming a multi-audio presentation.
///
/// Used to generate the audio portion of HLS multivariant playlists
/// and DASH MPD documents.
#[derive(Debug, Clone, Default)]
pub struct MultiAudioSet {
    /// Audio group ID (used in HLS GROUP-ID).
    pub group_id: String,
    /// Audio track descriptors.
    tracks: Vec<AudioTrackDescriptor>,
}

impl MultiAudioSet {
    /// Create a new multi-audio set with the given group ID.
    #[must_use]
    pub fn new(group_id: impl Into<String>) -> Self {
        Self {
            group_id: group_id.into(),
            tracks: Vec::new(),
        }
    }

    /// Add an audio track.
    pub fn add_track(&mut self, track: AudioTrackDescriptor) {
        self.tracks.push(track);
    }

    /// Return all tracks.
    #[must_use]
    pub fn tracks(&self) -> &[AudioTrackDescriptor] {
        &self.tracks
    }

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

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

    /// Return the default track, if one is set.
    #[must_use]
    pub fn default_track(&self) -> Option<&AudioTrackDescriptor> {
        self.tracks.iter().find(|t| t.is_default)
    }

    /// Return tracks for a given language.
    #[must_use]
    pub fn tracks_for_language(&self, lang: &str) -> Vec<&AudioTrackDescriptor> {
        self.tracks.iter().filter(|t| t.language == lang).collect()
    }

    /// Return all unique languages in the set.
    #[must_use]
    pub fn languages(&self) -> Vec<&str> {
        let mut langs: Vec<&str> = self.tracks.iter().map(|t| t.language.as_str()).collect();
        langs.sort();
        langs.dedup();
        langs
    }

    /// Render all tracks as HLS `EXT-X-MEDIA` tags.
    #[must_use]
    pub fn to_hls_media_tags(&self) -> String {
        let mut out = String::new();
        for track in &self.tracks {
            out.push_str(&track.to_hls_media_tag(&self.group_id));
            out.push('\n');
        }
        out
    }

    /// Render all tracks as DASH `<AdaptationSet>` XML fragments.
    #[must_use]
    pub fn to_dash_adaptation_sets(&self) -> String {
        let mut out = String::new();
        for track in &self.tracks {
            out.push_str(&track.to_dash_adaptation_set());
            out.push('\n');
        }
        out
    }

    /// Validate all tracks in the set.
    ///
    /// # Errors
    ///
    /// Returns the first validation error encountered.
    pub fn validate(&self) -> PackagerResult<()> {
        if self.group_id.is_empty() {
            return Err(PackagerError::InvalidConfig(
                "Audio group ID must not be empty".into(),
            ));
        }
        // Check for duplicate IDs
        for (i, track) in self.tracks.iter().enumerate() {
            track.validate()?;
            for other in &self.tracks[i + 1..] {
                if track.id == other.id {
                    return Err(PackagerError::InvalidConfig(format!(
                        "Duplicate audio track ID: {}",
                        track.id
                    )));
                }
            }
        }
        // Check at most one default
        let default_count = self.tracks.iter().filter(|t| t.is_default).count();
        if default_count > 1 {
            return Err(PackagerError::InvalidConfig(
                "At most one audio track can be the default".into(),
            ));
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn opus_codec() -> AudioCodecInfo {
        AudioCodecInfo::opus(48_000, 2)
    }

    fn flac_codec() -> AudioCodecInfo {
        AudioCodecInfo::flac(44_100, 2)
    }

    // --- AudioRole ----------------------------------------------------------

    #[test]
    fn test_audio_role_dash_values() {
        assert_eq!(AudioRole::Main.dash_role_value(), "main");
        assert_eq!(AudioRole::Dub.dash_role_value(), "dub");
        assert_eq!(AudioRole::Description.dash_role_value(), "description");
        assert_eq!(AudioRole::Commentary.dash_role_value(), "commentary");
        assert_eq!(AudioRole::Emergency.dash_role_value(), "emergency");
        assert_eq!(AudioRole::Supplementary.dash_role_value(), "supplementary");
    }

    #[test]
    fn test_audio_role_hls_characteristics() {
        assert!(AudioRole::Description.hls_characteristics().is_some());
        assert!(AudioRole::Commentary.hls_characteristics().is_some());
        assert!(AudioRole::Main.hls_characteristics().is_none());
        assert!(AudioRole::Dub.hls_characteristics().is_none());
    }

    // --- AudioCodecInfo -----------------------------------------------------

    #[test]
    fn test_opus_codec() {
        let c = AudioCodecInfo::opus(48_000, 2);
        assert_eq!(c.codecs_string, "opus");
        assert_eq!(c.sample_rate, 48_000);
        assert_eq!(c.channels, 2);
    }

    #[test]
    fn test_flac_codec() {
        let c = AudioCodecInfo::flac(96_000, 6);
        assert_eq!(c.codecs_string, "fLaC");
        assert_eq!(c.sample_rate, 96_000);
        assert_eq!(c.channels, 6);
    }

    #[test]
    fn test_vorbis_codec() {
        let c = AudioCodecInfo::vorbis(44_100, 2);
        assert_eq!(c.codecs_string, "vorbis");
    }

    // --- AudioTrackDescriptor -----------------------------------------------

    #[test]
    fn test_audio_track_new() {
        let t = AudioTrackDescriptor::new("en-main", "en", "English", opus_codec(), 128_000);
        assert_eq!(t.id, "en-main");
        assert_eq!(t.language, "en");
        assert_eq!(t.label, "English");
        assert_eq!(t.bitrate, 128_000);
        assert_eq!(t.role, AudioRole::Main);
        assert!(!t.is_default);
    }

    #[test]
    fn test_audio_track_with_role() {
        let t = AudioTrackDescriptor::new("ad-en", "en", "Audio Description", opus_codec(), 64_000)
            .with_role(AudioRole::Description);
        assert_eq!(t.role, AudioRole::Description);
    }

    #[test]
    fn test_audio_track_as_default() {
        let t =
            AudioTrackDescriptor::new("en", "en", "English", opus_codec(), 128_000).as_default();
        assert!(t.is_default);
    }

    #[test]
    fn test_audio_track_hls_media_tag() {
        let t = AudioTrackDescriptor::new("en-main", "en", "English", opus_codec(), 128_000)
            .as_default()
            .with_playlist_uri("audio/en/index.m3u8");

        let tag = t.to_hls_media_tag("audio-group");
        assert!(tag.contains("EXT-X-MEDIA"));
        assert!(tag.contains("TYPE=AUDIO"));
        assert!(tag.contains("GROUP-ID=\"audio-group\""));
        assert!(tag.contains("NAME=\"English\""));
        assert!(tag.contains("LANGUAGE=\"en\""));
        assert!(tag.contains("DEFAULT=YES"));
        assert!(tag.contains("CHANNELS=\"2\""));
        assert!(tag.contains("URI=\"audio/en/index.m3u8\""));
    }

    #[test]
    fn test_audio_track_hls_media_tag_non_default() {
        let t = AudioTrackDescriptor::new("ja", "ja", "Japanese", opus_codec(), 128_000)
            .with_playlist_uri("audio/ja/index.m3u8");

        let tag = t.to_hls_media_tag("audio-group");
        assert!(tag.contains("DEFAULT=NO"));
        assert!(tag.contains("LANGUAGE=\"ja\""));
    }

    #[test]
    fn test_audio_track_hls_with_accessibility() {
        let t = AudioTrackDescriptor::new("ad", "en", "Audio Description", opus_codec(), 64_000)
            .with_role(AudioRole::Description)
            .with_playlist_uri("audio/ad/index.m3u8");

        let tag = t.to_hls_media_tag("audio-group");
        assert!(tag.contains("CHARACTERISTICS=\"public.accessibility.describes-video\""));
    }

    #[test]
    fn test_audio_track_dash_adaptation_set() {
        let t = AudioTrackDescriptor::new("en-main", "en", "English", opus_codec(), 128_000);

        let xml = t.to_dash_adaptation_set();
        assert!(xml.contains("<AdaptationSet"));
        assert!(xml.contains("lang=\"en\""));
        assert!(xml.contains("contentType=\"audio\""));
        assert!(xml.contains("value=\"main\""));
        assert!(xml.contains("bandwidth=\"128000\""));
        assert!(xml.contains("codecs=\"opus\""));
        assert!(xml.contains("audioSamplingRate=\"48000\""));
        assert!(xml.contains("</AdaptationSet>"));
    }

    #[test]
    fn test_audio_track_validate_ok() {
        let t = AudioTrackDescriptor::new("en", "en", "English", opus_codec(), 128_000);
        assert!(t.validate().is_ok());
    }

    #[test]
    fn test_audio_track_validate_empty_id() {
        let t = AudioTrackDescriptor::new("", "en", "English", opus_codec(), 128_000);
        assert!(t.validate().is_err());
    }

    #[test]
    fn test_audio_track_validate_empty_language() {
        let t = AudioTrackDescriptor::new("en", "", "English", opus_codec(), 128_000);
        assert!(t.validate().is_err());
    }

    #[test]
    fn test_audio_track_validate_zero_bitrate() {
        let t = AudioTrackDescriptor::new("en", "en", "English", opus_codec(), 0);
        assert!(t.validate().is_err());
    }

    #[test]
    fn test_audio_track_validate_zero_sample_rate() {
        let c = AudioCodecInfo::new("opus", 0, 2);
        let t = AudioTrackDescriptor::new("en", "en", "English", c, 128_000);
        assert!(t.validate().is_err());
    }

    #[test]
    fn test_audio_track_validate_zero_channels() {
        let c = AudioCodecInfo::new("opus", 48_000, 0);
        let t = AudioTrackDescriptor::new("en", "en", "English", c, 128_000);
        assert!(t.validate().is_err());
    }

    // --- MultiAudioSet ------------------------------------------------------

    #[test]
    fn test_multi_audio_set_new() {
        let set = MultiAudioSet::new("audio-group");
        assert_eq!(set.group_id, "audio-group");
        assert!(set.is_empty());
    }

    #[test]
    fn test_multi_audio_set_add_tracks() {
        let mut set = MultiAudioSet::new("audio-group");
        set.add_track(
            AudioTrackDescriptor::new("en", "en", "English", opus_codec(), 128_000)
                .as_default()
                .with_playlist_uri("audio/en/index.m3u8"),
        );
        set.add_track(
            AudioTrackDescriptor::new("ja", "ja", "Japanese", opus_codec(), 128_000)
                .with_playlist_uri("audio/ja/index.m3u8"),
        );
        assert_eq!(set.len(), 2);
    }

    #[test]
    fn test_multi_audio_set_default_track() {
        let mut set = MultiAudioSet::new("audio-group");
        set.add_track(
            AudioTrackDescriptor::new("en", "en", "English", opus_codec(), 128_000).as_default(),
        );
        set.add_track(AudioTrackDescriptor::new(
            "ja",
            "ja",
            "Japanese",
            opus_codec(),
            128_000,
        ));
        let def = set.default_track();
        assert!(def.is_some());
        assert_eq!(def.map(|t| t.id.as_str()), Some("en"));
    }

    #[test]
    fn test_multi_audio_set_languages() {
        let mut set = MultiAudioSet::new("audio-group");
        set.add_track(AudioTrackDescriptor::new(
            "en",
            "en",
            "English",
            opus_codec(),
            128_000,
        ));
        set.add_track(AudioTrackDescriptor::new(
            "ja",
            "ja",
            "Japanese",
            opus_codec(),
            128_000,
        ));
        set.add_track(
            AudioTrackDescriptor::new("en-ad", "en", "English AD", opus_codec(), 64_000)
                .with_role(AudioRole::Description),
        );

        let langs = set.languages();
        assert_eq!(langs, vec!["en", "ja"]);
    }

    #[test]
    fn test_multi_audio_set_tracks_for_language() {
        let mut set = MultiAudioSet::new("audio-group");
        set.add_track(AudioTrackDescriptor::new(
            "en",
            "en",
            "English",
            opus_codec(),
            128_000,
        ));
        set.add_track(AudioTrackDescriptor::new(
            "ja",
            "ja",
            "Japanese",
            opus_codec(),
            128_000,
        ));

        let en = set.tracks_for_language("en");
        assert_eq!(en.len(), 1);
        assert_eq!(en[0].id, "en");
    }

    #[test]
    fn test_multi_audio_set_to_hls_media_tags() {
        let mut set = MultiAudioSet::new("audio-group");
        set.add_track(
            AudioTrackDescriptor::new("en", "en", "English", opus_codec(), 128_000)
                .as_default()
                .with_playlist_uri("audio/en.m3u8"),
        );
        set.add_track(
            AudioTrackDescriptor::new("ja", "ja", "Japanese", opus_codec(), 128_000)
                .with_playlist_uri("audio/ja.m3u8"),
        );

        let tags = set.to_hls_media_tags();
        let lines: Vec<&str> = tags.lines().collect();
        assert_eq!(lines.len(), 2);
        assert!(lines[0].contains("LANGUAGE=\"en\""));
        assert!(lines[1].contains("LANGUAGE=\"ja\""));
    }

    #[test]
    fn test_multi_audio_set_to_dash_adaptation_sets() {
        let mut set = MultiAudioSet::new("audio-group");
        set.add_track(AudioTrackDescriptor::new(
            "en",
            "en",
            "English",
            opus_codec(),
            128_000,
        ));
        set.add_track(AudioTrackDescriptor::new(
            "ja",
            "ja",
            "Japanese",
            flac_codec(),
            256_000,
        ));

        let xml = set.to_dash_adaptation_sets();
        assert!(xml.contains("lang=\"en\""));
        assert!(xml.contains("lang=\"ja\""));
        assert!(xml.contains("codecs=\"opus\""));
        assert!(xml.contains("codecs=\"fLaC\""));
    }

    #[test]
    fn test_multi_audio_set_validate_ok() {
        let mut set = MultiAudioSet::new("audio-group");
        set.add_track(
            AudioTrackDescriptor::new("en", "en", "English", opus_codec(), 128_000).as_default(),
        );
        assert!(set.validate().is_ok());
    }

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

    #[test]
    fn test_multi_audio_set_validate_duplicate_ids() {
        let mut set = MultiAudioSet::new("audio-group");
        set.add_track(AudioTrackDescriptor::new(
            "en",
            "en",
            "English",
            opus_codec(),
            128_000,
        ));
        set.add_track(AudioTrackDescriptor::new(
            "en",
            "en",
            "English 2",
            opus_codec(),
            64_000,
        ));
        assert!(set.validate().is_err());
    }

    #[test]
    fn test_multi_audio_set_validate_multiple_defaults() {
        let mut set = MultiAudioSet::new("audio-group");
        set.add_track(
            AudioTrackDescriptor::new("en", "en", "English", opus_codec(), 128_000).as_default(),
        );
        set.add_track(
            AudioTrackDescriptor::new("ja", "ja", "Japanese", opus_codec(), 128_000).as_default(),
        );
        assert!(set.validate().is_err());
    }

    #[test]
    fn test_audio_track_with_init_uri() {
        let t = AudioTrackDescriptor::new("en", "en", "English", opus_codec(), 128_000)
            .with_init_uri("audio/en/init.mp4");
        assert_eq!(t.init_uri, Some("audio/en/init.mp4".to_string()));
    }

    #[test]
    fn test_audio_track_with_custom_accessibility() {
        let t = AudioTrackDescriptor::new("en-hi", "en", "Hearing Impaired", opus_codec(), 128_000)
            .with_accessibility("public.accessibility.transcribes-spoken-dialog");
        assert_eq!(t.accessibility_labels.len(), 1);
    }
}