From 4e7d5cbf8b044d7dd1d2ef78c94d4f99c79f7f5e Mon Sep 17 00:00:00 2001 From: Woodrow Douglass Date: Thu, 28 Mar 2019 10:46:36 -0400 Subject: [PATCH] Test the packetizer, and reverse an import dependancy Relates to #15 --- codecs/opus_packet.go | 9 +++----- codecs/opus_packet_test.go | 11 ++-------- codecs/vp8_packet.go | 8 +++----- codecs/vp8_packet_test.go | 42 +++++++++----------------------------- packetizer_test.go | 18 ++++++++++++++++ 5 files changed, 36 insertions(+), 52 deletions(-) diff --git a/codecs/opus_packet.go b/codecs/opus_packet.go index 8d1965e..68af893 100644 --- a/codecs/opus_packet.go +++ b/codecs/opus_packet.go @@ -2,8 +2,6 @@ package codecs import ( "fmt" - - "github.com/pions/rtp" ) // OpusPayloader payloads Opus packets @@ -26,13 +24,12 @@ type OpusPacket struct { } // Unmarshal parses the passed byte slice and stores the result in the OpusPacket this method is called upon -func (p *OpusPacket) Unmarshal(packet *rtp.Packet) ([]byte, error) { +func (p *OpusPacket) Unmarshal(packet []byte) ([]byte, error) { if packet == nil { return nil, fmt.Errorf("invalid nil packet") } - if packet.Payload == nil { + if len(packet) == 0 { return nil, fmt.Errorf("Payload is not large enough") } - p.Payload = packet.Payload - return p.Payload, nil + return packet, nil } diff --git a/codecs/opus_packet_test.go b/codecs/opus_packet_test.go index 309ec84..37f02e9 100644 --- a/codecs/opus_packet_test.go +++ b/codecs/opus_packet_test.go @@ -3,8 +3,6 @@ package codecs import ( "fmt" "testing" - - "github.com/pions/rtp" ) func TestOpusPacket_Unmarshal(t *testing.T) { @@ -23,9 +21,7 @@ func TestOpusPacket_Unmarshal(t *testing.T) { } // Empty packet - raw, err = pck.Unmarshal(&rtp.Packet{ - Payload: nil, - }) + raw, err = pck.Unmarshal([]byte{}) if raw != nil { t.Fatal("Result should be nil in case of error") } @@ -34,9 +30,7 @@ func TestOpusPacket_Unmarshal(t *testing.T) { } // Normal packet - raw, err = pck.Unmarshal(&rtp.Packet{ - Payload: []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x90}, - }) + raw, err = pck.Unmarshal([]byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x90}) if raw == nil { t.Fatal("Result shouldn't be nil in case of success") } @@ -55,7 +49,6 @@ func TestOpusPayloader_Payload(t *testing.T) { t.Fatal("Generated payload should be empty") } - // Note: MTU has no effect // Positive MTU, small payload res = pck.Payload(1, payload) if len(res) != 1 { diff --git a/codecs/vp8_packet.go b/codecs/vp8_packet.go index ad4df6b..5e71b64 100644 --- a/codecs/vp8_packet.go +++ b/codecs/vp8_packet.go @@ -2,8 +2,6 @@ package codecs import ( "fmt" - - "github.com/pions/rtp" ) // VP8Payloader payloads VP8 packets @@ -86,11 +84,11 @@ type VP8Packet struct { } // Unmarshal parses the passed byte slice and stores the result in the VP8Packet this method is called upon -func (p *VP8Packet) Unmarshal(packet *rtp.Packet) ([]byte, error) { - if packet == nil { +func (p *VP8Packet) Unmarshal(payload []byte) ([]byte, error) { + if payload == nil { return nil, fmt.Errorf("invalid nil packet") } - payload := packet.Payload + payloadLen := len(payload) if payloadLen < 4 { diff --git a/codecs/vp8_packet_test.go b/codecs/vp8_packet_test.go index 840d788..9a8cf16 100644 --- a/codecs/vp8_packet_test.go +++ b/codecs/vp8_packet_test.go @@ -3,8 +3,6 @@ package codecs import ( "fmt" "testing" - - "github.com/pions/rtp" ) func TestVP8Packet_Unmarshal(t *testing.T) { @@ -24,9 +22,7 @@ func TestVP8Packet_Unmarshal(t *testing.T) { } // Nil payload - raw, err = pck.Unmarshal(&rtp.Packet{ - Payload: nil, - }) + raw, err = pck.Unmarshal([]byte{}) if raw != nil { t.Fatal("Result should be nil in case of error") } @@ -35,9 +31,7 @@ func TestVP8Packet_Unmarshal(t *testing.T) { } // Payload smaller than header size - raw, err = pck.Unmarshal(&rtp.Packet{ - Payload: []byte{0x00, 0x11, 0x22}, - }) + raw, err = pck.Unmarshal([]byte{0x00, 0x11, 0x22}) if raw != nil { t.Fatal("Result should be nil in case of error") } @@ -46,9 +40,7 @@ func TestVP8Packet_Unmarshal(t *testing.T) { } // Normal payload - raw, err = pck.Unmarshal(&rtp.Packet{ - Payload: []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x90}, - }) + raw, err = pck.Unmarshal([]byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x90}) if raw == nil { t.Fatal("Result shouldn't be nil in case of success") } @@ -57,9 +49,7 @@ func TestVP8Packet_Unmarshal(t *testing.T) { } // Header size, only X - raw, err = pck.Unmarshal(&rtp.Packet{ - Payload: []byte{0x80, 0x00, 0x00, 0x00}, - }) + raw, err = pck.Unmarshal([]byte{0x80, 0x00, 0x00, 0x00}) if raw == nil { t.Fatal("Result shouldn't be nil in case of success") } @@ -68,9 +58,7 @@ func TestVP8Packet_Unmarshal(t *testing.T) { } // Header size, X and I - raw, err = pck.Unmarshal(&rtp.Packet{ - Payload: []byte{0x80, 0x80, 0x00, 0x00}, - }) + raw, err = pck.Unmarshal([]byte{0x80, 0x80, 0x00, 0x00}) if raw == nil { t.Fatal("Result shouldn't be nil in case of success") } @@ -79,9 +67,7 @@ func TestVP8Packet_Unmarshal(t *testing.T) { } // Header size, X and I, PID 16bits - raw, err = pck.Unmarshal(&rtp.Packet{ - Payload: []byte{0x80, 0x80, 0x81, 0x00}, - }) + raw, err = pck.Unmarshal([]byte{0x80, 0x80, 0x81, 0x00}) if raw != nil { t.Fatal("Result should be nil in case of error") } @@ -90,9 +76,7 @@ func TestVP8Packet_Unmarshal(t *testing.T) { } // Header size, X and L - raw, err = pck.Unmarshal(&rtp.Packet{ - Payload: []byte{0x80, 0x40, 0x00, 0x00}, - }) + raw, err = pck.Unmarshal([]byte{0x80, 0x40, 0x00, 0x00}) if raw == nil { t.Fatal("Result shouldn't be nil in case of success") } @@ -101,9 +85,7 @@ func TestVP8Packet_Unmarshal(t *testing.T) { } // Header size, X and T - raw, err = pck.Unmarshal(&rtp.Packet{ - Payload: []byte{0x80, 0x20, 0x00, 0x00}, - }) + raw, err = pck.Unmarshal([]byte{0x80, 0x20, 0x00, 0x00}) if raw == nil { t.Fatal("Result shouldn't be nil in case of success") } @@ -112,9 +94,7 @@ func TestVP8Packet_Unmarshal(t *testing.T) { } // Header size, X and K - raw, err = pck.Unmarshal(&rtp.Packet{ - Payload: []byte{0x80, 0x10, 0x00, 0x00}, - }) + raw, err = pck.Unmarshal([]byte{0x80, 0x10, 0x00, 0x00}) if raw == nil { t.Fatal("Result shouldn't be nil in case of success") } @@ -123,9 +103,7 @@ func TestVP8Packet_Unmarshal(t *testing.T) { } // Header size, all flags - raw, err = pck.Unmarshal(&rtp.Packet{ - Payload: []byte{0xff, 0xff, 0x00, 0x00}, - }) + raw, err = pck.Unmarshal([]byte{0xff, 0xff, 0x00, 0x00}) if raw != nil { t.Fatal("Result should be nil in case of error") } diff --git a/packetizer_test.go b/packetizer_test.go index 0e20a86..526fc30 100644 --- a/packetizer_test.go +++ b/packetizer_test.go @@ -1,9 +1,11 @@ package rtp import ( + "fmt" "testing" "time" + "github.com/pions/rtp/codecs" "github.com/stretchr/testify/assert" ) @@ -24,3 +26,19 @@ func TestNtpConversion(t *testing.T) { assert.Equal(t, in.n, out) } } + +func TestPacketizer(t *testing.T) { + multiplepayload := make([]byte, 128) + //use the G722 payloader here, because it's very simple and all 0s is valid G722 data. + packetizer := NewPacketizer(100, 98, 0x1234ABCD, &codecs.G722Payloader{}, NewRandomSequencer(), 90000) + packets := packetizer.Packetize(multiplepayload, 2000) + + if len(packets) != 2 { + packetlengths := "" + for i := 0; i < len(packets); i += 1 { + packetlengths += fmt.Sprintf("Packet %d length %d\n", i, len(packets[i].Payload)) + } + t.Fatalf("Generated %d packets instead of 2\n%s", len(packets), packetlengths) + } + +}