Skip to content

Commit e60bf03

Browse files
Alex PokotiloSean-Der
authored andcommitted
Add padding support to TrackLocalStaticSample
To add padding-only samples call GeneratePadding
1 parent b2af663 commit e60bf03

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

track_local_static.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,28 @@ func (s *TrackLocalStaticSample) WriteSample(sample media.Sample) error {
304304

305305
return util.FlattenErrs(writeErrs)
306306
}
307+
308+
// GeneratePadding writes padding-only samples to the TrackLocalStaticSample
309+
// If one PeerConnection fails the packets will still be sent to
310+
// all PeerConnections. The error message will contain the ID of the failed
311+
// PeerConnections so you can remove them
312+
func (s *TrackLocalStaticSample) GeneratePadding(samples uint32) error {
313+
s.rtpTrack.mu.RLock()
314+
p := s.packetizer
315+
s.rtpTrack.mu.RUnlock()
316+
317+
if p == nil {
318+
return nil
319+
}
320+
321+
packets := p.GeneratePadding(samples)
322+
323+
writeErrs := []error{}
324+
for _, p := range packets {
325+
if err := s.rtpTrack.WriteRTP(p); err != nil {
326+
writeErrs = append(writeErrs, err)
327+
}
328+
}
329+
330+
return util.FlattenErrs(writeErrs)
331+
}

track_local_static_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,64 @@ func BenchmarkTrackLocalWrite(b *testing.B) {
252252
assert.NoError(b, err)
253253
}
254254
}
255+
256+
func Test_TrackLocalStatic_Padding(t *testing.T) {
257+
mediaEngineOne := &MediaEngine{}
258+
assert.NoError(t, mediaEngineOne.RegisterCodec(RTPCodecParameters{
259+
RTPCodecCapability: RTPCodecCapability{MimeType: "video/VP8", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
260+
PayloadType: 100,
261+
}, RTPCodecTypeVideo))
262+
263+
mediaEngineTwo := &MediaEngine{}
264+
assert.NoError(t, mediaEngineTwo.RegisterCodec(RTPCodecParameters{
265+
RTPCodecCapability: RTPCodecCapability{MimeType: "video/VP8", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
266+
PayloadType: 200,
267+
}, RTPCodecTypeVideo))
268+
269+
offerer, err := NewAPI(WithMediaEngine(mediaEngineOne)).NewPeerConnection(Configuration{})
270+
assert.NoError(t, err)
271+
272+
answerer, err := NewAPI(WithMediaEngine(mediaEngineTwo)).NewPeerConnection(Configuration{})
273+
assert.NoError(t, err)
274+
275+
track, err := NewTrackLocalStaticSample(RTPCodecCapability{MimeType: MimeTypeVP8}, "video", "pion")
276+
assert.NoError(t, err)
277+
278+
_, err = offerer.AddTransceiverFromKind(RTPCodecTypeVideo)
279+
assert.NoError(t, err)
280+
281+
_, err = answerer.AddTrack(track)
282+
assert.NoError(t, err)
283+
284+
onTrackFired, onTrackFiredFunc := context.WithCancel(context.Background())
285+
286+
offerer.OnTrack(func(track *TrackRemote, _ *RTPReceiver) {
287+
assert.Equal(t, track.PayloadType(), PayloadType(100))
288+
assert.Equal(t, track.Codec().RTPCodecCapability.MimeType, "video/VP8")
289+
290+
for i := 0; i < 20; i++ {
291+
// Padding payload
292+
p, _, e := track.ReadRTP()
293+
assert.NoError(t, e)
294+
assert.True(t, p.Padding)
295+
assert.Equal(t, p.PaddingSize, byte(255))
296+
}
297+
298+
onTrackFiredFunc()
299+
})
300+
301+
assert.NoError(t, signalPair(offerer, answerer))
302+
303+
exit := false
304+
305+
for !exit {
306+
select {
307+
case <-time.After(1 * time.Millisecond):
308+
assert.NoError(t, track.GeneratePadding(1))
309+
case <-onTrackFired.Done():
310+
exit = true
311+
}
312+
}
313+
314+
closePairNow(t, offerer, answerer)
315+
}

0 commit comments

Comments
 (0)