Files
piik/internal/media/forwarding/publication_test.go
T
AAA 4bc0fd1e41 fix(app): stabilize sharing startup and align shared controls
## Problem and outcome / 问题与结果

Windows Auto selection could spend its remaining startup budget on a probe that could not finish, recreate hardware after a successful probe, and discard the prepared codec on first relay activation. Preserve the proven encoder through the existing capture owner and keep VP8 fallback unchanged. Correct custom-port STUN survey membership without changing the published Hosted base preference or introducing a second route/retry owner.

Reject queued minimize/resize frames that no longer match the current WGC target, replacing the one-shot restoration flag that could end an active H.264 share. Align shared controls, help and appearance across the product and website. macOS audio readiness follows successful platform startup rather than the arrival of non-silent samples. The optional editorial headline is shared with the website, with English-first bilingual release copy. Room voice and multiplayer input implementations remain outside this candidate.

Related reports: #437 and #486. Their affected devices still need retesting; this does not establish that all reported driver or macOS failures have been resolved.

## Release notes

**Play together. Your turn!**

**远程同乐,轮到你啦!**

### English

This maintenance update improves sharing startup and brings common controls and guidance into line. Existing 2.x App and Site compatibility is preserved.

- Windows Auto reuses the H.264 encoder that passed its frame-rate probe, preserves it on first relay activation, and skips probes that cannot fit the remaining startup budget. Discard stale minimize/resize frames before they can end an active share.
- Fix optional NAT survey selection with custom STUN ports while preserving existing Hosted selection. Include the upstream ICE candidate-priority correction.
- Make macOS audio readiness follow successful capture startup, so a silent source does not have to produce samples before becoming ready.
- Align shared buttons, selectors, keyboard labels and status colours, and refresh the README previews. Preserve explicit theme choices between the website and documentation.
- Find the Web version, troubleshooting and browser diagnostics in Help; closing a nested hint keeps the menu actions available.
- Put English first in update notes and simplify the website's release section around the same opening headline.

### 中文

这是一次分享启动与界面一致性的维护更新,保留现有 2.x App 和站点的兼容性。

- Windows 自动编码复用通过帧率探测的 H.264 编码器,在首次转发启用时保留该实例,并跳过剩余启动时间不足以完成的探测;丢弃最小化、恢复或缩放窗口时残留的旧尺寸帧,避免它们导致分享结束。
- 修复自定义 STUN 端口下的辅助打洞采样选择,保留已有自部署站点的选择行为,并同步上游 ICE 候选优先级修正。
- macOS 音频在平台确认启动成功后就绪,静默音源无需等待产生音频采样。
- 对齐共享按钮、选择控件、键盘说明与状态颜色,更新 README 界面预览;官网和文档中心之间保留用户明确选择的主题。
- 网页版本、问题排查和浏览器诊断统一收进「帮助」;内部提示关闭后,菜单操作仍然保留。
- 更新说明改为英文在前、中文在后;官网更新区以同一份开篇主题收敛呈现。

## Verification / 验证

