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

use pipewire buffers #141

Merged
merged 19 commits into from
Nov 6, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
2323296
screencast: pipewire add functions to dequeue and enqueue a buffer
columbarius May 27, 2021
5216916
screencast: use dequeue/enqueue functions instead of on_process event…
columbarius May 27, 2021
68d938f
screencast: remove pipewire on_event callback
columbarius May 27, 2021
5b8b189
screencast: pipewire add import_wl_shm_buffer function
columbarius May 27, 2021
e726cda
screencast: rename xdpw_frame into xdpw_screencopy_frame
columbarius Jun 16, 2021
444aac9
screencast: only restart wlroots loop if stream is active
columbarius Jun 16, 2021
d7cc408
screencast: implement buffer property checks wrt. xdpw_frame and xdpw…
columbarius Jun 16, 2021
c822461
screencast: use pipewire buffers directly for wlroots screencopy
columbarius Jun 16, 2021
5e69384
screencast: cleanup screencopy_frame
columbarius Jun 16, 2021
ff9965d
screencast: only end the fps measurement when it was started before
columbarius Jul 6, 2021
804920e
screencast: reorder screencopy callbacks
columbarius Aug 18, 2021
c4e98b8
screencast: introduce xdpw_frame_state and xdpw_wlr_stream_finish
columbarius Aug 18, 2021
a56312a
screencast: drop imported PipeWire buffer on remove_buffer
columbarius Nov 3, 2021
32b55d3
screencast: don't fail when copy_buffer was unsuccessfull
columbarius Nov 4, 2021
91c53a9
screencast: mark buffer as invalid when screencopy fails
columbarius Aug 18, 2021
ca00e96
screencast: use asserts for fatal errors
columbarius Aug 18, 2021
f5d62fc
meson: bump required pipewire version to 0.3.34
columbarius Sep 9, 2021
ac2c3fe
screencast: use the pipewire callback process to start screencast
columbarius Sep 9, 2021
b224a75
screencast: bump the prefered amount of pipewire buffers to 4
columbarius Sep 9, 2021
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
2 changes: 2 additions & 0 deletions include/pipewire_screencast.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#define XDPW_PWR_BUFFERS 1
#define XDPW_PWR_ALIGN 16

void xdpw_pwr_dequeue_buffer(struct xdpw_screencast_instance *cast);
void xdpw_pwr_enqueue_buffer(struct xdpw_screencast_instance *cast);
void pwr_update_stream_param(struct xdpw_screencast_instance *cast);
void xdpw_pwr_stream_create(struct xdpw_screencast_instance *cast);
void xdpw_pwr_stream_destroy(struct xdpw_screencast_instance *cast);
Expand Down
17 changes: 15 additions & 2 deletions include/screencast_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ struct xdpw_frame_damage {
};

struct xdpw_frame {
uint32_t size;
uint32_t stride;
bool y_invert;
uint64_t tv_sec;
uint32_t tv_nsec;
struct xdpw_frame_damage damage;
struct wl_buffer *buffer;
struct pw_buffer *current_pw_buffer;
};

