Skip to content

Commit

Permalink
Fix nil rtcpInterceptor on track.Bind
Browse files Browse the repository at this point in the history
Create rtcpInterceptor before bind called.
  • Loading branch information
cnderrauber committed Nov 28, 2024
1 parent f8294e0 commit 2553783
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
13 changes: 6 additions & 7 deletions rtpsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ func (r *RTPSender) Send(parameters RTPSendParameters) error {
trackEncoding.ssrc = parameters.Encodings[idx].SSRC
trackEncoding.ssrcRTX = parameters.Encodings[idx].RTX.SSRC
trackEncoding.ssrcFEC = parameters.Encodings[idx].FEC.SSRC
trackEncoding.rtcpInterceptor = r.api.interceptor.BindRTCPReader(
interceptor.RTCPReaderFunc(func(in []byte, a interceptor.Attributes) (n int, attributes interceptor.Attributes, err error) {
n, err = trackEncoding.srtpStream.Read(in)
return n, a, err
}),
)
trackEncoding.context = &baseTrackLocalContext{
id: r.id,
params: rtpParameters,
Expand Down Expand Up @@ -339,13 +345,6 @@ func (r *RTPSender) Send(parameters RTPSendParameters) error {
parameters.HeaderExtensions,
)

trackEncoding.rtcpInterceptor = r.api.interceptor.BindRTCPReader(
interceptor.RTCPReaderFunc(func(in []byte, a interceptor.Attributes) (n int, attributes interceptor.Attributes, err error) {
n, err = trackEncoding.srtpStream.Read(in)
return n, a, err
}),
)

rtpInterceptor := r.api.interceptor.BindLocalStream(
&trackEncoding.streamInfo,
interceptor.RTPWriterFunc(func(header *rtp.Header, payload []byte, _ interceptor.Attributes) (int, error) {
Expand Down
37 changes: 37 additions & 0 deletions rtpsender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,40 @@ func Test_RTPSender_RTX_Support(t *testing.T) {
assert.NoError(t, peerConnection.Close())
})
}

func Test_RTPSender_RTCPReader_Bind_Not_Nil(t *testing.T) {
track, err := NewTrackLocalStaticSample(RTPCodecCapability{MimeType: MimeTypeVP8}, "video", "pion")
assert.NoError(t, err)

peerConnection, err := NewPeerConnection(Configuration{})
assert.NoError(t, err)

bindCalled := make(chan struct{})
rtpSender, err := peerConnection.AddTrack(&TrackLocalCheckRTCPReaderOnBind{
t: t,
TrackLocalStaticSample: track,
bindCalled: bindCalled,
})
assert.NoError(t, err)

parameter := rtpSender.GetParameters()
err = rtpSender.Send(parameter)
<-rtpSender.sendCalled
<-bindCalled
assert.NoError(t, err)

assert.NoError(t, peerConnection.Close())
}

type TrackLocalCheckRTCPReaderOnBind struct {
*TrackLocalStaticSample
t *testing.T
bindCalled chan struct{}
}

func (s *TrackLocalCheckRTCPReaderOnBind) Bind(ctx TrackLocalContext) (RTPCodecParameters, error) {
assert.NotNil(s.t, ctx.RTCPReader())
p, err := s.TrackLocalStaticSample.Bind(ctx)
close(s.bindCalled)
return p, err
}

0 comments on commit 2553783

Please sign in to comment.