transmux 0.24.0

Any-to-any media container muxing hub: demux TS, fMP4/CMAF, MPEG-PS, WebM, FLV, or RTMP into one neutral IR and mux to CMAF/fMP4, progressive MP4, TS, DASH, low-latency DASH, HLS, low-latency HLS, Smooth Streaming, or RTMP. CENC/CBCS encrypt+decrypt, SSAI splice, RTP/RTCP, and an fMP4/CMAF conformance validator; parses codec config headers only, samples stay opaque. no_std + alloc.
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
//! DASH `.mpd` manifest generation gate (issue #464).
//!
//! Oracle: `fixtures/dash/manifest.mpd` is a real ffmpeg-generated DASH MPD for
//! a 2-track (video + audio) CMAF. The input IR is built by `TsDemux`-ing the
//! deterministic 2-track `fixtures/ts/h264_aac.ts` (H.264 video + AAC audio)
//! and fed to [`DashPackager`].
//!
//! Every test bites: the produced XML is parsed with a std-only element walker
//! (no XML dependency) and asserted against real structure — never a bare
//! substring `contains`, and codec/geometry values are asserted against what
//! the crate itself computes, not hardcoded literals.

use std::path::PathBuf;

use broadcast_common::{Package, Parse, Unpackage};
use transmux::aac_asc::AudioSpecificConfig;
use transmux::dash::{DashPackager, MPD_NAMESPACE};
use transmux::pipeline::CodecConfig;
use transmux::sps::rfc6381_avc1;
use transmux::ts_demux::TsDemux;

// ---------------------------------------------------------------------------
// Fixtures
// ---------------------------------------------------------------------------

fn fixtures_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../fixtures")
}

fn demux_media() -> transmux::media::Media {
    let ts = std::fs::read(fixtures_dir().join("ts/h264_aac.ts"))
        .expect("h264_aac.ts fixture must exist");
    let mut demux = TsDemux::new();
    demux.unpackage(&ts[..]).expect("demux h264_aac.ts")
}

fn build_mpd() -> String {
    let media = demux_media();
    let mut pkg = DashPackager::default();
    pkg.package(&media).expect("package DASH MPD")
}

fn ref_mpd() -> String {
    std::fs::read_to_string(fixtures_dir().join("dash/manifest.mpd"))
        .expect("reference manifest.mpd must exist")
}

// ---------------------------------------------------------------------------
// Minimal XML walker — no external dependency.
// ---------------------------------------------------------------------------

/// A parsed XML element (name + attributes + children). Text content is ignored
/// (the MPD carries none we assert on).
#[derive(Debug, Clone)]
struct Element {
    name: String,
    attrs: Vec<(String, String)>,
    children: Vec<Element>,
}

impl Element {
    fn attr(&self, key: &str) -> Option<&str> {
        self.attrs
            .iter()
            .find(|(k, _)| k == key)
            .map(|(_, v)| v.as_str())
    }
    fn has_attr(&self, key: &str) -> bool {
        self.attrs.iter().any(|(k, _)| k == key)
    }
    /// Direct children with the given tag name.
    fn find_all<'a>(&'a self, name: &str) -> Vec<&'a Element> {
        self.children.iter().filter(|c| c.name == name).collect()
    }
    fn find<'a>(&'a self, name: &str) -> Option<&'a Element> {
        self.children.iter().find(|c| c.name == name)
    }
    /// Recursive descendant search (first match, DFS).
    fn descendant<'a>(&'a self, name: &str) -> Option<&'a Element> {
        for c in &self.children {
            if c.name == name {
                return Some(c);
            }
            if let Some(d) = c.descendant(name) {
                return Some(d);
            }
        }
        None
    }
    /// Collect the names of every element in the tree (self + descendants).
    fn all_names(&self, out: &mut std::collections::BTreeSet<String>) {
        out.insert(self.name.clone());
        for c in &self.children {
            c.all_names(out);
        }
    }
}

/// A hand-rolled recursive-descent XML parser sufficient for the MPD structure:
/// the `<?xml?>` decl, elements, attributes (double-quoted values), self-closing
/// tags, and nesting. Not general-purpose (no entities, CDATA, comments) but the
/// MPD we produce and the ffmpeg oracle both stay within this grammar.
struct XmlParser<'a> {
    s: &'a [u8],
    pos: usize,
}

impl<'a> XmlParser<'a> {
    fn new(s: &'a str) -> Self {
        Self {
            s: s.as_bytes(),
            pos: 0,
        }
    }

