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
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
//! ISO/IEC 14496-30 subtitle sample entries — `stpp` (TTML/IMSC) and `wvtt` (WebVTT).
//!
//! # Sample entry types
//!
//! | Box type | Spec class                  | Handler | Doc              |
//! |----------|-----------------------------|---------|------------------|
//! | `stpp`   | XMLSubtitleSampleEntry      | `subt`  | ISO/IEC 14496-30 §7.2 |
//! | `wvtt`   | WVTTSampleEntry             | `text`  | ISO/IEC 14496-30 §9.2 |
//!
//! Both follow the `SampleEntry` base (6 reserved bytes + `data_reference_index` u16,
//! ISO/IEC 14496-12 §8.5.2) and carry no `VisualSampleEntry` or `AudioSampleEntry`
//! sub-base — they are plain subtitle sample entries.
//!
//! `stpp` appends three null-terminated strings directly after the SampleEntry header;
//! any remaining bytes are optional boxes preserved verbatim for round-trip.
//!
//! `wvtt` appends a mandatory `vttC` box (WebVTT header block string) and optional
//! further boxes. Cue samples contain `vttc` boxes with `payl`/`sttg`/`iden` children,
//! and gap samples contain `vtte` boxes — these are sample-payload types, not boxes
//! inside the sample entry, but they are provided here for completeness so callers can
//! build and parse cue sample payloads.

use crate::error::{Error, Result};
use alloc::string::String;
use alloc::vec::Vec;
use broadcast_common::{Parse, Serialize};

const BOX_HDR: usize = 8;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Parse a null-terminated C string from `buf` starting at `pos`.
/// Returns `(string, new_pos)` where `new_pos` points past the NUL byte.
fn parse_cstring(buf: &[u8], pos: usize) -> Result<(String, usize)> {
    let rest = &buf[pos..];
    let nul = rest
        .iter()
        .position(|&b| b == 0)
        .ok_or(Error::BufferTooShort {
            need: pos + rest.len() + 1,
            have: buf.len(),
            what: "null-terminated string",
        })?;
    let s = core::str::from_utf8(&rest[..nul]).map_err(|_| Error::InvalidValue {
        field: "utf8 string",
        value: 0,
        reason: "invalid UTF-8 in null-terminated string",
    })?;
    Ok((String::from(s), pos + nul + 1))
}

/// Serialized length of a null-terminated string (payload + NUL byte).
fn cstring_len(s: &str) -> usize {
    s.len() + 1
}

// ---------------------------------------------------------------------------
// XmlSubtitleSampleEntry — 'stpp' (ISO/IEC 14496-30 §7.2)
// ---------------------------------------------------------------------------

/// XML subtitle sample entry (`stpp`) — ISO/IEC 14496-30 §7.2.
///
/// Carries TTML/IMSC subtitle tracks. Samples are whole XML documents.
/// The sample entry follows `SampleEntry` (reserved `[6]` + `data_reference_index`)
/// then three null-terminated strings identifying the XML namespace family,
/// followed by optional boxes (`btrt`, etc.) preserved verbatim.
///
/// Wire layout (ISO/IEC 14496-30 §7.2):
/// ```text
/// class XMLSubtitleSampleEntry extends SampleEntry('stpp') {
///     string namespace;             // null-terminated
///     string schema_location;       // null-terminated (may be empty)
///     string auxiliary_mime_types;  // null-terminated (may be empty)
///     // optional: BitRateBox, …
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct XmlSubtitleSampleEntry {
    /// Data reference index (SampleEntry field `[6:7]`).
    pub data_reference_index: u16,
    /// Space-separated XML namespace URIs (null-terminated on wire).
    pub namespace: String,
    /// Namespace–schema location pairs, null-terminated (may be empty).
    pub schema_location: String,
    /// Auxiliary MIME types, null-terminated (may be empty).
    pub auxiliary_mime_types: String,
    /// Optional trailing boxes (e.g. `btrt`) preserved verbatim for round-trip.
    pub extra_boxes: Vec<crate::init_segment::OpaqueBox>,
}

