## Problem and outcome / 问题与结果 This phase adds lightweight room interaction without making media depend on chat, and adds Windows source-audio exclusion and optional SFU-only hosting at their existing owners. It also corrects confirmed encoder/capture ownership and native color-conversion defects. Host microphone commentary remains on the shared media path; room voice and 4K are outside this phase. ## Release notes ### 中文 这是一次功能更新:Piik 加入了文字聊天与表情互动,并完善了 Windows 声音采集、画面颜色和自部署选项。 #### 新功能 - **边看边聊。** 在可移动、可缩放的聊天窗口中发送消息,或开启弹幕。停止分享后,房间中的朋友仍可继续聊天。 - **表情互动。** 点击沙发上的人物,给自己发表情,或把番茄等表情送给朋友;各端按同一事件显示动画。 - **排除应用声音。** Windows App 分享整个屏幕时,可排除一个应用及其子进程的声音,适合同时使用语音软件。所选应用退出后,来源声音停止,避免恢复为未过滤的系统声音。 - **可选的纯 SFU 模式。** 自部署管理员可让共享画面和声音统一通过媒体服务器转发。默认仍为优先 P2P;App 公网邀请保持纯 P2P。 - **Windows HDR 转 SDR。** 原生采集与缩略图自动将 HDR 来源转换为 SDR,沿用现有观看链路。 #### 修复与改进 - 修复原生颜色转换的范围与矩阵处理,避免已复现的暗部丢失、高光裁切和颜色偏差。 - 修复共享编码器切换时使用过期需求或过期帧信息的问题,保护正在观看的画面;修复换源后旧采集事件可能影响新来源的问题。 - 统一新控件、浮窗与来源选择文案,补充中英文使用说明。 #### 使用与升级 聊天与表情需要新版站点页面和 Server;声音排除需要新版页面与支持此能力的 Windows App。聊天只保留本页最近收到的消息,刷新后清空,不补发历史,且不提供端到端加密。 HDR 转换输出为 SDR,不代表端到端 HDR 或多声道支持。既有房主麦克风与立体声能力保留。 ### English This feature update brings text chat and reactions to Piik, alongside improvements to Windows audio capture, picture colors and self-hosting options. #### New features - **Chat while watching.** Send messages in a movable, resizable window, or enable on-screen chat. Friends can keep chatting after the host stops sharing. - **Reactions.** Select a person on the sofa to react above yourself or send a tomato and other reactions to a friend. Participants animate the same shared event. - **Exclude application audio.** When sharing a whole screen with Windows App, exclude one application and its child processes, useful when using a separate voice app. Source audio stops if the selected app exits, preventing a return to unfiltered system sound. - **Optional SFU-only hosting.** Site operators can require shared video and audio to pass through the media server. P2P-first remains the default; App Public invite remains P2P-only. - **Windows HDR-to-SDR conversion.** Native capture and source thumbnails automatically convert HDR sources to SDR for the existing viewing path. #### Fixes and improvements - Correct native color-conversion ranges and matrices, addressing reproduced crushed shadows, clipped highlights and color shifts. - Correct stale demand and frame evidence during shared-encoder handoffs to protect the current picture. Prevent events from a replaced capture from affecting its successor. - Align new controls, floating panels and source-picker copy, with updated Chinese and English guides. #### Using this update Chat and reactions require updated site pages and Server. Audio exclusion requires updated pages and a Windows App that supports the capability. Chat retains only recent messages received by the current page, clears on refresh and does not replay history. It is not end-to-end encrypted. HDR conversion produces SDR; it does not add end-to-end HDR or surround sound. Existing host microphone commentary and stereo support remain available. ## Verification / 验证 - Web type checks, 78 test files / 1275 tests and production build: PASS. - Go core tests, formatting/vet and target builds: PASS; Darwin cgo remains platform-limited on this Windows runner. - Native build/check/color probes and synthetic HDR-to-SDR H.264/VP8 browser reception: PASS. - Access/privacy gate, Native H.264 SFU-only media gate and local Auto/H.264 Host gate: PASS. - Three real production pages: chat/reactions, stop/resume, idle-room join, host departure/return, offline draft recovery and invitation revocation: PASS. - Synthetic parent/child process-audio exclusion and target lifetime: PASS; no raw audio retained. - Windows candidate archive extraction and packaged Local startup/shutdown: PASS. - Documentation and website build: PASS. - [Candidate CI](https://github.com/TNTcraftHIM/Piik/actions/runs/36873766646) at `f26cd208d03bc0dcbe353f8cac217f993216b0a3`: Go core and race checks, Web tests, Server/container checks and Windows/macOS/Linux App packaging all PASS. Publication was skipped as intended. The owner accepted the UI/model and bounded local walkthrough for phase closure. Physical HDR/mixed displays, mobile keyboard, real voice applications across output devices and macOS/Linux capture remain unverified. Existing field reports without matched evidence are not claimed resolved. The owner authorized v1.7.0 publication on 2026-10-02. The final documentation-only commit records that authorization; product code matches the verified candidate above. ## Impact / 影响 Authorized version: **v1.7.0**, matching the local release planner. Current wire contracts remain Browser/server 23, Native control 9 and capture 7. Optional capabilities gate new functionality; existing room/media authority remains owned by the existing services. No new ports, message persistence, room voice or automatic room creation on page visit. Updated README, guides, standards, research evidence and TODO stay with the candidate phase.
715 lines
20 KiB
Go
715 lines
20 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 }
|
|
|
|
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(true)
|
|
}
|
|
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
|
|
for _, edge := range edges {
|
|
if edge.State() == webrtc.PeerConnectionStateConnected {
|
|
count = max(count, edge.transport.RequiredActiveCount())
|
|
}
|
|
}
|
|
for _, publication := range publications {
|
|
count = max(count, publication.RequiredActiveCount())
|
|
}
|
|
for slot := 0; slot < min(count, len(plan.Active)); slot++ {
|
|
plan.Active[slot] = true
|
|
}
|
|
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.capacity ||
|
|
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
|
|
format := uint64(size.Width)<<32 | uint64(size.Height)
|
|
if source.formats[layer].Swap(format) != format {
|
|
_ = source.configureRelayFormats(false)
|
|
}
|
|
}
|
|
}
|
|
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 {
|
|
if groupErr := group.media.Source.WriteRTP(layer, &forwarded); 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.SenderReport(layer, &correlated); 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.
|
|
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
|
|
}
|