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

fix pipewire playback on more modern systems #138

Merged
merged 6 commits into from
Feb 9, 2025
Merged
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
24 changes: 15 additions & 9 deletions plugout_pipewire.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
* gbsplay is a Gameboy sound player
*
* 2024 (C) by Christian Garbs <[email protected]>
* 2024-2025 (C) by Christian Garbs <[email protected]>
* Lisa Riedler <[email protected]>
*
* Licensed under GNU GPL v1 or, at your option, any later version.
*/
Expand Down Expand Up @@ -32,7 +33,7 @@ static const struct pw_stream_events pipewire_stream_events = {

static long pipewire_open(enum plugout_endian *endian, long rate, long *buffer_bytes, const struct plugout_metadata metadata)
{
const struct spa_pod *params[1];
const struct spa_pod *params[2];
uint8_t buffer[1024];
struct pw_properties *props;
struct spa_pod_builder pod_builder = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
Expand Down Expand Up @@ -75,6 +76,10 @@ static long pipewire_open(enum plugout_endian *endian, long rate, long *buffer_b
.format = fmt,
.channels = CHANNELS,
.rate = rate));
// triple-buffer audio
params[1] = spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamBuffers,
SPA_PARAM_Buffers,
SPA_PARAM_BUFFERS_buffers, SPA_POD_Int(3));

// create stream
pipewire_data.stream = pw_stream_new_simple(pw_thread_loop_get_loop(pipewire_data.loop),
Expand All @@ -84,29 +89,32 @@ static long pipewire_open(enum plugout_endian *endian, long rate, long *buffer_b
&pipewire_data);

// connect the stream
// TODO: do we need the realtime flag?
Copy link
Owner

Choose a reason for hiding this comment

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

I did not really know what I was doing when writing this code, but at least I was suspicious of this ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know why the tutorials even use it 🤷🏻‍♀️

if ((err = pw_stream_connect(pipewire_data.stream,
PW_DIRECTION_OUTPUT,
PW_ID_ANY,
PW_STREAM_FLAG_AUTOCONNECT |
PW_STREAM_FLAG_MAP_BUFFERS |
PW_STREAM_FLAG_RT_PROCESS,
params, 1))) {
PW_STREAM_FLAG_MAP_BUFFERS,
params, 2))) {
fprintf(stderr, _("pw_stream_connect failed: %s\n"), spa_strerror(err));
return -1;
}

// run the loop
pw_thread_loop_start(pipewire_data.loop);
if ((err = pw_thread_loop_start(pipewire_data.loop) != 0)) {
fprintf(stderr, _("pw_thread_loop_start failed: %s\n"), spa_strerror(err));
return -1;
}

return 0;
}

static void pipewire_pause(int pause)
{
int err;
pw_thread_loop_lock(pipewire_data.loop);
if ((err = pw_stream_set_active(pipewire_data.stream, !pause)))
fprintf(stderr, _("pw_stream_set_active failed: %s\n"), spa_strerror(err));
pw_thread_loop_unlock(pipewire_data.loop);
}

static ssize_t pipewire_write(const void *buf, size_t count)
Expand Down Expand Up @@ -164,8 +172,6 @@ static ssize_t pipewire_write(const void *buf, size_t count)

static void pipewire_close()
{
pw_thread_loop_wait(pipewire_data.loop);
pw_thread_loop_unlock(pipewire_data.loop);
pw_thread_loop_stop(pipewire_data.loop);
pw_stream_destroy(pipewire_data.stream);
pw_thread_loop_destroy(pipewire_data.loop);
Expand Down