track: add negative tests

This commit is contained in:
aler9
2021-05-07 10:44:00 +02:00
parent 796f862a1f
commit bd1b88226e
2 changed files with 309 additions and 13 deletions

View File

@@ -80,12 +80,12 @@ func (t *Track) IsH264() bool {
func (t *Track) ExtractDataH264() ([]byte, []byte, error) { func (t *Track) ExtractDataH264() ([]byte, []byte, error) {
v, ok := t.Media.Attribute("fmtp") v, ok := t.Media.Attribute("fmtp")
if !ok { if !ok {
return nil, nil, fmt.Errorf("unable to find fmtp") return nil, nil, fmt.Errorf("fmtp attribute is missing")
} }
tmp := strings.SplitN(v, " ", 2) tmp := strings.SplitN(v, " ", 2)
if len(tmp) != 2 { if len(tmp) != 2 {
return nil, nil, fmt.Errorf("unable to parse fmtp (%v)", v) return nil, nil, fmt.Errorf("invalid fmtp attribute (%v)", v)
} }
var sps []byte var sps []byte
@@ -100,30 +100,30 @@ func (t *Track) ExtractDataH264() ([]byte, []byte, error) {
tmp := strings.SplitN(kv, "=", 2) tmp := strings.SplitN(kv, "=", 2)
if len(tmp) != 2 { if len(tmp) != 2 {
return nil, nil, fmt.Errorf("unable to parse fmtp (%v)", v) return nil, nil, fmt.Errorf("invalid fmtp attribute (%v)", v)
} }
if tmp[0] == "sprop-parameter-sets" { if tmp[0] == "sprop-parameter-sets" {
tmp := strings.SplitN(tmp[1], ",", 2) tmp := strings.SplitN(tmp[1], ",", 2)
if len(tmp) != 2 { if len(tmp) != 2 {
return nil, nil, fmt.Errorf("unable to parse sprop-parameter-sets (%v)", v) return nil, nil, fmt.Errorf("invalid sprop-parameter-sets (%v)", v)
} }
var err error var err error
sps, err = base64.StdEncoding.DecodeString(tmp[0]) sps, err = base64.StdEncoding.DecodeString(tmp[0])
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("unable to parse sprop-parameter-sets (%v)", v) return nil, nil, fmt.Errorf("invalid sprop-parameter-sets (%v)", v)
} }
pps, err = base64.StdEncoding.DecodeString(tmp[1]) pps, err = base64.StdEncoding.DecodeString(tmp[1])
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("unable to parse sprop-parameter-sets (%v)", v) return nil, nil, fmt.Errorf("invalid sprop-parameter-sets (%v)", v)
} }
} }
} }
if sps == nil || pps == nil { if sps == nil || pps == nil {
return nil, nil, fmt.Errorf("unable to find SPS or PPS (%v)", v) return nil, nil, fmt.Errorf("sprop-parameter-sets is missing (%v)", v)
} }
return sps, pps, nil return sps, pps, nil
@@ -189,12 +189,12 @@ func (t *Track) IsAAC() bool {
func (t *Track) ExtractDataAAC() ([]byte, error) { func (t *Track) ExtractDataAAC() ([]byte, error) {
v, ok := t.Media.Attribute("fmtp") v, ok := t.Media.Attribute("fmtp")
if !ok { if !ok {
return nil, fmt.Errorf("unable to find fmtp") return nil, fmt.Errorf("fmtp attribute is missing")
} }
tmp := strings.SplitN(v, " ", 2) tmp := strings.SplitN(v, " ", 2)
if len(tmp) != 2 { if len(tmp) != 2 {
return nil, fmt.Errorf("unable to parse fmtp (%v)", v) return nil, fmt.Errorf("invalid fmtp (%v)", v)
} }
var config []byte var config []byte
@@ -208,21 +208,21 @@ func (t *Track) ExtractDataAAC() ([]byte, error) {
tmp := strings.SplitN(kv, "=", 2) tmp := strings.SplitN(kv, "=", 2)
if len(tmp) != 2 { if len(tmp) != 2 {
return nil, fmt.Errorf("unable to parse fmtp (%v)", v) return nil, fmt.Errorf("invalid fmtp (%v)", v)
} }
if tmp[0] == "config" { if tmp[0] == "config" {
var err error var err error
config, err = hex.DecodeString(tmp[1]) config, err = hex.DecodeString(tmp[1])
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to parse config (%v)", v) return nil, fmt.Errorf("invalid config (%v)", v)
} }
break break
} }
} }
if config == nil { if config == nil {
return nil, fmt.Errorf("unable to find config (%v)", v) return nil, fmt.Errorf("config is missing (%v)", v)
} }
return config, nil return config, nil

View File