impl XmlSubtitleSampleEntry {
    /// Create a new `stpp` sample entry with the given namespace.
    pub fn new(namespace: impl Into<String>) -> Self {
        Self {
            data_reference_index: 1,
            namespace: namespace.into(),
            schema_location: String::new(),
            auxiliary_mime_types: String::new(),
            extra_boxes: Vec::new(),
        }
    }

    /// Parse from full box bytes (including the 8-byte box header).
    pub fn bare_parse(bytes: &[u8]) -> Result<Self> {
        // Minimum: box header (8) + SampleEntry reserved+dri (8) + 3 NUL bytes = 19
        if bytes.len() < 19 {
            return Err(Error::BufferTooShort {
                need: 19,
                have: bytes.len(),
                what: "stpp",
            });
        }
        // body starts past the 8-byte box header
        let body = &bytes[8..];
        // SampleEntry: reserved(6) + data_reference_index(2)
        let data_reference_index = u16::from_be_bytes([body[6], body[7]]);
        let pos = 8usize; // into body
        let (namespace, pos) = parse_cstring(body, pos)?;
        let (schema_location, pos) = parse_cstring(body, pos)?;
        let (auxiliary_mime_types, mut pos) = parse_cstring(body, pos)?;

        // Collect optional trailing boxes verbatim
        let mut extra_boxes = Vec::new();
        while pos + BOX_HDR <= body.len() {
            let sz = u32::from_be_bytes([body[pos], body[pos + 1], body[pos + 2], body[pos + 3]])
                as usize;
            if sz < BOX_HDR {
                break;
            }
            let end = (pos + sz).min(body.len());
            let bt = [body[pos + 4], body[pos + 5], body[pos + 6], body[pos + 7]];
            let data = body[pos + BOX_HDR..end].to_vec();
            extra_boxes.push(crate::init_segment::OpaqueBox::new(bt, data));
            pos += sz;
        }

        Ok(Self {
            data_reference_index,
            namespace,
            schema_location,
            auxiliary_mime_types,
            extra_boxes,
        })
    }
}

impl<'a> Parse<'a> for XmlSubtitleSampleEntry {
    type Error = Error;
    /// Parse from full box bytes (including the 8-byte box header).
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        Self::bare_parse(bytes)
    }
}

impl Serialize for XmlSubtitleSampleEntry {
    type Error = Error;

    fn serialized_len(&self) -> usize {
        // box header (8) + reserved(6) + dri(2)
        let mut n = BOX_HDR + 8;
        // three null-terminated strings
        n += cstring_len(&self.namespace);
        n += cstring_len(&self.schema_location);
        n += cstring_len(&self.auxiliary_mime_types);
        // optional extra boxes
        for eb in &self.extra_boxes {
            n += eb.serialized_len();
        }
        n
    }

    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let need = self.serialized_len();
        if buf.len() < need {
            return Err(Error::OutputBufferTooSmall {
                need,
                have: buf.len(),
            });
        }
        let mut c = 0usize;
        // box header: size(32) + fourcc(32)
        buf[c..c + 4].copy_from_slice(&(need as u32).to_be_bytes());
        c += 4;
        buf[c..c + 4].copy_from_slice(b"stpp");
        c += 4;
        // SampleEntry: reserved(6) zeros + data_reference_index(16)
        c += 6;
        buf[c..c + 2].copy_from_slice(&self.data_reference_index.to_be_bytes());
        c += 2;
        // namespace (null-terminated)
        buf[c..c + self.namespace.len()].copy_from_slice(self.namespace.as_bytes());
        c += self.namespace.len();
        buf[c] = 0;
        c += 1;
        // schema_location (null-terminated)
        buf[c..c + self.schema_location.len()].copy_from_slice(self.schema_location.as_bytes());
        c += self.schema_location.len();
        buf[c] = 0;
        c += 1;
        // auxiliary_mime_types (null-terminated)
        buf[c..c + self.auxiliary_mime_types.len()]
            .copy_from_slice(self.auxiliary_mime_types.as_bytes());
        c += self.auxiliary_mime_types.len();
        buf[c] = 0;
        c += 1;
        // extra boxes
        for eb in &self.extra_boxes {
            c += eb.serialize_into(&mut buf[c..])?;
        }
        Ok(c)
    }
}