struct xdpw_screencopy_frame {
uint32_t width;
uint32_t height;
uint32_t size;
Expand Down Expand Up @@ -83,9 +94,9 @@ struct xdpw_screencast_instance {
uint32_t refcount;
struct xdpw_screencast_context *ctx;
bool initialized;
struct xdpw_frame current_frame;

// pipewire
struct spa_source *event;
struct pw_stream *stream;
struct spa_hook stream_listener;
struct spa_video_info_raw pwr_format;
Expand All @@ -99,7 +110,7 @@ struct xdpw_screencast_instance {
struct xdpw_wlr_output *target_output;
uint32_t max_framerate;
struct zwlr_screencopy_frame_v1 *wlr_frame;
struct xdpw_frame simple_frame;
struct xdpw_screencopy_frame screencopy_frame;
bool with_cursor;
int err;
bool quit;
Expand All @@ -123,6 +134,8 @@ struct xdpw_wlr_output {

void randname(char *buf);
int anonymous_shm_open(void);
struct wl_buffer *import_wl_shm_buffer(struct xdpw_screencast_instance *cast, int fd,
enum wl_shm_format fmt, int width, int height, int stride);
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
129 changes: 70 additions & 59 deletions src/screencast/pipewire_screencast.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <spa/param/video/format-utils.h>
#include <sys/mman.h>
#include <unistd.h>
#include <assert.h>

#include "wlr_screencast.h"
#include "xdpw.h"
Expand Down Expand Up @@ -59,51 +60,6 @@ static struct spa_pod *build_format(struct spa_pod_builder *b, enum spa_video_fo
return spa_pod_builder_pop(b, &f[0]);
}

static void pwr_on_event(void *data, uint64_t expirations) {
struct xdpw_screencast_instance *cast = data;
struct pw_buffer *pw_buf;
struct spa_buffer *spa_buf;
struct spa_meta_header *h;
struct spa_data *d;

logprint(TRACE, "********************");
logprint(TRACE, "pipewire: event fired");

if ((pw_buf = pw_stream_dequeue_buffer(cast->stream)) == NULL) {
logprint(WARN, "pipewire: out of buffers");
goto out;
}

spa_buf = pw_buf->buffer;
d = spa_buf->datas;
if ((d[0].data) == NULL) {
logprint(TRACE, "pipewire: data pointer undefined");
goto out;
}
if ((h = spa_buffer_find_meta_data(spa_buf, SPA_META_Header, sizeof(*h)))) {
h->pts = -1;
h->flags = 0;
h->seq = cast->seq++;
h->dts_offset = 0;
}

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

logprint(TRACE, "pipewire: pointer %p", d[0].data);
logprint(TRACE, "pipewire: size %d", d[0].maxsize);
logprint(TRACE, "pipewire: stride %d", d[0].chunk->stride);
logprint(TRACE, "pipewire: width %d", cast->simple_frame.width);
logprint(TRACE, "pipewire: height %d", cast->simple_frame.height);
logprint(TRACE, "pipewire: y_invert %d", cast->simple_frame.y_invert);
logprint(TRACE, "********************");

pw_stream_queue_buffer(cast->stream, pw_buf);

out:
xdpw_wlr_frame_free(cast);
}

static void pwr_handle_stream_state_changed(void *data,
enum pw_stream_state old, enum pw_stream_state state, const char *error) {
struct xdpw_screencast_instance *cast = data;
Expand All @@ -116,6 +72,9 @@ static void pwr_handle_stream_state_changed(void *data,
switch (state) {
case PW_STREAM_STATE_STREAMING:
cast->pwr_stream_state = true;
if (!cast->wlr_frame) {
xdpw_wlr_register_cb(cast);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Checked every commit again and found a race condition only in screencast: only restart wlroots loop if stream is active
when PipeWire sends a state_changed changing the state to PW_STATE_STREAMING in between the buffer_done and the ready/failed events of the screencopy loop. This did trigger a new screencopy loop entrance while the old wasn't finished and triggered an assertion in xdpw_pwr_dequeue_buffer.

Checking if NO screencopy loop is currently running via cast->wlr_frame == NULL solved this issue.

break;
default:
cast->pwr_stream_state = false;
Expand Down Expand Up @@ -144,8 +103,8 @@ static void pwr_handle_stream_param_changed(void *data, uint32_t id,
SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers,
SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(XDPW_PWR_BUFFERS, 1, 32),
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_size, SPA_POD_Int(cast->screencopy_frame.size),
SPA_PARAM_BUFFERS_stride, SPA_POD_Int(cast->screencopy_frame.stride),
SPA_PARAM_BUFFERS_align, SPA_POD_Int(XDPW_PWR_ALIGN),
SPA_PARAM_BUFFERS_dataType,SPA_POD_CHOICE_FLAGS_Int(1<<SPA_DATA_MemFd));

Expand Down Expand Up @@ -177,10 +136,10 @@ static void pwr_handle_stream_add_buffer(void *data, struct pw_buffer *buffer) {
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].maxsize = cast->screencopy_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->size = cast->screencopy_frame.size;
d[0].chunk->stride = cast->screencopy_frame.stride;
d[0].chunk->offset = 0;
d[0].flags = 0;
d[0].fd = anonymous_shm_open();
Expand Down Expand Up @@ -231,6 +190,63 @@ static const struct pw_stream_events pwr_stream_events = {
.remove_buffer = pwr_handle_stream_remove_buffer,
};

void xdpw_pwr_dequeue_buffer(struct xdpw_screencast_instance *cast) {
logprint(TRACE, "pipewire: dequeueing buffer");

assert(cast->current_frame.current_pw_buffer == NULL);
if ((cast->current_frame.current_pw_buffer = pw_stream_dequeue_buffer(cast->stream)) == NULL) {
logprint(WARN, "pipewire: out of buffers");
return;
}

struct spa_buffer *spa_buf = cast->current_frame.current_pw_buffer->buffer;
struct spa_data *d = spa_buf->datas;
cast->current_frame.size = d[0].chunk->size;
cast->current_frame.stride = d[0].chunk->stride;
}

void xdpw_pwr_enqueue_buffer(struct xdpw_screencast_instance *cast) {
logprint(TRACE, "pipewire: exporting buffer");

struct pw_buffer *pw_buf = cast->current_frame.current_pw_buffer;

if (!pw_buf) {
logprint(TRACE, "pipewire: no pipewire buffer to queue");
return;
}

struct spa_buffer *spa_buf = pw_buf->buffer;
struct spa_data *d = spa_buf->datas;
struct spa_meta_header *h;
if ((h = spa_buffer_find_meta_data(spa_buf, SPA_META_Header, sizeof(*h)))) {
h->pts = -1;
h->flags = 0;
h->seq = cast->seq++;
h->dts_offset = 0;
}
if (d[0].data == NULL) {
logprint(TRACE, "pipewire: data pointer undefined");
goto queue;
}

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

logprint(TRACE, "********************");
logprint(TRACE, "pipewire: pointer %p", d[0].data);
logprint(TRACE, "pipewire: size %d", d[0].maxsize);
logprint(TRACE, "pipewire: stride %d", d[0].chunk->stride);
logprint(TRACE, "pipewire: width %d", cast->screencopy_frame.width);
logprint(TRACE, "pipewire: height %d", cast->screencopy_frame.height);
logprint(TRACE, "pipewire: y_invert %d", cast->screencopy_frame.y_invert);
logprint(TRACE, "********************");

queue:
pw_stream_queue_buffer(cast->stream, pw_buf);

cast->current_frame.current_pw_buffer = NULL;
}

void pwr_update_stream_param(struct xdpw_screencast_instance *cast) {
logprint(TRACE, "pipewire: stream update parameters");
struct pw_stream *stream = cast->stream;
Expand All @@ -239,10 +255,10 @@ void pwr_update_stream_param(struct xdpw_screencast_instance *cast) {
SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
const struct spa_pod *params[1];

enum spa_video_format format = xdpw_format_pw_from_wl_shm(cast->simple_frame.format);
enum spa_video_format format = xdpw_format_pw_from_wl_shm(cast->screencopy_frame.format);

params[0] = build_format(&b, format,
cast->simple_frame.width, cast->simple_frame.height, cast->framerate);
cast->screencopy_frame.width, cast->screencopy_frame.height, cast->framerate);

pw_stream_update_params(stream, params, 1);
}
Expand All @@ -269,15 +285,10 @@ void xdpw_pwr_stream_create(struct xdpw_screencast_instance *cast) {
}
cast->pwr_stream_state = false;

/* make an event to signal frame ready */
cast->event =
pw_loop_add_event(state->pw_loop, pwr_on_event, cast);
logprint(DEBUG, "pipewire: registered event %p", cast->event);

enum spa_video_format format = xdpw_format_pw_from_wl_shm(cast->simple_frame.format);
enum spa_video_format format = xdpw_format_pw_from_wl_shm(cast->screencopy_frame.format);

const struct spa_pod *param = build_format(&b, format,
cast->simple_frame.width, cast->simple_frame.height, cast->framerate);
cast->screencopy_frame.width, cast->screencopy_frame.height, cast->framerate);

pw_stream_add_listener(cast->stream, &cast->stream_listener,
&pwr_stream_events, cast);
Expand Down
2 changes: 1 addition & 1 deletion src/screencast/screencast.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ static int method_screencast_start(sd_bus_message *msg, void *data,
"streams", "a(ua{sv})", 1,
cast->node_id, 2,
"position", "(ii)", 0, 0,
"size", "(ii)", cast->simple_frame.width, cast->simple_frame.height);
"size", "(ii)", cast->screencopy_frame.width, cast->screencopy_frame.height);

if (ret < 0) {
return ret;
Expand Down
17 changes: 17 additions & 0 deletions src/screencast/screencast_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ int anonymous_shm_open(void) {
return -1;
}

struct wl_buffer *import_wl_shm_buffer(struct xdpw_screencast_instance *cast, int fd,
enum wl_shm_format fmt, int width, int height, int stride) {
struct xdpw_screencast_context *ctx = cast->ctx;
int size = stride * height;

if (fd < 0) {
return NULL;
}

struct wl_shm_pool *pool = wl_shm_create_pool(ctx->shm, fd, size);
struct wl_buffer *buffer =
wl_shm_pool_create_buffer(pool, 0, width, height, stride, fmt);
wl_shm_pool_destroy(pool);

return buffer;
}

enum spa_video_format xdpw_format_pw_from_wl_shm(enum wl_shm_format format) {
switch (format) {
case WL_SHM_FORMAT_ARGB8888:
Expand Down
Loading