Encode desktop video in-process instead of an ffmpeg child

Capture (x11grab) and H.264 encoding (h264_nvenc / libx264) now run in
this process through ffmpeg-next, with the same codec options as the old
ffmpeg CLI. Each encoder packet is one access unit, fed to the existing
DependencyGate. A PLI or FIR from the viewer forces the next frame to be
an IDR. Wayland and portal capture still use the child-process path.

Set UU_VIDEO_PIPELINE=process to fall back to the old ffmpeg child.
desktop-video-probe accepts UU_PROBE_FRAMES to lengthen the sample.

Measured with desktop-video-probe on ros2go-gpu (RTX 4090 D, Xvfb
1920x1080 @60fps, 600 frames, mostly static desktop). CPU is the sum of
the probe process and its children, sampled from /proc once per second.
Probe only: no WebRTC send, no audio.

  encoder     pipeline     CPU (%)    wall (s)   bitrate (kbps)
  nvenc       process      ~138       10.17      5356
  nvenc       in-process   ~43        10.34      5176
  libx264     process      ~135       10.09      5073
  libx264     in-process   ~72        10.08      4986

All four outputs decode without errors and start with an IDR.

Not covered: audio still runs its own ffmpeg child (src/audio.rs), PLI to
IDR has not been checked against a live viewer, and the CI jobs need the
libav*-dev and libclang-dev packages (added to ci.yml, release.yml,
AGENTS.md and the READMEs).
This commit is contained in:
ferstar authored and VallierDev committed 2026-10-15 07:43:21 +08:00
1 parent 078ca026a5
commit bf0aa6879a
11 files changed
+363 -22

No files matched your search

