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: allow interacting with streams with unsupported codecs #147

Merged
merged 1 commit into from
Sep 15, 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
19 changes: 19 additions & 0 deletions pkg/formats/mpegts/codec_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mpegts

import (
"github.com/asticode/go-astits"
)

// CodecUnsupported is an unsupported codec.
type CodecUnsupported struct{}

// IsVideo implements Codec.
func (CodecUnsupported) IsVideo() bool {
return false

Check warning on line 12 in pkg/formats/mpegts/codec_unsupported.go

View check run for this annotation

Codecov / codecov/patch

pkg/formats/mpegts/codec_unsupported.go#L11-L12

Added lines #L11 - L12 were not covered by tests
}

func (*CodecUnsupported) isCodec() {}

Check warning on line 15 in pkg/formats/mpegts/codec_unsupported.go

View check run for this annotation

Codecov / codecov/patch

pkg/formats/mpegts/codec_unsupported.go#L15

Added line #L15 was not covered by tests

func (c CodecUnsupported) marshal(uint16) (*astits.PMTElementaryStream, error) {
panic("this should not happen")

Check warning on line 18 in pkg/formats/mpegts/codec_unsupported.go

View check run for this annotation

Codecov / codecov/patch

pkg/formats/mpegts/codec_unsupported.go#L17-L18

Added lines #L17 - L18 were not covered by tests
}
7 changes: 0 additions & 7 deletions pkg/formats/mpegts/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,12 @@ func NewReader(br io.Reader) (*Reader, error) {
var track Track
err := track.unmarshal(dem, es)
if err != nil {
if errors.Is(err, errUnsupportedCodec) {
continue
}
return nil, err
}

tracks = append(tracks, &track)
}

if tracks == nil {
return nil, fmt.Errorf("no tracks with supported codecs found")
}

// rewind demuxer
dem = astits.NewDemuxer(
context.Background(),
Expand Down
18 changes: 6 additions & 12 deletions pkg/formats/mpegts/track.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mpegts

import (
"errors"
"fmt"

"github.com/asticode/go-astits"
Expand All @@ -15,8 +14,6 @@
opusIdentifier = 'O'<<24 | 'p'<<16 | 'u'<<8 | 's'
)

var errUnsupportedCodec = errors.New("unsupported codec")

func findMPEG4AudioConfig(dem *astits.Demuxer, pid uint16) (*mpeg4audio.Config, error) {
for {
data, err := dem.NextData()
Expand Down Expand Up @@ -125,19 +122,15 @@
switch es.StreamType {
case astits.StreamTypeH265Video:
t.Codec = &CodecH265{}
return nil

case astits.StreamTypeH264Video:
t.Codec = &CodecH264{}
return nil

case astits.StreamTypeMPEG4Video:
t.Codec = &CodecMPEG4Video{}
return nil

case astits.StreamTypeMPEG2Video, astits.StreamTypeMPEG1Video:
t.Codec = &CodecMPEG1Video{}
return nil

case astits.StreamTypeAACAudio:
conf, err := findMPEG4AudioConfig(dem, es.ElementaryPID)
Expand All @@ -148,11 +141,9 @@
t.Codec = &CodecMPEG4Audio{
Config: *conf,
}
return nil

case astits.StreamTypeMPEG1Audio:
t.Codec = &CodecMPEG1Audio{}
return nil

case astits.StreamTypeAC3Audio:
sampleRate, channelCount, err := findAC3Parameters(dem, es.ElementaryPID)
Expand All @@ -164,15 +155,18 @@
SampleRate: sampleRate,
ChannelCount: channelCount,
}
return nil

case astits.StreamTypePrivateData:
codec := findOpusCodec(es.ElementaryStreamDescriptors)
if codec != nil {
t.Codec = codec
return nil
} else {
t.Codec = &CodecUnsupported{}
}

default:
t.Codec = &CodecUnsupported{}

Check warning on line 168 in pkg/formats/mpegts/track.go

View check run for this annotation

Codecov / codecov/patch

pkg/formats/mpegts/track.go#L167-L168

Added lines #L167 - L168 were not covered by tests
}

return errUnsupportedCodec
return nil
}
Loading