c3fa6c1514
apple / swift (push) Successful in 1m15s
release / apple (push) Successful in 15m35s
apple / screenshots (push) Successful in 4m35s
android / android (push) Successful in 4m17s
arch / build-publish (push) Successful in 5m51s
ci / web (push) Successful in 1m5s
ci / docs-site (push) Successful in 1m22s
ci / rust (push) Successful in 5m14s
deb / build-publish (push) Successful in 3m31s
decky / build-publish (push) Successful in 12s
docker / build-push (--build-arg FEDORA_VERSION=44, ci, ci/fedora-rpm.Dockerfile, punktfunk-fedora44-rpm) (push) Successful in 5s
ci / bench (push) Successful in 5m0s
docker / build-push (., web/Dockerfile, punktfunk-web) (push) Successful in 4s
docker / build-push (ci, ci/fedora-rpm.Dockerfile, punktfunk-fedora-rpm) (push) Successful in 4s
docker / build-push (ci, ci/rust-ci.Dockerfile, punktfunk-rust-ci) (push) Successful in 4s
docker / build-push (docs-site, docs-site/Dockerfile, punktfunk-docs) (push) Successful in 4s
rpm / build-publish (44, fedora-44, punktfunk-fedora44-rpm) (push) Successful in 11m12s
rpm / build-publish (43, bazzite, punktfunk-fedora-rpm) (push) Successful in 11m20s
docker / deploy-docs (push) Successful in 6s
The Apple client was HEVC/H.264-only: the receive path spoke Annex-B NALs exclusively, so AV1 was never advertised and the codec picker hid it. Add the OBU flavor of the same plumbing (AV1.swift, sibling of AnnexB.swift): a zero-copy OBU walker, a full spec-5.5.1 sequence-header parser, an av1C CMVideoFormatDescription with colorimetry extensions (so isHDRFormat and the presenter stay codec-agnostic), and an ISOBMFF 'av01' sample repack (temporal delimiter stripped, everything size-fielded, one copy per AU). VideoCodec gains .av1 (wire 0x04); both pumps and VideoDecoder route through dispatching formatDescription(fromKeyframe:)/sampleBuffer(au:) — keyframe gating keys on the in-band sequence header exactly as the NAL codecs key on in-band parameter sets, so loss recovery and mid-session reconfigure work unchanged. AV1 sessions require a hardware decoder (VideoToolbox has no software AV1; same fail-fast policy as 4:4:4), and both the Hello advertisement and the Settings picker are gated on VTIsHardwareDecodeSupported — AV1 only appears on devices that can actually decode it (M3-class Macs, A17 Pro-class iPhones; no Apple TV). Tests: real SVT-AV1 blobs (generation recipe in the file) cover the walk, the parse against an independent reference, av1C bytes, delta-TU gating, repack byte-exactness, and — on AV1 hardware — a real VTDecompressionSession decode through VideoDecoder. Host precedence stays HEVC > AV1 > H.264, so AV1 engages only when explicitly picked. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
122 lines
6.4 KiB
Swift
122 lines
6.4 KiB
Swift
// The platform-independent heart of the presenters: one thread pulling AUs from the
|
|
// connection into an AVSampleBufferDisplayLayer, with the format description refreshed
|
|
// on every IDR (the host opens with an IDR carrying in-band parameter sets; recovery
|
|
// keyframes re-send them — there is no out-of-band extradata, ever). Shared by the
|
|
// macOS StreamLayerView and the iOS/iPadOS stream view.
|
|
|
|
import AVFoundation
|
|
import Foundation
|
|
import os
|
|
|
|
private let pumpLog = Logger(subsystem: "io.unom.punktfunk", category: "video")
|
|
|
|
/// One pump per instance; create a fresh StreamPump per start (the stop is permanent —
|
|
/// a restart hands the old pump its own token, so it can never be revived by a newer start()).
|
|
final class StreamPump {
|
|
private let token = StopFlag()
|
|
|
|
/// Pump thread: pull AUs, wrap, enqueue. Non-IDR AUs before the first format
|
|
/// description are dropped. `onFrame`/`onSessionEnd` fire on the pump thread.
|
|
func start(
|
|
connection: PunktfunkConnection,
|
|
layer: AVSampleBufferDisplayLayer,
|
|
onFrame: (@Sendable (AccessUnit) -> Void)?,
|
|
onSessionEnd: (@Sendable () -> Void)?
|
|
) {
|
|
let token = token
|
|
// Coalesced host keyframe requests (100 ms throttle — see KeyframeRecovery).
|
|
let recovery = KeyframeRecovery()
|
|
recovery.bind(connection)
|
|
// The layer is non-Sendable but its enqueue/flush are documented thread-safe, and after
|
|
// this point only the pump thread drives it — assert that so the @Sendable Thread closure
|
|
// may capture it.
|
|
nonisolated(unsafe) let layer = layer
|
|
layer.flush() // drop any frames a previous connection left queued
|
|
|
|
let thread = Thread {
|
|
var format: CMVideoFormatDescription?
|
|
var lastFramesDropped = connection.framesDropped()
|
|
// Recovery is a persistent WANT, not a one-shot edge: set it on detected loss (or a
|
|
// decoder reset), retry the throttled request EVERY iteration, and clear it only when a
|
|
// fresh IDR actually re-anchors decode. The old code advanced `lastFramesDropped` on the
|
|
// same edge it fired the throttled request — so a request swallowed by the throttle (a
|
|
// second drop within the window, e.g. the lost recovery IDR itself being pruned) was
|
|
// never re-sent: the counter went flat, the climb never re-fired, and the picture stayed
|
|
// frozen for good while audio kept playing. The iPhone's lossy Wi-Fi hits this where the
|
|
// Mac's Ethernet never does.
|
|
var awaitingIDR = false
|
|
var awaitingSince = Date.distantPast // when the current recovery began (for the resume log)
|
|
var wasFailed = false
|
|
while !token.isStopped {
|
|
do {
|
|
// Loss recovery (the primary path). Under the host's infinite GOP the only
|
|
// recovery keyframe is one we request. The reassembler drops unrecoverable AUs
|
|
// (framesDropped); the decoder then *conceals* the reference-missing deltas — a
|
|
// frozen / garbage picture that never flips the layer to .failed — so key off the
|
|
// drop count climbing, then keep asking (awaitingIDR) until an IDR lands. Polled
|
|
// every iteration so a total-loss drought still recovers when packets resume.
|
|
let dropped = connection.framesDropped()
|
|
if dropped > lastFramesDropped {
|
|
// Log only on the false→true transition (once per recovery cycle), not per
|
|
// dropped AU, so heavy loss doesn't spam the log.
|
|
if !awaitingIDR {
|
|
awaitingSince = Date()
|
|
pumpLog.notice(
|
|
"video: unrecoverable drop (framesDropped=\(dropped, privacy: .public)) — requesting recovery IDR")
|
|
}
|
|
lastFramesDropped = dropped
|
|
awaitingIDR = true
|
|
}
|
|
if awaitingIDR { recovery.request() }
|
|
|
|
guard let au = try connection.nextAU(timeoutMs: 100) else { continue }
|
|
onFrame?(au)
|
|
let idrFormat = connection.videoCodec.formatDescription(fromKeyframe: au.data)
|
|
if let f = idrFormat {
|
|
format = f // refreshed on every IDR (mode changes included)
|
|
if awaitingIDR {
|
|
let ms = Int(Date().timeIntervalSince(awaitingSince) * 1000)
|
|
pumpLog.notice("video: recovery IDR received — resumed after \(ms, privacy: .public) ms")
|
|
}
|
|
awaitingIDR = false // a fresh IDR re-anchored decode — recovery complete
|
|
}
|
|
let failed = layer.status == .failed
|
|
if failed {
|
|
// Decode wedged hard (the cold-first-connect case — a lost/corrupt opening
|
|
// IDR): flush and, unless THIS AU is the recovering IDR (re-anchored above),
|
|
// re-gate on the next in-band parameter sets and keep asking — enqueuing a
|
|
// delta into a failed layer can't recover it.
|
|
if !wasFailed { pumpLog.warning("video: display layer .failed — flushing + re-anchoring") }
|
|
layer.flush()
|
|
if idrFormat == nil {
|
|
format = nil
|
|
awaitingIDR = true
|
|
}
|
|
}
|
|
wasFailed = failed
|
|
guard let f = format,
|
|
let sample = connection.videoCodec.sampleBuffer(au: au, format: f),
|
|
!token.isStopped // don't enqueue a stale frame after a restart
|
|
else { continue }
|
|
layer.enqueue(sample)
|
|
} catch {
|
|
if !token.isStopped {
|
|
onSessionEnd?()
|
|
}
|
|
break // session closed
|
|
}
|
|
}
|
|
}
|
|
thread.name = "punktfunk-pump"
|
|
thread.qualityOfService = .userInteractive
|
|
thread.start()
|
|
}
|
|
|
|
/// Stop pumping (≤ one poll timeout). Does not close the connection.
|
|
func stop() {
|
|
token.stop()
|
|
}
|
|
|
|
deinit { token.stop() }
|
|
}
|