2018-02-15 17:54:08 +01:00
|
|
|
/* PipeWire
|
|
|
|
|
* Copyright (C) 2018 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 Library General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library 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
|
|
|
|
|
* Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <math.h>
|
2018-04-24 17:03:56 +02:00
|
|
|
#include <time.h>
|
2018-02-15 17:54:08 +01:00
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <spa/node/node.h>
|
|
|
|
|
#include <spa/utils/hook.h>
|
|
|
|
|
#include <spa/param/audio/format-utils.h>
|
|
|
|
|
|
|
|
|
|
#include "pipewire/core.h"
|
|
|
|
|
#include "pipewire/link.h"
|
|
|
|
|
#include "pipewire/log.h"
|
|
|
|
|
#include "pipewire/module.h"
|
|
|
|
|
#include "pipewire/type.h"
|
|
|
|
|
#include "pipewire/private.h"
|
|
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
#include "modules/spa/spa-node.h"
|
2018-02-20 09:32:40 +01:00
|
|
|
|
2018-09-28 17:47:37 +02:00
|
|
|
#define NAME "audio-dsp"
|
|
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
#define PORT_BUFFERS 1
|
2018-02-15 17:54:08 +01:00
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
extern const struct spa_handle_factory spa_floatmix_factory;
|
2018-02-15 17:54:08 +01:00
|
|
|
|
|
|
|
|
struct buffer {
|
2018-07-31 12:23:35 +02:00
|
|
|
struct spa_buffer buf;
|
|
|
|
|
struct spa_data datas[1];
|
|
|
|
|
struct spa_chunk chunk[1];
|
2018-02-15 17:54:08 +01:00
|
|
|
};
|
|
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
struct node;
|
|
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
struct port {
|
|
|
|
|
struct pw_port *port;
|
|
|
|
|
struct node *node;
|
|
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
struct buffer buffers[PORT_BUFFERS];
|
2018-02-15 17:54:08 +01:00
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
struct spa_buffer *bufs[PORT_BUFFERS];
|
2018-02-15 17:54:08 +01:00
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
struct spa_handle *spa_handle;
|
|
|
|
|
struct spa_node *spa_node;
|
2018-02-15 17:54:08 +01:00
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
float empty[4096];
|
2018-02-15 17:54:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct node {
|
2018-05-08 10:45:50 +02:00
|
|
|
struct pw_core *core;
|
2018-02-15 17:54:08 +01:00
|
|
|
struct pw_node *node;
|
|
|
|
|
|
2018-05-08 10:45:50 +02:00
|
|
|
void *user_data;
|
2018-02-15 17:54:08 +01:00
|
|
|
|
|
|
|
|
int channels;
|
2018-09-11 18:09:45 +02:00
|
|
|
uint64_t channelmask;
|
2018-02-15 17:54:08 +01:00
|
|
|
int sample_rate;
|
2018-05-08 10:45:50 +02:00
|
|
|
int max_buffer_size;
|
2018-02-15 17:54:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** \endcond */
|
|
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
static void init_buffer(struct port *port, uint32_t id)
|
|
|
|
|
{
|
|
|
|
|
struct buffer *b = &port->buffers[id];
|
|
|
|
|
b->buf.id = id;
|
|
|
|
|
b->buf.n_metas = 0;
|
|
|
|
|
b->buf.metas = NULL;
|
|
|
|
|
b->buf.n_datas = 1;
|
|
|
|
|
b->buf.datas = b->datas;
|
2018-08-23 17:47:57 +02:00
|
|
|
b->datas[0].type = SPA_DATA_MemPtr;
|
2018-07-31 12:23:35 +02:00
|
|
|
b->datas[0].flags = 0;
|
|
|
|
|
b->datas[0].fd = -1;
|
|
|
|
|
b->datas[0].mapoffset = 0;
|
|
|
|
|
b->datas[0].maxsize = sizeof(port->empty);
|
|
|
|
|
b->datas[0].data = port->empty;
|
|
|
|
|
b->datas[0].chunk = b->chunk;
|
|
|
|
|
b->datas[0].chunk->offset = 0;
|
|
|
|
|
b->datas[0].chunk->size = 0;
|
|
|
|
|
b->datas[0].chunk->stride = 0;
|
|
|
|
|
port->bufs[id] = &b->buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void init_port(struct port *p, enum spa_direction direction)
|
2018-02-15 17:54:08 +01:00
|
|
|
{
|
|
|
|
|
int i;
|
2018-07-31 12:23:35 +02:00
|
|
|
for (i = 0; i < PORT_BUFFERS; i++)
|
|
|
|
|
init_buffer(p, i);
|
2018-02-15 17:54:08 +01:00
|
|
|
}
|
2018-03-08 11:08:37 +01:00
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
static int port_use_buffers(void *data,
|
|
|
|
|
struct spa_buffer **buffers,
|
|
|
|
|
uint32_t n_buffers)
|
2018-03-08 11:08:37 +01:00
|
|
|
{
|
2018-07-31 12:23:35 +02:00
|
|
|
struct port *p = data;
|
|
|
|
|
struct pw_port *port = p->port;
|
|
|
|
|
struct pw_node *node = port->node;
|
|
|
|
|
int res, i;
|
2018-02-15 17:54:08 +01:00
|
|
|
|
2018-09-28 17:47:37 +02:00
|
|
|
pw_log_debug(NAME " %p: port %p", p->node->node, port);
|
2018-02-15 17:54:08 +01:00
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
if (n_buffers > 0) {
|
|
|
|
|
for (i = 0; i < PORT_BUFFERS; i++)
|
|
|
|
|
init_buffer(p, i);
|
2018-04-19 20:15:30 +02:00
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
n_buffers = PORT_BUFFERS;
|
|
|
|
|
buffers = p->bufs;
|
2018-02-15 17:54:08 +01:00
|
|
|
}
|
|
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
res = spa_node_port_use_buffers(port->mix,
|
|
|
|
|
pw_direction_reverse(port->direction),
|
|
|
|
|
0,
|
|
|
|
|
buffers,
|
|
|
|
|
n_buffers);
|
|
|
|
|
res = spa_node_port_use_buffers(node->node,
|
|
|
|
|
port->direction,
|
|
|
|
|
port->port_id,
|
|
|
|
|
buffers,
|
|
|
|
|
n_buffers);
|
2018-04-26 17:23:27 +02:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
static const struct pw_port_implementation port_implementation = {
|
|
|
|
|
.use_buffers = port_use_buffers,
|
2018-02-15 17:54:08 +01:00
|
|
|
};
|
|
|
|
|
|
2018-09-11 18:09:45 +02:00
|
|
|
static const char *chmap_names[] =
|
|
|
|
|
{
|
2018-09-14 15:23:12 +02:00
|
|
|
"UNK", /**< unknown */
|
|
|
|
|
"NA", /**< unmapped */
|
2018-09-13 17:03:56 +02:00
|
|
|
"MONO", /**< mono */
|
|
|
|
|
"FL", /**< front left */
|
2018-09-11 18:09:45 +02:00
|
|
|
"FR", /**< front right */
|
|
|
|
|
"RL", /**< rear left */
|
|
|
|
|
"RR", /**< rear right */
|
|
|
|
|
"FC", /**< front center */
|
|
|
|
|
"LFE", /**< LFE */
|
|
|
|
|
"SL", /**< side left */
|
|
|
|
|
"SR", /**< side right */
|
|
|
|
|
"RC", /**< rear center */
|
|
|
|
|
"FLC", /**< front left center */
|
|
|
|
|
"FRC", /**< front right center */
|
|
|
|
|
"RLC", /**< rear left center */
|
|
|
|
|
"RRC", /**< rear right center */
|
|
|
|
|
"FLW", /**< front left wide */
|
|
|
|
|
"FRW", /**< front right wide */
|
|
|
|
|
"FLH", /**< front left high */
|
|
|
|
|
"FCH", /**< front center high */
|
|
|
|
|
"FRH", /**< front right high */
|
|
|
|
|
"TC", /**< top center */
|
|
|
|
|
"TFL", /**< top front left */
|
|
|
|
|
"TFR", /**< top front right */
|
|
|
|
|
"TFC", /**< top front center */
|
|
|
|
|
"TRL", /**< top rear left */
|
|
|
|
|
"TRR", /**< top rear right */
|
|
|
|
|
"TRC", /**< top rear center */
|
|
|
|
|
"TFLC", /**< top front left center */
|
|
|
|
|
"TFRC", /**< top front right center */
|
|
|
|
|
"TSL", /**< top side left */
|
|
|
|
|
"TSR", /**< top side right */
|
|
|
|
|
"LLFE", /**< left LFE */
|
|
|
|
|
"RLFE", /**< right LFE */
|
|
|
|
|
"BC", /**< bottom center */
|
|
|
|
|
"BLC", /**< bottom left center */
|
|
|
|
|
"BRC", /**< bottom right center */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int make_channel_name(struct node *n, char *channel_name, int i, uint64_t channelmask)
|
|
|
|
|
{
|
|
|
|
|
int j;
|
|
|
|
|
|
|
|
|
|
sprintf(channel_name, "%d", i + 1);
|
|
|
|
|
for (j = 0; j < 64; j++) {
|
|
|
|
|
if (channelmask & (1LL << j)) {
|
|
|
|
|
if (i-- == 0) {
|
|
|
|
|
sprintf(channel_name, "%s", chmap_names[j]);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-08 10:45:50 +02:00
|
|
|
struct pw_node *pw_audio_dsp_new(struct pw_core *core,
|
|
|
|
|
const struct pw_properties *props,
|
|
|
|
|
enum pw_direction direction,
|
|
|
|
|
uint32_t channels,
|
2018-09-11 18:09:45 +02:00
|
|
|
uint64_t channelmask,
|
2018-05-11 10:01:13 +02:00
|
|
|
uint32_t sample_rate,
|
2018-05-08 10:45:50 +02:00
|
|
|
uint32_t max_buffer_size,
|
|
|
|
|
size_t user_data_size)
|
2018-02-15 17:54:08 +01:00
|
|
|
{
|
|
|
|
|
struct pw_node *node;
|
2018-07-31 12:23:35 +02:00
|
|
|
struct pw_port *port;
|
2018-02-15 17:54:08 +01:00
|
|
|
struct node *n;
|
2018-07-04 15:30:41 +02:00
|
|
|
const char *api, *alias, *plugged, *str;
|
2018-02-16 12:02:39 +01:00
|
|
|
char node_name[128];
|
2018-04-24 17:03:56 +02:00
|
|
|
struct pw_properties *pr;
|
2018-07-31 12:23:35 +02:00
|
|
|
const struct spa_support *support;
|
|
|
|
|
uint32_t n_support;
|
|
|
|
|
void *iface;
|
2018-02-15 17:54:08 +01:00
|
|
|
int i;
|
|
|
|
|
|
2018-04-24 17:03:56 +02:00
|
|
|
if ((api = pw_properties_get(props, "device.api")) == NULL)
|
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
if ((alias = pw_properties_get(props, "device.name")) == NULL)
|
2018-02-20 10:31:55 +01:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
snprintf(node_name, sizeof(node_name), "system_%s", alias);
|
|
|
|
|
for (i = 0; node_name[i]; i++) {
|
2018-03-14 11:52:13 +01:00
|
|
|
if (node_name[i] == ':' || node_name[i] == ',')
|
2018-02-20 10:31:55 +01:00
|
|
|
node_name[i] = '_';
|
|
|
|
|
}
|
2018-02-16 12:02:39 +01:00
|
|
|
|
2018-04-24 17:03:56 +02:00
|
|
|
pr = pw_properties_new(
|
|
|
|
|
"media.class",
|
|
|
|
|
direction == PW_DIRECTION_OUTPUT ?
|
|
|
|
|
"Audio/DSP/Playback" :
|
|
|
|
|
"Audio/DSP/Capture",
|
|
|
|
|
"device.name", alias,
|
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
|
|
if ((plugged = pw_properties_get(props, "node.plugged")) != NULL)
|
|
|
|
|
pw_properties_set(pr, "node.plugged", plugged);
|
2018-07-04 15:30:41 +02:00
|
|
|
if ((str = pw_properties_get(props, "node.id")) != NULL)
|
|
|
|
|
pw_properties_set(pr, "node.session", str);
|
2018-04-24 17:03:56 +02:00
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
pw_properties_setf(pr, "node.format.rate", "%d", sample_rate);
|
2018-09-13 17:03:56 +02:00
|
|
|
pw_properties_setf(pr, "node.format.channels", "%d", channels);
|
|
|
|
|
pw_properties_setf(pr, "node.format.channelmask", "%"PRIu64, channelmask);
|
2018-07-31 12:23:35 +02:00
|
|
|
|
|
|
|
|
node = pw_spa_node_load(core, NULL, NULL,
|
|
|
|
|
"audioconvert/libspa-audioconvert",
|
2018-08-20 17:57:38 +02:00
|
|
|
direction == PW_DIRECTION_OUTPUT ?
|
|
|
|
|
"merger" :
|
|
|
|
|
"splitter",
|
2018-07-31 12:23:35 +02:00
|
|
|
node_name,
|
|
|
|
|
PW_SPA_NODE_FLAG_ACTIVATE | PW_SPA_NODE_FLAG_NO_REGISTER,
|
|
|
|
|
pr, sizeof(struct node) + user_data_size);
|
|
|
|
|
|
2018-02-15 17:54:08 +01:00
|
|
|
if (node == NULL)
|
|
|
|
|
goto error;
|
|
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
n = pw_spa_node_get_user_data(node);
|
2018-05-08 10:45:50 +02:00
|
|
|
n->core = core;
|
2018-02-15 17:54:08 +01:00
|
|
|
n->node = node;
|
2018-05-08 10:45:50 +02:00
|
|
|
|
|
|
|
|
n->channels = channels;
|
2018-09-11 18:09:45 +02:00
|
|
|
n->channelmask = channelmask;
|
2018-05-11 10:01:13 +02:00
|
|
|
n->sample_rate = sample_rate;
|
2018-05-08 10:45:50 +02:00
|
|
|
n->max_buffer_size = max_buffer_size;
|
|
|
|
|
|
|
|
|
|
if (user_data_size > 0)
|
|
|
|
|
n->user_data = SPA_MEMBER(n, sizeof(struct node), void);
|
|
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
pw_node_update_ports(node);
|
2018-02-15 17:54:08 +01:00
|
|
|
|
|
|
|
|
direction = pw_direction_reverse(direction);
|
|
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
support = pw_core_get_support(core, &n_support);
|
2018-02-15 17:54:08 +01:00
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
for (i = 0; i < n->channels; i++) {
|
|
|
|
|
struct port *p;
|
|
|
|
|
struct pw_properties *props;
|
2018-09-11 18:09:45 +02:00
|
|
|
char channel_name[16];
|
|
|
|
|
|
|
|
|
|
make_channel_name(n, channel_name, i, channelmask);
|
2018-07-31 12:23:35 +02:00
|
|
|
|
|
|
|
|
props = pw_properties_new(
|
|
|
|
|
"port.dsp", "32 bit float mono audio",
|
|
|
|
|
"port.physical", "1",
|
|
|
|
|
"port.terminal", "1",
|
|
|
|
|
NULL);
|
2018-09-11 18:09:45 +02:00
|
|
|
pw_properties_setf(props, "port.name", "%s_%s",
|
2018-02-15 17:54:08 +01:00
|
|
|
direction == PW_DIRECTION_INPUT ? "playback" : "capture",
|
2018-09-11 18:09:45 +02:00
|
|
|
channel_name);
|
|
|
|
|
pw_properties_setf(props, "port.alias1", "%s_pcm:%s:%s%s",
|
2018-04-24 17:03:56 +02:00
|
|
|
api,
|
2018-02-16 12:02:39 +01:00
|
|
|
alias,
|
|
|
|
|
direction == PW_DIRECTION_INPUT ? "in" : "out",
|
2018-09-11 18:09:45 +02:00
|
|
|
channel_name);
|
2018-09-13 17:03:56 +02:00
|
|
|
pw_properties_setf(props, "port.channel", "%s", channel_name);
|
2018-07-31 12:23:35 +02:00
|
|
|
|
|
|
|
|
port = pw_port_new(direction,
|
|
|
|
|
i,
|
|
|
|
|
props,
|
|
|
|
|
sizeof(struct port) +
|
|
|
|
|
spa_handle_factory_get_size(&spa_floatmix_factory, NULL));
|
|
|
|
|
if (port == NULL)
|
2018-02-15 17:54:08 +01:00
|
|
|
goto error_free_node;
|
2018-07-31 12:23:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
p = pw_port_get_user_data(port);
|
|
|
|
|
port->owner_data = p;
|
|
|
|
|
p->node = n;
|
|
|
|
|
p->port = port;
|
|
|
|
|
init_port(p, direction);
|
|
|
|
|
p->spa_handle = SPA_MEMBER(p, sizeof(struct port), struct spa_handle);
|
|
|
|
|
|
|
|
|
|
spa_handle_factory_init(&spa_floatmix_factory,
|
|
|
|
|
p->spa_handle, NULL,
|
|
|
|
|
support, n_support);
|
|
|
|
|
|
2018-08-27 15:03:11 +02:00
|
|
|
spa_handle_get_interface(p->spa_handle, SPA_TYPE_INTERFACE_Node, &iface);
|
2018-07-31 12:23:35 +02:00
|
|
|
|
|
|
|
|
p->spa_node = iface;
|
|
|
|
|
|
2018-08-20 17:57:38 +02:00
|
|
|
if (direction == PW_DIRECTION_INPUT) {
|
|
|
|
|
pw_log_debug("mix node %p", p->spa_node);
|
2018-07-31 12:23:35 +02:00
|
|
|
|
2018-08-20 17:57:38 +02:00
|
|
|
pw_port_set_mix(port, p->spa_node, PW_PORT_MIX_FLAG_MULTI);
|
|
|
|
|
port->implementation = &port_implementation;
|
|
|
|
|
port->implementation_data = p;
|
|
|
|
|
}
|
2018-07-31 12:23:35 +02:00
|
|
|
if (pw_port_add(port, node) < 0)
|
|
|
|
|
goto error_free_port;
|
2018-02-15 17:54:08 +01:00
|
|
|
}
|
2018-05-08 10:45:50 +02:00
|
|
|
return node;
|
2018-02-15 17:54:08 +01:00
|
|
|
|
2018-07-31 12:23:35 +02:00
|
|
|
error_free_port:
|
|
|
|
|
pw_port_destroy(port);
|
2018-02-15 17:54:08 +01:00
|
|
|
error_free_node:
|
|
|
|
|
pw_node_destroy(node);
|
|
|
|
|
error:
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-08 10:45:50 +02:00
|
|
|
void *pw_audio_dsp_get_user_data(struct pw_node *node)
|
2018-02-15 17:54:08 +01:00
|
|
|
{
|
2018-09-10 18:49:04 +02:00
|
|
|
struct node *n = pw_spa_node_get_user_data(node);
|
2018-05-08 10:45:50 +02:00
|
|
|
return n->user_data;
|
2018-02-15 17:54:08 +01:00
|
|
|
}
|