## Problem and outcome / 问题与结果 Sharing could fail when an optional output or encoder comparison failed, while retired Browser work could block media cleanup. Browser pooling also spent its startup protection before publication and classified its synthetic transport clock as realtime video, delaying recovery after a bandwidth drop. These repairs stay within the existing capture, producer, edge and connection owners. Self-hosting samples now follow the configured public Origin and permit the site's requested camera/microphone capture. Piik's own Origin rejection has a specific bilingual explanation, distinct from arbitrary proxy 403 responses. ## Release notes ### 中文 这是一次分享稳定性维护更新,重点修复开播、换源与画质恢复,并改善自部署站点的配置和错误说明。 - Windows 自动选择编码器时,如果已验证 H.264 可用,后续软件编码对比失败或超时会保留可用的 H.264,避免因此无法开播。 - 较低画质输出或单条音频发送连接失败时,保留仍然健康的分享和其他连接。 - 修复停止分享、换源和媒体连接退役时,旧异步任务阻塞清理与恢复的问题。 - 改善浏览器共享编码的首播画质与网络波动后的恢复,避免因短时码率变化反复重建编码器。 - 自部署配置示例默认跟随站点网址;创建房间因网址未获允许而返回 403 时,页面会指出需要检查的配置。nginx 示例也修正了对本站摄像头、麦克风的权限限制。 已有自部署站点若遇到 403,请检查 `PUBLIC_BASE_URL` 与 `ALLOWED_ORIGINS`。使用旧 nginx 示例的站点需更新 `Permissions-Policy`;步骤见[自部署教程](https://piik.tv/docs/zh/self-hosting)。本次更新不改变房间、信令或 App 控制协议。 ### English This maintenance update improves sharing startup, source changes and video-quality recovery, with clearer configuration and error guidance for self-hosted sites. - Windows automatic codec selection retains a verified H.264 encoder if the subsequent software comparison fails or times out, instead of failing to start sharing. - A failed lower-quality output or individual audio sender no longer interrupts healthy sharing and other connections. - Fixes retired asynchronous work blocking cleanup and recovery when stopping sharing, changing sources or retiring media connections. - Improves initial quality and recovery after network fluctuations in Browser pooled encoding, while avoiding unnecessary encoder recreation after short bitrate changes. - The self-hosting sample follows the configured site address by default. Piik's Origin-related room-creation 403 now identifies the settings to check. The nginx sample also permits camera and microphone requests from the site itself. If an existing self-hosted site returns 403, check `PUBLIC_BASE_URL` and `ALLOWED_ORIGINS`. Sites using the old nginx sample must update `Permissions-Policy`; see the [self-hosting guide](https://piik.tv/docs/self-hosting). Room, signaling and App control protocols are unchanged. ## Verification / 验证 - `npm run check`: 75 files / 1,106 tests, TypeScript and production Web build passed. - `npm run check:go`: Go formatting, tests, vet, supported core builds, Windows native compile and regressions passed. Darwin cgo packaging remains owned by its macOS CI runner. - Repository hygiene, documentation checks and website/documentation build passed. - Isolated VP8/H.264 Browser checks: constrained-child isolation and recovery, short congestion pulse, live and quiet settings, pause/resume, source replacement and ordinary fallback passed. Controlled bandwidth can still cause temporary low resolution, stalls and A/V jitter. - Windows native Auto/H.264/VP8 startup, window resize/fullscreen/minimize/restore, Browser-to-App fanout and embedded-SFU checks passed in the bounded local fixtures. - Old-code negative controls reproduce the repaired failure paths. A real Chromium policy check rejects camera/microphone with the previous nginx header and captures both fake devices with the corrected same-origin policy; geolocation remains denied. - Independent read-only review covered root ownership, cancellation and the producer/carrier adaptation boundary. ## Impact / 影响 Compatible patch release, planned as v1.6.4. No wire, storage, routing-authority or interaction-model change. Matching Server restart interrupts current process-owned media; package cutover retains the existing bounded rollback procedure. Unknown field reports still require device/network evidence and are not claimed resolved by local fixtures. Raw diagnostics and experiment artifacts remain ignored; accepted constraints and concise evidence live in their existing documentation owners.
220 lines
8.3 KiB
Go
220 lines
8.3 KiB
Go
package mediaedge
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/TNTcraftHIM/Piik/internal/media/encoded"
|
|
"github.com/TNTcraftHIM/Piik/internal/media/forwarding"
|
|
"github.com/livekit/livekit-server/pkg/sfu"
|
|
"github.com/pion/interceptor"
|
|
"github.com/pion/webrtc/v4"
|
|
)
|
|
|
|
func TestPublicationOwnsOneReservationAndSourceLifetime(t *testing.T) {
|
|
engine, err := NewEngine(EngineOptions{BindAddress: "127.0.0.1:0", IncludeLoopback: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer engine.Close()
|
|
source, err := engine.NewSource("vp8", 3, 2, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer source.Close()
|
|
for index := 0; index < 2; index++ {
|
|
if err = source.SetFormat(index, 320, 180); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err = source.ConfigureOutputs([]uint32{90_000, 300_000}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
publication, err := engine.NewPublication(source, EdgeOptions{ConnectionID: "publication"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
candidate, err := engine.NewPublication(source, EdgeOptions{ConnectionID: "publication"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = publication.transport.SetConnected(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = candidate.transport.SetConnected(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(source.media.GetDownTracks()) != 4 {
|
|
t.Fatalf("candidate overwrote current outputs: tracks=%d current=%s candidate=%s",
|
|
len(source.media.GetDownTracks()), publication.transport.PC.ID(), candidate.transport.PC.ID())
|
|
}
|
|
if _, err = engine.NewPublication(source, EdgeOptions{ConnectionID: "third"}); err == nil {
|
|
t.Fatal("third publication was admitted")
|
|
}
|
|
if source.reservations != 2 {
|
|
t.Fatal("failed publication leaked its reservation")
|
|
}
|
|
edge, err := engine.NewEdge(source, EdgeOptions{ConnectionID: "sibling"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer edge.Close()
|
|
if _, err = engine.NewEdge(source, EdgeOptions{ConnectionID: "overflow"}); !errors.Is(err, ErrSourceCapacity) {
|
|
t.Fatal("publication did not consume exactly one outbound copy")
|
|
}
|
|
if err = candidate.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if source.reservations != 1 || len(source.publications) != 1 || publication.signaling.connection.ConnectionState() == webrtc.PeerConnectionStateClosed {
|
|
t.Fatal("candidate rollback retired the current publication")
|
|
}
|
|
replacement, err := engine.NewPublication(source, EdgeOptions{ConnectionID: "replacement"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = source.DisableLayer(0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = engine.NewPublication(source, EdgeOptions{ConnectionID: "failed-output"}); err == nil {
|
|
t.Fatal("failed output was admitted into publication metadata")
|
|
}
|
|
if source.reservations != 0 || len(source.publications) != 0 {
|
|
t.Fatal("failed publication demand retained a reservation")
|
|
}
|
|
if err = source.ConfigureOutputs([]uint32{90_000, 300_000}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
replacement, err = engine.NewPublication(source, EdgeOptions{ConnectionID: "restored-output"})
|
|
if err != nil {
|
|
t.Fatalf("validated replacement did not restore publication admission: %v", err)
|
|
}
|
|
if err = source.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if replacement.signaling.connection.ConnectionState() != webrtc.PeerConnectionStateClosed || len(engine.publications) != 0 || source.reservations != 0 {
|
|
t.Fatal("source retirement did not release the publication")
|
|
}
|
|
}
|
|
|
|
func TestPublicationAdmissionIgnoresUnrelatedFailedOutputs(t *testing.T) {
|
|
engine, err := NewEngine(EngineOptions{BindAddress: "127.0.0.1:0", IncludeLoopback: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer engine.Close()
|
|
source, err := engine.NewSource("vp8", 2, 2, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer source.Close()
|
|
publication, err := engine.NewPublication(source, EdgeOptions{ConnectionID: "initial"})
|
|
if err != nil {
|
|
t.Fatalf("unconfigured outputs prevented initial publication: %v", err)
|
|
}
|
|
defer publication.Close()
|
|
if err = source.ConfigureOutputs([]uint32{90_000, 300_000, 90_000, 90_000}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, slot := range []int{2, 3, 0, 1} {
|
|
if err = source.DisableLayer(slot); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
candidate, createErr := engine.NewPublication(source, EdgeOptions{ConnectionID: "replacement"})
|
|
if slot >= 2 {
|
|
if createErr != nil {
|
|
t.Fatalf("unrelated failed output %d prevented publication: %v", slot, createErr)
|
|
}
|
|
if len(candidate.Media().Layers) != 2 || len(source.publications) != 2 {
|
|
t.Fatal("independent output failure changed the base publication")
|
|
}
|
|
if err = candidate.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
} else if createErr == nil {
|
|
_ = candidate.Close()
|
|
t.Fatalf("failed base output %d was admitted", slot)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPublicationQualityUsesHighestSentLayerCadence(t *testing.T) {
|
|
check := func(err error) {
|
|
t.Helper()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
engine, err := NewEngine(EngineOptions{BindAddress: "127.0.0.1:0", IncludeLoopback: true})
|
|
check(err)
|
|
defer engine.Close()
|
|
source, err := engine.NewSource("vp8", 1, 2, nil)
|
|
check(err)
|
|
defer source.Close()
|
|
check(source.SetFormat(0, 320, 180))
|
|
check(source.SetFormat(1, 640, 360))
|
|
check(source.ConfigureOutputs([]uint32{90_000, 300_000}))
|
|
publication, err := engine.NewPublication(source, EdgeOptions{ConnectionID: "publication_quality"})
|
|
check(err)
|
|
mediaEngine := &webrtc.MediaEngine{}
|
|
check(mediaEngine.RegisterDefaultCodecs())
|
|
for _, uri := range []string{"urn:ietf:params:rtp-hdrext:sdes:mid", "urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id"} {
|
|
check(mediaEngine.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: uri}, webrtc.RTPCodecTypeVideo))
|
|
}
|
|
registry := &interceptor.Registry{}
|
|
check(webrtc.RegisterDefaultInterceptors(mediaEngine, registry))
|
|
settings := webrtc.SettingEngine{}
|
|
settings.SetIncludeLoopbackCandidate(true)
|
|
receiver, err := webrtc.NewAPI(webrtc.WithMediaEngine(mediaEngine), webrtc.WithInterceptorRegistry(registry), webrtc.WithSettingEngine(settings)).NewPeerConnection(webrtc.Configuration{})
|
|
check(err)
|
|
defer receiver.Close()
|
|
receiver.OnTrack(func(track *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
|
|
for {
|
|
if _, _, readErr := track.ReadRTP(); readErr != nil {
|
|
return
|
|
}
|
|
}
|
|
})
|
|
connectEdgeToReceiver(t, &publication.signaling, receiver)
|
|
assigned, err := forwarding.NewEncodedSource(forwarding.SourceOptions{
|
|
ID: string(source.media.TrackID()), StreamID: source.media.StreamID(), Codec: source.media.Codec(), MaxPackets: encoded.MaxPacketWindow,
|
|
Formats: []forwarding.LayerFormat{{Width: 8, Height: 8, Bitrate: 45_000}, {Width: 8, Height: 8, Bitrate: 150_000}},
|
|
})
|
|
check(err)
|
|
defer assigned.Close()
|
|
check(assigned.BeginFrame(0, time.Now()))
|
|
for layer := range 2 {
|
|
check(assigned.WriteFrame(layer, encoded.Frame{Data: sfu.VP8KeyFrame8x8, Duration: time.Second / 30, Recovery: true}))
|
|
}
|
|
check(publication.transport.ReplaceSource(assigned.Source))
|
|
metadata := publication.Media()
|
|
if len(metadata.Layers) != 2 || metadata.Layers[0].Width != 8 || metadata.Layers[0].Height != 8 ||
|
|
metadata.Layers[0].Bitrate != 45_000 || metadata.Layers[1].Bitrate != 150_000 {
|
|
t.Fatalf("publication descriptor ignored assigned source: %+v", metadata)
|
|
}
|
|
now := time.Now()
|
|
if _, ok := publication.QualitySample(now); ok {
|
|
t.Fatal("first sample invented a window")
|
|
}
|
|
for frame := range 60 {
|
|
pts := time.Duration(frame+1) * time.Second / 30
|
|
check(assigned.BeginFrame(pts, time.Now()))
|
|
check(assigned.WriteFrame(0, encoded.Frame{Data: sfu.VP8KeyFrame8x8, PTS: pts, Duration: time.Second / 30, Recovery: true}))
|
|
if frame%2 == 0 {
|
|
check(assigned.WriteFrame(1, encoded.Frame{Data: sfu.VP8KeyFrame8x8, PTS: pts, Duration: time.Second / 15, Recovery: true}))
|
|
}
|
|
time.Sleep(time.Second / 30)
|
|
}
|
|
sample, ok := publication.QualitySample(now.Add(2 * time.Second))
|
|
if !ok || sample.RID != "h" || sample.ActiveVideoEncodingCount != 2 || sample.Width != 8 || sample.Height != 8 {
|
|
t.Fatalf("actual publication dimensions or active outputs missing: %+v counters=%+v", sample, publication.transport.Counters())
|
|
}
|
|
if sample.IntervalFramesSent == 0 || sample.IntervalFramesSent != publication.qualityCounters.Frames[1] ||
|
|
sample.FramesPerSecond != float64(publication.qualityCounters.Frames[1])/2 {
|
|
t.Fatalf("publication summed layer cadence instead of highest sent output: %+v", sample)
|
|
}
|
|
if sample.BitrateKbps != float64(publication.qualityCounters.VideoBytes*8)/2/1000 {
|
|
t.Fatal("publication did not aggregate actual written layer bytes")
|
|
}
|
|
}
|