Skip to content

Commit

Permalink
tests, validation, format
Browse files Browse the repository at this point in the history
  • Loading branch information
aalekseevx committed Jan 19, 2025
1 parent 6476e79 commit 1adcddf
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 18 deletions.
6 changes: 3 additions & 3 deletions pkg/packetdump/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ type RTPFilterCallback func(pkt *rtp.Packet) bool

// RTCPFilterCallback can be used to filter RTCP packets to dump.
// The callback returns whether or not to print dump the packet's content.
// Deprecated: prefer RTCPFilterPerPacketCallback
// Deprecated: prefer RTCPPerPacketFilterCallback
type RTCPFilterCallback func(pkt []rtcp.Packet) bool

// RTCPFilterPerPacketCallback can be used to filter RTCP packets to dump.
// RTCPPerPacketFilterCallback can be used to filter RTCP packets to dump.
// It's called once per every packet opposing to RTCPFilterCallback which is called once per packet batch
type RTCPFilterPerPacketCallback func(pkt rtcp.Packet) bool
type RTCPPerPacketFilterCallback func(pkt rtcp.Packet) bool
3 changes: 2 additions & 1 deletion pkg/packetdump/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ package packetdump
import (
"fmt"

"github.com/pion/interceptor"
"github.com/pion/rtcp"
"github.com/pion/rtp"

"github.com/pion/interceptor"
)

// RTPFormatCallback can be used to apply custom formatting to each dumped RTP
Expand Down
8 changes: 4 additions & 4 deletions pkg/packetdump/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ func RTPFilter(callback RTPFilterCallback) PacketDumperOption {
}

// RTCPFilter sets the RTCP filter.
// Deprecated: prefer RTCPFilterPerPacket
// Deprecated: prefer RTCPPerPacketFilter
func RTCPFilter(callback RTCPFilterCallback) PacketDumperOption {
return func(d *PacketDumper) error {
d.rtcpFilter = callback
return nil
}
}