    fn parse_document(&mut self) -> Element {
        self.skip_ws();
        // Optional <?xml ...?> declaration.
        if self.starts_with("<?") {
            while self.pos < self.s.len() && !self.starts_with("?>") {
                self.pos += 1;
            }
            self.pos += 2; // consume "?>"
        }
        self.skip_ws();
        self.parse_element().expect("root element")
    }

    fn parse_element(&mut self) -> Option<Element> {
        self.skip_ws();
        // Skip comments / processing instructions between siblings.
        while self.starts_with("<!") || self.starts_with("<?") {
            while self.pos < self.s.len() && self.cur() != b'>' {
                self.pos += 1;
            }
            self.pos += 1;
            self.skip_ws();
        }
        if !self.starts_with("<") || self.starts_with("</") {
            return None;
        }
        self.pos += 1; // '<'
        let name = self.read_name();
        let mut attrs = Vec::new();
        loop {
            self.skip_ws();
            match self.cur() {
                b'/' => {
                    // self-closing
                    self.pos += 1; // '/'
                    self.expect(b'>');
                    return Some(Element {
                        name,
                        attrs,
                        children: Vec::new(),
                    });
                }
                b'>' => {
                    self.pos += 1;
                    break;
                }
                _ => {
                    let key = self.read_name();
                    self.skip_ws();
                    self.expect(b'=');
                    self.skip_ws();
                    let value = self.read_quoted();
                    attrs.push((key, value));
                }
            }
        }
        // Children until the matching close tag.
        let mut children = Vec::new();
        loop {
            self.skip_ws();
            self.skip_text();
            self.skip_ws();
            if self.starts_with("</") {
                self.pos += 2;
                let _close = self.read_name();
                self.skip_ws();
                self.expect(b'>');
                break;
            }
            match self.parse_element() {
                Some(c) => children.push(c),
                None => {
                    if self.pos >= self.s.len() {
                        break;
                    }
                }
            }
        }
        Some(Element {
            name,
            attrs,
            children,
        })
    }

    fn skip_text(&mut self) {
        while self.pos < self.s.len() && self.cur() != b'<' {
            self.pos += 1;
        }
    }

    fn cur(&self) -> u8 {
        if self.pos < self.s.len() {
            self.s[self.pos]
        } else {
            0
        }
    }

    fn starts_with(&self, tok: &str) -> bool {
        self.s[self.pos.min(self.s.len())..].starts_with(tok.as_bytes())
    }

    fn skip_ws(&mut self) {
        while self.pos < self.s.len() && self.s[self.pos].is_ascii_whitespace() {
            self.pos += 1;
        }
    }

    fn expect(&mut self, b: u8) {
        assert_eq!(
            self.cur(),
            b,
            "expected '{}' at pos {}",
            b as char,
            self.pos
        );
        self.pos += 1;
    }

    fn read_name(&mut self) -> String {
        let start = self.pos;
        while self.pos < self.s.len() {
            let c = self.s[self.pos];
            if c.is_ascii_whitespace() || c == b'=' || c == b'>' || c == b'/' {
                break;
            }
            self.pos += 1;
        }
        String::from_utf8_lossy(&self.s[start..self.pos]).into_owned()
    }

    fn read_quoted(&mut self) -> String {
        let q = self.cur();
        assert!(q == b'"' || q == b'\'', "attribute value must be quoted");
        self.pos += 1;
        let start = self.pos;
        while self.pos < self.s.len() && self.cur() != q {
            self.pos += 1;
        }
        let raw = String::from_utf8_lossy(&self.s[start..self.pos]).into_owned();
        self.pos += 1; // closing quote
        unescape(&raw)
    }
}

fn unescape(s: &str) -> String {
    s.replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&quot;", "\"")
        .replace("&apos;", "'")
        .replace("&amp;", "&")
}

fn parse_xml(s: &str) -> Element {
    XmlParser::new(s).parse_document()
}

// ---------------------------------------------------------------------------
// Oracle helpers — compute the expected codec/geometry values from the IR.
// ---------------------------------------------------------------------------

/// The expected RFC 6381 codec string + SPS dimensions for the video track.
fn expected_video(media: &transmux::media::Media) -> (String, u32, u32) {
    let vid = media
        .tracks
        .iter()
        .find(|t| matches!(t.spec.config, CodecConfig::Avc { .. }))
        .expect("video track");
    match &vid.spec.config {
        CodecConfig::Avc { config, .. } => {
            let codecs = rfc6381_avc1(
                config.config.profile_indication,
                config.config.profile_compatibility,
                config.config.level_indication,
            );
            let sps = config.config.sps.first().expect("SPS");
            let info = sps.decode().expect("decode SPS");
            (codecs, info.width, info.height)
        }
        _ => unreachable!(),
    }
}

