Files
Piik/internal/server/route/api.go
T
AAA cdd503a98d feat: improve feedback and App lifecycle reliability (#404)
Waiting feedback and tooltips now share consistent presentation lifecycles, while App shutdown and native-media operation failures stay within their owning session or output. This phase also consolidates the project standards and publishes searchable English/Chinese reader documentation alongside the homepage.

Validation: repository hygiene and Markdown links; TypeScript, 971 Web tests and production build; website/documentation build; Go tests, vet and Windows/Linux builds with Windows capture checks; desktop/mobile UI and documentation review. Linux race, container and three-platform packaging checks run in the release pipeline. Physical macOS/Linux capture and broader network coverage remain outside this acceptance.

The owner approved v1.4.0 publication. Existing empty-source, Windows URL-handler and public-invitation timeout reports remain tracked; this release makes no claim to resolve those device/network reports. The Windows capture-border request is next-phase work.

## Release notes

### 中文

这是一次以稳定性和交互一致性为重点的功能更新。等待与提示更有反馈,App 启动、原生分享和连接恢复的边界处理得到改善,使用与部署指南也有了统一的文档中心。

#### 新增与改进

- 欢迎语、等待短句和六类可轮换的标签页标题共用一致的显示规则。等待短句从一开始就出现,并在停留时轮换;明确的错误与操作提示保持固定。
- 房间准入等漫画采用更克制的小人物与循环动效。改进 tooltip 的定位与指向,减少对播放器的遮挡;状态、图标和文案保持相同含义。
- 新增中英文文档中心,提供搜索、手机目录、使用教程、问题排查与自部署指南。官网 FAQ 聚焦开始使用前的问题,详细排查统一进入文档中心。

#### 修复

- 使用 `--local` 启动不再清空已保存的 Piik 站点地址。
- 打开系统浏览器不再阻塞 App 的退出流程;改进临时公网邀请、终端和诊断导出在关闭时的清理及错误反馈。
- 单条原生媒体连接的操作失败不再被误判为整个会话的协议错误;重复停止分享保持幂等。
- 修复连接替换、转发停止和异常退出时的部分资源清理问题,避免迟到结果影响当前连接。
- Windows 原生采集中,单个输出的编码错误只结束对应输出;共享采集源本身的错误仍会结束分享。

#### 升级与已知边界

本次保持现有协议版本与配置格式。更新 App 或自托管 Server 后,重新打开页面即可使用对应的新界面。

个别设备的空窗口列表、Windows 浏览器启动异常,以及 Linux 编码输出的故障隔离仍在跟进。macOS/Linux 的物理采集和更广泛的公网网络场景仍需实机验证;本次不宣称这些问题已经解决,也不承诺所有网络都能直连。

### English

This feature update focuses on stability and consistent feedback. Waiting states are easier to follow, App startup and native sharing handle several failure cases more reliably, and the new documentation center brings usage and deployment guides together.

#### New and improved

- Welcome lines, waiting captions and six groups of rotating tab titles follow one presentation lifecycle. Waiting captions appear immediately and rotate while visible; actionable errors and instructions remain fixed.
- Room-admission comics use restrained characters and looping motion. Tooltips point more accurately to their controls and interfere less with the player, with consistent meanings across illustrations, icons and copy.
- A new English/Chinese documentation center includes search, mobile navigation, usage guides, troubleshooting and self-hosting. The homepage FAQ covers questions before getting started and links to the documentation for detailed help.

#### Fixes

- Starting with `--local` no longer clears a saved Piik site address.
- Opening the system browser no longer blocks App shutdown. Temporary public invitations, the terminal and diagnostic exports handle cleanup and exit errors more consistently.
- An operation failure on one native media connection no longer becomes a protocol failure for the entire session. Repeated stop-sharing requests are safe.
- Fixed cleanup cases during connection replacement, relay shutdown and unexpected exits, preventing late results from affecting the current connection.
- On Windows, an encoder failure for one native output stops that output only. A failure of the shared capture source still ends sharing.

#### Updating and known limits

Protocol versions and configuration formats are unchanged. After updating the App or your self-hosted Server, reopen the page to use its updated interface.

Empty source lists on some devices, Windows browser-launch failures and Linux output-failure isolation remain under investigation. Physical macOS/Linux capture and broader public-network coverage still need device testing; this update does not claim to resolve those cases or guarantee a direct connection on every network.
2026-09-16 13:13:06 +08:00

501 lines
16 KiB
Go

package route
import (
"github.com/TNTcraftHIM/Piik/internal/server/ordered"
"github.com/TNTcraftHIM/Piik/internal/server/protocol"
)
// Resource is the caller-owned SFU/overlap allocation the controller stores,
// moves between collections and compares by pointer identity (TS: the bare
// Resource type parameter, always used through JS Set semantics). The
// controller never reads its fields; they exist for the router and tests.
type Resource struct {
Kind ResourceKind
// EndpointPeerID is set for ResourceOverlap.
EndpointPeerID string
// Fence fields (RoomID, ShareGeneration, PublicationGeneration) are set
// for sfu-publication and sfu-subscription; ViewerPeerID only for the
// subscription. All are opaque IDs.
RoomID string
ShareGeneration string
PublicationGeneration string
ViewerPeerID string
ConnectionID string
// Released is flipped by the router once the allocation is freed.
Released bool
}
// ResourceKind is the RouteResource discriminator of hybrid-media-router.ts.
type ResourceKind string
// Resource kinds.
const (
ResourceOverlap ResourceKind = "overlap"
ResourceSfuSubscription ResourceKind = "sfu-subscription"
ResourceSfuPublication ResourceKind = "sfu-publication"
)
// UpstreamKind discriminates CandidateTuple, CommittedEdge, committedEdgeSeed
// and QualityUpstream (TS kind: "peer" | "sfu").
type UpstreamKind string
// Upstream kinds.
const (
UpstreamPeer UpstreamKind = "peer"
UpstreamSfu UpstreamKind = "sfu"
)
// Transport is the edge transport (TS transport: "direct" | "sfu").
type Transport string
// Transports.
const (
TransportDirect Transport = "direct"
TransportSfu Transport = "sfu"
)
// PublicationPlan is the SFU tuple action (TS publication: "reuse" | "create" | "replace").
type PublicationPlan string
// Publication plans.
const (
PublicationReuse PublicationPlan = "reuse"
PublicationCreate PublicationPlan = "create"
PublicationReplace PublicationPlan = "replace"
)
// ReservationKind discriminates CandidateReservation.
type ReservationKind string
// Reservation kinds.
const (
ReservationDirect ReservationKind = "direct"
ReservationSfuReuse ReservationKind = "sfu-reuse"
ReservationSfuCreate ReservationKind = "sfu-create"
)
// RetirementKind discriminates EndpointRetirement.
type RetirementKind string
// Retirement kinds.
const (
RetireEdge RetirementKind = "edge"
RetirePublication RetirementKind = "publication"
)
// TransitionKind discriminates EndpointTransition.
type TransitionKind string
// Transition kinds.
const (
TransitionNone TransitionKind = "none"
TransitionOverlap TransitionKind = "overlap"
TransitionBoundedGap TransitionKind = "bounded-gap"
)
// SenderQualityState is the sender-side health sample state
// (TS "unknown" | "healthy" | "degraded").
type SenderQualityState string
// Sender quality states.
const (
SenderQualityUnknown SenderQualityState = "unknown"
SenderQualityHealthy SenderQualityState = "healthy"
SenderQualityDegraded SenderQualityState = "degraded"
)
// DemandReason mirrors protocol RouteDemandReason.
type DemandReason string
// Demand reasons.
const (
DemandJoin DemandReason = "join"
DemandEdgeUnavailable DemandReason = "edge-unavailable"
DemandParentDeparted DemandReason = "parent-departed"
DemandCapacityReduction DemandReason = "capacity-reduction"
DemandSfuBootstrap DemandReason = "sfu-bootstrap"
DemandDirectConvergence DemandReason = "direct-convergence"
DemandQualityConvergence DemandReason = "quality-convergence"
DemandRootConvergence DemandReason = "root-convergence"
)
// FinalRoute mirrors protocol RouteDiagnosticFinalRoute.
type FinalRoute string
// Final routes.
const (
FinalRouteDirect FinalRoute = "direct"
FinalRouteSfu FinalRoute = "sfu"
FinalRouteWaiting FinalRoute = "waiting"
FinalRouteFailed FinalRoute = "failed"
)
// RejectionBucket mirrors protocol RouteDiagnosticRejectionBucket.
type RejectionBucket string
// Rejection buckets.
const (
RejectionNone RejectionBucket = "none"
RejectionStale RejectionBucket = "stale"
RejectionEndpointCapacity RejectionBucket = "endpoint-capacity"
RejectionSfuAdmission RejectionBucket = "sfu-admission"
RejectionCandidateFailed RejectionBucket = "candidate-failed"
RejectionFirstFrameTimeout RejectionBucket = "first-frame-timeout"
RejectionOperationDeadline RejectionBucket = "operation-deadline"
RejectionAborted RejectionBucket = "aborted"
)
// RouteQualityEvidenceResult is the ObserveQualityEvidence outcome.
type RouteQualityEvidenceResult string
// Quality evidence outcomes.
const (
QualityEvidenceRejected RouteQualityEvidenceResult = "rejected"
QualityEvidenceAccepted RouteQualityEvidenceResult = "accepted"
QualityEvidenceObserved RouteQualityEvidenceResult = "observed"
)
// CandidateTuple is one route candidate: a direct edge from ParentPeerID
// (Kind UpstreamPeer, Transport always TransportDirect, Regenerate = rebuild
// the same edge with a fresh connection) or an SFU edge (Kind UpstreamSfu,
// Publication set). TS: CandidateTuple.
type CandidateTuple struct {
Kind UpstreamKind
ParentPeerID string
Transport Transport
Regenerate bool
Publication PublicationPlan
}
// CandidateReservation is what the caller allocated before
// BeginCurrentCandidate. Edge/Publication are set per Kind
// (direct: neither; sfu-reuse: Edge, Borrowed when it is another holder's
// live subscription and must not be released; sfu-create: Edge and
// Publication). Overlap is optional for every kind. TS: CandidateReservation.
type CandidateReservation struct {
Kind ReservationKind
Edge *Resource
Publication *Resource
Borrowed bool
Overlap *Resource
}
// EndpointRetirement is the producer retirement a bounded-gap transition
// requires: an existing direct edge (RetireEdge: Child*/Parent*/Transport/
// ConnectionID) or the Host publication (RetirePublication: HostSessionID/
// Generation/ConnectionID). TS: EndpointRetirement.
type EndpointRetirement struct {
Kind RetirementKind
ChildPeerID string
ChildSessionID string
ParentPeerID string
ParentSessionID string
Transport Transport
ConnectionID string
HostSessionID string
Generation string
}
// EndpointTransition says how the producer's copy budget absorbs the
// candidate. ProducerPeerID is nil only for TransitionNone on an sfu/reuse
// plan (TS omitted it there and endpointTransitionEquals compares it).
// Retire is set only for TransitionBoundedGap. TS: EndpointTransition.
type EndpointTransition struct {
Kind TransitionKind
ProducerPeerID *string
Retire *EndpointRetirement
}
// CandidatePlan is a tuple with its endpoint transition. TS: CandidatePlan.
type CandidatePlan struct {
Tuple CandidateTuple
EndpointTransition EndpointTransition
}
// CommittedEdge is a child's one upstream edge in the committed graph.
// Peer edges (Kind UpstreamPeer) set ParentPeerID/ParentSessionID and leave
// Resource nil; SFU edges (Kind UpstreamSfu) set PublicationGeneration and
// Resource. TS: CommittedEdge.
type CommittedEdge struct {
Kind UpstreamKind
ChildSessionID string
ParentPeerID string
ParentSessionID string
PublicationGeneration string
Transport Transport
ConnectionID string
Usable bool
PhysicalActive bool
Resource *Resource
}
// HostPublication is the single live SFU ingress. TS: HostPublication.
type HostPublication struct {
Generation string
HostSessionID string
ConnectionID string
Usable bool
PhysicalActive bool
Resource *Resource
}
// ConnectionAttemptProgress is the NAT-prediction attempt counter
// (Total is always 3). TS: ConnectionAttemptProgress.
type ConnectionAttemptProgress struct {
Current int
Total int
}
// CurrentAttempt is OperationSnapshot.current: the live candidate attempt.
// ConnectionAttempt is nil when the attempt has no NAT opportunity.
type CurrentAttempt struct {
Tuple CandidateTuple
Revision int64
ConnectionID string
ConnectionAttempt *ConnectionAttemptProgress
}
// OperationSnapshot is a deep copy of the in-flight operation plus the
// derived WakeAtMs. Current is nil without a live attempt. TS: OperationSnapshot.
type OperationSnapshot struct {
ChildPeerID string
ChildSessionID string
DemandPeerID string
Reason DemandReason
BaseRevision int64
FactVersion int64
Candidates []CandidatePlan
Cursor int
DeadlineAtMs int64
WakeAtMs int64
Current *CurrentAttempt
}
// RouteSnapshot is the committed graph as the router reads it.
// UpstreamByViewer is a fresh insertion-ordered copy (child peerId -> edge);
// HostPublication is a shallow copy or nil; Operation is nil when idle.
type RouteSnapshot struct {
Revision int64
Paused bool
FactVersion int64
UpstreamByViewer *ordered.Map[string, CommittedEdge]
HostPublication *HostPublication
Operation *OperationSnapshot
}
// ReconcileResult is the scheduler outcome. TS: ReconcileResult.
type ReconcileResult struct {
Operation *OperationSnapshot
RemovedPeerIDs []string
FailedPeerIDs []string
Released []*Resource
}
// SettleResult is the outcome of candidate settlement or active replacement.
// Committed is true only when CandidateReady committed the attempt (TS: optional
// committed, read as truthy by the router). TS: SettleResult.
type SettleResult struct {
Accepted bool
Committed bool
FailedPeerIDs []string
ActiveRevision int64
Released []*Resource
}
// SenderQualityEvidenceResult is the outcome of the two sender-side
// observers. TS declares it separately with a required committed flag; the
// Go shape is identical to SettleResult.
type SenderQualityEvidenceResult = SettleResult
// BeginResult is the outcome of the cursor-guarded candidate calls.
type BeginResult struct {
Accepted bool
Operation *OperationSnapshot
FailedPeerIDs []string
Released []*Resource
}
// ParticipantInput describes a connected participant. TS: ParticipantInput.
type ParticipantInput struct {
PeerID string
Role protocol.Role
SessionID string
EffectiveDownstreamCapacity int
}
// EdgeGuard identifies one committed edge exactly. ParentSessionID is only
// compared for peer edges; callers pass the edge's own ParentSessionID for
// those and leave it empty for SFU edges. TS: EdgeGuard.
type EdgeGuard struct {
ChildPeerID string
ChildSessionID string
RouteRevision int64
ConnectionID string
ParentSessionID string
}
// ParentEdgeGuard identifies a direct edge from the parent's side.
type ParentEdgeGuard struct {
ParentPeerID string
ParentSessionID string
RouteRevision int64
ConnectionID string
}
// PublicationGuard identifies the Host publication exactly. TS: the inline
// guard of invalidateHostPublication and retireHostPublication.
type PublicationGuard struct {
HostSessionID string
RouteRevision int64
Generation string
ConnectionID string
}
// AdoptDirectConnectionInput is an EdgeGuard plus the replacement
// connection. TS: EdgeGuard & { newConnectionId }.
type AdoptDirectConnectionInput struct {
EdgeGuard
NewConnectionID string
}
// CandidateGuard identifies the live attempt exactly. TS: CandidateGuard.
type CandidateGuard struct {
ChildPeerID string
ChildSessionID string
Revision int64
ConnectionID string
}
// CandidateCursorGuard identifies one candidate slot of the current
// operation: it must still be at Cursor with an equal Plan, built at
// FactVersion on BaseRevision. TS: CandidateCursorGuard.
type CandidateCursorGuard struct {
ChildPeerID string
ChildSessionID string
BaseRevision int64
FactVersion int64
Cursor int
Plan CandidatePlan
}
// BeginInput is the BeginCurrentCandidate input. PublicationGeneration and
// PublicationConnectionID are required for sfu create/replace tuples;
// HostSessionID is required for every SFU tuple (empty = TS undefined,
// rejected the same way). TS: the inline input of beginCurrentCandidate.
type BeginInput struct {
Guard CandidateCursorGuard
NowMs int64
ConnectionID string
Reservation CandidateReservation
PublicationGeneration string
PublicationConnectionID string
HostSessionID string
}
// CandidateProof carries the Viewer's one-shot relative quality approval.
type CandidateProof struct {
RelativeQualityApproved bool
}
// QualityUpstream names the upstream a receiver-side window was measured
// on: PeerID is set only for UpstreamPeer. TS: RouteQualityEvidenceInput.upstream.
type QualityUpstream struct {
Kind UpstreamKind
PeerID string
}
// RouteQualityEvidenceInput is one receiver-side quality window. Only
// FramesDecodedDelta, FreezeCountDelta, FreezeDurationMsDelta,
// PauseCountDelta and PauseDurationMsDelta of Metrics are read.
type RouteQualityEvidenceInput struct {
ChildPeerID string
ChildSessionID string
RouteRevision int64
ConnectionID string
Upstream QualityUpstream
PresentationEpoch int64
WindowMs int64
Metrics protocol.ViewerQualityEvidenceMetrics
AcceptedAtMs int64
}
// SenderQualityEvidenceInput is one sender-side health sample from a parent
// about one child. SenderIdentity and SampleTimestampMs are nullable.
type SenderQualityEvidenceInput struct {
ParentPeerID string
ParentSessionID string
ChildPeerID string
RouteRevision int64
ConnectionID string
SenderIdentity *string
SampleTimestampMs *int64
State SenderQualityState
AcceptedAtMs int64
}
// SfuPublisherQualityEvidenceInput is one Host->SFU ingress health sample.
type SfuPublisherQualityEvidenceInput struct {
HostPeerID string
HostSessionID string
PublicationGeneration string
RouteRevision int64
State SenderQualityState
SampleTimestampMs *int64
AcceptedAtMs int64
}
// Options configures a Controller. DebugRoomID empty disables the sanitised
// route debug events. TS: ControllerOptions.
type Options struct {
HostPeerID string
DebugRoomID string
// DebugLog receives sanitized route events. nil disables them; the effect
// layer owns environment, logger and retention policy.
DebugLog func(string, ...any)
EndpointMediaCopyCapacity int
OperationTimeoutMs int64
SfuEnabled bool
QualityConvergenceEnabled bool
NatPredictionEnabled bool
}
// Controller never locks: the caller holds signal.Server.mu around every call.
type Controller struct {
// hostPeerID is its own mutable field because RebindHostIdentity
// reassigns it; the other
// options are copied once and never change.
hostPeerID string
debugRoomID string
debugLog func(string, ...any)
endpointMediaCopyCapacity int
operationTimeoutMs int64
sfuEnabled bool
qualityConvergenceEnabled bool
natPredictionEnabled bool
// Insertion order is load-bearing for participants, upstreamByViewer and
// directContinuations; the opportunity ledgers and the
// retiring set are ordered for the same delete-while-iterating semantics.
participants ordered.Map[string, *participant]
upstreamByViewer ordered.Map[string, *CommittedEdge]
hostPublication *HostPublication
operation *operation
revision int64
latestRevision int64
factVersion int64
nextJoinOrder int64
paused bool
routeTimings map[string]*routeTimingRecord
qualityObservations map[string]*routeQualityObservation
senderQualityObservations map[string]*senderQualityObservation
senderQualityBaselines map[string]bool // tri-state: absent / false / true (read with comma-ok)
senderQualityRegenerationBlocks map[string]senderQualityIdentity
sfuPublisherQualityObservation *sfuPublisherQualityObservation
qualityBaselinesPending map[string]struct{}
directContinuations ordered.Map[string, *directContinuation]
retiringPublicationGenerations ordered.Map[string, struct{}]
sfuBootstrapIntent *sfuBootstrapIntent
consumedSfuBootstrapOpportunities ordered.Map[string, int]
rootConvergenceRootPeerID string // "" = TS undefined
}