-
Notifications
You must be signed in to change notification settings - Fork 63
Add ability to preserve PAT/PMT information #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
asticode
merged 3 commits into
asticode:master
from
Vadym-Kupriyanchuk:feature/round-trip
Mar 13, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| package astits | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type pesRecord struct { | ||
| pid uint16 | ||
| pes *PESData | ||
| af *PacketAdaptationField | ||
| } | ||
|
|
||
| func TestRoundTrip(t *testing.T) { | ||
| originalBytes, err := os.ReadFile("testdata/ts/silent_audio.ts") | ||
| require.NoError(t, err) | ||
|
|
||
| // Phase 1: Demux the original TS file | ||
| dmx := NewDemuxer(context.Background(), bytes.NewReader(originalBytes), DemuxerOptPacketSize(MpegTsPacketSize)) | ||
|
|
||
| var originalPAT *PATData | ||
| var originalPMT *PMTData | ||
| var originalPMTPID uint16 = 0xFFFF | ||
|
|
||
| for { | ||
| d, err := dmx.NextData() | ||
| if errors.Is(err, ErrNoMorePackets) { | ||
| break | ||
| } | ||
| require.NoError(t, err) | ||
|
|
||
| if d.PAT != nil { | ||
| originalPAT = d.PAT | ||
| originalPMTPID = d.PAT.Programs[0].ProgramMapID | ||
| } | ||
| if d.PMT != nil { | ||
| originalPMT = d.PMT | ||
| } | ||
|
|
||
| if originalPMT != nil && originalPAT != nil { | ||
| break | ||
| } | ||
| } | ||
| require.NotNil(t, originalPAT) | ||
| require.NotNil(t, originalPMT) | ||
| require.NotEqual(t, 0xFFFF, originalPMTPID) | ||
|
|
||
| // Phase 2: Mux everything back into a new TS stream, preserving PAT/PMT identifiers | ||
| var buf bytes.Buffer | ||
| muxer := NewMuxer(context.Background(), &buf, | ||
| WithTransportStreamID(originalPAT.TransportStreamID), | ||
| WithPMTPID(originalPMTPID), | ||
| ) | ||
|
|
||
| for _, es := range originalPMT.ElementaryStreams { | ||
| err := muxer.AddElementaryStream(PMTElementaryStream{ | ||
| ElementaryPID: es.ElementaryPID, | ||
| StreamType: es.StreamType, | ||
| ElementaryStreamDescriptors: es.ElementaryStreamDescriptors, | ||
| }) | ||
| require.NoError(t, err) | ||
| } | ||
| muxer.SetPCRPID(originalPMT.PCRPID) | ||
| muxer.pmt.ProgramDescriptors = originalPMT.ProgramDescriptors | ||
| _, err = muxer.WriteTables() | ||
| require.NoError(t, err) | ||
|
|
||
| // Phase 3: Demux the round-tripped output | ||
| dmx2 := NewDemuxer(context.Background(), bytes.NewReader(buf.Bytes()), DemuxerOptPacketSize(MpegTsPacketSize)) | ||
|
|
||
| var rtPAT *PATData | ||
| var rtPMT *PMTData | ||
|
|
||
| for { | ||
| d, err := dmx2.NextData() | ||
| if errors.Is(err, ErrNoMorePackets) { | ||
| break | ||
| } | ||
| require.NoError(t, err) | ||
|
|
||
| if d.PAT != nil { | ||
| rtPAT = d.PAT | ||
| } | ||
| if d.PMT != nil { | ||
| rtPMT = d.PMT | ||
| } | ||
|
|
||
| if rtPAT != nil && rtPMT != nil { | ||
| break | ||
| } | ||
| } | ||
| require.NotNil(t, rtPAT) | ||
| require.NotNil(t, rtPMT) | ||
|
|
||
| // Phase 4: Validate round-trip preserved all meaningful information | ||
| // --- PAT --- | ||
| assert.Equal(t, originalPAT.TransportStreamID, rtPAT.TransportStreamID, "PAT TransportStreamID mismatch") | ||
| require.Equal(t, len(originalPAT.Programs), len(rtPAT.Programs), "PAT program count mismatch") | ||
| for i, origProg := range originalPAT.Programs { | ||
| assert.Equalf(t, origProg.ProgramNumber, rtPAT.Programs[i].ProgramNumber, | ||
| "PAT Programs[%d].ProgramNumber mismatch", i) | ||
| assert.Equalf(t, origProg.ProgramMapID, rtPAT.Programs[i].ProgramMapID, | ||
| "PAT Programs[%d].ProgramMapID mismatch", i) | ||
| } | ||
|
|
||
| // --- PMT --- | ||
| assert.Equal(t, originalPMT.PCRPID, rtPMT.PCRPID) | ||
| assert.Equal(t, originalPMT.ProgramNumber, rtPMT.ProgramNumber) | ||
| require.Equal(t, len(originalPMT.ProgramDescriptors), len(rtPMT.ProgramDescriptors)) | ||
| for i, desc := range originalPMT.ProgramDescriptors { | ||
| assert.Equalf(t, desc.Tag, rtPMT.ProgramDescriptors[i].Tag, | ||
| "PMT ProgramDescriptors[%d].Tag mismatch", i) | ||
| assert.Equalf(t, desc.Length, rtPMT.ProgramDescriptors[i].Length, | ||
| "PMT ProgramDescriptors[%d].Length mismatch", i) | ||
| } | ||
| require.Equal(t, len(originalPMT.ElementaryStreams), len(rtPMT.ElementaryStreams)) | ||
| for i, es := range originalPMT.ElementaryStreams { | ||
| rtES := rtPMT.ElementaryStreams[i] | ||
| assert.Equalf(t, es.ElementaryPID, rtES.ElementaryPID, | ||
| "PMT ElementaryStreams[%d].ElementaryPID mismatch", i) | ||
| assert.Equalf(t, es.StreamType, rtES.StreamType, | ||
| "PMT ElementaryStreams[%d].StreamType mismatch", i) | ||
| require.Equalf(t, len(es.ElementaryStreamDescriptors), len(rtES.ElementaryStreamDescriptors), | ||
| "PMT ElementaryStreams[%d].ElementaryStreamDescriptors count mismatch", i) | ||
| for j, desc := range es.ElementaryStreamDescriptors { | ||
| assert.Equalf(t, desc.Tag, rtES.ElementaryStreamDescriptors[j].Tag, | ||
| "PMT ElementaryStreams[%d].Descriptors[%d].Tag mismatch", i, j) | ||
| assert.Equalf(t, desc.Length, rtES.ElementaryStreamDescriptors[j].Length, | ||
| "PMT ElementaryStreams[%d].Descriptors[%d].Length mismatch", i, j) | ||
| } | ||
| } | ||
| } | ||
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your new test is failing since you forgot to add the
silent_audio.tsfile to this PR. Could you add it?