-
Notifications
You must be signed in to change notification settings - Fork 20
/
plugout_pipewire.c
182 lines (150 loc) · 5.05 KB
/
plugout_pipewire.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* gbsplay is a Gameboy sound player
*
* 2024 (C) by Christian Garbs <[email protected]>
*
* Licensed under GNU GPL v1 or, at your option, any later version.
*/
#include <stdbool.h>
#include <time.h>
#include <spa/param/audio/format-utils.h>
#include <spa/utils/result.h>
#include <pipewire/pipewire.h>
#include "common.h"
#include "plugout.h"
static const int BYTES_PER_SAMPLE = 2;
static const int CHANNELS = 2;
static const int STRIDE = BYTES_PER_SAMPLE * CHANNELS;
static struct pipewire_data {
struct pw_thread_loop *loop;
struct pw_stream *stream;
struct timespec buffer_fill_wait_time;
} pipewire_data;
static const struct pw_stream_events pipewire_stream_events = {
PW_VERSION_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];
uint8_t buffer[1024];
struct pw_properties *props;
struct spa_pod_builder pod_builder = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
enum spa_audio_format fmt;
int err;
// determine endianess
switch (*endian) {
case PLUGOUT_ENDIAN_BIG: fmt = SPA_AUDIO_FORMAT_S16_BE; break;
case PLUGOUT_ENDIAN_LITTLE: fmt = SPA_AUDIO_FORMAT_S16_LE; break;
default: fmt = SPA_AUDIO_FORMAT_S16; break;
}
// determine buffer wait time - use 25% gbsplay buffer length (~2 wait cycles on my machine)
pipewire_data.buffer_fill_wait_time.tv_sec = 0;
pipewire_data.buffer_fill_wait_time.tv_nsec =
1000000000 // nanoseconds per second
/ rate // sample rate
* (*buffer_bytes / STRIDE) // samples in buffer
/ 4; // 25% of that
// init pipewire
pw_init(0, NULL);
// set main loop
pipewire_data.loop = pw_thread_loop_new(metadata.player_name, NULL);
// set stream metadata
props = pw_properties_new(PW_KEY_MEDIA_TYPE, "Audio",
PW_KEY_MEDIA_CATEGORY, "Playback",
PW_KEY_MEDIA_ROLE, "Music",
NULL);
// TODO: we could add a PW_KEY_TARGET_OBJECT to select an audio target,
// but we can't yet pass parameters to plugouts.
// set audio format
params[0] = spa_format_audio_raw_build(&pod_builder, SPA_PARAM_EnumFormat,
&SPA_AUDIO_INFO_RAW_INIT(
.format = fmt,
.channels = CHANNELS,
.rate = rate));
// create stream
pipewire_data.stream = pw_stream_new_simple(pw_thread_loop_get_loop(pipewire_data.loop),
metadata.filename,
props,
&pipewire_stream_events,
&pipewire_data);
// connect the stream
// TODO: do we need the realtime flag?
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))) {
fprintf(stderr, _("pw_stream_connect failed: %s\n"), spa_strerror(err));
return -1;
}
// run the loop
pw_thread_loop_start(pipewire_data.loop);
return 0;
}
static void pipewire_pause(int pause)
{
int err;
if ((err = pw_stream_set_active(pipewire_data.stream, !pause)))
fprintf(stderr, _("pw_stream_set_active failed: %s\n"), spa_strerror(err));
}
static ssize_t pipewire_write(const void *buf, size_t count)
{
struct pw_buffer *b;
struct spa_buffer *spa_buf;
int n_frames;
uint8_t *p;
size_t buf_sent = 0;
size_t bytes_to_send;
// repeat until the whole buffer is sent
while (buf_sent < count) {
const int frames_to_send = (count - buf_sent) / STRIDE;
// wait until data can be sent by us
while ((b = pw_stream_dequeue_buffer(pipewire_data.stream)) == NULL) {
nanosleep(&pipewire_data.buffer_fill_wait_time, NULL);
}
// get send buffer
spa_buf = b->buffer;
if ((p = spa_buf->datas[0].data) == NULL) {
nanosleep(&pipewire_data.buffer_fill_wait_time, NULL);
break;
}
// check how much we can send
n_frames = spa_buf->datas[0].maxsize / STRIDE;
#if PW_CHECK_VERSION(0,3,49)
// unfortunately our CI pipeline runs Ubuntu 22.04LTS
// which is on 0.3.48, so we have to do this version check
if (b->requested)
n_frames = SPA_MIN((int)b->requested, n_frames);
#endif
n_frames = SPA_MIN(n_frames, frames_to_send);
bytes_to_send = n_frames * STRIDE;
// send audio data
memcpy(p, ((uint8_t *) buf) + buf_sent, bytes_to_send);
spa_buf->datas[0].chunk->offset = 0;
spa_buf->datas[0].chunk->stride = STRIDE;
spa_buf->datas[0].chunk->size = bytes_to_send;
pw_stream_queue_buffer(pipewire_data.stream, b);
// remember what was sent
buf_sent += bytes_to_send;
}
return 0;
}
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);
pw_deinit();
}
const struct output_plugin plugout_pipewire = {
.name = "pipewire",
.description = "PipeWire sound driver",
.open = pipewire_open,
.pause = pipewire_pause,
.write = pipewire_write,
.close = pipewire_close,
};