Skip to content
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

mpegts: emit decode error instead of exiting in case of parse errors #110

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"errors"
"fmt"
"io"
"strings"

"github.com/asticode/go-astits"

Expand Down Expand Up @@ -175,7 +176,7 @@
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 @@

// 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
}

Check warning on line 287 in pkg/formats/mpegts/reader.go

View check run for this annotation

Codecov / codecov/patch

pkg/formats/mpegts/reader.go#L286-L287

Added lines #L286 - L287 were not covered by tests

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