// ---------------------------------------------------------------------------
// WebVTT boxes — vttC, vttc, vtte, payl, sttg, iden
// ---------------------------------------------------------------------------

/// WebVTT Configuration Box (`vttC`) — ISO/IEC 14496-30 §9.2.
///
/// Contains the WebVTT file header block (e.g. `"WEBVTT"`), stored as a raw
/// byte string (no NUL terminator on wire).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct WebVttConfigurationBox {
    /// The WebVTT header block string (e.g. `"WEBVTT"`).
    pub config: String,
}

impl WebVttConfigurationBox {
    /// Construct a new `vttC` box.
    pub fn new(config: impl Into<String>) -> Self {
        Self {
            config: config.into(),
        }
    }

    /// Parse from full box bytes (including 8-byte header).
    pub fn bare_parse(bytes: &[u8]) -> Result<Self> {
        if bytes.len() < BOX_HDR {
            return Err(Error::BufferTooShort {
                need: BOX_HDR,
                have: bytes.len(),
                what: "vttC",
            });
        }
        let body = &bytes[BOX_HDR..];
        let config = core::str::from_utf8(body).map_err(|_| Error::InvalidValue {
            field: "vttC config",
            value: 0,
            reason: "invalid UTF-8 in vttC config string",
        })?;
        Ok(Self {
            config: String::from(config),
        })
    }
}

impl<'a> Parse<'a> for WebVttConfigurationBox {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        Self::bare_parse(bytes)
    }
}

impl Serialize for WebVttConfigurationBox {
    type Error = Error;

    fn serialized_len(&self) -> usize {
        BOX_HDR + self.config.len()
    }

    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let need = self.serialized_len();
        if buf.len() < need {
            return Err(Error::OutputBufferTooSmall {
                need,
                have: buf.len(),
            });
        }
        let mut c = 0usize;
        buf[c..c + 4].copy_from_slice(&(need as u32).to_be_bytes());
        c += 4;
        buf[c..c + 4].copy_from_slice(b"vttC");
        c += 4;
        buf[c..c + self.config.len()].copy_from_slice(self.config.as_bytes());
        c += self.config.len();
        Ok(c)
    }
}

/// WebVTT Cue Payload Box (`payl`) — ISO/IEC 14496-30 §9.4.
///
/// The cue text payload, stored without NUL terminator.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CuePayloadBox {
    /// The cue text.
    pub cue_text: String,
}

impl CuePayloadBox {
    /// Construct a new `payl` box.
    pub fn new(cue_text: impl Into<String>) -> Self {
        Self {
            cue_text: cue_text.into(),
        }
    }

    /// Parse from full box bytes (including 8-byte header).
    pub fn bare_parse(bytes: &[u8]) -> Result<Self> {
        if bytes.len() < BOX_HDR {
            return Err(Error::BufferTooShort {
                need: BOX_HDR,
                have: bytes.len(),
                what: "payl",
            });
        }
        let body = &bytes[BOX_HDR..];
        let s = core::str::from_utf8(body).map_err(|_| Error::InvalidValue {
            field: "payl cue_text",
            value: 0,
            reason: "invalid UTF-8 in payl cue_text",
        })?;
        Ok(Self {
            cue_text: String::from(s),
        })
    }
}

impl<'a> Parse<'a> for CuePayloadBox {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        Self::bare_parse(bytes)
    }
}

impl Serialize for CuePayloadBox {
    type Error = Error;

