Skip to main content

Crate m2ts_packet

Crate m2ts_packet 

Source
Expand description

A MPEG2 Transport Stream (TS) packet decoder.

This crate provides low-level decoding of 188-byte MPEG-TS packets and reassembly into PES (Packetized Elementary Stream) packets and PSI sections (PAT / PMT).

§Core types

TypeDescription
TsPacketA single 188-byte transport stream packet
TsPacketDecoderA tokio_util::codec::Decoder that reads TsPackets from a byte stream
PesPacketA reassembled elementary stream item (video, audio, PAT, PMT, etc.)
PacketizedElementaryStreamA Stream adapter that reassembles TsPackets into PesPackets
PesAssemblerA pull-based assembler — same logic, but driven by an async callback

§Stream-based usage

Wrap any AsyncRead source with TsPacketDecoder via FramedRead, then feed the packet stream into PacketizedElementaryStream:

use m2ts_packet::{TsPacketDecoder, TsPacketStreamAssemble};
use remote_file::HttpFile;
use tokio_stream::StreamExt;
use tokio_util::codec::FramedRead;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // streaming some sample file from internet, https://tsduck.io/streams/
    // or replace with your own local file by using tokio::fs::File
    let mut file = HttpFile::new(
        reqwest::Client::new(),
        "https://tsduck.io/streams/australia-dttv/Ten.ts",
    )
    .await
    .unwrap();

    let mut pes = FramedRead::new(&mut file, TsPacketDecoder::new(0))
        .assemble()
        .take(10);
    while let Some(packet) = pes.try_next().await? {
        println!("PES Packet: {:?}", packet);
    }
    Ok(())
}

§Pull-based usage

If you don’t have a Stream or need finer control, use PesAssembler with an async callback that provides TsPackets on demand:

use remote_file::HttpFile;
use tokio_stream::StreamExt;
use tokio_util::codec::FramedRead;

#[tokio::main]
async fn main() {
    // streaming some sample file from internet, https://tsduck.io/streams/
    // or replace with your own local file by using tokio::fs::File
    let mut file = HttpFile::new(
        reqwest::Client::new(),
        "https://tsduck.io/streams/australia-dttv/Ten.ts",
    )
    .await
    .unwrap();
    let mut ts_packets = FramedRead::new(&mut file, m2ts_packet::TsPacketDecoder::new(0));
    let mut assembler = m2ts_packet::PesAssembler::new();

    for _ in 0..10 {
        let next_packet = assembler
            .next_packet(async || Ok(ts_packets.try_next().await?.map(|(_, pkt)| pkt)))
            .await
            .unwrap();
        println!("Packet: {:?}", next_packet);
    }
}

Structs§

AdaptationField
AdaptationFieldFlags
PacketizedElementaryStream
Stream adapter that reassembles TS packets into complete PES packets and PSI sections.
PesAssembler
Pull-based assembler that reassembles TS packets into complete PES packets and PSI sections.
TsPacket
TsPacketDecoder
Decoder that reads 188-byte MPEG-TS packets from a byte stream.

Enums§

PesPacket
Unpacked Elementary Stream Item from TS packets
TsPacketError

Traits§

TsPacketStreamAssemble

Type Aliases§

Result