Skip to main content

sheathe_core/
sample.rs

1//! A single coded media sample (access unit / frame) on a stream's timeline.
2
3/// Per-sample flags relevant to fragmentation and segment boundaries.
4///
5/// A hand-rolled `u8` flag set, so `sheathe-core` needs no dependency beyond
6/// `thiserror`.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8pub struct SampleFlags(u8);
9
10impl SampleFlags {
11    /// This sample is a sync sample (IDR / keyframe) — a valid segment start.
12    pub const KEYFRAME: SampleFlags = SampleFlags(0b0000_0001);
13    /// This sample is not depended on by others (droppable).
14    pub const DISPOSABLE: SampleFlags = SampleFlags(0b0000_0010);
15
16    /// The empty flag set.
17    pub const fn empty() -> Self {
18        SampleFlags(0)
19    }
20
21    /// Whether every bit in `other` is set in `self`.
22    pub const fn contains(self, other: Self) -> bool {
23        (self.0 & other.0) == other.0
24    }
25
26    /// Set the bits in `other`.
27    pub fn insert(&mut self, other: Self) {
28        self.0 |= other.0;
29    }
30}
31
32impl core::ops::BitOr for SampleFlags {
33    type Output = Self;
34    fn bitor(self, rhs: Self) -> Self {
35        SampleFlags(self.0 | rhs.0)
36    }
37}
38
39/// One coded sample: its bytes plus the timing the muxer needs.
40#[derive(Debug, Clone)]
41pub struct Sample {
42    /// Decode timestamp, in the stream's timescale.
43    pub dts: u64,
44    /// Presentation timestamp, in the stream's timescale.
45    pub pts: u64,
46    /// Sample duration, in the stream's timescale.
47    pub duration: u32,
48    /// Flags describing the sample (keyframe, etc.).
49    pub flags: SampleFlags,
50    /// The coded bytes (NAL units, raw frame, …).
51    pub data: Vec<u8>,
52}
53
54impl Sample {
55    /// Whether this sample may begin a new segment.
56    pub fn is_segment_boundary(&self) -> bool {
57        self.flags.contains(SampleFlags::KEYFRAME)
58    }
59}