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
feat(capture): auto-enable dmabuf import with shm fallback (#507)
Benjamin Freeman2b18aea54af9
diff --git a/electron/native/pipewire-capture/csrc/pw_shim.c b/electron/native/pipewire-capture/csrc/pw_shim.cindex ece1e67ad..d094e3007 100644--- a/electron/native/pipewire-capture/csrc/pw_shim.c+++ b/electron/native/pipewire-capture/csrc/pw_shim.c@@ -207,6 +207,12 @@ struct osc_pw_session {struct spa_video_info_raw format;int buffer_info_reports;int want_video;+ /* Set by the caller when the VAAPI dmabuf-import pipeline is available, which+ * makes the stream offer dmabuf BEFORE shm so a tiled monitor buffer is+ * imported on the GPU instead of copied through throttled shm (issue #507).+ * shm stays in the offer as the fallback, so a compositor that cannot produce+ * dmabuf still negotiates. */+ int prefer_dmabuf;/* Set from the negotiated format's SPA_VIDEO_FLAG_MODIFIER, which is what* decides whether buffers arrive as dmabuf fds or shared memory. */int uses_dmabuf;@@ -1322,6 +1328,7 @@ static const struct pw_stream_events osc_stream_events = {};struct osc_pw_session *osc_pw_start(int fd, uint32_t node_id, int want_video,+ int prefer_dmabuf,const struct osc_pw_callbacks *callbacks, char *err,size_t err_len){@@ -1347,6 +1354,7 @@ struct osc_pw_session *osc_pw_start(int fd, uint32_t node_id, int want_video,}session->callbacks = *callbacks;session->want_video = want_video;+ session->prefer_dmabuf = prefer_dmabuf;/* calloc zeroes these, and 0 is a legitimate fd — so the "nothing pending"* sentinel has to be set explicitly. dmabuf_maps is keyed on ptr != NULL,* which calloc does get right. */@@ -1405,18 +1413,20 @@ struct osc_pw_session *osc_pw_start(int fd, uint32_t node_id, int want_video,params[1] = osc_build_enum_format_dmabuf(&builder);/*- * Test affordance. Every compositor available for local testing — mutter,- * sway via xdg-desktop-portal-wlr — offers shm, so params[0] always wins and- * the DMA-BUF branch below (osc_map_dmabuf, the DMA_BUF_IOCTL_SYNC bracket,- * the dmabuf arm of osc_read_frame) never executes outside niri. Dropping- * the shm object leaves the producer no choice, which is the only way to- * exercise that code without the compositor from issue #287.+ * When the GPU import path is available (prefer_dmabuf), offer dmabuf FIRST+ * and shm SECOND: mutter then hands us a tiled dmabuf we import on the GPU+ * (issue #507) instead of the shm buffer it throttles for a whole monitor.+ * shm stays as the fallback, so a compositor that cannot produce dmabuf still+ * negotiates on the shm object. The env var forces the same swap for testing+ * on a machine where the probe would say no.*- * Never set in production: it would break exactly the compatibility the- * ordering above exists to preserve.+ * Without either, the ordering is unchanged — shm first — so nothing moves on+ * a build or driver without the VAAPI import.*/- if (getenv("OPENSCREEN_PIPEWIRE_FORCE_DMABUF") != NULL) {+ if (session->prefer_dmabuf || getenv("OPENSCREEN_PIPEWIRE_FORCE_DMABUF") != NULL) {+ const struct spa_pod *shm = params[0];params[0] = params[1];+ params[1] = shm;}if (params[0] == NULL || params[1] == NULL) {diff --git a/electron/native/pipewire-capture/csrc/pw_shim.h b/electron/native/pipewire-capture/csrc/pw_shim.hindex 878f2445c..f4d3a04c3 100644--- a/electron/native/pipewire-capture/csrc/pw_shim.h+++ b/electron/native/pipewire-capture/csrc/pw_shim.h@@ -217,9 +217,14 @@ const char *osc_pw_library_version(void);* buffer types; without it neither happens, and a cursor-only session never pays* to map a full-screen framebuffer per frame.*+ * `prefer_dmabuf` offers dmabuf before shm so a tiled monitor buffer is imported+ * on the GPU rather than copied through throttled shm (issue #507); set it only+ * when the VAAPI import pipeline is available. shm remains the fallback.+ ** Returns NULL on failure, with a message in `err`.*/struct osc_pw_session *osc_pw_start(int fd, uint32_t node_id, int want_video,+ int prefer_dmabuf,const struct osc_pw_callbacks *callbacks, char *err,size_t err_len);diff --git a/electron/native/pipewire-capture/docs/dmabuf-vaapi-plan.md b/electron/native/pipewire-capture/docs/dmabuf-vaapi-plan.mdindex 304187697..e08bb18f3 100644--- a/electron/native/pipewire-capture/docs/dmabuf-vaapi-plan.md+++ b/electron/native/pipewire-capture/docs/dmabuf-vaapi-plan.md@@ -136,8 +136,17 @@ Key architecture calls:buffersrc then set params (hw_frames_ctx) then init, since a HW pix_fmt isrejected at init otherwise; (4) wrap the DRM descriptor in an AVBufferRef so thesource frame is ref-counted for `av_hwframe_map`.-- [ ] Once proven: make dmabuf preference automatic when the backend is VAAPI- (drop the FORCE env gate) + window/crop via VPP + non-VAAPI fallback guard.+- [x] **Auto-enable + fallback** (validated, no env). `dmabuf_import::available()`+ probes once at session start by building a nominal importer; when it succeeds+ the stream offers dmabuf BEFORE shm (`osc_pw_start(prefer_dmabuf)`), else stays+ on shm. shm remains in the offer as the negotiation fallback, so a compositor+ that cannot produce dmabuf — or a GPU where the importer will not build — keeps+ today's path with no regression. Confirmed: full-monitor scroll records 39.8+ distinct fps with `convertMs 0.0` and no force flag. `OPENSCREEN_PIPEWIRE_FORCE_DMABUF`+ still forces the swap for testing.+- [ ] Follow-ups: window/crop via VPP (v1 is whole-monitor); per-frame import+ failure after a successful probe still errors (rare) rather than renegotiating+ to shm; test on Intel/NVIDIA-vaapi.- [ ] Test on AMD/GNOME: confirm `uses_dmabuf=1`, distinct-fps ≈ OBS (~24),crop correct for window captures, cursor unaffected. Regression-checksoftware encode and a wlroots/niri compositor.diff --git a/electron/native/pipewire-capture/src/dmabuf_import.rs b/electron/native/pipewire-capture/src/dmabuf_import.rsindex 260e57878..95febbd11 100644--- a/electron/native/pipewire-capture/src/dmabuf_import.rs+++ b/electron/native/pipewire-capture/src/dmabuf_import.rs@@ -59,6 +59,17 @@ fn sw_format_for_fourcc(drm_fourcc: u32) -> Option<ff::AVPixelFormat> {}}+/// Whether the zero-copy VAAPI dmabuf-import pipeline can be built on this+/// machine. Constructs a nominal importer, which exercises the DRM→VAAPI device+/// creation, the frames contexts and the `scale_vaapi` graph — everything that+/// fails on a non-VAAPI GPU or a driver that cannot map a dmabuf. Success does+/// not depend on the exact dimensions, so a fixed probe size is representative.+/// When this is true the stream prefers dmabuf; when false it stays on shm.+pub fn available() -> bool {+ const XRGB8888: u32 = 0x34325258;+ DmabufImporter::new(1920, 1080, XRGB8888).is_ok()+}+pub struct DmabufImporter {width: i32,height: i32,diff --git a/electron/native/pipewire-capture/src/main.rs b/electron/native/pipewire-capture/src/main.rsindex d12b225f3..61aba8c8f 100644--- a/electron/native/pipewire-capture/src/main.rs+++ b/electron/native/pipewire-capture/src/main.rs@@ -506,6 +506,7 @@ fn begin_stream<W: Write>(portal_stream: &mut Option<StreamInfo>,granted_kind: &mut Option<portal::SourceKind>,stream: portal::PortalStream,+ prefer_dmabuf: bool,) -> Result<(), ()> {// The fd is consumed by libpipewire; the rest is kept for the// `stream-started` event, emitted once the format is negotiated.@@ -518,6 +519,7 @@ fn begin_stream<W: Write>(let _ = forward.send(Message::Stream(event));}),frames.clone(),+ prefer_dmabuf,) {Ok(started) => {*session = Some(started);@@ -577,6 +579,18 @@ fn run<W: Write>(.output_path.as_ref().map(|_| Arc::new(FrameMailbox::default()));+ // Offer dmabuf ahead of shm (issue #507) only for a video session AND only+ // when the VAAPI import pipeline actually builds on this GPU. Probed once+ // here — it constructs a VAAPI device and filtergraph — so a machine that+ // cannot import (no VAAPI, or a driver that will not map) simply keeps the+ // shm path with no per-recording cost.+ let prefer_dmabuf = frames.is_some() && crate::dmabuf_import::available();+ if frames.is_some() {+ let _ = emitter.emit(&Event::Debug {+ code: "dmabuf-import".to_owned(),+ data: json_map([("available", prefer_dmabuf.into())]),+ });+ }let mut capture: Option<Capture> = None;// Started before the portal picker so the streams are warm and the graph// has settled by the time the first video frame arrives. Everything they@@ -798,6 +812,7 @@ fn run<W: Write>(&mut portal_stream,&mut granted_kind,stream,+ prefer_dmabuf,) {exit_code = 1;break;@@ -856,6 +871,7 @@ fn run<W: Write>(&mut portal_stream,&mut granted_kind,stream,+ prefer_dmabuf,) {exit_code = 1;break;diff --git a/electron/native/pipewire-capture/src/shim.rs b/electron/native/pipewire-capture/src/shim.rsindex 6be0424e3..a37693b81 100644--- a/electron/native/pipewire-capture/src/shim.rs+++ b/electron/native/pipewire-capture/src/shim.rs@@ -117,6 +117,7 @@ extern "C" {fd: i32,node_id: u32,want_video: i32,+ prefer_dmabuf: i32,callbacks: *const RawCallbacks,err: *mut c_char,err_len: usize,@@ -827,8 +828,12 @@ impl Session {node_id: u32,sink: Sink,frames: Option<std::sync::Arc<FrameMailbox>>,+ // Offer dmabuf before shm for a whole-monitor GPU import (issue #507).+ // Only honoured for a video session; ignored for cursor-only.+ prefer_dmabuf: bool,) -> Result<Self, String> {let want_video = i32::from(frames.is_some());+ let prefer_dmabuf = i32::from(want_video != 0 && prefer_dmabuf);let state = Box::new(CallbackState { sink, frames });let user = &*state as *const CallbackState as *mut c_void;let callbacks = RawCallbacks {@@ -848,6 +853,7 @@ impl Session {fd.into_raw_fd(),node_id,want_video,+ prefer_dmabuf,&callbacks,err.as_mut_ptr(),ERR_LEN,