Skip to content

Commit

Permalink
screencast: pipewire change to self allocation of shm buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
columbarius committed May 27, 2021
1 parent e02a55a commit 437d74c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 26 deletions.
1 change: 1 addition & 0 deletions include/screencast_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ struct xdpw_wlr_output {
};

void randname(char *buf);
int anonymous_shm_open(void);
enum spa_video_format xdpw_format_pw_from_wl_shm(
struct xdpw_screencast_instance *cast);
enum spa_video_format xdpw_format_pw_strip_alpha(enum spa_video_format format);
Expand Down
48 changes: 41 additions & 7 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 @@ -151,7 +153,7 @@ static void pwr_handle_stream_param_changed(void *data, uint32_t id,
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_dataType,SPA_POD_CHOICE_FLAGS_Int(1<<SPA_DATA_MemPtr));
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 @@ -170,28 +172,59 @@ static void pwr_handle_stream_add_buffer(void *data, struct pw_buffer *buffer) {
d = buffer->buffer->datas;

// Select buffer type from negotiation result
if ((d[0].type & (1u << SPA_DATA_MemPtr)) > 0) {
d[0].type = SPA_DATA_MemPtr;
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 %d", d[0].type);
// Prepare buffer for choosen type
if (d[0].type == SPA_DATA_MemPtr) {
if (d[0].type == SPA_DATA_MemFd) {
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 = -1;
d[0].flags = SPA_DATA_FLAG_READWRITE;
d[0].fd = anonymous_shm_open();

if (d[0].fd == -1) {
logprint(ERROR, "pipewire: unable to create anonymous filedescriptor");
return;
}

if (ftruncate(d[0].fd, d[0].maxsize) < 0) {
logprint(ERROR, "pipewire: unable to truncate filedescriptor");
return;
}

// mmap buffer, so we can use the data_ptr int 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");
return;
}
}
}

static void pwr_handle_stream_remove_buffer(void *data, struct pw_buffer *buffer) {
struct spa_data *d;

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

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 = {
Expand Down Expand Up @@ -256,7 +289,8 @@ void xdpw_pwr_stream_create(struct xdpw_screencast_instance *cast) {
pw_stream_connect(cast->stream,
PW_DIRECTION_OUTPUT,
PW_ID_ANY,
PW_STREAM_FLAG_DRIVER,
(PW_STREAM_FLAG_DRIVER |
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(
struct xdpw_screencast_instance *cast) {
switch (cast->simple_frame.format) {
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

0 comments on commit 437d74c

Please sign in to comment.