## Problem and outcome Connection recovery and packet repair could leave stale pictures, missing sound or unreliable remote input. This maintenance release repairs media/route ownership, H.264 negotiation, voice forwarding and Windows input recovery, and makes shared mobile controls and popovers easier to use. ## 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 Piik v2.5.1 improves sharing recovery, voice playback and remote control, with clearer mobile controls. It keeps the existing 2.x compatibility baseline. - Improve H.264 selection and picture recovery after reconnecting, changing sources or recovering lost packets; keep a working share when a replacement source fails. - Keep audio and room voice continuous, and prevent duplicate sound when switching playback windows. - Improve Windows input recovery and release remote-control permissions when their connection ends. - Keep mobile controls together and popovers usable after layout changes or in short viewports. - Open the appropriate Android update download, including the Gitee ZIP containing the APK. Self-hosted SFU servers refresh public addresses for new connections without a restart. - Add macOS source-thumbnail support. Update the App and self-hosted site together to receive all fixes. Browser settings and existing room data are retained. ### 中文 Piik v2.5.1 改善分享恢复、语音播放与远程控制的稳定性,让手机操作更顺手,沿用现有 2.x 兼容基线。 - 改善 H.264 编码选择,以及重连、切换分享源和丢包后的画面恢复;新分享源切换失败时保留正常分享。 - 保持声音与房间语音连续,避免切换播放窗口时出现双重声音。 - 改善 Windows 输入恢复,并在连接结束时释放远程控制授权。 - 手机控件保持成组,浮层在布局变化或可见高度较小时仍可操作。 - Android 更新打开对应下载入口,Gitee 提供内含 APK 的 ZIP;自部署 SFU 为新连接刷新公网地址,无需重启服务。 - 补齐 macOS 选源缩略图支持。 请一起更新 App 与自部署站点,以获得全部修复。浏览器设置与已有房间数据保留。 ## Validation - Full frontend typecheck, test and production-build acceptance; current Windows App/Server packaging, extracted-file checks and exact served JavaScript verified locally. - Existing Go, vulnerability, native media and controlled-network results retained at their tested revisions. The final production delta from the fully checked candidate is the shared short-viewport popover correction. - The diagnostic report supplied for #502 informed the Windows input fixes. #500 sharing is confirmed working; its missing source previews are implemented. Original-device follow-up remains separate from this release's verified local fixes. ## Compatibility and scope Preserves the published 2.x Browser/server, Native control, capture and SQLite contracts. No 3.0 or iOS implementation or external contributor patch is included. Related: #500, #502, #510, #512.
494 lines
14 KiB
Go
494 lines
14 KiB
Go
package nativeviewer
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/TNTcraftHIM/Piik/internal/app/mediaedge"
|
|
"github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
|
|
"github.com/TNTcraftHIM/Piik/internal/media/inputroute"
|
|
"github.com/pion/webrtc/v4"
|
|
)
|
|
|
|
type Event struct {
|
|
Current func() bool
|
|
Type string
|
|
ShareID string
|
|
ConnectionID string
|
|
Candidate *webrtc.ICECandidateInit
|
|
State string
|
|
LocalType string
|
|
RemoteType string
|
|
NatTraversalPath string
|
|
Quality *mediaedge.QualitySample
|
|
}
|
|
|
|
type Options struct {
|
|
ShareID string
|
|
EdgeCapacity int
|
|
PortMapping bool
|
|
Events chan<- Event
|
|
Relay *mediaedge.RelayOptions
|
|
}
|
|
|
|
type sessionEdge struct {
|
|
edge *mediaedge.Edge
|
|
sourceConnectionID string
|
|
}
|
|
|
|
type sessionReceiver struct {
|
|
*mediaedge.Receiver
|
|
input *inputroute.Relay
|
|
}
|
|
|
|
func (receiver *sessionReceiver) Close() error {
|
|
if receiver.input != nil {
|
|
receiver.input.Close()
|
|
}
|
|
return receiver.Receiver.Close()
|
|
}
|
|
|
|
// One current upstream and one prepared route may overlap during a handoff.
|
|
const receiverCapacity = 2
|
|
|
|
// Session owns the native media implementation for one Browser Viewer. Room,
|
|
// participant, route, and recovery authority remain in the Browser/server.
|
|
type Session struct {
|
|
shareID string
|
|
edgeCapacity int
|
|
engine *mediaedge.Engine
|
|
events chan<- Event
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
relay *mediaedge.RelayOptions
|
|
|
|
mu sync.Mutex
|
|
receivers map[string]*sessionReceiver
|
|
edges map[string]sessionEdge
|
|
profile *nativecapture.VideoProfile
|
|
closed bool
|
|
}
|
|
|
|
func Start(parent context.Context, options Options) (*Session, error) {
|
|
if parent == nil {
|
|
parent = context.Background()
|
|
}
|
|
if options.ShareID == "" || len(options.ShareID) > 256 ||
|
|
options.EdgeCapacity < 1 || options.EdgeCapacity > mediaedge.MaxRelayChildren {
|
|
return nil, errors.New("native Viewer session input is invalid")
|
|
}
|
|
engine, err := mediaedge.NewEngine(mediaedge.EngineOptions{
|
|
IncludeLoopback: true,
|
|
PortMapping: options.PortMapping,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ctx, cancel := context.WithCancel(parent)
|
|
session := &Session{
|
|
shareID: options.ShareID, edgeCapacity: options.EdgeCapacity,
|
|
engine: engine, events: options.Events, ctx: ctx, cancel: cancel, relay: options.Relay,
|
|
receivers: make(map[string]*sessionReceiver),
|
|
edges: make(map[string]sessionEdge),
|
|
}
|
|
go session.runQuality()
|
|
return session, nil
|
|
}
|
|
|
|
func (session *Session) ShareID() string { return session.shareID }
|
|
|
|
// UpdateProfile records the Host ceiling; receiving alone never starts derivation.
|
|
func (session *Session) UpdateProfile(profile nativecapture.VideoProfile) error {
|
|
if !profile.Valid() {
|
|
return errors.New("native Viewer profile is invalid")
|
|
}
|
|
session.mu.Lock()
|
|
defer session.mu.Unlock()
|
|
if session.closed {
|
|
return errors.New("native Viewer session is closed")
|
|
}
|
|
if session.profile != nil && *session.profile == profile {
|
|
return nil
|
|
}
|
|
for _, receiver := range session.receivers {
|
|
if err := receiver.Source().SetRelayProfile(profile); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
session.profile = &profile
|
|
return nil
|
|
}
|
|
|
|
type OfferResult struct {
|
|
Answer webrtc.SessionDescription
|
|
Audio bool
|
|
Codec string
|
|
Reused bool
|
|
}
|
|
|
|
func (session *Session) AcceptOffer(
|
|
connectionID string,
|
|
offer webrtc.SessionDescription,
|
|
iceServers []webrtc.ICEServer,
|
|
reuse bool,
|
|
punched bool,
|
|
) (OfferResult, error) {
|
|
if connectionID == "" || len(connectionID) > 256 {
|
|
return OfferResult{}, errors.New("native Viewer receiver identity is invalid")
|
|
}
|
|
session.mu.Lock()
|
|
closed := session.closed
|
|
receiver := session.receivers[connectionID]
|
|
atCapacity := len(session.receivers) >= receiverCapacity && receiver == nil
|
|
session.mu.Unlock()
|
|
if closed {
|
|
return OfferResult{}, errors.New("native Viewer session is closed")
|
|
}
|
|
if atCapacity {
|
|
return OfferResult{}, errors.New("native Viewer receiver capacity reached")
|
|
}
|
|
if reuse && receiver != nil {
|
|
answer, reused, err := receiver.Renegotiate(offer, iceServers)
|
|
if err != nil {
|
|
return OfferResult{}, err
|
|
}
|
|
if reused {
|
|
return OfferResult{Answer: answer, Audio: receiver.HasAudio(), Codec: receiver.Codec(), Reused: true}, nil
|
|
}
|
|
}
|
|
var punchedSocket *mediaedge.PunchedSocket
|
|
if punched {
|
|
punchedSocket = session.engine.TakePunchedSocket(connectionID, nil)
|
|
if punchedSocket == nil {
|
|
return OfferResult{}, errors.New("prepared connection is unavailable")
|
|
}
|
|
}
|
|
session.CloseReceiver(connectionID)
|
|
var input *inputroute.Relay
|
|
var onDataChannel func(*webrtc.DataChannel)
|
|
var inputTransportAvailable func(bool)
|
|
if strings.Contains(offer.SDP, "\nm=application ") {
|
|
input = inputroute.New(nil)
|
|
onDataChannel = input.AttachUp
|
|
inputTransportAvailable = input.SetUpAvailable
|
|
}
|
|
mediaReceiver, answer, err := session.engine.NewReceiver(mediaedge.ReceiverOptions{
|
|
Offer: offer,
|
|
ICEServers: iceServers,
|
|
EdgeCapacity: mediaedge.PhysicalSourceCapacity(session.edgeCapacity),
|
|
Relay: session.relay,
|
|
PunchedSocket: punchedSocket,
|
|
Events: mediaedge.ReceiverEvents{
|
|
DataChannel: onDataChannel,
|
|
InputTransportAvailable: inputTransportAvailable,
|
|
LocalCandidate: func(candidate *webrtc.ICECandidateInit, current func() bool) {
|
|
session.emit(Event{
|
|
Current: current,
|
|
Type: "edge-candidate", ShareID: session.shareID,
|
|
ConnectionID: connectionID, Candidate: candidate,
|
|
})
|
|
},
|
|
ConnectionState: func(state webrtc.PeerConnectionState, pair *mediaedge.SelectedPair, isCurrentState func(webrtc.PeerConnectionState) bool) {
|
|
session.emit(Event{
|
|
Current: func() bool { return isCurrentState(state) },
|
|
Type: "edge-state", ShareID: session.shareID,
|
|
ConnectionID: connectionID, State: state.String(),
|
|
})
|
|
if pair != nil {
|
|
session.emit(Event{
|
|
Current: func() bool { return isCurrentState(state) },
|
|
Type: "edge-path", ShareID: session.shareID,
|
|
ConnectionID: connectionID,
|
|
LocalType: pair.Local.String(),
|
|
RemoteType: pair.Remote.String(),
|
|
NatTraversalPath: pair.NatTraversalPath,
|
|
})
|
|
}
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
if input != nil {
|
|
input.Close()
|
|
}
|
|
return OfferResult{}, err
|
|
}
|
|
receiver = &sessionReceiver{Receiver: mediaReceiver, input: input}
|
|
session.mu.Lock()
|
|
if session.closed || session.receivers[connectionID] != nil || len(session.receivers) >= receiverCapacity {
|
|
session.mu.Unlock()
|
|
_ = receiver.Close()
|
|
return OfferResult{}, errors.New("native Viewer receiver is unavailable")
|
|
}
|
|
if session.profile != nil {
|
|
if err = receiver.Source().SetRelayProfile(*session.profile); err != nil {
|
|
session.mu.Unlock()
|
|
_ = receiver.Close()
|
|
return OfferResult{}, err
|
|
}
|
|
}
|
|
session.receivers[connectionID] = receiver
|
|
session.mu.Unlock()
|
|
return OfferResult{Answer: answer, Audio: receiver.HasAudio(), Codec: receiver.Codec()}, nil
|
|
}
|
|
|
|
func (session *Session) AddReceiverCandidate(
|
|
connectionID string,
|
|
candidate *webrtc.ICECandidateInit,
|
|
) error {
|
|
receiver := session.receiver(connectionID)
|
|
if receiver == nil {
|
|
return nil
|
|
}
|
|
return receiver.AddRemoteCandidate(candidate)
|
|
}
|
|
|
|
func (session *Session) PrepareLocalEdge(
|
|
sourceConnectionID string,
|
|
connectionID, offerID string,
|
|
restart bool,
|
|
) (webrtc.SessionDescription, error) {
|
|
if restart {
|
|
session.mu.Lock()
|
|
current := session.edges[connectionID]
|
|
available := !session.closed && current.edge != nil && current.sourceConnectionID == sourceConnectionID
|
|
session.mu.Unlock()
|
|
if !available {
|
|
return webrtc.SessionDescription{}, errors.New("native local Viewer edge does not exist")
|
|
}
|
|
return current.edge.CreateLocalOffer(session.ctx, offerID, true)
|
|
}
|
|
return session.prepareEdge(sourceConnectionID, connectionID, nil, offerID, false)
|
|
}
|
|
|
|
func (session *Session) PrepareEdge(
|
|
sourceConnectionID string,
|
|
connectionID string,
|
|
iceServers []webrtc.ICEServer,
|
|
punched bool,
|
|
) (webrtc.SessionDescription, error) {
|
|
return session.prepareEdge(sourceConnectionID, connectionID, iceServers, "", punched)
|
|
}
|
|
|
|
func (session *Session) prepareEdge(
|
|
sourceConnectionID string,
|
|
connectionID string,
|
|
iceServers []webrtc.ICEServer,
|
|
localOfferID string,
|
|
punched bool,
|
|
) (webrtc.SessionDescription, error) {
|
|
local := localOfferID != ""
|
|
receiver := session.receiver(sourceConnectionID)
|
|
if receiver == nil {
|
|
return webrtc.SessionDescription{}, errors.New("native Viewer source does not exist")
|
|
}
|
|
session.mu.Lock()
|
|
if session.closed || session.edges[connectionID].edge != nil {
|
|
session.mu.Unlock()
|
|
return webrtc.SessionDescription{}, errors.New("native Viewer edge is unavailable")
|
|
}
|
|
if !local && session.profile == nil {
|
|
session.mu.Unlock()
|
|
return webrtc.SessionDescription{}, errors.New("native relay requires the Host profile")
|
|
}
|
|
session.mu.Unlock()
|
|
edge, err := session.engine.NewEdge(receiver.Source(), mediaedge.EdgeOptions{
|
|
ConnectionID: connectionID,
|
|
ICEServers: iceServers,
|
|
Audio: receiver.AudioSource(),
|
|
Local: local,
|
|
Punched: punched,
|
|
Events: mediaedge.EdgeEvents{
|
|
LocalCandidate: func(candidate *webrtc.ICECandidateInit) {
|
|
session.emit(Event{
|
|
Type: "edge-candidate", ShareID: session.shareID,
|
|
ConnectionID: connectionID, Candidate: candidate,
|
|
})
|
|
},
|
|
ConnectionState: func(state webrtc.PeerConnectionState, pair *mediaedge.SelectedPair, isCurrentState func(webrtc.PeerConnectionState) bool) {
|
|
session.emit(Event{
|
|
Current: func() bool { return isCurrentState(state) },
|
|
Type: "edge-state", ShareID: session.shareID,
|
|
ConnectionID: connectionID, State: state.String(),
|
|
})
|
|
if pair != nil {
|
|
session.emit(Event{
|
|
Current: func() bool { return isCurrentState(state) },
|
|
Type: "edge-path", ShareID: session.shareID,
|
|
ConnectionID: connectionID,
|
|
LocalType: pair.Local.String(),
|
|
RemoteType: pair.Remote.String(),
|
|
NatTraversalPath: pair.NatTraversalPath,
|
|
})
|
|
}
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return webrtc.SessionDescription{}, err
|
|
}
|
|
session.mu.Lock()
|
|
if session.closed || session.edges[connectionID].edge != nil ||
|
|
session.receivers[sourceConnectionID] != receiver {
|
|
session.mu.Unlock()
|
|
_ = edge.Close()
|
|
return webrtc.SessionDescription{}, errors.New("native Viewer edge is unavailable")
|
|
}
|
|
session.edges[connectionID] = sessionEdge{
|
|
edge: edge, sourceConnectionID: sourceConnectionID,
|
|
}
|
|
session.mu.Unlock()
|
|
var offer webrtc.SessionDescription
|
|
if receiver.input != nil {
|
|
channel, channelErr := edge.OpenDataChannel(inputroute.Label)
|
|
if channelErr != nil {
|
|
session.CloseEdge(connectionID)
|
|
return webrtc.SessionDescription{}, channelErr
|
|
}
|
|
edge.OnInputTransportLost(receiver.input.AttachDown(connectionID, channel))
|
|
}
|
|
if local {
|
|
offer, err = edge.CreateLocalOffer(session.ctx, localOfferID, false)
|
|
} else {
|
|
offer, err = edge.CreateOffer()
|
|
}
|
|
if err != nil {
|
|
session.CloseEdge(connectionID)
|
|
return webrtc.SessionDescription{}, err
|
|
}
|
|
return offer, nil
|
|
}
|
|
|
|
func (session *Session) SetAnswer(connectionID string, answer webrtc.SessionDescription, offerID string) error {
|
|
edge := session.edge(connectionID)
|
|
if edge == nil {
|
|
return errors.New("native Viewer edge does not exist")
|
|
}
|
|
return edge.SetAnswer(answer, offerID)
|
|
}
|
|
|
|
func (session *Session) AddCandidate(connectionID string, candidate *webrtc.ICECandidateInit) error {
|
|
edge := session.edge(connectionID)
|
|
if edge == nil {
|
|
return nil
|
|
}
|
|
return edge.AddRemoteCandidate(candidate)
|
|
}
|
|
|
|
func (session *Session) CloseReceiver(connectionID string) {
|
|
session.engine.ClosePunch(connectionID)
|
|
session.mu.Lock()
|
|
receiver := session.receivers[connectionID]
|
|
delete(session.receivers, connectionID)
|
|
edges := make([]*mediaedge.Edge, 0)
|
|
for edgeID, owned := range session.edges {
|
|
if owned.sourceConnectionID == connectionID {
|
|
edges = append(edges, owned.edge)
|
|
delete(session.edges, edgeID)
|
|
}
|
|
}
|
|
session.mu.Unlock()
|
|
for _, edge := range edges {
|
|
_ = edge.Close()
|
|
}
|
|
if receiver != nil {
|
|
_ = receiver.Close()
|
|
}
|
|
}
|
|
|
|
func (session *Session) CloseEdge(connectionID string) {
|
|
session.engine.ClosePunch(connectionID)
|
|
session.mu.Lock()
|
|
edge := session.edges[connectionID].edge
|
|
delete(session.edges, connectionID)
|
|
session.mu.Unlock()
|
|
if edge != nil {
|
|
_ = edge.Close()
|
|
}
|
|
}
|
|
|
|
func (session *Session) Close() error {
|
|
session.mu.Lock()
|
|
if session.closed {
|
|
session.mu.Unlock()
|
|
return nil
|
|
}
|
|
session.closed = true
|
|
receivers := make([]*sessionReceiver, 0, len(session.receivers))
|
|
for _, receiver := range session.receivers {
|
|
receivers = append(receivers, receiver)
|
|
}
|
|
edges := make([]*mediaedge.Edge, 0, len(session.edges))
|
|
for _, owned := range session.edges {
|
|
edges = append(edges, owned.edge)
|
|
}
|
|
session.receivers = make(map[string]*sessionReceiver)
|
|
session.edges = make(map[string]sessionEdge)
|
|
session.mu.Unlock()
|
|
session.cancel()
|
|
for _, edge := range edges {
|
|
_ = edge.Close()
|
|
}
|
|
for _, receiver := range receivers {
|
|
_ = receiver.Close()
|
|
}
|
|
return session.engine.Close()
|
|
}
|
|
|
|
func (session *Session) receiver(connectionID string) *sessionReceiver {
|
|
session.mu.Lock()
|
|
defer session.mu.Unlock()
|
|
if session.closed {
|
|
return nil
|
|
}
|
|
return session.receivers[connectionID]
|
|
}
|
|
|
|
func (session *Session) edge(connectionID string) *mediaedge.Edge {
|
|
session.mu.Lock()
|
|
defer session.mu.Unlock()
|
|
if session.closed {
|
|
return nil
|
|
}
|
|
return session.edges[connectionID].edge
|
|
}
|
|
|
|
func (session *Session) emit(event Event) {
|
|
if session.events == nil {
|
|
return
|
|
}
|
|
select {
|
|
case session.events <- event:
|
|
case <-session.ctx.Done():
|
|
}
|
|
}
|
|
|
|
func (session *Session) runQuality() {
|
|
ticker := time.NewTicker(2 * time.Second)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case now := <-ticker.C:
|
|
session.mu.Lock()
|
|
edges := make([]*mediaedge.Edge, 0, len(session.edges))
|
|
for _, owned := range session.edges {
|
|
edges = append(edges, owned.edge)
|
|
}
|
|
session.mu.Unlock()
|
|
for _, edge := range edges {
|
|
if sample, ok := edge.QualitySample(now); ok {
|
|
session.emit(Event{
|
|
Type: "edge-quality", ShareID: session.shareID,
|
|
ConnectionID: edge.ConnectionID(), Quality: &sample,
|
|
})
|
|
}
|
|
}
|
|
case <-session.ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}
|