Skip to content

Commit

Permalink
Demuxer: emit an error in case of incomplete PES packet and EOF
Browse files Browse the repository at this point in the history
At the moment, the demuxer returns (nil, nil) when there are no
more TS packets and a PES packet has not been fully received.
This patch makes the demuxer emit (nil, ErrNoMorePackets).
  • Loading branch information
aler9 committed Sep 30, 2021
1 parent ddb96a7 commit 9bb23e3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion demuxer.go
Expand Up @@ -119,14 +119,16 @@ func (dmx *Demuxer) NextData() (d *DemuxerData, err error) {
}

// Parse data
if ds, err = parseData(ps, dmx.optPacketsParser, dmx.programMap); err != nil {
var err2 error
if ds, err2 = parseData(ps, dmx.optPacketsParser, dmx.programMap); err2 != nil {
// We need to silence this error as there may be some incomplete data here
// We still want to try to parse all packets, in case final data is complete
continue
}

// Update data
if d = dmx.updateData(ds); d != nil {
err = nil
return
}
}
Expand Down
28 changes: 28 additions & 0 deletions demuxer_test.go
Expand Up @@ -101,6 +101,34 @@ func TestDemuxerNextData(t *testing.T) {
assert.EqualError(t, err, ErrNoMorePackets.Error())
}

func TestDemuxerNextDataInvalidFinalPackets(t *testing.T) {
buf := &bytes.Buffer{}
bufWriter := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})

// Packet that is part of an incomplete PES
b1, _ := packet(PacketHeader{
ContinuityCounter: uint8(0),
PID: 256,
PayloadUnitStartIndicator: true,
HasPayload: true,
}, PacketAdaptationField{}, []byte{0x01, 0x02, 0x03, 0x04}, true)
bufWriter.Write(b1)

// Packet that is part of an incomplete PES
b2, _ := packet(PacketHeader{
ContinuityCounter: uint8(1),
PID: 256,
HasPayload: true,
}, PacketAdaptationField{}, []byte{0x01, 0x02, 0x03, 0x04}, true)
bufWriter.Write(b2)

// The demuxer must return "no more packets"
dmx := NewDemuxer(context.Background(), bytes.NewReader(buf.Bytes()))
d, err := dmx.NextData()
assert.Equal(t, (*DemuxerData)(nil), d)
assert.EqualError(t, err, "astits: no more packets")
}

func TestDemuxerNextDataPATPMT(t *testing.T) {
pat := hexToBytes(`474000100000b00d0001c100000001f0002ab104b2ffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
Expand Down

0 comments on commit 9bb23e3

Please sign in to comment.