    fn serialized_len(&self) -> usize {
        BOX_HDR + self.cue_text.len()
    }

    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let need = self.serialized_len();
        if buf.len() < need {
            return Err(Error::OutputBufferTooSmall {
                need,
                have: buf.len(),
            });
        }
        let mut c = 0usize;
        buf[c..c + 4].copy_from_slice(&(need as u32).to_be_bytes());
        c += 4;
        buf[c..c + 4].copy_from_slice(b"payl");
        c += 4;
        buf[c..c + self.cue_text.len()].copy_from_slice(self.cue_text.as_bytes());
        c += self.cue_text.len();
        Ok(c)
    }
}

/// WebVTT Cue Settings Box (`sttg`) — ISO/IEC 14496-30 §9.4.
///
/// WebVTT cue settings string (position, alignment, etc.).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CueSettingsBox {
    /// The cue settings string.
    pub settings: String,
}

impl CueSettingsBox {
    /// Construct a new `sttg` box.
    pub fn new(settings: impl Into<String>) -> Self {
        Self {
            settings: settings.into(),
        }
    }

    /// Parse from full box bytes (including 8-byte header).
    pub fn bare_parse(bytes: &[u8]) -> Result<Self> {
        if bytes.len() < BOX_HDR {
            return Err(Error::BufferTooShort {
                need: BOX_HDR,
                have: bytes.len(),
                what: "sttg",
            });
        }
        let body = &bytes[BOX_HDR..];
        let s = core::str::from_utf8(body).map_err(|_| Error::InvalidValue {
            field: "sttg settings",
            value: 0,
            reason: "invalid UTF-8 in sttg settings",
        })?;
        Ok(Self {
            settings: String::from(s),
        })
    }
}

impl<'a> Parse<'a> for CueSettingsBox {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        Self::bare_parse(bytes)
    }
}

impl Serialize for CueSettingsBox {
    type Error = Error;

    fn serialized_len(&self) -> usize {
        BOX_HDR + self.settings.len()
    }

    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let need = self.serialized_len();
        if buf.len() < need {
            return Err(Error::OutputBufferTooSmall {
                need,
                have: buf.len(),
            });
        }
        let mut c = 0usize;
        buf[c..c + 4].copy_from_slice(&(need as u32).to_be_bytes());
        c += 4;
        buf[c..c + 4].copy_from_slice(b"sttg");
        c += 4;
        buf[c..c + self.settings.len()].copy_from_slice(self.settings.as_bytes());
        c += self.settings.len();
        Ok(c)
    }
}

/// WebVTT Cue ID Box (`iden`) — ISO/IEC 14496-30 §9.4.
///
/// Optional cue identifier string.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CueIdBox {
    /// The cue identifier.
    pub cue_id: String,
}

impl CueIdBox {
    /// Construct a new `iden` box.
    pub fn new(cue_id: impl Into<String>) -> Self {
        Self {
            cue_id: cue_id.into(),
        }
    }

    /// Parse from full box bytes (including 8-byte header).
    pub fn bare_parse(bytes: &[u8]) -> Result<Self> {
        if bytes.len() < BOX_HDR {
            return Err(Error::BufferTooShort {
                need: BOX_HDR,
                have: bytes.len(),
                what: "iden",
            });
        }
        let body = &bytes[BOX_HDR..];
        let s = core::str::from_utf8(body).map_err(|_| Error::InvalidValue {
            field: "iden cue_id",
            value: 0,
            reason: "invalid UTF-8 in iden cue_id",
        })?;
        Ok(Self {
            cue_id: String::from(s),
        })
    }
}

impl<'a> Parse<'a> for CueIdBox {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        Self::bare_parse(bytes)
    }
}

impl Serialize for CueIdBox {
    type Error = Error;

