| 1 |
|
| 2 |
/ |
| 3 |
|
| 4 |
|
| 5 |
#define __USE_GNU |
| 6 |
|
| 7 |
#include <limits.h> |
| 8 |
#include <sys/shm.h> |
| 9 |
#include <sys/types.h> |
| 10 |
#include <sys/socket.h> |
| 11 |
#include <sys/mman.h> |
| 12 |
|
| 13 |
#include <alsa/asoundlib.h> |
| 14 |
#include <alsa/pcm_external.h> |
| 15 |
|
| 16 |
#include <spa/param/audio/format-utils.h> |
| 17 |
#include <spa/debug/types.h> |
| 18 |
#include <spa/param/props.h> |
| 19 |
#include <spa/utils/atomic.h> |
| 20 |
#include <spa/utils/endian.h> |
| 21 |
#include <spa/utils/result.h> |
| 22 |
#include <spa/utils/string.h> |
| 23 |
#include <spa/utils/json.h> |
| 24 |
|
| 25 |
#include <pipewire/pipewire.h> |
| 26 |
|
| 27 |
PW_LOG_TOPIC_STATIC(alsa_log_topic, "alsa.pcm"); |
| 28 |
#define PW_LOG_TOPIC_DEFAULT alsa_log_topic |
| 29 |
|
| 30 |
#define MIN_BUFFERS 2u |
| 31 |
#define MAX_BUFFERS 64u |
| 32 |
|
| 33 |
#define MAX_RATE (48000*8) |
| 34 |
#define MAX_CHANNELS SPA_AUDIO_MAX_CHANNELS |
| 35 |
|
| 36 |
#define MIN_PERIOD 64 |
| 37 |
|
| 38 |
#define MIN_PERIOD_BYTES (128) |
| 39 |
#define MAX_PERIOD_BYTES (2*1024*1024) |
| 40 |
|
| 41 |
#define MIN_BUFFER_BYTES (2*MIN_PERIOD_BYTES) |
| 42 |
#define MAX_BUFFER_BYTES (2*MAX_PERIOD_BYTES) |
| 43 |
|
| 44 |
typedef struct { |
| 45 |
snd_pcm_ioplug_t io; |
| 46 |
|
| 47 |
snd_output_t *output; |
| 48 |
FILE *log_file; |
| 49 |
|
| 50 |
int fd; |
| 51 |
int error; |
| 52 |
unsigned int activated:1; |
| 53 |
unsigned int drained:1; |
| 54 |
unsigned int draining:1; |
| 55 |
unsigned int xrun_detected:1; |
| 56 |
unsigned int hw_params_changed:1; |
| 57 |
unsigned int negotiated:1; |
| 58 |
|
| 59 |
snd_pcm_uframes_t hw_ptr; |
| 60 |
snd_pcm_uframes_t boundary; |
| 61 |
snd_pcm_uframes_t min_avail; |
| 62 |
unsigned int sample_bits; |
| 63 |
uint32_t blocks; |
| 64 |
uint32_t stride; |
| 65 |
|
| 66 |
struct spa_system *system; |
| 67 |
struct pw_thread_loop *main_loop; |
| 68 |
|
| 69 |
struct pw_properties *props; |
| 70 |
struct pw_context *context; |
| 71 |
|
| 72 |
struct pw_core *core; |
| 73 |
struct spa_hook core_listener; |
| 74 |
|
| 75 |
struct pw_stream *stream; |
| 76 |
struct spa_hook stream_listener; |
| 77 |
|
| 78 |
int64_t delay; |
| 79 |
uint64_t transferred; |
| 80 |
uint64_t buffered; |
| 81 |
int64_t now; |
| 82 |
uintptr_t seq; |
| 83 |
|
| 84 |
struct spa_audio_info requested; |
| 85 |
struct spa_audio_info format; |
| 86 |
} snd_pcm_pipewire_t; |
| 87 |
|
| 88 |
static int snd_pcm_pipewire_stop(snd_pcm_ioplug_t *io); |
| 89 |
|
| 90 |
static int update_active(snd_pcm_ioplug_t *io) |
| 91 |
{ |
| 92 |
snd_pcm_pipewire_t *pw = io->private_data; |
| 93 |
snd_pcm_sframes_t avail; |
| 94 |
bool active; |
| 95 |
uint64_t val; |
| 96 |
|
| 97 |
avail = snd_pcm_ioplug_avail(io, pw->hw_ptr, io->appl_ptr); |
| 98 |
|
| 99 |
if (pw->error > 0) { |
| 100 |
active = true; |
| 101 |
} |
| 102 |
else if (io->state == SND_PCM_STATE_DRAINING) { |
| 103 |
active = pw->drained; |
| 104 |
} |
| 105 |
else if (avail >= 0 && avail < (snd_pcm_sframes_t)pw->min_avail) { |
| 106 |
active = false; |
| 107 |
} |
| 108 |
else if (avail >= (snd_pcm_sframes_t)pw->min_avail) { |
| 109 |
active = true; |
| 110 |
} |
| 111 |
else { |
| 112 |
active = false; |
| 113 |
} |
| 114 |
|
| 115 |
pw_log_trace("%p: avail:%lu min-avail:%lu state:%s hw:%lu appl:%lu active:%d state:%s", |
| 116 |
pw, avail, pw->min_avail, snd_pcm_state_name(io->state), |
| 117 |
pw->hw_ptr, io->appl_ptr, active, |
| 118 |
snd_pcm_state_name(io->state)); |
| 119 |
|
| 120 |
if (active) |
| 121 |
spa_system_eventfd_write(pw->system, io->poll_fd, 1); |
| 122 |
else |
| 123 |
spa_system_eventfd_read(pw->system, io->poll_fd, &val); |
| 124 |
|
| 125 |
return active; |
| 126 |
} |
| 127 |
|
| 128 |
static void snd_pcm_pipewire_free(snd_pcm_pipewire_t *pw) |
| 129 |
{ |
| 130 |
if (pw == NULL) |
| 131 |
return; |
| 132 |
|
| 133 |
pw_log_debug("%p: free", pw); |
| 134 |
if (pw->main_loop) |
| 135 |
pw_thread_loop_stop(pw->main_loop); |
| 136 |
if (pw->stream) |
| 137 |
pw_stream_destroy(pw->stream); |
| 138 |
if (pw->context) |
| 139 |
pw_context_destroy(pw->context); |
| 140 |
if (pw->fd >= 0) |
| 141 |
spa_system_close(pw->system, pw->fd); |
| 142 |
if (pw->main_loop) |
| 143 |
pw_thread_loop_destroy(pw->main_loop); |
| 144 |
pw_properties_free(pw->props); |
| 145 |
snd_output_close(pw->output); |
| 146 |
fclose(pw->log_file); |
| 147 |
free(pw); |
| 148 |
} |
| 149 |
|
| 150 |
static int snd_pcm_pipewire_close(snd_pcm_ioplug_t *io) |
| 151 |
{ |
| 152 |
snd_pcm_pipewire_t *pw = io->private_data; |
| 153 |
pw_log_debug("%p: close", pw); |
| 154 |
snd_pcm_pipewire_free(pw); |
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
static int snd_pcm_pipewire_poll_revents(snd_pcm_ioplug_t *io, |
| 159 |
struct pollfd *pfds, unsigned int nfds, |
| 160 |
unsigned short *revents) |
| 161 |
{ |
| 162 |
snd_pcm_pipewire_t *pw = io->private_data; |
| 163 |
|
| 164 |
assert(pfds && nfds == 1 && revents); |
| 165 |
|
| 166 |
if (pw->error < 0) |
| 167 |
return pw->error; |
| 168 |
|
| 169 |
*revents = pfds[0].revents & ~(POLLIN | POLLOUT); |
| 170 |
if (pfds[0].revents & POLLIN && update_active(io)) |
| 171 |
*revents |= (io->stream == SND_PCM_STREAM_PLAYBACK) ? POLLOUT : POLLIN; |
| 172 |
|
| 173 |
pw_log_trace_fp("poll %d", *revents); |
| 174 |
|
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
static snd_pcm_sframes_t snd_pcm_pipewire_pointer(snd_pcm_ioplug_t *io) |
| 179 |
{ |
| 180 |
snd_pcm_pipewire_t *pw = io->private_data; |
| 181 |
if (pw->xrun_detected) |
| 182 |
return -EPIPE; |
| 183 |
if (pw->error < 0) |
| 184 |
return pw->error; |
| 185 |
if (io->buffer_size == 0) |
| 186 |
return 0; |
| 187 |
#ifdef SND_PCM_IOPLUG_FLAG_BOUNDARY_WA |
| 188 |
return pw->hw_ptr; |
| 189 |
#else |
| 190 |
return pw->hw_ptr % io->buffer_size; |
| 191 |
#endif |
| 192 |
} |
| 193 |
|
| 194 |
static int snd_pcm_pipewire_delay(snd_pcm_ioplug_t *io, snd_pcm_sframes_t *delayp) |
| 195 |
{ |
| 196 |
snd_pcm_pipewire_t *pw = io->private_data; |
| 197 |
uintptr_t seq1, seq2; |
| 198 |
int64_t elapsed = 0, delay, now, avail; |
| 199 |
int64_t diff; |
| 200 |
|
| 201 |
do { |
| 202 |
seq1 = SPA_SEQ_READ(pw->seq); |
| 203 |
|
| 204 |
delay = pw->delay + pw->transferred; |
| 205 |
now = pw->now; |
| 206 |
if (io->stream == SND_PCM_STREAM_PLAYBACK) |
| 207 |
avail = snd_pcm_ioplug_hw_avail(io, pw->hw_ptr, io->appl_ptr); |
| 208 |
else |
| 209 |
avail = snd_pcm_ioplug_avail(io, pw->hw_ptr, io->appl_ptr); |
| 210 |
|
| 211 |
seq2 = SPA_SEQ_READ(pw->seq); |
| 212 |
} while (!SPA_SEQ_READ_SUCCESS(seq1, seq2)); |
| 213 |
|
| 214 |
if (now != 0 && (io->state == SND_PCM_STATE_RUNNING || |
| 215 |
io->state == SND_PCM_STATE_DRAINING)) { |
| 216 |
diff = pw_stream_get_nsec(pw->stream) - now; |
| 217 |
elapsed = (io->rate * diff) / SPA_NSEC_PER_SEC; |
| 218 |
|
| 219 |
if (io->stream == SND_PCM_STREAM_PLAYBACK) |
| 220 |
delay -= SPA_MIN(elapsed, delay); |
| 221 |
else |
| 222 |
delay += SPA_MIN(elapsed, (int64_t)io->buffer_size); |
| 223 |
} |
| 224 |
|
| 225 |
*delayp = delay + avail; |
| 226 |
|
| 227 |
pw_log_trace("avail:%"PRIi64" filled %"PRIi64" elapsed:%"PRIi64" delay:%ld hw:%lu appl:%lu", |
| 228 |
avail, delay, elapsed, *delayp, pw->hw_ptr, io->appl_ptr); |
| 229 |
|
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
static snd_pcm_uframes_t |
| 234 |
snd_pcm_pipewire_process(snd_pcm_pipewire_t *pw, struct pw_buffer *b, |
| 235 |
snd_pcm_uframes_t *hw_avail,snd_pcm_uframes_t want) |
| 236 |
{ |
| 237 |
snd_pcm_ioplug_t *io = &pw->io; |
| 238 |
snd_pcm_channel_area_t *pwareas; |
| 239 |
snd_pcm_uframes_t xfer = 0; |
| 240 |
snd_pcm_uframes_t nframes; |
| 241 |
unsigned int channel; |
| 242 |
struct spa_data *d; |
| 243 |
void *ptr; |
| 244 |
uint32_t bl, offset, size; |
| 245 |
|
| 246 |
d = b->buffer->datas; |
| 247 |
pwareas = alloca(io->channels * sizeof(snd_pcm_channel_area_t)); |
| 248 |
|
| 249 |
for (bl = 0; bl < pw->blocks; bl++) { |
| 250 |
if (io->stream == SND_PCM_STREAM_PLAYBACK) { |
| 251 |
size = SPA_MIN(d[bl].maxsize, pw->min_avail * pw->stride); |
| 252 |
} else { |
| 253 |
offset = SPA_MIN(d[bl].chunk->offset, d[bl].maxsize); |
| 254 |
size = SPA_MIN(d[bl].chunk->size, d[bl].maxsize - offset); |
| 255 |
} |
| 256 |
want = SPA_MIN(want, size / pw->stride); |
| 257 |
} |
| 258 |
nframes = SPA_MIN(want, *hw_avail); |
| 259 |
|
| 260 |
if (pw->blocks == 1) { |
| 261 |
if (io->stream == SND_PCM_STREAM_PLAYBACK) { |
| 262 |
d[0].chunk->size = want * pw->stride; |
| 263 |
d[0].chunk->offset = offset = 0; |
| 264 |
} else { |
| 265 |
offset = SPA_MIN(d[0].chunk->offset, d[0].maxsize); |
| 266 |
} |
| 267 |
ptr = SPA_PTROFF(d[0].data, offset, void); |
| 268 |
for (channel = 0; channel < io->channels; channel++) { |
| 269 |
pwareas[channel].addr = ptr; |
| 270 |
pwareas[channel].first = channel * pw->sample_bits; |
| 271 |
pwareas[channel].step = io->channels * pw->sample_bits; |
| 272 |
} |
| 273 |
} else { |
| 274 |
for (channel = 0; channel < io->channels; channel++) { |
| 275 |
if (io->stream == SND_PCM_STREAM_PLAYBACK) { |
| 276 |
d[channel].chunk->size = want * pw->stride; |
| 277 |
d[channel].chunk->offset = offset = 0; |
| 278 |
} else { |
| 279 |
offset = SPA_MIN(d[channel].chunk->offset, d[channel].maxsize); |
| 280 |
} |
| 281 |
ptr = SPA_PTROFF(d[channel].data, offset, void); |
| 282 |
pwareas[channel].addr = ptr; |
| 283 |
pwareas[channel].first = 0; |
| 284 |
pwareas[channel].step = pw->sample_bits; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
if (io->state == SND_PCM_STATE_RUNNING || |
| 289 |
io->state == SND_PCM_STATE_DRAINING) { |
| 290 |
snd_pcm_uframes_t hw_ptr = pw->hw_ptr; |
| 291 |
xfer = nframes; |
| 292 |
if (xfer > 0) { |
| 293 |
const snd_pcm_channel_area_t *areas = snd_pcm_ioplug_mmap_areas(io); |
| 294 |
if (areas != NULL) { |
| 295 |
const snd_pcm_uframes_t offset = hw_ptr % io->buffer_size; |
| 296 |
if (io->stream == SND_PCM_STREAM_PLAYBACK) |
| 297 |
snd_pcm_areas_copy_wrap(pwareas, 0, nframes, |
| 298 |
areas, offset, |
| 299 |
io->buffer_size, |
| 300 |
io->channels, xfer, |
| 301 |
io->format); |
| 302 |
else |
| 303 |
snd_pcm_areas_copy_wrap(areas, offset, |
| 304 |
io->buffer_size, |
| 305 |
pwareas, 0, nframes, |
| 306 |
io->channels, xfer, |
| 307 |
io->format); |
| 308 |
} |
| 309 |
hw_ptr += xfer; |
| 310 |
if (hw_ptr >= pw->boundary) |
| 311 |
hw_ptr -= pw->boundary; |
| 312 |
pw->hw_ptr = hw_ptr; |
| 313 |
*hw_avail -= xfer; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
if (xfer < want) { |
| 318 |
|
| 319 |
if (io->stream == SND_PCM_STREAM_PLAYBACK) { |
| 320 |
const snd_pcm_uframes_t frames = want - xfer; |
| 321 |
|
| 322 |
snd_pcm_areas_silence(pwareas, xfer, io->channels, |
| 323 |
frames, io->format); |
| 324 |
xfer += frames; |
| 325 |
} |
| 326 |
if (io->state == SND_PCM_STATE_RUNNING || |
| 327 |
io->state == SND_PCM_STATE_DRAINING) { |
| 328 |
|
| 329 |
pw->xrun_detected = true; |
| 330 |
} |
| 331 |
} |
| 332 |
return xfer; |
| 333 |
} |
| 334 |
|
| 335 |
static void on_stream_param_changed(void *data, uint32_t id, const struct spa_pod *param) |
| 336 |
{ |
| 337 |
snd_pcm_pipewire_t *pw = data; |
| 338 |
snd_pcm_ioplug_t *io = &pw->io; |
| 339 |
const struct spa_pod *params[4]; |
| 340 |
uint32_t n_params = 0; |
| 341 |
uint8_t buffer[4096]; |
| 342 |
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer)); |
| 343 |
uint32_t buffers, size; |
| 344 |
|
| 345 |
if (param == NULL || id != SPA_PARAM_Format) |
| 346 |
return; |
| 347 |
|
| 348 |
if (spa_format_audio_parse(param, &pw->format) < 0) { |
| 349 |
pw->error = -EINVAL; |
| 350 |
} else { |
| 351 |
switch (pw->format.media_subtype) { |
| 352 |
case SPA_MEDIA_SUBTYPE_raw: |
| 353 |
break; |
| 354 |
case SPA_MEDIA_SUBTYPE_dsd: |
| 355 |
if (pw->format.info.dsd.interleave != pw->requested.info.dsd.interleave || |
| 356 |
pw->format.info.dsd.bitorder != pw->requested.info.dsd.bitorder) { |
| 357 |
pw->error = -EINVAL; |
| 358 |
} |
| 359 |
break; |
| 360 |
} |
| 361 |
} |
| 362 |
if (pw->error < 0) { |
| 363 |
pw_thread_loop_signal(pw->main_loop, false); |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
io->period_size = pw->min_avail; |
| 368 |
|
| 369 |
buffers = SPA_CLAMP(io->buffer_size / io->period_size, MIN_BUFFERS, MAX_BUFFERS); |
| 370 |
size = io->period_size * pw->stride; |
| 371 |
|
| 372 |
pw_log_info("%p: buffer_size:%lu period_size:%lu buffers:%u size:%u min_avail:%lu", |
| 373 |
pw, io->buffer_size, io->period_size, buffers, size, pw->min_avail); |
| 374 |
|
| 375 |
params[n_params++] = spa_pod_builder_add_object(&b, |
| 376 |
SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers, |
| 377 |
SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(buffers, MIN_BUFFERS, MAX_BUFFERS), |
| 378 |
SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(pw->blocks), |
| 379 |
SPA_PARAM_BUFFERS_size, SPA_POD_CHOICE_RANGE_Int(size, size, INT_MAX), |
| 380 |
SPA_PARAM_BUFFERS_stride, SPA_POD_Int(pw->stride)); |
| 381 |
|
| 382 |
pw_stream_update_params(pw->stream, params, n_params); |
| 383 |
|
| 384 |
pw->negotiated = true; |
| 385 |
pw_thread_loop_signal(pw->main_loop, false); |
| 386 |
} |
| 387 |
|
| 388 |
static void on_stream_state_changed(void *data, enum pw_stream_state old, enum pw_stream_state state, const char *error) |
| 389 |
{ |
| 390 |
snd_pcm_pipewire_t *pw = data; |
| 391 |
|
| 392 |
if (state == PW_STREAM_STATE_ERROR) { |
| 393 |
pw_log_warn("%s", error); |
| 394 |
pw->error = -errno; |
| 395 |
update_active(&pw->io); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
static void on_stream_drained(void *data) |
| 400 |
{ |
| 401 |
snd_pcm_pipewire_t *pw = data; |
| 402 |
pw->drained = true; |
| 403 |
pw->draining = false; |
| 404 |
pw_log_debug("%p: drained", pw); |
| 405 |
pw_thread_loop_signal(pw->main_loop, false); |
| 406 |
} |
| 407 |
|
| 408 |
static void on_stream_process(void *data) |
| 409 |
{ |
| 410 |
snd_pcm_pipewire_t *pw = data; |
| 411 |
snd_pcm_ioplug_t *io = &pw->io; |
| 412 |
struct pw_buffer *b; |
| 413 |
snd_pcm_uframes_t hw_avail, before, want, xfer; |
| 414 |
struct pw_time pwt; |
| 415 |
int64_t delay; |
| 416 |
|
| 417 |
pw_stream_get_time_n(pw->stream, &pwt, sizeof(pwt)); |
| 418 |
|
| 419 |
delay = pwt.delay; |
| 420 |
if (pwt.rate.num != 0) |
| 421 |
delay = delay * io->rate * pwt.rate.num / pwt.rate.denom; |
| 422 |
|
| 423 |
before = hw_avail = snd_pcm_ioplug_hw_avail(io, pw->hw_ptr, io->appl_ptr); |
| 424 |
|
| 425 |
if (pw->drained) |
| 426 |
goto done; |
| 427 |
|
| 428 |
b = pw_stream_dequeue_buffer(pw->stream); |
| 429 |
if (b == NULL) |
| 430 |
return; |
| 431 |
|
| 432 |
want = b->requested ? b->requested : hw_avail; |
| 433 |
|
| 434 |
SPA_SEQ_WRITE(pw->seq); |
| 435 |
|
| 436 |
if (pw->now != pwt.now) { |
| 437 |
pw->transferred = pw->buffered; |
| 438 |
pw->buffered = 0; |
| 439 |
} |
| 440 |
|
| 441 |
xfer = snd_pcm_pipewire_process(pw, b, &hw_avail, want); |
| 442 |
|
| 443 |
pw->delay = delay; |
| 444 |
|
| 445 |
if (io->stream == SND_PCM_STREAM_PLAYBACK) |
| 446 |
pw->transferred += xfer; |
| 447 |
|
| 448 |
|
| 449 |
pw->buffered = (want == 0 || pw->transferred < want) ? 0 : (pw->transferred % want); |
| 450 |
|
| 451 |
pw->now = pwt.now; |
| 452 |
SPA_SEQ_WRITE(pw->seq); |
| 453 |
|
| 454 |
pw_log_trace("%p: avail-before:%lu avail:%lu want:%lu xfer:%lu hw:%lu appl:%lu", |
| 455 |
pw, before, hw_avail, want, xfer, pw->hw_ptr, io->appl_ptr); |
| 456 |
|
| 457 |
pw_stream_queue_buffer(pw->stream, b); |
| 458 |
|
| 459 |
if (io->state == SND_PCM_STATE_DRAINING && !pw->draining && hw_avail == 0) { |
| 460 |
if (io->stream == SND_PCM_STREAM_CAPTURE) { |
| 461 |
on_stream_drained (pw); |
| 462 |
} else { |
| 463 |
pw_stream_flush(pw->stream, true); |
| 464 |
pw->draining = true; |
| 465 |
pw->drained = false; |
| 466 |
} |
| 467 |
} |
| 468 |
done: |
| 469 |
update_active(io); |
| 470 |
} |
| 471 |
|
| 472 |
static const struct pw_stream_events stream_events = { |
| 473 |
PW_VERSION_STREAM_EVENTS, |
| 474 |
.param_changed = on_stream_param_changed, |
| 475 |
.state_changed = on_stream_state_changed, |
| 476 |
.process = on_stream_process, |
| 477 |
.drained = on_stream_drained, |
| 478 |
}; |
| 479 |
|
| 480 |
static int pipewire_start(snd_pcm_pipewire_t *pw) |
| 481 |
{ |
| 482 |
if (!pw->activated && pw->stream != NULL) { |
| 483 |
pw_stream_set_active(pw->stream, true); |
| 484 |
pw->activated = true; |
| 485 |
} |
| 486 |
return 0; |
| 487 |
} |
| 488 |
|
| 489 |
static int snd_pcm_pipewire_drain(snd_pcm_ioplug_t *io) |
| 490 |
{ |
| 491 |
int res; |
| 492 |
snd_pcm_pipewire_t *pw = io->private_data; |
| 493 |
|
| 494 |
pw_thread_loop_lock(pw->main_loop); |
| 495 |
pw_log_debug("%p: drain", pw); |
| 496 |
pw->drained = false; |
| 497 |
pw->draining = false; |
| 498 |
pipewire_start(pw); |
| 499 |
while (!pw->drained && pw->error >= 0 && pw->activated) { |
| 500 |
pw_thread_loop_wait(pw->main_loop); |
| 501 |
} |
| 502 |
res = pw->error; |
| 503 |
pw_thread_loop_unlock(pw->main_loop); |
| 504 |
return res; |
| 505 |
} |
| 506 |
|
| 507 |
static int snd_pcm_pipewire_prepare(snd_pcm_ioplug_t *io) |
| 508 |
{ |
| 509 |
snd_pcm_pipewire_t *pw = io->private_data; |
| 510 |
snd_pcm_sw_params_t *swparams; |
| 511 |
const struct spa_pod *params[1]; |
| 512 |
uint8_t buffer[1024]; |
| 513 |
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer)); |
| 514 |
uint32_t min_period; |
| 515 |
|
| 516 |
pw_thread_loop_lock(pw->main_loop); |
| 517 |
|
| 518 |
snd_pcm_sw_params_alloca(&swparams); |
| 519 |
if (snd_pcm_sw_params_current(io->pcm, swparams) == 0) { |
| 520 |
int event; |
| 521 |
snd_pcm_sw_params_get_period_event(swparams, &event); |
| 522 |
if (event) |
| 523 |
pw->min_avail = io->period_size; |
| 524 |
else |
| 525 |
snd_pcm_sw_params_get_avail_min(swparams, &pw->min_avail); |
| 526 |
snd_pcm_sw_params_get_boundary(swparams, &pw->boundary); |
| 527 |
snd_pcm_sw_params_dump(swparams, pw->output); |
| 528 |
fflush(pw->log_file); |
| 529 |
} else { |
| 530 |
pw->min_avail = io->period_size; |
| 531 |
pw->boundary = io->buffer_size; |
| 532 |
} |
| 533 |
|
| 534 |
min_period = (MIN_PERIOD * io->rate / 48000); |
| 535 |
pw->min_avail = SPA_MAX(pw->min_avail, min_period); |
| 536 |
|
| 537 |
pw_log_debug("%p: prepare error:%d stream:%p buffer-size:%lu " |
| 538 |
"period-size:%lu min-avail:%ld", pw, pw->error, |
| 539 |
pw->stream, io->buffer_size, io->period_size, pw->min_avail); |
| 540 |
|
| 541 |
if (pw->error >= 0 && pw->stream != NULL && !pw->hw_params_changed) |
| 542 |
goto done; |
| 543 |
pw->hw_params_changed = false; |
| 544 |
|
| 545 |
pw_properties_setf(pw->props, PW_KEY_NODE_LATENCY, "%lu/%u", pw->min_avail, io->rate); |
| 546 |
pw_properties_setf(pw->props, PW_KEY_NODE_RATE, "1/%u", io->rate); |
| 547 |
|
| 548 |
params[0] = spa_format_audio_build(&b, SPA_PARAM_EnumFormat, &pw->format); |
| 549 |
|
| 550 |
if (pw->stream != NULL) { |
| 551 |
pw_stream_set_active(pw->stream, false); |
| 552 |
pw_stream_update_properties(pw->stream, &pw->props->dict); |
| 553 |
pw_stream_update_params(pw->stream, params, 1); |
| 554 |
pw_stream_set_active(pw->stream, true); |
| 555 |
goto done; |
| 556 |
} |
| 557 |
|
| 558 |
pw->stream = pw_stream_new(pw->core, NULL, pw_properties_copy(pw->props)); |
| 559 |
if (pw->stream == NULL) |
| 560 |
goto error; |
| 561 |
|
| 562 |
pw_stream_add_listener(pw->stream, &pw->stream_listener, &stream_events, pw); |
| 563 |
|
| 564 |
pw->error = 0; |
| 565 |
|
| 566 |
pw->negotiated = false; |
| 567 |
pw_stream_connect(pw->stream, |
| 568 |
io->stream == SND_PCM_STREAM_PLAYBACK ? |
| 569 |
PW_DIRECTION_OUTPUT : |
| 570 |
PW_DIRECTION_INPUT, |
| 571 |
PW_ID_ANY, |
| 572 |
PW_STREAM_FLAG_AUTOCONNECT | |
| 573 |
PW_STREAM_FLAG_MAP_BUFFERS | |
| 574 |
PW_STREAM_FLAG_RT_PROCESS, |
| 575 |
params, 1); |
| 576 |
|
| 577 |
done: |
| 578 |
pw->hw_ptr = 0; |
| 579 |
pw->now = 0; |
| 580 |
pw->xrun_detected = false; |
| 581 |
pw->drained = false; |
| 582 |
pw->draining = false; |
| 583 |
|
| 584 |
while (!pw->negotiated && pw->error >= 0) |
| 585 |
pw_thread_loop_wait(pw->main_loop); |
| 586 |
if (pw->error < 0) |
| 587 |
goto error; |
| 588 |
|
| 589 |
pw_thread_loop_unlock(pw->main_loop); |
| 590 |
|
| 591 |
return 0; |
| 592 |
|
| 593 |
error: |
| 594 |
pw_thread_loop_unlock(pw->main_loop); |
| 595 |
return pw->error < 0 ? pw->error : -ENOMEM; |
| 596 |
} |
| 597 |
|
| 598 |
static int snd_pcm_pipewire_start(snd_pcm_ioplug_t *io) |
| 599 |
{ |
| 600 |
snd_pcm_pipewire_t *pw = io->private_data; |
| 601 |
|
| 602 |
pw_thread_loop_lock(pw->main_loop); |
| 603 |
pw_log_debug("%p: start", pw); |
| 604 |
pipewire_start(pw); |
| 605 |
pw_thread_loop_unlock(pw->main_loop); |
| 606 |
return 0; |
| 607 |
} |
| 608 |
|
| 609 |
static int snd_pcm_pipewire_stop(snd_pcm_ioplug_t *io) |
| 610 |
{ |
| 611 |
snd_pcm_pipewire_t *pw = io->private_data; |
| 612 |
|
| 613 |
pw_log_debug("%p: stop", pw); |
| 614 |
update_active(io); |
| 615 |
|
| 616 |
pw_thread_loop_lock(pw->main_loop); |
| 617 |
if (pw->activated && pw->stream != NULL) { |
| 618 |
pw_stream_set_active(pw->stream, false); |
| 619 |
pw->activated = false; |
| 620 |
pw_thread_loop_signal(pw->main_loop, false); |
| 621 |
} |
| 622 |
pw_thread_loop_unlock(pw->main_loop); |
| 623 |
return 0; |
| 624 |
} |
| 625 |
|
| 626 |
static int snd_pcm_pipewire_pause(snd_pcm_ioplug_t * io, int enable) |
| 627 |
{ |
| 628 |
pw_log_debug("%p: pause", io); |
| 629 |
|
| 630 |
if (enable) |
| 631 |
snd_pcm_pipewire_stop(io); |
| 632 |
else |
| 633 |
snd_pcm_pipewire_start(io); |
| 634 |
|
| 635 |
return 0; |
| 636 |
} |
| 637 |
|
| 638 |
#if __BYTE_ORDER == __BIG_ENDIAN |
| 639 |
#define _FORMAT_LE(p, fmt) p ? SPA_AUDIO_FORMAT_UNKNOWN : SPA_AUDIO_FORMAT_ ## fmt ## _OE |
| 640 |
#define _FORMAT_BE(p, fmt) p ? SPA_AUDIO_FORMAT_ ## fmt ## P : SPA_AUDIO_FORMAT_ ## fmt |
| 641 |
#elif __BYTE_ORDER == __LITTLE_ENDIAN |
| 642 |
#define _FORMAT_LE(p, fmt) p ? SPA_AUDIO_FORMAT_ ## fmt ## P : SPA_AUDIO_FORMAT_ ## fmt |
| 643 |
#define _FORMAT_BE(p, fmt) p ? SPA_AUDIO_FORMAT_UNKNOWN : SPA_AUDIO_FORMAT_ ## fmt ## _OE |
| 644 |
#endif |
| 645 |
|
| 646 |
static int set_default_channels(uint32_t channels, uint32_t position[MAX_CHANNELS]) |
| 647 |
{ |
| 648 |
switch (channels) { |
| 649 |
case 8: |
| 650 |
position[6] = SPA_AUDIO_CHANNEL_SL; |
| 651 |
position[7] = SPA_AUDIO_CHANNEL_SR; |
| 652 |
SPA_FALLTHROUGH |
| 653 |
case 6: |
| 654 |
position[5] = SPA_AUDIO_CHANNEL_LFE; |
| 655 |
SPA_FALLTHROUGH |
| 656 |
case 5: |
| 657 |
position[4] = SPA_AUDIO_CHANNEL_FC; |
| 658 |
SPA_FALLTHROUGH |
| 659 |
case 4: |
| 660 |
position[2] = SPA_AUDIO_CHANNEL_RL; |
| 661 |
position[3] = SPA_AUDIO_CHANNEL_RR; |
| 662 |
SPA_FALLTHROUGH |
| 663 |
case 2: |
| 664 |
position[0] = SPA_AUDIO_CHANNEL_FL; |
| 665 |
position[1] = SPA_AUDIO_CHANNEL_FR; |
| 666 |
return 1; |
| 667 |
case 1: |
| 668 |
position[0] = SPA_AUDIO_CHANNEL_MONO; |
| 669 |
return 1; |
| 670 |
default: |
| 671 |
return 0; |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
static int snd_pcm_pipewire_hw_params(snd_pcm_ioplug_t * io, |
| 676 |
snd_pcm_hw_params_t * params) |
| 677 |
{ |
| 678 |
snd_pcm_pipewire_t *pw = io->private_data; |
| 679 |
bool planar; |
| 680 |
const char *fmt_str = NULL; |
| 681 |
|
| 682 |
snd_pcm_hw_params_dump(params, pw->output); |
| 683 |
fflush(pw->log_file); |
| 684 |
|
| 685 |
pw_log_debug("%p: hw_params buffer_size:%lu period_size:%lu", pw, io->buffer_size, io->period_size); |
| 686 |
|
| 687 |
switch(io->access) { |
| 688 |
case SND_PCM_ACCESS_MMAP_INTERLEAVED: |
| 689 |
case SND_PCM_ACCESS_RW_INTERLEAVED: |
| 690 |
planar = false; |
| 691 |
break; |
| 692 |
case SND_PCM_ACCESS_MMAP_NONINTERLEAVED: |
| 693 |
case SND_PCM_ACCESS_RW_NONINTERLEAVED: |
| 694 |
planar = true; |
| 695 |
break; |
| 696 |
default: |
| 697 |
SNDERR("PipeWire: invalid access: %d\n", io->access); |
| 698 |
return -EINVAL; |
| 699 |
} |
| 700 |
|
| 701 |
pw->requested.media_type = SPA_MEDIA_TYPE_audio; |
| 702 |
switch(io->format) { |
| 703 |
case SND_PCM_FORMAT_U8: |
| 704 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_raw; |
| 705 |
pw->requested.info.raw.format = planar ? SPA_AUDIO_FORMAT_U8P : SPA_AUDIO_FORMAT_U8; |
| 706 |
break; |
| 707 |
case SND_PCM_FORMAT_S16_LE: |
| 708 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_raw; |
| 709 |
pw->requested.info.raw.format = _FORMAT_LE(planar, S16); |
| 710 |
break; |
| 711 |
case SND_PCM_FORMAT_S16_BE: |
| 712 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_raw; |
| 713 |
pw->requested.info.raw.format = _FORMAT_BE(planar, S16); |
| 714 |
break; |
| 715 |
case SND_PCM_FORMAT_S24_LE: |
| 716 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_raw; |
| 717 |
pw->requested.info.raw.format = _FORMAT_LE(planar, S24_32); |
| 718 |
break; |
| 719 |
case SND_PCM_FORMAT_S24_BE: |
| 720 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_raw; |
| 721 |
pw->requested.info.raw.format = _FORMAT_BE(planar, S24_32); |
| 722 |
break; |
| 723 |
case SND_PCM_FORMAT_S32_LE: |
| 724 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_raw; |
| 725 |
pw->requested.info.raw.format = _FORMAT_LE(planar, S32); |
| 726 |
break; |
| 727 |
case SND_PCM_FORMAT_S32_BE: |
| 728 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_raw; |
| 729 |
pw->requested.info.raw.format = _FORMAT_BE(planar, S32); |
| 730 |
break; |
| 731 |
case SND_PCM_FORMAT_S24_3LE: |
| 732 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_raw; |
| 733 |
pw->requested.info.raw.format = _FORMAT_LE(planar, S24); |
| 734 |
break; |
| 735 |
case SND_PCM_FORMAT_S24_3BE: |
| 736 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_raw; |
| 737 |
pw->requested.info.raw.format = _FORMAT_BE(planar, S24); |
| 738 |
break; |
| 739 |
case SND_PCM_FORMAT_FLOAT_LE: |
| 740 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_raw; |
| 741 |
pw->requested.info.raw.format = _FORMAT_LE(planar, F32); |
| 742 |
break; |
| 743 |
case SND_PCM_FORMAT_FLOAT_BE: |
| 744 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_raw; |
| 745 |
pw->requested.info.raw.format = _FORMAT_BE(planar, F32); |
| 746 |
break; |
| 747 |
case SND_PCM_FORMAT_DSD_U32_BE: |
| 748 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_dsd; |
| 749 |
pw->requested.info.dsd.interleave = 4; |
| 750 |
break; |
| 751 |
case SND_PCM_FORMAT_DSD_U32_LE: |
| 752 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_dsd; |
| 753 |
pw->requested.info.dsd.interleave = -4; |
| 754 |
break; |
| 755 |
case SND_PCM_FORMAT_DSD_U16_BE: |
| 756 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_dsd; |
| 757 |
pw->requested.info.dsd.interleave = 2; |
| 758 |
break; |
| 759 |
case SND_PCM_FORMAT_DSD_U16_LE: |
| 760 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_dsd; |
| 761 |
pw->requested.info.dsd.interleave = -2; |
| 762 |
break; |
| 763 |
case SND_PCM_FORMAT_DSD_U8: |
| 764 |
pw->requested.media_subtype = SPA_MEDIA_SUBTYPE_dsd; |
| 765 |
pw->requested.info.dsd.interleave = 1; |
| 766 |
break; |
| 767 |
default: |
| 768 |
SNDERR("PipeWire: invalid format: %d\n", io->format); |
| 769 |
return -EINVAL; |
| 770 |
} |
| 771 |
switch (pw->requested.media_subtype) { |
| 772 |
case SPA_MEDIA_SUBTYPE_raw: |
| 773 |
pw->requested.info.raw.channels = io->channels; |
| 774 |
pw->requested.info.raw.rate = io->rate; |
| 775 |
set_default_channels(io->channels, pw->requested.info.raw.position); |
| 776 |
fmt_str = spa_type_audio_format_to_short_name(pw->requested.info.raw.format); |
| 777 |
pw->format = pw->requested; |
| 778 |
break; |
| 779 |
case SPA_MEDIA_SUBTYPE_dsd: |
| 780 |
pw->requested.info.dsd.bitorder = SPA_PARAM_BITORDER_msb; |
| 781 |
pw->requested.info.dsd.channels = io->channels; |
| 782 |
pw->requested.info.dsd.rate = io->rate * SPA_ABS(pw->requested.info.dsd.interleave); |
| 783 |
set_default_channels(io->channels, pw->requested.info.dsd.position); |
| 784 |
pw->format = pw->requested; |
| 785 |
|
| 786 |
pw->format.info.dsd.bitorder = 0; |
| 787 |
pw->format.info.dsd.interleave = 0; |
| 788 |
fmt_str = "DSD"; |
| 789 |
break; |
| 790 |
default: |
| 791 |
return -EIO; |
| 792 |
} |
| 793 |
|
| 794 |
pw->sample_bits = snd_pcm_format_physical_width(io->format); |
| 795 |
if (planar) { |
| 796 |
pw->blocks = io->channels; |
| 797 |
pw->stride = pw->sample_bits / 8; |
| 798 |
} else { |
| 799 |
pw->blocks = 1; |
| 800 |
pw->stride = (io->channels * pw->sample_bits) / 8; |
| 801 |
} |
| 802 |
pw->hw_params_changed = true; |
| 803 |
pw_log_info("%p: format:%s channels:%d rate:%d stride:%d blocks:%d", pw, fmt_str, |
| 804 |
io->channels, io->rate, pw->stride, pw->blocks); |
| 805 |
|
| 806 |
return 0; |
| 807 |
} |
| 808 |
|
| 809 |
static int snd_pcm_pipewire_sw_params(snd_pcm_ioplug_t * io, |
| 810 |
snd_pcm_sw_params_t * sw_params) |
| 811 |
{ |
| 812 |
snd_pcm_pipewire_t *pw = io->private_data; |
| 813 |
|
| 814 |
pw_thread_loop_lock(pw->main_loop); |
| 815 |
if (pw->stream) { |
| 816 |
snd_pcm_uframes_t min_avail; |
| 817 |
snd_pcm_sw_params_get_avail_min( sw_params, &min_avail); |
| 818 |
snd_pcm_sw_params_get_boundary(sw_params, &pw->boundary); |
| 819 |
if (min_avail != pw->min_avail) { |
| 820 |
char latency[64]; |
| 821 |
struct spa_dict_item item[1]; |
| 822 |
uint32_t min_period = (MIN_PERIOD * io->rate / 48000); |
| 823 |
|
| 824 |
pw->min_avail = SPA_MAX(min_avail, min_period); |
| 825 |
|
| 826 |
spa_scnprintf(latency, sizeof(latency), "%lu/%u", pw->min_avail, io->rate); |
| 827 |
item[0] = SPA_DICT_ITEM_INIT(PW_KEY_NODE_LATENCY, latency); |
| 828 |
|
| 829 |
pw_log_debug("%p: sw_params update props %p %ld", pw, pw->stream, pw->min_avail); |
| 830 |
pw_stream_update_properties(pw->stream, &SPA_DICT_INIT(item, 1)); |
| 831 |
} |
| 832 |
} else { |
| 833 |
pw_log_debug("%p: sw_params pre-prepare noop", pw); |
| 834 |
} |
| 835 |
pw_thread_loop_unlock(pw->main_loop); |
| 836 |
|
| 837 |
return 0; |
| 838 |
} |
| 839 |
|
| 840 |
struct chmap_info { |
| 841 |
enum snd_pcm_chmap_position pos; |
| 842 |
enum spa_audio_channel channel; |
| 843 |
}; |
| 844 |
|
| 845 |
static const struct chmap_info chmap_info[] = { |
| 846 |
[SND_CHMAP_UNKNOWN] = { SND_CHMAP_UNKNOWN, SPA_AUDIO_CHANNEL_UNKNOWN }, |
| 847 |
[SND_CHMAP_NA] = { SND_CHMAP_NA, SPA_AUDIO_CHANNEL_NA }, |
| 848 |
[SND_CHMAP_MONO] = { SND_CHMAP_MONO, SPA_AUDIO_CHANNEL_MONO }, |
| 849 |
[SND_CHMAP_FL] = { SND_CHMAP_FL, SPA_AUDIO_CHANNEL_FL }, |
| 850 |
[SND_CHMAP_FR] = { SND_CHMAP_FR, SPA_AUDIO_CHANNEL_FR }, |
| 851 |
[SND_CHMAP_RL] = { SND_CHMAP_RL, SPA_AUDIO_CHANNEL_RL }, |
| 852 |
[SND_CHMAP_RR] = { SND_CHMAP_RR, SPA_AUDIO_CHANNEL_RR }, |
| 853 |
[SND_CHMAP_FC] = { SND_CHMAP_FC, SPA_AUDIO_CHANNEL_FC }, |
| 854 |
[SND_CHMAP_LFE] = { SND_CHMAP_LFE, SPA_AUDIO_CHANNEL_LFE }, |
| 855 |
[SND_CHMAP_SL] = { SND_CHMAP_SL, SPA_AUDIO_CHANNEL_SL }, |
| 856 |
[SND_CHMAP_SR] = { SND_CHMAP_SR, SPA_AUDIO_CHANNEL_SR }, |
| 857 |
[SND_CHMAP_RC] = { SND_CHMAP_RC, SPA_AUDIO_CHANNEL_RC }, |
| 858 |
[SND_CHMAP_FLC] = { SND_CHMAP_FLC, SPA_AUDIO_CHANNEL_FLC }, |
| 859 |
[SND_CHMAP_FRC] = { SND_CHMAP_FRC, SPA_AUDIO_CHANNEL_FRC }, |
| 860 |
[SND_CHMAP_RLC] = { SND_CHMAP_RLC, SPA_AUDIO_CHANNEL_RLC }, |
| 861 |
[SND_CHMAP_RRC] = { SND_CHMAP_RRC, SPA_AUDIO_CHANNEL_RRC }, |
| 862 |
[SND_CHMAP_FLW] = { SND_CHMAP_FLW, SPA_AUDIO_CHANNEL_FLW }, |
| 863 |
[SND_CHMAP_FRW] = { SND_CHMAP_FRW, SPA_AUDIO_CHANNEL_FRW }, |
| 864 |
[SND_CHMAP_FLH] = { SND_CHMAP_FLH, SPA_AUDIO_CHANNEL_FLH }, |
| 865 |
[SND_CHMAP_FCH] = { SND_CHMAP_FCH, SPA_AUDIO_CHANNEL_FCH }, |
| 866 |
[SND_CHMAP_FRH] = { SND_CHMAP_FRH, SPA_AUDIO_CHANNEL_FRH }, |
| 867 |
[SND_CHMAP_TC] = { SND_CHMAP_TC, SPA_AUDIO_CHANNEL_TC }, |
| 868 |
[SND_CHMAP_TFL] = { SND_CHMAP_TFL, SPA_AUDIO_CHANNEL_TFL }, |
| 869 |
[SND_CHMAP_TFR] = { SND_CHMAP_TFR, SPA_AUDIO_CHANNEL_TFR }, |
| 870 |
[SND_CHMAP_TFC] = { SND_CHMAP_TFC, SPA_AUDIO_CHANNEL_TFC }, |
| 871 |
[SND_CHMAP_TRL] = { SND_CHMAP_TRL, SPA_AUDIO_CHANNEL_TRL }, |
| 872 |
[SND_CHMAP_TRR] = { SND_CHMAP_TRR, SPA_AUDIO_CHANNEL_TRR }, |
| 873 |
[SND_CHMAP_TRC] = { SND_CHMAP_TRC, SPA_AUDIO_CHANNEL_TRC }, |
| 874 |
[SND_CHMAP_TFLC] = { SND_CHMAP_TFLC, SPA_AUDIO_CHANNEL_TFLC }, |
| 875 |
[SND_CHMAP_TFRC] = { SND_CHMAP_TFRC, SPA_AUDIO_CHANNEL_TFRC }, |
| 876 |
[SND_CHMAP_TSL] = { SND_CHMAP_TSL, SPA_AUDIO_CHANNEL_TSL }, |
| 877 |
[SND_CHMAP_TSR] = { SND_CHMAP_TSR, SPA_AUDIO_CHANNEL_TSR }, |
| 878 |
[SND_CHMAP_LLFE] = { SND_CHMAP_LLFE, SPA_AUDIO_CHANNEL_LLFE }, |
| 879 |
[SND_CHMAP_RLFE] = { SND_CHMAP_RLFE, SPA_AUDIO_CHANNEL_RLFE }, |
| 880 |
[SND_CHMAP_BC] = { SND_CHMAP_BC, SPA_AUDIO_CHANNEL_BC }, |
| 881 |
[SND_CHMAP_BLC] = { SND_CHMAP_BLC, SPA_AUDIO_CHANNEL_BLC }, |
| 882 |
[SND_CHMAP_BRC] = { SND_CHMAP_BRC, SPA_AUDIO_CHANNEL_BRC }, |
| 883 |
}; |
| 884 |
|
| 885 |
static enum snd_pcm_chmap_position channel_to_chmap(enum spa_audio_channel channel) |
| 886 |
{ |
| 887 |
SPA_FOR_EACH_ELEMENT_VAR(chmap_info, info) |
| 888 |
if (info->channel == channel) |
| 889 |
return info->pos; |
| 890 |
return SND_CHMAP_UNKNOWN; |
| 891 |
} |
| 892 |
|
| 893 |
static enum spa_audio_channel chmap_to_channel(enum snd_pcm_chmap_position pos) |
| 894 |
{ |
| 895 |
if (pos >= SPA_N_ELEMENTS(chmap_info)) |
| 896 |
return SPA_AUDIO_CHANNEL_UNKNOWN; |
| 897 |
return chmap_info[pos].channel; |
| 898 |
} |
| 899 |
|
| 900 |
static int snd_pcm_pipewire_set_chmap(snd_pcm_ioplug_t * io, |
| 901 |
const snd_pcm_chmap_t * map) |
| 902 |
{ |
| 903 |
snd_pcm_pipewire_t *pw = io->private_data; |
| 904 |
unsigned int i; |
| 905 |
uint32_t *position; |
| 906 |
|
| 907 |
switch (pw->requested.media_subtype) { |
| 908 |
case SPA_MEDIA_SUBTYPE_raw: |
| 909 |
pw->requested.info.raw.channels = map->channels; |
| 910 |
position = pw->requested.info.raw.position; |
| 911 |
break; |
| 912 |
case SPA_MEDIA_SUBTYPE_dsd: |
| 913 |
pw->requested.info.dsd.channels = map->channels; |
| 914 |
position = pw->requested.info.dsd.position; |
| 915 |
break; |
| 916 |
default: |
| 917 |
return -EINVAL; |
| 918 |
} |
| 919 |
if (map->channels > MAX_CHANNELS) |
| 920 |
return -ENOTSUP; |
| 921 |
|
| 922 |
for (i = 0; i < map->channels; i++) { |
| 923 |
char buf[8]; |
| 924 |
position[i] = chmap_to_channel(map->pos[i]); |
| 925 |
pw_log_debug("map %d: %s / %s", i, |
| 926 |
snd_pcm_chmap_name(map->pos[i]), |
| 927 |
spa_type_audio_channel_make_short_name(position[i], |
| 928 |
buf, sizeof(buf), "UNK")); |
| 929 |
} |
| 930 |
return 1; |
| 931 |
} |
| 932 |
|
| 933 |
static snd_pcm_chmap_t * snd_pcm_pipewire_get_chmap(snd_pcm_ioplug_t * io) |
| 934 |
{ |
| 935 |
snd_pcm_pipewire_t *pw = io->private_data; |
| 936 |
snd_pcm_chmap_t *map; |
| 937 |
uint32_t i, channels, *position; |
| 938 |
|
| 939 |
switch (pw->requested.media_subtype) { |
| 940 |
case SPA_MEDIA_SUBTYPE_raw: |
| 941 |
channels = pw->requested.info.raw.channels; |
| 942 |
position = pw->requested.info.raw.position; |
| 943 |
break; |
| 944 |
case SPA_MEDIA_SUBTYPE_dsd: |
| 945 |
channels = pw->requested.info.dsd.channels; |
| 946 |
position = pw->requested.info.dsd.position; |
| 947 |
break; |
| 948 |
default: |
| 949 |
return NULL; |
| 950 |
} |
| 951 |
|
| 952 |
map = calloc(1, sizeof(snd_pcm_chmap_t) + |
| 953 |
channels * sizeof(unsigned int)); |
| 954 |
map->channels = channels; |
| 955 |
for (i = 0; i < channels; i++) |
| 956 |
map->pos[i] = channel_to_chmap(position[i]); |
| 957 |
|
| 958 |
return map; |
| 959 |
} |
| 960 |
|
| 961 |
static void make_map(snd_pcm_chmap_query_t **maps, int index, int channels, ...) |
| 962 |
{ |
| 963 |
va_list args; |
| 964 |
int i; |
| 965 |
|
| 966 |
maps[index] = malloc(sizeof(snd_pcm_chmap_query_t) + (channels * sizeof(unsigned int))); |
| 967 |
maps[index]->type = SND_CHMAP_TYPE_FIXED; |
| 968 |
maps[index]->map.channels = channels; |
| 969 |
va_start(args, channels); |
| 970 |
for (i = 0; i < channels; i++) |
| 971 |
maps[index]->map.pos[i] = va_arg(args, int); |
| 972 |
va_end(args); |
| 973 |
} |
| 974 |
|
| 975 |
static snd_pcm_chmap_query_t **snd_pcm_pipewire_query_chmaps(snd_pcm_ioplug_t *io) |
| 976 |
{ |
| 977 |
snd_pcm_chmap_query_t **maps; |
| 978 |
|
| 979 |
maps = calloc(7, sizeof(*maps)); |
| 980 |
make_map(maps, 0, 1, SND_CHMAP_MONO); |
| 981 |
make_map(maps, 1, 2, SND_CHMAP_FL, SND_CHMAP_FR); |
| 982 |
make_map(maps, 2, 4, SND_CHMAP_FL, SND_CHMAP_FR, SND_CHMAP_RL, SND_CHMAP_RR); |
| 983 |
make_map(maps, 3, 5, SND_CHMAP_FL, SND_CHMAP_FR, SND_CHMAP_RL, SND_CHMAP_RR, |
| 984 |
SND_CHMAP_FC); |
| 985 |
make_map(maps, 4, 6, SND_CHMAP_FL, SND_CHMAP_FR, SND_CHMAP_RL, SND_CHMAP_RR, |
| 986 |
SND_CHMAP_FC, SND_CHMAP_LFE); |
| 987 |
make_map(maps, 5, 8, SND_CHMAP_FL, SND_CHMAP_FR, SND_CHMAP_RL, SND_CHMAP_RR, |
| 988 |
SND_CHMAP_FC, SND_CHMAP_LFE, SND_CHMAP_SL, SND_CHMAP_SR); |
| 989 |
|
| 990 |
return maps; |
| 991 |
} |
| 992 |
|
| 993 |
static snd_pcm_ioplug_callback_t pipewire_pcm_callback = { |
| 994 |
.close = snd_pcm_pipewire_close, |
| 995 |
.start = snd_pcm_pipewire_start, |
| 996 |
.stop = snd_pcm_pipewire_stop, |
| 997 |
.pause = snd_pcm_pipewire_pause, |
| 998 |
.pointer = snd_pcm_pipewire_pointer, |
| 999 |
.delay = snd_pcm_pipewire_delay, |
| 1000 |
.drain = snd_pcm_pipewire_drain, |
| 1001 |
.prepare = snd_pcm_pipewire_prepare, |
| 1002 |
.poll_revents = snd_pcm_pipewire_poll_revents, |
| 1003 |
.hw_params = snd_pcm_pipewire_hw_params, |
| 1004 |
.sw_params = snd_pcm_pipewire_sw_params, |
| 1005 |
.set_chmap = snd_pcm_pipewire_set_chmap, |
| 1006 |
.get_chmap = snd_pcm_pipewire_get_chmap, |
| 1007 |
.query_chmaps = snd_pcm_pipewire_query_chmaps, |
| 1008 |
}; |
| 1009 |
|
| 1010 |
#define MAX_VALS 64 |
| 1011 |
struct param_info { |
| 1012 |
const char *prop; |
| 1013 |
int key; |
| 1014 |
#define TYPE_LIST 0 |
| 1015 |
#define TYPE_MIN_MAX 1 |
| 1016 |
int type; |
| 1017 |
unsigned int vals[MAX_VALS]; |
| 1018 |
unsigned int n_vals; |
| 1019 |
int (*collect) (const char *str, int len, unsigned int *val); |
| 1020 |
|
| 1021 |
}; |
| 1022 |
|
| 1023 |
static int collect_access(const char *str, int len, unsigned int *val) |
| 1024 |
{ |
| 1025 |
char key[64]; |
| 1026 |
|
| 1027 |
if (spa_json_parse_stringn(str, len, key, sizeof(key)) <= 0) |
| 1028 |
return -EINVAL; |
| 1029 |
|
| 1030 |
if (strcasecmp(key, "MMAP_INTERLEAVED") == 0) |
| 1031 |
*val = SND_PCM_ACCESS_MMAP_INTERLEAVED; |
| 1032 |
else if (strcasecmp(key, "MMAP_NONINTERLEAVED") == 0) |
| 1033 |
*val = SND_PCM_ACCESS_MMAP_NONINTERLEAVED; |
| 1034 |
else if (strcasecmp(key, "RW_INTERLEAVED") == 0) |
| 1035 |
*val = SND_PCM_ACCESS_RW_INTERLEAVED; |
| 1036 |
else if (strcasecmp(key, "RW_NONINTERLEAVED") == 0) |
| 1037 |
*val = SND_PCM_ACCESS_RW_NONINTERLEAVED; |
| 1038 |
else |
| 1039 |
return -EINVAL; |
| 1040 |
return 0; |
| 1041 |
} |
| 1042 |
|
| 1043 |
static int collect_format(const char *str, int len, unsigned int *val) |
| 1044 |
{ |
| 1045 |
char key[64]; |
| 1046 |
snd_pcm_format_t fmt; |
| 1047 |
|
| 1048 |
if (spa_json_parse_stringn(str, len, key, sizeof(key)) < 0) |
| 1049 |
return -EINVAL; |
| 1050 |
|
| 1051 |
fmt = snd_pcm_format_value(key); |
| 1052 |
if (fmt != SND_PCM_FORMAT_UNKNOWN) |
| 1053 |
*val = fmt; |
| 1054 |
else |
| 1055 |
return -EINVAL; |
| 1056 |
return 0; |
| 1057 |
} |
| 1058 |
|
| 1059 |
static int collect_int(const char *str, int len, unsigned int *val) |
| 1060 |
{ |
| 1061 |
int v; |
| 1062 |
if (spa_json_parse_int(str, len, &v) > 0) |
| 1063 |
*val = v; |
| 1064 |
else |
| 1065 |
return -EINVAL; |
| 1066 |
return 0; |
| 1067 |
} |
| 1068 |
|
| 1069 |
struct param_info infos[] = { |
| 1070 |
{ "alsa.access", SND_PCM_IOPLUG_HW_ACCESS, TYPE_LIST, |
| 1071 |
{ SND_PCM_ACCESS_MMAP_INTERLEAVED, |
| 1072 |
SND_PCM_ACCESS_MMAP_NONINTERLEAVED, |
| 1073 |
SND_PCM_ACCESS_RW_INTERLEAVED, |
| 1074 |
SND_PCM_ACCESS_RW_NONINTERLEAVED }, 4, collect_access }, |
| 1075 |
{ "alsa.format", SND_PCM_IOPLUG_HW_FORMAT, TYPE_LIST, |
| 1076 |
{ |
| 1077 |
#if __BYTE_ORDER == __LITTLE_ENDIAN |
| 1078 |
SND_PCM_FORMAT_FLOAT_LE, |
| 1079 |
SND_PCM_FORMAT_S32_LE, |
| 1080 |
SND_PCM_FORMAT_S24_LE, |
| 1081 |
SND_PCM_FORMAT_S24_3LE, |
| 1082 |
SND_PCM_FORMAT_S24_3BE, |
| 1083 |
SND_PCM_FORMAT_S16_LE, |
| 1084 |
#elif __BYTE_ORDER == __BIG_ENDIAN |
| 1085 |
SND_PCM_FORMAT_FLOAT_BE, |
| 1086 |
SND_PCM_FORMAT_S32_BE, |
| 1087 |
SND_PCM_FORMAT_S24_BE, |
| 1088 |
SND_PCM_FORMAT_S24_3LE, |
| 1089 |
SND_PCM_FORMAT_S24_3BE, |
| 1090 |
SND_PCM_FORMAT_S16_BE, |
| 1091 |
#endif |
| 1092 |
SND_PCM_FORMAT_U8, |
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
}, 7, collect_format }, |
| 1102 |
{ "alsa.rate", SND_PCM_IOPLUG_HW_RATE, TYPE_MIN_MAX, |
| 1103 |
{ 1, MAX_RATE }, 2, collect_int }, |
| 1104 |
{ "alsa.channels", SND_PCM_IOPLUG_HW_CHANNELS, TYPE_MIN_MAX, |
| 1105 |
{ 1, MAX_CHANNELS }, 2, collect_int }, |
| 1106 |
{ "alsa.buffer-bytes", SND_PCM_IOPLUG_HW_BUFFER_BYTES, TYPE_MIN_MAX, |
| 1107 |
{ MIN_BUFFER_BYTES, MAX_BUFFER_BYTES }, 2, collect_int }, |
| 1108 |
{ "alsa.period-bytes", SND_PCM_IOPLUG_HW_PERIOD_BYTES, TYPE_MIN_MAX, |
| 1109 |
{ MIN_PERIOD_BYTES, MAX_PERIOD_BYTES }, 2, collect_int }, |
| 1110 |
{ "alsa.periods", SND_PCM_IOPLUG_HW_PERIODS, TYPE_MIN_MAX, |
| 1111 |
{ MIN_BUFFERS, 1024 }, 2, collect_int }, |
| 1112 |
}; |
| 1113 |
|
| 1114 |
static struct param_info *param_info_by_key(int key) |
| 1115 |
{ |
| 1116 |
SPA_FOR_EACH_ELEMENT_VAR(infos, p) { |
| 1117 |
if (p->key == key) |
| 1118 |
return p; |
| 1119 |
} |
| 1120 |
return NULL; |
| 1121 |
} |
| 1122 |
|
| 1123 |
static int parse_value(const char *str, struct param_info *info) |
| 1124 |
{ |
| 1125 |
struct spa_json it[2]; |
| 1126 |
unsigned int v; |
| 1127 |
const char *val; |
| 1128 |
int len; |
| 1129 |
|
| 1130 |
if ((len = spa_json_begin(&it[0], str, strlen(str), &val)) <= 0) |
| 1131 |
return -EINVAL; |
| 1132 |
|
| 1133 |
if (spa_json_is_array(val, len)) { |
| 1134 |
info->type = TYPE_LIST; |
| 1135 |
info->n_vals = 0; |
| 1136 |
spa_json_enter(&it[0], &it[1]); |
| 1137 |
while ((len = spa_json_next(&it[1], &val)) > 0 && info->n_vals < MAX_VALS) { |
| 1138 |
if (info->collect(val, len, &v) < 0) |
| 1139 |
continue; |
| 1140 |
info->vals[info->n_vals++] = v; |
| 1141 |
} |
| 1142 |
} |
| 1143 |
else if (spa_json_is_object(val, len)) { |
| 1144 |
char key[64]; |
| 1145 |
info->type = TYPE_MIN_MAX; |
| 1146 |
info->n_vals = 2; |
| 1147 |
spa_json_enter(&it[0], &it[1]); |
| 1148 |
while ((len = spa_json_object_next(&it[1], key, sizeof(key), &val)) > 0) { |
| 1149 |
if (info->collect(val, len, &v) < 0) |
| 1150 |
continue; |
| 1151 |
if (spa_streq(key, "min")) |
| 1152 |
info->vals[0] = v; |
| 1153 |
else if (spa_streq(key, "max")) |
| 1154 |
info->vals[1] = v; |
| 1155 |
} |
| 1156 |
} |
| 1157 |
else if (info->collect(val, len, &v) >= 0) { |
| 1158 |
info->type = TYPE_LIST; |
| 1159 |
info->vals[0] = v; |
| 1160 |
info->n_vals = 1; |
| 1161 |
} |
| 1162 |
return 0; |
| 1163 |
} |
| 1164 |
|
| 1165 |
static int set_constraint(snd_pcm_pipewire_t *pw, int key) |
| 1166 |
{ |
| 1167 |
struct param_info *p = param_info_by_key(key), info; |
| 1168 |
const char *str; |
| 1169 |
int err; |
| 1170 |
|
| 1171 |
if (p == NULL) |
| 1172 |
return -EINVAL; |
| 1173 |
|
| 1174 |
info = *p; |
| 1175 |
|
| 1176 |
str = pw_properties_get(pw->props, p->prop); |
| 1177 |
if (str != NULL) |
| 1178 |
parse_value(str, &info); |
| 1179 |
|
| 1180 |
switch (info.type) { |
| 1181 |
case TYPE_LIST: |
| 1182 |
pw_log_info("%s: list %d", p->prop, info.n_vals); |
| 1183 |
err = snd_pcm_ioplug_set_param_list(&pw->io, key, info.n_vals, info.vals); |
| 1184 |
break; |
| 1185 |
case TYPE_MIN_MAX: |
| 1186 |
pw_log_info("%s: min:%u max:%u", p->prop, info.vals[0], info.vals[1]); |
| 1187 |
err = snd_pcm_ioplug_set_param_minmax(&pw->io, key, info.vals[0], info.vals[1]); |
| 1188 |
break; |
| 1189 |
default: |
| 1190 |
return -EIO; |
| 1191 |
} |
| 1192 |
if (err < 0) |
| 1193 |
pw_log_warn("Can't set param %s: %s", info.prop, snd_strerror(err)); |
| 1194 |
|
| 1195 |
return err; |
| 1196 |
|
| 1197 |
} |
| 1198 |
static int pipewire_set_hw_constraint(snd_pcm_pipewire_t *pw) |
| 1199 |
{ |
| 1200 |
int err; |
| 1201 |
if ((err = set_constraint(pw, SND_PCM_IOPLUG_HW_ACCESS)) < 0 || |
| 1202 |
(err = set_constraint(pw, SND_PCM_IOPLUG_HW_FORMAT)) < 0 || |
| 1203 |
(err = set_constraint(pw, SND_PCM_IOPLUG_HW_RATE)) < 0 || |
| 1204 |
(err = set_constraint(pw, SND_PCM_IOPLUG_HW_CHANNELS)) < 0 || |
| 1205 |
(err = set_constraint(pw, SND_PCM_IOPLUG_HW_PERIOD_BYTES)) < 0 || |
| 1206 |
(err = set_constraint(pw, SND_PCM_IOPLUG_HW_BUFFER_BYTES)) < 0 || |
| 1207 |
(err = set_constraint(pw, SND_PCM_IOPLUG_HW_PERIODS)) < 0) |
| 1208 |
return err; |
| 1209 |
return 0; |
| 1210 |
} |
| 1211 |
|
| 1212 |
static void on_core_error(void *data, uint32_t id, int seq, int res, const char *message) |
| 1213 |
{ |
| 1214 |
snd_pcm_pipewire_t *pw = data; |
| 1215 |
|
| 1216 |
pw_log_warn("%p: error id:%u seq:%d res:%d (%s): %s", pw, |
| 1217 |
id, seq, res, spa_strerror(res), message); |
| 1218 |
|
| 1219 |
if (id == PW_ID_CORE) { |
| 1220 |
pw->error = res; |
| 1221 |
if (pw->fd != -1) |
| 1222 |
update_active(&pw->io); |
| 1223 |
} |
| 1224 |
pw_thread_loop_signal(pw->main_loop, false); |
| 1225 |
} |
| 1226 |
|
| 1227 |
static const struct pw_core_events core_events = { |
| 1228 |
PW_VERSION_CORE_EVENTS, |
| 1229 |
.error = on_core_error, |
| 1230 |
}; |
| 1231 |
|
| 1232 |
|
| 1233 |
static ssize_t log_write(void *cookie, const char *buf, size_t size) |
| 1234 |
{ |
| 1235 |
int len; |
| 1236 |
|
| 1237 |
while (size > 0) { |
| 1238 |
len = strcspn(buf, "\n"); |
| 1239 |
if (len > 0) |
| 1240 |
pw_log_debug("%.*s", (int)len, buf); |
| 1241 |
buf += len + 1; |
| 1242 |
size -= len + 1; |
| 1243 |
} |
| 1244 |
return size; |
| 1245 |
} |
| 1246 |
|
| 1247 |
static const cookie_io_functions_t io_funcs = { |
| 1248 |
.write = log_write, |
| 1249 |
}; |
| 1250 |
|
| 1251 |
static int execute_match(void *data, const char *location, const char *action, |
| 1252 |
const char *val, size_t len) |
| 1253 |
{ |
| 1254 |
snd_pcm_pipewire_t *pw = data; |
| 1255 |
if (spa_streq(action, "update-props")) |
| 1256 |
pw_properties_update_string(pw->props, val, len); |
| 1257 |
return 1; |
| 1258 |
} |
| 1259 |
|
| 1260 |
static int snd_pcm_pipewire_open(snd_pcm_t **pcmp, |
| 1261 |
struct pw_properties *props, snd_pcm_stream_t stream, int mode) |
| 1262 |
{ |
| 1263 |
snd_pcm_pipewire_t *pw; |
| 1264 |
int err; |
| 1265 |
const char *str, *node_name = NULL; |
| 1266 |
struct pw_loop *loop; |
| 1267 |
|
| 1268 |
assert(pcmp); |
| 1269 |
pw = calloc(1, sizeof(*pw)); |
| 1270 |
if (!pw) |
| 1271 |
return -ENOMEM; |
| 1272 |
|
| 1273 |
pw->props = props; |
| 1274 |
pw->fd = -1; |
| 1275 |
pw->io.poll_fd = -1; |
| 1276 |
pw->log_file = fopencookie(pw, "w", io_funcs); |
| 1277 |
if (pw->log_file == NULL) { |
| 1278 |
pw_log_error("can't create log file: %m"); |
| 1279 |
err = -errno; |
| 1280 |
goto error; |
| 1281 |
} |
| 1282 |
if ((err = snd_output_stdio_attach(&pw->output, pw->log_file, 0)) < 0) { |
| 1283 |
pw_log_error("can't attach log file: %s", snd_strerror(err)); |
| 1284 |
goto error; |
| 1285 |
} |
| 1286 |
|
| 1287 |
pw->main_loop = pw_thread_loop_new("alsa-pipewire", NULL); |
| 1288 |
if (pw->main_loop == NULL) { |
| 1289 |
err = -errno; |
| 1290 |
goto error; |
| 1291 |
} |
| 1292 |
loop = pw_thread_loop_get_loop(pw->main_loop); |
| 1293 |
pw->system = loop->system; |
| 1294 |
if ((pw->context = pw_context_new(loop, |
| 1295 |
pw_properties_new( |
| 1296 |
PW_KEY_CLIENT_API, "alsa", |
| 1297 |
NULL), |
| 1298 |
0)) == NULL) { |
| 1299 |
err = -errno; |
| 1300 |
goto error; |
| 1301 |
} |
| 1302 |
|
| 1303 |
pw_context_conf_update_props(pw->context, "alsa.properties", pw->props); |
| 1304 |
|
| 1305 |
pw_context_conf_section_match_rules(pw->context, "alsa.rules", |
| 1306 |
&pw_context_get_properties(pw->context)->dict, execute_match, pw); |
| 1307 |
|
| 1308 |
if (pw_properties_get(pw->props, PW_KEY_APP_NAME) == NULL) |
| 1309 |
pw_properties_setf(pw->props, PW_KEY_APP_NAME, "PipeWire ALSA [%s]", |
| 1310 |
pw_get_prgname()); |
| 1311 |
if (pw_properties_get(pw->props, PW_KEY_NODE_NAME) == NULL) |
| 1312 |
pw_properties_setf(pw->props, PW_KEY_NODE_NAME, "alsa_%s.%s", |
| 1313 |
stream == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture", |
| 1314 |
pw_get_prgname()); |
| 1315 |
if (pw_properties_get(pw->props, PW_KEY_NODE_DESCRIPTION) == NULL) |
| 1316 |
pw_properties_setf(pw->props, PW_KEY_NODE_DESCRIPTION, "ALSA %s [%s]", |
| 1317 |
stream == SND_PCM_STREAM_PLAYBACK ? "Playback" : "Capture", |
| 1318 |
pw_get_prgname()); |
| 1319 |
if (pw_properties_get(pw->props, PW_KEY_MEDIA_NAME) == NULL) |
| 1320 |
pw_properties_setf(pw->props, PW_KEY_MEDIA_NAME, "ALSA %s", |
| 1321 |
stream == SND_PCM_STREAM_PLAYBACK ? "Playback" : "Capture"); |
| 1322 |
if (pw_properties_get(pw->props, PW_KEY_MEDIA_TYPE) == NULL) |
| 1323 |
pw_properties_set(pw->props, PW_KEY_MEDIA_TYPE, "Audio"); |
| 1324 |
if (pw_properties_get(pw->props, PW_KEY_MEDIA_CATEGORY) == NULL) |
| 1325 |
pw_properties_set(pw->props, PW_KEY_MEDIA_CATEGORY, |
| 1326 |
stream == SND_PCM_STREAM_PLAYBACK ? |
| 1327 |
"Playback" : "Capture"); |
| 1328 |
|
| 1329 |
str = getenv("PIPEWIRE_ALSA"); |
| 1330 |
if (str != NULL) |
| 1331 |
pw_properties_update_string(pw->props, str, strlen(str)); |
| 1332 |
|
| 1333 |
if ((str = pw_properties_get(pw->props, "alsa.deny")) != NULL && |
| 1334 |
spa_atob(str)) { |
| 1335 |
err = -EACCES; |
| 1336 |
goto error; |
| 1337 |
} |
| 1338 |
|
| 1339 |
str = getenv("PIPEWIRE_NODE"); |
| 1340 |
if (str != NULL && str[0]) |
| 1341 |
pw_properties_set(pw->props, PW_KEY_TARGET_OBJECT, str); |
| 1342 |
|
| 1343 |
node_name = pw_properties_get(pw->props, PW_KEY_NODE_NAME); |
| 1344 |
if (pw_properties_get(pw->props, PW_KEY_MEDIA_NAME) == NULL) |
| 1345 |
pw_properties_set(pw->props, PW_KEY_MEDIA_NAME, node_name); |
| 1346 |
|
| 1347 |
if ((err = pw_thread_loop_start(pw->main_loop)) < 0) |
| 1348 |
goto error; |
| 1349 |
|
| 1350 |
pw_thread_loop_lock(pw->main_loop); |
| 1351 |
pw->core = pw_context_connect(pw->context, pw_properties_copy(pw->props), 0); |
| 1352 |
if (pw->core == NULL) { |
| 1353 |
err = -errno; |
| 1354 |
pw_thread_loop_unlock(pw->main_loop); |
| 1355 |
goto error; |
| 1356 |
} |
| 1357 |
pw_core_add_listener(pw->core, &pw->core_listener, &core_events, pw); |
| 1358 |
pw_thread_loop_unlock(pw->main_loop); |
| 1359 |
|
| 1360 |
pw->fd = spa_system_eventfd_create(pw->system, |
| 1361 |
SPA_FD_CLOEXEC | SPA_FD_NONBLOCK); |
| 1362 |
if (pw->fd < 0) { |
| 1363 |
err = pw->fd; |
| 1364 |
goto error; |
| 1365 |
} |
| 1366 |
|
| 1367 |
pw->io.version = SND_PCM_IOPLUG_VERSION; |
| 1368 |
pw->io.name = "ALSA <-> PipeWire PCM I/O Plugin"; |
| 1369 |
pw->io.callback = &pipewire_pcm_callback; |
| 1370 |
pw->io.private_data = pw; |
| 1371 |
pw->io.poll_fd = pw->fd; |
| 1372 |
pw->io.poll_events = POLLIN; |
| 1373 |
pw->io.mmap_rw = 1; |
| 1374 |
#ifdef SND_PCM_IOPLUG_FLAG_BOUNDARY_WA |
| 1375 |
pw->io.flags = SND_PCM_IOPLUG_FLAG_BOUNDARY_WA; |
| 1376 |
#else |
| 1377 |
#warning hw_ptr updates of buffer_size will not be recognized by the ALSA library. Consider to update your ALSA library. |
| 1378 |
#endif |
| 1379 |
pw->io.flags |= SND_PCM_IOPLUG_FLAG_MONOTONIC; |
| 1380 |
|
| 1381 |
if ((err = snd_pcm_ioplug_create(&pw->io, node_name, stream, mode)) < 0) |
| 1382 |
goto error; |
| 1383 |
|
| 1384 |
if ((err = pipewire_set_hw_constraint(pw)) < 0) |
| 1385 |
goto error; |
| 1386 |
|
| 1387 |
pw_log_debug("%p: opened name:%s stream:%s mode:%d", pw, node_name, |
| 1388 |
snd_pcm_stream_name(pw->io.stream), mode); |
| 1389 |
|
| 1390 |
*pcmp = pw->io.pcm; |
| 1391 |
|
| 1392 |
return 0; |
| 1393 |
|
| 1394 |
error: |
| 1395 |
pw_log_debug("%p: failed to open %s :%s", pw, node_name, spa_strerror(err)); |
| 1396 |
snd_pcm_pipewire_free(pw); |
| 1397 |
return err; |
| 1398 |
} |
| 1399 |
|
| 1400 |
|
| 1401 |
SPA_EXPORT |
| 1402 |
SND_PCM_PLUGIN_DEFINE_FUNC(pipewire) |
| 1403 |
{ |
| 1404 |
snd_config_iterator_t i, next; |
| 1405 |
struct pw_properties *props; |
| 1406 |
const char *str; |
| 1407 |
long val; |
| 1408 |
int err; |
| 1409 |
|
| 1410 |
pw_init(NULL, NULL); |
| 1411 |
if (spa_strstartswith(pw_get_library_version(), "0.2")) |
| 1412 |
return -ENOTSUP; |
| 1413 |
|
| 1414 |
props = pw_properties_new(NULL, NULL); |
| 1415 |
if (props == NULL) |
| 1416 |
return -errno; |
| 1417 |
|
| 1418 |
PW_LOG_TOPIC_INIT(alsa_log_topic); |
| 1419 |
|
| 1420 |
snd_config_for_each(i, next, conf) { |
| 1421 |
snd_config_t *n = snd_config_iterator_entry(i); |
| 1422 |
const char *id; |
| 1423 |
if (snd_config_get_id(n, &id) < 0) |
| 1424 |
continue; |
| 1425 |
if (spa_streq(id, "comment") || spa_streq(id, "type") || spa_streq(id, "hint")) |
| 1426 |
continue; |
| 1427 |
if (spa_streq(id, "name")) { |
| 1428 |
if (snd_config_get_string(n, &str) == 0) |
| 1429 |
pw_properties_set(props, PW_KEY_NODE_NAME, str); |
| 1430 |
continue; |
| 1431 |
} |
| 1432 |
if (spa_streq(id, "server")) { |
| 1433 |
if (snd_config_get_string(n, &str) == 0) |
| 1434 |
pw_properties_set(props, PW_KEY_REMOTE_NAME, str); |
| 1435 |
continue; |
| 1436 |
} |
| 1437 |
if (spa_streq(id, "playback_node")) { |
| 1438 |
if (stream == SND_PCM_STREAM_PLAYBACK && |
| 1439 |
snd_config_get_string(n, &str) == 0) |
| 1440 |
if (str != NULL && !spa_streq(str, "-1")) |
| 1441 |
pw_properties_set(props, PW_KEY_TARGET_OBJECT, str); |
| 1442 |
continue; |
| 1443 |
} |
| 1444 |
if (spa_streq(id, "capture_node")) { |
| 1445 |
if (stream == SND_PCM_STREAM_CAPTURE && |
| 1446 |
snd_config_get_string(n, &str) == 0) |
| 1447 |
if (str != NULL && !spa_streq(str, "-1")) |
| 1448 |
pw_properties_set(props, PW_KEY_TARGET_OBJECT, str); |
| 1449 |
continue; |
| 1450 |
} |
| 1451 |
if (spa_streq(id, "role")) { |
| 1452 |
if (snd_config_get_string(n, &str) == 0) |
| 1453 |
if (str != NULL && *str) |
| 1454 |
pw_properties_set(props, PW_KEY_MEDIA_ROLE, str); |
| 1455 |
continue; |
| 1456 |
} |
| 1457 |
if (spa_streq(id, "exclusive")) { |
| 1458 |
if (snd_config_get_bool(n)) |
| 1459 |
pw_properties_set(props, PW_KEY_NODE_EXCLUSIVE, "true"); |
| 1460 |
continue; |
| 1461 |
} |
| 1462 |
if (spa_streq(id, "rate")) { |
| 1463 |
if (snd_config_get_integer(n, &val) == 0) { |
| 1464 |
if (val != 0) |
| 1465 |
pw_properties_setf(props, "alsa.rate", "%ld", val); |
| 1466 |
} else { |
| 1467 |
SNDERR("%s: invalid type", id); |
| 1468 |
} |
| 1469 |
continue; |
| 1470 |
} |
| 1471 |
if (spa_streq(id, "format")) { |
| 1472 |
if (snd_config_get_string(n, &str) == 0) { |
| 1473 |
if (str != NULL && *str) |
| 1474 |
pw_properties_set(props, "alsa.format", str); |
| 1475 |
} else { |
| 1476 |
SNDERR("%s: invalid type", id); |
| 1477 |
} |
| 1478 |
continue; |
| 1479 |
} |
| 1480 |
if (spa_streq(id, "channels")) { |
| 1481 |
if (snd_config_get_integer(n, &val) == 0) { |
| 1482 |
if (val != 0) |
| 1483 |
pw_properties_setf(props, "alsa.channels", "%ld", val); |
| 1484 |
} else { |
| 1485 |
SNDERR("%s: invalid type", id); |
| 1486 |
} |
| 1487 |
continue; |
| 1488 |
} |
| 1489 |
if (spa_streq(id, "period_bytes")) { |
| 1490 |
if (snd_config_get_integer(n, &val) == 0) { |
| 1491 |
if (val != 0) |
| 1492 |
pw_properties_setf(props, "alsa.period-bytes", "%ld", val); |
| 1493 |
} else { |
| 1494 |
SNDERR("%s: invalid type", id); |
| 1495 |
} |
| 1496 |
continue; |
| 1497 |
} |
| 1498 |
if (spa_streq(id, "buffer_bytes")) { |
| 1499 |
long val; |
| 1500 |
|
| 1501 |
if (snd_config_get_integer(n, &val) == 0) { |
| 1502 |
if (val != 0) |
| 1503 |
pw_properties_setf(props, "alsa.buffer-bytes", "%ld", val); |
| 1504 |
} else { |
| 1505 |
SNDERR("%s: invalid type", id); |
| 1506 |
} |
| 1507 |
continue; |
| 1508 |
} |
| 1509 |
SNDERR("Unknown field %s", id); |
| 1510 |
pw_properties_free(props); |
| 1511 |
return -EINVAL; |
| 1512 |
} |
| 1513 |
|
| 1514 |
err = snd_pcm_pipewire_open(pcmp, props, stream, mode); |
| 1515 |
|
| 1516 |
return err; |
| 1517 |
} |
| 1518 |
|
| 1519 |
SPA_EXPORT |
| 1520 |
SND_PCM_PLUGIN_SYMBOL(pipewire); |