bbabc04bca
apple / swift (push) Successful in 54s
ci / rust (push) Successful in 1m32s
android / android (push) Successful in 1m49s
ci / web (push) Successful in 26s
ci / docs-site (push) Successful in 30s
ci / bench (push) Successful in 1m36s
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
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 3s
deb / build-publish (push) Successful in 2m20s
flatpak / build-publish (push) Successful in 4m6s
rpm / build-publish (bazzite, punktfunk-fedora-rpm) (push) Successful in 5m11s
docker / deploy-docs (push) Successful in 18s
rpm / build-publish (fedora-44, punktfunk-fedora44-rpm) (push) Successful in 4m32s
Adds true HDR (BT.2020 PQ) and 10-bit (HEVC Main10) streaming, negotiated so an 8-bit/SDR client is never sent a stream it can't decode, plus a robust fix for the capture losing the stream across a secure-desktop transition. Protocol (punktfunk-core/quic.rs): - Hello gains `video_caps` (VIDEO_CAP_10BIT / VIDEO_CAP_HDR), Welcome gains `bit_depth`, both as optional trailing bytes (back-compat). client-rs advertises 10-bit via PUNKTFUNK_CLIENT_10BIT; the connector advertises 0 for now (in-band detection drives the native clients). Regenerated punktfunk_core.h. Windows host: - 10-bit Main10: host enables it only when the client advertised VIDEO_CAP_10BIT AND PUNKTFUNK_10BIT is set; threaded through open_video → NVENC (profile Main10, pixelBitDepthMinus8). - HDR: when the captured desktop is scRGB FP16 (R16G16B16A16_FLOAT, HDR on), copy it to an FP16 surface, composite the cursor there, convert scRGB → BT.2020 PQ 10-bit (R10G10B10A2) via a shader, and encode HEVC Main10 with the BT.2020/PQ colour VUI (ABGR10 input). Fixes the freeze + cursor-trail that came from feeding FP16 into the BGRA path. Reacts dynamically to the HDR toggle. - Capture recovery: rebuild is now a single NON-BLOCKING attempt, throttled to ~4×/s, repeating the last good frame between attempts (format-tagged last_present). During a secure-desktop dwell SudoVDA's output is gone; the old blocking 12 s retry starved the send loop for seconds so the client timed out and disconnected — now the session stays fed (frozen) until the desktop returns. Also seeds a black frame on recovery. Apple client (PunktfunkKit): - Detects HDR in-band from the stream VUI (PQ transfer function), decodes to 10-bit P010, and presents via an rgba16Float + BT.2020 PQ CAMetalLayer with EDR; SDR path unchanged. Switches automatically on a mid-session HDR toggle. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
281 lines
11 KiB
Rust
281 lines
11 KiB
Rust
//! Frame capture (plan §7). On Linux: a PipeWire ScreenCast portal stream. M0 uses the
|
|
//! CPU-copy fallback (the portal delivers a CPU buffer; the encoder uploads it to the GPU
|
|
//! internally). Zero-copy dmabuf→NVENC import is deferred (plan §9 risk).
|
|
|
|
use anyhow::Result;
|
|
|
|
/// Packed pixel layout of a [`CapturedFrame`]. The ScreenCast portal negotiates the
|
|
/// format; on wlroots it is commonly packed `RGB` (3 bytes/pixel). The encoder maps these
|
|
/// to an NVENC-accepted input format (`rgb0`/`bgr0`/`rgba`/`bgra`), expanding 3→4 bytes
|
|
/// where needed — no host-side colour conversion.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum PixelFormat {
|
|
/// `[B,G,R,x]`, 4 bpp.
|
|
Bgrx,
|
|
/// `[R,G,B,x]`, 4 bpp.
|
|
Rgbx,
|
|
/// `[B,G,R,A]`, 4 bpp.
|
|
Bgra,
|
|
/// `[R,G,B,A]`, 4 bpp.
|
|
Rgba,
|
|
/// `[R,G,B]`, 3 bpp.
|
|
Rgb,
|
|
/// `[B,G,R]`, 3 bpp.
|
|
Bgr,
|
|
/// 10-bit RGB packed as `R10G10B10A2` (DXGI `R10G10B10A2_UNORM`), 4 bpp. The HDR capture path
|
|
/// produces this: scRGB FP16 desktop pixels are converted to BT.2020 PQ and written here, then
|
|
/// handed to NVENC as `ABGR10` for an HEVC Main10 / HDR10 encode.
|
|
Rgb10a2,
|
|
}
|
|
|
|
impl PixelFormat {
|
|
pub fn bytes_per_pixel(self) -> usize {
|
|
match self {
|
|
PixelFormat::Rgb | PixelFormat::Bgr => 3,
|
|
_ => 4,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A captured frame. [`format`](Self::format)/dimensions describe the pixels regardless of
|
|
/// where they live — [`payload`](Self::payload) is either a CPU buffer (the M0/fallback path)
|
|
/// or a GPU buffer already on the device (the zero-copy path, plan §9).
|
|
pub struct CapturedFrame {
|
|
pub width: u32,
|
|
pub height: u32,
|
|
pub pts_ns: u64,
|
|
/// Pixel layout of the payload.
|
|
pub format: PixelFormat,
|
|
pub payload: FramePayload,
|
|
}
|
|
|
|
/// Where a captured frame's pixels live.
|
|
pub enum FramePayload {
|
|
/// Tightly-packed CPU pixels in `format`, `width*height*bytes_per_pixel` (no row padding).
|
|
Cpu(Vec<u8>),
|
|
/// A pitched GPU buffer (BGRA-order, on the shared CUDA context) — the zero-copy path. The
|
|
/// dmabuf has already been imported + copied into this owned device buffer.
|
|
#[cfg(target_os = "linux")]
|
|
Cuda(crate::zerocopy::DeviceBuffer),
|
|
/// A GPU-resident D3D11 texture (Windows zero-copy path for NVENC). Owns the copied frame.
|
|
#[cfg(target_os = "windows")]
|
|
D3d11(dxgi::D3d11Frame),
|
|
}
|
|
|
|
impl CapturedFrame {
|
|
/// True if the frame's pixels are a GPU/CUDA buffer (the zero-copy path).
|
|
pub fn is_cuda(&self) -> bool {
|
|
#[cfg(target_os = "linux")]
|
|
{
|
|
matches!(self.payload, FramePayload::Cuda(_))
|
|
}
|
|
#[cfg(not(target_os = "linux"))]
|
|
{
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Produces frames from a captured output. Lives on its own thread, feeding the encoder
|
|
/// over a bounded drop-oldest channel (never block the compositor).
|
|
pub trait Capturer: Send {
|
|
fn next_frame(&mut self) -> Result<CapturedFrame>;
|
|
|
|
/// Non-blocking: the freshest frame available since the last call, or `None` if none has
|
|
/// arrived (the caller reuses its last frame to hold a steady output rate). The default
|
|
/// just produces a frame each call — fine for instant synthetic sources; the portal
|
|
/// overrides it to drain its channel without blocking.
|
|
fn try_latest(&mut self) -> Result<Option<CapturedFrame>> {
|
|
self.next_frame().map(Some)
|
|
}
|
|
|
|
/// Gate expensive per-frame work so the capturer can be kept alive (reused) between
|
|
/// streams without burning CPU. The portal capturer skips the de-pad copy while inactive;
|
|
/// the default is a no-op (synthetic sources are produced on demand). Set `true` for the
|
|
/// duration of a stream, `false` when it ends.
|
|
fn set_active(&self, _active: bool) {}
|
|
}
|
|
|
|
/// A deterministic moving test pattern (BGRx). Lets M0 exercise the encode → file →
|
|
/// `punktfunk_core` path with no live capture session, and produces obviously non-static
|
|
/// content (a sweeping bar + animated gradient) so the encoded output is verifiable.
|
|
pub struct SyntheticCapturer {
|
|
width: u32,
|
|
height: u32,
|
|
fps: u32,
|
|
frame_idx: u64,
|
|
buf: Vec<u8>,
|
|
}
|
|
|
|
impl SyntheticCapturer {
|
|
const BPP: usize = 4; // emits BGRx
|
|
|
|
pub fn new(width: u32, height: u32, fps: u32) -> Self {
|
|
assert!(width > 0 && height > 0 && fps > 0);
|
|
let buf = vec![0u8; width as usize * height as usize * Self::BPP];
|
|
SyntheticCapturer {
|
|
width,
|
|
height,
|
|
fps,
|
|
frame_idx: 0,
|
|
buf,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Capturer for SyntheticCapturer {
|
|
fn next_frame(&mut self) -> Result<CapturedFrame> {
|
|
let w = self.width as usize;
|
|
let h = self.height as usize;
|
|
let bpp = Self::BPP;
|
|
let t = self.frame_idx;
|
|
// A vertical bar sweeps left→right once every ~2s; the background is a gradient
|
|
// whose phase advances each frame, so every pixel changes frame-to-frame.
|
|
let bar_x = ((t * w as u64) / (self.fps as u64 * 2)) % w as u64;
|
|
let phase = (t % 256) as usize;
|
|
for y in 0..h {
|
|
let row = y * w * bpp;
|
|
for x in 0..w {
|
|
let i = row + x * bpp;
|
|
let on_bar = (x as u64).abs_diff(bar_x) < 8;
|
|
// BGRx byte order: [B, G, R, x]
|
|
self.buf[i] = if on_bar {
|
|
255
|
|
} else {
|
|
((x + phase) & 0xff) as u8
|
|
};
|
|
self.buf[i + 1] = if on_bar {
|
|
255
|
|
} else {
|
|
((y + phase) & 0xff) as u8
|
|
};
|
|
self.buf[i + 2] = if on_bar { 255 } else { ((x + y) & 0xff) as u8 };
|
|
self.buf[i + 3] = 0;
|
|
}
|
|
}
|
|
let pts_ns = self.frame_idx * 1_000_000_000 / self.fps as u64;
|
|
self.frame_idx += 1;
|
|
Ok(CapturedFrame {
|
|
width: self.width,
|
|
height: self.height,
|
|
pts_ns,
|
|
format: PixelFormat::Bgrx,
|
|
payload: FramePayload::Cpu(self.buf.clone()),
|
|
})
|
|
}
|
|
}
|
|
|
|
/// A cheap moving test pattern (BGRx) for the streaming path: a pulsing field + a white band
|
|
/// sweeping down, generated with whole-buffer `fill`s so it stays real-time even at 5K.
|
|
pub struct FastSyntheticCapturer {
|
|
width: u32,
|
|
height: u32,
|
|
frame_idx: u64,
|
|
buf: Vec<u8>,
|
|
/// PUNKTFUNK_SYNTH_NOISE: every frame is fresh high-entropy noise NVENC can't compress or
|
|
/// predict, so the encoder hits its (CBR) bitrate target — a throughput test of the real
|
|
/// encode→FEC→send→recv path. The default flat/band content compresses to ~nothing, so it
|
|
/// can't generate real Mbps (the encoder is content-driven). xorshift over u64 chunks.
|
|
noise: bool,
|
|
rng: u64,
|
|
}
|
|
|
|
impl FastSyntheticCapturer {
|
|
pub fn new(width: u32, height: u32) -> Self {
|
|
assert!(width > 0 && height > 0);
|
|
FastSyntheticCapturer {
|
|
width,
|
|
height,
|
|
frame_idx: 0,
|
|
buf: vec![0u8; width as usize * height as usize * 4],
|
|
noise: std::env::var_os("PUNKTFUNK_SYNTH_NOISE").is_some(),
|
|
rng: 0x9e3779b97f4a7c15,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Capturer for FastSyntheticCapturer {
|
|
fn next_frame(&mut self) -> Result<CapturedFrame> {
|
|
if self.noise {
|
|
// Fresh, every-frame-decorrelated noise: reseed from the frame index so consecutive
|
|
// frames share no structure (forces large P-frames too, not just the keyframe).
|
|
let mut s = self
|
|
.rng
|
|
.wrapping_add(self.frame_idx.wrapping_mul(0x2545F491_4F6CDD1D))
|
|
| 1;
|
|
for c in self.buf.chunks_exact_mut(8) {
|
|
s ^= s << 13;
|
|
s ^= s >> 7;
|
|
s ^= s << 17;
|
|
c.copy_from_slice(&s.to_le_bytes());
|
|
}
|
|
self.rng = s;
|
|
} else {
|
|
let (w, h) = (self.width as usize, self.height as usize);
|
|
let row = w * 4;
|
|
let shade = (self.frame_idx % 256) as u8;
|
|
self.buf.fill(shade);
|
|
let band_h = (h / 20).max(1);
|
|
let band_y = (self.frame_idx as usize * 6) % h;
|
|
for y in band_y..(band_y + band_h).min(h) {
|
|
self.buf[y * row..(y + 1) * row].fill(0xff);
|
|
}
|
|
}
|
|
self.frame_idx += 1;
|
|
Ok(CapturedFrame {
|
|
width: self.width,
|
|
height: self.height,
|
|
pts_ns: 0,
|
|
format: PixelFormat::Bgrx,
|
|
payload: FramePayload::Cpu(self.buf.clone()),
|
|
})
|
|
}
|
|
}
|
|
|
|
/// Open a live capturer for a client-sized monitor via the xdg ScreenCast portal
|
|
/// (`ashpd`) → PipeWire (`pipewire`). Implemented in the `linux` submodule.
|
|
#[cfg(target_os = "linux")]
|
|
pub fn open_portal_monitor() -> Result<Box<dyn Capturer>> {
|
|
// On RemoteDesktop-capable desktops (KWin/GNOME) anchor ScreenCast to a RemoteDesktop
|
|
// session so it inherits that grant headlessly; wlroots/Sway has no RemoteDesktop portal,
|
|
// so use a plain ScreenCast session there.
|
|
let anchored = crate::inject::default_backend() == crate::inject::Backend::Libei;
|
|
linux::PortalCapturer::open(anchored).map(|c| Box::new(c) as Box<dyn Capturer>)
|
|
}
|
|
|
|
#[cfg(not(target_os = "linux"))]
|
|
pub fn open_portal_monitor() -> Result<Box<dyn Capturer>> {
|
|
anyhow::bail!("portal capture requires Linux (xdg-desktop-portal + PipeWire)")
|
|
}
|
|
|
|
/// Build a capturer from an already-created virtual output (see [`crate::vdisplay`]). Consumes
|
|
/// the output's PipeWire node + optional remote fd + keepalive — the capturer owns the keepalive,
|
|
/// so dropping the capturer releases the virtual output. Compositor-agnostic: works for any
|
|
/// [`crate::vdisplay::VirtualDisplay`] backend. The captured size is the size the output was
|
|
/// created at — native, no scaling.
|
|
#[cfg(target_os = "linux")]
|
|
pub fn capture_virtual_output(vout: crate::vdisplay::VirtualOutput) -> Result<Box<dyn Capturer>> {
|
|
linux::PortalCapturer::from_virtual_output(vout).map(|c| Box::new(c) as Box<dyn Capturer>)
|
|
}
|
|
|
|
#[cfg(target_os = "windows")]
|
|
pub fn capture_virtual_output(vout: crate::vdisplay::VirtualOutput) -> Result<Box<dyn Capturer>> {
|
|
let target = vout.win_capture.clone().ok_or_else(|| {
|
|
anyhow::anyhow!(
|
|
"SudoVDA target not yet an active display (needs a WDDM GPU to activate it)"
|
|
)
|
|
})?;
|
|
dxgi::DuplCapturer::open(target, vout.preferred_mode, vout.keepalive)
|
|
.map(|c| Box::new(c) as Box<dyn Capturer>)
|
|
}
|
|
|
|
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
|
|
pub fn capture_virtual_output(_vout: crate::vdisplay::VirtualOutput) -> Result<Box<dyn Capturer>> {
|
|
anyhow::bail!("virtual-output capture requires Linux or Windows")
|
|
}
|
|
|
|
#[cfg(target_os = "windows")]
|
|
pub mod dxgi;
|
|
#[cfg(target_os = "linux")]
|
|
mod linux;
|