Files
VoelinClient/crates/voelin-media/src/workers.rs
T
Claude 152e0ee9a1 voelin-media: capture into sinks, zero-copy conversion, scaling, pools; libvpx tuning
Screen sharing was slow mostly because every frame was copied and
allocated several times on its way to the encoder: the PipeWire callback
copied the mapped buffer into a new Vec, X11 copied the shared-memory
segment, Windows copied twice, `to_i420` allocated fresh planes on one
thread, and libvpx output was copied into Vec after Vec.

Capture backends now hand each frame to a `FrameSink` on their own
thread, with the pixels still in the capture buffer (`FrameRef`: borrowed
planes with strides). `ScreenCapture::start_sink` is implemented by the
test pattern (renders into one reused buffer), X11 (the MIT-SHM segment,
cursor blended in place, monitor/window geometry looked up once a second
instead of every frame), the portal and Windows Graphics Capture (the
mapped staging texture with its row pitch); other backends get a
forwarding default. `start` keeps working through a queue adapter.
`FramePacer` caps frame rates by timestamps; sinks can change the cap
while capturing (X11 and the test pattern follow, the portal renegotiates
the frame rate).

Portal: LINEAR DMA-BUFs are negotiated in addition to shared memory
(mandatory modifier, fixated by us, `dataType` per buffer kind), mapped
once per buffer and read between DMA_BUF_IOCTL_SYNC start/end. When
reading them is slow (uncached VRAM), the stream renegotiates to shared
memory. Tiled modifiers are not offered (needs a GPU import).

The streaming path:
- `workers::Workers`: a fork-join pool that allocates nothing per job.
- `convert::Converter`: any stride, BGRx/BGRA/RGBx/RGBA (yuv's SIMD
  paths), I420 and NV12 into a caller's I420 frame, in row bands on all
  cores; handles buffers without padding after the last row.
- `scale`: separable area-average downscaling (bilinear up), weights per
  size pair computed once, a 2x2 box fast path, row bands on the pool;
  `Pyramid` converts a capture once and derives every requested size
  from the nearest larger one, sharing equal sizes.
- `pool::FramePool`: recycled `Arc<VideoFrame>`s; `handoff::Handoff`: a
  one-slot latest-wins atomic handoff that counts replaced frames.

