Skip to main content

OwnedTsPacket

Struct OwnedTsPacket 

Source
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: bool

Discontinuity 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

Source

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.

Source

pub fn raw(&self) -> &[u8; 188]

The raw 188 bytes.

Source

pub fn into_raw(self) -> [u8; 188]

Consume the packet, returning its raw 188 bytes.

Source

pub fn pid(&self) -> u16

13-bit PID (bytes 1–2).

Source

pub fn pusi(&self) -> bool

Payload Unit Start Indicator (byte 1 bit 6).

Source

pub fn has_adaptation(&self) -> bool

Adaptation field present flag (byte 3 bit 5).

Source

pub fn has_payload(&self) -> bool

Payload present flag (byte 3 bit 4).

Source

pub fn tei(&self) -> bool

Transport Error Indicator (byte 1 bit 7).

Source

pub fn scrambling(&self) -> u8

Raw 2-bit transport_scrambling_control (byte 3 bits 7–6); see scrambling_control for the typed view.

Source

pub fn continuity_counter(&self) -> u8

4-bit continuity_counter (byte 3 bits 3–0).

Source

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.

Source

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).

Source

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).

Source

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.

Source

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.

Source

pub fn serialize_with_payload( pid: u16, 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.

Source

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);
Source

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, &[]).unwrap();
OwnedTsPacket::set_continuity_counter(&mut raw, 7);
let pkt = TsPacket::parse(&raw).unwrap();
assert_eq!(pkt.header.continuity_counter, 7);
Source

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));
Source

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OwnedTsPacket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for OwnedTsPacket

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.