/// The expected audio RFC 6381 codec + sampling rate.
fn expected_audio(media: &transmux::media::Media) -> (String, u32) {
    let aud = media
        .tracks
        .iter()
        .find(|t| matches!(t.spec.config, CodecConfig::Aac { .. }))
        .expect("audio track");
    match &aud.spec.config {
        CodecConfig::Aac {
            esds, sample_rate, ..
        } => {
            let dsi = esds
                .es_descriptor
                .decoder_config
                .as_ref()
                .and_then(|dc| dc.decoder_specific_info.as_ref())
                .expect("ASC in esds");
            let asc = AudioSpecificConfig::parse(&dsi.data).expect("parse ASC");
            let rate = asc.sampling_frequency.unwrap_or(*sample_rate);
            (asc.rfc6381(), rate)
        }
        _ => unreachable!(),
    }
}

// ---------------------------------------------------------------------------
// Test 1 — well-formed + schema shape.
// ---------------------------------------------------------------------------

#[test]
fn well_formed_and_schema_shape() {
    let xml = build_mpd();
    let root = parse_xml(&xml);

    assert_eq!(root.name, "MPD", "root element must be MPD");
    assert_eq!(
        root.attr("xmlns"),
        Some(MPD_NAMESPACE),
        "MPD must carry the DASH namespace"
    );
    assert!(
        root.has_attr("profiles"),
        "MPD must carry a profiles attribute"
    );

    let periods = root.find_all("Period");
    assert_eq!(periods.len(), 1, "exactly one Period");
    let period = periods[0];

    let sets = period.find_all("AdaptationSet");
    assert_eq!(sets.len(), 2, "exactly two AdaptationSets (video + audio)");

    // One video/mp4 and one audio/mp4 set, each with exactly one Representation.
    let mimes: std::collections::BTreeSet<&str> =
        sets.iter().filter_map(|s| s.attr("mimeType")).collect();
    assert!(mimes.contains("video/mp4"), "a video/mp4 AdaptationSet");
    assert!(mimes.contains("audio/mp4"), "an audio/mp4 AdaptationSet");

    for s in &sets {
        assert_eq!(
            s.find_all("Representation").len(),
            1,
            "each AdaptationSet has exactly one Representation"
        );
    }
}

// ---------------------------------------------------------------------------
// Test 2 — codec strings correct (against the crate's own computation).
// ---------------------------------------------------------------------------

#[test]
fn codec_strings_match_crate_computation() {
    let media = demux_media();
    let (want_video_codecs, _, _) = expected_video(&media);
    let (want_audio_codecs, _) = expected_audio(&media);
    assert_eq!(
        want_audio_codecs, "mp4a.40.2",
        "AAC-LC audio codec string sanity"
    );

    let xml = build_mpd();
    let root = parse_xml(&xml);
    let period = root.find("Period").unwrap();

    let video_set = period
        .find_all("AdaptationSet")
        .into_iter()
        .find(|s| s.attr("mimeType") == Some("video/mp4"))
        .expect("video set");
    let audio_set = period
        .find_all("AdaptationSet")
        .into_iter()
        .find(|s| s.attr("mimeType") == Some("audio/mp4"))
        .expect("audio set");

    let video_repr = video_set.find("Representation").unwrap();
    let audio_repr = audio_set.find("Representation").unwrap();

    assert_eq!(
        video_repr.attr("codecs"),
        Some(want_video_codecs.as_str()),
        "video @codecs must equal the crate's rfc6381_avc1 output"
    );
    assert_eq!(
        audio_repr.attr("codecs"),
        Some(want_audio_codecs.as_str()),
        "audio @codecs must equal the crate's ASC rfc6381 output"
    );

    // mimeType present + correct on the AdaptationSet.
    assert_eq!(video_set.attr("mimeType"), Some("video/mp4"));
    assert_eq!(audio_set.attr("mimeType"), Some("audio/mp4"));
}

// ---------------------------------------------------------------------------
// Test 3 — SegmentTemplate structure matches the real reference MPD.
// ---------------------------------------------------------------------------

