diff --git a/spa/include/spa/param/audio/format-utils.h b/spa/include/spa/param/audio/format-utils.h index 4203447b8..0b5aab5a1 100644 --- a/spa/include/spa/param/audio/format-utils.h +++ b/spa/include/spa/param/audio/format-utils.h @@ -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_channels, "i", &info->channels, ":", 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 * diff --git a/spa/include/spa/param/audio/raw.h b/spa/include/spa/param/audio/raw.h index 1233374db..eebfbf941 100644 --- a/spa/include/spa/param/audio/raw.h +++ b/spa/include/spa/param/audio/raw.h @@ -140,7 +140,7 @@ struct spa_audio_info_raw { enum spa_audio_layout layout; /*< sample layout */ uint32_t rate; /*< sample rate */ 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__ } diff --git a/spa/plugins/alsa/alsa-utils.c b/spa/plugins/alsa/alsa-utils.c index 4f5011434..069e61f92 100644 --- a/spa/plugins/alsa/alsa-utils.c +++ b/spa/plugins/alsa/alsa-utils.c @@ -98,6 +98,50 @@ static snd_pcm_format_t spa_format_to_alsa(uint32_t format) 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 spa_alsa_enum_format(struct state *state, uint32_t *index, 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_format_mask_t *fmask; snd_pcm_access_mask_t *amask; + snd_pcm_chmap_query_t **maps; int err, i, j, dir; unsigned int min, max; uint8_t buffer[4096]; @@ -213,6 +258,19 @@ spa_alsa_enum_format(struct state *state, uint32_t *index, } 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); (*index)++; diff --git a/src/examples/media-session.c b/src/examples/media-session.c index 3d8041ac6..3c77ea71e 100644 --- a/src/examples/media-session.c +++ b/src/examples/media-session.c @@ -94,6 +94,8 @@ struct node { #define NODE_TYPE_DSP 2 #define NODE_TYPE_DEVICE 3 uint32_t type; + + struct spa_audio_info_raw format; }; struct port { @@ -101,6 +103,7 @@ struct port { struct spa_list l; enum pw_direction direction; + struct pw_port_info *info; struct node *node; struct spa_hook listener; @@ -284,6 +287,65 @@ handle_node(struct impl *impl, uint32_t id, uint32_t parent_id, 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 handle_port(struct impl *impl, uint32_t id, uint32_t parent_id, uint32_t type, 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->node = node; 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); 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); 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) { struct pw_properties *props; + struct node *node = sess->node; void *dsp; - if (sess->node->info->props == NULL) + if (node->info->props == NULL) 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.channels", "%d", 4); - pw_properties_setf(props, "audio-dsp.rate", "%d", DEFAULT_SAMPLERATE); + pw_properties_setf(props, "audio-dsp.channels", "%d", node->format.channels); + 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_log_debug(NAME" %p: making audio dsp for session %d", impl, sess->id); diff --git a/src/modules/module-audio-dsp.c b/src/modules/module-audio-dsp.c index ee9ec57ff..459b611b5 100644 --- a/src/modules/module-audio-dsp.c +++ b/src/modules/module-audio-dsp.c @@ -86,6 +86,7 @@ static void *create_object(void *_data, struct pw_client *client; struct pw_node *dsp; int res, channels, rate, maxbuffer; + uint64_t channelmask; const char *str; enum pw_direction direction; struct node_data *nd; @@ -106,6 +107,11 @@ static void *create_object(void *_data, 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) goto no_props; @@ -119,7 +125,7 @@ static void *create_object(void *_data, dsp = pw_audio_dsp_new(pw_module_get_core(d->module), properties, direction, - channels, rate, maxbuffer, + channels, channelmask, rate, maxbuffer, sizeof(struct node_data)); if (dsp == NULL) diff --git a/src/modules/module-media-session.c b/src/modules/module-media-session.c index 4c49f3e6a..9969ad9ab 100644 --- a/src/modules/module-media-session.c +++ b/src/modules/module-media-session.c @@ -620,6 +620,7 @@ static const struct pw_node_events dsp_events = { struct channel_data { struct impl *impl; uint32_t channels; + uint64_t channelmask; uint32_t rate; }; @@ -645,6 +646,7 @@ static int collect_audio_format(void *data, uint32_t id, if (info.channels > d->channels) { d->channels = info.channels; + d->channelmask = info.channel_mask; d->rate = info.rate; } 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, - uint32_t *channels, uint32_t *rate) + uint32_t *channels, uint64_t *channelmask, uint32_t *rate) { 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, 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; + *channelmask = data.channelmask; *rate = data.rate; return data.channels > 0 ? 0 : -1; @@ -679,6 +682,7 @@ static int on_global(void *data, struct pw_global *global) enum pw_direction direction; struct pw_port *node_port, *dsp_port; uint32_t id, channels, rate; + uint64_t channelmask; bool need_dsp; 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); 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; dsp = pw_audio_dsp_new(impl->core, properties, direction, channels, + channelmask, rate, MAX_QUANTUM_SIZE * sizeof(float), 0); diff --git a/src/modules/module-media-session/audio-dsp.c b/src/modules/module-media-session/audio-dsp.c index ae2a6e7cf..8a8f6f9f6 100644 --- a/src/modules/module-media-session/audio-dsp.c +++ b/src/modules/module-media-session/audio-dsp.c @@ -71,6 +71,7 @@ struct node { void *user_data; int channels; + uint64_t channelmask; int sample_rate; int max_buffer_size; }; @@ -141,10 +142,65 @@ static const struct pw_port_implementation port_implementation = { .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, const struct pw_properties *props, enum pw_direction direction, uint32_t channels, + uint64_t channelmask, uint32_t sample_rate, uint32_t max_buffer_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->channels = channels; + n->channelmask = channelmask; n->sample_rate = sample_rate; 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++) { struct port *p; struct pw_properties *props; + char channel_name[16]; + + make_channel_name(n, channel_name, i, channelmask); props = pw_properties_new( "port.dsp", "32 bit float mono audio", "port.physical", "1", "port.terminal", "1", NULL); - pw_properties_setf(props, "port.name", "%s_%d", + pw_properties_setf(props, "port.name", "%s_%s", direction == PW_DIRECTION_INPUT ? "playback" : "capture", - i + 1); - pw_properties_setf(props, "port.alias", "%s_pcm:%s:%s%d", + channel_name); + pw_properties_setf(props, "port.alias1", "%s_pcm:%s:%s%s", api, alias, direction == PW_DIRECTION_INPUT ? "in" : "out", - i + 1); + channel_name); port = pw_port_new(direction, i, diff --git a/src/modules/module-media-session/audio-dsp.h b/src/modules/module-media-session/audio-dsp.h index 4c77cc296..75034260c 100644 --- a/src/modules/module-media-session/audio-dsp.h +++ b/src/modules/module-media-session/audio-dsp.h @@ -32,6 +32,7 @@ pw_audio_dsp_new(struct pw_core *core, const struct pw_properties *properties, enum pw_direction direction, uint32_t channels, + uint64_t channelmask, uint32_t sample_rate, uint32_t max_buffer_size, size_t user_data_size); diff --git a/src/pipewire/interfaces.h b/src/pipewire/interfaces.h index ea8b02de3..7081f8d06 100644 --- a/src/pipewire/interfaces.h +++ b/src/pipewire/interfaces.h @@ -585,7 +585,7 @@ struct pw_port_proxy_methods { const struct spa_pod *filter); }; -/** Registry */ +/** Port params */ static inline void pw_port_proxy_enum_params(struct pw_port_proxy *port, uint32_t id, uint32_t index, uint32_t num, const struct spa_pod *filter)