Files
piik/internal/server/route/api.go
T
AAA da7218bf44 feat(voice): add room voice and secure local invitations
## Problem and outcome / 问题与结果

Planned release: **v2.3.0**, preserving the published 2.x sharing contracts.

Friends can now talk in a Piik room before, during or after screen sharing. Room
voice, media forwarding and the picture retain separate lifetimes under the
existing room authority and route controller. This phase also improves direct LAN
entry, native capture startup, interaction controls and first-use documentation.

Related: #484, #437, #486, #488, #485 and #489. These references do not declare
all reported hardware or distribution problems fixed.

## Release notes

**Less typing. More talking.**

Piik now supports room voice, with or without screen sharing.

**聊不够?开麦吧!**

Piik 现已支持房间语音,不开播也能聊。

### English

This update adds room voice alongside sharing, chat and reactions, with clearer
controls and direct HTTPS invitations for local and virtual networks.

#### Highlights

- Create a room before sharing. The Host opens or closes room voice; friends
  can join, control their own microphone and listening, or mute someone locally.
  Joining voice keeps the microphone off until you enable it.
- Choose your microphone, adjust microphone, master and individual listening
  volumes from 0% to 200%, and enable voice processing in the interaction window.
  Audio icons offer quick volume sliders. The microphone icon shows live input
  level. Voice continues when the picture stops.
- Use a local HTTPS invitation for microphone and remote-control features on a
  LAN or virtual LAN. A temporary local certificate requires a browser warning
  confirmation; a self-hosted Server can also use supplied certificate files.
- Rotate your local picture, optionally volunteer more relay capacity, and
  download a native Windows x64 Server package.

#### Fixes and polish

- Align Windows automatic encoder probes with the surfaces used during actual
  capture; keep video capture progressing while optional source audio starts.
- Correct Linux desktop pixel-aspect negotiation and accept fitted native
  source dimensions without forcing a different aspect ratio.
- Avoid retaining false media queue congestion when producer and consumer
  clocks disagree.
- Distinguish chat authors with identical names, clarify remote permissions,
  and keep floating tools, contextual hints and microphone indicators aligned.
- Separate first-use guides, room tools and troubleshooting so help is easier
  to find.

Use updated Server/Web pages for room voice and update the App for the native
capture and local HTTPS improvements. Established 2.x sharing remains supported.
macOS builds do not yet carry Developer ID signing or Apple notarization; see the first-launch instructions.
The macOS audio-startup changes still need confirmation on an affected Mac.

### 中文

本次更新为分享、聊天和表情互动加入房间语音,同时打磨操作控件,并为局域网和虚拟局域网提供直接 HTTPS 邀请。

#### 主要变化

- 不开播也能创建房间。房主开启或关闭房间语音;朋友可以加入、自行开关麦克风和收听,或仅在自己这边静音某人。加入语音时默认不开麦。
- 在互动窗口选择麦克风、分别调整 0%–200% 的麦克风、总收听与个人音量,并设置语音处理。音频图标可展开快捷音量条,麦克风图标显示实时输入音量。停止画面分享后,语音仍可继续。
- 局域网和虚拟局域网可使用本地 HTTPS 邀请,启用麦克风和远程控制;临时本地证书需要在浏览器确认警告。自部署服务端也可使用自行提供的证书文件。
- 可旋转本地观看画面、自愿增加转发容量,并提供 Windows x64 服务端下载包。

#### 修复与打磨

- Windows 自动编码探测使用与实际采集一致的输入;可选来源音频启动期间,画面采集继续运行。
- 修正 Linux 桌面采集的像素比例协商;原生来源保留实际画幅,不再要求固定比例的尺寸。
- 避免生产者和消费者时钟不一致时,错误保留媒体队列拥塞状态。
- 区分同名聊天作者,明确远控授权,并统一浮窗、上下文提示与麦克风指示器的细节。
- 分开首次使用、房间工具和故障排查文档,让帮助更容易查找。

房间语音需使用更新后的服务端和网页;原生采集与本地 HTTPS 改进需更新 App。既有 2.x 分享仍保持兼容。
macOS 构建仍未正式签名和公证,首次启动请参照文档;音频启动调整仍需在受影响的 Mac 上确认。

## Verification / 验证

Full Web checks, Go tests/vet/build/native probes, reachable vulnerability checks,
normal-page P2P/SFU voice and compatible sharing, LAN HTTPS, detached interaction
tools, responsive controls and tooltip/pure-visual checks provide acceptance
evidence. Physical macOS/Linux capture and acoustic behavior on arbitrary devices
are not established by these controls.