    fn serialized_len(&self) -> usize {
        BOX_HDR + self.cue_id.len()
    }

    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let need = self.serialized_len();
        if buf.len() < need {
            return Err(Error::OutputBufferTooSmall {
                need,
                have: buf.len(),
            });
        }
        let mut c = 0usize;
        buf[c..c + 4].copy_from_slice(&(need as u32).to_be_bytes());
        c += 4;
        buf[c..c + 4].copy_from_slice(b"iden");
        c += 4;
        buf[c..c + self.cue_id.len()].copy_from_slice(self.cue_id.as_bytes());
        c += self.cue_id.len();
        Ok(c)
    }
}

/// WebVTT Cue Box (`vttc`) — ISO/IEC 14496-30 §9.4.
///
/// A single presented cue. Contains a mandatory `payl` (payload) and
/// optional `sttg` (settings) and `iden` (identifier) children.
/// Used in media sample payloads, not in the sample entry itself.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct VttCueBox {
    /// Required cue payload (`payl`).
    pub payload: CuePayloadBox,
    /// Optional cue settings string (`sttg`).
    pub settings: Option<CueSettingsBox>,
    /// Optional cue identifier (`iden`).
    pub cue_id: Option<CueIdBox>,
}

impl VttCueBox {
    /// Construct a new `vttc` box with a payload.
    pub fn new(payload: impl Into<String>) -> Self {
        Self {
            payload: CuePayloadBox::new(payload),
            settings: None,
            cue_id: None,
        }
    }

    /// Parse from full box bytes (including 8-byte header).
    pub fn bare_parse(bytes: &[u8]) -> Result<Self> {
        if bytes.len() < BOX_HDR {
            return Err(Error::BufferTooShort {
                need: BOX_HDR,
                have: bytes.len(),
                what: "vttc",
            });
        }
        let body = &bytes[BOX_HDR..];
        let mut payload: Option<CuePayloadBox> = None;
        let mut settings: Option<CueSettingsBox> = None;
        let mut cue_id: Option<CueIdBox> = None;
        let mut pos = 0usize;
        while pos + BOX_HDR <= body.len() {
            let sz = u32::from_be_bytes([body[pos], body[pos + 1], body[pos + 2], body[pos + 3]])
                as usize;
            if sz < BOX_HDR {
                break;
            }
            let end = (pos + sz).min(body.len());
            let child = &body[pos..end];
            let fourcc = &child[4..8];
            match fourcc {
                b"payl" => payload = Some(CuePayloadBox::bare_parse(child)?),
                b"sttg" => settings = Some(CueSettingsBox::bare_parse(child)?),
                b"iden" => cue_id = Some(CueIdBox::bare_parse(child)?),
                _ => {} // unknown children skipped
            }
            pos += sz;
        }
        let payload = payload.ok_or(Error::BufferTooShort {
            need: 0,
            have: 0,
            what: "vttc missing required payl child",
        })?;
        Ok(Self {
            payload,
            settings,
            cue_id,
        })
    }
}

impl<'a> Parse<'a> for VttCueBox {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        Self::bare_parse(bytes)
    }
}

impl Serialize for VttCueBox {
    type Error = Error;

    fn serialized_len(&self) -> usize {
        let mut n = BOX_HDR + self.payload.serialized_len();
        if let Some(s) = &self.settings {
            n += s.serialized_len();
        }
        if let Some(i) = &self.cue_id {
            n += i.serialized_len();
        }
        n
    }

    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let need = self.serialized_len();
        if buf.len() < need {
            return Err(Error::OutputBufferTooSmall {
                need,
                have: buf.len(),
            });
        }
        let mut c = 0usize;
        buf[c..c + 4].copy_from_slice(&(need as u32).to_be_bytes());
        c += 4;
        buf[c..c + 4].copy_from_slice(b"vttc");
        c += 4;
        c += self.payload.serialize_into(&mut buf[c..])?;
        if let Some(s) = &self.settings {
            c += s.serialize_into(&mut buf[c..])?;
        }
        if let Some(i) = &self.cue_id {
            c += i.serialize_into(&mut buf[c..])?;
        }
        Ok(c)
    }
}

