Skip to content

Commit

Permalink
hls source: skip unsupported tracks
Browse files Browse the repository at this point in the history
Skip AC-3, EC-3, closed caption tracks
  • Loading branch information
aler9 committed Jan 5, 2023
1 parent 59c0ef5 commit fa1c072
Show file tree
Hide file tree
Showing 6 changed files with 644 additions and 281 deletions.
12 changes: 1 addition & 11 deletions internal/hls/client_downloader_primary.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,10 @@ func clientDownloadPlaylist(ctx context.Context, httpClient *http.Client, ur *ur
return m3u8.Unmarshal(byts)
}

func allCodecsAreSupported(codecs string) bool {
for _, codec := range strings.Split(codecs, ",") {
if !strings.HasPrefix(codec, "avc1") &&
!strings.HasPrefix(codec, "mp4a") {
return false
}
}
return true
}

func pickLeadingPlaylist(variants []*gm3u8.Variant) *gm3u8.Variant {
var candidates []*gm3u8.Variant //nolint:prealloc
for _, v := range variants {
if v.Codecs != "" && !allCodecsAreSupported(v.Codecs) {
if v.Codecs != "" && !codecParametersAreSupported(v.Codecs) {
continue
}
candidates = append(candidates, v)
Expand Down
2 changes: 1 addition & 1 deletion internal/hls/client_processor_fmp4.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (p *clientProcessorFMP4) processSegment(ctx context.Context, byts []byte) e

proc, ok := p.trackProcs[track.ID]
if !ok {
return fmt.Errorf("track ID %d not present in init file", track.ID)
continue
}

if processingCount >= (clientFMP4MaxPartTracksPerSegment - 1) {
Expand Down
47 changes: 47 additions & 0 deletions internal/hls/codecparameters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package hls

import (
"encoding/hex"
"strconv"
"strings"

"github.com/aler9/gortsplib/v2/pkg/codecs/h265"
"github.com/aler9/gortsplib/v2/pkg/format"
)

func codecParametersGenerate(track format.Format) string {
switch ttrack := track.(type) {
case *format.H264:
sps := ttrack.SafeSPS()
if len(sps) >= 4 {
return "avc1." + hex.EncodeToString(sps[1:4])
}

case *format.H265:
var sps h265.SPS
err := sps.Unmarshal(ttrack.SafeSPS())
if err == nil {
return "hvc1." + strconv.FormatInt(int64(sps.ProfileTierLevel.GeneralProfileIdc), 10) +
".4.L" + strconv.FormatInt(int64(sps.ProfileTierLevel.GeneralLevelIdc), 10) + ".B0"
}

case *format.MPEG4Audio:
// https://developer.mozilla.org/en-US/docs/Web/Media/Formats/codecs_parameter
return "mp4a.40." + strconv.FormatInt(int64(ttrack.Config.Type), 10)

case *format.Opus:
return "opus"
}

return ""
}

func codecParametersAreSupported(codecs string) bool {
for _, codec := range strings.Split(codecs, ",") {
if !strings.HasPrefix(codec, "avc1.") &&
!strings.HasPrefix(codec, "mp4a.") {
return false
}
}
return true
}
23 changes: 19 additions & 4 deletions internal/hls/fmp4/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,6 @@ func (i *Init) Unmarshal(byts []byte) error {
}
state = waitingTrak

case "ac-3":
return nil, fmt.Errorf("AC-3 codec is not supported (yet)")

case "Opus":
if state != waitingCodec {
return nil, fmt.Errorf("unexpected box 'Opus'")
Expand All @@ -247,6 +244,24 @@ func (i *Init) Unmarshal(byts []byte) error {
ChannelCount: int(dops.OutputChannelCount),
}
state = waitingTrak

case "ac-3": // ac-3, not supported yet
i.Tracks = i.Tracks[:len(i.Tracks)-1]
state = waitingTrak
return nil, nil

case "ec-3": // ec-3, not supported yet
i.Tracks = i.Tracks[:len(i.Tracks)-1]
state = waitingTrak
return nil, nil

case "c608", "c708": // closed captions, not supported yet
i.Tracks = i.Tracks[:len(i.Tracks)-1]
state = waitingTrak
return nil, nil

case "chrm", "nmhd":
return nil, nil
}

return h.Expand()
Expand All @@ -259,7 +274,7 @@ func (i *Init) Unmarshal(byts []byte) error {
return fmt.Errorf("parse error")
}

if i.Tracks == nil {
if len(i.Tracks) == 0 {
return fmt.Errorf("no tracks found")
}

Expand Down

0 comments on commit fa1c072

Please sign in to comment.