fix(video): recover encoder capacity, pace RTP and isolate preview sessions

This commit is contained in:
DCCONSTRUCTIONS
2026-11-21 20:58:25 +03:00
parent 3d04fa45d3
commit bed1db3508
29 changed files with 1534 additions and 141 deletions
@@ -0,0 +1,80 @@
package node
import (
"testing"
"time"
)
// After a real blackout, repeated isolated 130 ms echoes on a 50 ms
// baseline used to restart the capacity recovery timer forever. Exercise
// the shared estimator AND feedback gate; testing either alone misses it.
func TestVideoBudgetRecoversAfterBlackoutWithIsolatedEchoJitter(t *testing.T) {
for _, control := range []bool{false, true} {
b, now := testBudget(t)
b.Configure(LinkPolicy{"missioncore.link-budget/v1", 6000, 512})
b.SetControl(control)
b.EnableDatagramFeedback()
b.ObserveDatagramFeedback(50*time.Millisecond, 0, 0)
// A long interruption must close admission and lower the estimate.
b.SetControl(true)
b.ObserveCommandFrame(50*time.Millisecond, 20*time.Millisecond)
for i := 0; i < 100; i++ {
*now = now.Add(100 * time.Millisecond)
b.SetControl(true)
b.ObserveCommandSilence()
if b.optional(*now) != 0 && i >= 2 {
t.Fatal("blackout admitted payload")
}
}
if b.effective != 640 {
t.Fatal("blackout failed to reduce budget", b.effective)
}
for i := 0; i < 600; i++ {
*now = now.Add(100 * time.Millisecond)
b.SetControl(control)
b.ObserveCommandFrame(50*time.Millisecond, 30*time.Millisecond)
b.allocateRole(testClockID, "video", true, 5000000, true)
rtt := 50 * time.Millisecond
if i%5 == 0 {
rtt = 130 * time.Millisecond
}
b.ObserveDatagramFeedback(rtt, 0, b.DatagramFeedbackMark())
// Current useful camera packets only, under existing admission.
for packet := 0; packet < 45; packet++ {
*now = now.Add(2 * time.Millisecond)
b.Try("video", 1100)
}
}
allocation, _ := b.allocateRole(testClockID, "video", true, 5000000, true)
t.Logf("control=%v recovered capacity=%.0f allocation=%v", control, b.effective, allocation["allocation_bps"])
if b.effective < 4500 || b.effective > 6000 || allocation["allocation_bps"].(int) < 3000000 {
t.Fatal("isolated echo jitter stranded the recovered video budget", b.Snapshot())
}
// Return-path failure still blocks payload after recovery, immediately.
*now = now.Add(400 * time.Millisecond)
if b.optional(*now) != 0 {
t.Fatal("recovered budget bypassed feedback silence")
}
}
}
func TestSustainedModerateEchoDelayStillPreventsCapacityRecovery(t *testing.T) {
b, now := testBudget(t)
b.SetControl(false)
b.EnableDatagramFeedback()
b.ObserveDatagramFeedback(50*time.Millisecond, 0, 0)
b.effective = 640
for i := 0; i < 150; i++ {
*now = now.Add(100 * time.Millisecond)
b.SetControl(false)
b.allocateRole(testClockID, "video", true, 5000000, true)
b.ObserveDatagramFeedback(130*time.Millisecond, 0, b.DatagramFeedbackMark())
b.Try("video", 1100)
}
if b.effective != 640 || !b.healthy.IsZero() {
t.Fatal("continuing delay increased the budget", b.Snapshot())
}
if b.runtime.feedback.window > feedbackMinWindow {
t.Fatal("continuing delay failed to constrain outstanding bytes")
}
}
@@ -89,18 +89,30 @@ func (b *LinkBudget) feedbackLimited(now time.Time) bool {
if !h.enabled {
return false
}
// Previously earned capacity is usable only within the existing recovery
// lifetime. Check even after echoes resume: an almost five-second outage
// must not carry old capacity past the healthy-feedback settling period.
if h.limited && !h.recoverUntil.IsZero() && !now.Before(h.recoverUntil) {
h.window = min(h.window, uint64(linkClassBurst))
h.recoverWindow = 0
}
if now.Sub(h.seen) > max(250*time.Millisecond, b.baseline+150*time.Millisecond) {
if !h.limited {
h.recoverWindow = h.window
h.recoverUntil = h.seen.Add(5 * time.Second)
}
h.recordPressure(now, "feedback_silence", now.Sub(h.seen), h.rtt, max(250*time.Millisecond, b.baseline+150*time.Millisecond))
// A silent return path invalidates the previously grown send window.
// Latch pressure before updating seen: one fresh ACK after a blackout
// must not release old optional credit as a burst. Caller holds b.mu.
// Pause admission immediately, but distinguish a brief feedback gap
// from a new path. Forgetting capacity on every pause creates a circular
// recovery dependency with a receiver-limited camera. Keep only the
// recently earned window; prolonged silence expires it. Tokens never
// survive, and the unchanged healthy-feedback gate must still reopen.
h.limited = true
h.healthy = time.Time{}
h.window = min(h.window, uint64(linkClassBurst))
if !now.Before(h.recoverUntil) {
h.window = min(h.window, uint64(linkClassBurst))
h.recoverWindow = 0
}
h.adjusted = now
b.healthy = time.Time{}
b.tokens = 0
@@ -154,11 +166,14 @@ func (b *LinkBudget) ObserveDatagramFeedback(elapsed time.Duration, coreMS float
h.limited = true
h.healthy = time.Time{}
}
b.healthy = time.Time{}
// Delay-based admission must not mistake isolated RTT jitter for a
// growing queue. Require a sustained trend before reducing its window;
// severe delay and silence still suspend optional traffic immediately.
if h.limited || now.Sub(h.delayed) >= 200*time.Millisecond {
// The same sustained-pressure evidence must invalidate capacity
// recovery. Resetting it for every isolated delayed echo strands
// a recovered path at its blackout floor indefinitely.
b.healthy = time.Time{}
if h.adjusted.IsZero() || now.Sub(h.adjusted) >= 200*time.Millisecond {
h.window = max(feedbackMinWindow, h.window/2)
h.adjusted = now
@@ -142,8 +142,8 @@ func TestDatagramFeedbackSilenceLatchesBeforeFreshEcho(t *testing.T) {
b.Snapshot()
}
b.ObserveDatagramFeedback(100*time.Millisecond, 0, 0)
if !b.runtime.feedback.limited || b.runtime.feedback.window > linkClassBurst {
t.Fatal("fresh echo restored the old send window", b.runtime.feedback)
if !b.runtime.feedback.limited || b.optional(*now) != 0 {
t.Fatal("fresh echo restored old admission", b.runtime.feedback)
}
if b.tokens != 0 || b.classes["video"].tokens != 0 {
t.Fatal("old burst credit survived silence")
@@ -0,0 +1,67 @@
package node
import (
"testing"
"time"
)
// A receiver-limited camera cannot earn a collapsed admission window back by
// sending more bytes: the receiver is also waiting for its rate to recover.
// Brief silence must pause packets without forgetting recently earned capacity.
func TestFeedbackPauseDoesNotStrandReceiverLimitedCamera(t *testing.T) {
for _, gap := range []time.Duration{254 * time.Millisecond, 285 * time.Millisecond, 434 * time.Millisecond, time.Second} {
t.Run(gap.String(), func(t *testing.T) {
b, now := testBudget(t)
b.effective = 35000
b.SetControl(false)
b.EnableDatagramFeedback()
b.ObserveDatagramFeedback(15*time.Millisecond, 0, 0)
b.runtime.feedback.window = 64000 // Previously acknowledged path.
for cycle := 0; cycle < 6; cycle++ {
*now = now.Add(gap)
if b.optional(*now) != 0 {
t.Fatal("silent feedback admitted media")
}
b.ObserveDatagramFeedback(300*time.Millisecond, 0, b.DatagramFeedbackMark())
if ok, _ := b.Try("video", 1000); ok {
t.Fatal("delayed first echo resumed media")
}
for tick := 0; tick < 4; tick++ {
*now = now.Add(100 * time.Millisecond)
b.SetControl(false)
b.ObserveDatagramFeedback(15*time.Millisecond, 0, b.DatagramFeedbackMark())
if tick < 2 && b.optional(*now) != 0 {
t.Fatal("recovery bypassed continuous healthy feedback")
}
}
if b.optional(*now) < 5000 {
t.Fatal("brief pause stranded useful video despite healthy feedback", b.optional(*now))
}
if b.tokens != 0 || b.classes["video"].tokens != 0 {
t.Fatal("remembered capacity restored old burst tokens")
}
}
})
}
}
func TestFeedbackPauseCapacityExpiresBeforeRecovery(t *testing.T) {
for _, gap := range []time.Duration{4900 * time.Millisecond, 5100 * time.Millisecond, 10 * time.Second} {
t.Run(gap.String(), func(t *testing.T) {
b, now := testBudget(t)
b.SetControl(false)
b.EnableDatagramFeedback()
b.ObserveDatagramFeedback(15*time.Millisecond, 0, 0)
b.runtime.feedback.window = 64000
*now = now.Add(gap)
b.ObserveDatagramFeedback(15*time.Millisecond, 0, 0)
for tick := 0; tick < 3; tick++ {
*now = now.Add(100 * time.Millisecond)
b.ObserveDatagramFeedback(15*time.Millisecond, 0, 0)
}
if b.runtime.feedback.window > linkClassBurst {
t.Fatal("expired capacity survived a long outage", b.runtime.feedback.window)
}
})
}
}
@@ -204,7 +204,7 @@ func roverReceiveDatagrams(ctx context.Context, conn *net.UDPConn, tx, rx *rover
return errors.New("invalid datagram intent")
}
state.receivedFrame(frame.Reply, item.at)
state.recordDev("datagram_received", map[string]any{"channel": channel, "packet_sequence": item.counter, "bytes": len(item.raw) + 44})
state.recordDev("datagram_received", map[string]any{"channel": channel, "packet_sequence": item.counter, "bytes": len(item.raw) + 44, "socket_read_mono_ms": float64(item.at.Sub(state.origin).Microseconds()) / 1000, "receiver_queue_ms": float64(time.Since(item.at).Microseconds()) / 1000, "receive_gap_ms": float64(item.at.Sub(lastFrame).Microseconds()) / 1000})
if !frame.Reply.Watch {
return nil
}