Pass channelmask around and use it to name ports

Use the channel name in the port names of the dsp
This commit is contained in:
Wim Taymans 2018-09-11 18:09:45 +02:00
parent 7aae01fe15
commit fb3379e587
9 changed files with 217 additions and 15 deletions

View file

@ -39,7 +39,7 @@ spa_format_audio_raw_parse(const struct spa_pod *format, struct spa_audio_info_r
":", SPA_FORMAT_AUDIO_rate, "i", &info->rate, ":", SPA_FORMAT_AUDIO_rate, "i", &info->rate,
":", SPA_FORMAT_AUDIO_channels, "i", &info->channels, ":", SPA_FORMAT_AUDIO_channels, "i", &info->channels,
":", SPA_FORMAT_AUDIO_flags, "?i", &info->flags, ":", SPA_FORMAT_AUDIO_flags, "?i", &info->flags,
":", SPA_FORMAT_AUDIO_channelMask, "?i", &info->channel_mask, NULL); ":", SPA_FORMAT_AUDIO_channelMask, "?l", &info->channel_mask, NULL);
} }
static inline struct spa_pod * static inline struct spa_pod *

View file

@ -140,7 +140,7 @@ struct spa_audio_info_raw {
enum spa_audio_layout layout; /*< sample layout */ enum spa_audio_layout layout; /*< sample layout */
uint32_t rate; /*< sample rate */ uint32_t rate; /*< sample rate */
uint32_t channels; /*< number of channels */ uint32_t channels; /*< number of channels */
uint32_t channel_mask; /*< channel mask */ uint64_t channel_mask; /*< channel mask */
}; };
#define SPA_AUDIO_INFO_RAW_INIT(...) (struct spa_audio_info_raw) { __VA_ARGS__ } #define SPA_AUDIO_INFO_RAW_INIT(...) (struct spa_audio_info_raw) { __VA_ARGS__ }

View file

