fix config ack, add randomness for keyframes and interframes

This commit is contained in:
kulikov0 committed 2026-07-26 20:30:48 +02:00
1 parent c36c561e35
commit 2bad03c542
17 files changed
+312 -109

No files matched your search

+93 -27
View File
@@ -15,7 +15,14 @@ const (
defaultVP8FPS = 24
defaultVP8Batch = 30
keepaliveIdlePeriod = 100 * time.Millisecond
keepaliveIdleMin = 60 * time.Millisecond
keepaliveIdleMax = 200 * time.Millisecond
keepalivePadMax = 176
sendQueueDepth = 128
paceBatchFloorPercent = 80
paceDriftMin = 5 * time.Second
paceDriftMax = 20 * time.Second
)
type VP8DataTunnel struct {
@@ -29,9 +36,12 @@ type VP8DataTunnel struct {
stopOnce sync.Once
running atomic.Bool
cfgMu sync.Mutex
fps int
batch int
cfgMu sync.Mutex
fps int
batch int
keepaliveMin time.Duration
keepaliveMax time.Duration
keepalivePadMax int
sentFrames atomic.Uint64
recvFrames atomic.Uint64
@@ -55,17 +65,47 @@ func NewVP8DataTunnelWithQueue(track *webrtc.TrackLocalStaticSample, obf *Tunnel
queueDepth = sendQueueDepth
}
return &VP8DataTunnel{
track: track,
obf: obf,
logFn: logFn,
stopCh: make(chan struct{}),
sendQueue: make(chan []byte, queueDepth),
cfgChan: make(chan struct{}, 1),
fps: defaultVP8FPS,
batch: defaultVP8Batch,
track: track,
obf: obf,
logFn: logFn,
stopCh: make(chan struct{}),
sendQueue: make(chan []byte, queueDepth),
cfgChan: make(chan struct{}, 1),
fps: defaultVP8FPS,
batch: defaultVP8Batch,
keepaliveMin: keepaliveIdleMin,
keepaliveMax: keepaliveIdleMax,
keepalivePadMax: keepalivePadMax,
}
}
func (t *VP8DataTunnel) SetKeepaliveShape(minPeriod, maxPeriod time.Duration, padMax int) {
t.cfgMu.Lock()
if minPeriod > 0 {
t.keepaliveMin = minPeriod
}
if maxPeriod >= t.keepaliveMin {
t.keepaliveMax = maxPeriod
}
if padMax >= 0 {
t.keepalivePadMax = padMax
}
newMin, newMax, newPad := t.keepaliveMin, t.keepaliveMax, t.keepalivePadMax
t.cfgMu.Unlock()
t.logFn("vp8tunnel: keepalive shape min=%s max=%s padMax=%d", newMin, newMax, newPad)
}
func (t *VP8DataTunnel) nextKeepalive(sampleInterval time.Duration) (ticks, padLen int) {
t.cfgMu.Lock()
minPeriod, maxPeriod, padMax := t.keepaliveMin, t.keepaliveMax, t.keepalivePadMax
t.cfgMu.Unlock()
ticks = int(common.DurationInRange(minPeriod, maxPeriod) / sampleInterval)
if ticks < 1 {
ticks = 1
}
return ticks, common.IntInRange(0, padMax)
}
func (t *VP8DataTunnel) Reconfigure(fps, batch int) {
if fps <= 0 && batch <= 0 {
return
@@ -153,35 +193,49 @@ func (t *VP8DataTunnel) Stop() {
}
}
func (t *VP8DataTunnel) currentIntervals() (sampleInterval time.Duration, keepaliveEvery, fps, batch int) {
func (t *VP8DataTunnel) currentRate() (fps, batch int) {
t.cfgMu.Lock()
fps = t.fps
batch = t.batch
t.cfgMu.Unlock()
defer t.cfgMu.Unlock()
return t.fps, t.batch
}
func sampleIntervalFor(fps, batch int) time.Duration {
if fps < 1 {
fps = 1
}
frameInterval := time.Second / time.Duration(fps)
sampleInterval = frameInterval
interval := frameInterval
if batch > 1 {
sampleInterval = frameInterval / time.Duration(batch)
interval = frameInterval / time.Duration(batch)
}
if sampleInterval <= 0 {
sampleInterval = time.Millisecond
if interval <= 0 {
interval = time.Millisecond
}
return interval
}
keepaliveEvery = int(keepaliveIdlePeriod / sampleInterval)
if keepaliveEvery < 1 {
keepaliveEvery = 1
func pacedBatchFor(batch int) int {
if batch <= 1 {
return batch
}
return
floor := batch * paceBatchFloorPercent / 100
if floor < 1 {
floor = 1
}
return common.IntInRange(floor, batch)
}
func (t *VP8DataTunnel) writerLoop() {
for {
sampleInterval, keepaliveEvery, fps, batch := t.currentIntervals()
t.logFn("vp8tunnel: writer (re)started fps=%d batch=%d sampleInterval=%s keepaliveEvery=%d",
fps, batch, sampleInterval, keepaliveEvery)
fps, batch := t.currentRate()
pacedBatch := pacedBatchFor(batch)
sampleInterval := sampleIntervalFor(fps, pacedBatch)
keepaliveEvery, keepalivePad := t.nextKeepalive(sampleInterval)
t.logFn("vp8tunnel: writer (re)started fps=%d batch=%d pacedBatch=%d sampleInterval=%s keepaliveEvery=%d",
fps, batch, pacedBatch, sampleInterval, keepaliveEvery)
ticker := time.NewTicker(sampleInterval)
drift := time.NewTimer(common.DurationInRange(paceDriftMin, paceDriftMax))
idleTicks := 0
reconfigure := false
@@ -189,9 +243,19 @@ func (t *VP8DataTunnel) writerLoop() {
select {
case <-t.stopCh:
ticker.Stop()
drift.Stop()
return
case <-t.cfgChan:
reconfigure = true
case <-drift.C:
pacedBatch = pacedBatchFor(batch)
sampleInterval = sampleIntervalFor(fps, pacedBatch)
ticker.Reset(sampleInterval)
keepaliveEvery, keepalivePad = t.nextKeepalive(sampleInterval)
drift.Reset(common.DurationInRange(paceDriftMin, paceDriftMax))
if common.Debug {
t.logFn("vp8tunnel: pace drift pacedBatch=%d/%d sampleInterval=%s", pacedBatch, batch, sampleInterval)
}
case <-ticker.C:
var sample []byte
isKeepalive := false
@@ -205,7 +269,8 @@ func (t *VP8DataTunnel) writerLoop() {
continue
}
idleTicks = 0
sample = t.obf.EncodeKeepalive()
sample = t.obf.EncodeKeepalive(keepalivePad)
keepaliveEvery, keepalivePad = t.nextKeepalive(sampleInterval)
isKeepalive = true
}
if sample == nil {
@@ -228,6 +293,7 @@ func (t *VP8DataTunnel) writerLoop() {
}
}
ticker.Stop()
drift.Stop()
}
}