## 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.
680 lines
23 KiB
Go
680 lines
23 KiB
Go
package forwarding
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"strconv"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/TNTcraftHIM/Piik/internal/diagnostics"
|
|
"github.com/TNTcraftHIM/Piik/internal/thirdparty/livekit/sendsidebwe"
|
|
"github.com/livekit/livekit-server/pkg/sfu"
|
|
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
|
"github.com/livekit/livekit-server/pkg/sfu/bwe"
|
|
"github.com/livekit/livekit-server/pkg/sfu/ccutils"
|
|
"github.com/livekit/livekit-server/pkg/sfu/pacer"
|
|
"github.com/livekit/livekit-server/pkg/sfu/streamallocator"
|
|
"github.com/livekit/protocol/livekit"
|
|
"github.com/pion/interceptor"
|
|
"github.com/pion/rtcp"
|
|
"github.com/pion/rtp"
|
|
"github.com/pion/webrtc/v4"
|
|
)
|
|
|
|
const publicationRIDExtension = "urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id"
|
|
const publicationRepairRIDExtension = "urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id"
|
|
|
|
// Publication sends existing representations over one simulcast connection.
|
|
// LiveKit's allocator owns all track allocations and probes on the shared BWE.
|
|
type Publication struct {
|
|
PC *webrtc.PeerConnection
|
|
source *Source
|
|
tracks []*sfu.DownTrack
|
|
counters []publicationCounters
|
|
audioBytes atomic.Uint64
|
|
allocator *streamallocator.StreamAllocator
|
|
bandwidth *publicationBWE
|
|
pacer *publicationPacer
|
|
mu sync.Mutex
|
|
writeMu sync.Mutex
|
|
attached bool
|
|
closed bool
|
|
activeCount int
|
|
readers sync.WaitGroup
|
|
closeOnce sync.Once
|
|
closeErr error
|
|
}
|
|
|
|
func NewPublication(options TransportOptions) (_ *Publication, err error) {
|
|
if options.Source == nil || options.ConnectionID == "" || options.InitialBitrate <= 0 {
|
|
return nil, errors.New("publication options are invalid")
|
|
}
|
|
publication := &Publication{source: options.Source, activeCount: len(options.Source.TrackInfo().Layers)}
|
|
publication.counters = make([]publicationCounters, publication.activeCount)
|
|
defer func() {
|
|
if err != nil {
|
|
_ = publication.Close()
|
|
}
|
|
}()
|
|
log := diagnostics.MediaLogger("publication").WithValues("connectionId", diagnostics.ID(options.ConnectionID))
|
|
publication.bandwidth = &publicationBWE{SendSideBWE: sendsidebwe.NewSendSideBWE(sendsidebwe.SendSideBWEParams{
|
|
Config: sendsidebwe.DefaultSendSideBWEConfig, Logger: log,
|
|
})}
|
|
publication.bandwidth.target.Store(int64(options.InitialBitrate))
|
|
publication.pacer = &publicationPacer{headers: make(map[uint32]publicationHeaders), boundedPacer: newPacedQueue(
|
|
log, options.Source.maxPackets, options.InitialBitrate,
|
|
)}
|
|
publication.bandwidth.pacer = publication.pacer.boundedPacer
|
|
publication.pacer.sender = pacer.NewBase(log, publication.bandwidth)
|
|
publication.allocator = streamallocator.NewStreamAllocator(streamallocator.StreamAllocatorParams{
|
|
Config: streamallocator.DefaultStreamAllocatorConfig, BWE: publication.bandwidth,
|
|
AdditionalProbeDemand: publication.encoderProbeDemand,
|
|
Pacer: publication.pacer, Logger: log,
|
|
}, true, true)
|
|
publication.allocator.Start()
|
|
media := &webrtc.MediaEngine{}
|
|
codec := options.Source.Codec()
|
|
if err = registerVideoCodec(media, codec); err != nil {
|
|
return nil, err
|
|
}
|
|
for _, uri := range []string{
|
|
"http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01",
|
|
"urn:ietf:params:rtp-hdrext:sdes:mid", publicationRIDExtension, publicationRepairRIDExtension,
|
|
} {
|
|
if err = media.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: uri}, webrtc.RTPCodecTypeVideo); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if options.Audio != nil {
|
|
audio, ok := options.Audio.(interface {
|
|
Codec() webrtc.RTPCodecCapability
|
|
})
|
|
if !ok {
|
|
return nil, errors.New("publication audio codec is unavailable")
|
|
}
|
|
if err = media.RegisterCodec(webrtc.RTPCodecParameters{RTPCodecCapability: audio.Codec(), PayloadType: 111}, webrtc.RTPCodecTypeAudio); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
factory := buffer.NewFactoryOfBufferFactory(options.Source.maxPackets, buffer.InitPacketBufferSizeAudio).CreateBufferFactory()
|
|
settings := options.Settings
|
|
if slog.Default().Enabled(context.Background(), slog.LevelDebug) {
|
|
settings.LoggerFactory = diagnostics.PionLoggerFactory("connectionId", diagnostics.ID(options.ConnectionID))
|
|
}
|
|
settings.BufferFactory = packetBufferFactory(factory)
|
|
registry := &interceptor.Registry{}
|
|
if err = configureRTCPReports(registry, func(ssrc uint32) *sfu.DownTrack {
|
|
for _, track := range publication.tracks {
|
|
if track.SSRC() == ssrc {
|
|
return track
|
|
}
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
publication.PC, err = webrtc.NewAPI(webrtc.WithMediaEngine(media), webrtc.WithInterceptorRegistry(registry),
|
|
webrtc.WithSettingEngine(settings)).NewPeerConnection(webrtc.Configuration{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var transceiver *webrtc.RTPTransceiver
|
|
// Pion's diagnostic PC.ID is clock-based and can repeat. Source membership
|
|
// needs a unique physical owner even when signaling reuses a connection ID.
|
|
subscriberID := rand.Text()
|
|
for index := range options.Source.TrackInfo().Layers {
|
|
receiver := &publicationLayer{Source: options.Source, layer: int32(index), publication: publication}
|
|
track, trackErr := sfu.NewDownTrack(sfu.DownTrackParams{
|
|
Codecs: []webrtc.RTPCodecParameters{codec}, Source: livekit.TrackSource_SCREEN_SHARE,
|
|
Receiver: receiver, BufferFactory: factory,
|
|
SubID: livekit.ParticipantID(subscriberID + "_" + strconv.Itoa(index)),
|
|
StreamID: options.Source.StreamID(), MaxTrack: options.Source.maxPackets,
|
|
Pacer: publication.pacer, Logger: log, Listener: publication,
|
|
RTCPWriter: publication.PC.WriteRTCP, DisableSenderReportPassThrough: true,
|
|
EnableStartAtDesiredQuality: true,
|
|
})
|
|
if trackErr != nil {
|
|
return nil, trackErr
|
|
}
|
|
publication.tracks = append(publication.tracks, track)
|
|
track.SetMaxSpatialLayer(int32(index))
|
|
local := &publicationTrack{DownTrack: track, id: options.Source.TrackID(), rid: PublicationRID(index, publication.activeCount), pacer: publication.pacer, counters: &publication.counters[index]}
|
|
if transceiver == nil {
|
|
transceiver, err = publication.PC.AddTransceiverFromTrack(local, webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionSendonly})
|
|
} else {
|
|
err = transceiver.Sender().AddEncoding(local)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
local.transceiver = transceiver
|
|
track.SetTransceiver(transceiver)
|
|
publication.allocator.AddTrack(track, streamallocator.AddTrackParams{
|
|
Source: livekit.TrackSource_SCREEN_SHARE, IsMultiLayered: true,
|
|
PublisherID: livekit.ParticipantID(options.Source.StreamID()),
|
|
})
|
|
}
|
|
if options.Audio != nil {
|
|
sender, audioErr := publication.PC.AddTrack(&publicationAudio{TrackLocal: options.Audio, bytes: &publication.audioBytes})
|
|
if audioErr != nil {
|
|
return nil, audioErr
|
|
}
|
|
publication.readers.Add(1)
|
|
go func() {
|
|
defer publication.readers.Done()
|
|
for {
|
|
if _, _, readErr := sender.ReadRTCP(); readErr != nil {
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
return publication, nil
|
|
}
|
|
|
|
func PublicationRID(layer, count int) string {
|
|
if count == 1 {
|
|
return ""
|
|
}
|
|
return []string{"q", "h", "f"}[layer]
|
|
}
|
|
|
|
func (publication *Publication) SetConnected() error {
|
|
publication.mu.Lock()
|
|
defer publication.mu.Unlock()
|
|
if publication.closed {
|
|
return io.ErrClosedPipe
|
|
}
|
|
if publication.attached {
|
|
return nil
|
|
}
|
|
for _, track := range publication.tracks {
|
|
receiver := track.Receiver().(*publicationLayer)
|
|
receiver.attached = true
|
|
if err := receiver.AddDownTrack(track); err != nil {
|
|
receiver.attached = false
|
|
return err
|
|
}
|
|
track.SetConnected()
|
|
}
|
|
publication.attached = true
|
|
return nil
|
|
}
|
|
|
|
func (publication *Publication) CurrentSource() *Source {
|
|
publication.mu.Lock()
|
|
defer publication.mu.Unlock()
|
|
return publication.source
|
|
}
|
|
|
|
// ReplaceSource keeps every RID, allocator, counter and sender on this PC.
|
|
func (publication *Publication) ReplaceSource(next *Source) error {
|
|
recovery := false
|
|
// Source feedback may retire this publication; release all locks first.
|
|
defer func() {
|
|
if recovery {
|
|
for layer := range next.TrackInfo().Layers {
|
|
next.SendPLI(int32(layer), true)
|
|
}
|
|
}
|
|
}()
|
|
publication.mu.Lock()
|
|
defer publication.mu.Unlock()
|
|
if publication.closed {
|
|
return io.ErrClosedPipe
|
|
}
|
|
if next == nil {
|
|
return errors.New("replacement publication source is absent")
|
|
}
|
|
next.mu.Lock()
|
|
defer next.mu.Unlock()
|
|
if err := validateReplacement(publication.source, next); err != nil {
|
|
return err
|
|
}
|
|
if next == publication.source {
|
|
return nil
|
|
}
|
|
publication.writeMu.Lock()
|
|
defer publication.writeMu.Unlock()
|
|
for _, track := range publication.tracks {
|
|
if track.IsClosed() {
|
|
return io.ErrClosedPipe
|
|
}
|
|
}
|
|
for index, track := range publication.tracks {
|
|
receiver := &publicationLayer{Source: next, layer: int32(index), publication: publication, attached: publication.attached}
|
|
track.ReceiverRestart(track.Receiver())
|
|
track.SetReceiver(receiver)
|
|
// The subscribed max layer did not change, so SetReceiver's layer
|
|
// notification alone does not wake the allocator for this fresh input.
|
|
publication.allocator.OnSubscriptionChanged(track)
|
|
}
|
|
publication.source = next
|
|
recovery = publication.attached
|
|
return nil
|
|
}
|
|
|
|
func (publication *Publication) SetActiveCount(count int) error {
|
|
publication.mu.Lock()
|
|
defer publication.mu.Unlock()
|
|
if publication.closed {
|
|
return io.ErrClosedPipe
|
|
}
|
|
if count < 0 || count > len(publication.tracks) {
|
|
return errors.New("publication layer demand is invalid")
|
|
}
|
|
publication.activeCount = count
|
|
for index, track := range publication.tracks {
|
|
// Publisher demand is authoritative even while allocation is paused;
|
|
// subscription visibility mute may be ignored by the framework then.
|
|
track.PubMute(index >= count)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (publication *Publication) RequiredActiveCount() int {
|
|
publication.mu.Lock()
|
|
defer publication.mu.Unlock()
|
|
if publication.closed || !publication.attached {
|
|
return 0
|
|
}
|
|
// The allocator still needs actual input for requested encodings when it
|
|
// probes recovery. This is codec demand, not affordable or delivered upload.
|
|
return publication.activeCount
|
|
}
|
|
|
|
func (publication *Publication) VideoBudget() int64 {
|
|
return publication.bandwidth.target.Load()
|
|
}
|
|
func (publication *Publication) LowestLayerBudget() int64 {
|
|
remaining := publication.VideoBudget()
|
|
for _, track := range publication.tracks[1:] {
|
|
remaining -= track.BandwidthRequested()
|
|
}
|
|
return max(remaining, 0)
|
|
}
|
|
|
|
// A local encoder can need more bandwidth without another spatial layer.
|
|
// Keep desired output separate from measured source bitrate and capacity;
|
|
// the allocator remains the only owner of probe timing and recovery.
|
|
func (publication *Publication) encoderProbeDemand() int64 {
|
|
publication.mu.Lock()
|
|
defer publication.mu.Unlock()
|
|
if publication.closed || !publication.attached || publication.activeCount == 0 {
|
|
return 0
|
|
}
|
|
ceiling := controlledLayerCeiling(publication.source, 0)
|
|
if ceiling <= publication.LowestLayerBudget() {
|
|
return 0
|
|
}
|
|
return max(ceiling-publication.tracks[0].BandwidthRequested(), 0)
|
|
}
|
|
|
|
type PublicationCounters struct {
|
|
Frames []uint64
|
|
VideoBytes, AudioBytes uint64
|
|
TargetBitrate int64
|
|
Observed, Limited bool
|
|
}
|
|
|
|
func (publication *Publication) Counters() PublicationCounters {
|
|
publication.mu.Lock()
|
|
defer publication.mu.Unlock()
|
|
stats := PublicationCounters{Frames: make([]uint64, len(publication.counters)),
|
|
AudioBytes: publication.audioBytes.Load(), TargetBitrate: publication.bandwidth.target.Load(),
|
|
Observed: publication.bandwidth.observed.Load(), Limited: !publication.bandwidth.probing.Load() &&
|
|
publication.bandwidth.CongestionState() == bwe.CongestionStateCongested}
|
|
if publication.activeCount > 0 && controlledLayerCeiling(publication.source, 0) > publication.LowestLayerBudget() {
|
|
stats.Limited = true
|
|
}
|
|
for index := range publication.counters {
|
|
stats.Frames[index] = publication.counters[index].frames.Load()
|
|
stats.VideoBytes += publication.counters[index].bytes.Load()
|
|
if index < publication.activeCount && publication.tracks[index].IsDeficient() {
|
|
stats.Limited = true
|
|
}
|
|
}
|
|
return stats
|
|
}
|
|
|
|
func (publication *Publication) Close() error {
|
|
publication.closeOnce.Do(func() {
|
|
publication.mu.Lock()
|
|
publication.closed = true
|
|
publication.mu.Unlock()
|
|
if publication.PC != nil {
|
|
publication.closeErr = publication.PC.Close()
|
|
}
|
|
if publication.allocator != nil {
|
|
publication.allocator.Stop()
|
|
}
|
|
for _, track := range publication.tracks {
|
|
publication.source.DeleteDownTrack(track.SubscriberID())
|
|
track.Close()
|
|
}
|
|
if publication.pacer != nil {
|
|
publication.pacer.Stop()
|
|
}
|
|
publication.readers.Wait()
|
|
})
|
|
return publication.closeErr
|
|
}
|
|
|
|
func (*Publication) OnBindAndConnected() {}
|
|
func (publication *Publication) OnStatsUpdate(stats *livekit.AnalyticsStat) {
|
|
if slog.Default().Enabled(context.Background(), slog.LevelDebug) {
|
|
slog.Debug("media-stats", "scope", "publication", "rtcPeerId", diagnostics.ID(publication.PC.ID()),
|
|
"rtp", stats, "egress", publication.pacer.stats(), "targetBitrate", publication.bandwidth.target.Load(),
|
|
"pendingPackets", publication.pacer.pendingCount(), "packetCapacity", publication.pacer.limit)
|
|
}
|
|
}
|
|
func (*Publication) OnMaxSubscribedLayerChanged(int32) {}
|
|
func (publication *Publication) OnRttUpdate(rtt uint32) {
|
|
publication.bandwidth.UpdateRTT(float64(rtt) / 1000)
|
|
}
|
|
func (*Publication) OnCodecNegotiated(webrtc.RTPCodecCapability) {}
|
|
func (*Publication) OnDownTrackClose(bool) {}
|
|
func (*Publication) OnStreamStarted(time.Duration) {}
|
|
|
|
// Each RID exposes precisely one existing layer to the allocator; another RID
|
|
// can never substitute the same lower output and charge it twice.
|
|
type publicationLayer struct {
|
|
*Source
|
|
layer int32
|
|
publication *Publication
|
|
attached bool
|
|
}
|
|
|
|
func (layer *publicationLayer) AddDownTrack(sender sfu.TrackSender) error {
|
|
if !layer.attached {
|
|
return nil
|
|
}
|
|
filtered := &publicationLayerSender{
|
|
DownTrack: sender.(*sfu.DownTrack), layer: layer.layer, receiver: layer,
|
|
}
|
|
filtered.registering.Store(true)
|
|
err := layer.Source.AddDownTrack(filtered)
|
|
filtered.reportMu.Lock()
|
|
filtered.registering.Store(false)
|
|
filtered.reportMu.Unlock()
|
|
return err
|
|
}
|
|
|
|
func (layer *publicationLayer) TrackID() livekit.TrackID {
|
|
return livekit.TrackID(string(layer.Source.TrackID()) + "_" + strconv.Itoa(int(layer.layer)))
|
|
}
|
|
func (layer *publicationLayer) GetLayeredBitrate() ([]int32, sfu.Bitrates) {
|
|
available, rates := layer.Source.GetLayeredBitrate()
|
|
return onlyPublicationLayer(layer.layer, available, rates)
|
|
}
|
|
func (*publicationLayer) SetMaxExpectedSpatialLayer(int32) {}
|
|
|
|
func onlyPublicationLayer(layer int32, available []int32, rates sfu.Bitrates) ([]int32, sfu.Bitrates) {
|
|
var selected sfu.Bitrates
|
|
selected[layer] = rates[layer]
|
|
for _, index := range available {
|
|
if index == layer {
|
|
return []int32{layer}, selected
|
|
}
|
|
}
|
|
return nil, selected
|
|
}
|
|
|
|
type publicationLayerSender struct {
|
|
*sfu.DownTrack
|
|
layer int32
|
|
receiver *publicationLayer
|
|
registering atomic.Bool
|
|
reportMu sync.Mutex
|
|
}
|
|
|
|
func (sender *publicationLayerSender) UpTrackBitrateReport(available []int32, rates sfu.Bitrates) {
|
|
sender.withReceiver(func() {
|
|
available, rates = onlyPublicationLayer(sender.layer, available, rates)
|
|
sender.DownTrack.UpTrackBitrateReport(available, rates)
|
|
})
|
|
}
|
|
func (sender *publicationLayerSender) WriteRTP(packet *buffer.ExtPacket, layer int32) int32 {
|
|
sender.receiver.publication.writeMu.Lock()
|
|
defer sender.receiver.publication.writeMu.Unlock()
|
|
if sender.Receiver() != sender.receiver {
|
|
return 0
|
|
}
|
|
if layer != sender.layer {
|
|
return 0
|
|
}
|
|
return sender.DownTrack.WriteRTP(packet, layer)
|
|
}
|
|
|
|
func (sender *publicationLayerSender) Close() {
|
|
sender.withReceiver(sender.DownTrack.Close)
|
|
}
|
|
|
|
func (sender *publicationLayerSender) withReceiver(fn func()) {
|
|
if !sender.registering.Load() {
|
|
sender.receiver.publication.writeMu.Lock()
|
|
defer sender.receiver.publication.writeMu.Unlock()
|
|
}
|
|
if sender.Receiver() == sender.receiver {
|
|
fn()
|
|
}
|
|
}
|
|
|
|
func (sender *publicationLayerSender) ReceiverRestart(receiver sfu.TrackReceiver) {
|
|
sender.withReceiver(func() { sender.DownTrack.ReceiverRestart(receiver) })
|
|
}
|
|
|
|
func (sender *publicationLayerSender) UpTrackLayersChange() {
|
|
sender.withReceiver(sender.DownTrack.UpTrackLayersChange)
|
|
}
|
|
|
|
func (sender *publicationLayerSender) UpTrackBitrateAvailabilityChange() {
|
|
sender.withReceiver(sender.DownTrack.UpTrackBitrateAvailabilityChange)
|
|
}
|
|
|
|
func (sender *publicationLayerSender) UpTrackMaxPublishedLayerChange(layer int32) {
|
|
sender.withReceiver(func() { sender.DownTrack.UpTrackMaxPublishedLayerChange(layer) })
|
|
}
|
|
|
|
func (sender *publicationLayerSender) UpTrackMaxTemporalLayerSeenChange(layer int32) {
|
|
sender.withReceiver(func() { sender.DownTrack.UpTrackMaxTemporalLayerSeenChange(layer) })
|
|
}
|
|
|
|
func (sender *publicationLayerSender) HandleRTCPSenderReportData(payloadType webrtc.PayloadType, layer int32, _ *livekit.RTCPSenderReportState) error {
|
|
apply := func() error {
|
|
if sender.Receiver() != sender.receiver {
|
|
return nil
|
|
}
|
|
current := sender.receiver.currentSenderReport(layer)
|
|
if current == nil {
|
|
return nil
|
|
}
|
|
return sender.DownTrack.HandleRTCPSenderReportData(payloadType, layer, current)
|
|
}
|
|
sender.reportMu.Lock()
|
|
if sender.registering.Load() {
|
|
defer sender.reportMu.Unlock()
|
|
return apply()
|
|
}
|
|
sender.reportMu.Unlock()
|
|
sender.receiver.publication.writeMu.Lock()
|
|
defer sender.receiver.publication.writeMu.Unlock()
|
|
return apply()
|
|
}
|
|
|
|
type publicationTrack struct {
|
|
*sfu.DownTrack
|
|
id livekit.TrackID
|
|
rid string
|
|
transceiver *webrtc.RTPTransceiver
|
|
pacer *publicationPacer
|
|
counters *publicationCounters
|
|
}
|
|
|
|
func (track *publicationTrack) ID() string { return string(track.id) }
|
|
func (track *publicationTrack) RID() string { return track.rid }
|
|
func (track *publicationTrack) Bind(context webrtc.TrackLocalContext) (webrtc.RTPCodecParameters, error) {
|
|
var extension, repairExtension, midExtension uint8
|
|
for _, header := range context.HeaderExtensions() {
|
|
if header.URI == publicationRIDExtension {
|
|
extension = uint8(header.ID)
|
|
}
|
|
if header.URI == publicationRepairRIDExtension {
|
|
repairExtension = uint8(header.ID)
|
|
}
|
|
if header.URI == "urn:ietf:params:rtp-hdrext:sdes:mid" {
|
|
midExtension = uint8(header.ID)
|
|
}
|
|
}
|
|
if track.rid != "" && extension == 0 {
|
|
return webrtc.RTPCodecParameters{}, errors.New("publication RID was not negotiated")
|
|
}
|
|
track.pacer.headersMu.Lock()
|
|
track.pacer.headers[uint32(context.SSRC())] = publicationHeaders{rid: track.rid, extension: extension,
|
|
mid: track.transceiver.Mid(), midExtension: midExtension, counters: track.counters}
|
|
if ssrc := context.SSRCRetransmission(); ssrc != 0 {
|
|
track.pacer.headers[uint32(ssrc)] = publicationHeaders{rid: track.rid, extension: repairExtension,
|
|
mid: track.transceiver.Mid(), midExtension: midExtension, counters: track.counters}
|
|
}
|
|
track.pacer.headersMu.Unlock()
|
|
return track.DownTrack.Bind(context)
|
|
}
|
|
|
|
type publicationHeaders struct {
|
|
rid, mid string
|
|
extension, midExtension uint8
|
|
counters *publicationCounters
|
|
}
|
|
|
|
type publicationCounters struct{ frames, bytes atomic.Uint64 }
|
|
|
|
type publicationWriter struct {
|
|
webrtc.TrackLocalWriter
|
|
bytes *atomic.Uint64
|
|
frames *atomic.Uint64
|
|
primary bool
|
|
}
|
|
|
|
func (writer *publicationWriter) WriteRTP(header *rtp.Header, payload []byte) (int, error) {
|
|
written, err := writer.TrackLocalWriter.WriteRTP(header, payload)
|
|
if err == nil && written > 0 {
|
|
size := header.MarshalSize() + len(payload)
|
|
if header.Padding {
|
|
size += int(header.PaddingSize)
|
|
}
|
|
writer.bytes.Add(uint64(size))
|
|
if writer.frames != nil && writer.primary && header.Marker && len(payload) > 0 {
|
|
writer.frames.Add(1)
|
|
}
|
|
}
|
|
return written, err
|
|
}
|
|
|
|
type publicationAudio struct {
|
|
webrtc.TrackLocal
|
|
bytes *atomic.Uint64
|
|
}
|
|
|
|
func (audio *publicationAudio) Bind(context webrtc.TrackLocalContext) (webrtc.RTPCodecParameters, error) {
|
|
return audio.TrackLocal.Bind(publicationAudioContext{TrackLocalContext: context,
|
|
writer: &publicationWriter{TrackLocalWriter: context.WriteStream(), bytes: audio.bytes}})
|
|
}
|
|
|
|
type publicationAudioContext struct {
|
|
webrtc.TrackLocalContext
|
|
writer webrtc.TrackLocalWriter
|
|
}
|
|
|
|
func (context publicationAudioContext) WriteStream() webrtc.TrackLocalWriter { return context.writer }
|
|
|
|
// TWCC measures video and its probes. Audio shares the path but is not counted
|
|
// in this estimate; subtracting it here would understate the video budget.
|
|
type publicationBWE struct {
|
|
*sendsidebwe.SendSideBWE
|
|
pacer *boundedPacer
|
|
listener bwe.BWEListener
|
|
target atomic.Int64
|
|
observed atomic.Bool
|
|
probing atomic.Bool
|
|
}
|
|
|
|
func (bandwidth *publicationBWE) HandleTWCCFeedback(report *rtcp.TransportLayerCC) {
|
|
bandwidth.observed.Store(true)
|
|
bandwidth.SendSideBWE.HandleTWCCFeedback(report)
|
|
}
|
|
|
|
func (bandwidth *publicationBWE) SetBWEListener(listener bwe.BWEListener) {
|
|
bandwidth.listener = listener
|
|
bandwidth.SendSideBWE.SetBWEListener(bandwidth)
|
|
}
|
|
|
|
func (bandwidth *publicationBWE) ProbeClusterStarting(info ccutils.ProbeClusterInfo) {
|
|
bandwidth.probing.Store(true)
|
|
bandwidth.SendSideBWE.ProbeClusterStarting(info)
|
|
}
|
|
|
|
func (bandwidth *publicationBWE) Reset() {
|
|
bandwidth.SendSideBWE.Reset()
|
|
bandwidth.probing.Store(false)
|
|
}
|
|
|
|
func (bandwidth *publicationBWE) OnCongestionStateChange(from, to bwe.CongestionState, capacity int64) {
|
|
// As for an individual Transport, only a completed non-congesting probe
|
|
// can raise capacity. The allocator ignores congestion during a probe; the
|
|
// encoder-facing budget must retain that same value through settling.
|
|
if !bandwidth.probing.Load() && to == bwe.CongestionStateCongested && capacity > 0 && capacity < bandwidth.target.Load() {
|
|
bandwidth.target.Store(capacity)
|
|
if bandwidth.pacer != nil {
|
|
bandwidth.pacer.SetBitrate(int(capacity))
|
|
}
|
|
}
|
|
bandwidth.listener.OnCongestionStateChange(from, to, bandwidth.target.Load())
|
|
}
|
|
func (bandwidth *publicationBWE) ProbeClusterFinalize() (ccutils.ProbeSignal, int64, bool) {
|
|
signal, capacity, done := bandwidth.SendSideBWE.ProbeClusterFinalize()
|
|
if done && signal == ccutils.ProbeSignalNotCongesting && capacity > bandwidth.target.Load() {
|
|
bandwidth.target.Store(capacity)
|
|
if bandwidth.pacer != nil {
|
|
bandwidth.pacer.SetBitrate(int(capacity))
|
|
}
|
|
}
|
|
if done {
|
|
bandwidth.probing.Store(false)
|
|
}
|
|
return signal, bandwidth.target.Load(), done
|
|
}
|
|
|
|
type publicationPacer struct {
|
|
*boundedPacer
|
|
headersMu sync.Mutex
|
|
headers map[uint32]publicationHeaders
|
|
}
|
|
|
|
func (queue *publicationPacer) Enqueue(packet *pacer.Packet) {
|
|
queue.headersMu.Lock()
|
|
headers := queue.headers[packet.Header.SSRC]
|
|
queue.headersMu.Unlock()
|
|
if headers.extension != 0 && headers.rid != "" {
|
|
_ = packet.Header.SetExtension(headers.extension, []byte(headers.rid))
|
|
}
|
|
if headers.midExtension != 0 {
|
|
_ = packet.Header.SetExtension(headers.midExtension, []byte(headers.mid))
|
|
}
|
|
packet.HeaderSize = packet.Header.MarshalSize()
|
|
if headers.counters != nil {
|
|
packet.WriteStream = &publicationWriter{TrackLocalWriter: packet.WriteStream,
|
|
bytes: &headers.counters.bytes, frames: &headers.counters.frames, primary: !packet.IsRTX && !packet.IsProbe}
|
|
}
|
|
queue.boundedPacer.Enqueue(packet)
|
|
}
|
|
func (queue *publicationPacer) EndProbeCluster(id ccutils.ProbeClusterId) ccutils.ProbeClusterInfo {
|
|
info := queue.boundedPacer.EndProbeCluster(id)
|
|
queue.cancelProbes(id)
|
|
return info
|
|
}
|