Browse Source

hls source: skip unsupported tracks

Skip AC-3, EC-3, closed caption tracks
pull/1364/head
aler9 3 years ago
parent
commit
fa1c07253f
  1. 12
      internal/hls/client_downloader_primary.go
  2. 2
      internal/hls/client_processor_fmp4.go
  3. 47
      internal/hls/codecparameters.go
  4. 23
      internal/hls/fmp4/init.go
  5. 384
      internal/hls/fmp4/init_test.go
  6. 33
      internal/hls/muxer_primary_playlist.go

12
internal/hls/client_downloader_primary.go

@ -43,20 +43,10 @@ func clientDownloadPlaylist(ctx context.Context, httpClient *http.Client, ur *ur
return m3u8.Unmarshal(byts) 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 { func pickLeadingPlaylist(variants []*gm3u8.Variant) *gm3u8.Variant {
var candidates []*gm3u8.Variant //nolint:prealloc var candidates []*gm3u8.Variant //nolint:prealloc
for _, v := range variants { for _, v := range variants {
if v.Codecs != "" && !allCodecsAreSupported(v.Codecs) { if v.Codecs != "" && !codecParametersAreSupported(v.Codecs) {
continue continue
} }
candidates = append(candidates, v) candidates = append(candidates, v)

2
internal/hls/client_processor_fmp4.go

@ -146,7 +146,7 @@ func (p *clientProcessorFMP4) processSegment(ctx context.Context, byts []byte) e
proc, ok := p.trackProcs[track.ID] proc, ok := p.trackProcs[track.ID]
if !ok { if !ok {
return fmt.Errorf("track ID %d not present in init file", track.ID) continue
} }
if processingCount >= (clientFMP4MaxPartTracksPerSegment - 1) { if processingCount >= (clientFMP4MaxPartTracksPerSegment - 1) {

47
internal/hls/codecparameters.go

@ -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
internal/hls/fmp4/init.go

@ -221,9 +221,6 @@ func (i *Init) Unmarshal(byts []byte) error {
} }
state = waitingTrak state = waitingTrak
case "ac-3":
return nil, fmt.Errorf("AC-3 codec is not supported (yet)")
case "Opus": case "Opus":
if state != waitingCodec { if state != waitingCodec {
return nil, fmt.Errorf("unexpected box 'Opus'") return nil, fmt.Errorf("unexpected box 'Opus'")
@ -247,6 +244,24 @@ func (i *Init) Unmarshal(byts []byte) error {
ChannelCount: int(dops.OutputChannelCount), ChannelCount: int(dops.OutputChannelCount),
} }
state = waitingTrak 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() return h.Expand()
@ -259,7 +274,7 @@ func (i *Init) Unmarshal(byts []byte) error {
return fmt.Errorf("parse error") return fmt.Errorf("parse error")
} }
if i.Tracks == nil { if len(i.Tracks) == 0 {
return fmt.Errorf("no tracks found") return fmt.Errorf("no tracks found")
} }

384
internal/hls/fmp4/init_test.go

@ -684,8 +684,14 @@ func TestInitUnmarshal(t *testing.T) {
} }
func TestInitUnmarshalExternal(t *testing.T) { func TestInitUnmarshalExternal(t *testing.T) {
t.Run("h264", func(t *testing.T) { for _, ca := range []struct {
byts := []byte{ name string
byts []byte
init Init
}{
{
"h264",
[]byte{
0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x1c,
'f', 't', 'y', 'p', 'f', 't', 'y', 'p',
0x64, 0x61, 0x73, 0x68, 0x00, 0x00, 0x00, 0x01, 0x64, 0x61, 0x73, 0x68, 0x00, 0x00, 0x00, 0x01,
@ -783,13 +789,8 @@ func TestInitUnmarshalExternal(t *testing.T) {
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
} },
Init{
var init Init
err := init.Unmarshal(byts)
require.NoError(t, err)
require.Equal(t, Init{
Tracks: []*InitTrack{ Tracks: []*InitTrack{
{ {
ID: 256, ID: 256,
@ -809,11 +810,11 @@ func TestInitUnmarshalExternal(t *testing.T) {
}, },
}, },
}, },
}, init) },
}) },
{
t.Run("mpeg4audio", func(t *testing.T) { "mpeg4audio",
byts := []byte{ []byte{
0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x18,
'f', 't', 'y', 'p', 'f', 't', 'y', 'p',
0x69, 0x73, 0x6f, 0x35, 0x00, 0x00, 0x00, 0x01, 0x69, 0x73, 0x6f, 0x35, 0x00, 0x00, 0x00, 0x01,
@ -901,13 +902,8 @@ func TestInitUnmarshalExternal(t *testing.T) {
0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
} },
Init{
var init Init
err := init.Unmarshal(byts)
require.NoError(t, err)
require.Equal(t, Init{
Tracks: []*InitTrack{ Tracks: []*InitTrack{
{ {
ID: 257, ID: 257,
@ -925,6 +921,350 @@ func TestInitUnmarshalExternal(t *testing.T) {
}, },
}, },
}, },
}, init) },
},
{
"ignored closed captions",
[]byte{
0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70,
0x6d, 0x70, 0x34, 0x32, 0x00, 0x00, 0x00, 0x01,
0x6d, 0x70, 0x34, 0x31, 0x6d, 0x70, 0x34, 0x32,
0x69, 0x73, 0x6f, 0x6d, 0x68, 0x6c, 0x73, 0x66,
0x00, 0x00, 0x04, 0x3f, 0x6d, 0x6f, 0x6f, 0x76,
0x00, 0x00, 0x00, 0x6c, 0x6d, 0x76, 0x68, 0x64,
0x00, 0x00, 0x00, 0x00, 0xd5, 0x5b, 0xc6, 0x62,
0xd5, 0x5b, 0xc6, 0x62, 0x00, 0x00, 0x02, 0x58,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x02, 0x19,
0x74, 0x72, 0x61, 0x6b, 0x00, 0x00, 0x00, 0x5c,
0x74, 0x6b, 0x68, 0x64, 0x00, 0x00, 0x00, 0x01,
0xd5, 0x5b, 0xc6, 0x62, 0xd5, 0x5b, 0xc6, 0x62,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x07, 0x80, 0x00, 0x00, 0x04, 0x38, 0x00, 0x00,
0x00, 0x00, 0x01, 0xb5, 0x6d, 0x64, 0x69, 0x61,
0x00, 0x00, 0x00, 0x20, 0x6d, 0x64, 0x68, 0x64,
0x00, 0x00, 0x00, 0x00, 0xd5, 0x5b, 0xc6, 0x62,
0xd5, 0x5b, 0xc6, 0x62, 0x00, 0x00, 0x17, 0x70,
0x00, 0x00, 0x00, 0x00, 0x15, 0xc7, 0x00, 0x00,
0x00, 0x00, 0x00, 0x31, 0x68, 0x64, 0x6c, 0x72,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x76, 0x69, 0x64, 0x65, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x43, 0x6f, 0x72, 0x65, 0x20, 0x4d, 0x65, 0x64,
0x69, 0x61, 0x20, 0x56, 0x69, 0x64, 0x65, 0x6f,
0x00, 0x00, 0x00, 0x01, 0x5c, 0x6d, 0x69, 0x6e,
0x66, 0x00, 0x00, 0x00, 0x14, 0x76, 0x6d, 0x68,
0x64, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x24, 0x64, 0x69, 0x6e, 0x66, 0x00, 0x00, 0x00,
0x1c, 0x64, 0x72, 0x65, 0x66, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x0c, 0x75, 0x72, 0x6c, 0x20, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x01, 0x1c, 0x73, 0x74, 0x62,
0x6c, 0x00, 0x00, 0x00, 0xd0, 0x73, 0x74, 0x73,
0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0xc0, 0x61, 0x76, 0x63,
0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x07, 0x80, 0x04, 0x38, 0x00, 0x48, 0x00,
0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x18, 0xff, 0xff, 0x00,
0x00, 0x00, 0x33, 0x61, 0x76, 0x63, 0x43, 0x01,
0x64, 0x00, 0x2a, 0xff, 0xe1, 0x00, 0x1b, 0x27,
0x64, 0x00, 0x2a, 0xac, 0x52, 0x14, 0x07, 0x80,
0x22, 0x7e, 0x5f, 0xfc, 0x00, 0x04, 0x00, 0x05,
0xa8, 0x08, 0x08, 0x0d, 0xb6, 0x15, 0xaf, 0x7b,
0xe0, 0x20, 0x01, 0x00, 0x05, 0x28, 0xf9, 0x09,
0x09, 0xcb, 0x00, 0x00, 0x00, 0x13, 0x63, 0x6f,
0x6c, 0x72, 0x6e, 0x63, 0x6c, 0x78, 0x00, 0x01,
0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x0a, 0x66, 0x69, 0x65, 0x6c, 0x01, 0x00, 0x00,
0x00, 0x00, 0x0a, 0x63, 0x68, 0x72, 0x6d, 0x01,
0x01, 0x00, 0x00, 0x00, 0x10, 0x70, 0x61, 0x73,
0x70, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x10, 0x73, 0x74, 0x74,
0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x73, 0x74, 0x73,
0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x14, 0x73, 0x74, 0x73,
0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x10, 0x73, 0x74, 0x63, 0x6f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x6a, 0x74, 0x72, 0x61, 0x6b, 0x00, 0x00, 0x00,
0x5c, 0x74, 0x6b, 0x68, 0x64, 0x00, 0x00, 0x00,
0x01, 0xd5, 0x5b, 0xc6, 0x62, 0xd5, 0x5b, 0xc6,
0x62, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x06, 0x6d, 0x64, 0x69,
0x61, 0x00, 0x00, 0x00, 0x20, 0x6d, 0x64, 0x68,
0x64, 0x00, 0x00, 0x00, 0x00, 0xd5, 0x5b, 0xc6,
0x62, 0xd5, 0x5b, 0xc6, 0x62, 0x00, 0x00, 0x75,
0x30, 0x00, 0x00, 0x00, 0x00, 0x15, 0xc7, 0x00,
0x00, 0x00, 0x00, 0x00, 0x3a, 0x68, 0x64, 0x6c,
0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x63, 0x6c, 0x63, 0x70, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x43, 0x6f, 0x72, 0x65, 0x20, 0x4d, 0x65,
0x64, 0x69, 0x61, 0x20, 0x43, 0x6c, 0x6f, 0x73,
0x65, 0x64, 0x20, 0x43, 0x61, 0x70, 0x74, 0x69,
0x6f, 0x6e, 0x00, 0x00, 0x00, 0x00, 0xa4, 0x6d,
0x69, 0x6e, 0x66, 0x00, 0x00, 0x00, 0x0c, 0x6e,
0x6d, 0x68, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x24, 0x64, 0x69, 0x6e, 0x66, 0x00,
0x00, 0x00, 0x1c, 0x64, 0x72, 0x65, 0x66, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x0c, 0x75, 0x72, 0x6c, 0x20, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x6c, 0x73,
0x74, 0x62, 0x6c, 0x00, 0x00, 0x00, 0x20, 0x73,
0x74, 0x73, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10, 0x63,
0x36, 0x30, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10, 0x73,
0x74, 0x74, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x73,
0x74, 0x73, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x73,
0x74, 0x73, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x10, 0x73, 0x74, 0x63, 0x6f, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x48, 0x6d, 0x76, 0x65, 0x78, 0x00,
0x00, 0x00, 0x20, 0x74, 0x72, 0x65, 0x78, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x20, 0x74, 0x72, 0x65, 0x78, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
Init{
Tracks: []*InitTrack{{
ID: 1,
TimeScale: 6000,
Format: &format.H264{
PayloadTyp: 96,
PacketizationMode: 1,
SPS: []byte{
0x27, 0x64, 0x00, 0x2a, 0xac, 0x52, 0x14, 0x07,
0x80, 0x22, 0x7e, 0x5f, 0xfc, 0x00, 0x04, 0x00,
0x05, 0xa8, 0x08, 0x08, 0x0d, 0xb6, 0x15, 0xaf,
0x7b, 0xe0, 0x20,
},
PPS: []byte{
0x28, 0xf9, 0x09, 0x09, 0xcb,
},
},
}},
},
},
} {
t.Run(ca.name, func(t *testing.T) {
var init Init
err := init.Unmarshal(ca.byts)
require.NoError(t, err)
require.Equal(t, ca.init, init)
}) })
} }
}
func TestInitUnmarshalErrors(t *testing.T) {
for _, ca := range []struct {
name string
byts []byte
err string
}{
{
"ac3",
[]byte{
0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70,
0x6d, 0x70, 0x34, 0x32, 0x00, 0x00, 0x00, 0x01,
0x6d, 0x70, 0x34, 0x31, 0x6d, 0x70, 0x34, 0x32,
0x69, 0x73, 0x6f, 0x6d, 0x68, 0x6c, 0x73, 0x66,
0x00, 0x00, 0x02, 0x20, 0x6d, 0x6f, 0x6f, 0x76,
0x00, 0x00, 0x00, 0x6c, 0x6d, 0x76, 0x68, 0x64,
0x00, 0x00, 0x00, 0x00, 0xd5, 0x5b, 0xc6, 0x5d,
0xd5, 0x5b, 0xc6, 0x5d, 0x00, 0x00, 0x02, 0x58,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x84,
0x74, 0x72, 0x61, 0x6b, 0x00, 0x00, 0x00, 0x5c,
0x74, 0x6b, 0x68, 0x64, 0x00, 0x00, 0x00, 0x01,
0xd5, 0x5b, 0xc6, 0x5d, 0xd5, 0x5b, 0xc6, 0x5d,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x20, 0x6d, 0x64, 0x69, 0x61,
0x00, 0x00, 0x00, 0x20, 0x6d, 0x64, 0x68, 0x64,
0x00, 0x00, 0x00, 0x00, 0xd5, 0x5b, 0xc6, 0x5d,
0xd5, 0x5b, 0xc6, 0x5d, 0x00, 0x00, 0xbb, 0x80,
0x00, 0x00, 0x00, 0x00, 0x55, 0xc4, 0x00, 0x00,
0x00, 0x00, 0x00, 0x31, 0x68, 0x64, 0x6c, 0x72,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x73, 0x6f, 0x75, 0x6e, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x43, 0x6f, 0x72, 0x65, 0x20, 0x4d, 0x65, 0x64,
0x69, 0x61, 0x20, 0x41, 0x75, 0x64, 0x69, 0x6f,
0x00, 0x00, 0x00, 0x00, 0xc7, 0x6d, 0x69, 0x6e,
0x66, 0x00, 0x00, 0x00, 0x10, 0x73, 0x6d, 0x68,
0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x24, 0x64, 0x69, 0x6e,
0x66, 0x00, 0x00, 0x00, 0x1c, 0x64, 0x72, 0x65,
0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x0c, 0x75, 0x72, 0x6c,
0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x8b, 0x73, 0x74, 0x62, 0x6c, 0x00, 0x00, 0x00,
0x3f, 0x73, 0x74, 0x73, 0x64, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x2f, 0x61, 0x63, 0x2d, 0x33, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,
0x10, 0x00, 0x00, 0x00, 0x00, 0xbb, 0x80, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0b, 0x64, 0x61, 0x63,
0x33, 0x0c, 0x3d, 0x40, 0x00, 0x00, 0x00, 0x10,
0x73, 0x74, 0x74, 0x73, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x73, 0x74, 0x73, 0x63, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14,
0x73, 0x74, 0x73, 0x7a, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x10, 0x73, 0x74, 0x63, 0x6f,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x28, 0x6d, 0x76, 0x65, 0x78,
0x00, 0x00, 0x00, 0x20, 0x74, 0x72, 0x65, 0x78,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
"no tracks found",
},
{
"ac-3",
[]byte{
0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70,
0x6d, 0x70, 0x34, 0x32, 0x00, 0x00, 0x00, 0x01,
0x6d, 0x70, 0x34, 0x31, 0x6d, 0x70, 0x34, 0x32,
0x69, 0x73, 0x6f, 0x6d, 0x68, 0x6c, 0x73, 0x66,
0x00, 0x00, 0x02, 0x22, 0x6d, 0x6f, 0x6f, 0x76,
0x00, 0x00, 0x00, 0x6c, 0x6d, 0x76, 0x68, 0x64,
0x00, 0x00, 0x00, 0x00, 0xd5, 0x5b, 0xc6, 0x5d,
0xd5, 0x5b, 0xc6, 0x5d, 0x00, 0x00, 0x02, 0x58,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x86,
0x74, 0x72, 0x61, 0x6b, 0x00, 0x00, 0x00, 0x5c,
0x74, 0x6b, 0x68, 0x64, 0x00, 0x00, 0x00, 0x01,
0xd5, 0x5b, 0xc6, 0x5d, 0xd5, 0x5b, 0xc6, 0x5d,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x22, 0x6d, 0x64, 0x69, 0x61,
0x00, 0x00, 0x00, 0x20, 0x6d, 0x64, 0x68, 0x64,
0x00, 0x00, 0x00, 0x00, 0xd5, 0x5b, 0xc6, 0x5d,
0xd5, 0x5b, 0xc6, 0x5d, 0x00, 0x00, 0xbb, 0x80,
0x00, 0x00, 0x00, 0x00, 0x55, 0xc4, 0x00, 0x00,
0x00, 0x00, 0x00, 0x31, 0x68, 0x64, 0x6c, 0x72,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x73, 0x6f, 0x75, 0x6e, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x43, 0x6f, 0x72, 0x65, 0x20, 0x4d, 0x65, 0x64,
0x69, 0x61, 0x20, 0x41, 0x75, 0x64, 0x69, 0x6f,
0x00, 0x00, 0x00, 0x00, 0xc9, 0x6d, 0x69, 0x6e,
0x66, 0x00, 0x00, 0x00, 0x10, 0x73, 0x6d, 0x68,
0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x24, 0x64, 0x69, 0x6e,
0x66, 0x00, 0x00, 0x00, 0x1c, 0x64, 0x72, 0x65,
0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x0c, 0x75, 0x72, 0x6c,
0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x8d, 0x73, 0x74, 0x62, 0x6c, 0x00, 0x00, 0x00,
0x41, 0x73, 0x74, 0x73, 0x64, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x31, 0x65, 0x63, 0x2d, 0x33, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,
0x10, 0x00, 0x00, 0x00, 0x00, 0xbb, 0x80, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0d, 0x64, 0x65, 0x63,
0x33, 0x00, 0xc0, 0x20, 0x0f, 0x00, 0x00, 0x00,
0x00, 0x10, 0x73, 0x74, 0x74, 0x73, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x10, 0x73, 0x74, 0x73, 0x63, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x14, 0x73, 0x74, 0x73, 0x7a, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x73, 0x74,
0x63, 0x6f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x6d, 0x76,
0x65, 0x78, 0x00, 0x00, 0x00, 0x20, 0x74, 0x72,
0x65, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
},
"no tracks found",
},
} {
t.Run(ca.name, func(t *testing.T) {
var init Init
err := init.Unmarshal(ca.byts)
require.EqualError(t, err, ca.err)
})
}
}

33
internal/hls/muxer_primary_playlist.go

@ -2,43 +2,14 @@ package hls
import ( import (
"bytes" "bytes"
"encoding/hex"
"io" "io"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"github.com/aler9/gortsplib/v2/pkg/codecs/h265"
"github.com/aler9/gortsplib/v2/pkg/format" "github.com/aler9/gortsplib/v2/pkg/format"
) )
func codecParameters(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 ""
}
type muxerPrimaryPlaylist struct { type muxerPrimaryPlaylist struct {
fmp4 bool fmp4 bool
videoTrack format.Format videoTrack format.Format
@ -67,10 +38,10 @@ func (p *muxerPrimaryPlaylist) file() *MuxerFileResponse {
var codecs []string var codecs []string
if p.videoTrack != nil { if p.videoTrack != nil {
codecs = append(codecs, codecParameters(p.videoTrack)) codecs = append(codecs, codecParametersGenerate(p.videoTrack))
} }
if p.audioTrack != nil { if p.audioTrack != nil {
codecs = append(codecs, codecParameters(p.audioTrack)) codecs = append(codecs, codecParametersGenerate(p.audioTrack))
} }
var version int var version int

Loading…
Cancel
Save