Add MPEG-TS reader and writer - #3
Conversation
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 ☂️ |
Introduce MPEG-TS packet-header parsing and serialization with tests.
073e22f to
44fe10a
Compare
JoTurk
left a comment
There was a problem hiding this comment.
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.
| idx := bytes.Index(annexB, nalStartCode3) | ||
| if idx == -1 { | ||
| if len(annexB) > 0 { | ||
| fn(annexB) | ||
| } | ||
|
|
||
| return | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.tsfile 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