Codecs: `VideoEncoder::encode_with` hands out encoded frames borrowed
from the encoder (libvpx's own buffer; OpenH264 into a reused buffer).
libvpx: `cpu-used` adapts to the measured encode time against the frame
interval (VP8 levels -10..-4, VP9 starting at 8 within 5..9), threads from
all CPUs the frame size can use, VP8 token partitions and VP9 tile
columns from the thread count and width, keyframes only on request,
frame durations from timestamps (variable frame rate), `set_fps`, and
`speed` for stats. A probe on the desktop test pattern showed VP8 speeds
beyond -10 are no faster on screen content, only worse (about 3 dB PSNR).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
2026-09-27 08:35:57 +00:00

317 lines
9.0 KiB
Rust

//! A small fork-join thread pool for per-frame pixel work.
//!
//! [`Workers::run`] splits a job into numbered tasks that the pool's threads
//! and the calling thread claim until none are left, and returns once all of
//! them finished. Unlike `std::thread::scope` it spawns nothing per job, and
//! unlike a general task queue it allocates nothing per job: the job is a
//! borrowed closure, published through a mutex and condition variable
//! (futexes on Linux and Android, SRW locks on Windows).
#![allow(unsafe_code)]
use std::any::Any;
use std::panic::{AssertUnwindSafe, catch_unwind, resume_unwind};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError};
use std::thread::JoinHandle;
/// Most tasks one job is split into (see [`Workers::tasks`]).
pub const MAX_TASKS: usize = 64;
/// The job of one [`Workers::run`] call.
#[derive(Clone, Copy)]
struct Job {
/// The caller's closure with its lifetime erased; see [`Workers::run`]
/// for why it stays valid while a worker uses it.
f: *const (dyn Fn(usize) + Sync + 'static),
tasks: usize,
}
// SAFETY: the closure is `Sync`, so calling it from several threads through
// a shared pointer is allowed; the pointer is only dereferenced while the
// `run` call that owns the closure waits (see `Workers::run`).
unsafe impl Send for Job {}
struct State {
/// Bumped for every job.
generation: u64,
job: Option<Job>,
/// Workers that took the current job and have not given it back.
active: usize,
/// Tasks of the current job that are done.
finished: usize,
panic: Option<Box<dyn Any + Send>>,
stop: bool,
}
struct Shared {
state: Mutex<State>,
/// Workers wait here for a job.
wake: Condvar,
/// The caller waits here for the last task.
done: Condvar,
/// The next task to claim.
next: AtomicUsize,
}
impl Shared {
fn lock(&self) -> MutexGuard<'_, State> {
self.state.lock().unwrap_or_else(PoisonError::into_inner)
}
}
/// Run tasks of `job` until none are left; returns how many this thread
/// ran.
fn claim(shared: &Shared, job: Job, panic: &mut Option<Box<dyn Any + Send>>) -> usize {
let mut count = 0;
loop {
let task = shared.next.fetch_add(1, Ordering::Relaxed);
if task >= job.tasks {
return count;
}
// SAFETY: `job.f` points to the closure of the `run` call that
// published `job`; that call does not return before every thread
// that took the job gave it back (`active`), so the closure lives.
let f = unsafe { &*job.f };
if let Err(e) = catch_unwind(AssertUnwindSafe(|| f(task))) {
panic.get_or_insert(e);
}
count += 1;
}
}
/// A pool of threads for splitting per-frame work. Stops its threads when
/// dropped.
pub struct Workers {
shared: Arc<Shared>,
threads: Vec<JoinHandle<()>>,
}
impl Workers {
/// A pool that uses `threads` threads in total, the caller of
/// [`Workers::run`] included (so `threads - 1` are spawned). 0 means one
/// per available CPU.
pub fn new(name: &str, threads: usize) -> Self {
let threads = match threads {
0 => std::thread::available_parallelism().map_or(1, |n| n.get()),
n => n,
};
let shared = Arc::new(Shared {
state: Mutex::new(State {
generation: 0,
job: None,
active: 0,
finished: 0,
panic: None,
stop: false,
}),
wake: Condvar::new(),
done: Condvar::new(),
next: AtomicUsize::new(0),
});
let mut workers = Self { shared, threads: Vec::new() };
for i in 1..threads {
let shared = workers.shared.clone();
let spawned = std::thread::Builder::new()
.name(format!("{name}-{i}"))
.spawn(move || worker(&shared));
match spawned {
Ok(thread) => workers.threads.push(thread),
Err(e) => {
tracing::warn!("cannot start a pixel worker thread: {e}");
break;
}
}
}
workers
}
/// Threads that work on a job, the caller included.
pub fn threads(&self) -> usize {
self.threads.len() + 1
}
/// A good number of tasks to split a job of `units` independent parts
/// (e.g. pairs of rows) into: a few per thread, so a thread that gets
/// descheduled does not hold everyone up.
pub fn tasks(&self, units: usize) -> usize {
(self.threads() * 3).min(MAX_TASKS).min(units).max(1)
}
/// Call `f(0)`, ..., `f(tasks - 1)` on the pool's threads and this one,
/// each exactly once, and return when all returned. A panic in `f` is
/// resumed here after the other tasks finished.
pub fn run(&mut self, tasks: usize, f: &(dyn Fn(usize) + Sync)) {
if tasks <= 1 || self.threads.is_empty() {
(0..tasks).for_each(f);
return;
}
// SAFETY: this only erases the lifetime. The pointer is published in
// `State::job` below and removed before this function returns. A
// worker copies it only while holding the lock and counts itself in
// `active` in the same critical section; it gives it back
// (`active -= 1`) after its last use. This function waits until
// `active` is zero, and takes `&mut self`, so no second job can
// overlap. Hence `f` outlives every dereference of the pointer.
let f_static: &'static (dyn Fn(usize) + Sync) = unsafe { std::mem::transmute(f) };
let job = Job { f: f_static, tasks };
{
let mut state = self.shared.lock();
self.shared.next.store(0, Ordering::Relaxed);
state.generation = state.generation.wrapping_add(1);
state.job = Some(job);
state.finished = 0;
}
self.shared.wake.notify_all();
let mut panic = None;
let count = claim(&self.shared, job, &mut panic);
let mut state = self.shared.lock();
state.finished += count;
while state.finished < tasks || state.active > 0 {
state = self.shared.done.wait(state).unwrap_or_else(PoisonError::into_inner);
}
state.job = None;
let panic = panic.or_else(|| state.panic.take());
drop(state);
if let Some(panic) = panic {
resume_unwind(panic);
}
}
}
fn worker(shared: &Shared) {
let mut seen = 0;
loop {
let job = {
let mut state = shared.lock();
loop {
if state.stop {
return;
}
if let Some(job) = state.job
&& state.generation != seen
{
seen = state.generation;
state.active += 1;
break job;
}
state = shared.wake.wait(state).unwrap_or_else(PoisonError::into_inner);
}
};
let mut panic = None;
let count = claim(shared, job, &mut panic);
let mut state = shared.lock();
state.finished += count;
state.active -= 1;
if panic.is_some() && state.panic.is_none() {
state.panic = panic;
}
if state.active == 0 && state.finished >= job.tasks {
shared.done.notify_all();
}
}
}
impl Drop for Workers {
fn drop(&mut self) {
self.shared.lock().stop = true;
self.shared.wake.notify_all();
for thread in self.threads.drain(..) {
let _ = thread.join();
}
}
}
/// Hands out the items of a slice to the tasks of a [`Workers::run`] job,
/// each item to exactly one task, without `unsafe` or allocation.
pub struct Slots<T> {
slots: [Mutex<Option<T>>; MAX_TASKS],
len: usize,
}
impl<T> Slots<T> {
/// Up to [`MAX_TASKS`] items; more panic.
pub fn new(items: impl IntoIterator<Item = T>) -> Self {
let slots = std::array::from_fn(|_| Mutex::new(None));
let mut len = 0;
for (slot, item) in slots.iter().zip(items) {
*slot.lock().unwrap_or_else(PoisonError::into_inner) = Some(item);
len += 1;
}
Self { slots, len }
}
pub fn len(&self) -> usize {
self.len
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
/// Item `i`, once.
pub fn take(&self, i: usize) -> Option<T> {
self.slots.get(i)?.lock().unwrap_or_else(PoisonError::into_inner).take()
}
}
#[cfg(test)]
mod tests {
use std::sync::atomic::AtomicU64;
use super::*;
#[test]
fn runs_every_task_once() {
let mut workers = Workers::new("test-workers", 4);
assert_eq!(workers.threads(), 4);
for round in 0..200 {
let tasks = 1 + round % 40;
let hits: Vec<AtomicU64> = (0..tasks).map(|_| AtomicU64::new(0)).collect();
workers.run(tasks, &|i| {
hits[i].fetch_add(1, Ordering::Relaxed);
});
assert!(hits.iter().all(|h| h.load(Ordering::Relaxed) == 1), "round {round}");
}
}
#[test]
fn borrows_and_mutates_through_slots() {
let mut workers = Workers::new("test-workers", 3);
let mut data = vec![0u32; 1000];
let slots = Slots::new(data.chunks_mut(100));
assert_eq!(slots.len(), 10);
workers.run(slots.len(), &|i| {
for x in slots.take(i).unwrap() {
*x = i as u32 + 1;
}
});
assert!(data.chunks(100).enumerate().all(|(i, c)| c.iter().all(|&x| x == i as u32 + 1)));
}
#[test]
fn panics_reach_the_caller_and_the_pool_survives() {
let mut workers = Workers::new("test-workers", 2);
let result = catch_unwind(AssertUnwindSafe(|| {
workers.run(8, &|i| assert_ne!(i, 5, "task five"));
}));
assert!(result.is_err());
let count = AtomicU64::new(0);
workers.run(8, &|_| {
count.fetch_add(1, Ordering::Relaxed);
});
assert_eq!(count.load(Ordering::Relaxed), 8);
}
#[test]
fn single_thread_runs_inline() {
let mut workers = Workers::new("test-workers", 1);
assert_eq!(workers.threads(), 1);
assert_eq!(workers.tasks(100), 3);
let count = AtomicU64::new(0);
workers.run(5, &|_| {
count.fetch_add(1, Ordering::Relaxed);
});
assert_eq!(count.load(Ordering::Relaxed), 5);
}
}