Files
piik/internal/server/route/timing.go
T
TNTcraft ad02ae0bbc fix(routing): restore room creation after relay disconnects
Fix a room-creation stall caused by repeated publication attachment attempts, and preserve healthy relay subtrees when capacity is reduced. This maintenance update also fixes two UI details and clarifies audio troubleshooting.

## Release notes

**Piik, now in your pocket.**

Piik App supports Android screen sharing, with touch controls for a friend's computer.

**手机,也能开播。**

Piik App 支持 Android 屏幕分享,也能用手机控制朋友的电脑。

### English

This maintenance update restores room creation after an interrupted relay connection and improves relay-limit changes and controls.

- Fix a disconnected relay causing room creation and routing to become unresponsive (#536).
- Preserve healthy relay subtrees when reducing capacity; move a feasible viewer before retiring the relay branch.
- Keep resolution inputs focused when their values update, so editing can continue normally.
- Keep participant headsets visible in both light and dark themes.
- Clarify how virtual audio outputs affect screen-sharing sound and how to check the selected output device.

Existing 2.x App/site compatibility and saved room data are preserved. Refresh open site pages after the update. App-local users need the updated App bundle for its included Web fixes. A hosted-service restart reconnects active rooms.

### 中文

本次维护更新修复转发连接中断后无法创建房间的问题,并改善转发上限调整和控件操作。

- 修复转发节点断线后,房间创建和线路分配可能卡住的问题(#536)。
- 降低转发容量时保留正常的下游分支,优先迁移可迁移的观众,避免直接拆掉整条转发分支。
- 修复分辨率数值更新时输入框失去焦点的问题,保持连续编辑。
- 改善亮色与暗色主题下人物耳机状态的可见性。
- 补充虚拟音频输出与屏幕共享声音的排查说明,帮助确认实际使用的输出设备。

保留现有 2.x App/站点兼容性及已保存的房间数据。站点更新后请刷新已打开的页面;App 本地分享用户需更新 App,才能获得包内网页修复。托管服务重启时,活动房间会重新连接。

## Validation

- `npm run check`: typecheck, 108 test files / 1953 tests, and the standard Web build passed on clean `e49b66497bf452268a3cb75a70d740fbe2eed072`.
- Existing route, signal, Server and HTTP App Go package checks passed, including the #536 regression and capacity subtree cases.
- The standard local Linux amd64 candidate from this revision passed archive/manifest/hash checks and actual execution on the owned Linux VM: configuration checks, health, served Web source identity, two create/authenticate/abandon cycles, and graceful shutdown. This was an explicitly identified development candidate; final v2.6.2 packages remain the release pipeline's output.
- Actual maintenance-branch component previews confirmed resolution input DOM identity/focus after external updates and headset visibility in both themes, including mute/deafen states.

No native platform matrix was rerun for these changes.

## Impact

Browser/server v24, Native control v10, capture v7 and SQLite schema 2 are unchanged. Deployment uses the existing atomic wrapper and scoped postflight; it does not change proxy, firewall or persistence configuration.
2026-11-10 12:02:24 -04:00

181 lines
5.7 KiB
Go

package route
import "github.com/TNTcraftHIM/Piik/internal/server/protocol"
// recordDemand replaces the peer's timing record wholesale.
func (c *Controller) recordDemand(childPeerID string, nowMs int64, reason DemandReason) {
c.routeTimings[childPeerID] = &routeTimingRecord{
demandAtMs: nowMs,
reason: reason,
finalRoute: FinalRouteWaiting,
rejectionBucket: RejectionNone,
}
}
func (c *Controller) ensureDemand(childPeerID string, nowMs int64, reason DemandReason) {
if _, ok := c.routeTimings[childPeerID]; !ok {
c.recordDemand(childPeerID, nowMs, reason)
}
}
func (c *Controller) startOperationTiming(childPeerID string, nowMs int64) {
record := c.routeTimings[childPeerID]
if record == nil {
return
}
startedAt := nowMs
record.operationStartedAtMs = &startedAt
record.candidateStartedAtMs = nil
record.firstDecodedFrameAtMs = nil
record.finalAtMs = nil
record.finalRoute = FinalRouteWaiting
record.rejectionBucket = RejectionNone
}
func (c *Controller) startCandidateTiming(childPeerID string, nowMs int64) {
record := c.routeTimings[childPeerID]
if record == nil {
return
}
startedAt := nowMs
record.candidateStartedAtMs = &startedAt
record.firstDecodedFrameAtMs = nil
record.finalAtMs = nil
record.finalRoute = FinalRouteWaiting
record.rejectionBucket = RejectionNone
}
func (c *Controller) noteRejection(childPeerID string, bucket RejectionBucket) {
if record := c.routeTimings[childPeerID]; record != nil {
record.rejectionBucket = bucket
}
}
func (c *Controller) clearCandidateTiming(childPeerID string) {
record := c.routeTimings[childPeerID]
if record == nil {
return
}
record.candidateStartedAtMs = nil
record.firstDecodedFrameAtMs = nil
record.finalAtMs = nil
record.finalRoute = FinalRouteWaiting
}
func (c *Controller) finishTiming(childPeerID string, nowMs int64, finalRoute FinalRoute, rejectionBucket RejectionBucket, firstDecodedFrame bool) {
record := c.routeTimings[childPeerID]
if record == nil {
return
}
if firstDecodedFrame {
decodedAt := nowMs
record.firstDecodedFrameAtMs = &decodedAt
}
finalAt := nowMs
record.finalAtMs = &finalAt
record.finalRoute = finalRoute
record.rejectionBucket = rejectionBucket
}
func (c *Controller) usableRoute(childPeerID string) bool {
return c.currentFinalRoute(childPeerID) != FinalRouteWaiting
}
func (c *Controller) currentFinalRoute(childPeerID string) FinalRoute {
edge, _ := c.upstreamByViewer.Get(childPeerID)
if edge == nil || !edge.Usable || !edge.PhysicalActive {
return FinalRouteWaiting
}
if edge.Kind == UpstreamPeer {
if c.sourceUsable(edge.ParentPeerID) {
return FinalRouteDirect
}
return FinalRouteWaiting
}
if c.hostPublication != nil && c.hostPublication.Usable &&
c.hostPublication.PhysicalActive &&
c.hostPublication.Generation == edge.PublicationGeneration {
return FinalRouteSfu
}
return FinalRouteWaiting
}
func (c *Controller) routeDemandReason(childPeerID string) DemandReason {
edge, _ := c.upstreamByViewer.Get(childPeerID)
if edge == nil {
return DemandJoin
}
if edge.Kind == UpstreamPeer {
if parent, ok := c.participants.Get(edge.ParentPeerID); ok && parent.departureConfirmed {
return DemandParentDeparted
}
if edge.PhysicalActive && len(c.overflowPeerChildren(edge.ParentPeerID)) > 0 {
return DemandCapacityReduction
}
}
return DemandEdgeUnavailable
}
func (c *Controller) diagnosticParent(edge *CommittedEdge, ordinals map[string]int) protocol.RouteDiagnosticParent {
if edge == nil || !edge.Usable || !edge.PhysicalActive {
return protocol.RouteDiagnosticParent{Kind: "none"}
}
if edge.Kind == UpstreamSfu {
if c.hostPublication != nil && c.hostPublication.Usable &&
c.hostPublication.PhysicalActive &&
c.hostPublication.Generation == edge.PublicationGeneration {
return protocol.RouteDiagnosticParent{Kind: "sfu"}
}
return protocol.RouteDiagnosticParent{Kind: "none"}
}
if edge.ParentPeerID == c.hostPeerID {
return protocol.RouteDiagnosticParent{Kind: "host"}
}
ordinal, ok := ordinals[edge.ParentPeerID]
if !ok {
return protocol.RouteDiagnosticParent{Kind: "none"}
}
return protocol.RouteDiagnosticParent{Kind: "viewer", Ordinal: protocol.Int(ordinal)}
}
// diagnosticQuality returns nil unless the observation is
// fresh, non-empty and still describes the current edge.
func (c *Controller) diagnosticQuality(childPeerID string, edge *CommittedEdge, nowMs int64) *protocol.RouteDiagnosticQuality {
observation := c.qualityObservations[childPeerID]
current, _ := c.participants.Get(childPeerID)
if c.paused || !c.videoWorkEnabled() ||
observation == nil ||
observation.eligibleWindows == 0 ||
nowMs >= observation.lastAcceptedAtMs+qualityEvidenceExpiryMs ||
current == nil || current.sessionID == "" ||
!current.sessionIs(observation.childSessionID) ||
edge == nil ||
!edge.Usable ||
!edge.PhysicalActive ||
c.qualityConnectionID(edge) != observation.connectionID ||
!qualityObservationMatchesEdge(observation, edge) ||
!c.sourceUsableForQuality(edge) {
return nil
}
return &protocol.RouteDiagnosticQuality{
EligibleWindows: protocol.Int(observation.eligibleWindows),
EligibleDurationMs: protocol.Int(observation.eligibleDurationMs),
FreezeWindows: protocol.Int(observation.freezeWindows),
FreezeCount: protocol.Int(observation.freezeCount),
FreezeDurationMs: protocol.Int(observation.freezeDurationMs),
PauseCount: protocol.Int(observation.pauseCount),
PauseDurationMs: protocol.Int(observation.pauseDurationMs),
}
}
// elapsedMs returns nil when the end precedes the start or the difference
// exceeds the wire integer bound.
func elapsedMs(startedAtMs, endedAtMs int64) *protocol.Int {
elapsed := endedAtMs - startedAtMs
if !isSafeInteger(elapsed) || elapsed < 0 {
return nil
}
value := protocol.Int(elapsed)
return &value
}