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 all 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
6 changes: 5 additions & 1 deletion include/pipewire_screencast.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

#include "screencast_common.h"

#define XDPW_PWR_BUFFERS 1
#define XDPW_PWR_BUFFERS 4
#define XDPW_PWR_ALIGN 16

void xdpw_pwr_trigger_process(struct xdpw_screencast_instance *cast);
bool xdpw_pwr_is_driving(struct xdpw_screencast_instance *cast);
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
27 changes: 21 additions & 6 deletions include/screencast_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ enum xdpw_chooser_types {
XDPW_CHOOSER_DMENU,
};

enum xdpw_frame_state {
XDPW_FRAME_STATE_NONE,
XDPW_FRAME_STATE_RENEG,
XDPW_FRAME_STATE_FAILED,
XDPW_FRAME_STATE_SUCCESS,
};

struct xdpw_output_chooser {
enum xdpw_chooser_types type;
char *cmd;
Expand All @@ -42,17 +49,22 @@ struct xdpw_frame_damage {
};

struct xdpw_frame {
uint32_t width;
uint32_t height;
uint32_t size;
uint32_t stride;
bool y_invert;
uint64_t tv_sec;
uint32_t tv_nsec;
enum wl_shm_format format;
struct xdpw_frame_damage damage;
struct wl_buffer *buffer;
void *data;
struct pw_buffer *current_pw_buffer;
};

struct xdpw_screencopy_frame {
uint32_t width;
uint32_t height;
uint32_t size;
uint32_t stride;
enum wl_shm_format format;
};

struct xdpw_screencast_context {
Expand Down Expand Up @@ -83,9 +95,10 @@ struct xdpw_screencast_instance {
uint32_t refcount;
struct xdpw_screencast_context *ctx;
bool initialized;
struct xdpw_frame current_frame;
enum xdpw_frame_state frame_state;

// 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 +112,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 +136,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
3 changes: 2 additions & 1 deletion include/wlr_screencast.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ struct xdpw_wlr_output *xdpw_wlr_output_find(struct xdpw_screencast_context *ctx
struct wl_output *out, uint32_t id);
struct xdpw_wlr_output *xdpw_wlr_output_chooser(struct xdpw_screencast_context *ctx);

void xdpw_wlr_frame_free(struct xdpw_screencast_instance *cast);
void xdpw_wlr_frame_finish(struct xdpw_screencast_instance *cast);
void xdpw_wlr_frame_start(struct xdpw_screencast_instance *cast);
void xdpw_wlr_register_cb(struct xdpw_screencast_instance *cast);

#endif
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ add_project_arguments('-DSYSCONFDIR="@0@"'.format(join_paths(prefix, sysconfdir)
inc = include_directories('include')

rt = cc.find_library('rt')
pipewire = dependency('libpipewire-0.3', version: '>= 0.3.2')
pipewire = dependency('libpipewire-0.3', version: '>= 0.3.34')
wayland_client = dependency('wayland-client')
wayland_protos = dependency('wayland-protocols', version: '>=1.14')
iniparser = dependency('inih')
Expand Down
178 changes: 100 additions & 78 deletions src/screencast/pipewire_screencast.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,12 @@
#include <spa/param/video/format-utils.h>
#include <sys/mman.h>
#include <unistd.h>
#include <assert.h>

#include "wlr_screencast.h"
#include "xdpw.h"
#include "logger.h"

static void writeFrameData(void *pwFramePointer, void *wlrFramePointer,
uint32_t height, uint32_t stride, bool inverted) {
if (!inverted) {
memcpy(pwFramePointer, wlrFramePointer, height * stride);
return;
}

for (size_t i = 0; i < (size_t)height; ++i) {
void *flippedWlrRowPointer = wlrFramePointer + ((height - i - 1) * stride);
void *pwRowPointer = pwFramePointer + (i * stride);
memcpy(pwRowPointer, flippedWlrRowPointer, stride);
}

return;
}

static struct spa_pod *build_format(struct spa_pod_builder *b, enum spa_video_format format,
uint32_t width, uint32_t height, uint32_t framerate) {
struct spa_pod_frame f[1];
Expand Down Expand Up @@ -59,49 +44,12 @@ 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) {
static void pwr_handle_stream_process(void *data) {
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);
logprint(TRACE, "pipewire: stream process");

out:
xdpw_wlr_frame_free(cast);
xdpw_wlr_frame_start(cast);
}

static void pwr_handle_stream_state_changed(void *data,
Expand All @@ -116,6 +64,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_frame_start(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 +95,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,13 +128,14 @@ 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();
d[0].data = NULL;

if (d[0].fd == -1) {
logprint(ERROR, "pipewire: unable to create anonymous filedescriptor");
Expand All @@ -199,23 +151,26 @@ static void pwr_handle_stream_add_buffer(void *data, struct pw_buffer *buffer) {
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;
}
// create wl_buffer
buffer->user_data = import_wl_shm_buffer(cast, d[0].fd, cast->screencopy_frame.format,
cast->screencopy_frame.width, cast->screencopy_frame.height, cast->screencopy_frame.stride);
}
}

static void pwr_handle_stream_remove_buffer(void *data, struct pw_buffer *buffer) {
struct xdpw_screencast_instance *cast = data;

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

struct spa_data *d = buffer->buffer->datas;
if (cast->current_frame.current_pw_buffer == buffer) {
logprint(TRACE, "pipewire: remove buffer currently in use");
cast->current_frame.current_pw_buffer = NULL;
cast->current_frame.buffer = NULL;
}
switch (d[0].type) {
case SPA_DATA_MemFd:
munmap(d[0].data, d[0].maxsize);
wl_buffer_destroy(buffer->user_data);
close(d[0].fd);
break;
default:
Expand All @@ -229,8 +184,80 @@ static const struct pw_stream_events pwr_stream_events = {
.param_changed = pwr_handle_stream_param_changed,
.add_buffer = pwr_handle_stream_add_buffer,
.remove_buffer = pwr_handle_stream_remove_buffer,
.process = pwr_handle_stream_process,
};

void xdpw_pwr_trigger_process(struct xdpw_screencast_instance *cast) {
pw_stream_trigger_process(cast->stream);
}

bool xdpw_pwr_is_driving(struct xdpw_screencast_instance *cast) {
return pw_stream_is_driving(cast->stream);
}
Copy link
Owner

Choose a reason for hiding this comment

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

Do we really need these trivial wrappers?

Copy link
Collaborator Author

@columbarius columbarius Nov 4, 2021

Choose a reason for hiding this comment

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

We need a wrapper for pw_stream_trigger_process, otherwise we cast a function pointer from int (*) to void (*)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We could make xdpw_pwr_trigger_process a noop if we aren't the driver (removing the xdpw_pwr_is_driving wrapper), but i'm not happy with waiting for a noop.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

or we put the whole fps_limit logic into the .process callback so we limit the fps even when we are not the driver. But that's sth. for a follow up imho.


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");
cast->current_frame.buffer = NULL;
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;
cast->current_frame.buffer = cast->current_frame.current_pw_buffer->user_data;
}

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;
bool buffer_corrupt = cast->frame_state != XDPW_FRAME_STATE_SUCCESS;

assert(pw_buf);

struct spa_buffer *spa_buf = pw_buf->buffer;
struct spa_data *d = spa_buf->datas;

if (cast->current_frame.y_invert) {
//TODO: Flip buffer or set stride negative
buffer_corrupt = true;
cast->err = 1;
}

struct spa_meta_header *h;
if ((h = spa_buffer_find_meta_data(spa_buf, SPA_META_Header, sizeof(*h)))) {
h->pts = -1;
h->flags = buffer_corrupt ? SPA_META_HEADER_FLAG_CORRUPTED : 0;
h->seq = cast->seq++;
h->dts_offset = 0;
}

if (buffer_corrupt) {
d[0].chunk->flags = SPA_CHUNK_FLAG_CORRUPTED;
} else {
d[0].chunk->flags = SPA_CHUNK_FLAG_NONE;
}

logprint(TRACE, "********************");
logprint(TRACE, "pipewire: fd %u", d[0].fd);
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->current_frame.y_invert);
logprint(TRACE, "********************");

pw_stream_queue_buffer(cast->stream, pw_buf);

cast->current_frame.current_pw_buffer = NULL;
cast->current_frame.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 +266,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 +296,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
Loading