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.
384 lines
11 KiB
Go
384 lines
11 KiB
Go
// Package sfu owns admitted physical publication and subscription resources.
|
|
package sfu
|
|
|
|
import (
|
|
"regexp"
|
|
|
|
"github.com/TNTcraftHIM/Piik/internal/server/ordered"
|
|
"github.com/TNTcraftHIM/Piik/internal/server/protocol"
|
|
)
|
|
|
|
// ResourceFence is SfuResourceFence. It is a comparable value, so the
|
|
// TypeScript cloneFence/sameFence helpers are a plain copy and `==`.
|
|
type ResourceFence struct {
|
|
RoomID string
|
|
ShareGeneration string
|
|
PublicationGeneration string
|
|
}
|
|
|
|
// SubscriptionFence is SfuSubscriptionFence, which extends SfuResourceFence.
|
|
// The embedded value is the `{ ...fence, viewerPeerId }` spread the router
|
|
// uses to derive one from a publication fence.
|
|
type SubscriptionFence struct {
|
|
ResourceFence
|
|
ViewerPeerID string
|
|
ConnectionID string
|
|
}
|
|
|
|
// Usage is SfuResourceUsage.
|
|
type Usage struct {
|
|
Ingress int
|
|
Egress int
|
|
}
|
|
|
|
// AdmissionOptions is SfuResourceAdmissionOptions.
|
|
type AdmissionOptions struct {
|
|
IngressCapacity int
|
|
EgressCapacity int
|
|
}
|
|
|
|
// resourceState is the TypeScript ResourceState union.
|
|
type resourceState string
|
|
|
|
const (
|
|
stateReserved resourceState = "reserved"
|
|
stateCommitted resourceState = "committed"
|
|
stateDraining resourceState = "draining"
|
|
)
|
|
|
|
type subscriptionEntry struct {
|
|
fence SubscriptionFence
|
|
state resourceState
|
|
}
|
|
|
|
type publicationEntry struct {
|
|
fence ResourceFence
|
|
state resourceState
|
|
// Ordering site: subscriptions are only counted, looked up by viewer and
|
|
// state-scanned, so their iteration order is never observable. A plain map
|
|
// is enough here; the two maps above it are ordered.
|
|
subscriptions map[string]*subscriptionEntry
|
|
}
|
|
|
|
// roomPublications is RoomResources.publications. Ordering site: commitPublication
|
|
// returns the drained generations and beginDrainRoom returns every generation in
|
|
// insertion order, both of which the router turns into drain tasks.
|
|
type roomPublications = ordered.Map[string, *publicationEntry]
|
|
|
|
// Admission is SfuResourceAdmission: the ingress/egress ledger for the embedded SFU. It is not safe for concurrent use; the signaling server calls it
|
|
// under its global mutex, exactly as Node's single thread did.
|
|
type Admission struct {
|
|
// Ordering site: beginDrainAll walks the rooms in insertion order.
|
|
rooms ordered.Map[string, *roomPublications]
|
|
ingressCapacity int
|
|
egressCapacity int
|
|
ingressInUse int
|
|
egressInUse int
|
|
}
|
|
|
|
// NewAdmission mirrors the SfuResourceAdmission constructor. An invalid
|
|
// capacity panics where the TypeScript throws: the capacities come from a
|
|
// constant and from validated configuration, so this is an assertion.
|
|
func NewAdmission(options AdmissionOptions) *Admission {
|
|
assertPositiveSafeInteger(options.IngressCapacity, "SFU ingress capacity")
|
|
assertPositiveSafeInteger(options.EgressCapacity, "SFU egress capacity")
|
|
return &Admission{
|
|
ingressCapacity: options.IngressCapacity,
|
|
egressCapacity: options.EgressCapacity,
|
|
}
|
|
}
|
|
|
|
func (admission *Admission) ReservePublication(fence ResourceFence) bool {
|
|
assertFence(fence)
|
|
room, _ := admission.rooms.Get(fence.RoomID)
|
|
var existing *publicationEntry
|
|
if room != nil {
|
|
existing, _ = room.Get(publicationKey(fence))
|
|
}
|
|
if existing != nil {
|
|
return existing.state != stateDraining && existing.fence == fence
|
|
}
|
|
if admission.ingressInUse >= admission.ingressCapacity {
|
|
return false
|
|
}
|
|
if room != nil {
|
|
for _, publication := range room.All() {
|
|
if publication.state == stateReserved {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
|
|
resources := room
|
|
if resources == nil {
|
|
resources = &roomPublications{}
|
|
}
|
|
resources.Set(publicationKey(fence), &publicationEntry{
|
|
fence: fence,
|
|
state: stateReserved,
|
|
subscriptions: map[string]*subscriptionEntry{},
|
|
})
|
|
admission.rooms.Set(fence.RoomID, resources)
|
|
admission.ingressInUse++
|
|
return true
|
|
}
|
|
|
|
func (admission *Admission) ReserveSubscription(fence SubscriptionFence) bool {
|
|
assertSubscriptionFence(fence)
|
|
publication := admission.publication(fence.ResourceFence)
|
|
if publication == nil || publication.state == stateDraining {
|
|
return false
|
|
}
|
|
key := fence.ViewerPeerID + "\x00" + fence.ConnectionID
|
|
if existing, ok := publication.subscriptions[key]; ok {
|
|
return existing.fence == fence && existing.state != stateDraining
|
|
}
|
|
if admission.egressInUse >= admission.egressCapacity {
|
|
return false
|
|
}
|
|
|
|
publication.subscriptions[key] = &subscriptionEntry{
|
|
fence: fence,
|
|
state: stateReserved,
|
|
}
|
|
admission.egressInUse++
|
|
return true
|
|
}
|
|
|
|
// CommitPublication returns false as its second result where TypeScript returns
|
|
// null; an empty slice with true is its empty array.
|
|
func (admission *Admission) CommitPublication(fence ResourceFence) ([]ResourceFence, bool) {
|
|
assertFence(fence)
|
|
room, _ := admission.rooms.Get(fence.RoomID)
|
|
var publication *publicationEntry
|
|
if room != nil {
|
|
publication, _ = room.Get(publicationKey(fence))
|
|
}
|
|
if room == nil || publication == nil || publication.fence != fence {
|
|
return nil, false
|
|
}
|
|
if publication.state == stateCommitted {
|
|
return nil, true
|
|
}
|
|
if publication.state != stateReserved {
|
|
return nil, false
|
|
}
|
|
reserved := false
|
|
for _, subscription := range publication.subscriptions {
|
|
if subscription.state == stateReserved {
|
|
reserved = true
|
|
break
|
|
}
|
|
}
|
|
if !reserved {
|
|
return nil, false
|
|
}
|
|
|
|
var draining []ResourceFence
|
|
for _, other := range room.All() {
|
|
if other == publication || other.state != stateCommitted {
|
|
continue
|
|
}
|
|
markDraining(other)
|
|
draining = append(draining, other.fence)
|
|
}
|
|
publication.state = stateCommitted
|
|
for _, subscription := range publication.subscriptions {
|
|
if subscription.state == stateReserved {
|
|
subscription.state = stateCommitted
|
|
}
|
|
}
|
|
return draining, true
|
|
}
|
|
|
|
func (admission *Admission) CommitSubscription(fence SubscriptionFence) bool {
|
|
assertSubscriptionFence(fence)
|
|
publication := admission.publication(fence.ResourceFence)
|
|
if publication == nil || publication.state != stateCommitted {
|
|
return false
|
|
}
|
|
subscription, ok := publication.subscriptions[fence.ViewerPeerID+"\x00"+fence.ConnectionID]
|
|
if !ok || subscription.fence != fence {
|
|
return false
|
|
}
|
|
if subscription.state == stateCommitted {
|
|
return true
|
|
}
|
|
if subscription.state != stateReserved {
|
|
return false
|
|
}
|
|
subscription.state = stateCommitted
|
|
return true
|
|
}
|
|
|
|
func (admission *Admission) BeginSubscriptionDrain(fence SubscriptionFence) bool {
|
|
assertSubscriptionFence(fence)
|
|
publication := admission.publication(fence.ResourceFence)
|
|
if publication == nil {
|
|
return false
|
|
}
|
|
subscription, ok := publication.subscriptions[fence.ViewerPeerID+"\x00"+fence.ConnectionID]
|
|
if !ok || subscription.fence != fence {
|
|
return false
|
|
}
|
|
subscription.state = stateDraining
|
|
return true
|
|
}
|
|
|
|
func (admission *Admission) BeginDrain(fence ResourceFence) bool {
|
|
assertFence(fence)
|
|
publication := admission.publication(fence)
|
|
if publication == nil {
|
|
return false
|
|
}
|
|
markDraining(publication)
|
|
return true
|
|
}
|
|
|
|
func (admission *Admission) HasPublication(fence ResourceFence) bool {
|
|
publication := admission.publication(fence)
|
|
return publication != nil && publication.state != stateDraining
|
|
}
|
|
|
|
func (admission *Admission) HasSubscription(fence SubscriptionFence) bool {
|
|
publication := admission.publication(fence.ResourceFence)
|
|
if publication == nil || publication.state == stateDraining {
|
|
return false
|
|
}
|
|
entry := publication.subscriptions[fence.ViewerPeerID+"\x00"+fence.ConnectionID]
|
|
return entry != nil && entry.fence == fence && entry.state != stateDraining
|
|
}
|
|
|
|
// In-process closure revokes this exact connection; no reusable token remains.
|
|
func (admission *Admission) CompleteSubscriptionDrain(fence SubscriptionFence) bool {
|
|
publication := admission.publication(fence.ResourceFence)
|
|
if publication == nil {
|
|
return false
|
|
}
|
|
key := fence.ViewerPeerID + "\x00" + fence.ConnectionID
|
|
entry := publication.subscriptions[key]
|
|
if entry == nil || entry.fence != fence || entry.state != stateDraining {
|
|
return false
|
|
}
|
|
delete(publication.subscriptions, key)
|
|
admission.subtractEgress(1)
|
|
return true
|
|
}
|
|
|
|
func (admission *Admission) CompleteDrain(fence ResourceFence) bool {
|
|
assertFence(fence)
|
|
room, _ := admission.rooms.Get(fence.RoomID)
|
|
key := publicationKey(fence)
|
|
var publication *publicationEntry
|
|
if room != nil {
|
|
publication, _ = room.Get(key)
|
|
}
|
|
if room == nil || publication == nil ||
|
|
publication.state != stateDraining || publication.fence != fence {
|
|
return false
|
|
}
|
|
|
|
room.Delete(key)
|
|
admission.ingressInUse--
|
|
admission.subtractEgress(len(publication.subscriptions))
|
|
admission.assertNoUnderflow()
|
|
if room.Len() == 0 {
|
|
admission.rooms.Delete(fence.RoomID)
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (admission *Admission) BeginDrainRoom(roomID string) []ResourceFence {
|
|
if roomID == "" {
|
|
panic("SFU resource room ID is invalid")
|
|
}
|
|
room, _ := admission.rooms.Get(roomID)
|
|
if room == nil {
|
|
return nil
|
|
}
|
|
for _, publication := range room.All() {
|
|
markDraining(publication)
|
|
}
|
|
fences := make([]ResourceFence, 0, room.Len())
|
|
for _, publication := range room.All() {
|
|
fences = append(fences, publication.fence)
|
|
}
|
|
return fences
|
|
}
|
|
|
|
func (admission *Admission) BeginDrainAll() []ResourceFence {
|
|
var fences []ResourceFence
|
|
for _, roomID := range admission.rooms.Keys() {
|
|
fences = append(fences, admission.BeginDrainRoom(roomID)...)
|
|
}
|
|
return fences
|
|
}
|
|
|
|
func (admission *Admission) Usage() Usage {
|
|
return Usage{Ingress: admission.ingressInUse, Egress: admission.egressInUse}
|
|
}
|
|
|
|
func (admission *Admission) publication(fence ResourceFence) *publicationEntry {
|
|
room, ok := admission.rooms.Get(fence.RoomID)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
publication, ok := room.Get(publicationKey(fence))
|
|
if !ok || publication.fence != fence {
|
|
return nil
|
|
}
|
|
return publication
|
|
}
|
|
|
|
func (admission *Admission) subtractEgress(count int) {
|
|
admission.egressInUse -= count
|
|
admission.assertNoUnderflow()
|
|
}
|
|
|
|
func (admission *Admission) assertNoUnderflow() {
|
|
if admission.ingressInUse < 0 || admission.egressInUse < 0 {
|
|
panic("SFU resource accounting underflow")
|
|
}
|
|
}
|
|
|
|
func markDraining(publication *publicationEntry) {
|
|
publication.state = stateDraining
|
|
for _, subscription := range publication.subscriptions {
|
|
subscription.state = stateDraining
|
|
}
|
|
}
|
|
|
|
func publicationKey(fence ResourceFence) string {
|
|
return fence.ShareGeneration + "\x00" + fence.PublicationGeneration
|
|
}
|
|
|
|
func assertFence(fence ResourceFence) {
|
|
if fence.RoomID == "" || fence.ShareGeneration == "" ||
|
|
fence.PublicationGeneration == "" {
|
|
panic("SFU resource fence is invalid")
|
|
}
|
|
}
|
|
|
|
func assertSubscriptionFence(fence SubscriptionFence) {
|
|
assertFence(fence.ResourceFence)
|
|
if fence.ViewerPeerID == "" {
|
|
panic("SFU subscription fence is invalid")
|
|
}
|
|
}
|
|
|
|
// Go's int makes the Number.isSafeInteger fractional case unrepresentable; only the
|
|
// range checks survive.
|
|
func assertPositiveSafeInteger(value int, name string) {
|
|
if value <= 0 || int64(value) > protocol.MaxSafeInteger {
|
|
panic(name + " must be a positive safe integer")
|
|
}
|
|
}
|
|
|
|
var roomIDPattern = regexp.MustCompile(`^[1-9][0-9]{0,11}$`)
|
|
|
|
func validResourceFence(fence ResourceFence) bool {
|
|
return roomIDPattern.MatchString(fence.RoomID) &&
|
|
protocol.ValidOpaqueID(fence.ShareGeneration) &&
|
|
protocol.ValidOpaqueID(fence.PublicationGeneration)
|
|
}
|