2017-10-19 11:50:14 +02:00
|
|
|
/*
|
|
|
|
|
* PCM - PipeWire plugin
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2017 Wim Taymans <wim.taymans@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define __USE_GNU
|
|
|
|
|
|
|
|
|
|
#include <byteswap.h>
|
|
|
|
|
#include <sys/shm.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/eventfd.h>
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
|
#include <alsa/pcm_external.h>
|
|
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
#include <spa/support/type-map.h>
|
|
|
|
|
#include <spa/param/format-utils.h>
|
|
|
|
|
#include <spa/param/audio/format-utils.h>
|
|
|
|
|
#include <spa/param/props.h>
|
2017-10-19 11:50:14 +02:00
|
|
|
#include <spa/lib/debug.h>
|
|
|
|
|
|
|
|
|
|
#include <pipewire/pipewire.h>
|
|
|
|
|
|
|
|
|
|
struct type {
|
|
|
|
|
uint32_t format;
|
|
|
|
|
uint32_t props;
|
|
|
|
|
struct spa_type_meta meta;
|
|
|
|
|
struct spa_type_data data;
|
|
|
|
|
struct spa_type_media_type media_type;
|
|
|
|
|
struct spa_type_media_subtype media_subtype;
|
|
|
|
|
struct spa_type_format_audio format_audio;
|
|
|
|
|
struct spa_type_audio_format audio_format;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static inline void init_type(struct type *type, struct spa_type_map *map)
|
|
|
|
|
{
|
|
|
|
|
type->format = spa_type_map_get_id(map, SPA_TYPE__Format);
|
|
|
|
|
type->props = spa_type_map_get_id(map, SPA_TYPE__Props);
|
|
|
|
|
spa_type_meta_map(map, &type->meta);
|
|
|
|
|
spa_type_data_map(map, &type->data);
|
|
|
|
|
spa_type_media_type_map(map, &type->media_type);
|
|
|
|
|
spa_type_media_subtype_map(map, &type->media_subtype);
|
|
|
|
|
spa_type_format_audio_map(map, &type->format_audio);
|
|
|
|
|
spa_type_audio_format_map(map, &type->audio_format);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct buffer {
|
|
|
|
|
struct spa_buffer *buffer;
|
|
|
|
|
struct spa_list link;
|
2017-11-10 13:39:59 +01:00
|
|
|
struct spa_meta_header *h;
|
|
|
|
|
struct spa_meta_ringbuffer *rb;
|
2017-10-19 11:50:14 +02:00
|
|
|
void *ptr;
|
|
|
|
|
size_t size;
|
|
|
|
|
bool mapped;
|
|
|
|
|
bool used;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef enum _pipewire_format {
|
|
|
|
|
SND_PCM_PIPEWIRE_FORMAT_RAW
|
|
|
|
|
} snd_pcm_pipewire_format_t;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
snd_pcm_ioplug_t io;
|
|
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
|
int activated; /* PipeWire is activated? */
|
2017-11-10 13:39:59 +01:00
|
|
|
bool error;
|
2017-10-19 11:50:14 +02:00
|
|
|
|
|
|
|
|
unsigned int num_ports;
|
|
|
|
|
unsigned int hw_ptr;
|
|
|
|
|
unsigned int sample_bits;
|
|
|
|
|
snd_pcm_uframes_t min_avail;
|
|
|
|
|
|
|
|
|
|
struct type type;
|
|
|
|
|
struct pw_loop *loop;
|
|
|
|
|
struct pw_thread_loop *main_loop;
|
|
|
|
|
struct pw_core *core;
|
|
|
|
|
struct pw_type *t;
|
|
|
|
|
|
|
|
|
|
struct pw_remote *remote;
|
|
|
|
|
struct spa_hook remote_listener;
|
|
|
|
|
|
|
|
|
|
struct pw_node *node;
|
|
|
|
|
|
|
|
|
|
struct spa_node impl_node;
|
|
|
|
|
const struct spa_node_callbacks *callbacks;
|
|
|
|
|
void *callbacks_data;
|
|
|
|
|
struct spa_port_io *port_io;
|
|
|
|
|
struct spa_port_info port_info;
|
|
|
|
|
|
|
|
|
|
struct spa_audio_info_raw format;
|
|
|
|
|
|
|
|
|
|
struct buffer buffers[32];
|
|
|
|
|
int n_buffers;
|
|
|
|
|
struct spa_list empty;
|
|
|
|
|
|
|
|
|
|
} snd_pcm_pipewire_t;
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_pipewire_stop(snd_pcm_ioplug_t *io);
|
|
|
|
|
|
|
|
|
|
static int pcm_poll_block_check(snd_pcm_ioplug_t *io)
|
|
|
|
|
{
|
|
|
|
|
uint64_t val;
|
|
|
|
|
snd_pcm_sframes_t avail;
|
|
|
|
|
snd_pcm_pipewire_t *pw = io->private_data;
|
|
|
|
|
|
|
|
|
|
if (io->state == SND_PCM_STATE_RUNNING ||
|
|
|
|
|
(io->state == SND_PCM_STATE_PREPARED && io->stream == SND_PCM_STREAM_CAPTURE)) {
|
|
|
|
|
avail = snd_pcm_avail_update(io->pcm);
|
|
|
|
|
if (avail >= 0 && avail < pw->min_avail) {
|
|
|
|
|
read(io->poll_fd, &val, sizeof(val));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int pcm_poll_unblock_check(snd_pcm_ioplug_t *io)
|
|
|
|
|
{
|
|
|
|
|
uint64_t val = 1;
|
|
|
|
|
snd_pcm_sframes_t avail;
|
|
|
|
|
snd_pcm_pipewire_t *pw = io->private_data;
|
|
|
|
|
|
|
|
|
|
avail = snd_pcm_avail_update(io->pcm);
|
2017-11-10 13:39:59 +01:00
|
|
|
if (avail < 0 || avail >= pw->min_avail || pw->error) {
|
2017-10-19 11:50:14 +02:00
|
|
|
write(pw->fd, &val, sizeof(val));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void snd_pcm_pipewire_free(snd_pcm_pipewire_t *pw)
|
|
|
|
|
{
|
|
|
|
|
if (pw) {
|
2017-11-10 13:39:59 +01:00
|
|
|
if (pw->main_loop)
|
|
|
|
|
pw_thread_loop_stop(pw->main_loop);
|
2017-10-19 11:50:14 +02:00
|
|
|
if (pw->core)
|
|
|
|
|
pw_core_destroy(pw->core);
|
|
|
|
|
if (pw->main_loop)
|
|
|
|
|
pw_thread_loop_destroy(pw->main_loop);
|
|
|
|
|
if (pw->loop)
|
|
|
|
|
pw_loop_destroy(pw->loop);
|
|
|
|
|
if (pw->fd >= 0)
|
|
|
|
|
close(pw->fd);
|
|
|
|
|
free(pw);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_pipewire_close(snd_pcm_ioplug_t *io)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *pw = io->private_data;
|
|
|
|
|
snd_pcm_pipewire_free(pw);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_pipewire_poll_revents(snd_pcm_ioplug_t *io,
|
|
|
|
|
struct pollfd *pfds, unsigned int nfds,
|
|
|
|
|
unsigned short *revents)
|
|
|
|
|
{
|
2017-11-10 13:39:59 +01:00
|
|
|
snd_pcm_pipewire_t *pw = io->private_data;
|
|
|
|
|
|
2017-10-19 11:50:14 +02:00
|
|
|
assert(pfds && nfds == 1 && revents);
|
|
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
if (pw->error)
|
|
|
|
|
return -EBADFD;
|
|
|
|
|
|
2017-10-19 11:50:14 +02:00
|
|
|
*revents = pfds[0].revents & ~(POLLIN | POLLOUT);
|
|
|
|
|
if (pfds[0].revents & POLLIN && !pcm_poll_block_check(io))
|
|
|
|
|
*revents |= (io->stream == SND_PCM_STREAM_PLAYBACK) ? POLLOUT : POLLIN;
|
2017-11-10 13:39:59 +01:00
|
|
|
|
2017-10-19 11:50:14 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static snd_pcm_sframes_t snd_pcm_pipewire_pointer(snd_pcm_ioplug_t *io)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *pw = io->private_data;
|
|
|
|
|
return pw->hw_ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
snd_pcm_pipewire_process(snd_pcm_pipewire_t *pw, struct buffer *b)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_ioplug_t *io = &pw->io;
|
|
|
|
|
const snd_pcm_channel_area_t *areas;
|
|
|
|
|
snd_pcm_channel_area_t *pwareas;
|
|
|
|
|
snd_pcm_uframes_t xfer = 0;
|
2017-11-14 18:00:55 +01:00
|
|
|
unsigned int channel, bps, bpf;
|
2017-10-19 11:50:14 +02:00
|
|
|
snd_pcm_uframes_t nframes;
|
2017-11-10 13:39:59 +01:00
|
|
|
uint32_t index = 0, nbytes, maxsize, avail;
|
|
|
|
|
void *ptr;
|
2017-10-19 11:50:14 +02:00
|
|
|
|
|
|
|
|
bps = io->channels * pw->sample_bits;
|
2017-11-14 18:00:55 +01:00
|
|
|
bpf = bps / 8;
|
2017-10-19 11:50:14 +02:00
|
|
|
|
|
|
|
|
pwareas = alloca(io->channels * sizeof(snd_pcm_channel_area_t));
|
|
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
do {
|
|
|
|
|
|
|
|
|
|
if (b->rb) {
|
|
|
|
|
int32_t filled;
|
|
|
|
|
uint32_t offset;
|
|
|
|
|
|
2017-11-14 18:00:55 +01:00
|
|
|
filled = spa_ringbuffer_get_write_index(&b->rb->ringbuffer, &index);
|
|
|
|
|
avail = b->rb->ringbuffer.size - filled;
|
2017-11-10 13:39:59 +01:00
|
|
|
offset = index % b->rb->ringbuffer.size;
|
2017-11-14 18:00:55 +01:00
|
|
|
avail = SPA_MIN(avail, b->rb->ringbuffer.size - offset);
|
|
|
|
|
avail = SPA_MIN(avail, pw->min_avail * bpf);
|
|
|
|
|
nbytes = avail;
|
2017-11-10 13:39:59 +01:00
|
|
|
|
|
|
|
|
ptr = SPA_MEMBER(b->ptr, offset, void);
|
|
|
|
|
pw_log_trace("%d %d %d %d %p", nbytes, avail, filled, offset, ptr);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
avail = b->buffer->datas[0].chunk->size;
|
2017-11-14 18:00:55 +01:00
|
|
|
avail = SPA_MIN(avail, pw->min_avail * bpf);
|
2017-11-10 13:39:59 +01:00
|
|
|
maxsize = b->buffer->datas[0].maxsize;
|
2017-11-14 18:00:55 +01:00
|
|
|
nbytes = SPA_MIN(avail, maxsize);
|
2017-11-10 13:39:59 +01:00
|
|
|
ptr = b->ptr;
|
|
|
|
|
}
|
2017-11-14 18:00:55 +01:00
|
|
|
nframes = nbytes / bpf;
|
2017-11-10 13:39:59 +01:00
|
|
|
|
2017-10-19 11:50:14 +02:00
|
|
|
for (channel = 0; channel < io->channels; channel++) {
|
2017-11-10 13:39:59 +01:00
|
|
|
pwareas[channel].addr = ptr;
|
2017-10-19 11:50:14 +02:00
|
|
|
pwareas[channel].first = channel * pw->sample_bits;
|
|
|
|
|
pwareas[channel].step = bps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (io->state != SND_PCM_STATE_RUNNING) {
|
|
|
|
|
if (io->stream == SND_PCM_STREAM_PLAYBACK) {
|
2017-11-14 17:28:46 +01:00
|
|
|
pw_log_trace("slience %lu frames", nframes);
|
2017-10-19 11:50:14 +02:00
|
|
|
for (channel = 0; channel < io->channels; channel++)
|
|
|
|
|
snd_pcm_area_silence(&pwareas[channel], 0, nframes, io->format);
|
2017-11-10 13:39:59 +01:00
|
|
|
goto done;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
areas = snd_pcm_ioplug_mmap_areas(io);
|
|
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
xfer = 0;
|
2017-10-19 11:50:14 +02:00
|
|
|
while (xfer < nframes) {
|
|
|
|
|
snd_pcm_uframes_t frames = nframes - xfer;
|
|
|
|
|
snd_pcm_uframes_t offset = pw->hw_ptr;
|
|
|
|
|
snd_pcm_uframes_t cont = io->buffer_size - offset;
|
|
|
|
|
|
|
|
|
|
if (cont < frames)
|
|
|
|
|
frames = cont;
|
|
|
|
|
|
|
|
|
|
if (io->stream == SND_PCM_STREAM_PLAYBACK)
|
|
|
|
|
snd_pcm_areas_copy(pwareas, xfer,
|
|
|
|
|
areas, offset,
|
|
|
|
|
io->channels, frames, io->format);
|
|
|
|
|
else
|
|
|
|
|
snd_pcm_areas_copy(areas, offset,
|
|
|
|
|
pwareas, xfer,
|
|
|
|
|
io->channels, frames, io->format);
|
|
|
|
|
|
|
|
|
|
pw->hw_ptr += frames;
|
|
|
|
|
pw->hw_ptr %= io->buffer_size;
|
|
|
|
|
xfer += frames;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pcm_poll_unblock_check(io); /* unblock socket for polling if needed */
|
|
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
done:
|
|
|
|
|
if (b->rb)
|
|
|
|
|
spa_ringbuffer_write_update(&b->rb->ringbuffer, index + nbytes);
|
|
|
|
|
|
|
|
|
|
avail -= nbytes;
|
|
|
|
|
} while (avail > 0);
|
|
|
|
|
|
2017-10-19 11:50:14 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_pipewire_prepare(snd_pcm_ioplug_t *io)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *pw = io->private_data;
|
|
|
|
|
snd_pcm_sw_params_t *swparams;
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
pw->hw_ptr = 0;
|
|
|
|
|
|
|
|
|
|
pw->min_avail = io->period_size;
|
|
|
|
|
snd_pcm_sw_params_alloca(&swparams);
|
|
|
|
|
err = snd_pcm_sw_params_current(io->pcm, swparams);
|
|
|
|
|
if (err == 0) {
|
|
|
|
|
snd_pcm_sw_params_get_avail_min(swparams, &pw->min_avail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* deactivate PipeWire connections if this is XRUN recovery */
|
|
|
|
|
snd_pcm_pipewire_stop(io);
|
|
|
|
|
|
|
|
|
|
if (io->stream == SND_PCM_STREAM_PLAYBACK)
|
|
|
|
|
pcm_poll_unblock_check(io); /* playback pcm initially accepts writes */
|
|
|
|
|
else
|
|
|
|
|
pcm_poll_block_check(io); /* block capture pcm if that's XRUN recovery */
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_pipewire_start(snd_pcm_ioplug_t *io)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *pw = io->private_data;
|
|
|
|
|
|
|
|
|
|
pw_node_set_active(pw->node, true);
|
|
|
|
|
pw->activated = 1;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_pipewire_stop(snd_pcm_ioplug_t *io)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *pw = io->private_data;
|
|
|
|
|
|
|
|
|
|
if (pw->activated) {
|
|
|
|
|
pw_node_set_active(pw->node, false);
|
|
|
|
|
pw->activated = 0;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static snd_pcm_ioplug_callback_t pipewire_pcm_callback = {
|
|
|
|
|
.close = snd_pcm_pipewire_close,
|
|
|
|
|
.start = snd_pcm_pipewire_start,
|
|
|
|
|
.stop = snd_pcm_pipewire_stop,
|
|
|
|
|
.pointer = snd_pcm_pipewire_pointer,
|
|
|
|
|
.prepare = snd_pcm_pipewire_prepare,
|
|
|
|
|
.poll_revents = snd_pcm_pipewire_poll_revents,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define ARRAY_SIZE(ary) (sizeof(ary)/sizeof(ary[0]))
|
|
|
|
|
|
|
|
|
|
static int pipewire_set_hw_constraint(snd_pcm_pipewire_t *pw)
|
|
|
|
|
{
|
|
|
|
|
unsigned int access_list[] = {
|
|
|
|
|
SND_PCM_ACCESS_MMAP_INTERLEAVED,
|
|
|
|
|
SND_PCM_ACCESS_MMAP_NONINTERLEAVED,
|
|
|
|
|
SND_PCM_ACCESS_RW_INTERLEAVED,
|
|
|
|
|
SND_PCM_ACCESS_RW_NONINTERLEAVED
|
|
|
|
|
};
|
|
|
|
|
unsigned int format = SND_PCM_FORMAT_S16_LE;
|
|
|
|
|
unsigned int rate = 44100;
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
pw->sample_bits = snd_pcm_format_physical_width(format);
|
|
|
|
|
if ((err = snd_pcm_ioplug_set_param_list(&pw->io, SND_PCM_IOPLUG_HW_ACCESS,
|
|
|
|
|
ARRAY_SIZE(access_list), access_list)) < 0 ||
|
|
|
|
|
(err = snd_pcm_ioplug_set_param_list(&pw->io, SND_PCM_IOPLUG_HW_FORMAT,
|
|
|
|
|
1, &format)) < 0 ||
|
|
|
|
|
(err = snd_pcm_ioplug_set_param_minmax(&pw->io, SND_PCM_IOPLUG_HW_CHANNELS,
|
|
|
|
|
2, 2)) < 0 ||
|
|
|
|
|
(err = snd_pcm_ioplug_set_param_minmax(&pw->io, SND_PCM_IOPLUG_HW_RATE,
|
|
|
|
|
rate, rate)) < 0 ||
|
|
|
|
|
(err = snd_pcm_ioplug_set_param_minmax(&pw->io, SND_PCM_IOPLUG_HW_PERIOD_BYTES,
|
|
|
|
|
128, 64*1024)) < 0 ||
|
|
|
|
|
(err = snd_pcm_ioplug_set_param_minmax(&pw->io, SND_PCM_IOPLUG_HW_PERIODS,
|
|
|
|
|
2, 64)) < 0)
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_send_command(struct spa_node *node, const struct spa_command *command)
|
|
|
|
|
{
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_set_callbacks(struct spa_node *node,
|
|
|
|
|
const struct spa_node_callbacks *callbacks, void *data)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *d = SPA_CONTAINER_OF(node, snd_pcm_pipewire_t, impl_node);
|
|
|
|
|
d->callbacks = callbacks;
|
|
|
|
|
d->callbacks_data = data;
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_get_n_ports(struct spa_node *node,
|
|
|
|
|
uint32_t *n_input_ports,
|
|
|
|
|
uint32_t *max_input_ports,
|
|
|
|
|
uint32_t *n_output_ports,
|
|
|
|
|
uint32_t *max_output_ports)
|
|
|
|
|
{
|
2017-11-10 13:39:59 +01:00
|
|
|
snd_pcm_pipewire_t *d = SPA_CONTAINER_OF(node, snd_pcm_pipewire_t, impl_node);
|
|
|
|
|
|
|
|
|
|
pw_log_debug("%d %d", d->io.stream, SND_PCM_STREAM_PLAYBACK);
|
|
|
|
|
if (d->io.stream == SND_PCM_STREAM_PLAYBACK) {
|
|
|
|
|
*n_input_ports = *max_input_ports = 0;
|
|
|
|
|
*n_output_ports = *max_output_ports = 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
*n_input_ports = *max_input_ports = 1;
|
|
|
|
|
*n_output_ports = *max_output_ports = 0;
|
|
|
|
|
}
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_get_port_ids(struct spa_node *node,
|
|
|
|
|
uint32_t n_input_ports,
|
|
|
|
|
uint32_t *input_ids,
|
|
|
|
|
uint32_t n_output_ports,
|
|
|
|
|
uint32_t *output_ids)
|
|
|
|
|
{
|
2017-11-10 13:39:59 +01:00
|
|
|
snd_pcm_pipewire_t *d = SPA_CONTAINER_OF(node, snd_pcm_pipewire_t, impl_node);
|
|
|
|
|
|
|
|
|
|
pw_log_debug("%d %d", d->io.stream, SND_PCM_STREAM_PLAYBACK);
|
|
|
|
|
if (d->io.stream == SND_PCM_STREAM_PLAYBACK) {
|
|
|
|
|
if (n_output_ports > 0)
|
|
|
|
|
output_ids[0] = 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (n_input_ports > 0)
|
|
|
|
|
input_ids[0] = 0;
|
|
|
|
|
}
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_port_set_io(struct spa_node *node, enum spa_direction direction, uint32_t port_id,
|
|
|
|
|
struct spa_port_io *io)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *d = SPA_CONTAINER_OF(node, snd_pcm_pipewire_t, impl_node);
|
|
|
|
|
d->port_io = io;
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
static int impl_port_get_info(struct spa_node *node, enum spa_direction direction, uint32_t port_id,
|
|
|
|
|
const struct spa_port_info **info)
|
2017-10-19 11:50:14 +02:00
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *d = SPA_CONTAINER_OF(node, snd_pcm_pipewire_t, impl_node);
|
|
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
d->port_info.flags = SPA_PORT_INFO_FLAG_CAN_USE_BUFFERS;
|
|
|
|
|
d->port_info.rate = 0;
|
|
|
|
|
d->port_info.props = NULL;
|
2017-10-19 11:50:14 +02:00
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
*info = &d->port_info;
|
2017-10-19 11:50:14 +02:00
|
|
|
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
static int impl_port_enum_params(struct spa_node *node,
|
|
|
|
|
enum spa_direction direction, uint32_t port_id,
|
|
|
|
|
uint32_t id, uint32_t *index,
|
2017-11-14 17:28:46 +01:00
|
|
|
const struct spa_pod *filter,
|
|
|
|
|
struct spa_pod **result,
|
2017-11-10 13:39:59 +01:00
|
|
|
struct spa_pod_builder *builder)
|
2017-10-19 11:50:14 +02:00
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *d = SPA_CONTAINER_OF(node, snd_pcm_pipewire_t, impl_node);
|
|
|
|
|
struct pw_type *t = d->t;
|
2017-11-10 13:39:59 +01:00
|
|
|
int bps;
|
2017-10-19 11:50:14 +02:00
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
bps = d->io.channels * (d->sample_bits / 8);
|
2017-10-19 11:50:14 +02:00
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
if (id == t->param.idList) {
|
|
|
|
|
uint32_t list[] = { t->param.idEnumFormat,
|
|
|
|
|
t->param.idFormat,
|
|
|
|
|
t->param.idBuffers,
|
|
|
|
|
t->param.idMeta };
|
2017-10-19 11:50:14 +02:00
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
if (*index < SPA_N_ELEMENTS(list))
|
2017-11-14 17:28:46 +01:00
|
|
|
*result = spa_pod_builder_object(builder,
|
2017-11-10 13:39:59 +01:00
|
|
|
id, t->param.List,
|
|
|
|
|
":", t->param.listId, "I", list[*index]);
|
|
|
|
|
else
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-11-10 13:39:59 +01:00
|
|
|
}
|
|
|
|
|
else if (id == t->param.idEnumFormat) {
|
|
|
|
|
if (*index != 0)
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-11-10 13:39:59 +01:00
|
|
|
|
2017-11-14 17:28:46 +01:00
|
|
|
*result = spa_pod_builder_object(builder,
|
2017-11-10 13:39:59 +01:00
|
|
|
id, d->type.format,
|
|
|
|
|
"I", d->type.media_type.audio,
|
|
|
|
|
"I", d->type.media_subtype.raw,
|
|
|
|
|
":", d->type.format_audio.format, "I", d->type.audio_format.S16,
|
|
|
|
|
":", d->type.format_audio.channels, "i", 2,
|
|
|
|
|
":", d->type.format_audio.rate, "i", 44100);
|
|
|
|
|
}
|
|
|
|
|
else if (id == t->param.idFormat) {
|
|
|
|
|
if (*index != 0 || d->format.format == 0)
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-11-10 13:39:59 +01:00
|
|
|
|
2017-11-14 17:28:46 +01:00
|
|
|
*result = spa_pod_builder_object(builder,
|
2017-11-10 13:39:59 +01:00
|
|
|
id, d->type.format,
|
|
|
|
|
"I", d->type.media_type.audio,
|
|
|
|
|
"I", d->type.media_subtype.raw,
|
|
|
|
|
":", d->type.format_audio.format, "I", d->format.format,
|
|
|
|
|
":", d->type.format_audio.channels, "i", d->format.channels,
|
|
|
|
|
":", d->type.format_audio.rate, "i", d->format.rate);
|
|
|
|
|
}
|
|
|
|
|
else if (id == t->param.idBuffers) {
|
|
|
|
|
if (*index != 0 || d->format.format == 0)
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-11-10 13:39:59 +01:00
|
|
|
|
2017-11-14 17:28:46 +01:00
|
|
|
*result = spa_pod_builder_object(builder,
|
2017-11-10 13:39:59 +01:00
|
|
|
id, t->param_buffers.Buffers,
|
|
|
|
|
":", t->param_buffers.size, "iru", d->min_avail * bps,
|
|
|
|
|
2, d->min_avail * bps, INT32_MAX / bps,
|
|
|
|
|
":", t->param_buffers.stride, "i", 0,
|
|
|
|
|
":", t->param_buffers.buffers, "iru", d->io.buffer_size / d->min_avail,
|
2017-10-19 11:50:14 +02:00
|
|
|
2, 2, 32,
|
2017-11-10 13:39:59 +01:00
|
|
|
":", t->param_buffers.align, "i", 16);
|
|
|
|
|
}
|
|
|
|
|
else if (id == t->param.idMeta) {
|
|
|
|
|
if (d->format.format == 0)
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-11-10 13:39:59 +01:00
|
|
|
|
|
|
|
|
switch (*index) {
|
|
|
|
|
case 0:
|
2017-11-14 17:28:46 +01:00
|
|
|
*result = spa_pod_builder_object(builder,
|
2017-11-10 13:39:59 +01:00
|
|
|
id, t->param_meta.Meta,
|
|
|
|
|
":", t->param_meta.type, "I", t->meta.Header,
|
|
|
|
|
":", t->param_meta.size, "i", sizeof(struct spa_meta_header));
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
2017-11-14 17:28:46 +01:00
|
|
|
*result = spa_pod_builder_object(builder,
|
2017-11-10 13:39:59 +01:00
|
|
|
id, t->param_meta.Meta,
|
|
|
|
|
":", t->param_meta.type, "I", t->meta.Ringbuffer,
|
|
|
|
|
":", t->param_meta.size, "i", sizeof(struct spa_meta_ringbuffer),
|
|
|
|
|
":", t->param_meta.ringbufferSize, "iru", d->io.buffer_size * bps,
|
|
|
|
|
2, d->min_avail * bps, INT32_MAX / bps,
|
|
|
|
|
":", t->param_meta.ringbufferStride, "i", 0,
|
|
|
|
|
":", t->param_meta.ringbufferBlocks, "i", 1,
|
|
|
|
|
":", t->param_meta.ringbufferAlign, "i", 16);
|
|
|
|
|
break;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-11-10 13:39:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2017-11-14 17:28:46 +01:00
|
|
|
return -ENOENT;
|
2017-10-19 11:50:14 +02:00
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
(*index)++;
|
2017-10-19 11:50:14 +02:00
|
|
|
|
2017-11-14 17:28:46 +01:00
|
|
|
return 1;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
static int port_set_format(struct spa_node *node,
|
|
|
|
|
enum spa_direction direction, uint32_t port_id,
|
2017-11-14 17:28:46 +01:00
|
|
|
uint32_t flags, const struct spa_pod *format)
|
2017-10-19 11:50:14 +02:00
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *d = SPA_CONTAINER_OF(node, snd_pcm_pipewire_t, impl_node);
|
|
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
if (format == NULL) {
|
|
|
|
|
d->format.format = 0;
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-11-10 13:39:59 +01:00
|
|
|
}
|
2017-10-19 11:50:14 +02:00
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
if (spa_format_audio_raw_parse(format, &d->format, &d->type.format_audio) < 0)
|
2017-11-14 17:28:46 +01:00
|
|
|
return -EINVAL;
|
2017-10-19 11:50:14 +02:00
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
if (d->format.format != d->type.audio_format.S16)
|
2017-11-14 17:28:46 +01:00
|
|
|
return -EINVAL;
|
2017-10-19 11:50:14 +02:00
|
|
|
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
static int impl_port_set_param(struct spa_node *node,
|
|
|
|
|
enum spa_direction direction, uint32_t port_id,
|
|
|
|
|
uint32_t id, uint32_t flags,
|
2017-11-14 17:28:46 +01:00
|
|
|
const struct spa_pod *param)
|
2017-10-19 11:50:14 +02:00
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *d = SPA_CONTAINER_OF(node, snd_pcm_pipewire_t, impl_node);
|
2017-11-10 13:39:59 +01:00
|
|
|
struct pw_type *t = d->t;
|
2017-10-19 11:50:14 +02:00
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
if (id == t->param.idFormat) {
|
|
|
|
|
return port_set_format(node, direction, port_id, flags, param);
|
|
|
|
|
}
|
|
|
|
|
else
|
2017-11-14 17:28:46 +01:00
|
|
|
return -ENOENT;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_port_use_buffers(struct spa_node *node, enum spa_direction direction, uint32_t port_id,
|
|
|
|
|
struct spa_buffer **buffers, uint32_t n_buffers)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *d = SPA_CONTAINER_OF(node, snd_pcm_pipewire_t, impl_node);
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < n_buffers; i++) {
|
|
|
|
|
struct buffer *b = &d->buffers[i];
|
|
|
|
|
struct spa_data *datas = buffers[i]->datas;
|
|
|
|
|
|
|
|
|
|
if (datas[0].data != NULL) {
|
|
|
|
|
b->ptr = datas[0].data;
|
|
|
|
|
b->mapped = false;
|
|
|
|
|
}
|
|
|
|
|
else if (datas[0].type == d->type.data.MemFd ||
|
|
|
|
|
datas[0].type == d->type.data.DmaBuf) {
|
|
|
|
|
b->ptr = mmap(NULL, datas[0].maxsize + datas[0].mapoffset, PROT_WRITE,
|
|
|
|
|
MAP_SHARED, datas[0].fd, 0);
|
|
|
|
|
if (b->ptr == MAP_FAILED) {
|
|
|
|
|
pw_log_error("failed to buffer mem");
|
2017-11-14 17:28:46 +01:00
|
|
|
return -errno;
|
2017-10-19 11:50:14 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
b->ptr = SPA_MEMBER(b->ptr, datas[0].mapoffset, void);
|
|
|
|
|
b->mapped = true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
pw_log_error("invalid buffer mem");
|
2017-11-14 17:28:46 +01:00
|
|
|
return -EINVAL;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
b->size = datas[0].maxsize;
|
|
|
|
|
b->buffer = buffers[i];
|
2017-11-10 13:39:59 +01:00
|
|
|
b->h = spa_buffer_find_meta(b->buffer, d->type.meta.Header);
|
|
|
|
|
b->rb = spa_buffer_find_meta(b->buffer, d->type.meta.Ringbuffer);
|
2017-10-19 11:50:14 +02:00
|
|
|
pw_log_info("got buffer %d size %zd", i, b->size);
|
|
|
|
|
spa_list_append(&d->empty, &b->link);
|
|
|
|
|
b->used = false;
|
|
|
|
|
}
|
|
|
|
|
d->n_buffers = n_buffers;
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void reuse_buffer(snd_pcm_pipewire_t *d, uint32_t id)
|
|
|
|
|
{
|
|
|
|
|
struct buffer *b = &d->buffers[id];
|
|
|
|
|
if (b->used) {
|
|
|
|
|
pw_log_trace("reuse buffer %d", id);
|
|
|
|
|
spa_list_append(&d->empty, &b->link);
|
|
|
|
|
b->used = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_port_reuse_buffer(struct spa_node *node, uint32_t port_id, uint32_t buffer_id)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *d = SPA_CONTAINER_OF(node, snd_pcm_pipewire_t, impl_node);
|
|
|
|
|
reuse_buffer(d, buffer_id);
|
2017-11-14 17:28:46 +01:00
|
|
|
return 0;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_node_process_input(struct spa_node *node)
|
|
|
|
|
{
|
2017-11-10 13:39:59 +01:00
|
|
|
snd_pcm_pipewire_t *d = SPA_CONTAINER_OF(node, snd_pcm_pipewire_t, impl_node);
|
|
|
|
|
struct spa_port_io *io = d->port_io;
|
|
|
|
|
struct buffer *b;
|
|
|
|
|
|
|
|
|
|
if (io->buffer_id >= d->n_buffers) {
|
2017-11-14 17:28:46 +01:00
|
|
|
io->status = -EINVAL;
|
|
|
|
|
return -EINVAL;
|
2017-11-10 13:39:59 +01:00
|
|
|
}
|
|
|
|
|
b = &d->buffers[io->buffer_id];
|
|
|
|
|
|
|
|
|
|
snd_pcm_pipewire_process(d, b);
|
|
|
|
|
|
2017-11-14 17:28:46 +01:00
|
|
|
return io->status = SPA_STATUS_NEED_BUFFER;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_node_process_output(struct spa_node *node)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *d = SPA_CONTAINER_OF(node, snd_pcm_pipewire_t, impl_node);
|
|
|
|
|
struct buffer *b;
|
|
|
|
|
struct spa_port_io *io = d->port_io;
|
|
|
|
|
|
|
|
|
|
if (io->buffer_id < d->n_buffers) {
|
|
|
|
|
reuse_buffer(d, io->buffer_id);
|
|
|
|
|
io->buffer_id = SPA_ID_INVALID;
|
|
|
|
|
}
|
|
|
|
|
if (spa_list_is_empty(&d->empty)) {
|
|
|
|
|
pw_log_error("alsa-pipewire %p: out of buffers", d);
|
2017-11-14 17:28:46 +01:00
|
|
|
return -EPIPE;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
b = spa_list_first(&d->empty, struct buffer, link);
|
|
|
|
|
spa_list_remove(&b->link);
|
2017-11-10 13:39:59 +01:00
|
|
|
|
2017-10-19 11:50:14 +02:00
|
|
|
b->used = true;
|
2017-11-10 13:39:59 +01:00
|
|
|
io->buffer_id = b->buffer->id;
|
2017-10-19 11:50:14 +02:00
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
pw_log_trace("alsa-pipewire %p: process buffer %d", d, io->buffer_id);
|
2017-10-19 11:50:14 +02:00
|
|
|
snd_pcm_pipewire_process(d, b);
|
|
|
|
|
|
2017-11-14 17:28:46 +01:00
|
|
|
return io->status = SPA_STATUS_HAVE_BUFFER;
|
2017-10-19 11:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct spa_node impl_node = {
|
|
|
|
|
SPA_VERSION_NODE,
|
|
|
|
|
.send_command = impl_send_command,
|
|
|
|
|
.set_callbacks = impl_set_callbacks,
|
|
|
|
|
.get_n_ports = impl_get_n_ports,
|
|
|
|
|
.get_port_ids = impl_get_port_ids,
|
|
|
|
|
.port_set_io = impl_port_set_io,
|
|
|
|
|
.port_get_info = impl_port_get_info,
|
|
|
|
|
.port_enum_params = impl_port_enum_params,
|
2017-11-10 13:39:59 +01:00
|
|
|
.port_set_param = impl_port_set_param,
|
2017-10-19 11:50:14 +02:00
|
|
|
.port_use_buffers = impl_port_use_buffers,
|
|
|
|
|
.port_reuse_buffer = impl_port_reuse_buffer,
|
|
|
|
|
.process_output = impl_node_process_output,
|
|
|
|
|
.process_input = impl_node_process_input,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void on_state_changed(void *data, enum pw_remote_state old,
|
|
|
|
|
enum pw_remote_state state, const char *error)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *pw = data;
|
|
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
|
case PW_REMOTE_STATE_ERROR:
|
2017-11-10 13:39:59 +01:00
|
|
|
pw->error = true;
|
|
|
|
|
pcm_poll_unblock_check(&pw->io);
|
|
|
|
|
/** FALLTHROUGH */
|
2017-10-19 11:50:14 +02:00
|
|
|
case PW_REMOTE_STATE_CONNECTED:
|
|
|
|
|
pw_thread_loop_signal(pw->main_loop, false);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_remote_events remote_events = {
|
|
|
|
|
PW_VERSION_REMOTE_EVENTS,
|
|
|
|
|
.state_changed = on_state_changed,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int pipewire_node_create(snd_pcm_pipewire_t *pw,
|
|
|
|
|
const char *node_name,
|
|
|
|
|
const char *target,
|
|
|
|
|
bool can_fallback)
|
|
|
|
|
{
|
|
|
|
|
const char *error = NULL;
|
|
|
|
|
enum pw_remote_state state;
|
|
|
|
|
|
|
|
|
|
pw_thread_loop_lock(pw->main_loop);
|
|
|
|
|
|
|
|
|
|
if (pw_remote_connect(pw->remote) < 0) {
|
|
|
|
|
error = "connect failed";
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
state = pw_remote_get_state(pw->remote, &error);
|
|
|
|
|
if (state == PW_REMOTE_STATE_ERROR)
|
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
if (state == PW_REMOTE_STATE_CONNECTED)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
pw_thread_loop_wait(pw->main_loop);
|
|
|
|
|
}
|
|
|
|
|
pw_log_debug("node %s", target);
|
|
|
|
|
pw->node = pw_node_new(pw->core,
|
|
|
|
|
node_name,
|
|
|
|
|
pw_properties_new(PW_NODE_PROP_AUTOCONNECT, "1",
|
|
|
|
|
PW_NODE_PROP_TARGET_NODE, target,
|
|
|
|
|
NULL),
|
|
|
|
|
0);
|
|
|
|
|
pw->impl_node = impl_node;
|
|
|
|
|
pw_node_set_implementation(pw->node, &pw->impl_node);
|
|
|
|
|
pw_node_register(pw->node, NULL, NULL);
|
|
|
|
|
|
|
|
|
|
pw_remote_export(pw->remote, pw->node);
|
|
|
|
|
pw_thread_loop_unlock(pw->main_loop);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
if (!can_fallback)
|
|
|
|
|
SNDERR("PipeWire: Unable to connect: %s\n", error);
|
|
|
|
|
|
|
|
|
|
pw_thread_loop_unlock(pw->main_loop);
|
|
|
|
|
|
|
|
|
|
return -ECONNREFUSED;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_pipewire_open(snd_pcm_t **pcmp, const char *name,
|
|
|
|
|
const char *node_name,
|
|
|
|
|
const char *playback_node,
|
|
|
|
|
const char *capture_node,
|
|
|
|
|
snd_pcm_stream_t stream, int mode)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_pipewire_t *pw;
|
|
|
|
|
int err;
|
|
|
|
|
static unsigned int num = 0;
|
|
|
|
|
char pipewire_node_name[32];
|
|
|
|
|
const char *target;
|
|
|
|
|
|
|
|
|
|
assert(pcmp);
|
|
|
|
|
pw = calloc(1, sizeof(*pw));
|
|
|
|
|
if (!pw)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
2017-11-10 13:39:59 +01:00
|
|
|
pw_log_debug("open %s %d %d", name, stream, mode);
|
|
|
|
|
|
2017-10-19 11:50:14 +02:00
|
|
|
pw->fd = -1;
|
|
|
|
|
pw->io.poll_fd = -1;
|
|
|
|
|
spa_list_init(&pw->empty);
|
|
|
|
|
|
|
|
|
|
if (node_name == NULL)
|
|
|
|
|
err = snprintf(pipewire_node_name, sizeof(pipewire_node_name),
|
|
|
|
|
"alsa-pipewire.%s%s.%d.%d", name,
|
|
|
|
|
stream == SND_PCM_STREAM_PLAYBACK ? "P" : "C",
|
|
|
|
|
getpid(), num++);
|
|
|
|
|
else
|
|
|
|
|
err = snprintf(pipewire_node_name, sizeof(pipewire_node_name),
|
|
|
|
|
"%s", node_name);
|
|
|
|
|
|
|
|
|
|
if (stream == SND_PCM_STREAM_PLAYBACK)
|
|
|
|
|
target = playback_node;
|
|
|
|
|
else
|
|
|
|
|
target = capture_node;
|
|
|
|
|
|
|
|
|
|
pw->loop = pw_loop_new(NULL);
|
|
|
|
|
pw->main_loop = pw_thread_loop_new(pw->loop, "alsa-pipewire");
|
|
|
|
|
pw->core = pw_core_new(pw->loop, NULL);
|
|
|
|
|
pw->t = pw_core_get_type(pw->core);
|
|
|
|
|
init_type(&pw->type, pw->t->map);
|
|
|
|
|
spa_debug_set_type_map(pw->t->map);
|
|
|
|
|
|
|
|
|
|
pw->remote = pw_remote_new(pw->core, NULL, 0);
|
|
|
|
|
pw_remote_add_listener(pw->remote, &pw->remote_listener, &remote_events, pw);
|
|
|
|
|
|
|
|
|
|
err = pw_thread_loop_start(pw->main_loop);
|
|
|
|
|
if (err < 0) {
|
|
|
|
|
snd_pcm_pipewire_free(pw);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pw->fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
|
|
|
|
|
|
|
|
|
|
pw->io.version = SND_PCM_IOPLUG_VERSION;
|
|
|
|
|
pw->io.name = "ALSA <-> PipeWire PCM I/O Plugin";
|
|
|
|
|
pw->io.callback = &pipewire_pcm_callback;
|
|
|
|
|
pw->io.private_data = pw;
|
|
|
|
|
pw->io.poll_fd = pw->fd;
|
|
|
|
|
pw->io.poll_events = POLLIN;
|
|
|
|
|
pw->io.mmap_rw = 1;
|
|
|
|
|
|
|
|
|
|
err = snd_pcm_ioplug_create(&pw->io, name, stream, mode);
|
|
|
|
|
if (err < 0) {
|
|
|
|
|
snd_pcm_pipewire_free(pw);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2017-11-10 13:39:59 +01:00
|
|
|
pw_log_debug("open %s %d %d", name, pw->io.stream, mode);
|
2017-10-19 11:50:14 +02:00
|
|
|
|
|
|
|
|
err = pipewire_set_hw_constraint(pw);
|
|
|
|
|
if (err < 0) {
|
|
|
|
|
snd_pcm_ioplug_delete(&pw->io);
|
2017-11-10 13:39:59 +01:00
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = pipewire_node_create(pw, pipewire_node_name, target, false);
|
|
|
|
|
if (err < 0) {
|
|
|
|
|
snd_pcm_pipewire_free(pw);
|
2017-10-19 11:50:14 +02:00
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*pcmp = pw->io.pcm;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SND_PCM_PLUGIN_DEFINE_FUNC(pipewire)
|
|
|
|
|
{
|
|
|
|
|
snd_config_iterator_t i, next;
|
|
|
|
|
const char *node_name = NULL;
|
|
|
|
|
const char *playback_node = NULL;
|
|
|
|
|
const char *capture_node = NULL;
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
pw_init(NULL, NULL);
|
|
|
|
|
|
|
|
|
|
snd_config_for_each(i, next, conf) {
|
|
|
|
|
snd_config_t *n = snd_config_iterator_entry(i);
|
|
|
|
|
const char *id;
|
|
|
|
|
if (snd_config_get_id(n, &id) < 0)
|
|
|
|
|
continue;
|
|
|
|
|
if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 || strcmp(id, "hint") == 0)
|
|
|
|
|
continue;
|
|
|
|
|
if (strcmp(id, "name") == 0) {
|
|
|
|
|
snd_config_get_string(n, &node_name);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (strcmp(id, "playback_node") == 0) {
|
|
|
|
|
snd_config_get_string(n, &playback_node);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (strcmp(id, "capture_node") == 0) {
|
|
|
|
|
snd_config_get_string(n, &capture_node);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
SNDERR("Unknown field %s", id);
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = snd_pcm_pipewire_open(pcmp, name, node_name, playback_node, capture_node, stream, mode);
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SND_PCM_PLUGIN_SYMBOL(pipewire);
|