Experimental flag to try time stamp adjustment to control drift. (#1687)

* Experimental flag to try time stamp adjustment to control drift.

There is a config to enable this.

Using a PID controller to try and keep the sample rate at expected
value. Need to be seen if this works well. Adjustment are limited
to 25 ms max at a time to ensure there are no large jumps.
And it is applied when doing RTCP sender report which happens
once in 5 seconds currently for both audio and video tracks.

A nice introduction to PID controllers - https://alphaville.github.io/qub/pid-101/#/
Implementation borrowed from - https://github.com/pms67/PID

A few things TODO
1. PID controller tuning is a process. Have picked values from test from
   that implementation above. May not be the best. Need to try.
2. Can potentially run this more often. Rather than running it only when
   running RTCP sender report (which is once in 5 seconds now), can
   potentially run it every second and limit the amount of change to
   something like 10 ms max.

* remove unused variable

* debug log a bit more
This commit is contained in:
Raja Subramanian
2023-05-06 11:52:57 +05:30
committed by GitHub
parent ae5d2c3261
commit 3fb93135f5
11 changed files with 298 additions and 49 deletions
+1
View File
@@ -104,6 +104,7 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, wr *
sub.GetBufferFactory(),
subscriberID,
t.params.ReceiverConfig.PacketBufferSize,
sub.GetAllowTimestampAdjustment(),
LoggerWithTrack(sub.GetLogger(), trackID, t.params.IsRelayed),
)
if err != nil {
+5
View File
@@ -92,6 +92,7 @@ type ParticipantParams struct {
SubscriberAllowPause bool
SubscriptionLimitAudio int32
SubscriptionLimitVideo int32
AllowTimestampAdjustment bool
}
type ParticipantImpl struct {
@@ -228,6 +229,10 @@ func (p *ParticipantImpl) GetAdaptiveStream() bool {
return p.params.AdaptiveStream
}
func (p *ParticipantImpl) GetAllowTimestampAdjustment() bool {
return p.params.AllowTimestampAdjustment
}
func (p *ParticipantImpl) ID() livekit.ParticipantID {
return p.params.SID
}
+2
View File
@@ -341,6 +341,8 @@ type LocalParticipant interface {
// down stream bandwidth management
SetSubscriberAllowPause(allowPause bool)
SetSubscriberChannelCapacity(channelCapacity int64)
GetAllowTimestampAdjustment() bool
}
// Room is a container of participants, and can provide room-level actions
@@ -165,6 +165,16 @@ type FakeLocalParticipant struct {
getAdaptiveStreamReturnsOnCall map[int]struct {
result1 bool
}
GetAllowTimestampAdjustmentStub func() bool
getAllowTimestampAdjustmentMutex sync.RWMutex
getAllowTimestampAdjustmentArgsForCall []struct {
}
getAllowTimestampAdjustmentReturns struct {
result1 bool
}
getAllowTimestampAdjustmentReturnsOnCall map[int]struct {
result1 bool
}
GetAudioLevelStub func() (float64, bool)
getAudioLevelMutex sync.RWMutex
getAudioLevelArgsForCall []struct {
@@ -1612,6 +1622,59 @@ func (fake *FakeLocalParticipant) GetAdaptiveStreamReturnsOnCall(i int, result1
}{result1}
}
func (fake *FakeLocalParticipant) GetAllowTimestampAdjustment() bool {
fake.getAllowTimestampAdjustmentMutex.Lock()
ret, specificReturn := fake.getAllowTimestampAdjustmentReturnsOnCall[len(fake.getAllowTimestampAdjustmentArgsForCall)]
fake.getAllowTimestampAdjustmentArgsForCall = append(fake.getAllowTimestampAdjustmentArgsForCall, struct {
}{})
stub := fake.GetAllowTimestampAdjustmentStub
fakeReturns := fake.getAllowTimestampAdjustmentReturns
fake.recordInvocation("GetAllowTimestampAdjustment", []interface{}{})
fake.getAllowTimestampAdjustmentMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeLocalParticipant) GetAllowTimestampAdjustmentCallCount() int {
fake.getAllowTimestampAdjustmentMutex.RLock()
defer fake.getAllowTimestampAdjustmentMutex.RUnlock()
return len(fake.getAllowTimestampAdjustmentArgsForCall)
}
func (fake *FakeLocalParticipant) GetAllowTimestampAdjustmentCalls(stub func() bool) {
fake.getAllowTimestampAdjustmentMutex.Lock()
defer fake.getAllowTimestampAdjustmentMutex.Unlock()
fake.GetAllowTimestampAdjustmentStub = stub
}
func (fake *FakeLocalParticipant) GetAllowTimestampAdjustmentReturns(result1 bool) {
fake.getAllowTimestampAdjustmentMutex.Lock()
defer fake.getAllowTimestampAdjustmentMutex.Unlock()
fake.GetAllowTimestampAdjustmentStub = nil
fake.getAllowTimestampAdjustmentReturns = struct {
result1 bool
}{result1}
}
func (fake *FakeLocalParticipant) GetAllowTimestampAdjustmentReturnsOnCall(i int, result1 bool) {
fake.getAllowTimestampAdjustmentMutex.Lock()
defer fake.getAllowTimestampAdjustmentMutex.Unlock()
fake.GetAllowTimestampAdjustmentStub = nil
if fake.getAllowTimestampAdjustmentReturnsOnCall == nil {
fake.getAllowTimestampAdjustmentReturnsOnCall = make(map[int]struct {
result1 bool
})
}
fake.getAllowTimestampAdjustmentReturnsOnCall[i] = struct {
result1 bool
}{result1}
}
func (fake *FakeLocalParticipant) GetAudioLevel() (float64, bool) {
fake.getAudioLevelMutex.Lock()
ret, specificReturn := fake.getAudioLevelReturnsOnCall[len(fake.getAudioLevelArgsForCall)]
@@ -5456,6 +5519,8 @@ func (fake *FakeLocalParticipant) Invocations() map[string][][]interface{} {
defer fake.debugInfoMutex.RUnlock()
fake.getAdaptiveStreamMutex.RLock()
defer fake.getAdaptiveStreamMutex.RUnlock()
fake.getAllowTimestampAdjustmentMutex.RLock()
defer fake.getAllowTimestampAdjustmentMutex.RUnlock()
fake.getAudioLevelMutex.RLock()
defer fake.getAudioLevelMutex.RUnlock()
fake.getBufferFactoryMutex.RLock()