Scoped contributions from #488 (Linux pixel aspect) and #485 (Windows Server
packaging) are retained as their original authors' patch commits in this PR.
The remaining implementation and integration belong to the maintainer.

## Impact / 影响

Preserve the published 2.x sharing contract. New room-voice behavior is opt-in;
Host voice activation, capture and picture lifetimes remain distinct. Local
certificate material remains in memory; no system trust installation is performed.
2026-10-21 06:51:22 -04:00

516 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. The controller
// never reads its fields; the router owns their meaning and release.
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 identifies the allocation owned by the router.
type ResourceKind string
// Resource kinds.
const (
ResourceOverlap ResourceKind = "overlap"
ResourceSfuSubscription ResourceKind = "sfu-subscription"
ResourceSfuPublication ResourceKind = "sfu-publication"
)
// UpstreamKind discriminates CandidateTuple, CommittedEdge, committedEdgeSeed
// and QualityUpstream.
type UpstreamKind string
// Upstream kinds.
const (
UpstreamPeer UpstreamKind = "peer"
UpstreamSfu UpstreamKind = "sfu"
)
// Transport is the edge transport.
type Transport string
// Transports.
const (
TransportDirect Transport = "direct"
TransportSfu Transport = "sfu"
)
// PublicationPlan is the SFU tuple action.
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.
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"
DemandPublicationAttach DemandReason = "publication-attach"
)
// 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).
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.
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).
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. Retire is set only for TransitionBoundedGap.
type EndpointTransition struct {
Kind TransitionKind
ProducerPeerID *string
Retire *EndpointRetirement
}
// CandidatePlan is a tuple with its endpoint transition.
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.
type CommittedEdge struct {
Kind UpstreamKind
ChildSessionID string
ParentPeerID string
ParentSessionID string
PublicationGeneration string
Transport Transport
ConnectionID string
Usable bool
PhysicalActive bool
// Video attachment belongs to a publication, independently of room transport.
VideoGeneration string
VideoConnectionID string
VideoReady bool
Resource *Resource
// The acquisition's session key stays fixed when signaling ownership rebinds.
opportunityKey string
}
// HostPublication is the single live SFU ingress.
type HostPublication struct {
Generation string
HostSessionID string
ConnectionID string
Usable bool
PhysicalActive bool
Resource *Resource
}
// ConnectionAttemptProgress is the NAT-prediction attempt counter
// (initial attempt plus retries; the controller owns Total).
type ConnectionAttemptProgress struct {
Current int
Total int
}
// CurrentAttempt describes the operation's 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.
type OperationSnapshot struct {
// VideoGeneration fences decoded-frame proof. AttachConnectionID names
// the retained room edge only for DemandPublicationAttach.
VideoGeneration string
AttachConnectionID string
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 {
RoomMedia bool
VideoGeneration string
VideoPaused bool
Revision int64
Paused bool
FactVersion int64
UpstreamByViewer *ordered.Map[string, CommittedEdge]
HostPublication *HostPublication
Operation *OperationSnapshot
}
// ReconcileResult is the scheduler outcome.
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.
type SettleResult struct {
Accepted bool
Committed bool
FailedPeerIDs []string
ActiveRevision int64
Released []*Resource
}
// SenderQualityEvidenceResult is the settlement outcome of sender-side evidence.
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.
type ParticipantInput struct {
PeerID string
Role protocol.Role
SessionID string
EffectiveDownstreamCapacity int
RoutePreference protocol.ViewerRoutePreference
}
// 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.
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.
type PublicationGuard struct {
HostSessionID string
RouteRevision int64
Generation string
ConnectionID string
}
// AdoptDirectConnectionInput is an EdgeGuard plus the replacement
// connection.
type AdoptDirectConnectionInput struct {
EdgeGuard
NewConnectionID string
}
// CandidateGuard identifies the live attempt exactly.
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.
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.
type BeginInput struct {
Guard CandidateCursorGuard
NowMs int64
ConnectionID string
Reservation CandidateReservation
PublicationGeneration string
PublicationConnectionID string
HostSessionID string
}
// CandidateProof carries the decoded publication identity in room-media mode
// and the Viewer's one-shot relative quality approval when required.
type CandidateProof struct {
RelativeQualityApproved bool
VideoGeneration string
}
// QualityUpstream names the upstream a receiver-side window was measured
// on: PeerID is set only for UpstreamPeer.
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.
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
SfuOnly 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
sfuOnly 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
roomMedia bool
voiceListeners map[string]bool
videoGeneration string
videoPaused 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
}