Skip to content

Add MPEG-TS reader and writer - #3

Open
gokuljs wants to merge 19 commits into
pion:mainfrom
gokuljs:mpeg-ts-container
Open

Add MPEG-TS reader and writer#3
gokuljs wants to merge 19 commits into
pion:mainfrom
gokuljs:mpeg-ts-container

Conversation

@gokuljs

@gokuljs gokuljs commented Aug 4, 2026

Copy link
Copy Markdown

MPEG-TS reader and writer support to pion/format, with H.264 and H.265 video support. I also opened pion/webrtc#3492 to manually test recording WebRTC video to a .ts file and playing it back, and the complete flow works.

Once this PR is merged, I think we can clean up the test PR pion/webrtc#3492 and merge it as an example of how to use the MPEG-TS package with Pion WebRTC. What do you think?

cc: @JoTurk

@gokuljs
gokuljs marked this pull request as draft August 4, 2026 18:38
@gokuljs gokuljs changed the title Mpeg ts container Mpeg ts container (Do not review Work in progress) Aug 4, 2026
@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@gokuljs

gokuljs commented Aug 8, 2026

Copy link
Copy Markdown
Author

#2

@gokuljs
gokuljs marked this pull request as ready for review August 9, 2026 19:04
@gokuljs gokuljs changed the title Mpeg ts container (Do not review Work in progress) Add MPEG-TS reader and writer Aug 9, 2026
@gokuljs
gokuljs requested a review from JoTurk August 9, 2026 19:05

@JoTurk JoTurk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much, I'll give the reader and writer a quick test, if there are nits or issues we can just fix them later.

Comment thread mpegts/testdata/h264.ts.license Outdated
Comment thread mpegts/testdata/README.md Outdated
Comment thread mpegts/h26x.go
Comment thread mpegts/h26x.go
Comment thread mpegts/h26x.go Outdated
Comment thread mpegts/h26x.go Outdated
Comment on lines +26 to +33
idx := bytes.Index(annexB, nalStartCode3)
if idx == -1 {
if len(annexB) > 0 {
fn(annexB)
}

return
}

@JoTurk JoTurk Aug 9, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This treats buffers with no annex-b prefix as a single NAL right? unless we later get a prefix, if i understand the code correctly? I think this will be problematic if it's used to read chunks from streaming annex-b or from a streaming format. and seems to be unexpected behavior from an annex-b reader.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the fallback. A buffer with no start code now yields no NALs instead of being treated as one. This helper only ever receives complete access units, and I have also updated the doc comment to say it must not be fed partial chunks.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new mpegts package to pion/format that can demux and mux single-program MPEG-2 Transport Streams for H.264 and H.265, including PAT/PMT handling, PES timestamp parsing, and basic keyframe/random-access signaling. It adds unit tests plus FFmpeg-generated fixtures to validate interoperability.

Changes:

  • Added MPEG-TS Reader that discovers tracks via PAT/PMT and yields Annex-B access units with unwrapped PTS/DTS.
  • Added MPEG-TS Writer that muxes H.264/H.265 access units into TS packets (PAT/PMT + PES, optional AUD insertion, PCR on first track).
  • Added extensive tests (round-trips, known vectors, fuzzing seeds, and optional FFmpeg fixture validation) and updated module deps for testify.

Reviewed changes

Copilot reviewed 27 out of 31 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
mpegts/mpegts.go Defines MPEG-TS package and core constants (packet size, sync byte, well-known PIDs).
mpegts/track.go Introduces Track and Codec types plus stream type mappings for H.264/H.265.
mpegts/packet.go TS packet header marshal/parse utilities.
mpegts/adaptation.go Adaptation field parse/marshal for RAI and PCR.
mpegts/crc32.go Implements CRC-32/MPEG-2 used by PSI sections.
mpegts/psi.go PSI section parse/marshal (PAT/PMT envelope) with CRC validation.
mpegts/pat.go PAT parse/marshal for program -> PMT PID mapping.
mpegts/pmt.go PMT parse/marshal for PCR PID + stream entries.
mpegts/pes.go PES header parse/serialize including PTS/DTS encoding.
mpegts/h26x.go Annex-B NAL iteration, AUD detection/insertion helpers, and keyframe detection for H.264/H.265.
mpegts/reader.go Implements MPEG-TS demuxer: table parsing, ES reassembly, CC handling, timestamp unwrapping.
mpegts/writer.go Implements MPEG-TS muxer: track config, PAT/PMT emission, PES packetization, PCR + adaptation fields.
mpegts/*_test.go Adds unit tests for each component + fuzz seed and round-trip validation.
mpegts/fixture_test.go Validates Reader interoperability against optional FFmpeg-generated .ts fixtures.
mpegts/testdata/README.md Documents fixture purpose and regeneration steps.
mpegts/testdata/*.license Adds SPDX license files for fixtures.
go.mod Sets module path to github.com/pion/format and adds testify + indirect deps.
go.sum Adds checksums for new Go test dependencies.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread mpegts/writer.go
Comment thread mpegts/reader.go
@gokuljs
gokuljs requested a review from JoTurk August 11, 2026 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants