pub struct OwnedTsPacket {
pub discontinuity: bool,
/* private fields */
}Expand description
Owned 188-byte TS packet with pre-parsed header fields.
The raw bytes are stored privately; the parsed flags (pid, pusi, etc.)
are pre-extracted at construction time so hot paths avoid repeated byte
masking. The fields are private (audit r01-W8, #1074): a public copy
of a header field that a caller could assign would silently drift from
the bytes that raw serialises. Read them through the
getters; the only mutation entry points are the associated functions that
edit a raw [u8; 188] (set_pcr, set_continuity_counter) followed by a
re-parse.
§Payload access
Use payload / payload_mut to obtain
a slice that correctly skips the 4-byte header and any adaptation field.
§Building packets
serialize_with_payload constructs a plain
payload-only packet (no adaptation field) filled with 0xFF stuffing.
Fields§
§discontinuity: boolDiscontinuity flag: true if the adaptation-field discontinuity_indicator
was set in the source packet, or if the caller marks this as a
continuity-counter discontinuity boundary. Defaults to false on parse.
Implementations§
Source§impl OwnedTsPacket
impl OwnedTsPacket
Sourcepub const MAX_PAYLOAD_LEN: usize
pub const MAX_PAYLOAD_LEN: usize
Maximum payload bytes a plain (no-adaptation-field) TS packet can carry: 188 bytes minus the 4-byte header.
Sourcepub fn has_adaptation(&self) -> bool
pub fn has_adaptation(&self) -> bool
Adaptation field present flag (byte 3 bit 5).
Sourcepub fn has_payload(&self) -> bool
pub fn has_payload(&self) -> bool
Payload present flag (byte 3 bit 4).
Sourcepub fn scrambling(&self) -> u8
pub fn scrambling(&self) -> u8
Raw 2-bit transport_scrambling_control (byte 3 bits 7–6); see
scrambling_control for the typed view.
Sourcepub fn continuity_counter(&self) -> u8
pub fn continuity_counter(&self) -> u8
4-bit continuity_counter (byte 3 bits 3–0).
Sourcepub fn parse(raw: [u8; 188]) -> Result<Self>
pub fn parse(raw: [u8; 188]) -> Result<Self>
Parse a 188-byte owned TS packet.
Returns Error::InvalidSync if raw[0] != 0x47.
Header bit-parsing is delegated to TsHeader::parse.
The discontinuity field defaults to false; set it manually if needed.
Sourcepub fn scrambling_control(&self) -> ScramblingControl
pub fn scrambling_control(&self) -> ScramblingControl
Typed view of the 2-bit transport_scrambling_control field.
See ScramblingControl for the spec citation (H.222.0 Table 2-4 +
ETSI TS 100 289 §5.1 Table 1).
Sourcepub fn adaptation_field_control(&self) -> AdaptationFieldControl
pub fn adaptation_field_control(&self) -> AdaptationFieldControl
Typed view of the adaptation_field_control 2-bit field, derived from the
stored has_adaptation/has_payload booleans.
See AdaptationFieldControl for the spec citation (H.222.0 Table 2-5).
Sourcepub fn payload(&self) -> Option<&[u8]>
pub fn payload(&self) -> Option<&[u8]>
Return the payload bytes (after the 4-byte header and any adaptation field).
Returns None when has_payload is false or the
adaptation field consumed all remaining bytes.
Sourcepub fn payload_mut(&mut self) -> Option<&mut [u8]>
pub fn payload_mut(&mut self) -> Option<&mut [u8]>
Return a mutable slice of the payload bytes.
Returns None when has_payload is false or the
adaptation field consumed all remaining bytes.
Sourcepub fn serialize_with_payload(
pid: Pid,
pusi: bool,
cc: u8,
payload: &[u8],
) -> Result<[u8; 188]>
pub fn serialize_with_payload( pid: Pid, pusi: bool, cc: u8, payload: &[u8], ) -> Result<[u8; 188]>
Build a 188-byte payload-only TS packet (no adaptation field).
The packet is initialised to 0xFF (MPEG-TS stuffing), the 4-byte header
is written via TsHeader::serialize_into, then payload is copied
starting at byte 4. Any unfilled bytes remain 0xFF.
Returns Error::PayloadTooLarge if payload is longer than
MAX_PAYLOAD_LEN (184 bytes) — the fixed
per-packet payload capacity would previously be silently truncated
(#1129).
§Panics
Never panics — serializing a 4-byte header into a 188-byte buffer cannot fail.
Sourcepub fn null_packet(cc: u8) -> [u8; 188]
pub fn null_packet(cc: u8) -> [u8; 188]
Build a 188-byte null packet (PID 0x1FFF) with 0xFF-stuffed payload.
Null packets carry PID 0x1FFF and have no meaningful payload
(ISO/IEC 13818-1 §2.4.1). The continuity counter cc is masked to 4
bits. Transport scrambling is 00 (not scrambled).
§Example
use mpeg_ts::OwnedTsPacket;
use mpeg_ts::ts::{TsPacket, TS_PACKET_SIZE};
let raw = OwnedTsPacket::null_packet(3);
assert_eq!(raw.len(), TS_PACKET_SIZE);
let pkt = TsPacket::parse(&raw).unwrap();
assert_eq!(pkt.header.pid, mpeg_ts::pid::well_known::NULL);
assert_eq!(pkt.header.continuity_counter, 3);Sourcepub fn set_continuity_counter(packet: &mut [u8; 188], cc: u8)
pub fn set_continuity_counter(packet: &mut [u8; 188], cc: u8)
Overwrite the continuity_counter field in packet without re-parsing.
Writes the low 4 bits of cc into byte 3 bits [3:0] in place
(ISO/IEC 13818-1 §2.4.3.3). The other bits of byte 3 are preserved.
§Example
use mpeg_ts::OwnedTsPacket;
use mpeg_ts::pid::Pid;
use mpeg_ts::ts::TsPacket;
let mut raw = OwnedTsPacket::serialize_with_payload(Pid::new(0x0100), false, 0, &[]).unwrap();
OwnedTsPacket::set_continuity_counter(&mut raw, 7);
let pkt = TsPacket::parse(&raw).unwrap();
assert_eq!(pkt.header.continuity_counter, 7);Sourcepub fn set_pcr(packet: &mut [u8; 188], pcr: Pcr) -> Result<()>
pub fn set_pcr(packet: &mut [u8; 188], pcr: Pcr) -> Result<()>
Overwrite the PCR value in an existing adaptation field in packet.
The packet must already have adaptation_field_control with adaptation
present (has_adaptation == true) and the PCR flag set in the adaptation
field flags byte. This function locates the 6-byte PCR field and
overwrites it with the encoding of pcr.
§Errors
Error::BufferTooShort— the adaptation field does not contain a PCR slot (no adaptation field, zero-length field, or PCR flag not set).
§Example
use mpeg_ts::ts::{AdaptationField, AF_PCR_FLAG, Pcr, TsPacket,
TS_PACKET_SIZE, TS_SYNC_BYTE,
ADAPTATION_FLAG, PAYLOAD_FLAG};
use mpeg_ts::OwnedTsPacket;
// Build a packet that has a PCR slot (adaptation_field_length = 7).
let mut raw = [0xAAu8; TS_PACKET_SIZE];
raw[0] = TS_SYNC_BYTE;
raw[1] = 0x00; raw[2] = 0x64; // PID = 100
raw[3] = ADAPTATION_FLAG | PAYLOAD_FLAG;
raw[4] = 7; // adaptation_field_length
raw[5] = AF_PCR_FLAG;
raw[6..12].copy_from_slice(&[0u8; 6]);
let new_pcr = Pcr { base: 10_000, extension: 0 };
OwnedTsPacket::set_pcr(&mut raw, new_pcr).unwrap();
let pkt = TsPacket::parse(&raw).unwrap();
let af = pkt.adaptation_field().unwrap().unwrap();
assert_eq!(af.pcr, Some(new_pcr));Sourcepub fn adaptation_field(&self) -> Option<Result<AdaptationField<'_>>>
pub fn adaptation_field(&self) -> Option<Result<AdaptationField<'_>>>
Decode the adaptation field from this packet, if present.
Returns None when no adaptation field is present, and
Some(Err(..)) when the adaptation field bytes are malformed.
The returned AdaptationField borrows from self.raw.