@ -98,6 +98,50 @@ static snd_pcm_format_t spa_format_to_alsa(uint32_t format)
return SND_PCM_FORMAT_UNKNOWN; return SND_PCM_FORMAT_UNKNOWN;
} }
static uint64_t map_to_mask(snd_pcm_chmap_t* map)
{
uint64_t result = 0;
int i;
for (i = 0; i < map->channels; i++) {
switch (map->pos[i]) {
case SND_CHMAP_UNKNOWN:
case SND_CHMAP_NA: /* N/A, silent */
case SND_CHMAP_MONO: /* mono stream */
break;
case SND_CHMAP_FL: /* front left */
case SND_CHMAP_FR: /* front right */
case SND_CHMAP_RL: /* rear left */
case SND_CHMAP_RR: /* rear right */
case SND_CHMAP_FC: /* front center */
case SND_CHMAP_LFE: /* LFE */
case SND_CHMAP_SL: /* side left */
case SND_CHMAP_SR: /* side right */
case SND_CHMAP_RC: /* rear center */
case SND_CHMAP_FLC: /* front left center */
case SND_CHMAP_FRC: /* front right center */
case SND_CHMAP_RLC: /* rear left center */
case SND_CHMAP_RRC: /* rear right center */
case SND_CHMAP_FLW: /* front left wide */
case SND_CHMAP_FRW: /* front right wide */
case SND_CHMAP_FLH: /* front left high */
case SND_CHMAP_FCH: /* front center high */
case SND_CHMAP_FRH: /* front right high */
case SND_CHMAP_TC: /* top center */
case SND_CHMAP_TFL: /* top front left */
case SND_CHMAP_TFR: /* top front right */
case SND_CHMAP_TFC: /* top front center */
case SND_CHMAP_TRL: /* top rear left */
case SND_CHMAP_TRR: /* top rear right */
case SND_CHMAP_TRC: /* top rear center */
result |= (1LL << (map->pos[i] - 3));
break;
}
}
return result;
}
int int
spa_alsa_enum_format(struct state *state, uint32_t *index, spa_alsa_enum_format(struct state *state, uint32_t *index,
const struct spa_pod *filter, const struct spa_pod *filter,
@ -108,6 +152,7 @@ spa_alsa_enum_format(struct state *state, uint32_t *index,
snd_pcm_hw_params_t *params; snd_pcm_hw_params_t *params;
snd_pcm_format_mask_t *fmask; snd_pcm_format_mask_t *fmask;
snd_pcm_access_mask_t *amask; snd_pcm_access_mask_t *amask;
snd_pcm_chmap_query_t **maps;
int err, i, j, dir; int err, i, j, dir;
unsigned int min, max; unsigned int min, max;
uint8_t buffer[4096]; uint8_t buffer[4096];
@ -213,6 +258,19 @@ spa_alsa_enum_format(struct state *state, uint32_t *index,
} }
spa_pod_builder_pop(&b); spa_pod_builder_pop(&b);
if ((maps = snd_pcm_query_chmaps(hndl)) != NULL) {
spa_pod_builder_prop(&b, SPA_FORMAT_AUDIO_channelMask, 0);
spa_pod_builder_push_choice(&b, SPA_CHOICE_None, 0);
for (i = 0; maps[i]; i++) {
uint64_t mask = map_to_mask(&maps[i]->map);
spa_pod_builder_long(&b, mask);
}
spa_pod_builder_pop(&b);
snd_pcm_free_chmaps(maps);
}
fmt = spa_pod_builder_pop(&b); fmt = spa_pod_builder_pop(&b);
(*index)++; (*index)++;

View file

@ -94,6 +94,8 @@ struct node {
#define NODE_TYPE_DSP 2 #define NODE_TYPE_DSP 2
#define NODE_TYPE_DEVICE 3 #define NODE_TYPE_DEVICE 3
uint32_t type; uint32_t type;
struct spa_audio_info_raw format;
}; };
struct port { struct port {
@ -101,6 +103,7 @@ struct port {
struct spa_list l; struct spa_list l;
enum pw_direction direction; enum pw_direction direction;
struct pw_port_info *info;
struct node *node; struct node *node;
struct spa_hook listener; struct spa_hook listener;
@ -284,6 +287,65 @@ handle_node(struct impl *impl, uint32_t id, uint32_t parent_id,
return 1; return 1;
} }
static void port_event_info(void *object, struct pw_port_info *info)
{
struct port *p = object;
pw_log_debug(NAME" %p: info for port %d", p->obj.impl, p->obj.id);
p->info = pw_port_info_update(p->info, info);
}
static void port_event_param(void *object,
uint32_t id, uint32_t index, uint32_t next,
const struct spa_pod *param)
{
struct port *p = object;
struct node *node = p->node;
uint32_t media_type, media_subtype;
struct spa_audio_info_raw info;
pw_log_debug(NAME" %p: param for port %d", p->obj.impl, p->obj.id);
if (id != SPA_PARAM_EnumFormat)
return;
if (spa_format_parse(param, &media_type, &media_subtype) < 0)
return;
if (media_type != SPA_MEDIA_TYPE_audio ||
media_subtype != SPA_MEDIA_SUBTYPE_raw)
return;
spa_pod_fixate((struct spa_pod*)param);
if (spa_format_audio_raw_parse(param, &info) < 0)
return;
if (info.channels > node->format.channels)
node->format = info;
}
static const struct pw_port_proxy_events port_events = {
PW_VERSION_PORT_PROXY_EVENTS,
.info = port_event_info,
.param = port_event_param,
};
static void port_proxy_destroy(void *data)
{
struct port *p = data;
pw_log_debug(NAME " %p: proxy destroy port %d", p->obj.impl, p->obj.id);
spa_list_remove(&p->l);
if (p->info)
pw_port_info_free(p->info);
}
static const struct pw_proxy_events port_proxy_events = {
PW_VERSION_PROXY_EVENTS,
.destroy = port_proxy_destroy,
};
static int static int
handle_port(struct impl *impl, uint32_t id, uint32_t parent_id, uint32_t type, handle_port(struct impl *impl, uint32_t id, uint32_t parent_id, uint32_t type,
const struct spa_dict *props) const struct spa_dict *props)
@ -311,10 +373,18 @@ handle_port(struct impl *impl, uint32_t id, uint32_t parent_id, uint32_t type,
port->obj.proxy = p; port->obj.proxy = p;
port->node = node; port->node = node;
port->direction = strcmp(str, "out") ? PW_DIRECTION_OUTPUT : PW_DIRECTION_INPUT; port->direction = strcmp(str, "out") ? PW_DIRECTION_OUTPUT : PW_DIRECTION_INPUT;
pw_proxy_add_listener(p, &port->obj.listener, &port_proxy_events, port);
pw_proxy_add_proxy_listener(p, &port->listener, &port_events, port);
add_object(impl, &port->obj); add_object(impl, &port->obj);
spa_list_append(&node->port_list, &port->l); spa_list_append(&node->port_list, &port->l);
if (node->type == NODE_TYPE_DEVICE) {
pw_port_proxy_enum_params((struct pw_port_proxy*)p,
SPA_PARAM_EnumFormat,
0, -1, NULL);
}
pw_log_debug(NAME" %p: new port %d for node %d", impl, id, parent_id); pw_log_debug(NAME" %p: new port %d for node %d", impl, id, parent_id);
return 0; return 0;
@ -648,15 +718,17 @@ static void rescan_session(struct impl *impl, struct session *sess)
} }
if (sess->need_dsp && sess->dsp == NULL && !sess->dsp_pending) { if (sess->need_dsp && sess->dsp == NULL && !sess->dsp_pending) {
struct pw_properties *props; struct pw_properties *props;
struct node *node = sess->node;
void *dsp; void *dsp;
if (sess->node->info->props == NULL) if (node->info->props == NULL)
return; return;
props = pw_properties_new_dict(sess->node->info->props); props = pw_properties_new_dict(node->info->props);
pw_properties_setf(props, "audio-dsp.direction", "%d", sess->direction); pw_properties_setf(props, "audio-dsp.direction", "%d", sess->direction);
pw_properties_setf(props, "audio-dsp.channels", "%d", 4); pw_properties_setf(props, "audio-dsp.channels", "%d", node->format.channels);
pw_properties_setf(props, "audio-dsp.rate", "%d", DEFAULT_SAMPLERATE); pw_properties_setf(props, "audio-dsp.channelmask", "%"PRIu64, node->format.channel_mask);
pw_properties_setf(props, "audio-dsp.rate", "%d", node->format.rate);
pw_properties_setf(props, "audio-dsp.maxbuffer", "%ld", MAX_QUANTUM_SIZE * sizeof(float)); pw_properties_setf(props, "audio-dsp.maxbuffer", "%ld", MAX_QUANTUM_SIZE * sizeof(float));
pw_log_debug(NAME" %p: making audio dsp for session %d", impl, sess->id); pw_log_debug(NAME" %p: making audio dsp for session %d", impl, sess->id);

View file

@ -86,6 +86,7 @@ static void *create_object(void *_data,
struct pw_client *client; struct pw_client *client;
struct pw_node *dsp; struct pw_node *dsp;
int res, channels, rate, maxbuffer; int res, channels, rate, maxbuffer;
uint64_t channelmask;
const char *str; const char *str;
enum pw_direction direction; enum pw_direction direction;
struct node_data *nd; struct node_data *nd;
@ -106,6 +107,11 @@ static void *create_object(void *_data,
channels = pw_properties_parse_int(str); channels = pw_properties_parse_int(str);
if ((str = pw_properties_get(properties, "audio-dsp.channelmask")) == NULL)
goto no_props;
channelmask = pw_properties_parse_uint64(str);
if ((str = pw_properties_get(properties, "audio-dsp.rate")) == NULL) if ((str = pw_properties_get(properties, "audio-dsp.rate")) == NULL)
goto no_props; goto no_props;
@ -119,7 +125,7 @@ static void *create_object(void *_data,
dsp = pw_audio_dsp_new(pw_module_get_core(d->module), dsp = pw_audio_dsp_new(pw_module_get_core(d->module),
properties, properties,
direction, direction,
channels, rate, maxbuffer, channels, channelmask, rate, maxbuffer,
sizeof(struct node_data)); sizeof(struct node_data));
if (dsp == NULL) if (dsp == NULL)

View file

@ -620,6 +620,7 @@ static const struct pw_node_events dsp_events = {
struct channel_data { struct channel_data {
struct impl *impl; struct impl *impl;
uint32_t channels; uint32_t channels;
uint64_t channelmask;
uint32_t rate; uint32_t rate;
}; };
@ -645,6 +646,7 @@ static int collect_audio_format(void *data, uint32_t id,
if (info.channels > d->channels) { if (info.channels > d->channels) {
d->channels = info.channels; d->channels = info.channels;
d->channelmask = info.channel_mask;
d->rate = info.rate; d->rate = info.rate;
} }
return 0; return 0;
@ -652,7 +654,7 @@ static int collect_audio_format(void *data, uint32_t id,
static int find_port_format(struct impl *impl, struct pw_port *port, static int find_port_format(struct impl *impl, struct pw_port *port,
uint32_t *channels, uint32_t *rate) uint32_t *channels, uint64_t *channelmask, uint32_t *rate)
{ {
struct channel_data data = { impl, 0, 0 }; struct channel_data data = { impl, 0, 0 };
@ -661,9 +663,10 @@ static int find_port_format(struct impl *impl, struct pw_port *port,
0, 0, NULL, 0, 0, NULL,
collect_audio_format, &data); collect_audio_format, &data);
pw_log_debug("port channels %d rate %d", data.channels, data.rate); pw_log_debug("port channels %d %"PRIu64" rate %d", data.channels, data.channelmask, data.rate);
*channels = data.channels; *channels = data.channels;
*channelmask = data.channelmask;
*rate = data.rate; *rate = data.rate;
return data.channels > 0 ? 0 : -1; return data.channels > 0 ? 0 : -1;
@ -679,6 +682,7 @@ static int on_global(void *data, struct pw_global *global)
enum pw_direction direction; enum pw_direction direction;
struct pw_port *node_port, *dsp_port; struct pw_port *node_port, *dsp_port;
uint32_t id, channels, rate; uint32_t id, channels, rate;
uint64_t channelmask;
bool need_dsp; bool need_dsp;
uint64_t plugged; uint64_t plugged;
@ -739,13 +743,14 @@ static int on_global(void *data, struct pw_global *global)
pw_node_add_listener(node, &sess->node_listener, &node_events, sess); pw_node_add_listener(node, &sess->node_listener, &node_events, sess);
if (need_dsp) { if (need_dsp) {
if (find_port_format(impl, node_port, &channels, &rate) < 0) if (find_port_format(impl, node_port, &channels, &channelmask, &rate) < 0)
return 0; return 0;
dsp = pw_audio_dsp_new(impl->core, dsp = pw_audio_dsp_new(impl->core,
properties, properties,
direction, direction,
channels, channels,
channelmask,
rate, rate,
MAX_QUANTUM_SIZE * sizeof(float), MAX_QUANTUM_SIZE * sizeof(float),
0); 0);

View file

@ -71,6 +71,7 @@ struct node {
void *user_data; void *user_data;
int channels; int channels;
uint64_t channelmask;
int sample_rate; int sample_rate;
int max_buffer_size; int max_buffer_size;
}; };
@ -141,10 +142,65 @@ static const struct pw_port_implementation port_implementation = {
.use_buffers = port_use_buffers, .use_buffers = port_use_buffers,
}; };
static const char *chmap_names[] =
{
"FL",
"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;
}
struct pw_node *pw_audio_dsp_new(struct pw_core *core, struct pw_node *pw_audio_dsp_new(struct pw_core *core,
const struct pw_properties *props, const struct pw_properties *props,
enum pw_direction direction, enum pw_direction direction,
uint32_t channels, uint32_t channels,
uint64_t channelmask,
uint32_t sample_rate, uint32_t sample_rate,
uint32_t max_buffer_size, uint32_t max_buffer_size,
size_t user_data_size) size_t user_data_size)
@ -204,6 +260,7 @@ struct pw_node *pw_audio_dsp_new(struct pw_core *core,
n->node = node; n->node = node;
n->channels = channels; n->channels = channels;
n->channelmask = channelmask;
n->sample_rate = sample_rate; n->sample_rate = sample_rate;
n->max_buffer_size = max_buffer_size; n->max_buffer_size = max_buffer_size;
@ -219,20 +276,23 @@ struct pw_node *pw_audio_dsp_new(struct pw_core *core,
for (i = 0; i < n->channels; i++) { for (i = 0; i < n->channels; i++) {
struct port *p; struct port *p;
struct pw_properties *props; struct pw_properties *props;
char channel_name[16];
make_channel_name(n, channel_name, i, channelmask);
props = pw_properties_new( props = pw_properties_new(
"port.dsp", "32 bit float mono audio", "port.dsp", "32 bit float mono audio",
"port.physical", "1", "port.physical", "1",
"port.terminal", "1", "port.terminal", "1",
NULL); NULL);
pw_properties_setf(props, "port.name", "%s_%d", pw_properties_setf(props, "port.name", "%s_%s",
direction == PW_DIRECTION_INPUT ? "playback" : "capture", direction == PW_DIRECTION_INPUT ? "playback" : "capture",
i + 1); channel_name);
pw_properties_setf(props, "port.alias", "%s_pcm:%s:%s%d", pw_properties_setf(props, "port.alias1", "%s_pcm:%s:%s%s",
api, api,
alias, alias,
direction == PW_DIRECTION_INPUT ? "in" : "out", direction == PW_DIRECTION_INPUT ? "in" : "out",
i + 1); channel_name);
port = pw_port_new(direction, port = pw_port_new(direction,
i, i,

View file

@ -32,6 +32,7 @@ pw_audio_dsp_new(struct pw_core *core,
const struct pw_properties *properties, const struct pw_properties *properties,
enum pw_direction direction, enum pw_direction direction,
uint32_t channels, uint32_t channels,
uint64_t channelmask,
uint32_t sample_rate, uint32_t sample_rate,
uint32_t max_buffer_size, uint32_t max_buffer_size,
size_t user_data_size); size_t user_data_size);

View file

@ -585,7 +585,7 @@ struct pw_port_proxy_methods {
const struct spa_pod *filter); const struct spa_pod *filter);
}; };
/** Registry */ /** Port params */
static inline void static inline void
pw_port_proxy_enum_params(struct pw_port_proxy *port, uint32_t id, uint32_t index, pw_port_proxy_enum_params(struct pw_port_proxy *port, uint32_t id, uint32_t index,
uint32_t num, const struct spa_pod *filter) uint32_t num, const struct spa_pod *filter)