Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve pipewire buffertype handling #61

Merged
merged 5 commits into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/screencast_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ struct xdpw_wlr_output {
};

void randname(char *buf);
int anonymous_shm_open(void);
enum spa_video_format xdpw_format_pw_from_wl_shm(enum wl_shm_format format);
enum spa_video_format xdpw_format_pw_strip_alpha(enum spa_video_format format);

Expand Down
84 changes: 73 additions & 11 deletions src/screencast/pipewire_screencast.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <spa/param/props.h>
#include <spa/param/format-utils.h>
#include <spa/param/video/format-utils.h>
#include <sys/mman.h>
#include <unistd.h>

#include "wlr_screencast.h"
#include "xdpw.h"
Expand Down Expand Up @@ -85,15 +87,6 @@ static void pwr_on_event(void *data, uint64_t expirations) {
h->dts_offset = 0;
}

d[0].type = SPA_DATA_MemPtr;
d[0].maxsize = cast->simple_frame.size;
d[0].mapoffset = 0;
d[0].chunk->size = cast->simple_frame.size;
d[0].chunk->stride = cast->simple_frame.stride;
d[0].chunk->offset = 0;
d[0].flags = 0;
d[0].fd = -1;

writeFrameData(d[0].data, cast->simple_frame.data, cast->simple_frame.height,
cast->simple_frame.stride, cast->simple_frame.y_invert);

Expand Down Expand Up @@ -153,7 +146,8 @@ static void pwr_handle_stream_param_changed(void *data, uint32_t id,
SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(1),
SPA_PARAM_BUFFERS_size, SPA_POD_Int(cast->simple_frame.size),
SPA_PARAM_BUFFERS_stride, SPA_POD_Int(cast->simple_frame.stride),
SPA_PARAM_BUFFERS_align, SPA_POD_Int(XDPW_PWR_ALIGN));
SPA_PARAM_BUFFERS_align, SPA_POD_Int(XDPW_PWR_ALIGN),
SPA_PARAM_BUFFERS_dataType,SPA_POD_CHOICE_FLAGS_Int(1<<SPA_DATA_MemFd));

params[1] = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
Expand All @@ -163,10 +157,78 @@ static void pwr_handle_stream_param_changed(void *data, uint32_t id,
pw_stream_update_params(stream, params, 2);
}

static void pwr_handle_stream_add_buffer(void *data, struct pw_buffer *buffer) {
struct xdpw_screencast_instance *cast = data;
struct spa_data *d;

logprint(TRACE, "pipewire: add buffer event handle");

d = buffer->buffer->datas;

// Select buffer type from negotiation result
if ((d[0].type & (1u << SPA_DATA_MemFd)) > 0) {
d[0].type = SPA_DATA_MemFd;
} else {
logprint(ERROR, "pipewire: unsupported buffer type");
cast->err = 1;
return;
}

logprint(TRACE, "pipewire: selected buffertype %u", d[0].type);
// Prepare buffer for choosen type
if (d[0].type == SPA_DATA_MemFd) {
d[0].maxsize = cast->simple_frame.size;
d[0].mapoffset = 0;
d[0].chunk->size = cast->simple_frame.size;
d[0].chunk->stride = cast->simple_frame.stride;
d[0].chunk->offset = 0;
d[0].flags = 0;
d[0].fd = anonymous_shm_open();

if (d[0].fd == -1) {
logprint(ERROR, "pipewire: unable to create anonymous filedescriptor");
cast->err = 1;
return;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we set cast->err for this error condition? (And the next 2 as well)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah sounds good. Don't know if we should check for that before registering the wlroots callback or somewhere else.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, yeah we should probably check this as soon as we get the broken buffer from somewhere else, which is probably when sending the screencopy request to the compositor. It's unfortunate that add_buffer can't return an error.

}

if (ftruncate(d[0].fd, d[0].maxsize) < 0) {
logprint(ERROR, "pipewire: unable to truncate filedescriptor");
close(d[0].fd);
d[0].fd = -1;
cast->err = 1;
return;
}

// mmap buffer, so we can use the data_ptr in on_process
d[0].data = mmap(NULL, d[0].maxsize, PROT_READ | PROT_WRITE, MAP_SHARED, d[0].fd, d[0].mapoffset);
if (d[0].data == MAP_FAILED) {
logprint(ERROR, "pipewire: unable to mmap memory");
cast->err = 1;
return;
}
}
}

static void pwr_handle_stream_remove_buffer(void *data, struct pw_buffer *buffer) {
logprint(TRACE, "pipewire: remove buffer event handle");

struct spa_data *d = buffer->buffer->datas;
switch (d[0].type) {
case SPA_DATA_MemFd:
munmap(d[0].data, d[0].maxsize);
close(d[0].fd);
break;
default:
break;
}
}

static const struct pw_stream_events pwr_stream_events = {
PW_VERSION_STREAM_EVENTS,
.state_changed = pwr_handle_stream_state_changed,
.param_changed = pwr_handle_stream_param_changed,
.add_buffer = pwr_handle_stream_add_buffer,
.remove_buffer = pwr_handle_stream_remove_buffer,
};

void pwr_update_stream_param(struct xdpw_screencast_instance *cast) {
Expand Down Expand Up @@ -224,7 +286,7 @@ void xdpw_pwr_stream_create(struct xdpw_screencast_instance *cast) {
PW_DIRECTION_OUTPUT,
PW_ID_ANY,
(PW_STREAM_FLAG_DRIVER |
PW_STREAM_FLAG_MAP_BUFFERS),
PW_STREAM_FLAG_ALLOC_BUFFERS),
&param, 1);
}

Expand Down
24 changes: 24 additions & 0 deletions src/screencast/screencast_common.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "screencast_common.h"
#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

void randname(char *buf) {
struct timespec ts;
Expand All @@ -12,6 +17,25 @@ void randname(char *buf) {
}
}

int anonymous_shm_open(void) {
char name[] = "/xdpw-shm-XXXXXX";
int retries = 100;

do {
randname(name + strlen(name) - 6);

--retries;
// shm_open guarantees that O_CLOEXEC is set
int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (fd >= 0) {
shm_unlink(name);
return fd;
}
} while (retries > 0 && errno == EEXIST);

return -1;
}

enum spa_video_format xdpw_format_pw_from_wl_shm(enum wl_shm_format format) {
switch (format) {
case WL_SHM_FORMAT_ARGB8888:
Expand Down
19 changes: 0 additions & 19 deletions src/screencast/wlr_screencast.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,6 @@ void xdpw_wlr_frame_free(struct xdpw_screencast_instance *cast) {
}
}

static int anonymous_shm_open(void) {
char name[] = "/xdpw-shm-XXXXXX";
int retries = 100;

do {
randname(name + strlen(name) - 6);

--retries;
// shm_open guarantees that O_CLOEXEC is set
int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (fd >= 0) {
shm_unlink(name);
return fd;
}
} while (retries > 0 && errno == EEXIST);

return -1;
}

static struct wl_buffer *create_shm_buffer(struct xdpw_screencast_instance *cast,
enum wl_shm_format fmt, int width, int height, int stride,
void **data_out) {
Expand Down