/// WebVTT Empty Cue Box (`vtte`) — ISO/IEC 14496-30 §9.4.
///
/// A gap sample with no active cue. Wire: 8-byte header only (size=8, type=`vtte`).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct VttEmptyCueBox;

impl VttEmptyCueBox {
    /// Parse from full box bytes (including 8-byte header).
    pub fn bare_parse(bytes: &[u8]) -> Result<Self> {
        if bytes.len() < BOX_HDR {
            return Err(Error::BufferTooShort {
                need: BOX_HDR,
                have: bytes.len(),
                what: "vtte",
            });
        }
        Ok(Self)
    }
}

impl<'a> Parse<'a> for VttEmptyCueBox {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        Self::bare_parse(bytes)
    }
}

impl Serialize for VttEmptyCueBox {
    type Error = Error;

    fn serialized_len(&self) -> usize {
        BOX_HDR
    }

    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let need = BOX_HDR;
        if buf.len() < need {
            return Err(Error::OutputBufferTooSmall {
                need,
                have: buf.len(),
            });
        }
        buf[..4].copy_from_slice(&(BOX_HDR as u32).to_be_bytes());
        buf[4..8].copy_from_slice(b"vtte");
        Ok(BOX_HDR)
    }
}

// ---------------------------------------------------------------------------
// WVTTSampleEntry — 'wvtt' (ISO/IEC 14496-30 §9.2)
// ---------------------------------------------------------------------------

/// WebVTT sample entry (`wvtt`) — ISO/IEC 14496-30 §9.2.
///
/// Carries WebVTT subtitle tracks. Samples contain `vttc` or `vtte` boxes.
/// The sample entry follows `SampleEntry` (reserved `[6]` + `data_reference_index`)
/// then a mandatory `vttC` configuration box, followed by optional boxes preserved
/// verbatim for round-trip (e.g. `vlab`, `btrt`).
///
/// Wire layout (ISO/IEC 14496-30 §9.2):
/// ```text
/// class WVTTSampleEntry extends SampleEntry('wvtt') {
///     WebVTTConfigurationBox config;  // 'vttC'
///     // optional: WebVTTSourceLabelBox 'vlab', BitRateBox 'btrt', ...
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct WvttSampleEntry {
    /// Data reference index (SampleEntry field `[6:7]`).
    pub data_reference_index: u16,
    /// WebVTT configuration box (`vttC`), mandatory.
    pub config: WebVttConfigurationBox,
    /// Optional trailing boxes preserved verbatim for round-trip.
    pub extra_boxes: Vec<crate::init_segment::OpaqueBox>,
}

impl WvttSampleEntry {
    /// Construct a new `wvtt` sample entry with the given WebVTT header config.
    pub fn new(config: impl Into<String>) -> Self {
        Self {
            data_reference_index: 1,
            config: WebVttConfigurationBox::new(config),
            extra_boxes: Vec::new(),
        }
    }

    /// Parse from full box bytes (including 8-byte header).
    pub fn bare_parse(bytes: &[u8]) -> Result<Self> {
        // Minimum: box header (8) + SampleEntry reserved+dri (8) + vttC header (8) = 24
        if bytes.len() < 24 {
            return Err(Error::BufferTooShort {
                need: 24,
                have: bytes.len(),
                what: "wvtt",
            });
        }
        let body = &bytes[BOX_HDR..];
        let data_reference_index = u16::from_be_bytes([body[6], body[7]]);

        // Walk child boxes in body[8..]
        let mut pos = 8usize;
        let mut config: Option<WebVttConfigurationBox> = None;
        let mut extra_boxes: Vec<crate::init_segment::OpaqueBox> = Vec::new();

        while pos + BOX_HDR <= body.len() {
            let sz = u32::from_be_bytes([body[pos], body[pos + 1], body[pos + 2], body[pos + 3]])
                as usize;
            if sz < BOX_HDR {
                break;
            }
            let end = (pos + sz).min(body.len());
            let child = &body[pos..end];
            let fourcc = &child[4..8];
            match fourcc {
                b"vttC" => config = Some(WebVttConfigurationBox::bare_parse(child)?),
                _ => {
                    let mut bt = [0u8; 4];
                    bt.copy_from_slice(fourcc);
                    extra_boxes.push(crate::init_segment::OpaqueBox::new(
                        bt,
                        child[BOX_HDR..].to_vec(),
                    ));
                }
            }
            pos += sz;
        }

        let config = config.ok_or(Error::BufferTooShort {
            need: 0,
            have: 0,
            what: "wvtt missing required vttC child",
        })?;

        Ok(Self {
            data_reference_index,
            config,
            extra_boxes,
        })
    }
}

