Piik now has one public presentation and a coordinated App/Server release path. This phase unifies the room UI, rewrites the bilingual README and guides, adds a static website with an inline product film, and prepares matching GitHub/Gitee packages and update links. The film uses the current homepage and shared product controls on one seekable timeline. Runtime fixes preserve presentation when the App opens a room, normalize Site origins, and keep capture and media-recovery status in the television indicator. New visits use Chinese for a Chinese system language and English otherwise; explicit language and visual-mode choices remain available. An optional site passphrase controls site entry, while room authorization remains enforced. GitHub is the release authority. Gitee mirrors verified packages and provides update metadata when GitHub is unavailable. Reviewed PR release notes become the GitHub Release description. The separate public demo uses P2P only, with HTTPS and STUN verified. Validation: 756 Web tests, TypeScript and production build; Go core tests, formatting, vet and available target builds; bounded Windows capture and App/Site interoperability; browser film checks, audible playback and narrow layouts; anonymous Gitee Windows-package download, checksum and launcher/update-link checks. Public demo acceptance covers authenticated WebSocket signaling, synthetic P2P video and room cleanup on one network. macOS/Linux physical capture and the wider device/network/endurance matrix remain explicitly deferred. Publication builds every package from the merged source SHA. The repository is public, main protection is active, and Pages is configured for piik.tv. CI/publisher and Pages deployment records own publication results. ## Release notes ### 中文 Piik 是免费开源的屏幕共享工具。开个房间,发出邀请,最多 20 位朋友就能用浏览器加入观看。 - 使用浏览器或 Piik App 分享游戏、窗口和整个屏幕。 - 优先建立直连;自托管站点可开启媒体转发作为备用线路。 - Piik App 支持本地房间、临时公网邀请和已有站点。 - Piik Server 内置网页界面、房间管理和连接服务,方便自行部署。 - 提供中英文界面与使用指南,以及 GitHub、Gitee 两条下载线路。 [官网](https://piik.tv) · [在线体验](https://demo.piik.tv) · [使用指南](https://github.com/TNTcraftHIM/Piik/blob/main/docs/guide/getting-started.zh-CN.md) · [自行部署](https://github.com/TNTcraftHIM/Piik/blob/main/docs/operations/self-hosting.zh-CN.md) · [Gitee 下载](https://gitee.com/TNTcraftHIM/Piik/releases) Windows App 和桌面浏览器是本次主要验证的平台。macOS 和 Linux 尚未经过实机测试,欢迎有设备的朋友测试并反馈。演示站和 App 公网邀请使用 P2P,受限网络下可能无法连接;自托管站点可开启媒体转发。 ### English Piik is free, open-source screen sharing for you and up to 20 viewers. Start a room, send an invitation, and your friends can join in a browser. - Share games, windows or a whole screen from your browser or Piik App. - Direct connections come first, with optional media forwarding on self-hosted sites. - Piik App supports local rooms, temporary public invitations and existing sites. - Piik Server includes the web interface, rooms and connection services for self-hosting. - English and Chinese interfaces and guides are available, with GitHub and Gitee downloads. [Website](https://piik.tv) · [Online demo](https://demo.piik.tv) · [User guide](https://github.com/TNTcraftHIM/Piik/blob/main/docs/guide/getting-started.md) · [Self-hosting](https://github.com/TNTcraftHIM/Piik/blob/main/docs/operations/self-hosting.md) · [Gitee downloads](https://gitee.com/TNTcraftHIM/Piik/releases) Windows App and desktop browsers are the primary validated paths. macOS and Linux have not yet been tested on physical devices; testing and feedback are welcome. The demo and App public invitations rely on P2P and may not connect on restrictive networks. Self-hosted sites can enable media forwarding.
275 lines
8.2 KiB
Go
275 lines
8.2 KiB
Go
package signal
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/TNTcraftHIM/Piik/internal/server/protocol"
|
|
"github.com/TNTcraftHIM/Piik/internal/server/route"
|
|
"github.com/TNTcraftHIM/Piik/internal/server/sfu"
|
|
)
|
|
|
|
type drainKind string
|
|
|
|
const (
|
|
drainPublication drainKind = "publication"
|
|
drainSubscription drainKind = "subscription"
|
|
)
|
|
|
|
// sfuDrainTask owns one asynchronous exact physical close.
|
|
type sfuDrainTask struct {
|
|
kind drainKind
|
|
fence sfu.SubscriptionFence
|
|
operation *drainOperation
|
|
}
|
|
|
|
// drainOperation is the promise runSfuDrain tracked: done is closed after err
|
|
// is set, with mu held, so a waiter that relocks sees the settled task.
|
|
type drainOperation struct {
|
|
done chan struct{}
|
|
err error
|
|
}
|
|
|
|
// commitReservation ports commitReservation (O30: drained generations in
|
|
// admission order).
|
|
func (r *router) commitReservation(reservation route.CandidateReservation) bool {
|
|
if reservation.Kind == route.ReservationDirect {
|
|
return true
|
|
}
|
|
if reservation.Kind == route.ReservationSfuReuse {
|
|
return reservation.Edge != nil &&
|
|
reservation.Edge.Kind == route.ResourceSfuSubscription &&
|
|
r.sfu != nil &&
|
|
r.sfu.admission.CommitSubscription(subscriptionFence(reservation.Edge))
|
|
}
|
|
if reservation.Publication == nil || reservation.Publication.Kind != route.ResourceSfuPublication {
|
|
return false
|
|
}
|
|
if r.sfu == nil {
|
|
return false
|
|
}
|
|
draining, ok := r.sfu.admission.CommitPublication(resourceFence(reservation.Publication))
|
|
if !ok {
|
|
return false
|
|
}
|
|
for _, fence := range draining {
|
|
r.scheduleSfuPublicationDrain(fence)
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (r *router) releaseReservation(reservation route.CandidateReservation) {
|
|
r.releaseResources(reservationResources(reservation))
|
|
}
|
|
|
|
// releaseResources retires publications before their exact subscriptions.
|
|
func (r *router) releaseResources(resources []*route.Resource) {
|
|
for _, resource := range resources {
|
|
if resource.Kind == route.ResourceSfuPublication {
|
|
r.releaseResource(resource)
|
|
}
|
|
}
|
|
for _, resource := range resources {
|
|
if resource.Kind != route.ResourceSfuPublication {
|
|
r.releaseResource(resource)
|
|
}
|
|
}
|
|
}
|
|
|
|
// releaseResource ports releaseResource: idempotent through Released.
|
|
func (r *router) releaseResource(resource *route.Resource) {
|
|
if resource.Released {
|
|
return
|
|
}
|
|
resource.Released = true
|
|
switch resource.Kind {
|
|
case route.ResourceOverlap:
|
|
return
|
|
case route.ResourceSfuSubscription:
|
|
r.releaseSubscription(subscriptionFence(resource))
|
|
return
|
|
}
|
|
if r.sfu != nil && r.sfu.admission.BeginDrain(resourceFence(resource)) {
|
|
r.scheduleSfuPublicationDrain(resourceFence(resource))
|
|
}
|
|
}
|
|
|
|
func (r *router) releaseSubscription(fence sfu.SubscriptionFence) bool {
|
|
if r.sfu == nil {
|
|
return false
|
|
}
|
|
accepted := r.sfu.admission.BeginSubscriptionDrain(fence)
|
|
if accepted {
|
|
r.scheduleSfuSubscriptionDrain(fence)
|
|
}
|
|
return accepted
|
|
}
|
|
|
|
// wakeResourceWaiters ports wakeResourceWaiters (O29: snapshot order).
|
|
func (r *router) wakeResourceWaiters() {
|
|
roomIDs := r.resourceWaiters.Keys()
|
|
r.resourceWaiters.Clear()
|
|
for _, roomID := range roomIDs {
|
|
rm, _ := r.rooms.Get(roomID)
|
|
if rm == nil || rm.controller == nil {
|
|
continue
|
|
}
|
|
rm.controller.TouchExternalFacts()
|
|
r.requestPump(roomID)
|
|
}
|
|
}
|
|
|
|
// Reauthentication describes the existing physical connection without replacing it.
|
|
func (r *router) sendFreshSfuConfig(participant authenticatedRouteParticipant) {
|
|
rm, _ := r.rooms.Get(participant.roomID)
|
|
if r.sfu == nil || rm == nil || rm.controller == nil {
|
|
return
|
|
}
|
|
snapshot := rm.controller.Snapshot()
|
|
publication := snapshot.HostPublication
|
|
if publication == nil || !publication.PhysicalActive {
|
|
return
|
|
}
|
|
connectionID := publication.ConnectionID
|
|
if participant.role == protocol.RoleViewer {
|
|
edge, ok := snapshot.UpstreamByViewer.Get(participant.peerID)
|
|
if !ok || edge.Kind != route.UpstreamSfu || !edge.PhysicalActive {
|
|
return
|
|
}
|
|
connectionID = edge.ConnectionID
|
|
}
|
|
r.hooks.sendToSession(participant.sessionID, protocol.SfuConfigMessage{
|
|
Type: "sfu-config", Revision: protocol.Int(snapshot.Revision),
|
|
PublicationGeneration: publication.Generation, ConnectionID: connectionID,
|
|
})
|
|
if participant.role == protocol.RoleHost && publication.Resource != nil {
|
|
if active, err := r.sfu.media.PublicationDemand(resourceFence(publication.Resource), connectionID); err == nil {
|
|
count := protocol.Int(active)
|
|
r.hooks.sendToSession(participant.sessionID, protocol.SfuSignalMessage{
|
|
Type: "sfu-signal", Kind: "layers", Revision: protocol.Int(snapshot.Revision),
|
|
PublicationGeneration: publication.Generation, ConnectionID: connectionID, ActiveCount: &count,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
// scheduleSfuPublicationDrain ports scheduleSfuPublicationDrain (O28: the
|
|
// sibling subscription tasks are deleted during the live iteration).
|
|
func (r *router) scheduleSfuPublicationDrain(fence sfu.ResourceFence) {
|
|
for key, task := range r.sfuDrainTasks.All() {
|
|
if task.kind == drainSubscription && task.fence.ResourceFence == fence {
|
|
r.sfuDrainTasks.Delete(key)
|
|
}
|
|
}
|
|
r.scheduleSfuDrain(drainPublication, sfu.SubscriptionFence{ResourceFence: fence})
|
|
}
|
|
|
|
// scheduleSfuSubscriptionDrain ports scheduleSfuSubscriptionDrain: a pending
|
|
// publication drain of the same room already removes every participant.
|
|
func (r *router) scheduleSfuSubscriptionDrain(fence sfu.SubscriptionFence) {
|
|
if r.sfuDrainTasks.Has(sfu.SubscriptionFence{ResourceFence: fence.ResourceFence}) {
|
|
return
|
|
}
|
|
r.scheduleSfuDrain(drainSubscription, fence)
|
|
}
|
|
|
|
// scheduleSfuDrain ports scheduleSfuDrain.
|
|
func (r *router) scheduleSfuDrain(kind drainKind, fence sfu.SubscriptionFence) {
|
|
if r.sfu == nil {
|
|
return
|
|
}
|
|
key := fence
|
|
task, _ := r.sfuDrainTasks.Get(key)
|
|
if task == nil {
|
|
task = &sfuDrainTask{kind: kind, fence: fence}
|
|
r.sfuDrainTasks.Set(key, task)
|
|
}
|
|
if task.operation == nil {
|
|
r.runSfuDrain(key, task)
|
|
}
|
|
}
|
|
|
|
// runSfuDrain performs physical close outside the authority mutex.
|
|
func (r *router) runSfuDrain(key sfu.SubscriptionFence, task *sfuDrainTask) *drainOperation {
|
|
if task.operation != nil {
|
|
return task.operation
|
|
}
|
|
operation := &drainOperation{done: make(chan struct{})}
|
|
fallback := r.sfu
|
|
if fallback == nil {
|
|
close(operation.done)
|
|
return operation
|
|
}
|
|
task.operation = operation
|
|
r.inflight++
|
|
go func() {
|
|
var err error
|
|
if task.kind == drainPublication {
|
|
err = fallback.media.ClosePublication(task.fence.ResourceFence)
|
|
} else {
|
|
err = fallback.media.CloseSubscription(task.fence, task.fence.ConnectionID)
|
|
}
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.inflight--
|
|
operation.err = r.settleSfuDrain(key, task, err)
|
|
if task.operation == operation {
|
|
task.operation = nil
|
|
}
|
|
close(operation.done)
|
|
}()
|
|
return operation
|
|
}
|
|
|
|
// A failed physical close remains charged until shutdown completes it.
|
|
func (r *router) settleSfuDrain(key sfu.SubscriptionFence, task *sfuDrainTask, err error) error {
|
|
fallback := r.sfu
|
|
if err == nil {
|
|
if registered, _ := r.sfuDrainTasks.Get(key); registered != task {
|
|
return nil
|
|
}
|
|
if task.kind == drainPublication && !fallback.admission.CompleteDrain(task.fence.ResourceFence) {
|
|
err = errors.New("SFU drain has no matching resource generation")
|
|
} else {
|
|
if task.kind == drainSubscription {
|
|
fallback.admission.CompleteSubscriptionDrain(task.fence)
|
|
}
|
|
r.sfuDrainTasks.Delete(key)
|
|
r.wakeResourceWaiters()
|
|
return nil
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
// awaitDrain is `await task.operation` inside close(): mu is released while
|
|
// waiting and re-acquired before returning; ctx bounds the wait (D6).
|
|
func (r *router) awaitDrain(ctx context.Context, operation *drainOperation) error {
|
|
var err error
|
|
r.io(func() {
|
|
select {
|
|
case <-operation.done:
|
|
err = operation.err
|
|
case <-ctx.Done():
|
|
err = ctx.Err()
|
|
}
|
|
})
|
|
return err
|
|
}
|
|
|
|
// reservationResources ports reservationResources: a borrowed reuse edge
|
|
// belongs to another holder and is not released.
|
|
func reservationResources(reservation route.CandidateReservation) []*route.Resource {
|
|
var resources []*route.Resource
|
|
if reservation.Edge != nil && (reservation.Kind != route.ReservationSfuReuse || !reservation.Borrowed) {
|
|
resources = append(resources, reservation.Edge)
|
|
}
|
|
if reservation.Publication != nil {
|
|
resources = append(resources, reservation.Publication)
|
|
}
|
|
if reservation.Overlap != nil {
|
|
resources = append(resources, reservation.Overlap)
|
|
}
|
|
return resources
|
|
}
|