- Local Web type-check, unit tests and production build: passed.
- Local Go tests, vet, Windows/Linux builds and Windows native compile/probe: passed.
- Website/documentation build, repository hygiene and diff checks: passed.
- Seven media-lifecycle cases passed: all Browser/App pairings, SFU-only, and Browser/Native fallback followed by direct recovery; restart, replacement and stale-signal retirement were included.
- The first candidate CI exposed an asynchronous allocation-test wait gap. Its correction waits for the committed deficit, retaining the capacity assertion; focused repetition passed locally.
- The native Host gate and benchmark now follow the current picture settings and stable codec name/value instead of removed UI structure or localized captions.
- Non-publishing CI passed on dd208e83, including Windows, both macOS architectures, both Linux architectures and Server packages. The later Windows frame-admission correction passed the focused native suite, physical Auto/H.264 and manual VP8 window gates, and the H.264 display gate. The window checks include minimized/resize/fullscreen recovery; the display check preserves the same media through profile and source changes.
- Final UI review covered narrow/desktop layouts, both themes, header/help, sharing controls, keyboard and interaction panels, packaged Local entry and website/docs preference handoff. The compact release entry passed 320/1280 px checks in both languages and themes, including metadata-request failure.
- Final candidate e88cb236: [all-platform packages and the full paired NAT/weak-network comparison](https://github.com/TNTcraftHIM/Piik/actions/runs/39284032227).

## Impact / 影响

No signaling, native-control, capture-envelope or storage version changes. Route authority, consent, capacity and retry lifetimes remain with their existing owners. The CI allocation correction changes the test only, not production media policy.

Physical macOS capture/permissions, the affected reporter GPUs and difficult external networks remain evidence limits. The owner authorized v2.2.2 after final acceptance; the non-publishing candidate checks must pass before the publishing squash merge. Room voice remains in its isolated experiment and is not a feature claim for this release.
2026-10-20 01:15:17 +08:00

585 lines
23 KiB
Go

package forwarding
import (
"bytes"
"context"
"net"
"sync/atomic"
"testing"
"time"
"github.com/TNTcraftHIM/Piik/internal/media/encoded"
"github.com/TNTcraftHIM/Piik/internal/thirdparty/livekit/sendsidebwe"
"github.com/livekit/livekit-server/pkg/sfu"
"github.com/livekit/livekit-server/pkg/sfu/bwe"
"github.com/livekit/livekit-server/pkg/sfu/ccutils"
packetpacer "github.com/livekit/mediatransportutil/pkg/pacer"
"github.com/livekit/protocol/logger"
"github.com/pion/interceptor"
"github.com/pion/interceptor/pkg/twcc"
"github.com/pion/rtcp"
"github.com/pion/rtp/codecs"
"github.com/pion/webrtc/v4"
"github.com/pion/webrtc/v4/pkg/media"
)
func TestPublicationEncoderDemandFollowsCurrentSourceAndLifetime(t *testing.T) {
codec := webrtc.RTPCodecParameters{RTPCodecCapability: webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeVP8, ClockRate: 90_000,
}, PayloadType: 96}
newSource := func(ceiling uint32) *Source {
t.Helper()
source, err := NewSource(SourceOptions{ID: "demand", StreamID: "demand", Codec: codec,
Formats: []LayerFormat{{Width: 640, Height: 360, Bitrate: ceiling}}, MaxPackets: 500})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { source.Close() })
return source
}
source := newSource(300_000)
publication, err := NewPublication(TransportOptions{
Source: source, ConnectionID: "demand-child", InitialBitrate: 100_000,
})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { publication.Close() })
checkDemand := func(want int64) {
t.Helper()
if got := publication.encoderProbeDemand(); got != want {
t.Fatalf("encoder demand = %d, want %d", got, want)
}
if publication.VideoBudget() != 100_000 {
t.Fatal("desired encoder output replaced measured capacity")
}
}
source.SetLowestLayerRateControlled(true)
checkDemand(0) // The source is not attached yet.
if err := publication.SetConnected(); err != nil {
t.Fatal(err)
}
checkDemand(300_000) // No measured allocation: demand is not ceiling minus capacity.
source.SetLowestLayerRateControlled(false)
checkDemand(0) // A raw forwarder cannot spend capacity inside this layer.
source.SetLowestLayerRateControlled(true)
if err := publication.SetActiveCount(0); err != nil {
t.Fatal(err)
}
checkDemand(0)
if err := publication.SetActiveCount(1); err != nil {
t.Fatal(err)
}
checkDemand(300_000)
next := newSource(600_000)
next.SetLowestLayerRateControlled(true)
if err := publication.ReplaceSource(next); err != nil {
t.Fatal(err)
}
source.SetLowestLayerRateControlled(false)
checkDemand(600_000) // Retirement of the old encoder cannot erase new demand.
publication.bandwidth.target.Store(600_000)
if got := publication.encoderProbeDemand(); got != 0 {
t.Fatalf("satisfied encoder kept probing: %d", got)
}
publication.bandwidth.target.Store(100_000)
checkDemand(600_000)
next.SetLowestLayerRateControlled(false)
checkDemand(0)
next.SetLowestLayerRateControlled(true)
if err := publication.Close(); err != nil {
t.Fatal(err)
}
checkDemand(0)
}
func TestPublicationEmptyProbeCannotIncreaseCapacity(t *testing.T) {
const target = int64(1_000_000)
log := logger.GetDiscardLogger()
bandwidth := &publicationBWE{SendSideBWE: sendsidebwe.NewSendSideBWE(sendsidebwe.SendSideBWEParams{
Config: sendsidebwe.DefaultSendSideBWEConfig, Logger: log,
})}
bandwidth.target.Store(target)
queue := &publicationPacer{boundedPacer: newBoundedPacer(
packetpacer.NewNoQueue(log), log, 500,
)}
defer queue.Stop()
info := ccutils.ProbeClusterInfo{Id: 1, CreatedAt: time.Now(), Goal: ccutils.ProbeClusterGoal{
AvailableBandwidthBps: int(target), ExpectedUsageBps: int(target),
DesiredBps: int(target * 2), Duration: bandwidth.ProbeDuration(),
}}
info.Goal.DesiredBytes = int(float64(info.Goal.DesiredBps) * info.Goal.Duration.Seconds() / 8)
bandwidth.ProbeClusterStarting(info)
queue.StartProbeCluster(info)
time.Sleep(info.Goal.Duration)
info = queue.EndProbeCluster(info.Id)
if info.Result.Bytes() != 0 || sendsidebwe.DefaultSendSideBWEConfig.CongestionDetector.ProbeSignal.IsValid(info) {
t.Fatal("fixture did not complete an empty invalid probe")
}
bandwidth.ProbeClusterDone(info)
deadline := time.Now().Add(sendsidebwe.DefaultSendSideBWEConfig.CongestionDetector.ProbePacketGroup.SettleWaitMax + time.Second)
for {
signal, capacity, done := bandwidth.ProbeClusterFinalize()
if done {
if signal != ccutils.ProbeSignalInconclusive || capacity > target || bandwidth.target.Load() != target {
t.Fatalf("empty probe raised publication capacity: signal=%v capacity=%d target=%d", signal, capacity, bandwidth.target.Load())
}
break
}
if time.Now().After(deadline) {
t.Fatal("empty probe did not finalize within the library settle bound")
}
time.Sleep(5 * time.Millisecond)
}
}
func TestPublicationProbeUsesAcknowledgedCapacity(t *testing.T) {
for _, received := range []bool{true, false} {
t.Run(map[bool]string{true: "received", false: "no-feedback"}[received], func(t *testing.T) {
checkPublicationProbeCapacity(t, received)
})
}
}
func checkPublicationProbeCapacity(t *testing.T, received bool) {
t.Helper()
const target = int64(100_000)
bandwidth := &publicationBWE{SendSideBWE: sendsidebwe.NewSendSideBWE(sendsidebwe.SendSideBWEParams{
Config: sendsidebwe.DefaultSendSideBWEConfig, Logger: logger.GetDiscardLogger(),
})}
bandwidth.target.Store(target)
bandwidth.SetBWEListener(&publicationCapacityRecorder{})
// A real non-congesting probe after reset still carries the library's
// initial capacity. A successful 800 kbps probe cannot prove 100 Mbps.
start := time.Now().Add(-time.Second)
recorder := twcc.NewRecorder(42)
// Establish transport history before the idle interval. The initial packet
// has relative time zero, so include a subsequent packet as in a live stream.
for _, age := range []time.Duration{6 * time.Second, 5 * time.Second} {
previous := start.Add(-age).UnixMicro()
sequence := bandwidth.RecordPacketSendAndGetSequenceNumber(previous, 200, false, ccutils.ProbeClusterIdInvalid, false)
recorder.Record(42, sequence, previous+10_000)
}
for _, packet := range recorder.BuildFeedbackPacket() {
bandwidth.HandleTWCCFeedback(packet.(*rtcp.TransportLayerCC))
}
bandwidth.Reset()
info := ccutils.ProbeClusterInfo{Id: 1, CreatedAt: start, Goal: ccutils.ProbeClusterGoal{
AvailableBandwidthBps: int(target), DesiredBps: 800_000,
Duration: 200 * time.Millisecond, DesiredBytes: 20_000,
}}
if !received {
// A delayed completion must back off from now, not an expired start time.
info.CreatedAt = start.Add(-time.Minute)
}
bandwidth.ProbeClusterStarting(info)
for index := range 100 {
at := start.Add(time.Duration(index) * 2 * time.Millisecond).UnixMicro()
sequence := bandwidth.RecordPacketSendAndGetSequenceNumber(at, 200, false, info.Id, true)
recorder.Record(42, sequence, at+10_000)
}
info.Result = ccutils.ProbeClusterResult{StartTime: start.UnixNano(),
EndTime: start.Add(info.Goal.Duration).UnixNano(), PacketsProbe: 100, BytesProbe: 20_000, IsCompleted: true}
bandwidth.ProbeClusterDone(info)
if received {
for _, packet := range recorder.BuildFeedbackPacket() {
bandwidth.HandleTWCCFeedback(packet.(*rtcp.TransportLayerCC))
}
}
deadline := time.Now().Add(sendsidebwe.DefaultSendSideBWEConfig.CongestionDetector.ProbePacketGroup.SettleWaitMax + time.Second)
for {
signal, capacity, done := bandwidth.ProbeClusterFinalize()
if done {
if received {
// Allow the first-packet/time-window difference in the library's
// acknowledged rate, but never its untouched 100 Mbps initial value.
if signal != ccutils.ProbeSignalNotCongesting || capacity <= target || capacity > 1_000_000 {
t.Fatalf("unexpected acknowledged probe capacity: signal=%v committed=%d sent=%.0f", signal, capacity, info.Result.Bitrate())
}
} else if signal != ccutils.ProbeSignalInconclusive || capacity != target {
t.Fatalf("unacknowledged probe changed capacity: signal=%v committed=%d", signal, capacity)
}
if !received && (bandwidth.ProbeDuration() > info.Goal.Duration || bandwidth.CanProbe()) {
t.Fatalf("unacknowledged probe did not back off: duration=%v ready=%v", bandwidth.ProbeDuration(), bandwidth.CanProbe())
}
bandwidth.OnCongestionStateChange(bwe.CongestionStateNone, bwe.CongestionStateCongested, target/2)
if bandwidth.target.Load() != target/2 {
t.Fatal("finalized probe suppressed ordinary media congestion")
}
return
}
if time.Now().After(deadline) {
t.Fatal("probe did not finalize within the library settle bound")
}
time.Sleep(5 * time.Millisecond)
}
}
type publicationCapacityRecorder struct{ capacity int64 }
func (recorder *publicationCapacityRecorder) OnCongestionStateChange(_, _ bwe.CongestionState, capacity int64) {
recorder.capacity = capacity
}
func TestPublicationCongestionUsesCommittedCapacity(t *testing.T) {
recorder := &publicationCapacityRecorder{}
bandwidth := &publicationBWE{listener: recorder, SendSideBWE: sendsidebwe.NewSendSideBWE(sendsidebwe.SendSideBWEParams{
Config: sendsidebwe.DefaultSendSideBWEConfig, Logger: logger.GetDiscardLogger(),
})}
bandwidth.target.Store(1_200_000)
bandwidth.OnCongestionStateChange(bwe.CongestionStateNone, bwe.CongestionStateEarlyWarning, 100_000_000)
bandwidth.OnCongestionStateChange(bwe.CongestionStateEarlyWarning, bwe.CongestionStateCongested, 100_000_000)
if bandwidth.target.Load() != 1_200_000 || recorder.capacity != 1_200_000 {
t.Fatalf("publication promoted an unverified capacity: target=%d allocation=%d", bandwidth.target.Load(), recorder.capacity)
}
bandwidth.OnCongestionStateChange(bwe.CongestionStateCongested, bwe.CongestionStateCongested, 500_000)
bandwidth.OnCongestionStateChange(bwe.CongestionStateCongested, bwe.CongestionStateNone, 800_000)
if bandwidth.target.Load() != 500_000 || recorder.capacity != 500_000 {
t.Fatalf("publication disagrees with its committed video budget: target=%d allocation=%d", bandwidth.target.Load(), recorder.capacity)
}
info := ccutils.ProbeClusterInfo{Id: 1, CreatedAt: time.Now()}
bandwidth.ProbeClusterStarting(info)
for _, ended := range []bool{false, true} {
if ended {
info.Result.EndTime = time.Now().UnixNano()
bandwidth.ProbeClusterDone(info)
}
bandwidth.OnCongestionStateChange(bwe.CongestionStateNone, bwe.CongestionStateCongested, 250_000)
if bandwidth.target.Load() != 500_000 || recorder.capacity != 500_000 {
t.Fatalf("probe congestion reached encoder/allocation budget (settling=%v): target=%d allocation=%d",
ended, bandwidth.target.Load(), recorder.capacity)
}
}
bandwidth.Reset()
bandwidth.OnCongestionStateChange(bwe.CongestionStateCongested, bwe.CongestionStateCongested, 250_000)
if bandwidth.target.Load() != 250_000 || recorder.capacity != 250_000 {
t.Fatal("retired publication probe suppressed ordinary congestion")
}
}
func TestPublicationSimulcastReuseAudioDemandAndClose(t *testing.T) {
ctx, cancel := context.WithTimeout(t.Context(), 12*time.Second)
defer cancel()
check := func(err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
codec := webrtc.RTPCodecParameters{RTPCodecCapability: webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeVP8, ClockRate: 90_000,
RTCPFeedback: []webrtc.RTCPFeedback{{Type: "nack"}, {Type: "nack", Parameter: "pli"}, {Type: webrtc.TypeRTCPFBTransportCC}},
}, PayloadType: 96}
source, err := NewEncodedSource(SourceOptions{ID: "screen", StreamID: "publication-fixture", Codec: codec,
Formats: []LayerFormat{{Width: 8, Height: 8, Bitrate: 90_000}, {Width: 8, Height: 8, Bitrate: 300_000}}})
check(err)
defer source.Close()
settings := webrtc.SettingEngine{}
settings.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeUDP4})
settings.SetIncludeLoopbackCandidate(true)
settings.SetIPFilter(func(ip net.IP) bool { return ip.IsLoopback() })
audio, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeOpus, ClockRate: 48_000, Channels: 2}, "audio", "publication-fixture")
check(err)
publication, err := NewPublication(TransportOptions{Source: source.Source, Settings: settings, ConnectionID: "publication", InitialBitrate: 1_000_000, Audio: audio})
check(err)
defer publication.Close()
if publication.VideoBudget() != 1_000_000 {
t.Fatal("audio changed the video capacity estimate")
}
if len(publication.PC.GetSenders()) != 2 || len(publication.PC.GetSenders()[0].GetParameters().Encodings) != 2 {
t.Fatal("publication must use one simulcast video sender and one audio sender")
}
mediaEngine := &webrtc.MediaEngine{}
check(mediaEngine.RegisterDefaultCodecs())
check(mediaEngine.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: publicationRIDExtension}, webrtc.RTPCodecTypeVideo))
check(mediaEngine.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: "urn:ietf:params:rtp-hdrext:sdes:mid"}, webrtc.RTPCodecTypeVideo))
registry := &interceptor.Registry{}
reports := &receiverReportGate{reportDelay: 20 * time.Millisecond}
registry.Add(reports)
check(webrtc.RegisterDefaultInterceptors(mediaEngine, registry))
receiver, err := webrtc.NewAPI(webrtc.WithMediaEngine(mediaEngine), webrtc.WithInterceptorRegistry(registry), webrtc.WithSettingEngine(settings)).NewPeerConnection(webrtc.Configuration{})
check(err)
defer receiver.Close()
var received [3]atomic.Uint32
var invalid atomic.Bool
var lastSequence, lowSSRC, missingSequence atomic.Uint32
var lowTimestamp atomic.Uint32
lowReports := make(chan *rtcp.SenderReport, 4)
var repaired atomic.Bool
deltaFrame := append([]byte(nil), sfu.VP8KeyFrame8x8...)
deltaFrame[0] |= 1
receiver.OnTrack(func(track *webrtc.TrackRemote, rtpReceiver *webrtc.RTPReceiver) {
index := 2
if track.Kind() == webrtc.RTPCodecTypeVideo {
if track.RID() == "q" {
index = 0
} else if track.RID() == "h" {
index = 1
} else {
invalid.Store(true)
return
}
}
if index == 0 {
go func() {
for {
packets, _, err := rtpReceiver.ReadSimulcastRTCP(track.RID())
if err != nil {
return
}
for _, packet := range packets {
if report, ok := packet.(*rtcp.SenderReport); ok && report.SSRC == uint32(track.SSRC()) {
select {
case lowReports <- report:
default:
}
}
}
}
}()
}
for {
packet, attributes, readErr := ReadRTP(track)
if readErr != nil {
return
}
if len(packet.Payload) == 0 {
continue
}
if index < 2 {
var vp8 codecs.VP8Packet
payload, decodeErr := vp8.Unmarshal(packet.Payload)
if decodeErr != nil || !(bytes.Equal(payload, sfu.VP8KeyFrame8x8) || bytes.Equal(payload, deltaFrame)) {
invalid.Store(true)
return
}
}
if index == 0 {
lastSequence.Store(uint32(packet.SequenceNumber))
lowTimestamp.Store(packet.Timestamp)
lowSSRC.Store(packet.SSRC)
if uint32(packet.SequenceNumber) == missingSequence.Load() && attributes.Get(webrtc.AttributeRtxPayloadType) != nil {
repaired.Store(true)
}
}
received[index].Add(1)
}
})
offer, err := publication.PC.CreateOffer(nil)
check(err)
gather := webrtc.GatheringCompletePromise(publication.PC)
check(publication.PC.SetLocalDescription(offer))
select {
case <-gather:
case <-ctx.Done():
t.Fatal(ctx.Err())
}
check(receiver.SetRemoteDescription(*publication.PC.LocalDescription()))
answer, err := receiver.CreateAnswer(nil)
check(err)
gather = webrtc.GatheringCompletePromise(receiver)
check(receiver.SetLocalDescription(answer))
select {
case <-gather:
case <-ctx.Done():
t.Fatal(ctx.Err())
}
check(publication.PC.SetRemoteDescription(*receiver.LocalDescription()))
for publication.PC.ConnectionState() != webrtc.PeerConnectionStateConnected {
select {
case <-time.After(5 * time.Millisecond):
case <-ctx.Done():
t.Fatal(ctx.Err())
}
}
check(publication.SetConnected())
check(publication.SetConnected())
if len(source.GetDownTracks()) != 2 {
t.Fatal("publication did not attach exactly one output per encoding")
}
started := time.Now()
frameData := sfu.VP8KeyFrame8x8
write := func() {
pts := max(time.Since(started), source.inputPTS+time.Microsecond)
check(source.BeginFrame(pts, time.Now()))
for layer := 0; layer < 2; layer++ {
check(source.WriteFrame(layer, encoded.Frame{Data: frameData, PTS: pts, Duration: time.Second / 30, Recovery: bytes.Equal(frameData, sfu.VP8KeyFrame8x8)}))
}
check(audio.WriteSample(media.Sample{Data: []byte{0xf8, 0xff, 0xfe}, Duration: 20 * time.Millisecond}))
}
for received[0].Load() < 3 || received[1].Load() < 3 || received[2].Load() < 3 {
write()
select {
case <-time.After(time.Second / 30):
case <-ctx.Done():
t.Fatalf("publication RTP missing: low=%d high=%d audio=%d invalid=%t", received[0].Load(), received[1].Load(), received[2].Load(), invalid.Load())
}
}
if invalid.Load() {
t.Fatal("publication changed encoded frame payloads")
}
if counters := publication.Counters(); counters.Frames[0] == 0 || counters.Frames[1] == 0 || counters.AudioBytes == 0 {
t.Fatalf("successful RTP writes were not accounted per encoding: %+v", counters)
}
// Lose a real simulcast packet, then delay its NACK beyond SRTP's replay
// window. The publishing path needs the same negotiated repair as P2P.
missingSequence.Store(uint32(uint16(lastSequence.Load() + 1)))
publication.pacer.mu.Lock()
publication.pacer.limit = 0
publication.pacer.mu.Unlock()
write()
for publication.pacer.stats().DroppedPackets < 2 {
select {
case <-time.After(time.Millisecond):
case <-ctx.Done():
t.Fatal("publication loss was not exercised")
}
}
publication.pacer.mu.Lock()
publication.pacer.limit = source.maxPackets
publication.pacer.mu.Unlock()
frameData = deltaFrame
for range 96 {
before := received[0].Load()
write()
for received[0].Load() == before {
select {
case <-time.After(time.Millisecond):
case <-ctx.Done():
t.Fatal("publication stopped before delayed feedback")
}
}
}
reports.allow.Store(true)
check(receiver.WriteRTCP([]rtcp.Packet{&rtcp.TransportLayerNack{
MediaSSRC: lowSSRC.Load(), Nacks: []rtcp.NackPair{{PacketID: uint16(missingSequence.Load())}},
}}))
for !repaired.Load() {
write()
select {
case <-time.After(10 * time.Millisecond):
case <-ctx.Done():
t.Fatal("publication did not recover delayed loss through negotiated RTX")
}
}
frameData = sfu.VP8KeyFrame8x8
// Once real bitrate measurements exist, one shared capacity update must
// constrain the sum of both video RID allocations.
for {
_, rates := source.GetLayeredBitrate()
if rates[0][0] > 0 && rates[1][0] > 0 {
break
}
write()
select {
case <-time.After(time.Second / 30):
case <-ctx.Done():
t.Fatal("publication did not measure both available encodings")
}
}
// The sender-report owner must also receive its matching receiver feedback.
// Delayed real RTCP distinguishes an observed RTT from loopback's zero ms.
for publication.tracks[0].GetTrackStats().RttMax < 10 {
write()
select {
case <-time.After(time.Second / 30):
case <-ctx.Done():
t.Fatal("receiver reports never reached the publication RTT owner")
}
}
publication.bandwidth.OnCongestionStateChange(bwe.CongestionStateNone, bwe.CongestionStateCongested, 1_000)
// Empty allocations also request zero while the allocator's event is queued.
// Wait for the committed deficit as well as its shared capacity bound.
for publication.tracks[0].BandwidthRequested()+publication.tracks[1].BandwidthRequested() > 1_000 || !publication.Counters().Limited {
select {
case <-time.After(5 * time.Millisecond):
case <-ctx.Done():
t.Fatal("simulcast allocation did not commit its shared capacity limit")
}
}
if publication.LowestLayerBudget() != 1_000 {
t.Fatal("paused higher output retained the lowest encoder's budget")
}
if publication.bandwidth.CongestionState() != bwe.CongestionStateNone || !publication.Counters().Limited {
t.Fatalf("clear transport feedback hid the committed publication allocation deficit: state=%v counters=%+v low=%v high=%v", publication.bandwidth.CongestionState(), publication.Counters(), publication.tracks[0].IsDeficient(), publication.tracks[1].IsDeficient())
}
// Retire high output while allocation is paused, before the low-cache
// rollover below can queue frames for it.
check(publication.SetActiveCount(1))
if publication.RequiredActiveCount() != 1 {
t.Fatal("publication did not reduce its direct demand")
}
// A paused sender's recent packets can age out while capture continues.
// Recovery must still send a probe when those RTX payloads are unavailable.
if publication.tracks[0].WriteProbePackets(300, true) == 0 {
t.Fatal("publication has no receiver-report authority for padding probes")
}
for range source.maxPackets + 8 {
pts := max(time.Since(started), source.inputPTS+time.Microsecond)
check(source.BeginFrame(pts, time.Now()))
for layer := range 2 {
check(source.WriteFrame(layer, encoded.Frame{Data: frameData, PTS: pts, Duration: time.Second / 30}))
}
}
if publication.tracks[0].WriteProbePackets(300, false) == 0 {
t.Fatal("publication probe stalled after its forwarded packets left the source cache")
}
// Restore this allocation fixture's capacity explicitly. A congestion event
// cannot prove an increase; this test covers RID reuse, not bandwidth probing.
publication.bandwidth.target.Store(1_000_000)
publication.allocator.SetChannelCapacity(1_000_000)
// Drain packets already admitted by the pacer before observing inactive output.
for range 6 {
write()
time.Sleep(time.Second / 30)
}
high, low := received[1].Load(), received[0].Load()
for range 6 {
write()
time.Sleep(time.Second / 30)
}
if received[1].Load() != high || received[0].Load() <= low {
t.Fatalf("inactive high RID affected low or continued sending: high=%d->%d low=%d->%d limited=%t lowtrack=%+v hightrack=%+v", high, received[1].Load(), low, received[0].Load(), publication.Counters().Limited, publication.tracks[0].DebugInfo(), publication.tracks[1].DebugInfo())
}
if publication.Counters().Limited {
t.Fatal("publication stayed limited after requested low output recovered and high was deactivated")
}
// The report after paused RTX probing must still match the primary clock.
// DownTrack omits reports while no layer is selected, then resumes normally.
for len(lowReports) > 0 {
<-lowReports
}
select {
case report := <-lowReports:
if delta := int32(report.RTPTime - lowTimestamp.Load()); delta < 0 || delta > 3*90_000 {
t.Fatalf("publication lost its primary clock after RTX probing: delta=%d", delta)
}
case <-ctx.Done():
t.Fatal("resumed publication did not report its media clock")
}
// A live encoder can be constrained inside its sole requested RID. The
// inactive high RID and a fitting measured rate must not hide that demand.
source.SetLowestLayerRateControlled(true)
publication.bandwidth.target.Store(80_000)
if !publication.Counters().Limited {
t.Fatal("publication hid the current encoder's unmet 90 kbps ceiling")
}
publication.bandwidth.target.Store(1_000_000)
if publication.Counters().Limited {
t.Fatal("publication kept a limitation after the encoder ceiling was satisfied")
}
check(publication.SetActiveCount(2))
for received[1].Load() <= high {
write()
select {
case <-time.After(time.Second / 30):
case <-ctx.Done():
t.Fatal("requested high RID did not resume after publisher activation")
}
}
check(publication.Close())
if len(source.GetDownTracks()) != 0 {
t.Fatal("publication retained source outputs")
}
}