From 7d0e8ed05844c8c48a18b26f5337e6701a53614d Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Tue, 14 Jun 2022 12:43:48 +0200 Subject: [PATCH] improve tests --- pkg/rtpcleaner/cleaner.go | 2 +- pkg/rtpcleaner/cleaner_test.go | 15 ++++++++++ track_h264_test.go | 3 +- track_h265_test.go | 51 ++++++++++++++++++++++++++++++++++ track_jpeg.go | 2 +- track_jpeg_test.go | 43 ++++++++++++++++++++++++++++ track_pcma_test.go | 43 ++++++++++++++++++++++++++++ track_pcmu_test.go | 43 ++++++++++++++++++++++++++++ track_test.go | 25 ++++++++++++----- 9 files changed, 217 insertions(+), 10 deletions(-) create mode 100644 track_h265_test.go create mode 100644 track_jpeg_test.go create mode 100644 track_pcma_test.go create mode 100644 track_pcmu_test.go diff --git a/pkg/rtpcleaner/cleaner.go b/pkg/rtpcleaner/cleaner.go index e9b2b9cf..2596f844 100644 --- a/pkg/rtpcleaner/cleaner.go +++ b/pkg/rtpcleaner/cleaner.go @@ -26,7 +26,7 @@ type Output struct { // Cleaner is used to clean incoming RTP packets, in order to: // - remove padding -// - re-encode packets if they are bigger than maximum allowed. +// - re-encode them if they are bigger than maximum allowed type Cleaner struct { isH264 bool isTCP bool diff --git a/pkg/rtpcleaner/cleaner_test.go b/pkg/rtpcleaner/cleaner_test.go index a7070f91..0797d228 100644 --- a/pkg/rtpcleaner/cleaner_test.go +++ b/pkg/rtpcleaner/cleaner_test.go @@ -37,6 +37,21 @@ func TestRemovePadding(t *testing.T) { }}, out) } +func TestGenericOversized(t *testing.T) { + cleaner := NewCleaner(false, true) + + _, err := cleaner.Clear(&rtp.Packet{ + Header: rtp.Header{ + Version: 2, + PayloadType: 96, + Marker: false, + SequenceNumber: 34572, + }, + Payload: bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04, 0x05}, 2050/5), + }) + require.EqualError(t, err, "payload size (2062) greater than maximum allowed (1472)") +} + func TestH264Oversized(t *testing.T) { cleaner := NewCleaner(true, true) diff --git a/track_h264_test.go b/track_h264_test.go index e0565b7d..afa30203 100644 --- a/track_h264_test.go +++ b/track_h264_test.go @@ -158,7 +158,8 @@ func TestTrackH264GetSPSPPSErrors(t *testing.T) { }, } { t.Run(ca.name, func(t *testing.T) { - _, _, err := trackH264GetSPSPPS(ca.md) + tr := &TrackH264{} + err := tr.fillParamsFromMediaDescription(ca.md) require.EqualError(t, err, ca.err) }) } diff --git a/track_h265_test.go b/track_h265_test.go new file mode 100644 index 00000000..558e3407 --- /dev/null +++ b/track_h265_test.go @@ -0,0 +1,51 @@ +package gortsplib + +import ( + "testing" + + psdp "github.com/pion/sdp/v3" + "github.com/stretchr/testify/require" +) + +func TestTrackH265New(t *testing.T) { + track := NewTrackH265(96, + []byte{0x01, 0x02}, []byte{0x03, 0x04}, []byte{0x05, 0x06}) + require.Equal(t, "", track.GetControl()) + require.Equal(t, []byte{0x01, 0x02}, track.VPS()) + require.Equal(t, []byte{0x03, 0x04}, track.SPS()) + require.Equal(t, []byte{0x05, 0x06}, track.PPS()) +} + +func TestTrackH265Clone(t *testing.T) { + track := NewTrackH265(96, []byte{0x01, 0x02}, []byte{0x03, 0x04}, []byte{0x05, 0x06}) + + clone := track.clone() + require.NotSame(t, track, clone) + require.Equal(t, track, clone) +} + +func TestTrackH265MediaDescription(t *testing.T) { + track := NewTrackH265(96, []byte{0x01, 0x02}, []byte{0x03, 0x04}, []byte{0x05, 0x06}) + + require.Equal(t, &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "video", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"96"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "96 H265/90000", + }, + { + Key: "fmtp", + Value: "96 sprop-vps=AQI=; sprop-sps=AwQ=; sprop-pps=BQY=", + }, + { + Key: "control", + Value: "", + }, + }, + }, track.MediaDescription()) +} diff --git a/track_jpeg.go b/track_jpeg.go index a59cea8c..2dddce59 100644 --- a/track_jpeg.go +++ b/track_jpeg.go @@ -39,7 +39,7 @@ func (t *TrackJPEG) clone() Track { func (t *TrackJPEG) MediaDescription() *psdp.MediaDescription { return &psdp.MediaDescription{ MediaName: psdp.MediaName{ - Media: "audio", + Media: "video", Protos: []string{"RTP", "AVP"}, Formats: []string{"26"}, }, diff --git a/track_jpeg_test.go b/track_jpeg_test.go new file mode 100644 index 00000000..9d324073 --- /dev/null +++ b/track_jpeg_test.go @@ -0,0 +1,43 @@ +package gortsplib + +import ( + "testing" + + psdp "github.com/pion/sdp/v3" + "github.com/stretchr/testify/require" +) + +func TestTrackJPEGNew(t *testing.T) { + track := NewTrackJPEG() + require.Equal(t, "", track.GetControl()) +} + +func TestTrackJPEGClone(t *testing.T) { + track := NewTrackJPEG() + + clone := track.clone() + require.NotSame(t, track, clone) + require.Equal(t, track, clone) +} + +func TestTrackJPEGMediaDescription(t *testing.T) { + track := NewTrackJPEG() + + require.Equal(t, &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "video", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"26"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "26 JPEG/90000", + }, + { + Key: "control", + Value: "", + }, + }, + }, track.MediaDescription()) +} diff --git a/track_pcma_test.go b/track_pcma_test.go new file mode 100644 index 00000000..ce8a194e --- /dev/null +++ b/track_pcma_test.go @@ -0,0 +1,43 @@ +package gortsplib + +import ( + "testing" + + psdp "github.com/pion/sdp/v3" + "github.com/stretchr/testify/require" +) + +func TestTrackPCMANew(t *testing.T) { + track := NewTrackPCMA() + require.Equal(t, "", track.GetControl()) +} + +func TestTrackPCMAClone(t *testing.T) { + track := NewTrackPCMA() + + clone := track.clone() + require.NotSame(t, track, clone) + require.Equal(t, track, clone) +} + +func TestTrackPCMAMediaDescription(t *testing.T) { + track := NewTrackPCMA() + + require.Equal(t, &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "audio", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"8"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "8 PCMA/8000", + }, + { + Key: "control", + Value: "", + }, + }, + }, track.MediaDescription()) +} diff --git a/track_pcmu_test.go b/track_pcmu_test.go new file mode 100644 index 00000000..7e2c5a61 --- /dev/null +++ b/track_pcmu_test.go @@ -0,0 +1,43 @@ +package gortsplib + +import ( + "testing" + + psdp "github.com/pion/sdp/v3" + "github.com/stretchr/testify/require" +) + +func TestTrackPCMUNew(t *testing.T) { + track := NewTrackPCMU() + require.Equal(t, "", track.GetControl()) +} + +func TestTrackPCMUClone(t *testing.T) { + track := NewTrackPCMU() + + clone := track.clone() + require.NotSame(t, track, clone) + require.Equal(t, track, clone) +} + +func TestTrackPCMUMediaDescription(t *testing.T) { + track := NewTrackPCMU() + + require.Equal(t, &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "audio", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"0"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "0 PCMU/8000", + }, + { + Key: "control", + Value: "", + }, + }, + }, track.MediaDescription()) +} diff --git a/track_test.go b/track_test.go index 1255f935..fb10187e 100644 --- a/track_test.go +++ b/track_test.go @@ -339,13 +339,24 @@ func TestTrackNewFromMediaDescription(t *testing.T) { }, }, }, - &TrackGeneric{ - clockRate: 90000, - media: "video", - formats: []string{"96"}, - rtpmap: "96 H265/90000", - fmtp: "96 sprop-vps=QAEMAf//AWAAAAMAkAAAAwAAAwB4mZgJ; " + - "sprop-sps=QgEBAWAAAAMAkAAAAwAAAwB4oAPAgBDllmZpJMrgEAAAAwAQAAADAeCA; sprop-pps=RAHBcrRiQA==", + &TrackH265{ + payloadType: 96, + vps: []byte{ + 0x40, 0x1, 0xc, 0x1, 0xff, 0xff, 0x1, 0x60, + 0x0, 0x0, 0x3, 0x0, 0x90, 0x0, 0x0, 0x3, + 0x0, 0x0, 0x3, 0x0, 0x78, 0x99, 0x98, 0x9, + }, + sps: []byte{ + 0x42, 0x1, 0x1, 0x1, 0x60, 0x0, 0x0, 0x3, + 0x0, 0x90, 0x0, 0x0, 0x3, 0x0, 0x0, 0x3, + 0x0, 0x78, 0xa0, 0x3, 0xc0, 0x80, 0x10, 0xe5, + 0x96, 0x66, 0x69, 0x24, 0xca, 0xe0, 0x10, 0x0, + 0x0, 0x3, 0x0, 0x10, 0x0, 0x0, 0x3, 0x1, + 0xe0, 0x80, + }, + pps: []byte{ + 0x44, 0x1, 0xc1, 0x72, 0xb4, 0x62, 0x40, + }, }, }, {