Skip to content

Commit

Permalink
Demuxer: emit an error in case of unknown data packets and EOF (#32)
Browse files Browse the repository at this point in the history
At the moment, the demuxer returns (nil, nil) when the stream
is finished, there are TS packets in the queue but they're
unknown data packets.

This patch makes the demuxer emit (nil, ErrNoMorePackets).
  • Loading branch information
aler9 committed Oct 1, 2021
1 parent ddb96a7 commit 473f66c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion demuxer.go
Original file line number Diff line number Diff line change
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 errParseData error
if ds, errParseData = parseData(ps, dmx.optPacketsParser, dmx.programMap); errParseData != 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
21 changes: 21 additions & 0 deletions demuxer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,27 @@ func TestDemuxerNextData(t *testing.T) {
assert.EqualError(t, err, ErrNoMorePackets.Error())
}

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

// Packet that isn't a data packet (PSI or PES)
b1, _ := packet(PacketHeader{
ContinuityCounter: uint8(0),
PID: 256,
PayloadUnitStartIndicator: true,
HasPayload: true,
}, PacketAdaptationField{}, []byte{0x01, 0x02, 0x03, 0x04}, true)
bufWriter.Write(b1)

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

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

0 comments on commit 473f66c

Please sign in to comment.