// RTCPFilterPerPacket sets the RTCP per-packet filter.
func RTCPFilterPerPacket(callback RTCPFilterPerPacketCallback) PacketDumperOption {
// RTCPPerPacketFilter sets the RTCP per-packet filter.
func RTCPPerPacketFilter(callback RTCPPerPacketFilterCallback) PacketDumperOption {
return func(d *PacketDumper) error {
d.rtcpFilterPerPacket = callback
d.rtcpPerPacketFilter = callback
return nil
}
}
3 changes: 2 additions & 1 deletion pkg/packetdump/packet_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
package packetdump

import (
"github.com/pion/interceptor"
"github.com/pion/rtcp"
"github.com/pion/rtp"

"github.com/pion/interceptor"
)

type rtpDump struct {
Expand Down
16 changes: 12 additions & 4 deletions pkg/packetdump/packet_dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import (
"os"
"sync"

"github.com/pion/interceptor"
"github.com/pion/logging"
"github.com/pion/rtcp"
"github.com/pion/rtp"

"github.com/pion/interceptor"
)

// ErrBothBinaryAndDeprecatedFormat is returned when both binary and deprecated format callbacks are set
var ErrBothBinaryAndDeprecatedFormat = fmt.Errorf("both binary and deprecated format callbacks are set")

// PacketDumper dumps packet to a io.Writer
type PacketDumper struct {
log logging.LeveledLogger
Expand All @@ -36,7 +40,7 @@ type PacketDumper struct {

rtpFilter RTPFilterCallback
rtcpFilter RTCPFilterCallback
rtcpFilterPerPacket RTCPFilterPerPacketCallback
rtcpPerPacketFilter RTCPPerPacketFilterCallback
}

// NewPacketDumper creates a new PacketDumper
Expand All @@ -59,11 +63,15 @@ func NewPacketDumper(opts ...PacketDumperOption) (*PacketDumper, error) {
rtcpFilter: func([]rtcp.Packet) bool {
return true
},
rtcpFilterPerPacket: func(rtcp.Packet) bool {
rtcpPerPacketFilter: func(rtcp.Packet) bool {
return true
},
}

if d.rtpFormat != nil && d.rtpFormatBinary != nil {
return nil, ErrBothBinaryAndDeprecatedFormat

Check warning on line 72 in pkg/packetdump/packet_dumper.go

View check run for this annotation

Codecov / codecov/patch

pkg/packetdump/packet_dumper.go#L72

Added line #L72 was not covered by tests
}

for _, opt := range opts {
if err := opt(d); err != nil {
return nil, err
Expand Down Expand Up @@ -178,7 +186,7 @@ func (d *PacketDumper) writeDumpedRTCP(dump *rtcpDump) error {
}

for _, pkt := range dump.packets {
if !d.rtcpFilterPerPacket(pkt) {
if !d.rtcpPerPacketFilter(pkt) {
continue
}

Expand Down
52 changes: 50 additions & 2 deletions pkg/packetdump/receiver_interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"testing"
"time"

"github.com/pion/interceptor"
"github.com/pion/interceptor/internal/test"
"github.com/pion/logging"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/stretchr/testify/assert"

"github.com/pion/interceptor"
"github.com/pion/interceptor/internal/test"
)

func TestReceiverFilterEverythingOut(t *testing.T) {
Expand Down Expand Up @@ -164,3 +165,50 @@ func TestReceiverCustomBinaryFormatter(t *testing.T) {
assert.Equal(t, []byte{123}, rtpBuf.Bytes())
assert.Equal(t, []byte{45}, rtcpBuf.Bytes())
}

func TestReceiverRTCPPerPacketFilter(t *testing.T) {
buf := bytes.Buffer{}

factory, err := NewReceiverInterceptor(
RTCPWriter(&buf),
Log(logging.NewDefaultLoggerFactory().NewLogger("test")),
RTCPPerPacketFilter(func(packet rtcp.Packet) bool {
_, isPli := packet.(*rtcp.PictureLossIndication)
return isPli
}),
RTCPBinaryFormatter(func(p rtcp.Packet, _ interceptor.Attributes) ([]byte, error) {
assert.IsType(t, &rtcp.PictureLossIndication{}, p)
return []byte{123}, nil
}),
)
assert.NoError(t, err)

i, err := factory.NewInterceptor("")
assert.NoError(t, err)

assert.EqualValues(t, 0, buf.Len())

stream := test.NewMockStream(&interceptor.StreamInfo{
SSRC: 123456,
ClockRate: 90000,
}, i)
defer func() {
assert.NoError(t, stream.Close())
}()

stream.ReceiveRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{
SenderSSRC: 123,
MediaSSRC: 456,
}})
stream.ReceiveRTCP([]rtcp.Packet{&rtcp.ReceiverReport{
SSRC: 789,
}})
// Give time for packets to be handled and stream written to.
time.Sleep(50 * time.Millisecond)

err = i.Close()
assert.NoError(t, err)

// Only single PictureLossIndication should have been written.
assert.Equal(t, []byte{123}, buf.Bytes())
}
3 changes: 2 additions & 1 deletion pkg/packetdump/sender_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
package packetdump

import (
"github.com/pion/interceptor"
"github.com/pion/rtcp"
"github.com/pion/rtp"

"github.com/pion/interceptor"
)

// SenderInterceptorFactory is a interceptor.Factory for a SenderInterceptor
Expand Down
56 changes: 54 additions & 2 deletions pkg/packetdump/sender_interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"testing"
"time"

"github.com/pion/interceptor"
"github.com/pion/interceptor/internal/test"
"github.com/pion/logging"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/stretchr/testify/assert"

"github.com/pion/interceptor"
"github.com/pion/interceptor/internal/test"
)

func TestSenderFilterEverythingOut(t *testing.T) {
Expand Down Expand Up @@ -173,3 +174,54 @@ func TestSenderCustomBinaryFormatter(t *testing.T) {
assert.Equal(t, []byte{123}, rtpBuf.Bytes())
assert.Equal(t, []byte{45}, rtcpBuf.Bytes())
}

func TestSenderRTCPPerPacketFilter(t *testing.T) {
buf := bytes.Buffer{}

factory, err := NewSenderInterceptor(
RTCPWriter(&buf),
Log(logging.NewDefaultLoggerFactory().NewLogger("test")),
RTCPPerPacketFilter(func(packet rtcp.Packet) bool {
_, isPli := packet.(*rtcp.PictureLossIndication)
return isPli
}),
RTCPBinaryFormatter(func(p rtcp.Packet, _ interceptor.Attributes) ([]byte, error) {
assert.IsType(t, &rtcp.PictureLossIndication{}, p)
return []byte{123}, nil
}),
)
assert.NoError(t, err)

i, err := factory.NewInterceptor("")
assert.NoError(t, err)

assert.EqualValues(t, 0, buf.Len())

stream := test.NewMockStream(&interceptor.StreamInfo{
SSRC: 123456,
ClockRate: 90000,
}, i)
defer func() {
assert.NoError(t, stream.Close())
}()

err = stream.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{
SenderSSRC: 123,
MediaSSRC: 456,
}})
assert.NoError(t, err)

err = stream.WriteRTCP([]rtcp.Packet{&rtcp.ReceiverReport{
SSRC: 789,
}})
assert.NoError(t, err)

// Give time for packets to be handled and stream written to.
time.Sleep(50 * time.Millisecond)

err = i.Close()
assert.NoError(t, err)

// Only single PictureLossIndication should have been written.
assert.Equal(t, []byte{123}, buf.Bytes())
}

0 comments on commit 1adcddf

Please sign in to comment.