Skip to content

Commit

Permalink
Test the packetizer, and reverse an import dependancy
Browse files Browse the repository at this point in the history
Relates to #15
  • Loading branch information
wdouglass committed Mar 28, 2019
1 parent 8331ee0 commit 4e7d5cb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 52 deletions.
9 changes: 3 additions & 6 deletions codecs/opus_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package codecs

import (
"fmt"

"github.com/pions/rtp"
)

// OpusPayloader payloads Opus packets
Expand All @@ -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
}
11 changes: 2 additions & 9 deletions codecs/opus_packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package codecs
import (
"fmt"
"testing"

"github.com/pions/rtp"
)

func TestOpusPacket_Unmarshal(t *testing.T) {
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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 {
Expand Down
8 changes: 3 additions & 5 deletions codecs/vp8_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package codecs

import (
"fmt"

"github.com/pions/rtp"
)

// VP8Payloader payloads VP8 packets
Expand Down Expand Up @@ -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 {
Expand Down
42 changes: 10 additions & 32 deletions codecs/vp8_packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package codecs
import (
"fmt"
"testing"

"github.com/pions/rtp"
)

func TestVP8Packet_Unmarshal(t *testing.T) {
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand Down
18 changes: 18 additions & 0 deletions packetizer_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package rtp

import (
"fmt"
"testing"
"time"

"github.com/pions/rtp/codecs"
"github.com/stretchr/testify/assert"
)

Expand All @@ -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)
}

}

0 comments on commit 4e7d5cb

Please sign in to comment.