mirror of
				https://gitlab.freedesktop.org/pipewire/pipewire.git
				synced 2025-11-03 09:01:54 -05:00 
			
		
		
		
	pulse: parse and fill up the array of formats
A sink and source can have an array of pa_format_info structures that contain the possible formats of the device. Parse them from the EnumFormat and return them when introspecting.
This commit is contained in:
		
							parent
							
								
									8406ad8a4e
								
							
						
					
					
						commit
						023281fd0b
					
				
					 6 changed files with 333 additions and 180 deletions
				
			
		| 
						 | 
				
			
			@ -847,3 +847,102 @@ pa_channel_position_mask_t pa_channel_map_mask(const pa_channel_map *map) {
 | 
			
		|||
 | 
			
		||||
    return r;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const uint32_t audio_channels[] = {
 | 
			
		||||
	[PA_CHANNEL_POSITION_MONO] = SPA_AUDIO_CHANNEL_MONO,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_FRONT_LEFT] = SPA_AUDIO_CHANNEL_FL,
 | 
			
		||||
	[PA_CHANNEL_POSITION_FRONT_RIGHT] = SPA_AUDIO_CHANNEL_FR,
 | 
			
		||||
	[PA_CHANNEL_POSITION_FRONT_CENTER] = SPA_AUDIO_CHANNEL_FC,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_REAR_CENTER] = SPA_AUDIO_CHANNEL_RC,
 | 
			
		||||
	[PA_CHANNEL_POSITION_REAR_LEFT] = SPA_AUDIO_CHANNEL_RL,
 | 
			
		||||
	[PA_CHANNEL_POSITION_REAR_RIGHT] = SPA_AUDIO_CHANNEL_RR,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_LFE] = SPA_AUDIO_CHANNEL_LFE,
 | 
			
		||||
	[PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = SPA_AUDIO_CHANNEL_FLC,
 | 
			
		||||
	[PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = SPA_AUDIO_CHANNEL_FRC,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_SIDE_LEFT] = SPA_AUDIO_CHANNEL_SL,
 | 
			
		||||
	[PA_CHANNEL_POSITION_SIDE_RIGHT] = SPA_AUDIO_CHANNEL_SR,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX0] = SPA_AUDIO_CHANNEL_CUSTOM_START + 1,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX1] = SPA_AUDIO_CHANNEL_CUSTOM_START + 2,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX2] = SPA_AUDIO_CHANNEL_CUSTOM_START + 3,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX3] = SPA_AUDIO_CHANNEL_CUSTOM_START + 4,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX4] = SPA_AUDIO_CHANNEL_CUSTOM_START + 5,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX5] = SPA_AUDIO_CHANNEL_CUSTOM_START + 6,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX6] = SPA_AUDIO_CHANNEL_CUSTOM_START + 7,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX7] = SPA_AUDIO_CHANNEL_CUSTOM_START + 8,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX8] = SPA_AUDIO_CHANNEL_CUSTOM_START + 9,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX9] = SPA_AUDIO_CHANNEL_CUSTOM_START + 10,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX10] = SPA_AUDIO_CHANNEL_CUSTOM_START + 11,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX11] = SPA_AUDIO_CHANNEL_CUSTOM_START + 12,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX12] = SPA_AUDIO_CHANNEL_CUSTOM_START + 13,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX13] = SPA_AUDIO_CHANNEL_CUSTOM_START + 14,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX14] = SPA_AUDIO_CHANNEL_CUSTOM_START + 15,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX15] = SPA_AUDIO_CHANNEL_CUSTOM_START + 16,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX16] = SPA_AUDIO_CHANNEL_CUSTOM_START + 17,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX17] = SPA_AUDIO_CHANNEL_CUSTOM_START + 18,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX18] = SPA_AUDIO_CHANNEL_CUSTOM_START + 19,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX19] = SPA_AUDIO_CHANNEL_CUSTOM_START + 20,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX20] = SPA_AUDIO_CHANNEL_CUSTOM_START + 21,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX21] = SPA_AUDIO_CHANNEL_CUSTOM_START + 22,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX22] = SPA_AUDIO_CHANNEL_CUSTOM_START + 23,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX23] = SPA_AUDIO_CHANNEL_CUSTOM_START + 24,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX24] = SPA_AUDIO_CHANNEL_CUSTOM_START + 25,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX25] = SPA_AUDIO_CHANNEL_CUSTOM_START + 26,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX26] = SPA_AUDIO_CHANNEL_CUSTOM_START + 27,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX27] = SPA_AUDIO_CHANNEL_CUSTOM_START + 28,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX28] = SPA_AUDIO_CHANNEL_CUSTOM_START + 29,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX29] = SPA_AUDIO_CHANNEL_CUSTOM_START + 30,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX30] = SPA_AUDIO_CHANNEL_CUSTOM_START + 31,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX31] = SPA_AUDIO_CHANNEL_CUSTOM_START + 32,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_TOP_CENTER] = SPA_AUDIO_CHANNEL_TC,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = SPA_AUDIO_CHANNEL_TFL,
 | 
			
		||||
	[PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = SPA_AUDIO_CHANNEL_TFR,
 | 
			
		||||
	[PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = SPA_AUDIO_CHANNEL_TFC,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_TOP_REAR_LEFT] = SPA_AUDIO_CHANNEL_TRL,
 | 
			
		||||
	[PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = SPA_AUDIO_CHANNEL_TRR,
 | 
			
		||||
	[PA_CHANNEL_POSITION_TOP_REAR_CENTER] = SPA_AUDIO_CHANNEL_TRC,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static inline uint32_t channel_pa2id(pa_channel_position_t channel)
 | 
			
		||||
{
 | 
			
		||||
	if (channel < 0 || (size_t)channel >= SPA_N_ELEMENTS(audio_channels))
 | 
			
		||||
		return SPA_AUDIO_CHANNEL_UNKNOWN;
 | 
			
		||||
	return audio_channels[channel];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline pa_channel_position_t channel_id2pa(uint32_t id, uint32_t *aux)
 | 
			
		||||
{
 | 
			
		||||
	size_t i;
 | 
			
		||||
	for (i = 0; i < SPA_N_ELEMENTS(audio_channels); i++) {
 | 
			
		||||
		if (id == audio_channels[i])
 | 
			
		||||
			return i;
 | 
			
		||||
	}
 | 
			
		||||
	return PA_CHANNEL_POSITION_AUX0 + (*aux)++;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void pw_channel_map_from_positions(pa_channel_map *map, uint32_t n_pos, const uint32_t *pos)
 | 
			
		||||
{
 | 
			
		||||
	uint32_t i, aux = 0;
 | 
			
		||||
 | 
			
		||||
	pa_channel_map_init(map);
 | 
			
		||||
	map->channels = n_pos;
 | 
			
		||||
	for (i = 0; i < n_pos; i++)
 | 
			
		||||
		map->map[i] = channel_id2pa(pos[i], &aux);
 | 
			
		||||
 | 
			
		||||
	if (!pa_channel_map_valid(map))
 | 
			
		||||
		pa_channel_map_init_extend(map, n_pos, PA_CHANNEL_MAP_DEFAULT);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void pw_channel_map_to_positions(const pa_channel_map *map, uint32_t *pos)
 | 
			
		||||
{
 | 
			
		||||
	int i;
 | 
			
		||||
	for (i = 0; i < map->channels; i++)
 | 
			
		||||
		pos[i] = channel_pa2id(map->map[i]);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -940,6 +940,13 @@ struct global_info device_info = {
 | 
			
		|||
	.sync = device_sync,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void clear_node_formats(struct global *g)
 | 
			
		||||
{
 | 
			
		||||
	pa_format_info *f;
 | 
			
		||||
	pw_array_for_each(f, &g->node_info.formats)
 | 
			
		||||
		pa_format_info_free(f);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void node_event_info(void *object, const struct pw_node_info *info)
 | 
			
		||||
{
 | 
			
		||||
	struct global *g = object;
 | 
			
		||||
| 
						 | 
				
			
			@ -964,6 +971,9 @@ static void node_event_info(void *object, const struct pw_node_info *info)
 | 
			
		|||
				continue;
 | 
			
		||||
 | 
			
		||||
			switch (info->params[i].id) {
 | 
			
		||||
			case SPA_PARAM_EnumFormat:
 | 
			
		||||
				clear_node_formats(g);
 | 
			
		||||
				/* fallthrough */
 | 
			
		||||
			case SPA_PARAM_Props:
 | 
			
		||||
			case SPA_PARAM_Format:
 | 
			
		||||
				pw_device_enum_params((struct pw_device*)g->proxy,
 | 
			
		||||
| 
						 | 
				
			
			@ -989,6 +999,13 @@ static void node_event_param(void *object, int seq,
 | 
			
		|||
		if (!SPA_FLAG_IS_SET(g->node_info.flags, NODE_FLAG_DEVICE_VOLUME | NODE_FLAG_DEVICE_MUTE))
 | 
			
		||||
			parse_props(g, param, false);
 | 
			
		||||
		break;
 | 
			
		||||
	case SPA_PARAM_EnumFormat:
 | 
			
		||||
	{
 | 
			
		||||
		pa_format_info *f = pa_format_info_from_param(param);
 | 
			
		||||
		if (f)
 | 
			
		||||
			pw_array_add_ptr(&g->node_info.formats, f);
 | 
			
		||||
		break;
 | 
			
		||||
	}
 | 
			
		||||
	case SPA_PARAM_Format:
 | 
			
		||||
		pa_format_parse_param(param, &g->node_info.sample_spec, &g->node_info.channel_map);
 | 
			
		||||
		break;
 | 
			
		||||
| 
						 | 
				
			
			@ -1006,6 +1023,7 @@ static const struct pw_node_events node_events = {
 | 
			
		|||
static void node_destroy(void *data)
 | 
			
		||||
{
 | 
			
		||||
	struct global *global = data;
 | 
			
		||||
	clear_node_formats(global);
 | 
			
		||||
	if (global->info)
 | 
			
		||||
		pw_node_info_free(global->info);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1281,6 +1299,7 @@ static int set_mask(pa_context *c, struct global *g)
 | 
			
		|||
		g->node_info.volume_step = 1.0f / (PA_VOLUME_NORM+1);
 | 
			
		||||
		g->node_info.active_port = SPA_ID_INVALID;
 | 
			
		||||
		g->node_info.available_port = SPA_PARAM_AVAILABILITY_unknown;
 | 
			
		||||
		pw_array_init(&g->node_info.formats, sizeof(void*) * 4);
 | 
			
		||||
	} else if (strcmp(g->type, PW_TYPE_INTERFACE_Port) == 0) {
 | 
			
		||||
		if (g->props == NULL)
 | 
			
		||||
			return 0;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,118 +26,6 @@
 | 
			
		|||
 | 
			
		||||
#include "internal.h"
 | 
			
		||||
 | 
			
		||||
static const uint32_t audio_formats[] = {
 | 
			
		||||
	[PA_SAMPLE_U8] = SPA_AUDIO_FORMAT_U8,
 | 
			
		||||
	[PA_SAMPLE_ALAW] = SPA_AUDIO_FORMAT_UNKNOWN,
 | 
			
		||||
	[PA_SAMPLE_ULAW] = SPA_AUDIO_FORMAT_UNKNOWN,
 | 
			
		||||
	[PA_SAMPLE_S16NE] = SPA_AUDIO_FORMAT_S16,
 | 
			
		||||
	[PA_SAMPLE_S16RE] = SPA_AUDIO_FORMAT_S16_OE,
 | 
			
		||||
	[PA_SAMPLE_FLOAT32NE] = SPA_AUDIO_FORMAT_F32,
 | 
			
		||||
	[PA_SAMPLE_FLOAT32RE] = SPA_AUDIO_FORMAT_F32_OE,
 | 
			
		||||
	[PA_SAMPLE_S32NE] = SPA_AUDIO_FORMAT_S32,
 | 
			
		||||
	[PA_SAMPLE_S32RE] = SPA_AUDIO_FORMAT_S32_OE,
 | 
			
		||||
	[PA_SAMPLE_S24NE] = SPA_AUDIO_FORMAT_S24,
 | 
			
		||||
	[PA_SAMPLE_S24RE] = SPA_AUDIO_FORMAT_S24_OE,
 | 
			
		||||
	[PA_SAMPLE_S24_32NE] = SPA_AUDIO_FORMAT_S24_32,
 | 
			
		||||
	[PA_SAMPLE_S24_32RE] = SPA_AUDIO_FORMAT_S24_32_OE,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static inline uint32_t format_pa2id(pa_sample_format_t format)
 | 
			
		||||
{
 | 
			
		||||
	if (format < 0 || (size_t)format >= SPA_N_ELEMENTS(audio_formats))
 | 
			
		||||
		return SPA_AUDIO_FORMAT_UNKNOWN;
 | 
			
		||||
	return audio_formats[format];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline pa_sample_format_t format_id2pa(uint32_t id)
 | 
			
		||||
{
 | 
			
		||||
	size_t i;
 | 
			
		||||
	for (i = 0; i < SPA_N_ELEMENTS(audio_formats); i++) {
 | 
			
		||||
		if (id == audio_formats[i])
 | 
			
		||||
			return i;
 | 
			
		||||
	}
 | 
			
		||||
	return PA_SAMPLE_INVALID;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const uint32_t audio_channels[] = {
 | 
			
		||||
	[PA_CHANNEL_POSITION_MONO] = SPA_AUDIO_CHANNEL_MONO,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_FRONT_LEFT] = SPA_AUDIO_CHANNEL_FL,
 | 
			
		||||
	[PA_CHANNEL_POSITION_FRONT_RIGHT] = SPA_AUDIO_CHANNEL_FR,
 | 
			
		||||
	[PA_CHANNEL_POSITION_FRONT_CENTER] = SPA_AUDIO_CHANNEL_FC,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_REAR_CENTER] = SPA_AUDIO_CHANNEL_RC,
 | 
			
		||||
	[PA_CHANNEL_POSITION_REAR_LEFT] = SPA_AUDIO_CHANNEL_RL,
 | 
			
		||||
	[PA_CHANNEL_POSITION_REAR_RIGHT] = SPA_AUDIO_CHANNEL_RR,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_LFE] = SPA_AUDIO_CHANNEL_LFE,
 | 
			
		||||
	[PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = SPA_AUDIO_CHANNEL_FLC,
 | 
			
		||||
	[PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = SPA_AUDIO_CHANNEL_FRC,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_SIDE_LEFT] = SPA_AUDIO_CHANNEL_SL,
 | 
			
		||||
	[PA_CHANNEL_POSITION_SIDE_RIGHT] = SPA_AUDIO_CHANNEL_SR,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX0] = SPA_AUDIO_CHANNEL_CUSTOM_START + 1,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX1] = SPA_AUDIO_CHANNEL_CUSTOM_START + 2,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX2] = SPA_AUDIO_CHANNEL_CUSTOM_START + 3,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX3] = SPA_AUDIO_CHANNEL_CUSTOM_START + 4,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX4] = SPA_AUDIO_CHANNEL_CUSTOM_START + 5,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX5] = SPA_AUDIO_CHANNEL_CUSTOM_START + 6,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX6] = SPA_AUDIO_CHANNEL_CUSTOM_START + 7,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX7] = SPA_AUDIO_CHANNEL_CUSTOM_START + 8,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX8] = SPA_AUDIO_CHANNEL_CUSTOM_START + 9,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX9] = SPA_AUDIO_CHANNEL_CUSTOM_START + 10,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX10] = SPA_AUDIO_CHANNEL_CUSTOM_START + 11,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX11] = SPA_AUDIO_CHANNEL_CUSTOM_START + 12,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX12] = SPA_AUDIO_CHANNEL_CUSTOM_START + 13,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX13] = SPA_AUDIO_CHANNEL_CUSTOM_START + 14,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX14] = SPA_AUDIO_CHANNEL_CUSTOM_START + 15,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX15] = SPA_AUDIO_CHANNEL_CUSTOM_START + 16,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX16] = SPA_AUDIO_CHANNEL_CUSTOM_START + 17,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX17] = SPA_AUDIO_CHANNEL_CUSTOM_START + 18,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX18] = SPA_AUDIO_CHANNEL_CUSTOM_START + 19,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX19] = SPA_AUDIO_CHANNEL_CUSTOM_START + 20,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX20] = SPA_AUDIO_CHANNEL_CUSTOM_START + 21,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX21] = SPA_AUDIO_CHANNEL_CUSTOM_START + 22,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX22] = SPA_AUDIO_CHANNEL_CUSTOM_START + 23,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX23] = SPA_AUDIO_CHANNEL_CUSTOM_START + 24,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX24] = SPA_AUDIO_CHANNEL_CUSTOM_START + 25,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX25] = SPA_AUDIO_CHANNEL_CUSTOM_START + 26,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX26] = SPA_AUDIO_CHANNEL_CUSTOM_START + 27,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX27] = SPA_AUDIO_CHANNEL_CUSTOM_START + 28,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX28] = SPA_AUDIO_CHANNEL_CUSTOM_START + 29,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX29] = SPA_AUDIO_CHANNEL_CUSTOM_START + 30,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX30] = SPA_AUDIO_CHANNEL_CUSTOM_START + 31,
 | 
			
		||||
	[PA_CHANNEL_POSITION_AUX31] = SPA_AUDIO_CHANNEL_CUSTOM_START + 32,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_TOP_CENTER] = SPA_AUDIO_CHANNEL_TC,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = SPA_AUDIO_CHANNEL_TFL,
 | 
			
		||||
	[PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = SPA_AUDIO_CHANNEL_TFR,
 | 
			
		||||
	[PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = SPA_AUDIO_CHANNEL_TFC,
 | 
			
		||||
 | 
			
		||||
	[PA_CHANNEL_POSITION_TOP_REAR_LEFT] = SPA_AUDIO_CHANNEL_TRL,
 | 
			
		||||
	[PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = SPA_AUDIO_CHANNEL_TRR,
 | 
			
		||||
	[PA_CHANNEL_POSITION_TOP_REAR_CENTER] = SPA_AUDIO_CHANNEL_TRC,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static inline uint32_t channel_pa2id(pa_channel_position_t channel)
 | 
			
		||||
{
 | 
			
		||||
	if (channel < 0 || (size_t)channel >= SPA_N_ELEMENTS(audio_channels))
 | 
			
		||||
		return SPA_AUDIO_CHANNEL_UNKNOWN;
 | 
			
		||||
	return audio_channels[channel];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline pa_channel_position_t channel_id2pa(uint32_t id, uint32_t *aux)
 | 
			
		||||
{
 | 
			
		||||
	size_t i;
 | 
			
		||||
	for (i = 0; i < SPA_N_ELEMENTS(audio_channels); i++) {
 | 
			
		||||
		if (id == audio_channels[i])
 | 
			
		||||
			return i;
 | 
			
		||||
	}
 | 
			
		||||
	return PA_CHANNEL_POSITION_AUX0 + (*aux)++;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
SPA_EXPORT
 | 
			
		||||
int pa_format_info_get_sample_format(const pa_format_info *f, pa_sample_format_t *sf) {
 | 
			
		||||
    int r;
 | 
			
		||||
| 
						 | 
				
			
			@ -358,55 +246,3 @@ int pa_format_info_to_sample_spec_fake(const pa_format_info *f, pa_sample_spec *
 | 
			
		|||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int pa_format_parse_param(const struct spa_pod *param, pa_sample_spec *spec, pa_channel_map *map)
 | 
			
		||||
{
 | 
			
		||||
	struct spa_audio_info info = { 0 };
 | 
			
		||||
	uint32_t i, aux;
 | 
			
		||||
 | 
			
		||||
	if (param == NULL)
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
 | 
			
		||||
	spa_format_parse(param, &info.media_type, &info.media_subtype);
 | 
			
		||||
 | 
			
		||||
	if (info.media_type != SPA_MEDIA_TYPE_audio ||
 | 
			
		||||
	    info.media_subtype != SPA_MEDIA_SUBTYPE_raw ||
 | 
			
		||||
	    spa_format_audio_raw_parse(param, &info.info.raw) < 0 ||
 | 
			
		||||
	    !SPA_AUDIO_FORMAT_IS_INTERLEAVED(info.info.raw.format)) {
 | 
			
		||||
		return -ENOTSUP;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	spec->format = format_id2pa(info.info.raw.format);
 | 
			
		||||
	if (spec->format == PA_SAMPLE_INVALID)
 | 
			
		||||
		return -ENOTSUP;
 | 
			
		||||
 | 
			
		||||
	spec->rate = info.info.raw.rate;
 | 
			
		||||
	spec->channels = info.info.raw.channels;
 | 
			
		||||
 | 
			
		||||
	aux = 0;
 | 
			
		||||
	pa_channel_map_init(map);
 | 
			
		||||
	map->channels = info.info.raw.channels;
 | 
			
		||||
	for (i = 0; i < info.info.raw.channels; i++)
 | 
			
		||||
		map->map[i] = channel_id2pa(info.info.raw.position[i], &aux);
 | 
			
		||||
 | 
			
		||||
	if (!pa_channel_map_valid(map))
 | 
			
		||||
		pa_channel_map_init_extend(map, info.info.raw.channels, PA_CHANNEL_MAP_DEFAULT);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const struct spa_pod *pa_format_build_param(struct spa_pod_builder *b,
 | 
			
		||||
		uint32_t id, pa_sample_spec *spec, pa_channel_map *map)
 | 
			
		||||
{
 | 
			
		||||
	struct spa_audio_info_raw info;
 | 
			
		||||
 | 
			
		||||
	info = SPA_AUDIO_INFO_RAW_INIT( .format = format_pa2id(spec->format),
 | 
			
		||||
		                .channels = spec->channels,
 | 
			
		||||
		                .rate = spec->rate);
 | 
			
		||||
	if (map) {
 | 
			
		||||
		int i;
 | 
			
		||||
		for (i = 0; i < map->channels; i++)
 | 
			
		||||
			info.position[i] = channel_pa2id(map->map[i]);
 | 
			
		||||
	}
 | 
			
		||||
	return spa_format_audio_raw_build(b, id, &info);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -36,6 +36,39 @@
 | 
			
		|||
 | 
			
		||||
static int pa_format_info_prop_compatible(const char *one, const char *two);
 | 
			
		||||
 | 
			
		||||
static const uint32_t audio_formats[] = {
 | 
			
		||||
	[PA_SAMPLE_U8] = SPA_AUDIO_FORMAT_U8,
 | 
			
		||||
	[PA_SAMPLE_ALAW] = SPA_AUDIO_FORMAT_UNKNOWN,
 | 
			
		||||
	[PA_SAMPLE_ULAW] = SPA_AUDIO_FORMAT_UNKNOWN,
 | 
			
		||||
	[PA_SAMPLE_S16NE] = SPA_AUDIO_FORMAT_S16,
 | 
			
		||||
	[PA_SAMPLE_S16RE] = SPA_AUDIO_FORMAT_S16_OE,
 | 
			
		||||
	[PA_SAMPLE_FLOAT32NE] = SPA_AUDIO_FORMAT_F32,
 | 
			
		||||
	[PA_SAMPLE_FLOAT32RE] = SPA_AUDIO_FORMAT_F32_OE,
 | 
			
		||||
	[PA_SAMPLE_S32NE] = SPA_AUDIO_FORMAT_S32,
 | 
			
		||||
	[PA_SAMPLE_S32RE] = SPA_AUDIO_FORMAT_S32_OE,
 | 
			
		||||
	[PA_SAMPLE_S24NE] = SPA_AUDIO_FORMAT_S24,
 | 
			
		||||
	[PA_SAMPLE_S24RE] = SPA_AUDIO_FORMAT_S24_OE,
 | 
			
		||||
	[PA_SAMPLE_S24_32NE] = SPA_AUDIO_FORMAT_S24_32,
 | 
			
		||||
	[PA_SAMPLE_S24_32RE] = SPA_AUDIO_FORMAT_S24_32_OE,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static inline uint32_t format_pa2id(pa_sample_format_t format)
 | 
			
		||||
{
 | 
			
		||||
	if (format < 0 || (size_t)format >= SPA_N_ELEMENTS(audio_formats))
 | 
			
		||||
		return SPA_AUDIO_FORMAT_UNKNOWN;
 | 
			
		||||
	return audio_formats[format];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline pa_sample_format_t format_id2pa(uint32_t id)
 | 
			
		||||
{
 | 
			
		||||
	size_t i;
 | 
			
		||||
	for (i = 0; i < SPA_N_ELEMENTS(audio_formats); i++) {
 | 
			
		||||
		if (id == audio_formats[i])
 | 
			
		||||
			return i;
 | 
			
		||||
	}
 | 
			
		||||
	return PA_SAMPLE_INVALID;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const char* const _encoding_str_table[]= {
 | 
			
		||||
    [PA_ENCODING_PCM] = "pcm",
 | 
			
		||||
    [PA_ENCODING_AC3_IEC61937] = "ac3-iec61937",
 | 
			
		||||
| 
						 | 
				
			
			@ -705,3 +738,175 @@ out:
 | 
			
		|||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void format_info_fill_int(pa_format_info *f, const struct spa_pod *val, const char *key)
 | 
			
		||||
{
 | 
			
		||||
	const struct spa_pod *vals;
 | 
			
		||||
	int *int_vals;
 | 
			
		||||
	uint32_t *uvals, i, n_vals, choice;
 | 
			
		||||
	vals = spa_pod_get_values(val, &n_vals, &choice);
 | 
			
		||||
	uvals = SPA_POD_BODY(vals);
 | 
			
		||||
 | 
			
		||||
	int_vals = alloca(n_vals * sizeof(int));
 | 
			
		||||
	for (i = 0; i < n_vals; i++)
 | 
			
		||||
		int_vals[i] = uvals[i];
 | 
			
		||||
 | 
			
		||||
	if (n_vals == 1)
 | 
			
		||||
		choice = SPA_CHOICE_None;
 | 
			
		||||
 | 
			
		||||
	switch (choice) {
 | 
			
		||||
	case SPA_CHOICE_None:
 | 
			
		||||
		if (n_vals > 0)
 | 
			
		||||
			pa_format_info_set_prop_int(f,
 | 
			
		||||
					key, int_vals[0]);
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	case SPA_CHOICE_Enum:
 | 
			
		||||
		if (n_vals > 1)
 | 
			
		||||
			pa_format_info_set_prop_int_array(f,
 | 
			
		||||
					key, &int_vals[1], n_vals-1);
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	case SPA_CHOICE_Range:
 | 
			
		||||
		if (n_vals > 2)
 | 
			
		||||
			pa_format_info_set_prop_int_range(f,
 | 
			
		||||
					key, int_vals[1], int_vals[2]);
 | 
			
		||||
		break;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void format_info_fill_format(pa_format_info *f, const struct spa_pod *val, const char *key)
 | 
			
		||||
{
 | 
			
		||||
	const struct spa_pod *vals;
 | 
			
		||||
	const char **svals;
 | 
			
		||||
	uint32_t *uvals, i, j, n_vals, choice;
 | 
			
		||||
	vals = spa_pod_get_values(val, &n_vals, &choice);
 | 
			
		||||
	uvals = SPA_POD_BODY(vals);
 | 
			
		||||
 | 
			
		||||
	svals = alloca(n_vals * sizeof(char *));
 | 
			
		||||
	for (i = 0, j = 0; i < n_vals; i++) {
 | 
			
		||||
		pa_sample_format_t fmt = format_id2pa(uvals[i]);
 | 
			
		||||
		const char *s = pa_sample_format_to_string(fmt);
 | 
			
		||||
		if (s)
 | 
			
		||||
			svals[j++] = s;
 | 
			
		||||
	}
 | 
			
		||||
	n_vals = j;
 | 
			
		||||
	if (n_vals == 1)
 | 
			
		||||
		choice = SPA_CHOICE_None;
 | 
			
		||||
 | 
			
		||||
	switch (choice) {
 | 
			
		||||
	case SPA_CHOICE_None:
 | 
			
		||||
		if (n_vals > 0)
 | 
			
		||||
			pa_format_info_set_prop_string(f, key, svals[0]);
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	case SPA_CHOICE_Enum:
 | 
			
		||||
		if (n_vals > 1)
 | 
			
		||||
			pa_format_info_set_prop_string_array(f,
 | 
			
		||||
					key, &svals[1], n_vals-1);
 | 
			
		||||
		break;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pa_format_info* pa_format_info_from_param(const struct spa_pod *param)
 | 
			
		||||
{
 | 
			
		||||
	pa_format_info *f = pa_format_info_new();
 | 
			
		||||
	struct spa_audio_info info = { 0 };
 | 
			
		||||
	const struct spa_pod_object *obj;
 | 
			
		||||
	const struct spa_pod_prop *p;
 | 
			
		||||
 | 
			
		||||
	if (param == NULL ||
 | 
			
		||||
	    !spa_pod_is_object_type(param, SPA_TYPE_OBJECT_Format))
 | 
			
		||||
		goto error;
 | 
			
		||||
 | 
			
		||||
	spa_format_parse(param, &info.media_type, &info.media_subtype);
 | 
			
		||||
	if (info.media_type != SPA_MEDIA_TYPE_audio)
 | 
			
		||||
		goto error;
 | 
			
		||||
 | 
			
		||||
	switch (info.media_subtype) {
 | 
			
		||||
	case SPA_MEDIA_SUBTYPE_raw:
 | 
			
		||||
		f->encoding = PA_ENCODING_PCM;
 | 
			
		||||
		break;
 | 
			
		||||
	default:
 | 
			
		||||
		goto error;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	obj = (const struct spa_pod_object*)param;
 | 
			
		||||
	SPA_POD_OBJECT_FOREACH(obj, p) {
 | 
			
		||||
		switch (p->key) {
 | 
			
		||||
		case SPA_FORMAT_AUDIO_format:
 | 
			
		||||
			format_info_fill_format(f, &p->value, PA_PROP_FORMAT_SAMPLE_FORMAT);
 | 
			
		||||
			break;
 | 
			
		||||
		case SPA_FORMAT_AUDIO_rate:
 | 
			
		||||
			format_info_fill_int(f, &p->value, PA_PROP_FORMAT_RATE);
 | 
			
		||||
			break;
 | 
			
		||||
		case SPA_FORMAT_AUDIO_channels:
 | 
			
		||||
			format_info_fill_int(f, &p->value, PA_PROP_FORMAT_CHANNELS);
 | 
			
		||||
			break;
 | 
			
		||||
		case SPA_FORMAT_AUDIO_position:
 | 
			
		||||
		{
 | 
			
		||||
			uint32_t pos[SPA_AUDIO_MAX_CHANNELS], n_pos;
 | 
			
		||||
 | 
			
		||||
			n_pos = spa_pod_copy_array(&p->value, SPA_TYPE_Id, pos, SPA_AUDIO_MAX_CHANNELS);
 | 
			
		||||
			if (n_pos > 0) {
 | 
			
		||||
				char cm[PA_CHANNEL_MAP_SNPRINT_MAX];
 | 
			
		||||
				pa_channel_map map;
 | 
			
		||||
				pw_channel_map_from_positions(&map, n_pos, pos);
 | 
			
		||||
				pa_channel_map_snprint(cm, sizeof(cm), &map);
 | 
			
		||||
				pa_format_info_set_prop_string(f, PA_PROP_FORMAT_CHANNEL_MAP, cm);
 | 
			
		||||
			}
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
		default:
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
out:
 | 
			
		||||
	return f;
 | 
			
		||||
error:
 | 
			
		||||
	pa_format_info_free(f);
 | 
			
		||||
	goto out;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int pa_format_parse_param(const struct spa_pod *param, pa_sample_spec *spec, pa_channel_map *map)
 | 
			
		||||
{
 | 
			
		||||
	struct spa_audio_info info = { 0 };
 | 
			
		||||
 | 
			
		||||
	if (param == NULL)
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
 | 
			
		||||
	spa_format_parse(param, &info.media_type, &info.media_subtype);
 | 
			
		||||
 | 
			
		||||
	if (info.media_type != SPA_MEDIA_TYPE_audio ||
 | 
			
		||||
	    info.media_subtype != SPA_MEDIA_SUBTYPE_raw ||
 | 
			
		||||
	    spa_format_audio_raw_parse(param, &info.info.raw) < 0 ||
 | 
			
		||||
	    !SPA_AUDIO_FORMAT_IS_INTERLEAVED(info.info.raw.format)) {
 | 
			
		||||
		return -ENOTSUP;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	spec->format = format_id2pa(info.info.raw.format);
 | 
			
		||||
	if (spec->format == PA_SAMPLE_INVALID)
 | 
			
		||||
		return -ENOTSUP;
 | 
			
		||||
 | 
			
		||||
	spec->rate = info.info.raw.rate;
 | 
			
		||||
	spec->channels = info.info.raw.channels;
 | 
			
		||||
 | 
			
		||||
	pw_channel_map_from_positions(map, info.info.raw.channels, info.info.raw.position);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const struct spa_pod *pa_format_build_param(struct spa_pod_builder *b,
 | 
			
		||||
		uint32_t id, pa_sample_spec *spec, pa_channel_map *map)
 | 
			
		||||
{
 | 
			
		||||
	struct spa_audio_info_raw info;
 | 
			
		||||
 | 
			
		||||
	info = SPA_AUDIO_INFO_RAW_INIT( .format = format_pa2id(spec->format),
 | 
			
		||||
		                .channels = spec->channels,
 | 
			
		||||
		                .rate = spec->rate);
 | 
			
		||||
	if (map)
 | 
			
		||||
		pw_channel_map_to_positions(map, info.position);
 | 
			
		||||
 | 
			
		||||
	return spa_format_audio_raw_build(b, id, &info);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -301,6 +301,7 @@ struct global {
 | 
			
		|||
			uint32_t active_port;
 | 
			
		||||
			enum spa_param_availability available_port;
 | 
			
		||||
			uint32_t device_index;
 | 
			
		||||
			struct pw_array formats;
 | 
			
		||||
		} node_info;
 | 
			
		||||
		struct {
 | 
			
		||||
			uint32_t node_id;
 | 
			
		||||
| 
						 | 
				
			
			@ -516,11 +517,16 @@ int pa_metadata_update(struct global *global, uint32_t subject, const char *key,
 | 
			
		|||
int pa_metadata_get(struct global *global, uint32_t subject, const char *key,
 | 
			
		||||
                        const char **type, const char **value);
 | 
			
		||||
 | 
			
		||||
void pw_channel_map_from_positions(pa_channel_map *map, uint32_t n_pos, const uint32_t *pos);
 | 
			
		||||
void pw_channel_map_to_positions(const pa_channel_map *map, uint32_t *pos);
 | 
			
		||||
 | 
			
		||||
int pa_format_parse_param(const struct spa_pod *param,
 | 
			
		||||
		pa_sample_spec *spec, pa_channel_map *map);
 | 
			
		||||
const struct spa_pod *pa_format_build_param(struct spa_pod_builder *b,
 | 
			
		||||
		uint32_t id, pa_sample_spec *spec, pa_channel_map *map);
 | 
			
		||||
 | 
			
		||||
pa_format_info* pa_format_info_from_param(const struct spa_pod *param);
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -102,8 +102,6 @@ static int sink_callback(pa_context *c, struct global *g, struct sink_data *d)
 | 
			
		|||
	uint32_t n, j;
 | 
			
		||||
	char monitor_name[1024];
 | 
			
		||||
	pa_sink_info i;
 | 
			
		||||
	pa_format_info ii[1];
 | 
			
		||||
	pa_format_info *ip[1];
 | 
			
		||||
 | 
			
		||||
	spa_zero(i);
 | 
			
		||||
	if (info->props && (str = spa_dict_lookup(info->props, PW_KEY_NODE_NAME)))
 | 
			
		||||
| 
						 | 
				
			
			@ -184,14 +182,10 @@ static int sink_callback(pa_context *c, struct global *g, struct sink_data *d)
 | 
			
		|||
		else
 | 
			
		||||
			i.ports[j] = NULL;
 | 
			
		||||
	}
 | 
			
		||||
	i.n_formats = 1;
 | 
			
		||||
	ii[0].encoding = PA_ENCODING_PCM;
 | 
			
		||||
	ii[0].plist = pa_proplist_new();
 | 
			
		||||
	ip[0] = ii;
 | 
			
		||||
	i.formats = ip;
 | 
			
		||||
	i.n_formats = pw_array_get_len(&g->node_info.formats, pa_format_info *);
 | 
			
		||||
	i.formats = g->node_info.formats.data;
 | 
			
		||||
	d->cb(c, &i, 0, d->userdata);
 | 
			
		||||
	pa_proplist_free(i.proplist);
 | 
			
		||||
	pa_proplist_free(ii[0].plist);
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -817,8 +811,6 @@ static int source_callback(pa_context *c, struct global *g, struct source_data *
 | 
			
		|||
	const char *str;
 | 
			
		||||
	uint32_t n, j;
 | 
			
		||||
	pa_source_info i;
 | 
			
		||||
	pa_format_info ii[1];
 | 
			
		||||
	pa_format_info *ip[1];
 | 
			
		||||
	enum pa_source_flags flags;
 | 
			
		||||
	bool monitor;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -917,14 +909,10 @@ static int source_callback(pa_context *c, struct global *g, struct source_data *
 | 
			
		|||
		else
 | 
			
		||||
			i.ports[j] = NULL;
 | 
			
		||||
	}
 | 
			
		||||
	i.n_formats = 1;
 | 
			
		||||
	ii[0].encoding = PA_ENCODING_PCM;
 | 
			
		||||
	ii[0].plist = pa_proplist_new();
 | 
			
		||||
	ip[0] = ii;
 | 
			
		||||
	i.formats = ip;
 | 
			
		||||
	i.n_formats = pw_array_get_len(&g->node_info.formats, pa_format_info *);
 | 
			
		||||
	i.formats = g->node_info.formats.data;
 | 
			
		||||
	d->cb(c, &i, 0, d->userdata);
 | 
			
		||||
	pa_proplist_free(i.proplist);
 | 
			
		||||
	pa_proplist_free(ii[0].plist);
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue