## Problem and outcome / 问题与结果 Bring Android into Piik's shared App workflow and make phone control usable without a separate Web UI. One signed APK covers ARM64 and x64; screen capture, room authority, routing, voice and site approval reuse the existing Go and Browser owners. Android opens the user's system browser rather than shipping an embedded browser. This phase also fixes capture replacement and encoding-clock failures, improves source-audio startup, contains recoverable input and transport failures, adds a bounded relay-volunteer setting and supports hostnames in `SFU_PUBLIC_IP`. The website separates desktop and mobile downloads, gives iOS users an online entry and aligns the bilingual installation guides. Related reports: #495, #499, #500, #501, #502 and #503. For #502, input execution failures now preserve authorization, and temporary target observations suspend rather than revoke it; the original Windows report remains open for candidate feedback. ## Release notes **Piik, now in your pocket.** Piik App now supports Android screen sharing, with touch controls for a friend's computer. **手机,也能开播。** Piik App 现已支持 Android 屏幕分享,也能用手机控制朋友的电脑。 ### English Piik v2.5.0 brings the App to Android and improves capture reliability, touch control and everyday startup. The existing 2.x compatibility baseline is unchanged. - **Android App:** share your screen, join room voice or control a supported computer. One APK supports ARM64 and x64, using your own browser and the same Piik interface. - **Phone controls:** use direct touch or a compact floating mouse with dragging, left/right buttons and a scroll wheel. Pinch to zoom and pan the picture while controlling. - **More reliable sharing:** keep video progressing through source and encoder-output changes, and improve low-bitrate recovery and stationary-screen keyframes. Correct Windows window-size handling and Linux NVENC settings; start optional source sound after video is ready. - **Smoother App entry:** retry failed startup in place, reopen the running room and retain the correct App connection when returning from the browser. Compress shared page assets to reduce loading traffic and keep microphone choices stable during ordinary page updates. - **Recovery stays local:** a rejected input or canceled source replacement no longer revokes remote-control permission. Full ICE candidate queues and failed audio-parameter updates retain healthy connections. A failed local room leaves the App and unrelated approved sites running. - **Connection settings:** relay volunteers can cap their downstream viewers. Self-hosted SFU setups can use a DNS hostname in `SFU_PUBLIC_IP`. - **Clearer help:** refine sharing-error guidance, preserve input-failure diagnostics without recording input content, and update the bilingual platform and network guides. Update both the App and your self-hosted site to use the new features. Android can control a supported computer; the Android device itself is not a controlled endpoint. If you previously installed a test APK, uninstall it once before installing this release: the official signing key is different. Uninstalling clears App-local site approvals and starred-site choices; browser settings remain in the browser. Later official releases can be installed over this one. ### 中文 Piik v2.5.0 带来 Android App,并改善采集稳定性、手机远控与日常启动体验。沿用现有 2.x 兼容基线。 - **Android App:** 分享手机画面、加入房间语音,或控制支持远控的电脑。一个安装包同时支持 ARM64 与 x64,使用自己的浏览器和熟悉的 Piik 界面。 - **手机远控:** 直接触摸操作,或展开小巧的悬浮鼠标,使用拖拽、左右键与滚轮;控制时也能双指缩放、移动画面。 - **分享更稳定:** 修复切换采集来源、编码输出时画面停止推进的问题,改善低码率恢复与静止画面的关键帧响应。修正 Windows 窗口尺寸处理和 Linux NVENC 设置,来源声音会在画面就绪后启动。 - **App 进出更顺畅:** 启动失败可原地重试,再次打开会回到正在运行的房间;从浏览器返回时保持连接到正确的 App。共用页面资源启用压缩,减少加载流量,麦克风选项不再随普通页面更新反复加载。 - **局部问题就地恢复:** 单次操作失败或取消换源不再撤销远控授权;ICE 候选队列满或音频参数调整失败时保留健康连接。本地房间故障不会连带停止 App 和其他已授权站点的分享。 - **连接设置:** 帮助转发的观众可设置承接人数上限;自部署 SFU 的 `SFU_PUBLIC_IP` 现在支持 DNS 主机名。 - **排查更直接:** 打磨分享失败提示,补充不记录输入内容的远控故障诊断,并更新中英文平台说明与网络指南。 使用新功能时,请一起更新 App 与自部署站点。Android 可作为控制端操作电脑,手机本身不作为被控端。 曾安装测试 APK 的用户需先卸载一次,再安装正式版,两者签名不同。卸载会清除 App 内的站点授权和收藏,浏览器设置仍保留在浏览器中;后续正式版可直接覆盖安装。 ## Verification / 验证 - Local full Web/type/build checks passed: 104 test files, 1,848 tests; the website and documentation builds passed. - Android full-App emulator acceptance covered real decoded screen sharing, concurrent 1080p/540p encoder allocation, live bitrate changes, stationary keyframes, Viewer departure, consent reuse/reentry and final capture-resource retirement. - A local universal release APK passed signing, package identity, version-code, ABI and non-debuggable checks using the permanent release key. GitHub Actions receives the same key through encrypted Secrets; manual candidate runs use a disposable candidate signer. - Windows real capture checks covered H264/VP8 window resizing and restoration. Source-audio startup and failure/close ordering passed focused runtime checks; Linux race checks passed. - Desktop/mobile website layouts were reviewed in English and Chinese, both themes, including grouped downloads, the iOS online entry and self-hosting links. - All three Server and six App candidate targets, including the signed Android universal APK, passed in https://github.com/TNTcraftHIM/Piik/actions/runs/40350004792. This uses the same release packaging path with publication disabled. - The complete network laboratory passed in https://github.com/TNTcraftHIM/Piik/actions/runs/40354253735: 43 expected scenario outcomes (37 media connections, six expected-unavailable controls), six negative controls, and complete process/network cleanup. - Recoverable-error checks cover real encrypted input replies and legacy opt-in, retained grants and held-key cleanup, real Windows target geometry recovery, candidate queue overflow, and audio-parameter retry on the existing edge. Android room failure and full shutdown use real launcher, control WebSocket and supervised-process checks; unrelated site resources remain intact. - The lifecycle fixes at `7eb04de373567cfe7aff2db6c93adf2a38e0b1aa` passed all nine package targets and Go/vulnerability/race/Web checks in https://github.com/TNTcraftHIM/Piik/actions/runs/40362794088, including macOS x64/ARM64 cgo and Android. Publication was skipped. - Final cross-review found and fixed the same candidate-overflow false failure in the SFU voice owner. Its real-Pion regression verifies the first 64 candidates remain queued, a valid answer drains them, and renegotiation succeeds. Independent reviews confirmed real SDP and authorization failures still propagate. - A real-browser before/after check reduced microphone enumeration from nine calls to one across eight ordinary Host rerenders. Changing the native client or switching to Browser capture still enumerates the new devices. The separate focused contribution #507 preserves input focus during queued floating-panel opening and is not attributed to this phase. - The verified Windows candidate and reproduction steps have been updated in #502. macOS/Linux physical-device coverage and iOS device acceptance keep their documented boundaries. ## Impact / 影响 - Planned release: **v2.5.0**; no public wire or storage version bump. - Android is a sharing and controlling endpoint, not a remotely controlled Android device. iOS is held outside this release. - New Android release target joins the existing complete-target, checksum, GitHub and Gitee artifact pipeline. Signing material stays outside Git; optional input SDKs/drivers are not bundled. - The owner authorized closure and publication after the final design review and acceptance checks. Restore the publication gate after the held focused contribution and merge this complete phase once; CI publishes only after its full checks and registered packages pass. - Current TODO, platform guidance, media/routing evidence and release operations retain their existing document owners; no additional audit ledger or changelog archive is introduced.
746 lines
21 KiB
Go
746 lines
21 KiB
Go
package mediaedge
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
|
|
"github.com/TNTcraftHIM/Piik/internal/media/encoded"
|
|
"github.com/TNTcraftHIM/Piik/internal/media/forwarding"
|
|
mediacodec "github.com/livekit/mediatransportutil/pkg/codec"
|
|
"github.com/pion/rtcp"
|
|
"github.com/pion/rtp"
|
|
"github.com/pion/webrtc/v4"
|
|
)
|
|
|
|
const videoClockRate = 90_000
|
|
const videoPacketMTU = 1200
|
|
const h264PayloadType = 102
|
|
const vp8PayloadType = 96
|
|
const allRecoveryLayers = ^uint32(0)
|
|
|
|
var ErrSourceCapacity = errors.New("native media source capacity is exhausted")
|
|
|
|
// OutputPlan belongs to the capture input that produced it. The caller applies
|
|
// it to that exact stream after BeginFrame releases the source locks.
|
|
type OutputPlan struct {
|
|
Active []bool
|
|
RecoveryLayers []int
|
|
Bitrates []uint32
|
|
}
|
|
|
|
type Source struct {
|
|
engine *Engine
|
|
codec string
|
|
media *forwarding.EncodedSource
|
|
formats []atomic.Uint64
|
|
outputBitrates []uint32
|
|
capacity int
|
|
requestKeyFrame func([]int)
|
|
relayProfile *nativecapture.VideoProfile
|
|
relay *relayDerivation
|
|
groups map[int]*outputGroup
|
|
memberships map[groupConsumer]*outputGroup
|
|
groupRecovery atomic.Uint32
|
|
pendingRecovery atomic.Uint32
|
|
recoveryRequests chan struct{}
|
|
recoveryStopped chan struct{}
|
|
inputPTS time.Duration
|
|
inputAt time.Time
|
|
hasInput bool
|
|
|
|
mu sync.Mutex
|
|
writeMu sync.Mutex
|
|
edges map[*Edge]bool
|
|
publications map[*Publication]struct{}
|
|
reservations int
|
|
localReservation bool
|
|
closed bool
|
|
}
|
|
|
|
func (source *Source) Codec() string { return source.codec }
|
|
|
|
// Consumers share the existing bounded encoder groups; transport fanout does
|
|
// not allocate an encoder per child.
|
|
func (source *Source) outputGroupCapacity() int {
|
|
return min(source.capacity, nativecapture.MaxOutputGroups)
|
|
}
|
|
|
|
func (source *Source) SetLowestLayerRateControlled(controlled bool) {
|
|
source.writeMu.Lock()
|
|
defer source.writeMu.Unlock()
|
|
source.media.SetLowestLayerRateControlled(controlled)
|
|
for slot, group := range source.groups {
|
|
if slot != 0 {
|
|
group.media.SetLowestLayerRateControlled(controlled)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The room supplies a relay ceiling, never a replacement capture authority.
|
|
// Recording it does not create an encoder or invoke the capture process.
|
|
func (source *Source) SetRelayProfile(profile nativecapture.VideoProfile) error {
|
|
if !profile.Valid() {
|
|
return errors.New("native relay profile is invalid")
|
|
}
|
|
source.mu.Lock()
|
|
if source.closed {
|
|
source.mu.Unlock()
|
|
return io.ErrClosedPipe
|
|
}
|
|
if source.relayProfile != nil && *source.relayProfile == profile {
|
|
source.mu.Unlock()
|
|
return nil
|
|
}
|
|
source.relayProfile = &profile
|
|
source.mu.Unlock()
|
|
if source.relay != nil {
|
|
source.relay.cancel()
|
|
source.writeMu.Lock()
|
|
defer source.writeMu.Unlock()
|
|
return source.configureRelayFormats()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// BeginFrame runs once before outputs for an actual source input. Unconnected
|
|
// edges retain their capacity reservation but do not demand encoded output yet.
|
|
func (source *Source) BeginFrame(timestamp time.Duration) (OutputPlan, error) {
|
|
return source.beginFrame(timestamp, time.Now())
|
|
}
|
|
|
|
func (source *Source) beginFrame(timestamp time.Duration, at time.Time) (OutputPlan, error) {
|
|
source.writeMu.Lock()
|
|
var unavailable []*Edge
|
|
defer func() {
|
|
source.writeMu.Unlock()
|
|
closeEdges(unavailable)
|
|
}()
|
|
source.mu.Lock()
|
|
if source.closed {
|
|
source.mu.Unlock()
|
|
return OutputPlan{}, io.ErrClosedPipe
|
|
}
|
|
edges := make([]*Edge, 0, len(source.edges))
|
|
for edge := range source.edges {
|
|
edges = append(edges, edge)
|
|
}
|
|
publications := make([]*Publication, 0, len(source.publications))
|
|
for publication := range source.publications {
|
|
publications = append(publications, publication)
|
|
}
|
|
source.mu.Unlock()
|
|
if err := source.beginGroupFrame(timestamp, at); err != nil {
|
|
return OutputPlan{}, err
|
|
}
|
|
for _, edge := range edges {
|
|
if edge.State() != webrtc.PeerConnectionStateConnected {
|
|
continue
|
|
}
|
|
if err := edge.transport.SetConnected(); err != nil {
|
|
// Retirement can race the connected snapshot. An attachment failure
|
|
// belongs to this consumer, not the shared capture and its siblings.
|
|
unavailable = append(unavailable, edge)
|
|
continue
|
|
}
|
|
if source.unavailableDemand(edge) {
|
|
unavailable = append(unavailable, edge)
|
|
continue
|
|
}
|
|
edge.transport.Output.BeginFrame()
|
|
}
|
|
if len(source.formats) == 2 && len(source.outputBitrates) >= 2 {
|
|
plan, err := source.planGroups(source.collectDemands(edges, publications))
|
|
for mask, slot := source.groupRecovery.Swap(0), 0; mask != 0; mask, slot = mask>>1, slot+1 {
|
|
if mask&1 != 0 && slot < len(plan.Active) && plan.Active[slot] {
|
|
plan.RecoveryLayers = append(plan.RecoveryLayers, slot)
|
|
}
|
|
}
|
|
return plan, err
|
|
}
|
|
plan := OutputPlan{Active: make([]bool, len(source.outputBitrates)), Bitrates: append([]uint32(nil), source.outputBitrates...)}
|
|
count := 0
|
|
var sharedBudget int64
|
|
remoteDemand := false
|
|
for _, edge := range edges {
|
|
if edge.State() == webrtc.PeerConnectionStateConnected {
|
|
required := edge.transport.RequiredActiveCount()
|
|
count = max(count, required)
|
|
if !edge.local && required > 0 {
|
|
remoteDemand = true
|
|
sharedBudget = max(sharedBudget, edge.transport.Output.State().VideoBudget)
|
|
}
|
|
}
|
|
}
|
|
for _, publication := range publications {
|
|
required := publication.RequiredActiveCount()
|
|
count = max(count, required)
|
|
if required > 0 {
|
|
remoteDemand = true
|
|
sharedBudget = max(sharedBudget, publication.LowestLayerBudget())
|
|
}
|
|
}
|
|
for slot := 0; slot < min(count, len(plan.Active)); slot++ {
|
|
plan.Active[slot] = true
|
|
}
|
|
if len(source.formats) == 1 && len(plan.Bitrates) == 1 && plan.Bitrates[0] > 0 && remoteDemand {
|
|
// One shared rendition follows the largest child demand. Local preview
|
|
// does not hold it at the ceiling or reduce a healthy child's quality.
|
|
plan.Bitrates[0] = uint32(max(1000, min(source.media.CodecBudget(0, sharedBudget), int64(plan.Bitrates[0]))))
|
|
}
|
|
return plan, nil
|
|
}
|
|
|
|
// ConfigureOutputs installs one validated capture generation's bitrate bounds.
|
|
// A zero slot subsequently means that producer has failed, not a new preset.
|
|
func (source *Source) ConfigureOutputs(ceilings []uint32) error {
|
|
if len(ceilings) < len(source.formats) || len(ceilings) > len(source.formats)+source.outputGroupCapacity() ||
|
|
len(source.formats) != 2 && len(ceilings) != len(source.formats) {
|
|
return errors.New("native output count does not match the source")
|
|
}
|
|
for _, bitrate := range ceilings {
|
|
if bitrate < 1000 {
|
|
return errors.New("native output bitrate is invalid")
|
|
}
|
|
}
|
|
source.writeMu.Lock()
|
|
defer source.writeMu.Unlock()
|
|
source.mu.Lock()
|
|
defer source.mu.Unlock()
|
|
if source.closed {
|
|
return io.ErrClosedPipe
|
|
}
|
|
source.outputBitrates = append([]uint32(nil), ceilings...)
|
|
formats := make([]forwarding.LayerFormat, len(source.formats))
|
|
for layer := range formats {
|
|
format := source.formats[layer].Load()
|
|
formats[layer] = forwarding.LayerFormat{Width: uint32(format >> 32), Height: uint32(format), Bitrate: ceilings[layer]}
|
|
}
|
|
if err := source.media.UpdateFormats(formats); err != nil {
|
|
return err
|
|
}
|
|
for slot, group := range source.groups {
|
|
group.budget = ceilings[slot]
|
|
if slot != 0 {
|
|
formats[0].Bitrate = ceilings[slot]
|
|
if err := group.media.UpdateFormats(formats); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// MarkLayerUnavailable commits output failure without closing transports. The
|
|
// caller must run the returned retirement after releasing its capture-owner lock.
|
|
func (source *Source) MarkLayerUnavailable(layer int) (func(), error) {
|
|
return source.markLayerUnavailable(layer, nil)
|
|
}
|
|
|
|
func (source *Source) markLayerUnavailable(layer int, run *relayRun) (func(), error) {
|
|
if run != nil {
|
|
run.owner.mu.Lock()
|
|
defer run.owner.mu.Unlock()
|
|
}
|
|
source.writeMu.Lock()
|
|
defer source.writeMu.Unlock()
|
|
source.mu.Lock()
|
|
// Normal retirement cannot disable a later run's slots. A real cancellation
|
|
// cause still owns failure isolation until that run has finished retiring.
|
|
if run != nil && (run.owner.run != run || errors.Is(context.Cause(run.ctx), context.Canceled) ||
|
|
source.relayProfile != run.plan.profile || source.formats[len(source.formats)-1].Load() != run.plan.format) {
|
|
source.mu.Unlock()
|
|
return nil, nil
|
|
}
|
|
if source.closed || layer < 0 || layer >= len(source.outputBitrates) {
|
|
source.mu.Unlock()
|
|
return nil, errors.New("native output layer is unavailable")
|
|
}
|
|
source.outputBitrates[layer] = 0
|
|
if layer == 0 {
|
|
source.media.SetLowestLayerRateControlled(false)
|
|
}
|
|
publications := make([]*Publication, 0, len(source.publications))
|
|
group := source.groups[layer]
|
|
for publication := range source.publications {
|
|
if layer == 1 || group == nil && layer < len(source.formats) || group != nil &&
|
|
(publication.transport.CurrentSource() == group.media.Source || source.memberships[publication.transport] == group) {
|
|
publications = append(publications, publication)
|
|
}
|
|
}
|
|
var affected []*Edge
|
|
for edge := range source.edges {
|
|
state := edge.transport.Output.State()
|
|
_, observed := edge.transport.TargetBitrate()
|
|
selectedLayer := int32(layer)
|
|
if group != nil {
|
|
if source.memberships[edge.transport] == group {
|
|
affected = append(affected, edge)
|
|
continue
|
|
}
|
|
if edge.transport.CurrentSource() != group.media.Source {
|
|
continue
|
|
}
|
|
selectedLayer = 0
|
|
}
|
|
if state.Target == selectedLayer || state.Current == selectedLayer ||
|
|
(observed && state.Paused && state.Prepare == selectedLayer) ||
|
|
(state.Target < 0 && state.Current < 0 && edge.transport.Output.MaxLayer().Spatial == selectedLayer) {
|
|
affected = append(affected, edge)
|
|
}
|
|
}
|
|
source.mu.Unlock()
|
|
media, logical := source.media, layer
|
|
if group != nil {
|
|
media, logical = group.media, 0
|
|
group.active = false
|
|
media.SetLowestLayerRateControlled(false)
|
|
}
|
|
if logical < len(source.formats) {
|
|
if tracker := media.StreamTrackerManager().GetTracker(int32(logical)); tracker != nil {
|
|
tracker.SetPaused(true)
|
|
}
|
|
}
|
|
// An unavailable encoder retires its consumers through the existing edge
|
|
// lifecycle. Healthy sibling outputs are neither stopped nor restarted.
|
|
return func() {
|
|
closeEdges(affected)
|
|
// Publication metadata declares every slot, including paused or preparing
|
|
// uploads. None may publish a descriptor containing a failed encoding.
|
|
for _, publication := range publications {
|
|
_ = publication.Close()
|
|
}
|
|
}, nil
|
|
}
|
|
|
|
func (source *Source) WriteVideo(layer int, frame encoded.Frame) error {
|
|
source.writeMu.Lock()
|
|
defer source.writeMu.Unlock()
|
|
return source.writeVideo(layer, frame)
|
|
}
|
|
|
|
// Caller serializes capture/source generation with this output.
|
|
func (source *Source) writeVideo(layer int, frame encoded.Frame) error {
|
|
source.mu.Lock()
|
|
closed := source.closed
|
|
source.mu.Unlock()
|
|
if closed {
|
|
return io.ErrClosedPipe
|
|
}
|
|
if len(source.outputBitrates) > 0 &&
|
|
(layer < 0 || layer >= len(source.outputBitrates) || source.outputBitrates[layer] == 0) {
|
|
return errors.New("native output layer is unavailable")
|
|
}
|
|
if len(frame.Data) == 0 || frame.Duration <= 0 || !source.hasInput || frame.PTS < 0 || frame.PTS > source.inputPTS {
|
|
return errors.New("encoded output has no matching source input")
|
|
}
|
|
if len(source.formats) != 2 {
|
|
return source.media.WriteFrame(layer, frame)
|
|
}
|
|
if layer == 1 {
|
|
if err := source.media.WriteFrame(1, frame); err != nil {
|
|
return err
|
|
}
|
|
for slot, group := range source.groups {
|
|
if slot != 0 && group.active && frame.PTS >= group.start {
|
|
if err := group.media.WriteFrame(1, frame); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
if frame.Recovery {
|
|
source.installGroup(nil, true)
|
|
}
|
|
return nil
|
|
}
|
|
group := source.groups[layer]
|
|
if group == nil {
|
|
if layer == 0 {
|
|
return source.media.WriteFrame(0, frame)
|
|
}
|
|
return nil
|
|
}
|
|
if !group.active || frame.PTS < group.start {
|
|
return nil
|
|
}
|
|
if err := group.media.WriteFrame(0, frame); err != nil {
|
|
return err
|
|
}
|
|
if frame.Recovery {
|
|
source.installGroup(group, false)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// WriteRTP preserves the received representation. Optional lower derivation is
|
|
// fed without waiting for its decoder, encoder, or process pipe.
|
|
func (source *Source) WriteRTP(packet *rtp.Packet) error {
|
|
if packet == nil {
|
|
return errors.New("native video RTP packet is invalid")
|
|
}
|
|
source.writeMu.Lock()
|
|
source.mu.Lock()
|
|
if source.closed {
|
|
source.mu.Unlock()
|
|
source.writeMu.Unlock()
|
|
return io.ErrClosedPipe
|
|
}
|
|
source.mu.Unlock()
|
|
layer := len(source.formats) - 1
|
|
recovery := false
|
|
if source.relay != nil {
|
|
var size mediacodec.VideoSize
|
|
if source.codec == "h264" {
|
|
size = mediacodec.ExtractH264VideoSize(packet.Payload)
|
|
} else {
|
|
var header mediacodec.VP8
|
|
if header.Unmarshal(packet.Payload) == nil {
|
|
size = mediacodec.ExtractVP8VideoSize(&header, packet.Payload)
|
|
}
|
|
}
|
|
if size.Width > 0 && size.Height > 0 {
|
|
recovery = true
|
|
// Actual input dimensions do not replace the Host's profile or reset
|
|
// forwarding history. Derived dimensions belong to their own output.
|
|
if err := source.setFormat(layer, size.Width, size.Height); err != nil {
|
|
source.writeMu.Unlock()
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
forwarded := connectionNeutralRTP(packet)
|
|
forwarded.SSRC = uint32(layer + 1)
|
|
forwarded.PayloadType = uint8(videoCodecs[source.codec].PayloadType)
|
|
err := source.media.Source.WriteRTP(layer, &forwarded)
|
|
for slot, group := range source.groups {
|
|
if slot != 0 && group.active {
|
|
groupErr := group.media.Source.WriteRTP(layer, &forwarded)
|
|
if groupErr == nil && group.clockPending {
|
|
groupErr = group.media.CopyCorrelation(layer, source.media.Source)
|
|
group.clockPending = false
|
|
}
|
|
if err == nil {
|
|
err = groupErr
|
|
}
|
|
}
|
|
}
|
|
if recovery && err == nil {
|
|
source.installGroup(nil, true)
|
|
}
|
|
var unavailable []*Edge
|
|
if packet.Marker {
|
|
source.mu.Lock()
|
|
for edge := range source.edges {
|
|
if edge.State() == webrtc.PeerConnectionStateConnected && source.unavailableDemand(edge) {
|
|
unavailable = append(unavailable, edge)
|
|
}
|
|
}
|
|
source.mu.Unlock()
|
|
}
|
|
source.writeMu.Unlock()
|
|
closeEdges(unavailable)
|
|
if source.relay != nil {
|
|
source.relay.push(packet)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Called with writeMu held. Preparation is advisory; the source owns whether
|
|
// that requested codec output still exists after a producer failure.
|
|
func (source *Source) unavailableDemand(edge *Edge) bool {
|
|
state := edge.transport.Output.State()
|
|
selected := state.Target
|
|
if !state.Paused {
|
|
selected = max(selected, state.Current)
|
|
}
|
|
physical := selected
|
|
if selected == 0 {
|
|
if group := source.groupForMedia(edge.transport.CurrentSource()); group != nil {
|
|
physical = int32(group.slot)
|
|
}
|
|
}
|
|
if selected >= 0 && !source.failedOutput(physical) {
|
|
return false
|
|
}
|
|
fallback := int32(edge.transport.RequiredActiveCount() - 1)
|
|
if fallback == 0 {
|
|
if group := source.groupForMedia(edge.transport.CurrentSource()); group != nil {
|
|
fallback = int32(group.slot)
|
|
}
|
|
}
|
|
return source.failedOutput(fallback)
|
|
}
|
|
|
|
func (source *Source) failedOutput(layer int32) bool {
|
|
return layer >= 0 && int(layer) < len(source.outputBitrates) && source.outputBitrates[layer] == 0
|
|
}
|
|
|
|
func (source *Source) SenderReport(report *rtcp.SenderReport) error {
|
|
source.writeMu.Lock()
|
|
defer source.writeMu.Unlock()
|
|
correlated := *report
|
|
layer := len(source.formats) - 1
|
|
correlated.SSRC = uint32(layer + 1)
|
|
err := source.media.SenderReport(layer, &correlated)
|
|
for slot, group := range source.groups {
|
|
if slot != 0 && group.active {
|
|
if groupErr := group.media.CopyCorrelation(layer, source.media.Source); err == nil {
|
|
err = groupErr
|
|
}
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
func connectionNeutralRTP(packet *rtp.Packet) rtp.Packet {
|
|
forwarded := *packet
|
|
forwarded.Header = packet.Header
|
|
// Header extensions are negotiated per PeerConnection. The outbound Pion
|
|
// interceptors add fresh TWCC using that edge's negotiated ID.
|
|
forwarded.Extension = false
|
|
forwarded.ExtensionProfile = 0
|
|
forwarded.Extensions = nil
|
|
return forwarded
|
|
}
|
|
|
|
func (source *Source) BeginGeneration() {
|
|
source.writeMu.Lock()
|
|
defer source.writeMu.Unlock()
|
|
source.media.BeginGeneration()
|
|
source.hasInput = false
|
|
for slot, group := range source.groups {
|
|
group.start = 0
|
|
if slot != 0 {
|
|
group.media.BeginGeneration()
|
|
}
|
|
}
|
|
}
|
|
|
|
func (source *Source) SetFormat(layer int, width, height uint32) error {
|
|
source.writeMu.Lock()
|
|
defer source.writeMu.Unlock()
|
|
return source.setFormat(layer, width, height)
|
|
}
|
|
|
|
func (source *Source) setFormat(layer int, width, height uint32) error {
|
|
if layer < 0 || layer >= max(len(source.formats), len(source.outputBitrates)) || width == 0 || height == 0 {
|
|
return errors.New("native output format is invalid")
|
|
}
|
|
source.mu.Lock()
|
|
defer source.mu.Unlock()
|
|
if source.closed {
|
|
return io.ErrClosedPipe
|
|
}
|
|
format := uint64(width)<<32 | uint64(height)
|
|
if layer >= len(source.formats) {
|
|
if group := source.groups[layer]; group != nil {
|
|
return group.media.UpdateDimensions(0, width, height)
|
|
}
|
|
return nil
|
|
}
|
|
if err := source.media.UpdateDimensions(layer, width, height); err != nil {
|
|
return err
|
|
}
|
|
source.formats[layer].Store(format)
|
|
if layer == 1 {
|
|
for slot, group := range source.groups {
|
|
if slot != 0 {
|
|
if err := group.media.UpdateDimensions(1, width, height); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (source *Source) reserve(local bool) error {
|
|
source.mu.Lock()
|
|
defer source.mu.Unlock()
|
|
if source.closed {
|
|
return errors.New("native media source is closed")
|
|
}
|
|
if local {
|
|
if source.localReservation {
|
|
return ErrSourceCapacity
|
|
}
|
|
for _, existingLocal := range source.edges {
|
|
if existingLocal {
|
|
return ErrSourceCapacity
|
|
}
|
|
}
|
|
source.localReservation = true
|
|
return nil
|
|
}
|
|
routeEdges := 0
|
|
for _, existingLocal := range source.edges {
|
|
if !existingLocal {
|
|
routeEdges++
|
|
}
|
|
}
|
|
if routeEdges+source.reservations >= source.capacity {
|
|
return ErrSourceCapacity
|
|
}
|
|
source.reservations++
|
|
return nil
|
|
}
|
|
|
|
func (source *Source) attach(edge *Edge, local bool) error {
|
|
source.writeMu.Lock()
|
|
defer source.writeMu.Unlock()
|
|
source.mu.Lock()
|
|
defer source.mu.Unlock()
|
|
if local {
|
|
source.localReservation = false
|
|
} else if source.reservations > 0 {
|
|
source.reservations--
|
|
}
|
|
if source.closed {
|
|
return errors.New("native media source is closed")
|
|
}
|
|
if len(source.outputBitrates) > 0 {
|
|
highest := len(source.formats) - 1
|
|
for highest >= 0 && source.outputBitrates[highest] == 0 {
|
|
highest--
|
|
}
|
|
if highest < 0 {
|
|
return errors.New("native media source has no available output")
|
|
}
|
|
edge.transport.Output.SetMaxSpatialLayer(int32(highest))
|
|
}
|
|
source.edges[edge] = local
|
|
return nil
|
|
}
|
|
|
|
func (source *Source) releaseReservation(local bool) {
|
|
source.mu.Lock()
|
|
if local {
|
|
source.localReservation = false
|
|
} else if source.reservations > 0 {
|
|
source.reservations--
|
|
}
|
|
source.mu.Unlock()
|
|
}
|
|
|
|
func (source *Source) detach(edge *Edge) {
|
|
source.writeMu.Lock()
|
|
source.mu.Lock()
|
|
delete(source.edges, edge)
|
|
source.mu.Unlock()
|
|
source.writeMu.Unlock()
|
|
if source.relay != nil && !source.relayPlan().wanted {
|
|
source.relay.cancel()
|
|
}
|
|
source.RequestRecoveryFrame()
|
|
}
|
|
|
|
func (source *Source) requestLayerKeyFrame(layer int) {
|
|
// Direct capture receives this request through its callback, including while
|
|
// quiet. A relay's callback only wakes upstream; its local encoders still
|
|
// need recovery in the next frame's plan, as do callback-free producers.
|
|
if source.relay != nil || source.requestKeyFrame == nil {
|
|
source.groupRecovery.Or(uint32(1) << layer)
|
|
}
|
|
source.requestRecovery(layer)
|
|
}
|
|
|
|
func (source *Source) RequestRecoveryFrame() {
|
|
source.requestRecovery(-1)
|
|
}
|
|
|
|
func (source *Source) requestRecovery(layer int) {
|
|
if source.requestKeyFrame != nil {
|
|
mask := allRecoveryLayers
|
|
if layer >= 0 {
|
|
mask = uint32(1) << layer
|
|
}
|
|
// Coalesce notifications without dropping another output's request.
|
|
// groupRecovery separately belongs to the next input's output plan.
|
|
source.pendingRecovery.Or(mask)
|
|
select {
|
|
case source.recoveryRequests <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
}
|
|
|
|
// One coalescing notification owner per source, never per encoder or frame.
|
|
// Producer I/O cannot reenter a source lock from a forwarding callback.
|
|
func (source *Source) runRecovery() {
|
|
var engineDone <-chan struct{}
|
|
if source.engine.ctx != nil {
|
|
engineDone = source.engine.ctx.Done()
|
|
}
|
|
for {
|
|
select {
|
|
case <-engineDone:
|
|
return
|
|
case <-source.recoveryStopped:
|
|
return
|
|
case <-source.recoveryRequests:
|
|
source.mu.Lock()
|
|
closed := source.closed
|
|
source.mu.Unlock()
|
|
if !closed {
|
|
mask := source.pendingRecovery.Swap(0)
|
|
if mask == 0 {
|
|
continue
|
|
}
|
|
layers := []int{-1}
|
|
if mask != allRecoveryLayers {
|
|
layers = nil
|
|
for layer := 0; mask != 0; mask, layer = mask>>1, layer+1 {
|
|
if mask&1 != 0 {
|
|
layers = append(layers, layer)
|
|
}
|
|
}
|
|
}
|
|
source.requestKeyFrame(layers)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (source *Source) Close() error {
|
|
source.mu.Lock()
|
|
if source.closed {
|
|
source.mu.Unlock()
|
|
return nil
|
|
}
|
|
source.closed = true
|
|
if source.recoveryStopped != nil {
|
|
close(source.recoveryStopped)
|
|
}
|
|
publications := make([]*Publication, 0, len(source.publications))
|
|
for publication := range source.publications {
|
|
publications = append(publications, publication)
|
|
}
|
|
edges := make([]*Edge, 0, len(source.edges))
|
|
for edge := range source.edges {
|
|
edges = append(edges, edge)
|
|
}
|
|
source.mu.Unlock()
|
|
// Close transports before waiting on their possible writes into this group.
|
|
source.engine.closeSourcePunch(source)
|
|
closeEdges(edges)
|
|
for _, publication := range publications {
|
|
_ = publication.Close()
|
|
}
|
|
if source.relay != nil {
|
|
source.relay.close()
|
|
}
|
|
source.writeMu.Lock()
|
|
for slot, group := range source.groups {
|
|
if slot != 0 {
|
|
group.media.Close()
|
|
}
|
|
}
|
|
source.media.Close()
|
|
source.writeMu.Unlock()
|
|
return nil
|
|
}
|