Skip to content

Commit

Permalink
chore: Add test to JB receiver interceptor read for buffer slice length
Browse files Browse the repository at this point in the history
  • Loading branch information
thatsnotright committed Sep 2, 2024
1 parent 014fb6c commit 3fc6676
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pkg/jitterbuffer/receiver_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func NewInterceptor(opts ...ReceiverInterceptorOption) (*InterceptorFactory, err
return &InterceptorFactory{opts}, nil
}

// BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method
// will be called once per rtp packet.
// BindRemoteStream lets you modify any incoming RTP packets. It is called once per
// RemoteStream. The returned method will be called once per rtp packet.
func (i *ReceiverInterceptor) BindRemoteStream(_ *interceptor.StreamInfo, reader interceptor.RTPReader) interceptor.RTPReader {
return interceptor.RTPReaderFunc(func(b []byte, a interceptor.Attributes) (int, interceptor.Attributes, error) {
buf := make([]byte, len(b))
Expand Down
54 changes: 49 additions & 5 deletions pkg/jitterbuffer/receiver_interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package jitterbuffer

import (
"bytes"
"errors"
"testing"
"time"

Expand All @@ -17,8 +18,6 @@ import (
)

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

factory, err := NewInterceptor(
Log(logging.NewDefaultLoggerFactory().NewLogger("test")),
)
Expand All @@ -27,8 +26,6 @@ func TestBufferStart(t *testing.T) {
i, err := factory.NewInterceptor("")
assert.NoError(t, err)

assert.Zero(t, buf.Len())

stream := test.NewMockStream(&interceptor.StreamInfo{
SSRC: 123456,
ClockRate: 90000,
Expand All @@ -55,7 +52,6 @@ func TestBufferStart(t *testing.T) {
}
err = i.Close()
assert.NoError(t, err)
assert.Zero(t, buf.Len())
}

func TestReceiverBuffersAndPlaysout(t *testing.T) {
Expand Down Expand Up @@ -96,3 +92,51 @@ func TestReceiverBuffersAndPlaysout(t *testing.T) {
err = i.Close()
assert.NoError(t, err)
}

type MockRTPReader struct {
readFunc func([]byte, interceptor.Attributes) (int, interceptor.Attributes, error)
}

func (m *MockRTPReader) Read(data []byte, attrs interceptor.Attributes) (int, interceptor.Attributes, error) {
if m.readFunc != nil {
return m.readFunc(data, attrs)
}
return 0, nil, errors.New("mock function not implemented")

Check failure on line 104 in pkg/jitterbuffer/receiver_interceptor_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

do not define dynamic errors, use wrapped static errors instead: "errors.New(\"mock function not implemented\")" (err113)
}

func NewMockRTPReader(readFunc func([]byte, interceptor.Attributes) (int, interceptor.Attributes, error)) *MockRTPReader {
return &MockRTPReader{
readFunc: readFunc,
}
}

func TestReceiverInterceptorHonorsBufferLength(t *testing.T) {
buf := []byte{0x80, 0x88, 0xe6, 0xfd, 0x01, 0x01, 0x01, 0x01, 0x01,
0xde, 0xad, 0xbe, 0xef, 0x01, 0x01, 0x01, 0x01, 0x01}
readBuf := make([]byte, 2048)
copy(readBuf[0:], buf)
copy(readBuf[17:], buf)
factory, err := NewInterceptor(
Log(logging.NewDefaultLoggerFactory().NewLogger("test")),
)
assert.NoError(t, err)

i, err := factory.NewInterceptor("")

Check failure on line 124 in pkg/jitterbuffer/receiver_interceptor_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

ineffectual assignment to err (ineffassign)

rtpReadFn := NewMockRTPReader(func(data []byte, attrs interceptor.Attributes) (int, interceptor.Attributes, error) {
copy(data, readBuf)
return 7, attrs, nil
})
reader := i.BindRemoteStream(&interceptor.StreamInfo{
SSRC: 123456,
ClockRate: 90000,
}, rtpReadFn)

bufLen, _, err := reader.Read(readBuf, interceptor.Attributes{})
assert.Contains(t, err.Error(), "7 < 12")
assert.Equal(t, 0, bufLen)

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

}

Check failure on line 142 in pkg/jitterbuffer/receiver_interceptor_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

unnecessary trailing newline (whitespace)

0 comments on commit 3fc6676

Please sign in to comment.