Files
rustdesk/libs/scrap
Jericho Siahaya 90f3fb136b fix(linux): capture Wayland portals that offer only DMA-BUF (niri) (#17175)
* fix(linux): capture Wayland portals that offer only DMA-BUF (niri)

niri's screencast stream offers BGRx only as DMA-BUF (linear and tiled
modifiers), with no system-memory format. The appsink accepts only
system-memory BGRx/RGBx, so the PipeWire link fails with "no more input
formats" and the capturer fails with "Element failed to change its
state".

When the system-memory pipeline cannot start, retry with a pipeline
that accepts linear DMA-BUF (XR24/XB24). Linear buffers are CPU
readable, so the frame is read by mmap of the dmabuf fd using the
VideoMeta stride and offset, inside DMA_BUF_IOCTL_SYNC. A normal map
cannot be used: for DMA-BUF the PipeWire chunk size is a placeholder
(1 byte on niri), and always-copy copies only that chunk, so it is not
set on this pipeline.

Desktops where the current pipeline works do not change.

Refs #15911

* scrap/wayland: address review of the DMA-BUF fallback

- Leave the existing system-memory setup in PipeWireRecorder::new as it
  was. Only a fallback hook replaces its set_state(Playing)?, and the
  DMA-BUF pipeline is built by a separate build_dmabuf_pipeline().
- Check every result in read_linear_dmabuf: DMA_BUF_IOCTL_SYNC start and
  end (restarted on EINTR/EAGAIN, as the kernel requires), lseek, mmap.
  Log munmap and pipeline-stop failures.
- Map the whole dmabuf (size from lseek) and bounds-check the frame
  against it in pack_rows, so a bad or negative stride is an error.
- Unit tests for row packing, the bounds check, and the XR24/XB24
  format mapping.

* scrap/wayland: throttle the per-frame DMA-BUF unmap warning

read_linear_dmabuf runs once per frame, so a persistent munmap failure would log a line per frame. Use hbb_common::throttled_log! (one line per minute with a repeat count).

* scrap/wayland: use checked arithmetic for the DMA-BUF frame end

The stride and offset come from the compositor. On 32-bit targets, start + stride * (h - 1) + row_len can wrap to a small value that passes the bounds check, and the row copy then panics. Compute the end with checked_mul/checked_add and return an error on overflow. The existing test gets one case for it.

* scrap/wayland: keep the system-memory capture path unchanged

capture() is back to master's body. A DMA-BUF sample now branches off at
the top into capture_dmabuf(), which has its own drm-format mapping, read,
duplicate check and crop, so GNOME and KDE run the same code as before.

A DMA-BUF frame that does not fit its buffer is now dropped with a trace
log, like the system-memory size mismatch (pipewire#985), instead of
ending the capture. mmap and sync failures are still errors.

* scrap/wayland: drop a DMA-BUF frame whose crop does not fit

The crop rectangle comes from the compositor's VideoCropMeta. One left
from an old allocation could slice past the packed frame and panic, so
capture_dmabuf now drops such a frame with a trace log before reading it.

* scrap/wayland: keep only the tests that pin the reported behavior

Drop the crop_fits test and the usize overflow assertion, which covered corner cases raised in review. The crop_fits check itself stays.
2026-11-23 00:04:20 +08:00
..
2026-09-06 00:21:28 +08:00
2026-06-18 22:37:15 +08:00
…

Derived from https://github.com/quadrupleslap/scrap

scrap

Scrap records your screen! At least it does if you're on Windows, macOS, or Linux.

Usage

[dependencies]
scrap = "0.5"

Its API is as simple as it gets!

struct Display; /// A screen.
struct Frame; /// An array of the pixels that were on-screen.
struct Capturer; /// A recording instance.

impl Capturer {
    /// Begin recording.
    pub fn new(display: Display) -> io::Result<Capturer>;

    /// Try to get a frame.
    /// Returns WouldBlock if it's not ready yet.
    pub fn frame<'a>(&'a mut self) -> io::Result<Frame<'a>>;

    pub fn width(&self) -> usize;
    pub fn height(&self) -> usize;
}

impl Display {
    /// The primary screen.
    pub fn primary() -> io::Result<Display>;

    /// All the screens.
    pub fn all() -> io::Result<Vec<Display>>;

    pub fn width(&self) -> usize;
    pub fn height(&self) -> usize;
}

impl<'a> ops::Deref for Frame<'a> {
    /// A frame is just an array of bytes.
    type Target = [u8];
}

The Frame Format

  • The frame format is guaranteed to be packed BGRA.
  • The width and height are guaranteed to remain constant.
  • The stride might be greater than the width, and it may also vary between frames.

System Requirements

OS Minimum Requirements
macOS macOS 10.8
Linux XCB + SHM + RandR
Windows DirectX 11.1