@@ -281,7 +281,7 @@ func TestTrackH264Extract(t *testing.T) {
}, },
{ {
Key: "fmtp", Key: "fmtp",
Value: "96 96 packetization-mode=1;profile-level-id=64001f;sprop-parameter-sets=Z2QAH6zZQFAFuwFsgAAAAwCAAAAeB4wYyw==,aOvjyyLA;", Value: "96 packetization-mode=1;profile-level-id=64001f;sprop-parameter-sets=Z2QAH6zZQFAFuwFsgAAAAwCAAAAeB4wYyw==,aOvjyyLA;",
}, },
}, },
}, },
@@ -306,6 +306,177 @@ func TestTrackH264Extract(t *testing.T) {
} }
} }
func TestTrackH264ExtractError(t *testing.T) {
for _, ca := range []struct {
name string
track *Track
err string
}{
{
"missing fmtp",
&Track{
Media: &psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "video",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 H264/90000",
},
},
},
},
"fmtp attribute is missing",
},
{
"invalid fmtp",
&Track{
Media: &psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "video",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 H264/90000",
},
{
Key: "fmtp",
Value: "96",
},
},
},
},
"invalid fmtp attribute (96)",
},
{
"fmtp without key",
&Track{
Media: &psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "video",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 H264/90000",
},
{
Key: "fmtp",
Value: "96 packetization-mode",
},
},
},
},
"invalid fmtp attribute (96 packetization-mode)",
},
{
"missing sprop-parameter-set",
&Track{
Media: &psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "video",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 H264/90000",
},
{
Key: "fmtp",
Value: "96 packetization-mode=1",
},
},
},
},
"sprop-parameter-sets is missing (96 packetization-mode=1)",
},
{
"invalid sprop-parameter-set 1",
&Track{
Media: &psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "video",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 H264/90000",
},
{
Key: "fmtp",
Value: "96 sprop-parameter-sets=aaaaaa",
},
},
},
},
"invalid sprop-parameter-sets (96 sprop-parameter-sets=aaaaaa)",
},
{
"invalid sprop-parameter-set 2",
&Track{
Media: &psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "video",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 H264/90000",
},
{
Key: "fmtp",
Value: "96 sprop-parameter-sets=aaaaaa,bbb",
},
},
},
},
"invalid sprop-parameter-sets (96 sprop-parameter-sets=aaaaaa,bbb)",
},
{
"invalid sprop-parameter-set 3",
&Track{
Media: &psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "video",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 H264/90000",
},
{
Key: "fmtp",
Value: "96 sprop-parameter-sets=Z2QAH6zZQFAFuwFsgAAAAwCAAAAeB4wYyw==,bbb",
},
},
},
},
"invalid sprop-parameter-sets (96 sprop-parameter-sets=Z2QAH6zZQFAFuwFsgAAAAwCAAAAeB4wYyw==,bbb)",
},
} {
t.Run(ca.name, func(t *testing.T) {
_, _, err := ca.track.ExtractDataH264()
require.Equal(t, ca.err, err.Error())
})
}
}
func TestTrackAACNew(t *testing.T) { func TestTrackAACNew(t *testing.T) {
tr, err := NewTrackAAC(96, []byte{17, 144}) tr, err := NewTrackAAC(96, []byte{17, 144})
require.NoError(t, err) require.NoError(t, err)
@@ -390,3 +561,128 @@ func TestTrackAACExtract(t *testing.T) {
}) })
} }
} }
func TestTrackAACExtractError(t *testing.T) {
for _, ca := range []struct {
name string
track *Track
err string
}{
{
"missing fmtp",
&Track{
Media: &psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "audio",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 MPEG4-GENERIC/48000/2",
},
},
},
},
"fmtp attribute is missing",
},
{
"invalid fmtp",
&Track{
Media: &psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "audio",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 MPEG4-GENERIC/48000/2",
},
{
Key: "fmtp",
Value: "96",
},
},
},
},
"invalid fmtp (96)",
},
{
"fmtp without key",
&Track{
Media: &psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "audio",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 MPEG4-GENERIC/48000/2",
},
{
Key: "fmtp",
Value: "96 profile-level-id",
},
},
},
},
"invalid fmtp (96 profile-level-id)",
},
{
"missing config",
&Track{
Media: &psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "audio",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 MPEG4-GENERIC/48000/2",
},
{
Key: "fmtp",
Value: "96 profile-level-id=1",
},
},
},
},
"config is missing (96 profile-level-id=1)",
},
{
"invalid config",
&Track{
Media: &psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "audio",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 MPEG4-GENERIC/48000/2",
},
{
Key: "fmtp",
Value: "96 profile-level-id=1; config=zz",
},
},
},
},
"invalid config (96 profile-level-id=1; config=zz)",
},
} {
t.Run(ca.name, func(t *testing.T) {
_, err := ca.track.ExtractDataAAC()
require.Equal(t, ca.err, err.Error())
})
}
}