#[test]
fn segment_template_structure_matches_reference() {
    let ours = parse_xml(&build_mpd());
    let reference = parse_xml(&ref_mpd());

    // The reference MPD carries these structural elements; ours must too.
    let mut ref_names = std::collections::BTreeSet::new();
    reference.all_names(&mut ref_names);
    let mut our_names = std::collections::BTreeSet::new();
    ours.all_names(&mut our_names);

    for e in [
        "MPD",
        "Period",
        "AdaptationSet",
        "Representation",
        "SegmentTemplate",
    ] {
        assert!(
            ref_names.contains(e),
            "reference MPD unexpectedly lacks <{e}>"
        );
        assert!(our_names.contains(e), "our MPD lacks <{e}>");
    }

    // SegmentTemplate attribute-presence must match the reference set.
    let ref_st = reference
        .descendant("SegmentTemplate")
        .expect("reference SegmentTemplate");
    let our_st = ours
        .descendant("SegmentTemplate")
        .expect("our SegmentTemplate");

    for attr in ["timescale", "startNumber", "initialization", "media"] {
        assert!(
            ref_st.has_attr(attr),
            "reference SegmentTemplate lacks @{attr}"
        );
        assert!(our_st.has_attr(attr), "our SegmentTemplate lacks @{attr}");
    }
    // We additionally carry @duration (number+duration addressing); require it.
    assert!(
        our_st.has_attr("duration"),
        "our SegmentTemplate must carry @duration"
    );

    // Templates must be addressing templates ($Number$ or $RepresentationID$).
    let init = our_st.attr("initialization").unwrap();
    let media = our_st.attr("media").unwrap();
    assert!(
        init.contains("$RepresentationID$"),
        "initialization template must reference $RepresentationID$: {init}"
    );
    assert!(
        media.contains("$Number$") && media.contains("$RepresentationID$"),
        "media template must reference $Number$ and $RepresentationID$: {media}"
    );

    // timescale/startNumber must be positive integers.
    let ts: u64 = our_st
        .attr("timescale")
        .unwrap()
        .parse()
        .expect("timescale int");
    let sn: u64 = our_st
        .attr("startNumber")
        .unwrap()
        .parse()
        .expect("startNumber int");
    assert!(ts > 0, "timescale must be positive");
    assert!(sn >= 1, "startNumber must be >= 1");
}

// ---------------------------------------------------------------------------
// Test 4 — video geometry + audio params against decoded values.
// ---------------------------------------------------------------------------

#[test]
fn video_geometry_and_audio_params() {
    let media = demux_media();
    let (_, sps_w, sps_h) = expected_video(&media);
    let (_, asc_rate) = expected_audio(&media);

    let root = parse_xml(&build_mpd());
    let period = root.find("Period").unwrap();

    let video_repr = period
        .find_all("AdaptationSet")
        .into_iter()
        .find(|s| s.attr("mimeType") == Some("video/mp4"))
        .unwrap()
        .find("Representation")
        .unwrap();
    let audio_set = period
        .find_all("AdaptationSet")
        .into_iter()
        .find(|s| s.attr("mimeType") == Some("audio/mp4"))
        .unwrap();
    let audio_repr = audio_set.find("Representation").unwrap();

    let w: u32 = video_repr.attr("width").expect("@width").parse().unwrap();
    let h: u32 = video_repr.attr("height").expect("@height").parse().unwrap();
    assert_eq!(w, sps_w, "video @width must match SPS-decoded width");
    assert_eq!(h, sps_h, "video @height must match SPS-decoded height");

    let rate: u32 = audio_repr
        .attr("audioSamplingRate")
        .expect("@audioSamplingRate")
        .parse()
        .unwrap();
    assert_eq!(rate, asc_rate, "audio @audioSamplingRate must match ASC");

    // AudioChannelConfiguration present with the DASH scheme + a value.
    let acc = audio_repr
        .find("AudioChannelConfiguration")
        .expect("AudioChannelConfiguration");
    assert_eq!(
        acc.attr("schemeIdUri"),
        Some("urn:mpeg:dash:23003:3:audio_channel_configuration:2011")
    );
    let ch: u32 = acc.attr("value").expect("channel value").parse().unwrap();
    assert!(ch >= 1, "channel count must be >= 1");
}

// ---------------------------------------------------------------------------
// Test 5 — bandwidth present + positive on every Representation.
// ---------------------------------------------------------------------------

#[test]
fn bandwidth_present_and_positive() {
    let root = parse_xml(&build_mpd());
    let period = root.find("Period").unwrap();

    let mut reprs = 0usize;
    for set in period.find_all("AdaptationSet") {
        for repr in set.find_all("Representation") {
            reprs += 1;
            let bw: u64 = repr
                .attr("bandwidth")
                .expect("every Representation has @bandwidth")
                .parse()
                .expect("bandwidth must be an integer");
            assert!(bw > 0, "@bandwidth must be a positive integer, got {bw}");
        }
    }
    assert_eq!(reprs, 2, "two Representations total");
}

// ---------------------------------------------------------------------------
// Empty media is rejected.
// ---------------------------------------------------------------------------

#[test]
fn empty_media_rejected() {
    let media = transmux::media::Media::new(vec![], 90_000);
    let mut pkg = DashPackager::default();
    assert!(pkg.package(&media).is_err(), "empty Media must not package");
}