Skip to content

Commit 561796c

Browse files
committed
chore: Add test to JB receiver interceptor read
Ensure reads only use length returned from upstream
1 parent 014fb6c commit 561796c

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

pkg/jitterbuffer/receiver_interceptor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ func NewInterceptor(opts ...ReceiverInterceptorOption) (*InterceptorFactory, err
6464
return &InterceptorFactory{opts}, nil
6565
}
6666

67-
// BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method
68-
// will be called once per rtp packet.
67+
// BindRemoteStream lets you modify any incoming RTP packets. It is called once per
68+
// RemoteStream. The returned method will be called once per rtp packet.
6969
func (i *ReceiverInterceptor) BindRemoteStream(_ *interceptor.StreamInfo, reader interceptor.RTPReader) interceptor.RTPReader {
7070
return interceptor.RTPReaderFunc(func(b []byte, a interceptor.Attributes) (int, interceptor.Attributes, error) {
7171
buf := make([]byte, len(b))

pkg/jitterbuffer/receiver_interceptor_test.go

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package jitterbuffer
55

66
import (
77
"bytes"
8+
"errors"
89
"testing"
910
"time"
1011

@@ -17,8 +18,6 @@ import (
1718
)
1819

1920
func TestBufferStart(t *testing.T) {
20-
buf := bytes.Buffer{}
21-
2221
factory, err := NewInterceptor(
2322
Log(logging.NewDefaultLoggerFactory().NewLogger("test")),
2423
)
@@ -27,8 +26,6 @@ func TestBufferStart(t *testing.T) {
2726
i, err := factory.NewInterceptor("")
2827
assert.NoError(t, err)
2928

30-
assert.Zero(t, buf.Len())
31-
3229
stream := test.NewMockStream(&interceptor.StreamInfo{
3330
SSRC: 123456,
3431
ClockRate: 90000,
@@ -55,7 +52,6 @@ func TestBufferStart(t *testing.T) {
5552
}
5653
err = i.Close()
5754
assert.NoError(t, err)
58-
assert.Zero(t, buf.Len())
5955
}
6056

6157
func TestReceiverBuffersAndPlaysout(t *testing.T) {
@@ -96,3 +92,51 @@ func TestReceiverBuffersAndPlaysout(t *testing.T) {
9692
err = i.Close()
9793
assert.NoError(t, err)
9894
}
95+
96+
type MockRTPReader struct {
97+
readFunc func([]byte, interceptor.Attributes) (int, interceptor.Attributes, error)
98+
}
99+
100+
func (m *MockRTPReader) Read(data []byte, attrs interceptor.Attributes) (int, interceptor.Attributes, error) {
101+
if m.readFunc != nil {
102+
return m.readFunc(data, attrs)
103+
}
104+
return 0, nil, errors.New("mock function not implemented")
105+
}
106+
107+
func NewMockRTPReader(readFunc func([]byte, interceptor.Attributes) (int, interceptor.Attributes, error)) *MockRTPReader {
108+
return &MockRTPReader{
109+
readFunc: readFunc,
110+
}
111+
}
112+
113+
func TestReceiverInterceptorHonorsBufferLength(t *testing.T) {
114+
buf := []byte{0x80, 0x88, 0xe6, 0xfd, 0x01, 0x01, 0x01, 0x01, 0x01,
115+
0xde, 0xad, 0xbe, 0xef, 0x01, 0x01, 0x01, 0x01, 0x01}
116+
readBuf := make([]byte, 2048)
117+
copy(readBuf[0:], buf)
118+
copy(readBuf[17:], buf)
119+
factory, err := NewInterceptor(
120+
Log(logging.NewDefaultLoggerFactory().NewLogger("test")),
121+
)
122+
assert.NoError(t, err)
123+
124+
i, err := factory.NewInterceptor("")
125+
126+
rtpReadFn := NewMockRTPReader(func(data []byte, attrs interceptor.Attributes) (int, interceptor.Attributes, error) {
127+
copy(data, readBuf)
128+
return 7, attrs, nil
129+
})
130+
reader := i.BindRemoteStream(&interceptor.StreamInfo{
131+
SSRC: 123456,
132+
ClockRate: 90000,
133+
}, rtpReadFn)
134+
135+
bufLen, _, err := reader.Read(readBuf, interceptor.Attributes{})
136+
assert.Contains(t, err.Error(), "7 < 12")
137+
assert.Equal(t, 0, bufLen)
138+
139+
err = i.Close()
140+
assert.NoError(t, err)
141+
142+
}

0 commit comments

Comments
 (0)