Skip to content

Commit

Permalink
mpegts: emit decode error instead of exiting in case of parse errors (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Feb 14, 2024
1 parent 7195bbc commit 76fe548
Show file tree
Hide file tree
Showing 12 changed files with 419 additions and 49 deletions.
5 changes: 2 additions & 3 deletions pkg/formats/mpegts/codec_ac3.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ func (*CodecAC3) isCodec() {}

func (c CodecAC3) marshal(pid uint16) (*astits.PMTElementaryStream, error) {
return &astits.PMTElementaryStream{
ElementaryPID: pid,
ElementaryStreamDescriptors: nil,
StreamType: astits.StreamTypeAC3Audio,
ElementaryPID: pid,
StreamType: astits.StreamTypeAC3Audio,
}, nil
}
5 changes: 2 additions & 3 deletions pkg/formats/mpegts/codec_h264.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ func (*CodecH264) isCodec() {}

func (c CodecH264) marshal(pid uint16) (*astits.PMTElementaryStream, error) {
return &astits.PMTElementaryStream{
ElementaryPID: pid,
ElementaryStreamDescriptors: nil,
StreamType: astits.StreamTypeH264Video,
ElementaryPID: pid,
StreamType: astits.StreamTypeH264Video,
}, nil
}
5 changes: 2 additions & 3 deletions pkg/formats/mpegts/codec_h265.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ func (*CodecH265) isCodec() {}

func (c CodecH265) marshal(pid uint16) (*astits.PMTElementaryStream, error) {
return &astits.PMTElementaryStream{
ElementaryPID: pid,
ElementaryStreamDescriptors: nil,
StreamType: astits.StreamTypeH265Video,
ElementaryPID: pid,
StreamType: astits.StreamTypeH265Video,
}, nil
}
5 changes: 2 additions & 3 deletions pkg/formats/mpegts/codec_mpeg1_audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ func (*CodecMPEG1Audio) isCodec() {}

func (c CodecMPEG1Audio) marshal(pid uint16) (*astits.PMTElementaryStream, error) {
return &astits.PMTElementaryStream{
ElementaryPID: pid,
ElementaryStreamDescriptors: nil,
StreamType: astits.StreamTypeMPEG1Audio,
ElementaryPID: pid,
StreamType: astits.StreamTypeMPEG1Audio,
}, nil
}
3 changes: 1 addition & 2 deletions pkg/formats/mpegts/codec_mpeg1_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ func (*CodecMPEG1Video) isCodec() {}

func (c CodecMPEG1Video) marshal(pid uint16) (*astits.PMTElementaryStream, error) {
return &astits.PMTElementaryStream{
ElementaryPID: pid,
ElementaryStreamDescriptors: nil,
ElementaryPID: pid,
// we use MPEG-2 to notify readers that video can be either MPEG-1 or MPEG-2
StreamType: astits.StreamTypeMPEG2Video,
}, nil
Expand Down
5 changes: 2 additions & 3 deletions pkg/formats/mpegts/codec_mpeg4_audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func (*CodecMPEG4Audio) isCodec() {}

func (c CodecMPEG4Audio) marshal(pid uint16) (*astits.PMTElementaryStream, error) {
return &astits.PMTElementaryStream{
ElementaryPID: pid,
ElementaryStreamDescriptors: nil,
StreamType: astits.StreamTypeAACAudio,
ElementaryPID: pid,
StreamType: astits.StreamTypeAACAudio,
}, nil
}
5 changes: 2 additions & 3 deletions pkg/formats/mpegts/codec_mpeg4_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ func (*CodecMPEG4Video) isCodec() {}

func (c CodecMPEG4Video) marshal(pid uint16) (*astits.PMTElementaryStream, error) {
return &astits.PMTElementaryStream{
ElementaryPID: pid,
ElementaryStreamDescriptors: nil,
StreamType: astits.StreamTypeMPEG4Video,
ElementaryPID: pid,
StreamType: astits.StreamTypeMPEG4Video,
}, nil
}
2 changes: 1 addition & 1 deletion pkg/formats/mpegts/codec_opus.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (*CodecOpus) isCodec() {}
func (c CodecOpus) marshal(pid uint16) (*astits.PMTElementaryStream, error) {
return &astits.PMTElementaryStream{
ElementaryPID: pid,
StreamType: astits.StreamTypePrivateData,
ElementaryStreamDescriptors: []*astits.Descriptor{
{
Length: 4,
Expand All @@ -36,6 +37,5 @@ func (c CodecOpus) marshal(pid uint16) (*astits.PMTElementaryStream, error) {
},
},
},
StreamType: astits.StreamTypePrivateData,
}, nil
}
2 changes: 1 addition & 1 deletion pkg/formats/mpegts/opus_access_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type opusAccessUnit struct {
func (au *opusAccessUnit) unmarshal(buf []byte) (int, error) {
n, err := au.ControlHeader.unmarshal(buf)
if err != nil {
return 0, fmt.Errorf("could not decode control header: %w", err)
return 0, fmt.Errorf("invalid control header: %w", err)
}
buf = buf[n:]

Expand Down
60 changes: 34 additions & 26 deletions pkg/formats/mpegts/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"strings"

"github.com/asticode/go-astits"

Expand Down Expand Up @@ -175,7 +176,7 @@ func (r *Reader) OnDataMPEG4Audio(track *Track, cb ReaderOnDataMPEG4AudioFunc) {
var pkts mpeg4audio.ADTSPackets
err := pkts.Unmarshal(data)
if err != nil {
r.onDecodeError(err)
r.onDecodeError(fmt.Errorf("invalid ADTS: %w", err))
return nil
}

Expand Down Expand Up @@ -249,35 +250,42 @@ func (r *Reader) OnDataAC3(track *Track, cb ReaderOnDataAC3Func) {

// Read reads data.
func (r *Reader) Read() error {
data, err := r.dem.NextData()
if err != nil {
return err
}
for {
data, err := r.dem.NextData()
if err != nil {
// https://github.com/asticode/go-astits/blob/b0b19247aa31633650c32638fb55f597fa6e2468/packet_buffer.go#L133C1-L133C5
if errors.Is(err, astits.ErrNoMorePackets) || strings.Contains(err.Error(), "astits: reading ") {
return err
}
r.onDecodeError(err)
continue
}

if data.PES == nil {
return nil
}
if data.PES == nil {
return nil
}

if data.PES.Header.OptionalHeader == nil ||
data.PES.Header.OptionalHeader.PTSDTSIndicator == astits.PTSDTSIndicatorNoPTSOrDTS ||
data.PES.Header.OptionalHeader.PTSDTSIndicator == astits.PTSDTSIndicatorIsForbidden {
r.onDecodeError(fmt.Errorf("PTS is missing"))
return nil
}
if data.PES.Header.OptionalHeader == nil ||
data.PES.Header.OptionalHeader.PTSDTSIndicator == astits.PTSDTSIndicatorNoPTSOrDTS ||
data.PES.Header.OptionalHeader.PTSDTSIndicator == astits.PTSDTSIndicatorIsForbidden {
r.onDecodeError(fmt.Errorf("PTS is missing"))
return nil
}

pts := data.PES.Header.OptionalHeader.PTS.Base
pts := data.PES.Header.OptionalHeader.PTS.Base

var dts int64
if data.PES.Header.OptionalHeader.PTSDTSIndicator == astits.PTSDTSIndicatorBothPresent {
dts = data.PES.Header.OptionalHeader.DTS.Base
} else {
dts = pts
}
var dts int64
if data.PES.Header.OptionalHeader.PTSDTSIndicator == astits.PTSDTSIndicatorBothPresent {
dts = data.PES.Header.OptionalHeader.DTS.Base
} else {
dts = pts
}

onData, ok := r.onData[data.PID]
if !ok {
return nil
}
onData, ok := r.onData[data.PID]
if !ok {
return nil
}

return onData(pts, dts, data.PES.Data)
return onData(pts, dts, data.PES.Data)
}
}
Loading

0 comments on commit 76fe548

Please sign in to comment.