Skip to content

Commit

Permalink
Add SetFireOnTrackBeforeFirstRTP
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniels committed Oct 15, 2024
1 parent 8fbf821 commit 76634b1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions peerconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,10 @@ func (pc *PeerConnection) startReceiver(incoming trackDetails, receiver *RTPRece
return
}

if pc.api.settingEngine.fireOnTrackBeforeFirstRTP {
pc.onTrack(t, receiver)
return
}
go func(track *TrackRemote) {
b := make([]byte, pc.api.settingEngine.getReceiveMTU())
n, _, err := track.peek(b)
Expand Down
10 changes: 10 additions & 0 deletions settingengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type SettingEngine struct {
srtpProtectionProfiles []dtls.SRTPProtectionProfile
receiveMTU uint
iceMaxBindingRequests *uint16
fireOnTrackBeforeFirstRTP bool
}

// getReceiveMTU returns the configured MTU. If SettingEngine's MTU is configured to 0 it returns the default
Expand Down Expand Up @@ -491,3 +492,12 @@ func (e *SettingEngine) SetSCTPRTOMax(rtoMax time.Duration) {
func (e *SettingEngine) SetICEBindingRequestHandler(bindingRequestHandler func(m *stun.Message, local, remote ice.Candidate, pair *ice.CandidatePair) bool) {
e.iceBindingRequestHandler = bindingRequestHandler
}

// SetFireOnTrackBeforeFirstRTP sets if firing the OnTrack event should happen
// before any RTP packets are received. Setting this to true will
// have the Track's Codec and PayloadTypes be initially set to their
// zero values in the OnTrack handler.
// Note: This does not yet affect simulcast tracks.
func (e *SettingEngine) SetFireOnTrackBeforeFirstRTP(fireOnTrackBeforeFirstRTP bool) {
e.fireOnTrackBeforeFirstRTP = fireOnTrackBeforeFirstRTP
}
54 changes: 54 additions & 0 deletions settingengine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,57 @@ func TestSetHooks(t *testing.T) {
t.Errorf("Failed to set DTLS Certificate Request Hook")
}
}

func TestSetFireOnTrackBeforeFirstRTP(t *testing.T) {
lim := test.TimeOut(time.Second * 30)
defer lim.Stop()

report := test.CheckRoutines(t)
defer report()

s := SettingEngine{}
s.SetFireOnTrackBeforeFirstRTP(true)

mediaEngineOne := &MediaEngine{}
assert.NoError(t, mediaEngineOne.RegisterCodec(RTPCodecParameters{
RTPCodecCapability: RTPCodecCapability{MimeType: "video/VP8", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
PayloadType: 100,
}, RTPCodecTypeVideo))

mediaEngineTwo := &MediaEngine{}
assert.NoError(t, mediaEngineTwo.RegisterCodec(RTPCodecParameters{
RTPCodecCapability: RTPCodecCapability{MimeType: "video/VP8", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
PayloadType: 200,
}, RTPCodecTypeVideo))

offerer, err := NewAPI(WithMediaEngine(mediaEngineOne), WithSettingEngine(s)).NewPeerConnection(Configuration{})
assert.NoError(t, err)

answerer, err := NewAPI(WithMediaEngine(mediaEngineTwo)).NewPeerConnection(Configuration{})
assert.NoError(t, err)

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

_, err = offerer.AddTransceiverFromKind(RTPCodecTypeVideo)
assert.NoError(t, err)

_, err = answerer.AddTrack(track)
assert.NoError(t, err)

onTrackFired, onTrackFiredFunc := context.WithCancel(context.Background())
offerer.OnTrack(func(track *TrackRemote, _ *RTPReceiver) {
_, _, err = track.Read(make([]byte, 1500))
assert.NoError(t, err)
assert.Equal(t, track.PayloadType(), PayloadType(100))
assert.Equal(t, track.Codec().RTPCodecCapability.MimeType, "video/VP8")

onTrackFiredFunc()
})

assert.NoError(t, signalPair(offerer, answerer))

sendVideoUntilDone(onTrackFired.Done(), t, []*TrackLocalStaticSample{track})

closePairNow(t, offerer, answerer)
}

0 comments on commit 76634b1

Please sign in to comment.