Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
fix(capture): own the dmabuf plane fds so a destroyed buffer can't corrupt an import
Benjamin Freemaneb209503b363
diff --git a/electron/native/pipewire-capture/src/dmabuf_import.rs b/electron/native/pipewire-capture/src/dmabuf_import.rsindex 19f09ef61..c3dc23553 100644--- a/electron/native/pipewire-capture/src/dmabuf_import.rs+++ b/electron/native/pipewire-capture/src/dmabuf_import.rs@@ -12,6 +12,7 @@//! `avcodec_send_frame` accepts directly. See docs/dmabuf-vaapi-plan.md.use crate::ffmpeg as ff;+use std::os::fd::AsRawFd;use std::ptr;/// `av_frame_free` wants a `**AVFrame`; wrap the pointer in a local so the null@@ -320,11 +321,17 @@ impl DmabufImporter {if frame.planes.is_empty() || frame.planes.len() > 4 {return Err(format!("dmabuf has {} planes", frame.planes.len()));}- // All planes must share one fd: we build a single DRM object from- // planes[0].fd and point every plane at it, so a buffer whose planes span- // multiple fds would make VAAPI read the wrong memory. Our RGB formats are- // single-plane; guard the assumption rather than rely on it.- if frame.planes.iter().any(|plane| plane.fd != frame.planes[0].fd) {+ // We build a single DRM object from planes[0]'s fd and point every plane at+ // it, so all planes must be backed by that one fd. Each plane now owns its+ // own dup (see `DmabufPlane`), so two planes aliasing one buffer no longer+ // share a NUMBER — this therefore accepts only the single-plane case. That is+ // exactly our RGB formats; a genuine multi-plane buffer (never negotiated) is+ // conservatively rejected rather than risk VAAPI reading the wrong memory.+ if frame+ .planes+ .iter()+ .any(|plane| plane.fd.as_raw_fd() != frame.planes[0].fd.as_raw_fd())+ {return Err("dmabuf planes span multiple fds, which this importer does not handle".to_owned());}// SAFETY: every allocated frame/buffer is freed on the error paths and on@@ -340,7 +347,7 @@ impl DmabufImporter {return Err("av_mallocz(drm descriptor) failed".to_owned());}(*desc).nb_objects = 1;- (*desc).objects[0].fd = frame.planes[0].fd;+ (*desc).objects[0].fd = frame.planes[0].fd.as_raw_fd();(*desc).objects[0].size = 0; // recovered by the driver from the fd(*desc).objects[0].format_modifier = frame.modifier;(*desc).nb_layers = 1;diff --git a/electron/native/pipewire-capture/src/shim.rs b/electron/native/pipewire-capture/src/shim.rsindex bcf8dfe08..68b58140f 100644--- a/electron/native/pipewire-capture/src/shim.rs+++ b/electron/native/pipewire-capture/src/shim.rs@@ -10,7 +10,7 @@//! letting a Rust panic unwind into C.use std::ffi::{c_char, c_void, CStr};-use std::os::fd::{IntoRawFd, OwnedFd};+use std::os::fd::{BorrowedFd, IntoRawFd, OwnedFd};use std::panic::{catch_unwind, AssertUnwindSafe};#[repr(C)]@@ -267,11 +267,14 @@ impl Drop for DmabufDesc {}}-/// One dmabuf plane: a BORROWED fd (owned by the still-held PipeWire buffer) plus-/// its layout. Valid until the buffer is re-queued.+/// One dmabuf plane: an OWNED (dup'd) fd plus its layout. The dup is taken when the+/// frame is claimed and closed when the owning `DmabufDesc` drops. Owning it — not+/// borrowing the PipeWire buffer's fd — is what keeps the plane valid if a+/// renegotiation destroys the buffer set (which closes the original fds and reuses+/// the numbers) while this plane is still queued for import.#[derive(Debug)]pub struct DmabufPlane {- pub fd: i32,+ pub fd: OwnedFd,pub offset: i32,pub stride: i32,}@@ -1050,7 +1053,24 @@ fn on_frame_inner(state: &CallbackState, frame: *const RawFrame) -> i32 {if fd < 0 {return 0;}- planes.push(DmabufPlane { fd, offset: frame.plane_offset[i], stride: frame.plane_stride[i] });+ // Dup the plane fd so the descriptor owns a handle independent of the+ // PipeWire buffer's lifetime: if a renegotiation destroys the buffer set+ // while this desc is still queued for import, the original fds are closed+ // and their numbers reused, and a borrowed fd would then import an+ // unrelated buffer. The dup keeps the dmabuf alive until the OwnedFd drops+ // with the desc, after `Capture::stage`; VAAPI dups again during surface+ // creation, so it costs nothing past import.+ // SAFETY: `fd` is valid for this callback; `try_clone_to_owned` dups it.+ let Ok(owned) = (unsafe { BorrowedFd::borrow_raw(fd) }).try_clone_to_owned() else {+ // fd exhaustion: decline. `planes` drops here, closing the dups taken+ // so far, and the shim re-queues the buffer.+ return 0;+ };+ planes.push(DmabufPlane {+ fd: owned,+ offset: frame.plane_offset[i],+ stride: frame.plane_stride[i],+ });}mailbox.put_dmabuf(DmabufDesc {