pub struct OwnedTsPacket {
pub raw: [u8; 188],
pub pid: u16,
pub pusi: bool,
pub has_adaptation: bool,
pub has_payload: bool,
pub tei: bool,
pub scrambling: u8,
pub continuity_counter: u8,
pub discontinuity: bool,
}Expand description
Owned 188-byte TS packet with pre-parsed header fields.
The raw bytes are stored in raw; the parsed flags (pid, pusi, etc.) are
pre-extracted at construction time so hot paths avoid repeated byte masking.
§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§
§raw: [u8; 188]The raw 188 bytes (serialized as a byte sequence).
pid: u1613-bit PID extracted from bytes 1–2.
pusi: boolPayload Unit Start Indicator (byte 1 bit 6).
has_adaptation: boolAdaptation field present flag (byte 3 bit 5).
has_payload: boolPayload present flag (byte 3 bit 4).
tei: boolTransport Error Indicator (byte 1 bit 7).
scrambling: u82-bit transport_scrambling_control (byte 3 bits 7–6).
continuity_counter: u84-bit continuity_counter (byte 3 bits 3–0).
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 fn parse(raw: [u8; 188]) -> Result<Self>
pub fn parse(raw: [u8; 188]) -> Result<Self>
Parse a 188-byte owned TS packet.
Returns Error::InvalidSyncByte 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: u16,
pusi: bool,
cc: u8,
payload: &[u8],
) -> [u8; 188]
pub fn serialize_with_payload( pid: u16, pusi: bool, cc: u8, payload: &[u8], ) -> [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 up to 184 bytes of
payload are copied starting at byte 4. Any unfilled bytes remain 0xFF.
§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, 0x1FFF);
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::ts::TsPacket;
let mut raw = OwnedTsPacket::serialize_with_payload(0x0100, false, 0, &[]);
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.
Trait Implementations§
Source§impl Clone for OwnedTsPacket
impl Clone for OwnedTsPacket
Source§fn clone(&self) -> OwnedTsPacket
fn clone(&self) -> OwnedTsPacket
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more