Skip to main content

rff_core/
packet.rs

1//! A `Packet` is a chunk of *compressed* data for one stream — the unit that
2//! flows demuxer → decoder and encoder → muxer. Analogous to `AVPacket`.
3
4use crate::rational::Rational;
5
6/// Per-packet flags.
7#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
8pub struct PacketFlags {
9    /// This packet begins a keyframe (random-access point).
10    pub keyframe: bool,
11}
12
13/// A compressed packet belonging to a single stream.
14#[derive(Debug, Clone, Default)]
15pub struct Packet {
16    /// Index of the stream this packet belongs to within its container.
17    pub stream_index: usize,
18    /// Presentation timestamp, in the stream's `time_base` units. `None` if unset.
19    pub pts: Option<i64>,
20    /// Decode timestamp, in the stream's `time_base` units. `None` if unset.
21    pub dts: Option<i64>,
22    /// Duration of this packet, in `time_base` units (0 if unknown).
23    pub duration: i64,
24    /// The stream time base these timestamps are expressed in.
25    pub time_base: Rational,
26    /// The compressed payload.
27    pub data: Vec<u8>,
28    /// Packet flags (keyframe, ...).
29    pub flags: PacketFlags,
30}
31
32impl Packet {
33    /// Construct an empty packet for the given stream.
34    pub fn new(stream_index: usize) -> Packet {
35        Packet {
36            stream_index,
37            time_base: Rational::new(1, 1000),
38            ..Default::default()
39        }
40    }
41
42    /// Construct a packet wrapping an existing payload.
43    pub fn from_data(stream_index: usize, data: Vec<u8>) -> Packet {
44        Packet {
45            data,
46            ..Packet::new(stream_index)
47        }
48    }
49
50    pub fn is_keyframe(&self) -> bool {
51        self.flags.keyframe
52    }
53}