impl<'a> Parse<'a> for WvttSampleEntry {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        Self::bare_parse(bytes)
    }
}

impl Serialize for WvttSampleEntry {
    type Error = Error;

    fn serialized_len(&self) -> usize {
        // box header (8) + SampleEntry reserved+dri (8) + vttC + extra boxes
        let mut n = BOX_HDR + 8 + self.config.serialized_len();
        for eb in &self.extra_boxes {
            n += eb.serialized_len();
        }
        n
    }

    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let need = self.serialized_len();
        if buf.len() < need {
            return Err(Error::OutputBufferTooSmall {
                need,
                have: buf.len(),
            });
        }
        let mut c = 0usize;
        buf[c..c + 4].copy_from_slice(&(need as u32).to_be_bytes());
        c += 4;
        buf[c..c + 4].copy_from_slice(b"wvtt");
        c += 4;
        // SampleEntry: reserved(6) zeros + data_reference_index(16)
        c += 6;
        buf[c..c + 2].copy_from_slice(&self.data_reference_index.to_be_bytes());
        c += 2;
        // vttC configuration box
        c += self.config.serialize_into(&mut buf[c..])?;
        // optional extra boxes
        for eb in &self.extra_boxes {
            c += eb.serialize_into(&mut buf[c..])?;
        }
        Ok(c)
    }
}

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

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

    #[test]
    fn xml_subtitle_sample_entry_round_trip() {
        let entry = XmlSubtitleSampleEntry::new("http://www.w3.org/ns/ttml");
        let bytes = entry.to_bytes();
        assert_eq!(&bytes[4..8], b"stpp");
        let parsed = XmlSubtitleSampleEntry::bare_parse(&bytes).unwrap();
        assert_eq!(parsed, entry);
        assert_eq!(parsed.to_bytes(), bytes);
    }

    #[test]
    fn wvtt_sample_entry_round_trip() {
        let entry = WvttSampleEntry::new("WEBVTT");
        let bytes = entry.to_bytes();
        assert_eq!(&bytes[4..8], b"wvtt");
        let parsed = WvttSampleEntry::bare_parse(&bytes).unwrap();
        assert_eq!(parsed, entry);
        assert_eq!(parsed.to_bytes(), bytes);
    }

    #[test]
    fn vtt_cue_box_round_trip() {
        let cue = VttCueBox::new("Hello World");
        let bytes = cue.to_bytes();
        assert_eq!(&bytes[4..8], b"vttc");
        let parsed = VttCueBox::bare_parse(&bytes).unwrap();
        assert_eq!(parsed.payload.cue_text, "Hello World");
        assert_eq!(parsed.to_bytes(), bytes);
    }

    #[test]
    fn vtte_round_trip() {
        let vtte = VttEmptyCueBox;
        let bytes = vtte.to_bytes();
        assert_eq!(bytes.len(), 8);
        assert_eq!(&bytes[4..8], b"vtte");
        let parsed = VttEmptyCueBox::bare_parse(&bytes).unwrap();
        assert_eq!(parsed.to_bytes(), bytes);
    }
}