Skip to content

Commit

Permalink
Adding String() method to Packet interface
Browse files Browse the repository at this point in the history
Nearly all RTCP packet types already include String() methods, which
are quite valuable for diagnostic and logging purposes. This change
updates the base Packet interface to include String() so that it
can be used by applications without having to perform a complex set
of type assertions.

As a minor bit of housekeeping, it also moves assertions that the
Packet interface is satisfied from implementation files into test
files.
  • Loading branch information
adamroach committed Sep 3, 2021
1 parent 35bb5c7 commit fb051e5
Show file tree
Hide file tree
Showing 25 changed files with 49 additions and 24 deletions.
15 changes: 13 additions & 2 deletions compound_packet.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package rtcp

import (
"strings"
)

// A CompoundPacket is a collection of RTCP packets transmitted as a single packet with
// the underlying protocol (for example UDP).
//
Expand All @@ -14,8 +18,6 @@ package rtcp
// Other RTCP packet types may follow in any order. Packet types may appear more than once.
type CompoundPacket []Packet

var _ Packet = (*CompoundPacket)(nil) // assert is a Packet

// Validate returns an error if this is not an RFC-compliant CompoundPacket.
func (c CompoundPacket) Validate() error {
if len(c) == 0 {
Expand Down Expand Up @@ -134,3 +136,12 @@ func (c CompoundPacket) DestinationSSRC() []uint32 {

return c[0].DestinationSSRC()
}

func (c CompoundPacket) String() string {
out := "CompoundPacket\n"
for _, p := range c {
out += p.String()
}
out = strings.TrimSuffix(strings.ReplaceAll(out, "\n", "\n\t"), "\t")
return out
}
2 changes: 2 additions & 0 deletions compound_packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/stretchr/testify/assert"
)

var _ Packet = (*CompoundPacket)(nil) // assert is a Packet

func TestReadEOF(t *testing.T) {
shortHeader := []byte{
0x81, 0xc9, // missing type & len
Expand Down
13 changes: 11 additions & 2 deletions goodbye.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rtcp

import (
"encoding/binary"
"fmt"
)

// The Goodbye packet indicates that one or more sources are no longer active.
Expand All @@ -12,8 +13,6 @@ type Goodbye struct {
Reason string
}

var _ Packet = (*Goodbye)(nil) // assert is a Packet

// Marshal encodes the Goodbye packet in binary
func (g Goodbye) Marshal() ([]byte, error) {
/*
Expand Down Expand Up @@ -144,3 +143,13 @@ func (g *Goodbye) DestinationSSRC() []uint32 {
copy(out, g.Sources)
return out
}

func (g Goodbye) String() string {
out := "Goodbye\n"
for i, s := range g.Sources {
out += fmt.Sprintf("\tSource %d: %x\n", i, s)
}
out += fmt.Sprintf("\tReason: %s\n", g.Reason)

return out
}
2 changes: 2 additions & 0 deletions goodbye_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
)

var _ Packet = (*Goodbye)(nil) // assert is a Packet

func TestGoodbyeUnmarshal(t *testing.T) {
for _, test := range []struct {
Name string
Expand Down
1 change: 1 addition & 0 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Packet interface {

Marshal() ([]byte, error)
Unmarshal(rawPacket []byte) error
String() string
}

// Unmarshal takes an entire udp datagram (which may consist of multiple RTCP packets) and
Expand Down
2 changes: 0 additions & 2 deletions picture_loss_indication.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ type PictureLossIndication struct {
MediaSSRC uint32
}

var _ Packet = (*PictureLossIndication)(nil) // assert is a Packet

const (
pliLength = 2
)
Expand Down
2 changes: 2 additions & 0 deletions picture_loss_indication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
)

var _ Packet = (*PictureLossIndication)(nil) // assert is a Packet

func TestPictureLossIndicationUnmarshal(t *testing.T) {
for _, test := range []struct {
Name string
Expand Down
2 changes: 0 additions & 2 deletions rapid_resynchronization_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ type RapidResynchronizationRequest struct {
MediaSSRC uint32
}

var _ Packet = (*RapidResynchronizationRequest)(nil) // assert is a Packet

const (
rrrLength = 2
rrrHeaderLength = ssrcLength * 2
Expand Down
2 changes: 2 additions & 0 deletions rapid_resynchronization_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
)

var _ Packet = (*RapidResynchronizationRequest)(nil) // assert is a Packet

func TestRapidResynchronizationRequestUnmarshal(t *testing.T) {
for _, test := range []struct {
Name string
Expand Down
2 changes: 0 additions & 2 deletions raw_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import "fmt"
// a packet with an unknown type is encountered.
type RawPacket []byte

var _ Packet = (*RawPacket)(nil) // assert is a Packet

// Marshal encodes the packet in binary.
func (r RawPacket) Marshal() ([]byte, error) {
return r, nil
Expand Down
2 changes: 2 additions & 0 deletions raw_packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
)

var _ Packet = (*RawPacket)(nil) // assert is a Packet

func TestRawPacketRoundTrip(t *testing.T) {
for _, test := range []struct {
Name string
Expand Down
2 changes: 0 additions & 2 deletions receiver_estimated_maximum_bitrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ type ReceiverEstimatedMaximumBitrate struct {
SSRCs []uint32
}

var _ Packet = (*ReceiverEstimatedMaximumBitrate)(nil) // assert is a Packet

// Marshal serializes the packet and returns a byte slice.
func (p ReceiverEstimatedMaximumBitrate) Marshal() (buf []byte, err error) {
// Allocate a buffer of the exact output size.
Expand Down
2 changes: 2 additions & 0 deletions receiver_estimated_maximum_bitrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/stretchr/testify/assert"
)

var _ Packet = (*ReceiverEstimatedMaximumBitrate)(nil) // assert is a Packet

func TestReceiverEstimatedMaximumBitrateMarshal(t *testing.T) {
assert := assert.New(t)

Expand Down
2 changes: 0 additions & 2 deletions receiver_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ type ReceiverReport struct {
ProfileExtensions []byte
}

var _ Packet = (*ReceiverReport)(nil) // assert is a Packet

const (
ssrcLength = 4
rrSSRCOffset = headerLength
Expand Down
2 changes: 2 additions & 0 deletions receiver_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
)

var _ Packet = (*ReceiverReport)(nil) // assert is a Packet

func TestReceiverReportUnmarshal(t *testing.T) {
for _, test := range []struct {
Name string
Expand Down
2 changes: 0 additions & 2 deletions sender_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ type SenderReport struct {
ProfileExtensions []byte
}

var _ Packet = (*SenderReport)(nil) // assert is a Packet

const (
srHeaderLength = 24
srSSRCOffset = 0
Expand Down
2 changes: 2 additions & 0 deletions sender_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
)

var _ Packet = (*SenderReport)(nil) // assert is a Packet

func TestSenderReportUnmarshal(t *testing.T) {
for _, test := range []struct {
Name string
Expand Down
2 changes: 0 additions & 2 deletions slice_loss_indication.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ type SliceLossIndication struct {
SLI []SLIEntry
}

var _ Packet = (*SliceLossIndication)(nil) // assert is a Packet

const (
sliLength = 2
sliOffset = 8
Expand Down
2 changes: 2 additions & 0 deletions slice_loss_indication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
)

var _ Packet = (*SliceLossIndication)(nil) // assert is a Packet

func TestSliceLossIndicationUnmarshal(t *testing.T) {
for _, test := range []struct {
Name string
Expand Down
2 changes: 0 additions & 2 deletions source_description.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ type SourceDescription struct {
Chunks []SourceDescriptionChunk
}

var _ Packet = (*SourceDescription)(nil) // assert is a Packet

// Marshal encodes the SourceDescription in binary
func (s SourceDescription) Marshal() ([]byte, error) {
/*
Expand Down
2 changes: 2 additions & 0 deletions source_description_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
)

var _ Packet = (*SourceDescription)(nil) // assert is a Packet

func TestSourceDescriptionUnmarshal(t *testing.T) {
for _, test := range []struct {
Name string
Expand Down
2 changes: 0 additions & 2 deletions transport_layer_cc.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ func numOfBitsOfSymbolSize() map[uint16]uint16 {
}
}

var _ Packet = (*TransportLayerCC)(nil) // assert is a Packet

var (
errPacketStatusChunkLength = errors.New("packet status chunk must be 2 bytes")
errDeltaExceedLimit = errors.New("delta exceed limit")
Expand Down
2 changes: 2 additions & 0 deletions transport_layer_cc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"
)

var _ Packet = (*TransportLayerCC)(nil) // assert is a Packet

func TestTransportLayerCC_RunLengthChunkUnmarshal(t *testing.T) {
for _, test := range []struct {
Name string
Expand Down
2 changes: 0 additions & 2 deletions transport_layer_nack.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ type TransportLayerNack struct {
Nacks []NackPair
}

var _ Packet = (*TransportLayerNack)(nil) // assert is a Packet

// NackPairsFromSequenceNumbers generates a slice of NackPair from a list of SequenceNumbers
// This handles generating the proper values for PacketID/LostPackets
func NackPairsFromSequenceNumbers(sequenceNumbers []uint16) (pairs []NackPair) {
Expand Down
2 changes: 2 additions & 0 deletions transport_layer_nack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
)

var _ Packet = (*TransportLayerNack)(nil) // assert is a Packet

func TestTransportLayerNackUnmarshal(t *testing.T) {
for _, test := range []struct {
Name string
Expand Down

0 comments on commit fb051e5

Please sign in to comment.