Add PortConfig parameter

Add a new PortConfig parameter to configure ports of elements that
are marked with the SPA_NODE_FLAG_*_PORT_CONFIG. This is used to
configure the operation of the audioconver/audioadapter nodes and
how it should convert the internal format. We want to use the
Profile parameter only for cases where there is an enumeration of
values, like with device configuration.

Add unit tests for audioconvert and adapter to check if they handle
PortConfig correctly.

Make the media session use the PortConfig to dynamically configure
the device nodes.

Remove audio-dsp, it is not used anymore and can/should be implemented
with a simple audioconvert spa node now and some PortConfig.
This commit is contained in:
Wim Taymans 2019-08-06 12:45:20 +02:00
parent acdfed0d04
commit f41720e7db
29 changed files with 1693 additions and 1300 deletions

View file

@ -55,4 +55,4 @@ publish: all
git commit -anm "Deploy on gh-pages" && \
git subtree split --prefix build/doc/html -b gh-pages && \
git push --force origin gh-pages:gh-pages && \
git checkout master 2>/dev/null
git checkout work 2>/dev/null

View file

@ -61,9 +61,13 @@ struct spa_node_info {
#define SPA_NODE_CHANGE_MASK_PARAMS (1u<<2)
uint64_t change_mask;
#define SPA_NODE_FLAG_DYNAMIC_INPUT_PORTS (1u<<0) /**< input ports can be added/removed */
#define SPA_NODE_FLAG_DYNAMIC_OUTPUT_PORTS (1u<<1) /**< output ports can be added/removed */
#define SPA_NODE_FLAG_RT (1u<<2) /**< node can do real-time processing */
#define SPA_NODE_FLAG_RT (1u<<0) /**< node can do real-time processing */
#define SPA_NODE_FLAG_IN_DYNAMIC_PORTS (1u<<1) /**< input ports can be added/removed */
#define SPA_NODE_FLAG_OUT_DYNAMIC_PORTS (1u<<2) /**< output ports can be added/removed */
#define SPA_NODE_FLAG_IN_PORT_CONFIG (1u<<3) /**< input ports can be reconfigured with
* PortConfig parameter */
#define SPA_NODE_FLAG_OUT_PORT_CONFIG (1u<<4) /**< output ports can be reconfigured with
* PortConfig parameter */
uint64_t flags;
struct spa_dict *props; /**< extra node properties */
struct spa_param_info *params; /**< parameter information */

View file

@ -43,17 +43,19 @@ enum spa_param_type {
SPA_PARAM_IO, /**< configurable IO areas as SPA_TYPE_OBJECT_ParamIO */
SPA_PARAM_EnumProfile, /**< profile enumeration as SPA_TYPE_OBJECT_ParamProfile */
SPA_PARAM_Profile, /**< profile configuration as SPA_TYPE_OBJECT_ParamProfile */
SPA_PARAM_EnumPortConfig, /**< port configuration enumeration as SPA_TYPE_OBJECT_ParamPortConfig */
SPA_PARAM_PortConfig, /**< port configuration as SPA_TYPE_OBJECT_ParamPortConfig */
};
/** Properties for SPA_TYPE_OBJECT_ParamList */
enum spa_param_list {
SPA_PARAM_LIST_START, /**< object id, one of enum spa_param_type */
SPA_PARAM_LIST_START,
SPA_PARAM_LIST_id, /**< id of the supported list param (Id enum spa_param_type) */
};
/** properties for SPA_TYPE_OBJECT_ParamBuffers */
enum spa_param_buffers {
SPA_PARAM_BUFFERS_START, /**< object id, one of enum spa_param_type */
SPA_PARAM_BUFFERS_START,
SPA_PARAM_BUFFERS_buffers, /**< number of buffers (Int) */
SPA_PARAM_BUFFERS_blocks, /**< number of data blocks per buffer (Int) */
SPA_PARAM_BUFFERS_size, /**< size of a data block memory (Int)*/
@ -63,25 +65,41 @@ enum spa_param_buffers {
/** properties for SPA_TYPE_OBJECT_ParamMeta */
enum spa_param_meta {
SPA_PARAM_META_START, /**< object id, one of enum spa_param_type */
SPA_PARAM_META_START,
SPA_PARAM_META_type, /**< the metadata, one of enum spa_meta_type (Id enum spa_meta_type) */
SPA_PARAM_META_size, /**< the expected maximum size the meta (Int) */
};
/** properties for SPA_TYPE_OBJECT_ParamIO */
enum spa_param_io {
SPA_PARAM_IO_START, /**< object id, one of enum spa_param_type */
SPA_PARAM_IO_START,
SPA_PARAM_IO_id, /**< type ID, uniquely identifies the io area (Id enum spa_io_type) */
SPA_PARAM_IO_size, /**< size of the io area (Int) */
};
/** properties for SPA_TYPE_OBJECT_ParamProfile */
enum spa_param_profile {
SPA_PARAM_PROFILE_START, /**< object id, one of enum spa_param_type */
SPA_PARAM_PROFILE_START,
SPA_PARAM_PROFILE_index, /**< profile index (Int) */
SPA_PARAM_PROFILE_name, /**< profile name (String) */
SPA_PARAM_PROFILE_direction, /**< direction, input/output (Id enum spa_direction) */
SPA_PARAM_PROFILE_format, /**< profile format specification (Object) */
};
enum spa_param_port_config_mode {
SPA_PARAM_PORT_CONFIG_MODE_none, /**< no configuration */
SPA_PARAM_PORT_CONFIG_MODE_passthrough, /**< passthrough configuration */
SPA_PARAM_PORT_CONFIG_MODE_convert, /**< convert configuration */
SPA_PARAM_PORT_CONFIG_MODE_dsp, /**< dsp configuration, depending on the external
* format. For audio, ports will be configured for
* the given number of channels with F32 format. */
};
/** properties for SPA_TYPE_OBJECT_ParamPortConfig */
enum spa_param_port_config {
SPA_PARAM_PORT_CONFIG_START,
SPA_PARAM_PORT_CONFIG_direction, /**< direction, input/output (Id enum spa_direction) */
SPA_PARAM_PORT_CONFIG_mode, /**< (Id enum spa_param_port_config_mode) mode */
SPA_PARAM_PORT_CONFIG_monitor, /**< (Bool) enable monitor output ports on input ports */
SPA_PARAM_PORT_CONFIG_format, /**< (Object) format filter */
};
#ifdef __cplusplus

View file

@ -263,8 +263,29 @@ static const struct spa_type_info spa_type_param_profile[] = {
{ SPA_PARAM_PROFILE_START, SPA_TYPE_Id, SPA_TYPE_INFO_PARAM_PROFILE_BASE, spa_type_param, },
{ SPA_PARAM_PROFILE_index, SPA_TYPE_Int, SPA_TYPE_INFO_PARAM_PROFILE_BASE "index", NULL },
{ SPA_PARAM_PROFILE_name, SPA_TYPE_String, SPA_TYPE_INFO_PARAM_PROFILE_BASE "name", NULL },
{ SPA_PARAM_PROFILE_direction, SPA_TYPE_Id, SPA_TYPE_INFO_PARAM_PROFILE_BASE "direction", spa_type_direction },
{ SPA_PARAM_PROFILE_format, SPA_TYPE_Object, SPA_TYPE_INFO_PARAM_PROFILE_BASE "format", NULL, },
{ 0, 0, NULL, NULL },
};
#define SPA_TYPE_INFO_ParamPortConfigMode SPA_TYPE_INFO_ENUM_BASE "ParamPortConfigMode"
#define SPA_TYPE_INFO_PARAM_PORT_CONFIG_MODE_BASE SPA_TYPE_INFO_ParamPortConfigMode ":"
static const struct spa_type_info spa_type_param_port_config_mode[] = {
{ SPA_PARAM_PORT_CONFIG_MODE_none, SPA_TYPE_Int, SPA_TYPE_INFO_PARAM_PORT_CONFIG_MODE_BASE "none", NULL },
{ SPA_PARAM_PORT_CONFIG_MODE_passthrough, SPA_TYPE_Int, SPA_TYPE_INFO_PARAM_PORT_CONFIG_MODE_BASE "passthrough", NULL },
{ SPA_PARAM_PORT_CONFIG_MODE_convert, SPA_TYPE_Int, SPA_TYPE_INFO_PARAM_PORT_CONFIG_MODE_BASE "convert", NULL },
{ SPA_PARAM_PORT_CONFIG_MODE_dsp, SPA_TYPE_Int, SPA_TYPE_INFO_PARAM_PORT_CONFIG_MODE_BASE "dsp", NULL },
{ 0, 0, NULL, NULL },
};
#define SPA_TYPE_INFO_PARAM_PortConfig SPA_TYPE_INFO_PARAM_BASE "PortConfig"
#define SPA_TYPE_INFO_PARAM_PORT_CONFIG_BASE SPA_TYPE_INFO_PARAM_PortConfig ":"
static const struct spa_type_info spa_type_param_port_config[] = {
{ SPA_PARAM_PORT_CONFIG_START, SPA_TYPE_Id, SPA_TYPE_INFO_PARAM_PORT_CONFIG_BASE, spa_type_param, },
{ SPA_PARAM_PORT_CONFIG_direction, SPA_TYPE_Id, SPA_TYPE_INFO_PARAM_PORT_CONFIG_BASE "direction", spa_type_direction, },
{ SPA_PARAM_PORT_CONFIG_mode, SPA_TYPE_Id, SPA_TYPE_INFO_PARAM_PORT_CONFIG_BASE "mode", spa_type_param_port_config_mode },
{ SPA_PARAM_PORT_CONFIG_monitor, SPA_TYPE_Bool, SPA_TYPE_INFO_PARAM_PORT_CONFIG_BASE "monitor", NULL },
{ SPA_PARAM_PORT_CONFIG_format, SPA_TYPE_Object, SPA_TYPE_INFO_PARAM_PORT_CONFIG_BASE "format", NULL },
{ 0, 0, NULL, NULL },
};

View file

@ -130,6 +130,7 @@ static const struct spa_type_info spa_types[] = {
{ SPA_TYPE_OBJECT_ParamMeta, SPA_TYPE_Object, SPA_TYPE_INFO_PARAM_Meta, spa_type_param_meta },
{ SPA_TYPE_OBJECT_ParamIO, SPA_TYPE_Object, SPA_TYPE_INFO_PARAM_IO, spa_type_param_io },
{ SPA_TYPE_OBJECT_ParamProfile, SPA_TYPE_Object, SPA_TYPE_INFO_PARAM_Profile, spa_type_param_profile },
{ SPA_TYPE_OBJECT_ParamPortConfig, SPA_TYPE_Object, SPA_TYPE_INFO_PARAM_PortConfig, spa_type_param_port_config },
{ 0, 0, NULL, NULL }
};

View file

@ -101,6 +101,7 @@ enum {
SPA_TYPE_OBJECT_ParamMeta,
SPA_TYPE_OBJECT_ParamIO,
SPA_TYPE_OBJECT_ParamProfile,
SPA_TYPE_OBJECT_ParamPortConfig,
SPA_TYPE_OBJECT_LAST, /**< not part of ABI */
/* vendor extensions */

View file

@ -31,6 +31,7 @@
#include <spa/buffer/alloc.h>
#include <spa/pod/parser.h>
#include <spa/pod/filter.h>
#include <spa/param/param.h>
#include <spa/debug/format.h>
#include <spa/debug/pod.h>
@ -72,10 +73,8 @@ struct impl {
unsigned int use_converter:1;
unsigned int started:1;
unsigned int active:1;
unsigned int driver:1;
unsigned int master:1;
unsigned int monitor:1;
};
/** \endcond */
@ -103,6 +102,8 @@ next:
spa_pod_builder_init(&b, buffer, sizeof(buffer));
switch (id) {
case SPA_PARAM_EnumPortConfig:
case SPA_PARAM_PortConfig:
case SPA_PARAM_PropInfo:
case SPA_PARAM_Props:
if ((res = spa_node_enum_params_sync(this->convert,
@ -140,7 +141,7 @@ static int link_io(struct impl *this)
if (!this->use_converter)
return 0;
spa_log_warn(this->log, NAME " %p: controls", this);
spa_log_debug(this->log, NAME " %p: controls", this);
spa_zero(this->io_rate_match);
this->io_rate_match.rate = 1.0;
@ -149,7 +150,7 @@ static int link_io(struct impl *this)
this->direction, 0,
SPA_IO_RateMatch,
&this->io_rate_match, sizeof(this->io_rate_match))) < 0) {
spa_log_warn(this->log, NAME " %p: set RateMatch on slave failed %d %s", this,
spa_log_debug(this->log, NAME " %p: set RateMatch on slave disabled %d %s", this,
res, spa_strerror(res));
}
else if ((res = spa_node_port_set_io(this->convert,
@ -207,7 +208,14 @@ static int impl_node_set_param(void *object, uint32_t id, uint32_t flags,
spa_log_debug(this->log, NAME" %p: set param %d", this, id);
switch (id) {
case SPA_PARAM_Profile:
case SPA_PARAM_Format:
if (this->started)
return -EIO;
if ((res = spa_node_port_set_param(this->slave, this->direction, 0, id, flags, param)) < 0)
return res;
break;
case SPA_PARAM_PortConfig:
if (this->started)
return -EIO;
if (this->target != this->slave) {
@ -215,6 +223,7 @@ static int impl_node_set_param(void *object, uint32_t id, uint32_t flags,
return res;
}
break;
case SPA_PARAM_Props:
if (this->target != this->slave) {
if ((res = spa_node_set_param(this->target, id, flags, param)) < 0)
@ -766,9 +775,6 @@ static int impl_node_process(void *object)
status = spa_node_process(this->slave);
if (this->monitor)
status |= SPA_STATUS_HAVE_BUFFER;
if (this->direction == SPA_DIRECTION_OUTPUT && !this->master) {
if (this->use_converter)
status = spa_node_process(this->convert);
@ -830,6 +836,22 @@ static int impl_clear(struct spa_handle *handle)
return 0;
}
static int configure_adapt(struct impl *this)
{
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_pod *param;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamPortConfig, SPA_PARAM_PortConfig,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(this->direction),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_dsp));
return spa_node_set_param(this->target, SPA_PARAM_PortConfig, 0, param);
}
extern const struct spa_handle_factory spa_audioconvert_factory;
static size_t
@ -878,9 +900,6 @@ impl_init(const struct spa_handle_factory *factory,
if (this->slave == NULL)
return -EINVAL;
if ((str = spa_dict_lookup(info, "merger.monitor")) != NULL)
this->monitor = atoi(str);
spa_node_add_listener(this->slave,
&this->slave_listener, &slave_node_events, this);
spa_node_set_callbacks(this->slave, &slave_node_callbacks, this);
@ -903,17 +922,19 @@ impl_init(const struct spa_handle_factory *factory,
&this->convert_listener, &convert_node_events, this);
this->use_converter = true;
configure_adapt(this);
link_io(this);
this->info_all = SPA_NODE_CHANGE_MASK_PARAMS;
this->info = SPA_NODE_INFO_INIT();
this->info.max_input_ports = 0;
this->info.max_output_ports = 0;
this->info.max_input_ports = this->direction == SPA_DIRECTION_INPUT ? 128 : 0;
this->info.max_output_ports = this->direction == SPA_DIRECTION_OUTPUT ? 128 : 0;
this->params[0] = SPA_PARAM_INFO(SPA_PARAM_EnumFormat, SPA_PARAM_INFO_READ);
this->params[1] = SPA_PARAM_INFO(SPA_PARAM_PropInfo, SPA_PARAM_INFO_READ);
this->params[2] = SPA_PARAM_INFO(SPA_PARAM_Props, SPA_PARAM_INFO_READWRITE);
this->params[3] = SPA_PARAM_INFO(SPA_PARAM_Format, SPA_PARAM_INFO_WRITE);
this->params[4] = SPA_PARAM_INFO(SPA_PARAM_Profile, SPA_PARAM_INFO_WRITE);
this->params[4] = SPA_PARAM_INFO(SPA_PARAM_PortConfig, SPA_PARAM_INFO_WRITE);
this->info.params = this->params;
this->info.n_params = 5;

View file

@ -71,30 +71,56 @@ struct impl {
struct spa_hook_list hooks;
uint64_t info_all;
struct spa_node_info info;
struct spa_param_info params[8];
int n_links;
struct link links[8];
int n_nodes;
struct spa_node *nodes[8];
#define MODE_SPLIT 0
#define MODE_MERGE 1
#define MODE_CONVERT 2
int mode;
enum spa_param_port_config_mode mode[2];
bool fmt_removing[2];
bool fmt_is_set[2];
bool buffers_set[2];
bool started;
struct spa_handle *hnd_fmt[2];
struct spa_handle *hnd_merger;
struct spa_handle *hnd_convert_in;
struct spa_handle *hnd_channelmix;
struct spa_handle *hnd_resample;
struct spa_handle *hnd_convert_out;
struct spa_handle *hnd_splitter;
struct spa_node *fmt[2];
struct spa_node *merger;
struct spa_node *convert_in;
struct spa_node *channelmix;
struct spa_node *resample;
struct spa_node *convert_out;
struct spa_node *splitter;
struct spa_hook listener[4];
struct spa_node *fmt[2];
struct spa_hook fmt_listener[2];
bool have_fmt_listener[2];
struct spa_hook listener[2];
unsigned int started:1;
};
#define IS_MONITOR_PORT(this,dir,port_id) (dir == SPA_DIRECTION_OUTPUT && port_id > 0 && \
this->mode[SPA_DIRECTION_INPUT] == SPA_PARAM_PORT_CONFIG_MODE_dsp)
static void emit_node_info(struct impl *this, bool full)
{
if (full)
this->info.change_mask = this->info_all;
if (this->info.change_mask) {
spa_node_emit_info(&this->hooks, &this->info);
this->info.change_mask = 0;
}
}
static int make_link(struct impl *this,
struct spa_node *out_node, uint32_t out_port,
struct spa_node *in_node, uint32_t in_port, uint32_t min_buffers)
@ -226,11 +252,11 @@ static int setup_convert(struct impl *this)
{
int i, j, res;
spa_log_debug(this->log, "setup convert n_links:%d", this->n_links);
if (this->n_links > 0)
return 0;
spa_log_debug(this->log, "setup convert");
this->n_nodes = 0;
/* unpack */
this->nodes[this->n_nodes++] = this->fmt[SPA_DIRECTION_INPUT];
@ -353,6 +379,9 @@ static int negotiate_link_buffers(struct impl *this, struct link *link)
static void clean_convert(struct impl *this)
{
int i;
spa_log_debug(this->log, NAME " %p: %d", this, this->n_links);
for (i = 0; i < this->n_links; i++)
clean_link(this, &this->links[i]);
this->n_links = 0;
@ -405,17 +434,31 @@ static int impl_node_enum_params(void *object, int seq,
spa_pod_builder_init(&b, buffer, sizeof(buffer));
switch (id) {
case SPA_PARAM_EnumProfile:
case SPA_PARAM_EnumPortConfig:
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamProfile, id,
SPA_PARAM_PROFILE_direction, SPA_POD_Id(SPA_DIRECTION_INPUT));
SPA_TYPE_OBJECT_ParamPortConfig, id,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(SPA_DIRECTION_INPUT),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_dsp));
break;
case 1:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamProfile, id,
SPA_PARAM_PROFILE_direction, SPA_POD_Id(SPA_DIRECTION_OUTPUT));
SPA_TYPE_OBJECT_ParamPortConfig, id,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(SPA_DIRECTION_OUTPUT),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_dsp));
break;
case 2:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamPortConfig, id,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(SPA_DIRECTION_INPUT),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_convert));
break;
case 3:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamPortConfig, id,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(SPA_DIRECTION_OUTPUT),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_convert));
break;
default:
return 0;
@ -463,6 +506,155 @@ static int impl_node_set_io(void *object, uint32_t id, void *data, size_t size)
return res;
}
static void on_node_result(void *data, int seq, int res, uint32_t type, const void *result)
{
struct impl *this = data;
spa_log_trace(this->log, "%p: result %d %d", this, seq, res);
spa_node_emit_result(&this->hooks, seq, res, type, result);
}
static void fmt_input_port_info(void *data,
enum spa_direction direction, uint32_t port,
const struct spa_port_info *info)
{
struct impl *this = data;
if (this->fmt_removing[direction])
info = NULL;
if (direction == SPA_DIRECTION_INPUT ||
IS_MONITOR_PORT(this, direction, port))
spa_node_emit_port_info(&this->hooks, direction, port, info);
}
static struct spa_node_events fmt_input_events = {
SPA_VERSION_NODE_EVENTS,
.port_info = fmt_input_port_info,
.result = on_node_result,
};
static void fmt_output_port_info(void *data,
enum spa_direction direction, uint32_t port,
const struct spa_port_info *info)
{
struct impl *this = data;
if (this->fmt_removing[direction])
info = NULL;
if (direction == SPA_DIRECTION_OUTPUT)
spa_node_emit_port_info(&this->hooks, direction, port, info);
}
static struct spa_node_events fmt_output_events = {
SPA_VERSION_NODE_EVENTS,
.port_info = fmt_output_port_info,
.result = on_node_result,
};
static struct spa_node_events node_events = {
SPA_VERSION_NODE_EVENTS,
.result = on_node_result,
};
static int reconfigure_mode(struct impl *this, enum spa_param_port_config_mode mode,
enum spa_direction direction, bool monitor, struct spa_audio_info *info)
{
int res = 0;
struct spa_node *old, *new;
bool do_signal;
spa_log_debug(this->log, NAME " %p: mode %d", this, mode);
/* old node on input/output */
old = this->fmt[direction];
/* decide on new node based on mode and direction */
switch (mode) {
case SPA_PARAM_PORT_CONFIG_MODE_convert:
new = direction == SPA_DIRECTION_INPUT ? this->convert_in : this->convert_out;
break;
case SPA_PARAM_PORT_CONFIG_MODE_dsp:
new = direction == SPA_DIRECTION_INPUT ? this->merger : this->splitter;
break;
default:
return -EIO;
}
this->mode[direction] = mode;
clean_convert(this);
this->fmt[direction] = new;
this->fmt_is_set[direction] = false;
/* signal if we change nodes or when DSP config changes */
do_signal = this->fmt[direction] != old ||
mode == SPA_PARAM_PORT_CONFIG_MODE_dsp;
if (do_signal) {
/* change, remove old ports. We trigger a new port_info event
* on the old node with info set to NULL to mark delete */
if (this->have_fmt_listener[direction]) {
spa_hook_remove(&this->fmt_listener[direction]);
this->fmt_removing[direction] = true;
spa_node_add_listener(old,
&this->fmt_listener[direction],
direction == SPA_DIRECTION_INPUT ?
&fmt_input_events : &fmt_output_events,
this);
this->fmt_removing[direction] = false;
spa_hook_remove(&this->fmt_listener[direction]);
this->have_fmt_listener[direction] = false;
}
}
if (info) {
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_pod *param;
spa_log_debug(this->log, NAME " %p: port config %d", this, info->info.raw.channels);
spa_pod_builder_init(&b, buffer, sizeof(buffer));
param = spa_format_audio_raw_build(&b, SPA_PARAM_Format, &info->info.raw);
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamPortConfig, SPA_PARAM_PortConfig,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(direction),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_dsp),
SPA_PARAM_PORT_CONFIG_monitor, SPA_POD_Bool(monitor),
SPA_PARAM_PORT_CONFIG_format, SPA_POD_Pod(param));
res = spa_node_set_param(this->fmt[direction], SPA_PARAM_PortConfig, 0, param);
if (res < 0)
return res;
this->fmt_is_set[direction] = true;
}
if (this->fmt_is_set[direction] &&
this->fmt_is_set[SPA_DIRECTION_REVERSE(direction)]) {
if ((res = setup_convert(this)) < 0)
return res;
}
/* notify ports of new node */
if (do_signal) {
if (this->have_fmt_listener[direction])
spa_hook_remove(&this->fmt_listener[direction]);
spa_node_add_listener(this->fmt[direction],
&this->fmt_listener[direction],
direction == SPA_DIRECTION_INPUT ?
&fmt_input_events : &fmt_output_events,
this);
this->have_fmt_listener[direction] = true;
}
return 0;
}
static int impl_node_set_param(void *object, uint32_t id, uint32_t flags,
const struct spa_pod *param)
{
@ -472,53 +664,58 @@ static int impl_node_set_param(void *object, uint32_t id, uint32_t flags,
spa_return_val_if_fail(this != NULL, -EINVAL);
switch (id) {
case SPA_PARAM_Profile:
case SPA_PARAM_PortConfig:
{
enum spa_direction direction;
struct spa_pod *format;
struct spa_audio_info info = { 0, };
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
enum spa_direction dir;
enum spa_param_port_config_mode mode;
struct spa_pod *format = NULL;
struct spa_audio_info info = { 0, }, *infop = NULL;
int monitor = false;
if (spa_pod_parse_object(param,
SPA_TYPE_OBJECT_ParamProfile, NULL,
SPA_PARAM_PROFILE_direction, SPA_POD_Id(&direction),
SPA_PARAM_PROFILE_format, SPA_POD_Pod(&format)) < 0)
SPA_TYPE_OBJECT_ParamPortConfig, NULL,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(&dir),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(&mode),
SPA_PARAM_PORT_CONFIG_monitor, SPA_POD_OPT_Bool(&monitor),
SPA_PARAM_PORT_CONFIG_format, SPA_POD_OPT_Pod(&format)) < 0)
return -EINVAL;
if (!SPA_POD_IS_OBJECT_TYPE(format, SPA_TYPE_OBJECT_Format))
return -EINVAL;
if (format) {
if (!SPA_POD_IS_OBJECT_TYPE(format, SPA_TYPE_OBJECT_Format))
return -EINVAL;
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
if (info.media_type != SPA_MEDIA_TYPE_audio ||
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)
return -EINVAL;
if (info.media_type != SPA_MEDIA_TYPE_audio ||
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)
return -ENOTSUP;
if (spa_format_audio_raw_parse(format, &info.info.raw) < 0)
return -EINVAL;
if (spa_format_audio_raw_parse(format, &info.info.raw) < 0)
return -EINVAL;
switch (direction) {
case SPA_DIRECTION_INPUT:
case SPA_DIRECTION_OUTPUT:
spa_log_debug(this->log, NAME " %p: profile %d", this, info.info.raw.channels);
infop = &info;
}
spa_log_debug(this->log, "mode:%d direction:%d %d", mode, dir, monitor);
switch (mode) {
case SPA_PARAM_PORT_CONFIG_MODE_none:
case SPA_PARAM_PORT_CONFIG_MODE_passthrough:
return -ENOTSUP;
case SPA_PARAM_PORT_CONFIG_MODE_convert:
break;
case SPA_PARAM_PORT_CONFIG_MODE_dsp:
info.info.raw.format = SPA_AUDIO_FORMAT_F32P;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
param = spa_format_audio_raw_build(&b, SPA_PARAM_Format, &info.info.raw);
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamProfile, id,
SPA_PARAM_PROFILE_direction, SPA_POD_Id(direction),
SPA_PARAM_PROFILE_format, SPA_POD_Pod(param));
res = spa_node_set_param(this->fmt[direction], id, flags, param);
break;
default:
res = -EINVAL;
break;
return -EINVAL;
}
res = reconfigure_mode(this, mode, dir, monitor, infop);
break;
}
case SPA_PARAM_Props:
@ -555,49 +752,6 @@ static int impl_node_send_command(void *object, const struct spa_command *comman
return 0;
}
static void on_node_result(void *data, int seq, int res, uint32_t type, const void *result)
{
struct impl *this = data;
spa_log_trace(this->log, "%p: result %d %d", this, seq, res);
spa_node_emit_result(&this->hooks, seq, res, type, result);
}
static void fmt_input_port_info(void *data,
enum spa_direction direction, uint32_t port,
const struct spa_port_info *info)
{
struct impl *this = data;
if (direction == SPA_DIRECTION_INPUT ||
(this->mode == MODE_MERGE && port > 0))
spa_node_emit_port_info(&this->hooks, direction, port, info);
}
static struct spa_node_events fmt_input_events = {
SPA_VERSION_NODE_EVENTS,
.port_info = fmt_input_port_info,
.result = on_node_result,
};
static void fmt_output_port_info(void *data,
enum spa_direction direction, uint32_t port,
const struct spa_port_info *info)
{
struct impl *this = data;
if (direction == SPA_DIRECTION_OUTPUT)
spa_node_emit_port_info(&this->hooks, direction, port, info);
}
static struct spa_node_events fmt_output_events = {
SPA_VERSION_NODE_EVENTS,
.port_info = fmt_output_port_info,
.result = on_node_result,
};
static struct spa_node_events node_events = {
SPA_VERSION_NODE_EVENTS,
.result = on_node_result,
};
static int
impl_node_add_listener(void *object,
struct spa_hook *listener,
@ -614,6 +768,8 @@ impl_node_add_listener(void *object,
spa_log_trace(this->log, "%p: add listener %p", this, listener);
emit_node_info(this, true);
spa_zero(l);
spa_node_add_listener(this->fmt[SPA_DIRECTION_INPUT],
&l[0], &fmt_input_events, this);
@ -645,21 +801,13 @@ impl_node_set_callbacks(void *object,
static int impl_node_add_port(void *object, enum spa_direction direction, uint32_t port_id,
const struct spa_dict *props)
{
struct impl *this = object;
spa_return_val_if_fail(this != NULL, -EINVAL);
return spa_node_add_port(this->fmt[direction], direction, port_id, props);
return -ENOTSUP;
}
static int
impl_node_remove_port(void *object, enum spa_direction direction, uint32_t port_id)
{
struct impl *this = object;
spa_return_val_if_fail(this != NULL, -EINVAL);
return spa_node_remove_port(this->fmt[direction], direction, port_id);
return -ENOTSUP;
}
static int
@ -727,7 +875,7 @@ impl_node_port_enum_params(void *object, int seq,
{
struct spa_node *target;
if (this->mode == MODE_MERGE && port_id > 0 && direction == SPA_DIRECTION_OUTPUT)
if (IS_MONITOR_PORT(this, direction, port_id))
target = this->fmt[SPA_DIRECTION_INPUT];
else
target = this->fmt[direction];
@ -757,26 +905,34 @@ impl_node_port_set_param(void *object,
struct impl *this = object;
int res;
struct spa_node *target;
bool is_monitor;
spa_return_val_if_fail(this != NULL, -EINVAL);
if (this->mode == MODE_MERGE && port_id > 0 && direction == SPA_DIRECTION_OUTPUT)
is_monitor = IS_MONITOR_PORT(this, direction, port_id);
if (is_monitor)
target = this->fmt[SPA_DIRECTION_INPUT];
else
target = this->fmt[direction];
if (id == SPA_PARAM_Format && !is_monitor) {
if (param == NULL ||
this->mode[direction] == SPA_PARAM_PORT_CONFIG_MODE_convert)
clean_convert(this);
}
if ((res = spa_node_port_set_param(target,
direction, port_id, id, flags, param)) < 0)
return res;
if (id == SPA_PARAM_Format) {
if (param == NULL)
clean_convert(this);
else if (this->fmt_is_set[SPA_DIRECTION_REVERSE(direction)]) {
if (id == SPA_PARAM_Format && !is_monitor) {
this->fmt_is_set[direction] = (param != NULL);
if (this->fmt_is_set[direction] &&
this->fmt_is_set[SPA_DIRECTION_REVERSE(direction)]) {
if ((res = setup_convert(this)) < 0)
return res;
}
this->fmt_is_set[direction] = (param != NULL);
}
return res;
}
@ -795,7 +951,7 @@ impl_node_port_use_buffers(void *object,
spa_return_val_if_fail(this != NULL, -EINVAL);
if (this->mode == MODE_MERGE && port_id > 0 && direction == SPA_DIRECTION_OUTPUT)
if (IS_MONITOR_PORT(this, direction, port_id))
target = this->fmt[SPA_DIRECTION_INPUT];
else
target = this->fmt[direction];
@ -833,7 +989,7 @@ impl_node_port_set_io(void *object,
res = spa_node_port_set_io(this->channelmix, direction, 0, id, data, size);
break;
default:
if (this->mode == MODE_MERGE && port_id > 0 && direction == SPA_DIRECTION_OUTPUT)
if (IS_MONITOR_PORT(this, direction, port_id))
target = this->fmt[SPA_DIRECTION_INPUT];
else
target = this->fmt[direction];
@ -851,7 +1007,7 @@ static int impl_node_port_reuse_buffer(void *object, uint32_t port_id, uint32_t
spa_return_val_if_fail(this != NULL, -EINVAL);
if (this->mode == MODE_MERGE && port_id > 0)
if (IS_MONITOR_PORT(this, SPA_DIRECTION_OUTPUT, port_id))
target = this->fmt[SPA_DIRECTION_INPUT];
else
target = this->fmt[SPA_DIRECTION_OUTPUT];
@ -943,10 +1099,12 @@ static int impl_clear(struct spa_handle *handle)
clean_convert(this);
spa_handle_clear(this->hnd_fmt[SPA_DIRECTION_INPUT]);
spa_handle_clear(this->hnd_merger);
spa_handle_clear(this->hnd_convert_in);
spa_handle_clear(this->hnd_channelmix);
spa_handle_clear(this->hnd_resample);
spa_handle_clear(this->hnd_fmt[SPA_DIRECTION_OUTPUT]);
spa_handle_clear(this->hnd_convert_out);
spa_handle_clear(this->hnd_splitter);
return 0;
}
@ -961,16 +1119,15 @@ static size_t
impl_get_size(const struct spa_handle_factory *factory,
const struct spa_dict *params)
{
size_t size, max;
max = spa_handle_factory_get_size(&spa_fmtconvert_factory, params);
max = SPA_MAX(max, spa_handle_factory_get_size(&spa_splitter_factory, params));
max = SPA_MAX(max, spa_handle_factory_get_size(&spa_merger_factory, params));
size_t size;
size = sizeof(struct impl);
size += max * 2;
size += spa_handle_factory_get_size(&spa_merger_factory, params);
size += spa_handle_factory_get_size(&spa_fmtconvert_factory, params);
size += spa_handle_factory_get_size(&spa_channelmix_factory, params);
size += spa_handle_factory_get_size(&spa_resample_factory, params);
size += spa_handle_factory_get_size(&spa_fmtconvert_factory, params);
size += spa_handle_factory_get_size(&spa_splitter_factory, params);
return size;
}
@ -986,8 +1143,6 @@ impl_init(const struct spa_handle_factory *factory,
uint32_t i;
size_t size;
void *iface;
const char *str;
const struct spa_handle_factory *in_factory, *out_factory;
spa_return_val_if_fail(factory != NULL, -EINVAL);
spa_return_val_if_fail(handle != NULL, -EINVAL);
@ -1007,32 +1162,34 @@ impl_init(const struct spa_handle_factory *factory,
&impl_node, this);
spa_hook_list_init(&this->hooks);
if (info == NULL || (str = spa_dict_lookup(info, "factory.mode")) == NULL)
str = "convert";
this->info_all = SPA_NODE_CHANGE_MASK_FLAGS |
SPA_NODE_CHANGE_MASK_PARAMS;
this->info = SPA_NODE_INFO_INIT();
this->info.max_input_ports = 128;
this->info.max_output_ports = 128;
this->info.flags = SPA_NODE_FLAG_RT |
SPA_NODE_FLAG_IN_PORT_CONFIG |
SPA_NODE_FLAG_OUT_PORT_CONFIG;
this->params[0] = SPA_PARAM_INFO(SPA_PARAM_EnumPortConfig, SPA_PARAM_INFO_READ);
this->params[1] = SPA_PARAM_INFO(SPA_PARAM_PortConfig, SPA_PARAM_INFO_READWRITE);
this->params[2] = SPA_PARAM_INFO(SPA_PARAM_PropInfo, SPA_PARAM_INFO_READ);
this->params[3] = SPA_PARAM_INFO(SPA_PARAM_Props, SPA_PARAM_INFO_READWRITE);
this->info.params = this->params;
this->info.n_params = 4;
if (strcmp(str, "split") == 0) {
this->mode = MODE_SPLIT;
in_factory = &spa_fmtconvert_factory;
out_factory = &spa_splitter_factory;
}
else if (strcmp(str, "merge") == 0) {
this->mode = MODE_MERGE;
in_factory = &spa_merger_factory;
out_factory = &spa_fmtconvert_factory;
}
else {
this->mode = MODE_CONVERT;
in_factory = &spa_fmtconvert_factory;
out_factory = &spa_fmtconvert_factory;
}
this->hnd_fmt[SPA_DIRECTION_INPUT] = SPA_MEMBER(this, sizeof(struct impl), struct spa_handle);
spa_handle_factory_init(in_factory,
this->hnd_fmt[SPA_DIRECTION_INPUT],
this->hnd_merger = SPA_MEMBER(this, sizeof(struct impl), struct spa_handle);
spa_handle_factory_init(&spa_merger_factory,
this->hnd_merger,
info, support, n_support);
size = spa_handle_factory_get_size(in_factory, info);
size = spa_handle_factory_get_size(&spa_merger_factory, info);
this->hnd_channelmix = SPA_MEMBER(this->hnd_fmt[SPA_DIRECTION_INPUT], size, struct spa_handle);
this->hnd_convert_in = SPA_MEMBER(this->hnd_merger, size, struct spa_handle);
spa_handle_factory_init(&spa_fmtconvert_factory,
this->hnd_convert_in,
info, support, n_support);
size = spa_handle_factory_get_size(&spa_fmtconvert_factory, info);
this->hnd_channelmix = SPA_MEMBER(this->hnd_convert_in, size, struct spa_handle);
spa_handle_factory_init(&spa_channelmix_factory,
this->hnd_channelmix,
info, support, n_support);
@ -1044,29 +1201,37 @@ impl_init(const struct spa_handle_factory *factory,
info, support, n_support);
size = spa_handle_factory_get_size(&spa_resample_factory, info);
this->hnd_fmt[SPA_DIRECTION_OUTPUT] = SPA_MEMBER(this->hnd_resample, size, struct spa_handle);
spa_handle_factory_init(out_factory,
this->hnd_fmt[SPA_DIRECTION_OUTPUT],
this->hnd_convert_out = SPA_MEMBER(this->hnd_resample, size, struct spa_handle);
spa_handle_factory_init(&spa_fmtconvert_factory,
this->hnd_convert_out,
info, support, n_support);
size = spa_handle_factory_get_size(out_factory, info);
size = spa_handle_factory_get_size(&spa_fmtconvert_factory, info);
spa_handle_get_interface(this->hnd_fmt[SPA_DIRECTION_INPUT], SPA_TYPE_INTERFACE_Node, &iface);
this->fmt[SPA_DIRECTION_INPUT] = iface;
this->hnd_splitter = SPA_MEMBER(this->hnd_convert_out, size, struct spa_handle);
spa_handle_factory_init(&spa_splitter_factory,
this->hnd_splitter,
info, support, n_support);
spa_handle_get_interface(this->hnd_merger, SPA_TYPE_INTERFACE_Node, &iface);
this->merger = iface;
spa_handle_get_interface(this->hnd_convert_in, SPA_TYPE_INTERFACE_Node, &iface);
this->convert_in = iface;
spa_handle_get_interface(this->hnd_channelmix, SPA_TYPE_INTERFACE_Node, &iface);
this->channelmix = iface;
spa_handle_get_interface(this->hnd_resample, SPA_TYPE_INTERFACE_Node, &iface);
this->resample = iface;
spa_handle_get_interface(this->hnd_fmt[SPA_DIRECTION_OUTPUT], SPA_TYPE_INTERFACE_Node, &iface);
this->fmt[SPA_DIRECTION_OUTPUT] = iface;
spa_handle_get_interface(this->hnd_convert_out, SPA_TYPE_INTERFACE_Node, &iface);
this->convert_out = iface;
spa_handle_get_interface(this->hnd_splitter, SPA_TYPE_INTERFACE_Node, &iface);
this->splitter = iface;
reconfigure_mode(this, SPA_PARAM_PORT_CONFIG_MODE_convert, SPA_DIRECTION_OUTPUT, false, NULL);
reconfigure_mode(this, SPA_PARAM_PORT_CONFIG_MODE_convert, SPA_DIRECTION_INPUT, false, NULL);
spa_node_add_listener(this->fmt[SPA_DIRECTION_INPUT],
&this->listener[0], &fmt_input_events, this);
spa_node_add_listener(this->channelmix,
&this->listener[1], &node_events, this);
&this->listener[0], &node_events, this);
spa_node_add_listener(this->resample,
&this->listener[2], &node_events, this);
spa_node_add_listener(this->fmt[SPA_DIRECTION_OUTPUT],
&this->listener[3], &fmt_output_events, this);
&this->listener[1], &node_events, this);
return 0;
}

View file

@ -25,7 +25,6 @@
#include <errno.h>
#include <string.h>
#include <stdio.h>
//#include <math.h>
#include <spa/support/log.h>
#include <spa/support/cpu.h>

View file

@ -132,9 +132,9 @@ static int can_convert(const struct spa_audio_info *info1, const struct spa_audi
{
if (info1->info.raw.channels != info2->info.raw.channels ||
info1->info.raw.rate != info2->info.raw.rate) {
return -EINVAL;
return 0;
}
return 0;
return 1;
}
static int setup_convert(struct impl *this)
@ -165,7 +165,7 @@ static int setup_convert(struct impl *this)
outformat.info.raw.channels,
outformat.info.raw.rate);
if (can_convert(&informat, &outformat) < 0)
if (!can_convert(&informat, &outformat))
return -EINVAL;
for (i = 0; i < informat.info.raw.channels; i++) {
@ -555,7 +555,7 @@ static int port_set_format(void *object,
{
struct impl *this = object;
struct port *port, *other;
int res = 0;
int res;
port = GET_PORT(this, direction, port_id);
other = GET_PORT(this, SPA_DIRECTION_REVERSE(direction), port_id);
@ -584,7 +584,7 @@ static int port_set_format(void *object,
spa_log_info(this->log, NAME "%p: %d %d %d %d", this,
info.info.raw.channels, other->format.info.raw.channels,
info.info.raw.rate, other->format.info.raw.rate);
if (can_convert(&info, &other->format) < 0)
if (!can_convert(&info, &other->format))
return -ENOTSUP;
}
@ -602,10 +602,11 @@ static int port_set_format(void *object,
port->format = info;
if (other->have_format && port->have_format)
res = setup_convert(this);
if ((res = setup_convert(this)) < 0)
return res;
spa_log_debug(this->log, NAME " %p: set format on port %d %d %d",
this, port_id, res, port->stride);
spa_log_debug(this->log, NAME " %p: set format on port %d:%d res:%d stride:%d",
this, direction, port_id, res, port->stride);
}
if (port->have_format) {
port->params[3] = SPA_PARAM_INFO(SPA_PARAM_Format, SPA_PARAM_INFO_READWRITE);
@ -614,7 +615,7 @@ static int port_set_format(void *object,
port->params[3] = SPA_PARAM_INFO(SPA_PARAM_Format, SPA_PARAM_INFO_WRITE);
port->params[4] = SPA_PARAM_INFO(SPA_PARAM_Buffers, 0);
}
return res;
return 0;
}
static int
@ -623,10 +624,14 @@ impl_node_port_set_param(void *object,
uint32_t id, uint32_t flags,
const struct spa_pod *param)
{
spa_return_val_if_fail(object != NULL, -EINVAL);
struct impl *this = object;
spa_return_val_if_fail(object != NULL, -EINVAL);
spa_return_val_if_fail(CHECK_PORT(object, direction, port_id), -EINVAL);
spa_log_debug(this->log, NAME " %p: set param %u on port %d:%d %p",
this, id, direction, port_id, param);
switch (id) {
case SPA_PARAM_Format:
return port_set_format(object, direction, port_id, flags, param);

View file

@ -186,8 +186,8 @@ static int init_port(struct impl *this, enum spa_direction direction, uint32_t p
port->format.info.raw.position[0] = position;
spa_list_init(&port->queue);
spa_log_debug(this->log, NAME " %p: add port %d rate:%d position:%s",
this, port_id, rate, port->position);
spa_log_debug(this->log, NAME " %p: add port %d:%d rate:%d position:%s",
this, direction, port_id, rate, port->position);
emit_port_info(this, port, true);
return 0;
@ -215,7 +215,7 @@ static int impl_node_enum_params(void *object, int seq,
spa_pod_builder_init(&b, buffer, sizeof(buffer));
switch (id) {
case SPA_PARAM_Profile:
case SPA_PARAM_PortConfig:
return -ENOTSUP;
default:
return 0;
@ -246,21 +246,32 @@ static int impl_node_set_param(void *object, uint32_t id, uint32_t flags,
spa_return_val_if_fail(this != NULL, -EINVAL);
switch (id) {
case SPA_PARAM_Profile:
case SPA_PARAM_PortConfig:
{
struct spa_audio_info info = { 0, };
struct port *port;
struct spa_pod *format;
enum spa_direction direction;
enum spa_param_port_config_mode mode;
bool monitor = false;
uint32_t i;
if (spa_pod_parse_object(param,
SPA_TYPE_OBJECT_ParamProfile, NULL,
SPA_PARAM_PROFILE_format, SPA_POD_Pod(&format)) < 0)
SPA_TYPE_OBJECT_ParamPortConfig, NULL,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(&direction),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(&mode),
SPA_PARAM_PORT_CONFIG_monitor, SPA_POD_OPT_Bool(&monitor),
SPA_PARAM_PORT_CONFIG_format, SPA_POD_Pod(&format)) < 0)
return -EINVAL;
if (!SPA_POD_IS_OBJECT_TYPE(format, SPA_TYPE_OBJECT_Format))
return -EINVAL;
if (mode != SPA_PARAM_PORT_CONFIG_MODE_dsp)
return -ENOTSUP;
if (direction != SPA_DIRECTION_INPUT)
return -EINVAL;
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
@ -275,8 +286,8 @@ static int impl_node_set_param(void *object, uint32_t id, uint32_t flags,
if (port->have_format && memcmp(&port->format, &info, sizeof(info)) == 0)
return 0;
spa_log_debug(this->log, NAME " %p: profile %d/%d", this,
info.info.raw.rate, info.info.raw.channels);
spa_log_debug(this->log, NAME " %p: port config %d/%d %d", this,
info.info.raw.rate, info.info.raw.channels, monitor);
for (i = 0; i < this->port_count; i++) {
spa_node_emit_port_info(&this->hooks,
@ -288,6 +299,7 @@ static int impl_node_set_param(void *object, uint32_t id, uint32_t flags,
port->have_format = true;
port->format = info;
this->monitor = monitor;
this->have_profile = true;
this->port_count = info.info.raw.channels;
@ -344,8 +356,11 @@ impl_node_add_listener(void *object,
emit_node_info(this, true);
emit_port_info(this, GET_OUT_PORT(this, 0), true);
for (i = 0; i < this->port_count; i++)
for (i = 0; i < this->port_count; i++) {
emit_port_info(this, GET_IN_PORT(this, i), true);
if (this->monitor)
emit_port_info(this, GET_OUT_PORT(this, i+1), true);
}
spa_hook_list_join(&this->hooks, &save);
@ -663,7 +678,8 @@ static int port_set_format(void *object,
port_id, port->stride, port->blocks);
if (!PORT_IS_DSP(direction, port_id))
setup_convert(this);
if ((res = setup_convert(this)) < 0)
return res;
port->have_format = true;
}
@ -1038,7 +1054,6 @@ impl_init(const struct spa_handle_factory *factory,
{
struct impl *this;
struct port *port;
const char *str;
uint32_t i;
spa_return_val_if_fail(factory != NULL, -EINVAL);
@ -1063,9 +1078,6 @@ impl_init(const struct spa_handle_factory *factory,
if (this->cpu)
this->cpu_flags = spa_cpu_get_flags(this->cpu);
if (info != NULL && (str = spa_dict_lookup(info, "merger.monitor")) != NULL)
this->monitor = atoi(str);
this->node.iface = SPA_INTERFACE_INIT(
SPA_TYPE_INTERFACE_Node,
SPA_VERSION_NODE,
@ -1078,7 +1090,7 @@ impl_init(const struct spa_handle_factory *factory,
this->info.max_input_ports = MAX_PORTS;
this->info.max_output_ports = MAX_PORTS+1;
this->info.flags = SPA_NODE_FLAG_RT;
this->params[0] = SPA_PARAM_INFO(SPA_PARAM_Profile, SPA_PARAM_INFO_WRITE);
this->params[0] = SPA_PARAM_INFO(SPA_PARAM_PortConfig, SPA_PARAM_INFO_WRITE);
this->info.params = this->params;
this->info.n_params = 1;

View file

@ -84,7 +84,16 @@ audioconvertlib = shared_library('spa-audioconvert',
install : true,
install_dir : '@0@/spa/audioconvert/'.format(get_option('libdir')))
test_lib = static_library('test_lib',
['test-source.c' ],
c_args : ['-O3'],
include_directories : [spa_inc],
install : false
)
test_apps = [
'test-audioadapter',
'test-audioconvert',
'test-fmt-ops',
'test-resample',
]
@ -94,7 +103,7 @@ foreach a : test_apps
executable(a, a + '.c',
dependencies : [dl_lib, pthread_lib, mathlib ],
include_directories : [spa_inc ],
link_with : simd_dependencies,
link_with : [ simd_dependencies, test_lib, audioconvertlib ],
c_args : [ '-D_GNU_SOURCE' ],
install : false),
env : [

View file

@ -114,6 +114,8 @@ static int impl_peaks_init(struct resample *r)
if (r->data == NULL)
return -errno;
spa_log_debug(r->log, "peaks %p: in:%d out:%d", r, r->i_rate, r->o_rate);
d->i_count = d->o_count = 0;
return 0;
}

View file

@ -117,7 +117,7 @@ struct impl {
#define MODE_CONVERT 2
int mode;
unsigned int started:1;
unsigned int monitor:1;
unsigned int peaks:1;
struct resample resample;
};
@ -161,7 +161,7 @@ static int setup_convert(struct impl *this,
this->resample.o_rate = dst_info->info.raw.rate;
this->resample.log = this->log;
if (this->monitor)
if (this->peaks)
err = impl_peaks_init(&this->resample);
else if (1)
err = impl_native_init(&this->resample);
@ -912,7 +912,7 @@ impl_init(const struct spa_handle_factory *factory,
if (info != NULL) {
if ((str = spa_dict_lookup(info, "resample.peaks")) != NULL)
this->monitor = atoi(str);
this->peaks = atoi(str);
if ((str = spa_dict_lookup(info, "factory.mode")) != NULL) {
if (strcmp(str, "split") == 0)
this->mode = MODE_SPLIT;

View file

@ -182,8 +182,8 @@ static int init_port(struct impl *this, enum spa_direction direction,
port->format.info.raw.channels = 1;
port->format.info.raw.position[0] = position;
spa_log_debug(this->log, NAME " %p: init port %d rate:%d position:%s",
this, port_id, rate, port->position);
spa_log_debug(this->log, NAME " %p: init port %d:%d rate:%d position:%s",
this, direction, port_id, rate, port->position);
emit_port_info(this, port, true);
return 0;
@ -240,27 +240,36 @@ static int impl_node_set_param(void *object, uint32_t id, uint32_t flags,
spa_return_val_if_fail(this != NULL, -EINVAL);
switch (id) {
case SPA_PARAM_Profile:
case SPA_PARAM_PortConfig:
{
struct port *port;
struct spa_audio_info info = { 0, };
struct spa_pod *format;
enum spa_direction direction;
enum spa_param_port_config_mode mode;
uint32_t i;
if (spa_pod_parse_object(param,
SPA_TYPE_OBJECT_ParamProfile, NULL,
SPA_PARAM_PROFILE_format, SPA_POD_Pod(&format)) < 0)
SPA_TYPE_OBJECT_ParamPortConfig, NULL,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(&direction),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(&mode),
SPA_PARAM_PORT_CONFIG_format, SPA_POD_Pod(&format)) < 0)
return -EINVAL;
if (!SPA_POD_IS_OBJECT_TYPE(format, SPA_TYPE_OBJECT_Format))
return -EINVAL;
if (mode != SPA_PARAM_PORT_CONFIG_MODE_dsp)
return -ENOTSUP;
if (direction != SPA_DIRECTION_OUTPUT)
return -EINVAL;
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
if (info.media_type != SPA_MEDIA_TYPE_audio ||
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)
return -EINVAL;
return -ENOTSUP;
if (spa_format_audio_raw_parse(format, &info.info.raw) < 0)
return -EINVAL;
@ -325,6 +334,7 @@ impl_node_add_listener(void *object,
spa_return_val_if_fail(this != NULL, -EINVAL);
spa_log_debug(this->log, NAME " %p: listener %p", this, listener);
spa_hook_list_isolate(&this->hooks, &save, listener, events, data);
emit_node_info(this, true);
@ -635,7 +645,8 @@ static int port_set_format(void *object,
spa_log_debug(this->log, NAME " %p: %d %d %d", this, port_id, port->stride, port->blocks);
if (direction == SPA_DIRECTION_INPUT)
setup_convert(this);
if ((res = setup_convert(this)) < 0)
return res;
port->have_format = true;
}
@ -994,7 +1005,7 @@ impl_init(const struct spa_handle_factory *factory,
this->info.max_input_ports = 1;
this->info.max_output_ports = MAX_PORTS;
this->info.flags = SPA_NODE_FLAG_RT;
this->params[0] = SPA_PARAM_INFO(SPA_PARAM_Profile, SPA_PARAM_INFO_WRITE);
this->params[0] = SPA_PARAM_INFO(SPA_PARAM_PortConfig, SPA_PARAM_INFO_WRITE);
this->info.params = this->params;
this->info.n_params = 1;

View file

@ -0,0 +1,245 @@
/* Spa
*
* Copyright © 2019 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <spa/utils/names.h>
#include <spa/support/plugin.h>
#include <spa/param/param.h>
#include <spa/param/audio/format.h>
#include <spa/param/audio/format-utils.h>
#include <spa/node/node.h>
#include <spa/debug/mem.h>
#include <spa/support/log-impl.h>
SPA_LOG_IMPL(logger);
extern const struct spa_handle_factory test_source_factory;
struct context {
struct spa_handle *slave_handle;
struct spa_node *slave_node;
struct spa_handle *adapter_handle;
struct spa_node *adapter_node;
};
static const struct spa_handle_factory *find_factory(const char *name)
{
uint32_t index = 0;
const struct spa_handle_factory *factory;
while (spa_handle_factory_enum(&factory, &index) == 1) {
if (strcmp(factory->name, name) == 0)
return factory;
}
return NULL;
}
static int setup_context(struct context *ctx)
{
size_t size;
int res;
struct spa_support support[1];
struct spa_dict_item items[1];
const struct spa_handle_factory *factory;
char value[32];
void *iface;
logger.log.level = SPA_LOG_LEVEL_TRACE;
support[0] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Log, &logger);
/* make slave */
factory = &test_source_factory;
size = spa_handle_factory_get_size(factory, NULL);
ctx->slave_handle = calloc(1, size);
spa_assert(ctx->slave_handle != NULL);
res = spa_handle_factory_init(factory,
ctx->slave_handle,
NULL, support, 1);
spa_assert(res >= 0);
res = spa_handle_get_interface(ctx->slave_handle,
SPA_TYPE_INTERFACE_Node, &iface);
spa_assert(res >= 0);
ctx->slave_node = iface;
/* make adapter */
factory = find_factory(SPA_NAME_AUDIO_ADAPT);
spa_assert(factory != NULL);
size = spa_handle_factory_get_size(factory, NULL);
ctx->adapter_handle = calloc(1, size);
spa_assert(ctx->adapter_handle != NULL);
snprintf(value, sizeof(value), "pointer:%p", ctx->slave_node);
items[0] = SPA_DICT_ITEM_INIT("audio.adapt.slave", value);
res = spa_handle_factory_init(factory,
ctx->adapter_handle,
&SPA_DICT_INIT(items, 1),
support, 1);
spa_assert(res >= 0);
res = spa_handle_get_interface(ctx->adapter_handle,
SPA_TYPE_INTERFACE_Node, &iface);
spa_assert(res >= 0);
ctx->adapter_node = iface;
return 0;
}
static int clean_context(struct context *ctx)
{
spa_handle_clear(ctx->adapter_handle);
spa_handle_clear(ctx->slave_handle);
free(ctx->adapter_handle);
free(ctx->slave_handle);
return 0;
}
static void node_info(void *data, const struct spa_node_info *info)
{
fprintf(stderr, "input %d, output %d\n",
info->max_input_ports,
info->max_output_ports);
spa_assert(info->max_input_ports == 0);
spa_assert(info->max_output_ports > 0);
}
static void port_info_none(void *data,
enum spa_direction direction, uint32_t port,
const struct spa_port_info *info)
{
spa_assert_not_reached();
}
static int test_init_state(struct context *ctx)
{
struct spa_hook listener;
static const struct spa_node_events init_events = {
SPA_VERSION_NODE_EVENTS,
.info = node_info,
.port_info = port_info_none,
};
spa_zero(listener);
spa_node_add_listener(ctx->adapter_node,
&listener, &init_events, ctx);
spa_hook_remove(&listener);
return 0;
}
static void port_info_5_1(void *data,
enum spa_direction direction, uint32_t port,
const struct spa_port_info *info)
{
spa_assert(direction == SPA_DIRECTION_OUTPUT);
spa_assert(port < 6);
}
static int test_split_setup(struct context *ctx)
{
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_pod *param;
struct spa_audio_info_raw info;
int res;
struct spa_hook listener;
static const struct spa_node_events node_events = {
SPA_VERSION_NODE_EVENTS,
.port_info = port_info_5_1,
};
/* external format */
spa_zero(info);
info.format = SPA_AUDIO_FORMAT_F32P;
info.channels = 6;
info.position[0] = SPA_AUDIO_CHANNEL_FL;
info.position[1] = SPA_AUDIO_CHANNEL_FR;
info.position[2] = SPA_AUDIO_CHANNEL_FC;
info.position[3] = SPA_AUDIO_CHANNEL_LFE;
info.position[4] = SPA_AUDIO_CHANNEL_SL;
info.position[5] = SPA_AUDIO_CHANNEL_SR;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
param = spa_format_audio_raw_build(&b, SPA_PARAM_Format, &info);
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamPortConfig, SPA_PARAM_PortConfig,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(SPA_DIRECTION_OUTPUT),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_dsp),
SPA_PARAM_PORT_CONFIG_format, SPA_POD_Pod(param));
res = spa_node_set_param(ctx->adapter_node, SPA_PARAM_PortConfig, 0, param);
spa_assert(res == 0);
spa_zero(listener);
spa_node_add_listener(ctx->adapter_node,
&listener, &node_events, ctx);
spa_hook_remove(&listener);
/* internal format */
spa_pod_builder_init(&b, buffer, sizeof(buffer));
spa_zero(info);
info.format = SPA_AUDIO_FORMAT_S16;
info.channels = 2;
info.position[0] = SPA_AUDIO_CHANNEL_FL;
info.position[1] = SPA_AUDIO_CHANNEL_FR;
param = spa_format_audio_raw_build(&b, SPA_PARAM_Format, &info);
res = spa_node_set_param(ctx->adapter_node, SPA_PARAM_Format, 0, param);
spa_assert(res == 0);
return 0;
}
int main(int argc, char *argv[])
{
struct context ctx;
spa_zero(ctx);
setup_context(&ctx);
test_init_state(&ctx);
test_split_setup(&ctx);
clean_context(&ctx);
return 0;
}

View file

@ -0,0 +1,536 @@
/* Spa
*
* Copyright © 2019 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <spa/utils/names.h>
#include <spa/support/plugin.h>
#include <spa/param/param.h>
#include <spa/param/audio/format.h>
#include <spa/param/audio/format-utils.h>
#include <spa/node/node.h>
#include <spa/debug/mem.h>
#include <spa/support/log-impl.h>
SPA_LOG_IMPL(logger);
extern const struct spa_handle_factory test_source_factory;
#define MAX_PORTS 128
struct context {
struct spa_handle *convert_handle;
struct spa_node *convert_node;
bool got_node_info;
uint32_t n_port_info[2];
bool got_port_info[2][MAX_PORTS];
};
static const struct spa_handle_factory *find_factory(const char *name)
{
uint32_t index = 0;
const struct spa_handle_factory *factory;
while (spa_handle_factory_enum(&factory, &index) == 1) {
if (strcmp(factory->name, name) == 0)
return factory;
}
return NULL;
}
static int setup_context(struct context *ctx)
{
size_t size;
int res;
struct spa_support support[1];
const struct spa_handle_factory *factory;
void *iface;
logger.log.level = SPA_LOG_LEVEL_TRACE;
support[0] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Log, &logger);
/* make convert */
factory = find_factory(SPA_NAME_AUDIO_CONVERT);
spa_assert(factory != NULL);
size = spa_handle_factory_get_size(factory, NULL);
ctx->convert_handle = calloc(1, size);
spa_assert(ctx->convert_handle != NULL);
res = spa_handle_factory_init(factory,
ctx->convert_handle,
NULL,
support, 1);
spa_assert(res >= 0);
res = spa_handle_get_interface(ctx->convert_handle,
SPA_TYPE_INTERFACE_Node, &iface);
spa_assert(res >= 0);
ctx->convert_node = iface;
return 0;
}
static int clean_context(struct context *ctx)
{
spa_handle_clear(ctx->convert_handle);
free(ctx->convert_handle);
return 0;
}
static void node_info_check(void *data, const struct spa_node_info *info)
{
struct context *ctx = data;
fprintf(stderr, "input %d, output %d\n",
info->max_input_ports,
info->max_output_ports);
spa_assert(info->max_input_ports == 128);
spa_assert(info->max_output_ports == 128);
ctx->got_node_info = true;
}
static void port_info_check(void *data,
enum spa_direction direction, uint32_t port,
const struct spa_port_info *info)
{
struct context *ctx = data;
fprintf(stderr, "port %d %d %p\n", direction, port, info);
ctx->got_port_info[direction][port] = true;
ctx->n_port_info[direction]++;
}
static int test_init_state(struct context *ctx)
{
struct spa_hook listener;
static const struct spa_node_events init_events = {
SPA_VERSION_NODE_EVENTS,
.info = node_info_check,
.port_info = port_info_check,
};
spa_zero(ctx->got_node_info);
spa_zero(ctx->n_port_info);
spa_zero(ctx->got_port_info);
spa_zero(listener);
spa_node_add_listener(ctx->convert_node,
&listener, &init_events, ctx);
spa_hook_remove(&listener);
spa_assert(ctx->got_node_info);
spa_assert(ctx->n_port_info[0] == 1);
spa_assert(ctx->n_port_info[1] == 1);
spa_assert(ctx->got_port_info[0][0] == true);
spa_assert(ctx->got_port_info[1][0] == true);
return 0;
}
static int test_set_in_format(struct context *ctx)
{
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_pod *param;
struct spa_audio_info_raw info;
int res;
/* other format */
spa_pod_builder_init(&b, buffer, sizeof(buffer));
info = (struct spa_audio_info_raw) {
.format = SPA_AUDIO_FORMAT_S16,
.rate = 44100,
.channels = 2,
.position = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, }
};
param = spa_format_audio_raw_build(&b, SPA_PARAM_Format, &info);
res = spa_node_port_set_param(ctx->convert_node, SPA_DIRECTION_INPUT, 0,
SPA_PARAM_Format, 0, param);
spa_assert(res == 0);
return 0;
}
static int test_split_setup1(struct context *ctx)
{
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_pod *param;
struct spa_audio_info_raw info;
int res;
struct spa_hook listener;
static const struct spa_node_events node_events = {
SPA_VERSION_NODE_EVENTS,
.port_info = port_info_check,
};
spa_zero(listener);
spa_node_add_listener(ctx->convert_node,
&listener, &node_events, ctx);
/* port config, output as DSP */
spa_zero(info);
info.format = SPA_AUDIO_FORMAT_F32P;
info.rate = 48000;
info.channels = 6;
info.position[0] = SPA_AUDIO_CHANNEL_FL;
info.position[1] = SPA_AUDIO_CHANNEL_FR;
info.position[2] = SPA_AUDIO_CHANNEL_FC;
info.position[3] = SPA_AUDIO_CHANNEL_LFE;
info.position[4] = SPA_AUDIO_CHANNEL_SL;
info.position[5] = SPA_AUDIO_CHANNEL_SR;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
param = spa_format_audio_raw_build(&b, SPA_PARAM_Format, &info);
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamPortConfig, SPA_PARAM_PortConfig,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(SPA_DIRECTION_OUTPUT),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_dsp),
SPA_PARAM_PORT_CONFIG_format, SPA_POD_Pod(param));
res = spa_node_set_param(ctx->convert_node, SPA_PARAM_PortConfig, 0, param);
spa_assert(res == 0);
spa_hook_remove(&listener);
return 0;
}
static int test_split_setup2(struct context *ctx)
{
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_pod *param;
struct spa_audio_info_raw info;
int res;
struct spa_hook listener;
static const struct spa_node_events node_events = {
SPA_VERSION_NODE_EVENTS,
.port_info = port_info_check,
};
spa_zero(listener);
spa_node_add_listener(ctx->convert_node,
&listener, &node_events, ctx);
/* port config, output as DSP */
spa_zero(info);
info.format = SPA_AUDIO_FORMAT_F32P;
info.rate = 48000;
info.channels = 4;
info.position[0] = SPA_AUDIO_CHANNEL_FL;
info.position[1] = SPA_AUDIO_CHANNEL_FR;
info.position[2] = SPA_AUDIO_CHANNEL_RL;
info.position[3] = SPA_AUDIO_CHANNEL_RR;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
param = spa_format_audio_raw_build(&b, SPA_PARAM_Format, &info);
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamPortConfig, SPA_PARAM_PortConfig,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(SPA_DIRECTION_OUTPUT),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_dsp),
SPA_PARAM_PORT_CONFIG_format, SPA_POD_Pod(param));
res = spa_node_set_param(ctx->convert_node, SPA_PARAM_PortConfig, 0, param);
spa_assert(res == 0);
spa_hook_remove(&listener);
return 0;
}
static int test_convert_setup1(struct context *ctx)
{
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_pod *param;
int res;
struct spa_hook listener;
static const struct spa_node_events node_events = {
SPA_VERSION_NODE_EVENTS,
.port_info = port_info_check,
};
spa_zero(listener);
spa_node_add_listener(ctx->convert_node,
&listener, &node_events, ctx);
/* port config, output convert */
spa_pod_builder_init(&b, buffer, sizeof(buffer));
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamPortConfig, SPA_PARAM_PortConfig,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(SPA_DIRECTION_OUTPUT),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_convert));
res = spa_node_set_param(ctx->convert_node, SPA_PARAM_PortConfig, 0, param);
spa_assert(res == 0);
spa_hook_remove(&listener);
return 0;
}
static int test_set_out_format(struct context *ctx)
{
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_pod *param;
struct spa_audio_info_raw info;
int res;
/* out format */
spa_pod_builder_init(&b, buffer, sizeof(buffer));
info = (struct spa_audio_info_raw) {
.format = SPA_AUDIO_FORMAT_S32P,
.rate = 96000,
.channels = 8,
.position = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR,
SPA_AUDIO_CHANNEL_FC, SPA_AUDIO_CHANNEL_LFE,
SPA_AUDIO_CHANNEL_SL, SPA_AUDIO_CHANNEL_SR,
SPA_AUDIO_CHANNEL_RL, SPA_AUDIO_CHANNEL_RR, }
};
param = spa_format_audio_raw_build(&b, SPA_PARAM_Format, &info);
res = spa_node_port_set_param(ctx->convert_node, SPA_DIRECTION_OUTPUT, 0,
SPA_PARAM_Format, 0, param);
spa_assert(res == 0);
return 0;
}
static int test_merge_setup1(struct context *ctx)
{
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_pod *param;
struct spa_audio_info_raw info;
int res;
struct spa_hook listener;
static const struct spa_node_events node_events = {
SPA_VERSION_NODE_EVENTS,
.port_info = port_info_check,
};
spa_zero(listener);
spa_node_add_listener(ctx->convert_node,
&listener, &node_events, ctx);
/* port config, output as DSP */
spa_zero(info);
info.format = SPA_AUDIO_FORMAT_F32P;
info.rate = 48000;
info.channels = 6;
info.position[0] = SPA_AUDIO_CHANNEL_FL;
info.position[1] = SPA_AUDIO_CHANNEL_FR;
info.position[2] = SPA_AUDIO_CHANNEL_FC;
info.position[3] = SPA_AUDIO_CHANNEL_LFE;
info.position[4] = SPA_AUDIO_CHANNEL_RL;
info.position[5] = SPA_AUDIO_CHANNEL_RR;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
param = spa_format_audio_raw_build(&b, SPA_PARAM_Format, &info);
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamPortConfig, SPA_PARAM_PortConfig,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(SPA_DIRECTION_INPUT),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_dsp),
SPA_PARAM_PORT_CONFIG_format, SPA_POD_Pod(param));
res = spa_node_set_param(ctx->convert_node, SPA_PARAM_PortConfig, 0, param);
spa_assert(res == 0);
spa_hook_remove(&listener);
return 0;
}
static int test_set_out_format2(struct context *ctx)
{
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_pod *param;
struct spa_audio_info_raw info;
int res;
/* out format */
spa_pod_builder_init(&b, buffer, sizeof(buffer));
info = (struct spa_audio_info_raw) {
.format = SPA_AUDIO_FORMAT_S16,
.rate = 32000,
.channels = 2,
.position = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, }
};
param = spa_format_audio_raw_build(&b, SPA_PARAM_Format, &info);
res = spa_node_port_set_param(ctx->convert_node, SPA_DIRECTION_OUTPUT, 0,
SPA_PARAM_Format, 0, param);
spa_assert(res == 0);
return 0;
}
static int test_merge_setup2(struct context *ctx)
{
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_pod *param;
struct spa_audio_info_raw info;
int res;
struct spa_hook listener;
static const struct spa_node_events node_events = {
SPA_VERSION_NODE_EVENTS,
.port_info = port_info_check,
};
spa_zero(listener);
spa_node_add_listener(ctx->convert_node,
&listener, &node_events, ctx);
/* port config, output as DSP */
spa_zero(info);
info.format = SPA_AUDIO_FORMAT_F32P;
info.rate = 96000;
info.channels = 4;
info.position[0] = SPA_AUDIO_CHANNEL_FL;
info.position[1] = SPA_AUDIO_CHANNEL_FR;
info.position[2] = SPA_AUDIO_CHANNEL_FC;
info.position[3] = SPA_AUDIO_CHANNEL_LFE;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
param = spa_format_audio_raw_build(&b, SPA_PARAM_Format, &info);
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamPortConfig, SPA_PARAM_PortConfig,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(SPA_DIRECTION_INPUT),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_dsp),
SPA_PARAM_PORT_CONFIG_format, SPA_POD_Pod(param));
res = spa_node_set_param(ctx->convert_node, SPA_PARAM_PortConfig, 0, param);
spa_assert(res == 0);
spa_hook_remove(&listener);
return 0;
}
static int test_convert_setup2(struct context *ctx)
{
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_pod *param;
int res;
struct spa_hook listener;
static const struct spa_node_events node_events = {
SPA_VERSION_NODE_EVENTS,
.port_info = port_info_check,
};
spa_zero(listener);
spa_node_add_listener(ctx->convert_node,
&listener, &node_events, ctx);
/* port config, input convert */
spa_pod_builder_init(&b, buffer, sizeof(buffer));
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamPortConfig, SPA_PARAM_PortConfig,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(SPA_DIRECTION_INPUT),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_convert));
res = spa_node_set_param(ctx->convert_node, SPA_PARAM_PortConfig, 0, param);
spa_assert(res == 0);
spa_hook_remove(&listener);
return 0;
}
static int test_set_in_format2(struct context *ctx)
{
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_pod *param;
struct spa_audio_info_raw info;
int res;
/* other format */
spa_pod_builder_init(&b, buffer, sizeof(buffer));
info = (struct spa_audio_info_raw) {
.format = SPA_AUDIO_FORMAT_S24,
.rate = 48000,
.channels = 3,
.position = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR,
SPA_AUDIO_CHANNEL_LFE, }
};
param = spa_format_audio_raw_build(&b, SPA_PARAM_Format, &info);
res = spa_node_port_set_param(ctx->convert_node, SPA_DIRECTION_INPUT, 0,
SPA_PARAM_Format, 0, param);
spa_assert(res == 0);
return 0;
}
int main(int argc, char *argv[])
{
struct context ctx;
spa_zero(ctx);
setup_context(&ctx);
test_init_state(&ctx);
test_set_in_format(&ctx);
test_split_setup1(&ctx);
test_split_setup2(&ctx);
test_convert_setup1(&ctx);
test_set_out_format(&ctx);
test_merge_setup1(&ctx);
test_set_out_format2(&ctx);
test_merge_setup2(&ctx);
test_convert_setup2(&ctx);
test_set_in_format2(&ctx);
test_set_out_format(&ctx);
clean_context(&ctx);
return 0;
}

View file

@ -913,7 +913,7 @@ impl_init(const struct spa_handle_factory *factory,
this->info.max_input_ports = MAX_PORTS;
this->info.max_output_ports = 1;
this->info.change_mask |= SPA_NODE_CHANGE_MASK_FLAGS;
this->info.flags = SPA_NODE_FLAG_DYNAMIC_INPUT_PORTS |
this->info.flags = SPA_NODE_FLAG_IN_DYNAMIC_PORTS |
SPA_NODE_FLAG_RT;
this->info.params = this->params;

View file

@ -210,7 +210,7 @@ static int impl_node_set_param(void *object, uint32_t id, uint32_t flags,
spa_log_debug(this->log, NAME" %p: set param %d", this, id);
switch (id) {
case SPA_PARAM_Profile:
case SPA_PARAM_PortConfig:
if (this->started)
return -EIO;
if (this->target != this->slave) {
@ -885,9 +885,6 @@ impl_init(const struct spa_handle_factory *factory,
if (this->slave == NULL)
return -EINVAL;
if ((str = spa_dict_lookup(info, "merger.monitor")) != NULL)
this->monitor = atoi(str);
spa_node_add_listener(this->slave,
&this->slave_listener, &slave_node_events, this);
spa_node_set_callbacks(this->slave, &slave_node_callbacks, this);
@ -928,7 +925,7 @@ impl_init(const struct spa_handle_factory *factory,
this->params[1] = SPA_PARAM_INFO(SPA_PARAM_PropInfo, SPA_PARAM_INFO_READ);
this->params[2] = SPA_PARAM_INFO(SPA_PARAM_Props, SPA_PARAM_INFO_READWRITE);
this->params[3] = SPA_PARAM_INFO(SPA_PARAM_Format, SPA_PARAM_INFO_READ);
this->params[4] = SPA_PARAM_INFO(SPA_PARAM_Profile, SPA_PARAM_INFO_WRITE);
this->params[4] = SPA_PARAM_INFO(SPA_PARAM_PortConfig, SPA_PARAM_INFO_WRITE);
this->info.params = this->params;
this->info.n_params = 5;

View file

@ -118,7 +118,8 @@ static void test_abi(void)
spa_assert(SPA_TYPE_OBJECT_ParamMeta == 0x50005);
spa_assert(SPA_TYPE_OBJECT_ParamIO == 0x50006);
spa_assert(SPA_TYPE_OBJECT_ParamProfile == 0x50007);
spa_assert(SPA_TYPE_OBJECT_LAST == 0x50008);
spa_assert(SPA_TYPE_OBJECT_ParamPortConfig == 0x50008);
spa_assert(SPA_TYPE_OBJECT_LAST == 0x50009);
spa_assert(SPA_TYPE_VENDOR_PipeWire == 0x02000000);
spa_assert(SPA_TYPE_VENDOR_Other == 0x7f000000);

View file

@ -121,7 +121,6 @@ static struct alsa_node *alsa_create_node(struct alsa_object *obj, uint32_t id,
str = "alsa-device";
pw_properties_set(node->props, PW_KEY_NODE_NAME, str);
pw_properties_set(node->props, "factory.name", info->factory_name);
pw_properties_set(node->props, "merger.monitor", "1");
node->monitor = monitor;
node->object = obj;

View file

@ -1098,15 +1098,16 @@ do_link_profile:
spa_pod_builder_init(&b, buf, sizeof(buf));
param = spa_format_audio_raw_build(&b, SPA_PARAM_Format, &audio_info);
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamProfile, SPA_PARAM_Profile,
SPA_PARAM_PROFILE_direction, SPA_POD_Id(pw_direction_reverse(direction)),
SPA_PARAM_PROFILE_format, SPA_POD_Pod(param));
SPA_TYPE_OBJECT_ParamPortConfig, SPA_PARAM_PortConfig,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(pw_direction_reverse(direction)),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_dsp),
SPA_PARAM_PORT_CONFIG_format, SPA_POD_Pod(param));
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
spa_debug_pod(2, NULL, param);
pw_node_proxy_set_param((struct pw_node_proxy*)node->obj.proxy,
SPA_PARAM_Profile, 0, param);
SPA_PARAM_PortConfig, 0, param);
stream_set_volume(impl, node, 1.0, false);
n_links = audio_info.channels;
@ -1144,17 +1145,19 @@ static void rescan_session(struct impl *impl, struct session *sess)
info = node->format;
info.rate = DEFAULT_SAMPLERATE;
pw_log_debug(NAME" %p: setting profile for session %d", impl, sess->id);
pw_log_debug(NAME" %p: setting profile for session %d %d", impl, sess->id, sess->direction);
spa_pod_builder_init(&b, buf, sizeof(buf));
param = spa_format_audio_raw_build(&b, SPA_PARAM_Format, &info);
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamProfile, SPA_PARAM_Profile,
SPA_PARAM_PROFILE_direction, SPA_POD_Id(pw_direction_reverse(sess->direction)),
SPA_PARAM_PROFILE_format, SPA_POD_Pod(param));
SPA_TYPE_OBJECT_ParamPortConfig, SPA_PARAM_PortConfig,
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(pw_direction_reverse(sess->direction)),
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_dsp),
SPA_PARAM_PORT_CONFIG_monitor, SPA_POD_Bool(true),
SPA_PARAM_PORT_CONFIG_format, SPA_POD_Pod(param));
pw_node_proxy_set_param((struct pw_node_proxy*)sess->node->obj.proxy,
SPA_PARAM_Profile, 0, param);
SPA_PARAM_PortConfig, 0, param);
schedule_rescan(impl);
sess->starting = false;

View file

@ -97,18 +97,6 @@ test('pw-test-protocol-native',
'PIPEWIRE_MODULE_DIR=@0@/src/modules/'.format(meson.build_root())
])
pipewire_module_audio_dsp = shared_library('pipewire-module-audio-dsp',
[ 'module-audio-dsp.c',
'module-audio-dsp/audio-dsp.c',
'module-audio-dsp/floatmix.c',
'spa/spa-node.c' ],
c_args : pipewire_module_c_args,
include_directories : [configinc, spa_inc],
install : true,
install_dir : modules_install_dir,
dependencies : [mathlib, dl_lib, rt_lib, pipewire_dep],
)
pipewire_module_adapter = shared_library('pipewire-module-adapter',
[ 'module-adapter.c',
'module-adapter/adapter.c',

View file

@ -872,7 +872,7 @@ impl_init(const struct spa_handle_factory *factory,
this->info.max_input_ports = MAX_PORTS;
this->info.max_output_ports = 1;
this->info.change_mask |= SPA_NODE_CHANGE_MASK_FLAGS;
this->info.flags = SPA_NODE_FLAG_RT | SPA_NODE_FLAG_DYNAMIC_INPUT_PORTS;
this->info.flags = SPA_NODE_FLAG_RT | SPA_NODE_FLAG_IN_DYNAMIC_PORTS;
port = GET_OUT_PORT(this, 0);
port->valid = true;

View file

@ -1,232 +0,0 @@
/* PipeWire
*
* Copyright © 2018 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <dlfcn.h>
#include "config.h"
#include <spa/node/node.h>
#include <spa/utils/hook.h>
#include <spa/param/audio/format-utils.h>
#include <spa/pod/filter.h>
#include <pipewire/pipewire.h>
#include "pipewire/private.h"
#include "module-audio-dsp/audio-dsp.h"
static const struct spa_dict_item module_props[] = {
{ PW_KEY_MODULE_AUTHOR, "Wim Taymans <wim.taymans@gmail.com>" },
{ PW_KEY_MODULE_DESCRIPTION, "Manage audio DSP nodes" },
{ PW_KEY_MODULE_VERSION, PACKAGE_VERSION },
};
struct factory_data {
struct pw_factory *this;
struct spa_list node_list;
struct pw_module *module;
struct spa_hook module_listener;
};
struct node_data {
struct factory_data *data;
struct spa_list link;
struct pw_node *dsp;
struct spa_hook dsp_listener;
struct spa_hook resource_listener;
};
static void resource_destroy(void *data)
{
struct node_data *nd = data;
spa_hook_remove(&nd->resource_listener);
if (nd->dsp)
pw_node_destroy(nd->dsp);
}
static const struct pw_resource_events resource_events = {
PW_VERSION_RESOURCE_EVENTS,
.destroy = resource_destroy
};
static void node_destroy(void *data)
{
struct node_data *nd = data;
spa_list_remove(&nd->link);
nd->dsp = NULL;
}
static const struct pw_node_events node_events = {
PW_VERSION_NODE_EVENTS,
.destroy = node_destroy
};
static void *create_object(void *_data,
struct pw_resource *resource,
uint32_t type,
uint32_t version,
struct pw_properties *properties,
uint32_t new_id)
{
struct factory_data *d = _data;
struct pw_client *client;
struct pw_node *dsp;
int res;
struct node_data *nd;
struct pw_resource *bound_resource;
if (resource == NULL)
goto error_resource;
client = pw_resource_get_client(resource);
dsp = pw_audio_dsp_new(pw_module_get_core(d->module),
properties,
sizeof(struct node_data));
properties = NULL;
if (dsp == NULL) {
if (errno == ENOMEM)
goto error_no_mem;
else
goto error_usage;
}
nd = pw_audio_dsp_get_user_data(dsp);
nd->data = d;
nd->dsp = dsp;
spa_list_append(&d->node_list, &nd->link);
pw_node_register(dsp, client, pw_module_get_global(d->module), NULL);
pw_node_add_listener(dsp, &nd->dsp_listener, &node_events, nd);
res = pw_global_bind(pw_node_get_global(dsp), client,
PW_PERM_RWX, PW_VERSION_NODE_PROXY, new_id);
if (res < 0)
goto error_bind;
if ((bound_resource = pw_client_find_resource(client, new_id)) == NULL)
goto error_bind;
pw_resource_add_listener(bound_resource, &nd->resource_listener, &resource_events, nd);
pw_node_set_active(dsp, true);
return dsp;
error_resource:
res = -EINVAL;
pw_log_error("audio-dsp needs a resource");
pw_resource_error(resource, res, "no resource");
goto error_cleanup;
error_no_mem:
res = -errno;
pw_log_error("can't create node: %m");
pw_resource_error(resource, res, "no memory");
goto error_cleanup;
error_usage:
res = -EINVAL;
pw_log_error("usage: "AUDIO_DSP_USAGE);
pw_resource_error(resource, res, "usage: "AUDIO_DSP_USAGE);
goto error_cleanup;
error_bind:
pw_resource_error(resource, res, "can't bind dsp node");
goto error_cleanup;
error_cleanup:
if (properties)
pw_properties_free(properties);
errno = -res;
return NULL;
}
static const struct pw_factory_implementation impl_factory = {
PW_VERSION_FACTORY_IMPLEMENTATION,
.create_object = create_object,
};
static void module_destroy(void *data)
{
struct factory_data *d = data;
struct node_data *nd, *t;
spa_hook_remove(&d->module_listener);
spa_list_for_each_safe(nd, t, &d->node_list, link)
pw_node_destroy(nd->dsp);
pw_factory_destroy(d->this);
}
static const struct pw_module_events module_events = {
PW_VERSION_MODULE_EVENTS,
.destroy = module_destroy,
};
static int module_init(struct pw_module *module, struct pw_properties *properties)
{
struct pw_core *core = pw_module_get_core(module);
struct pw_factory *factory;
struct factory_data *data;
factory = pw_factory_new(core,
"audio-dsp",
PW_TYPE_INTERFACE_Node,
PW_VERSION_NODE_PROXY,
pw_properties_new(
PW_KEY_FACTORY_USAGE, AUDIO_DSP_USAGE,
NULL),
sizeof(*data));
if (factory == NULL)
return -errno;
data = pw_factory_get_user_data(factory);
data->this = factory;
data->module = module;
spa_list_init(&data->node_list);
pw_log_debug("module %p: new", module);
pw_factory_set_implementation(factory,
&impl_factory,
data);
pw_factory_register(factory, NULL, pw_module_get_global(module), NULL);
pw_module_add_listener(module, &data->module_listener, &module_events, data);
pw_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props));
return 0;
}
SPA_EXPORT
int pipewire__module_init(struct pw_module *module, const char *args)
{
return module_init(module, NULL);
}

View file

@ -1,359 +0,0 @@
/* PipeWire
*
* Copyright © 2018 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <time.h>
#include "config.h"
#include <spa/node/node.h>
#include <spa/utils/hook.h>
#include <spa/utils/names.h>
#include <spa/param/audio/format-utils.h>
#include <spa/utils/type-info.h>
#include <spa/param/audio/type-info.h>
#include "pipewire/pipewire.h"
#include "pipewire/private.h"
#include "modules/spa/spa-node.h"
#define NAME "audio-dsp"
#define PORT_BUFFERS 1
#define MAX_BUFFER_SIZE 2048
extern const struct spa_handle_factory spa_floatmix_factory;
struct buffer {
struct spa_buffer buf;
struct spa_data datas[1];
struct spa_chunk chunk[1];
};
struct node;
struct port {
struct spa_list link;
struct pw_port *port;
struct node *node;
struct buffer buffers[PORT_BUFFERS];
struct spa_buffer *bufs[PORT_BUFFERS];
struct spa_handle *spa_handle;
struct spa_node *spa_node;
float empty[MAX_BUFFER_SIZE + 15];
};
struct node {
struct pw_core *core;
struct pw_node *node;
struct spa_hook node_listener;
void *user_data;
enum pw_direction direction;
struct pw_properties *props;
struct spa_audio_info format;
int max_buffer_size;
struct spa_list ports;
};
/** \endcond */
static void init_buffer(struct port *port, uint32_t id)
{
struct buffer *b = &port->buffers[id];
b->buf.n_metas = 0;
b->buf.metas = NULL;
b->buf.n_datas = 1;
b->buf.datas = b->datas;
b->datas[0].type = SPA_DATA_MemPtr;
b->datas[0].flags = SPA_DATA_FLAG_DYNAMIC;
b->datas[0].fd = -1;
b->datas[0].mapoffset = 0;
b->datas[0].maxsize = SPA_ROUND_DOWN_N(sizeof(port->empty), 16);
b->datas[0].data = SPA_PTR_ALIGN(port->empty, 16, void);
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;
memset(port->empty, 0, sizeof(port->empty));
pw_log_debug("%p %d", b->datas[0].data, b->datas[0].maxsize);
}
static void init_port(struct port *p, enum spa_direction direction)
{
int i;
for (i = 0; i < PORT_BUFFERS; i++)
init_buffer(p, i);
}
static int port_use_buffers(void *data,
uint32_t flags,
struct spa_buffer **buffers, uint32_t n_buffers)
{
struct port *p = data;
struct pw_port *port = p->port;
struct pw_node *node = port->node;
int res, i;
pw_log_debug(NAME " %p: port %p", p->node->node, port);
if (n_buffers > 0) {
for (i = 0; i < PORT_BUFFERS; i++)
init_buffer(p, i);
n_buffers = PORT_BUFFERS;
buffers = p->bufs;
}
res = spa_node_port_use_buffers(port->mix,
pw_direction_reverse(port->direction), 0,
flags,
buffers, n_buffers);
res = spa_node_port_use_buffers(node->node,
port->direction, port->port_id,
flags,
buffers, n_buffers);
return res;
}
static const struct pw_port_implementation port_implementation = {
.use_buffers = port_use_buffers,
};
static void node_destroy(void *data)
{
struct node *n = data;
struct port *p, *tmp;
pw_properties_free(n->props);
spa_list_for_each_safe(p, tmp, &n->ports, link) {
pw_port_set_mix(p->port, NULL, 0);
spa_list_remove(&p->link);
spa_handle_clear(p->spa_handle);
free(p);
}
}
static void node_port_init(void *data, struct pw_port *port)
{
struct node *n = data;
struct port *p;
const struct pw_properties *old;
enum pw_direction direction;
struct pw_properties *new;
const char *str;
void *iface;
const struct spa_support *support;
uint32_t n_support;
char position[8], *prefix;
bool monitor;
direction = pw_port_get_direction(port);
old = pw_port_get_properties(port);
monitor = (str = pw_properties_get(old, PW_KEY_PORT_MONITOR)) != NULL &&
pw_properties_parse_bool(str);
if (!monitor && direction == n->direction)
return;
new = pw_properties_new(PW_KEY_FORMAT_DSP, "32 bit float mono audio", NULL);
if (monitor)
prefix = "monitor";
else if (direction == PW_DIRECTION_INPUT)
prefix = "playback";
else
prefix = "capture";
if ((str = pw_properties_get(old, PW_KEY_AUDIO_CHANNEL)) == NULL ||
strcmp(str, "UNK") == 0) {
snprintf(position, 7, "%d", port->port_id);
str = position;
}
pw_properties_setf(new, PW_KEY_PORT_NAME, "%s_%s", prefix, str);
if (direction != n->direction) {
pw_properties_setf(new, PW_KEY_PORT_ALIAS1, "%s_pcm:%s:%s%s",
pw_properties_get(n->props, PW_KEY_DEVICE_API),
pw_properties_get(n->props, "audio-dsp.name"),
direction == PW_DIRECTION_INPUT ? "in" : "out",
str);
pw_properties_set(new, PW_KEY_PORT_PHYSICAL, "1");
pw_properties_set(new, PW_KEY_PORT_TERMINAL, "1");
}
pw_port_update_properties(port, &new->dict);
pw_properties_free(new);
if (direction == n->direction)
return;
p = calloc(1, sizeof(struct port) +
spa_handle_factory_get_size(&spa_floatmix_factory, NULL));
p->node = n;
p->port = port;
init_port(p, direction);
p->spa_handle = SPA_MEMBER(p, sizeof(struct port), struct spa_handle);
support = pw_core_get_support(n->core, &n_support);
spa_handle_factory_init(&spa_floatmix_factory,
p->spa_handle, NULL,
support, n_support);
spa_handle_get_interface(p->spa_handle, SPA_TYPE_INTERFACE_Node, &iface);
p->spa_node = iface;
if (direction == PW_DIRECTION_INPUT) {
pw_log_debug("mix node %p", p->spa_node);
pw_port_set_mix(port, p->spa_node, PW_PORT_MIX_FLAG_MULTI);
port->impl = SPA_CALLBACKS_INIT(&port_implementation, p);
}
spa_list_append(&n->ports, &p->link);
}
static const struct pw_node_events node_events = {
PW_VERSION_NODE_EVENTS,
.destroy = node_destroy,
.port_init = node_port_init,
};
struct pw_node *pw_audio_dsp_new(struct pw_core *core,
struct pw_properties *props,
size_t user_data_size)
{
struct pw_node *node;
struct node *n;
const char *api, *alias, *str;
char node_name[128];
enum pw_direction direction;
uint32_t max_buffer_size;
int i, res = -ENOENT;
if ((str = pw_properties_get(props, "audio-dsp.direction")) == NULL) {
pw_log_error("missing audio-dsp.direction property");
goto error;
}
direction = pw_properties_parse_int(str);
if ((str = pw_properties_get(props, "audio-dsp.maxbuffer")) == NULL) {
pw_log_error("missing audio-dsp.maxbuffer property");
goto error;
}
max_buffer_size = pw_properties_parse_int(str);
if ((api = pw_properties_get(props, PW_KEY_DEVICE_API)) == NULL) {
pw_log_error("missing "PW_KEY_DEVICE_API" property");
goto error;
}
if ((alias = pw_properties_get(props, "audio-dsp.name")) == NULL) {
pw_log_error("missing audio-dsp.name property");
goto error;
}
snprintf(node_name, sizeof(node_name), "system_%s", alias);
for (i = 0; node_name[i]; i++) {
if (node_name[i] == ':' || node_name[i] == ',')
node_name[i] = '_';
}
pw_properties_set(props,
PW_KEY_MEDIA_CLASS,
direction == PW_DIRECTION_OUTPUT ?
"Audio/DSP/Playback" :
"Audio/DSP/Capture");
pw_properties_set(props, PW_KEY_NODE_DRIVER, NULL);
if ((str = pw_properties_get(props, PW_KEY_NODE_ID)) != NULL)
pw_properties_set(props, PW_KEY_NODE_SESSION, str);
if ((str = pw_properties_get(props, "audio-dsp.mode")) == NULL) {
if (direction == PW_DIRECTION_OUTPUT) {
if (pw_properties_get(props, "merger.monitor") == NULL)
pw_properties_set(props, "merger.monitor", "1");
str = "merge";
} else {
str = "split";
}
}
pw_properties_set(props, "factory.mode", str);
pw_properties_set(props, SPA_KEY_LIBRARY_NAME, "audioconvert/libspa-audioconvert");
node = pw_spa_node_load(core, NULL, NULL,
SPA_NAME_AUDIO_CONVERT,
node_name,
PW_SPA_NODE_FLAG_ACTIVATE | PW_SPA_NODE_FLAG_NO_REGISTER,
pw_properties_copy(props),
sizeof(struct node) + user_data_size);
if (node == NULL) {
res = -errno;
pw_log_error("can't load spa node: %m");
goto error;
}
n = pw_spa_node_get_user_data(node);
n->core = core;
n->node = node;
n->direction = direction;
n->props = props;
spa_list_init(&n->ports);
n->max_buffer_size = max_buffer_size;
if (user_data_size > 0)
n->user_data = SPA_MEMBER(n, sizeof(struct node), void);
pw_node_add_listener(node, &n->node_listener, &node_events, n);
return node;
error:
if (props)
pw_properties_free(props);
errno = -res;
return NULL;
}
void *pw_audio_dsp_get_user_data(struct pw_node *node)
{
struct node *n = pw_spa_node_get_user_data(node);
return n->user_data;
}

View file

@ -1,52 +0,0 @@
/* PipeWire
*
* Copyright © 2018 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef PIPEWIRE_AUDIO_DSP_H
#define PIPEWIRE_AUDIO_DSP_H
#include <pipewire/core.h>
#include <pipewire/node.h>
#ifdef __cplusplus
extern "C" {
#endif
#define AUDIO_DSP_USAGE "audio-dsp.direction=<enum spa_direction> " \
"audio-dsp.maxbuffer=<int> " \
"audio-dsp.name=<string> " \
PW_KEY_DEVICE_API"=<string> " \
"["PW_KEY_NODE_ID"=<int>]"
struct pw_node *
pw_audio_dsp_new(struct pw_core *core,
struct pw_properties *properties,
size_t user_data_size);
void *pw_audio_dsp_get_user_data(struct pw_node *node);
#ifdef __cplusplus
}
#endif
#endif /* PIPEWIRE_AUDIO_DSP_H */

View file

@ -1430,21 +1430,24 @@ uint32_t pw_node_get_free_port_id(struct pw_node *node, enum pw_direction direct
uint32_t n_ports, max_ports;
struct pw_map *portmap;
uint32_t port_id;
bool dynamic;
int res;
if (direction == PW_DIRECTION_INPUT) {
max_ports = node->info.max_input_ports;
n_ports = node->info.n_input_ports;
portmap = &node->input_port_map;
dynamic = SPA_FLAG_CHECK(node->spa_flags, SPA_NODE_FLAG_IN_DYNAMIC_PORTS);
} else {
max_ports = node->info.max_output_ports;
n_ports = node->info.n_output_ports;
portmap = &node->output_port_map;
dynamic = SPA_FLAG_CHECK(node->spa_flags, SPA_NODE_FLAG_OUT_DYNAMIC_PORTS);
}
pw_log_debug("node %p: direction %s n_ports:%u max_ports:%u",
node, pw_direction_as_string(direction), n_ports, max_ports);
if (n_ports >= max_ports) {
if (!dynamic || n_ports >= max_ports) {
res = -ENOSPC;
goto error;
}