fix(capture): map the DMA-BUF frames niri actually sends (#26)

* fix(capture): map the DMA-BUF frames niri actually sends

`uur run` reported "No Wayland frames arrived within 120 seconds" on a
session where the portal handshake succeeded. The session log showed the
stream never leaving negotiation:

    portal session ready (video node 73)
    stream state: connecting
    stream state: paused
    stream state: error - no more input formats
    stream state: unconnected

Three separate defects in `uur-pw-capture`:

1. The format filter omitted `modifier`. niri marks that property
   MANDATORY (SPA flags 0x18), and SPA filtering fails unless both sides
   carry a mandatory property -- which surfaces as "no more input
   formats". The filter now advertises LINEAR, the layout the frame
   protocol already assumes.

2. `on_process` only handled `data != NULL` and `SPA_DATA_MemFd`. niri
   delivers SPA_DATA_DmaBuf planes, which carry an fd and no pointer, so
   every frame was dropped in silence. Since the session negotiates
   LINEAR, that fd is directly mappable without GBM/EGL; the helper now
   mmaps it. A non-linear modifier is refused instead of publishing
   garbage.

3. DMA-BUF producers leave `chunk->size` and `maxsize` at 1. Both the
   mapping length and the row count were derived from those, so the mmap
   covered one byte and `rows` came out zero -- publishing an all-black
   frame. Both now use the negotiated stride and height.

Verified on niri 26.04 with Wine 11.19: the stream reaches `streaming`,
~60 fps (1195 frames in 20 s), and the published frame decodes to the
real desktop rather than black.

* fix(capture): validate DMA-BUFs and recover interrupted screen streams

Use immutable transport files across geometry changes and remap Wine readers safely. Bound stream recovery and preserve portal stop-sharing intent. Refs #29.

* fix(capture): watch Mutter layout changes and support older build headers

* fix(capture): keep display changes outside the failure retry budget

* fix(capture): honor portal stop signals during GNOME recovery

Subscribe before starting on the portal-owned D-Bus connection and accept the real Session.Closed dictionary body. Correlate Mutter's close-before-MonitorsChanged ordering without restarting an explicit Stop Sharing session.

Verified in a private GNOME 51.1 namespace with real GBM DMA-BUF, 100/125 percent scaling, portrait and landscape, and native Stop Sharing. Existing 48 tests and strict clippy pass. Real Wine GDI reader captured 120 final-run frames across four layout changes with no hook fallbacks. No test scripts added.

---------

Co-authored-by: Xuc Pan <[email protected]>
This commit is contained in:
迢辰andXuc Pan authored and GitHub committed 2026-10-15 16:57:34 +08:00
1 parent 306489d8ad
commit a5e2aa44cd
5 files changed
+552 -133

No files matched your search

+236 -57
View File
@@ -16,6 +16,8 @@
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <linux/dma-buf.h>
#include <poll.h>
#include <signal.h>
#include <stdatomic.h>
#include <stdint.h>
@@ -23,14 +25,20 @@
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <pipewire/pipewire.h>
#include <spa/param/video/format-utils.h>
#include <drm/drm_fourcc.h>
#include "../hook/frame-protocol.h"
#define FRAME_FORMAT_BGRX 1
#ifndef SPA_CHUNK_FLAG_EMPTY
#define SPA_CHUNK_FLAG_EMPTY (1u << 1)
#endif
struct frame_header {
uint32_t magic;
@@ -63,16 +71,31 @@ struct capture {
int shm_fd;
uint8_t *shm;
size_t shm_size;
int dmabuf_usable;
int exit_status;
volatile sig_atomic_t stopping;
int was_streaming;
uint64_t started_ms;
uint64_t paused_ms;
char frame_path[PATH_MAX];
};
static struct capture *active_capture;
static uint64_t monotonic_ms(void)
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (uint64_t)now.tv_sec * 1000 + (uint64_t)now.tv_nsec / 1000000;
}
static void stop_capture(int signal_number)
{
(void)signal_number;
if (active_capture != NULL && active_capture->loop != NULL)
if (active_capture != NULL && active_capture->loop != NULL) {
active_capture->stopping = 1;
pw_main_loop_quit(active_capture->loop);
}
}
static struct frame_slot *slot_at(struct capture *c, uint64_t n)
@@ -88,8 +111,8 @@ static uint8_t *slot_frame(struct frame_slot *slot)
return (uint8_t *)slot + sizeof(struct frame_slot);
}
/* Create (or resize) the shared file for the negotiated geometry and write
* the header. Called once, from param_changed, before any frame exists. */
/* Publish a new inode for every geometry. A Wine reader may still map the
* previous inode: never truncate or resize memory another process is reading. */
static int shm_setup(struct capture *c)
{
size_t slot_bytes = sizeof(struct frame_slot) +
@@ -103,26 +126,30 @@ static int shm_setup(struct capture *c)
return -1;
}
c->shm_fd = open(c->frame_path, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC,
0600);
if (c->shm_fd < 0) {
char temporary[PATH_MAX];
if (snprintf(temporary, sizeof(temporary), "%s.XXXXXX", c->frame_path) >=
(int)sizeof(temporary))
return -1;
int fd = mkostemp(temporary, O_CLOEXEC);
if (fd < 0) {
perror("open frame transport");
return -1;
}
if (ftruncate(c->shm_fd, (off_t)total) < 0) {
if (fchmod(fd, 0600) < 0 || ftruncate(fd, (off_t)total) < 0) {
perror("ftruncate");
close(fd);
unlink(temporary);
return -1;
}
c->shm = mmap(NULL, total, PROT_READ | PROT_WRITE, MAP_SHARED,
c->shm_fd, 0);
if (c->shm == MAP_FAILED) {
uint8_t *mapping = mmap(NULL, total, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (mapping == MAP_FAILED) {
perror("mmap");
c->shm = NULL;
close(fd);
unlink(temporary);
return -1;
}
c->shm_size = total;
struct frame_header *header = (struct frame_header *)c->shm;
struct frame_header *header = (struct frame_header *)mapping;
header->magic = 0x46525555u; /* "UURF" */
header->version = UURF_VERSION;
header->width = c->width;
@@ -131,8 +158,29 @@ static int shm_setup(struct capture *c)
header->format = FRAME_FORMAT_BGRX;
atomic_init(&header->total_frames, 0);
for (uint32_t i = 0; i < UURF_SLOT_COUNT; ++i) {
atomic_init(&slot_at(c, (uint64_t)i)->seq, 0);
struct frame_slot *slot = (struct frame_slot *)(mapping +
UURF_HEADER_BYTES + i * slot_bytes);
atomic_init(&slot->seq, 0);
}
if (rename(temporary, c->frame_path) < 0) {
perror("publish frame transport");
munmap(mapping, total);
close(fd);
unlink(temporary);
return -1;
}
if (c->shm != NULL) {
struct frame_header *previous = (struct frame_header *)c->shm;
atomic_store_explicit(&previous->total_frames, 0, memory_order_release);
munmap(c->shm, c->shm_size);
}
if (c->shm_fd >= 0)
close(c->shm_fd);
c->shm = mapping;
c->shm_fd = fd;
c->shm_size = total;
c->counter = 0;
c->started_ms = monotonic_ms();
return 0;
}
@@ -180,6 +228,16 @@ static void publish_frame(struct capture *c, const uint8_t *pixels,
memory_order_release);
}
static int sync_dmabuf(int fd, uint64_t flags)
{
struct dma_buf_sync sync = {.flags = flags};
int result;
do {
result = ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
} while (result < 0 && errno == EINTR);
return result;
}
static void on_process(void *userdata)
{
struct capture *c = userdata;
@@ -193,38 +251,67 @@ static void on_process(void *userdata)
uint8_t *pixels = NULL;
void *mapping = MAP_FAILED;
size_t mapping_size = 0;
int synchronized = 0;
int dmabuf = plane->type == SPA_DATA_DmaBuf;
if (plane->chunk == NULL) {
pw_stream_queue_buffer(c->stream, buffer);
return;
if (plane->chunk == NULL || plane->chunk->stride < 0 ||
(plane->chunk->flags & (SPA_CHUNK_FLAG_CORRUPTED | SPA_CHUNK_FLAG_EMPTY)))
goto done;
uint32_t row_bytes = c->width *
(c->format == SPA_VIDEO_FORMAT_BGR || c->format == SPA_VIDEO_FORMAT_RGB ? 3u : 4u);
uint32_t stride = plane->chunk->stride > 0 ?
(uint32_t)plane->chunk->stride : row_bytes;
uint64_t span = (uint64_t)stride * (c->height - 1) + row_bytes;
uint32_t data_offset = plane->chunk->offset;
if (stride < row_bytes || span > 256u * 1024 * 1024)
goto done;
if (dmabuf) {
// DMA-BUF maxsize/chunk size may be zero or placeholders. Query
// the fd and include BOTH the plane mapping and chunk offsets.
if (!c->dmabuf_usable || plane->fd < 0)
goto done;
off_t extent = lseek((int)plane->fd, 0, SEEK_END);
uint64_t end = (uint64_t)plane->mapoffset + data_offset + span;
if (extent < 0 || end > (uint64_t)extent)
goto done;
struct pollfd ready = {.fd = (int)plane->fd, .events = POLLIN};
if (poll(&ready, 1, 100) <= 0 || !(ready.revents & POLLIN) ||
sync_dmabuf((int)plane->fd, DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ) < 0)
goto done;
synchronized = 1;
} else {
if (plane->maxsize == 0)
goto done;
data_offset %= plane->maxsize;
if ((uint64_t)data_offset + span > plane->maxsize ||
span > plane->chunk->size)
goto done;
}
if (plane->data != NULL) {
pixels = plane->data + plane->chunk->offset;
} else if (plane->type == SPA_DATA_MemFd && plane->fd >= 0) {
pixels = (uint8_t *)plane->data + data_offset;
} else if ((dmabuf || plane->type == SPA_DATA_MemFd) && plane->fd >= 0) {
long page_size = sysconf(_SC_PAGESIZE);
uint64_t offset = (uint64_t)plane->mapoffset + plane->chunk->offset;
uint64_t offset = (uint64_t)plane->mapoffset + data_offset;
uint64_t page_mask = (uint64_t)(page_size > 0 ? page_size : 4096) - 1;
off_t aligned = (off_t)(offset & ~page_mask);
size_t delta = (size_t)(offset - (uint64_t)aligned);
mapping_size = delta + plane->chunk->size;
mapping = mmap(NULL, mapping_size, PROT_READ, MAP_PRIVATE,
mapping_size = delta + (size_t)span;
mapping = mmap(NULL, mapping_size, PROT_READ, MAP_SHARED,
plane->fd, aligned);
if (mapping != MAP_FAILED)
pixels = (uint8_t *)mapping + delta;
}
if (pixels != NULL) {
int32_t signed_stride = plane->chunk->stride;
uint32_t stride = signed_stride > 0
? (uint32_t)signed_stride
: (c->format == SPA_VIDEO_FORMAT_BGR ||
c->format == SPA_VIDEO_FORMAT_RGB
? c->width * 3
: c->width * 4);
uint32_t rows = stride > 0 ? plane->chunk->size / stride : 0;
publish_frame(c, pixels, stride, rows);
if (mapping != MAP_FAILED)
munmap(mapping, mapping_size);
if (c->counter == 0)
fprintf(stderr, "capture buffer: %s, source stride %u, chunk offset %u\n",
dmabuf ? "linear DMA-BUF" : "shared memory", stride, data_offset);
publish_frame(c, pixels, stride, c->height);
}
done:
if (synchronized)
(void)sync_dmabuf((int)plane->fd, DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ);
if (mapping != MAP_FAILED)
munmap(mapping, mapping_size);
}
pw_stream_queue_buffer(c->stream, buffer);
@@ -237,7 +324,7 @@ static void on_param_changed(void *userdata, uint32_t id,
if (param == NULL || id != SPA_PARAM_Format)
return;
struct spa_video_info_raw info;
struct spa_video_info_raw info = {0};
if (spa_format_video_raw_parse(param, &info) < 0)
return;
if (info.format != SPA_VIDEO_FORMAT_BGRx &&
@@ -250,29 +337,92 @@ static void on_param_changed(void *userdata, uint32_t id,
return;
}
if (info.size.width == 0 || info.size.height == 0 ||
info.size.width > 16384 || info.size.height > 16384 ||
(uint64_t)info.size.width * 4 * info.size.height > 256u * 1024 * 1024) {
fprintf(stderr, "unsupported capture geometry\n");
c->exit_status = 1;
pw_main_loop_quit(c->loop);
return;
}
c->width = (uint32_t)info.size.width;
c->height = (uint32_t)info.size.height;
c->format = info.format;
c->stride = SPA_ROUND_UP_N(c->width * 4, 4);
fprintf(stderr, "negotiated: %ux%u stride %u\n", c->width, c->height,
c->stride);
/* The DMA-BUF path mmaps the fd directly, which is only correct for a
* linear layout. A tiled modifier needs a real importer (GBM/EGL), so
* refuse to publish garbage rather than emit a corrupt frame. */
c->dmabuf_usable = spa_pod_find_prop(param, NULL, SPA_FORMAT_VIDEO_modifier) != NULL &&
info.modifier == DRM_FORMAT_MOD_LINEAR;
fprintf(stderr, "negotiated: %ux%u stride %u modifier %#llx%s\n", c->width,
c->height, c->stride, (unsigned long long)info.modifier,
c->dmabuf_usable ? "" : " (DMA-BUF CPU mapping disabled)");
if (c->shm == NULL && shm_setup(c) < 0) {
pw_main_loop_quit(c->loop);
const struct frame_header *header = (const struct frame_header *)c->shm;
if (header == NULL || header->width != c->width || header->height != c->height) {
if (shm_setup(c) < 0) {
c->exit_status = 1;
pw_main_loop_quit(c->loop);
}
}
}
static void on_state_changed(void *userdata, enum pw_stream_state old,
enum pw_stream_state state, const char *error)
{
(void)userdata;
(void)old;
struct capture *c = userdata;
fprintf(stderr, "stream state: %s%s%s\n",
pw_stream_state_as_string(state),
error != NULL && error[0] != '\0' ? " - " : "",
error != NULL ? error : "");
if (c->stopping)
return;
if (state == PW_STREAM_STATE_STREAMING) {
c->was_streaming = 1;
c->paused_ms = 0;
} else if (state == PW_STREAM_STATE_PAUSED && c->was_streaming) {
if (c->paused_ms == 0)
c->paused_ms = monotonic_ms();
} else if (state == PW_STREAM_STATE_ERROR ||
(state == PW_STREAM_STATE_UNCONNECTED && old != state)) {
c->exit_status = 75;
pw_main_loop_quit(c->loop);
}
}
static void on_watchdog(void *userdata, uint64_t expirations)
{
struct capture *c = userdata;
(void)expirations;
uint64_t now = monotonic_ms();
// A static desktop can legitimately produce no new frames while streaming.
// Only a prolonged PAUSED state or a stream with no FIRST frame is fatal.
if (!c->stopping &&
((c->paused_ms != 0 && now - c->paused_ms >= 10000) ||
(c->counter == 0 && now - c->started_ms >= 30000))) {
fprintf(stderr, "capture stream stopped producing usable frames; reconnecting\n");
c->exit_status = 75;
pw_main_loop_quit(c->loop);
}
}
static void on_core_error(void *userdata, uint32_t id, int sequence,
int result, const char *message)
{
struct capture *c = userdata;
(void)sequence;
if (!c->stopping && id == PW_ID_CORE && result < 0) {
fprintf(stderr, "PipeWire connection failed: %s\n", message);
c->exit_status = 75;
pw_main_loop_quit(c->loop);
}
}
static const struct pw_core_events core_events = {
PW_VERSION_CORE_EVENTS,
.error = on_core_error,
};
static const struct pw_stream_events stream_events = {
.version = PW_VERSION_STREAM_EVENTS,
.process = on_process,
@@ -280,6 +430,27 @@ static const struct pw_stream_events stream_events = {
.state_changed = on_state_changed,
};
static const struct spa_pod *capture_format(struct spa_pod_builder *builder,
int dmabuf)
{
struct spa_pod_frame frame;
spa_pod_builder_push_object(builder, &frame, SPA_TYPE_OBJECT_Format,
SPA_PARAM_EnumFormat);
spa_pod_builder_add(builder,
SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video),
SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
SPA_FORMAT_VIDEO_format,
SPA_POD_CHOICE_ENUM_Id(6, SPA_VIDEO_FORMAT_BGRx, SPA_VIDEO_FORMAT_BGRA,
SPA_VIDEO_FORMAT_RGBx, SPA_VIDEO_FORMAT_RGBA,
SPA_VIDEO_FORMAT_BGR, SPA_VIDEO_FORMAT_RGB), 0);
if (dmabuf) {
spa_pod_builder_prop(builder, SPA_FORMAT_VIDEO_modifier,
SPA_POD_PROP_FLAG_MANDATORY);
spa_pod_builder_long(builder, DRM_FORMAT_MOD_LINEAR);
}
return spa_pod_builder_pop(builder, &frame);
}
int main(void)
{
const char *fd_text = getenv("UUR_PW_FD");
@@ -330,6 +501,8 @@ int main(void)
fprintf(stderr, "pw_context_connect_fd failed\n");
return 1;
}
struct spa_hook core_listener;
pw_core_add_listener(core, &core_listener, &core_events, &c);
struct pw_properties *props = pw_properties_new(
PW_KEY_MEDIA_TYPE, "Video", PW_KEY_MEDIA_CATEGORY, "Capture",
@@ -344,39 +517,45 @@ int main(void)
uint8_t pod_buffer[1024];
struct spa_pod_builder pod_builder =
SPA_POD_BUILDER_INIT(pod_buffer, sizeof(pod_buffer));
const struct spa_pod *params[1];
params[0] = spa_pod_builder_add_object(
&pod_builder, SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat,
SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video),
SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
SPA_FORMAT_VIDEO_format,
SPA_POD_CHOICE_ENUM_Id(6, SPA_VIDEO_FORMAT_BGRx,
SPA_VIDEO_FORMAT_BGRA,
SPA_VIDEO_FORMAT_RGBx,
SPA_VIDEO_FORMAT_RGBA,
SPA_VIDEO_FORMAT_BGR,
SPA_VIDEO_FORMAT_RGB));
const struct spa_pod *params[2];
params[0] = capture_format(&pod_builder, 1);
params[1] = capture_format(&pod_builder, 0);
c.started_ms = monotonic_ms();
if (pw_stream_connect(c.stream, PW_DIRECTION_INPUT, node_id,
PW_STREAM_FLAG_MAP_BUFFERS |
PW_STREAM_FLAG_AUTOCONNECT |
PW_STREAM_FLAG_RT_PROCESS,
params, 1) < 0) {
PW_STREAM_FLAG_AUTOCONNECT,
params, 2) < 0) {
fprintf(stderr, "pw_stream_connect failed\n");
return 1;
}
struct spa_source *watchdog = pw_loop_add_timer(pw_main_loop_get_loop(loop),
on_watchdog, &c);
struct timespec interval = {.tv_sec = 1};
if (watchdog == NULL || pw_loop_update_timer(pw_main_loop_get_loop(loop),
watchdog, &interval, &interval, false) < 0) {
fprintf(stderr, "could not start capture watchdog\n");
return 1;
}
pw_main_loop_run(loop);
c.stopping = 1;
active_capture = NULL;
pw_loop_destroy_source(pw_main_loop_get_loop(loop), watchdog);
pw_stream_destroy(c.stream);
spa_hook_remove(&core_listener);
pw_core_disconnect(core);
pw_context_destroy(context);
pw_main_loop_destroy(loop);
if (c.shm != NULL)
if (c.shm != NULL) {
struct frame_header *header = (struct frame_header *)c.shm;
atomic_store_explicit(&header->total_frames, 0, memory_order_release);
munmap(c.shm, c.shm_size);
}
if (c.shm_fd >= 0)
close(c.shm_fd);
unlink(c.frame_path);
pw_deinit();
return 0;
return c.exit_status;
}