Skip to content

Commit d6154f6

Browse files
committed
Add WithRTPTimestamp to set initial RTP timestamp
Move to the new RTP API newPacketizerWithOptions, add WithRTPTimestamp To make it possible to set the initial RTP timestamp for the track.
1 parent ac1a132 commit d6154f6

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

track_local_static.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type TrackLocalStaticRTP struct {
3333
codec RTPCodecCapability
3434
payloader func(RTPCodecCapability) (rtp.Payloader, error)
3535
id, rid, streamID string
36+
rtpTimestamp *uint32
3637
}
3738

3839
// NewTrackLocalStaticRTP returns a TrackLocalStaticRTP.
@@ -69,6 +70,13 @@ func WithPayloader(h func(RTPCodecCapability) (rtp.Payloader, error)) func(*Trac
6970
}
7071
}
7172

73+
// WithRTPTimestamp set the initial RTP timestamp for the track.
74+
func WithRTPTimestamp(timestamp uint32) func(*TrackLocalStaticRTP) {
75+
return func(s *TrackLocalStaticRTP) {
76+
s.rtpTimestamp = &timestamp
77+
}
78+
}
79+
7280
// Bind is called by the PeerConnection after negotiation is complete
7381
// This asserts that the code requested is supported by the remote peer.
7482
// If so it sets up all the state (SSRC and PayloadType) to have a call.
@@ -282,14 +290,21 @@ func (s *TrackLocalStaticSample) Bind(t TrackLocalContext) (RTPCodecParameters,
282290
}
283291

284292
s.sequencer = rtp.NewRandomSequencer()
285-
s.packetizer = rtp.NewPacketizer(
293+
294+
options := []rtp.PacketizerOption{}
295+
296+
if s.rtpTrack.rtpTimestamp != nil {
297+
options = append(options, rtp.WithTimestamp(*s.rtpTrack.rtpTimestamp))
298+
}
299+
300+
s.packetizer = rtp.NewPacketizerWithOptions(
286301
outboundMTU,
287-
0, // Value is handled when writing
288-
0, // Value is handled when writing
289302
payloader,
290303
s.sequencer,
291304
codec.ClockRate,
305+
options...,
292306
)
307+
293308
s.clockRate = float64(codec.RTPCodecCapability.ClockRate)
294309

295310
return codec, nil

track_local_static_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,44 @@ func Test_TrackLocalStatic_Payloader(t *testing.T) {
425425

426426
closePairNow(t, offerer, answerer)
427427
}
428+
429+
func Test_TrackLocalStatic_Timestamp(t *testing.T) {
430+
lim := test.TimeOut(time.Second * 30)
431+
defer lim.Stop()
432+
433+
report := test.CheckRoutines(t)
434+
defer report()
435+
436+
initialTimestamp := uint32(12345)
437+
track, err := NewTrackLocalStaticSample(
438+
RTPCodecCapability{MimeType: MimeTypeVP8},
439+
"video",
440+
"pion",
441+
WithRTPTimestamp(initialTimestamp),
442+
)
443+
assert.NoError(t, err)
444+
445+
pcOffer, pcAnswer, err := newPair()
446+
assert.NoError(t, err)
447+
448+
_, err = pcOffer.AddTrack(track)
449+
assert.NoError(t, err)
450+
451+
onTrackFired, onTrackFiredFunc := context.WithCancel(context.Background())
452+
pcAnswer.OnTrack(func(trackRemote *TrackRemote, _ *RTPReceiver) {
453+
pkt, _, err := trackRemote.ReadRTP()
454+
assert.NoError(t, err)
455+
assert.GreaterOrEqual(t, pkt.Timestamp, initialTimestamp)
456+
// Allow for ~10 dropped packets
457+
assert.LessOrEqual(t, pkt.Timestamp, initialTimestamp+30000)
458+
459+
onTrackFiredFunc()
460+
})
461+
462+
assert.NoError(t, signalPair(pcOffer, pcAnswer))
463+
464+
sendVideoUntilDone(t, onTrackFired.Done(), []*TrackLocalStaticSample{track})
465+
466+
<-onTrackFired.Done()
467+
closePairNow(t, pcOffer, pcAnswer)
468+
}

0 commit comments

Comments
 (0)