+155
View File
@@ -0,0 +1,155 @@
//! In-process X11 capture and H.264 encoding through libavdevice/libavcodec.
//! It replaces the `ffmpeg` child process with the same codec options, so the viewer
//! still receives the same Annex-B access units (AUD, SPS/PPS, IDR, P).
use std::ffi::CString;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Once};
use std::time::{Duration, Instant};
use ffmpeg_next as ff;
use ff::format::{format::Format, Pixel};
use ff::util::picture;
use ff::{codec, encoder, frame, software::scaling, Dictionary, Packet, Rational};
use tokio::sync::mpsc;
pub struct Config {
pub display: String,
pub input: (u32, u32),
pub output: Option<(u32, u32)>,
pub fps: u32,
pub gop: u32,
pub cq: u32,
pub target_bps: i64,
pub max_bps: i64,
pub buffer_bits: i64,
pub gpu: bool,
}
/// `UU_VIDEO_PIPELINE=process` keeps the original ffmpeg child-process path.
pub fn disabled() -> bool {
std::env::var("UU_VIDEO_PIPELINE").is_ok_and(|v| v == "process")
}
fn text(error: ff::Error) -> String { error.to_string() }
fn ensure_init() -> Result<(), String> {
static INIT: Once = Once::new();
let mut result = Ok(());
INIT.call_once(|| {
if let Err(error) = ff::init() { result = Err(text(error)); }
ff::device::register_all();
});
result
}
/// Sends one access unit per encoder packet. Returns `Ok(false)` when the receiver is gone.
fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender<Vec<u8>>) -> Result<bool, String> {
loop {
match encoder.receive_packet(packet) {
Ok(()) => {
let data = packet.data().unwrap_or_default().to_vec();
if units.blocking_send(data).is_err() { return Ok(false); }
}
Err(ff::Error::Other { errno }) if errno == libc::EAGAIN => return Ok(true),
Err(error) => return Err(text(error)),
}
}
}
/// Runs until `stop` is set or the receiver is dropped. A set `idr` flag forces the next frame to be an IDR.
pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> {
ensure_init()?;
let name = CString::new("x11grab").map_err(|e| e.to_string())?;
let raw = unsafe { ff::sys::av_find_input_format(name.as_ptr()) };
if raw.is_null() { return Err("x11grab input is not available in this FFmpeg build".into()); }
let input_format = Format::Input(unsafe { ff::format::format::Input::wrap(raw as *mut _) });
let mut capture_options = Dictionary::new();
capture_options.set("framerate", &config.fps.to_string());
capture_options.set("video_size", &format!("{}x{}", config.input.0, config.input.1));
// The viewer draws its own pointer; do not bake a second one into the video.
capture_options.set("draw_mouse", "0");
let mut input = ff::format::open_with(&config.display, &input_format, capture_options).map_err(text)?.input();
let stream = input.streams().best(ff::media::Type::Video).ok_or("X11 capture has no video stream")?;
let index = stream.index();
let mut decoder = codec::context::Context::from_parameters(stream.parameters()).map_err(text)?.decoder().video().map_err(text)?;
let (width, height) = config.output.unwrap_or(config.input);
let codec = encoder::find_by_name(if config.gpu { "h264_nvenc" } else { "libx264" })
.ok_or("H.264 encoder is not available in this FFmpeg build")?;
// Allocate for this codec so FFmpeg fills its defaults; libx264 rejects a context allocated without one.
let mut context = unsafe { codec::context::Context::wrap(ff::sys::avcodec_alloc_context3(codec.as_ptr()), None) }.encoder().video().map_err(text)?;
context.set_width(width);
context.set_height(height);
context.set_format(Pixel::YUV420P);
context.set_time_base(Rational::new(1, config.fps as i32));
context.set_frame_rate(Some(Rational::new(config.fps as i32, 1)));
context.set_gop(config.gop);
context.set_max_b_frames(0);
context.set_bit_rate(config.target_bps as usize);
context.set_max_bit_rate(config.max_bps as usize);
// The CLI's -maxrate/-bufsize map to these raw fields, which the encoder wrappers read.
unsafe {
let raw = context.as_mut_ptr();
(*raw).rc_max_rate = config.max_bps;
(*raw).rc_buffer_size = config.buffer_bits as i32;
}
let cq = config.cq.to_string();
let gop = config.gop.to_string();
let mut options = Dictionary::new();
if config.gpu {
for (key, value) in [("preset", "p4"), ("tune", "ull"), ("profile", "baseline"), ("rc", "vbr"),
("rc-lookahead", "0"), ("zerolatency", "1"), ("delay", "0"), ("aud", "1"), ("forced-idr", "1")] {
options.set(key, value);
}
options.set("cq", &cq);
} else {
let x264 = format!("aud=1:repeat-headers=1:scenecut=0:keyint={gop}:min-keyint={gop}");
for (key, value) in [("preset", "superfast"), ("tune", "zerolatency"), ("profile", "baseline"), ("threads", "4")] {
options.set(key, value);
}
options.set("crf", &cq);
options.set("x264-params", &x264);
}
let mut encoder = context.open_as_with(codec, options).map_err(text)?;
let mut scaler: Option<(scaling::Context, (Pixel, u32, u32))> = None;
let mut decoded = frame::Video::empty();
let mut yuv = frame::Video::new(Pixel::YUV420P, width, height);
let mut packet = Packet::empty();
let started = Instant::now();
let frame_interval = Duration::from_nanos(1_000_000_000 / u64::from(config.fps));
let mut last_at: Option<Duration> = None;
let mut last_slot: i64 = -1;
for (stream, input_packet) in input.packets() {
if stop.load(Ordering::Relaxed) { return Ok(()); }
if stream.index() != index { continue; }
decoder.send_packet(&input_packet).map_err(text)?;
match decoder.receive_frame(&mut decoded) {
Ok(()) => {}
Err(ff::Error::Other { errno }) if errno == libc::EAGAIN => continue,
Err(error) => return Err(text(error)),
}
// x11grab catches up after a stall by grabbing back-to-back. A frame arriving within half
// a frame interval of the previous one is part of that burst and is dropped, as the old
// `-fps_mode vfr` did. Normal jitter stays well above that threshold.
let now = started.elapsed();
if last_at.is_some_and(|previous| now - previous < frame_interval / 2) { continue; }
last_at = Some(now);
let slot = ((now.as_micros() * u128::from(config.fps) + 500_000) / 1_000_000) as i64;
let slot = slot.max(last_slot + 1);
last_slot = slot;
let key = (decoded.format(), decoded.width(), decoded.height());
if scaler.as_ref().map(|(_, k)| *k) != Some(key) {
let context = scaling::Context::get(key.0, key.1, key.2, Pixel::YUV420P, width, height, scaling::Flags::BILINEAR).map_err(text)?;
scaler = Some((context, key));
}
if let Some((context, _)) = scaler.as_mut() { context.run(&decoded, &mut yuv).map_err(text)?; }
yuv.set_pts(Some(slot));
yuv.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
encoder.send_frame(&yuv).map_err(text)?;
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
}
Ok(())
}