| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <sched.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include <getopt.h>
 | 
					
						
							|  |  |  | #include <sys/time.h>
 | 
					
						
							|  |  |  | #include <math.h>
 | 
					
						
							| 
									
										
										
										
											2017-03-28 19:29:08 +02:00
										 |  |  | #include <limits.h>
 | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-13 17:17:23 +02:00
										 |  |  | #include <spa/pod/filter.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-10 15:36:26 +02:00
										 |  |  | #include <spa/utils/string.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | #include <spa/utils/result.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-06 15:21:40 +02:00
										 |  |  | #include <spa/support/system.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:27:36 +01:00
										 |  |  | #include <spa/utils/keys.h>
 | 
					
						
							| 
									
										
										
										
											2023-11-07 13:01:52 -05:00
										 |  |  | #include <spa/node/keys.h>
 | 
					
						
							|  |  |  | #include <spa/monitor/device.h>
 | 
					
						
							| 
									
										
										
										
											2017-06-20 12:30:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-23 20:51:34 +02:00
										 |  |  | #include "alsa-pcm.h"
 | 
					
						
							| 
									
										
										
										
											2016-09-19 09:16:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-30 17:36:11 +02:00
										 |  |  | static struct spa_list cards = SPA_LIST_INIT(&cards); | 
					
						
							| 
									
										
										
										
											2023-09-18 09:40:52 +02:00
										 |  |  | static struct spa_list states = SPA_LIST_INIT(&states); | 
					
						
							| 
									
										
										
										
											2025-07-04 14:51:15 +02:00
										 |  |  | #define SPA_ALSA_DLL_BW_MIN 0.001
 | 
					
						
							| 
									
										
										
										
											2021-08-30 17:36:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | static struct card *find_card(uint32_t index) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct card *c; | 
					
						
							|  |  |  | 	spa_list_for_each(c, &cards, link) { | 
					
						
							|  |  |  | 		if (c->index == index) { | 
					
						
							|  |  |  | 			c->ref++; | 
					
						
							|  |  |  | 			return c; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 13:27:09 +02:00
										 |  |  | static struct card *ensure_card(uint32_t index, bool ucm, bool ucm_split) | 
					
						
							| 
									
										
										
										
											2021-06-02 13:23:08 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-08-30 17:36:11 +02:00
										 |  |  | 	struct card *c; | 
					
						
							| 
									
										
										
										
											2024-12-07 13:27:09 +02:00
										 |  |  | 	char card_name[128]; | 
					
						
							| 
									
										
										
										
											2021-09-02 12:33:44 +02:00
										 |  |  | 	const char *alibpref = NULL; | 
					
						
							| 
									
										
										
										
											2021-06-02 13:23:08 +02:00
										 |  |  | 	int err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 10:42:58 -05:00
										 |  |  | 	if (index == SPA_ID_INVALID) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-30 17:36:11 +02:00
										 |  |  | 	if ((c = find_card(index)) != NULL) | 
					
						
							|  |  |  | 		return c; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c = calloc(1, sizeof(*c)); | 
					
						
							|  |  |  | 	c->ref = 1; | 
					
						
							|  |  |  | 	c->index = index; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 11:28:41 +01:00
										 |  |  | 	if (ucm) { | 
					
						
							| 
									
										
										
										
											2024-12-07 13:27:09 +02:00
										 |  |  | 		const char *split_prefix = ucm_split ? "<<<SplitPCM=1>>>" : ""; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		snprintf(card_name, sizeof(card_name), "%shw:%i", split_prefix, index); | 
					
						
							| 
									
										
										
										
											2021-08-30 17:36:11 +02:00
										 |  |  | 		err = snd_use_case_mgr_open(&c->ucm, card_name); | 
					
						
							| 
									
										
										
										
											2021-11-23 11:28:41 +01:00
										 |  |  | 		if (err < 0) { | 
					
						
							|  |  |  | 			char *name; | 
					
						
							|  |  |  | 			err = snd_card_get_name(index, &name); | 
					
						
							|  |  |  | 			if (err < 0) | 
					
						
							|  |  |  | 				goto error; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 13:27:09 +02:00
										 |  |  | 			snprintf(card_name, sizeof(card_name), "%s%s", split_prefix, name); | 
					
						
							| 
									
										
										
										
											2021-11-23 11:28:41 +01:00
										 |  |  | 			free(name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			err = snd_use_case_mgr_open(&c->ucm, card_name); | 
					
						
							|  |  |  | 			if (err < 0) | 
					
						
							|  |  |  | 				goto error; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if ((snd_use_case_get(c->ucm, "_alibpref", &alibpref) != 0)) | 
					
						
							|  |  |  | 			alibpref = NULL; | 
					
						
							|  |  |  | 		c->ucm_prefix = (char*)alibpref; | 
					
						
							| 
									
										
										
										
											2021-08-30 17:36:11 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	spa_list_append(&cards, &c->link); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return c; | 
					
						
							| 
									
										
										
										
											2021-11-17 12:55:51 +01:00
										 |  |  | error: | 
					
						
							|  |  |  | 	free(c); | 
					
						
							|  |  |  | 	errno = -err; | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											2021-08-30 17:36:11 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 19:29:05 +01:00
										 |  |  | static void release_card(struct card *c) | 
					
						
							| 
									
										
										
										
											2021-08-30 17:36:11 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-29 10:42:58 -05:00
										 |  |  | 	if (!c) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 19:29:05 +01:00
										 |  |  | 	spa_assert(c->ref > 0); | 
					
						
							| 
									
										
										
										
											2021-08-30 17:36:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (--c->ref > 0) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_list_remove(&c->link); | 
					
						
							| 
									
										
										
										
											2021-11-23 11:28:41 +01:00
										 |  |  | 	if (c->ucm) { | 
					
						
							|  |  |  | 		free(c->ucm_prefix); | 
					
						
							|  |  |  | 		snd_use_case_mgr_close(c->ucm); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-08-30 17:36:11 +02:00
										 |  |  | 	free(c); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-28 09:55:24 +00:00
										 |  |  | #define CHECK(s,msg,...) if ((err = (s)) < 0) { spa_log_error(state->log, msg ": %s", ##__VA_ARGS__, snd_strerror(err)); return err; }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int write_bind_ctl_param(struct state *state, const char *name, const char *param) { | 
					
						
							|  |  |  | 	int err; | 
					
						
							|  |  |  | 	unsigned int count, idx; | 
					
						
							|  |  |  | 	char _name[1024]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (unsigned int i = 0; i < state->num_bind_ctls; i++) { | 
					
						
							|  |  |  | 		snd_ctl_elem_info_t *info = state->bound_ctls[i].info; | 
					
						
							|  |  |  | 		bool changed = false; | 
					
						
							|  |  |  | 		int type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(!state->bound_ctls[i].value || !info) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		snprintf(_name, sizeof(_name), "api.alsa.bind-ctl.%s", | 
					
						
							|  |  |  | 				snd_ctl_elem_info_get_name(info)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!spa_streq(name, _name)) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		type = snd_ctl_elem_info_get_type(info); | 
					
						
							|  |  |  | 		count = snd_ctl_elem_info_get_count(info); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		switch (type) { | 
					
						
							|  |  |  | 		case SND_CTL_ELEM_TYPE_BOOLEAN: { | 
					
						
							|  |  |  | 				bool b = spa_atob(param); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				for (idx = 0; idx < count; idx++) | 
					
						
							|  |  |  | 					snd_ctl_elem_value_set_boolean(state->bound_ctls[i].value, idx, b); | 
					
						
							|  |  |  | 				changed = true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case SND_CTL_ELEM_TYPE_INTEGER: { | 
					
						
							|  |  |  | 				long l = (long) atoi(param); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				for (idx = 0; idx < count; idx++) | 
					
						
							|  |  |  | 					snd_ctl_elem_value_set_integer(state->bound_ctls[i].value, idx, l); | 
					
						
							|  |  |  | 				changed = true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			spa_log_warn(state->log, "%s ctl '%s' not supported", | 
					
						
							|  |  |  | 					snd_ctl_elem_type_name(snd_ctl_elem_info_get_type(info)), | 
					
						
							|  |  |  | 					snd_ctl_elem_info_get_name(info)); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(changed) | 
					
						
							|  |  |  | 			CHECK(snd_ctl_elem_write(state->ctl, state->bound_ctls[i].value), "snd_ctl_elem_write"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-01 12:54:44 +01:00
										 |  |  | static int alsa_set_param(struct state *state, const char *k, const char *s) | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-30 18:19:08 +01:00
										 |  |  | 	int fmt_change = 0; | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	if (spa_streq(k, SPA_KEY_AUDIO_CHANNELS)) { | 
					
						
							|  |  |  | 		state->default_channels = atoi(s); | 
					
						
							| 
									
										
										
										
											2025-10-20 15:33:17 +02:00
										 |  |  | 		if (state->default_channels > MAX_CHANNELS) { | 
					
						
							| 
									
										
										
										
											2025-05-08 18:14:02 +02:00
										 |  |  | 			spa_log_warn(state->log, "%p: %s: %s > %d, clamping", | 
					
						
							| 
									
										
										
										
											2025-10-20 15:33:17 +02:00
										 |  |  | 					state, k, s, MAX_CHANNELS); | 
					
						
							|  |  |  | 			state->default_channels = MAX_CHANNELS; | 
					
						
							| 
									
										
										
										
											2025-05-08 18:14:02 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-11-30 18:19:08 +01:00
										 |  |  | 		fmt_change++; | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	} else if (spa_streq(k, SPA_KEY_AUDIO_RATE)) { | 
					
						
							|  |  |  | 		state->default_rate = atoi(s); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:19:08 +01:00
										 |  |  | 		fmt_change++; | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	} else if (spa_streq(k, SPA_KEY_AUDIO_FORMAT)) { | 
					
						
							| 
									
										
										
										
											2024-09-18 09:54:34 +02:00
										 |  |  | 		state->default_format = spa_type_audio_format_from_short_name(s); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:19:08 +01:00
										 |  |  | 		fmt_change++; | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	} else if (spa_streq(k, SPA_KEY_AUDIO_POSITION)) { | 
					
						
							|  |  |  | 		spa_alsa_parse_position(&state->default_pos, s, strlen(s)); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:19:08 +01:00
										 |  |  | 		fmt_change++; | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 	} else if (spa_streq(k, SPA_KEY_AUDIO_ALLOWED_RATES)) { | 
					
						
							|  |  |  | 		state->n_allowed_rates = spa_alsa_parse_rates(state->allowed_rates, | 
					
						
							|  |  |  | 				MAX_RATES, s, strlen(s)); | 
					
						
							|  |  |  | 		fmt_change++; | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	} else if (spa_streq(k, "iec958.codecs")) { | 
					
						
							|  |  |  | 		spa_alsa_parse_iec958_codecs(&state->iec958_codecs, s, strlen(s)); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:19:08 +01:00
										 |  |  | 		fmt_change++; | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	} else if (spa_streq(k, "api.alsa.period-size")) { | 
					
						
							|  |  |  | 		state->default_period_size = atoi(s); | 
					
						
							| 
									
										
										
										
											2021-12-17 16:09:52 +01:00
										 |  |  | 	} else if (spa_streq(k, "api.alsa.period-num")) { | 
					
						
							|  |  |  | 		state->default_period_num = atoi(s); | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	} else if (spa_streq(k, "api.alsa.headroom")) { | 
					
						
							|  |  |  | 		state->default_headroom = atoi(s); | 
					
						
							|  |  |  | 	} else if (spa_streq(k, "api.alsa.start-delay")) { | 
					
						
							|  |  |  | 		state->default_start_delay = atoi(s); | 
					
						
							|  |  |  | 	} else if (spa_streq(k, "api.alsa.disable-mmap")) { | 
					
						
							|  |  |  | 		state->disable_mmap = spa_atob(s); | 
					
						
							|  |  |  | 	} else if (spa_streq(k, "api.alsa.disable-batch")) { | 
					
						
							|  |  |  | 		state->disable_batch = spa_atob(s); | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 	} else if (spa_streq(k, "api.alsa.disable-tsched")) { | 
					
						
							|  |  |  | 		state->disable_tsched = spa_atob(s); | 
					
						
							| 
									
										
										
										
											2025-07-04 14:51:15 +02:00
										 |  |  | 	} else if (spa_streq(k, "api.alsa.dll-bandwidth-max")) { | 
					
						
							|  |  |  | 		state->dll_bw_max = SPA_CLAMPD(spa_strtod(s, NULL), | 
					
						
							|  |  |  | 					       SPA_ALSA_DLL_BW_MIN, SPA_DLL_BW_MAX); | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	} else if (spa_streq(k, "api.alsa.use-chmap")) { | 
					
						
							|  |  |  | 		state->props.use_chmap = spa_atob(s); | 
					
						
							|  |  |  | 	} else if (spa_streq(k, "api.alsa.multi-rate")) { | 
					
						
							|  |  |  | 		state->multi_rate = spa_atob(s); | 
					
						
							| 
									
										
										
										
											2023-06-29 16:23:39 +02:00
										 |  |  | 	} else if (spa_streq(k, "api.alsa.htimestamp")) { | 
					
						
							|  |  |  | 		state->htimestamp = spa_atob(s); | 
					
						
							| 
									
										
										
										
											2024-02-20 08:47:03 +01:00
										 |  |  | 	} else if (spa_streq(k, "api.alsa.htimestamp.max-errors")) { | 
					
						
							|  |  |  | 		state->htimestamp_max_errors = atoi(s); | 
					
						
							| 
									
										
										
										
											2023-10-09 10:10:46 +02:00
										 |  |  | 	} else if (spa_streq(k, "api.alsa.auto-link")) { | 
					
						
							|  |  |  | 		state->auto_link = spa_atob(s); | 
					
						
							| 
									
										
										
										
											2024-11-27 16:23:24 +01:00
										 |  |  | 	} else if (spa_streq(k, "api.alsa.dsd-lsb")) { | 
					
						
							|  |  |  | 		state->dsd_lsb = spa_atob(s); | 
					
						
							| 
									
										
										
										
											2022-01-18 11:46:08 +01:00
										 |  |  | 	} else if (spa_streq(k, "latency.internal.rate")) { | 
					
						
							|  |  |  | 		state->process_latency.rate = atoi(s); | 
					
						
							|  |  |  | 	} else if (spa_streq(k, "latency.internal.ns")) { | 
					
						
							|  |  |  | 		state->process_latency.ns = atoi(s); | 
					
						
							| 
									
										
										
										
											2021-12-10 11:04:38 +01:00
										 |  |  | 	} else if (spa_streq(k, "clock.name")) { | 
					
						
							|  |  |  | 		spa_scnprintf(state->clock_name, | 
					
						
							|  |  |  | 				sizeof(state->clock_name), "%s", s); | 
					
						
							| 
									
										
										
										
											2024-06-28 09:55:24 +00:00
										 |  |  | 	} else if (spa_strstartswith(k,  "api.alsa.bind-ctl.")) { | 
					
						
							|  |  |  | 		write_bind_ctl_param(state, k, s); | 
					
						
							|  |  |  | 		fmt_change++; | 
					
						
							| 
									
										
										
										
											2024-12-07 13:27:09 +02:00
										 |  |  | 	} else if (spa_streq(k, SPA_KEY_MEDIA_CLASS)) { | 
					
						
							|  |  |  | 		spa_scnprintf(state->props.media_class, sizeof(state->props.media_class), "%s", s); | 
					
						
							|  |  |  | 	} else if (spa_streq(k, "api.alsa.split.parent")) { | 
					
						
							|  |  |  | 		state->is_split_parent = true; | 
					
						
							| 
									
										
										
										
											2021-11-30 18:19:08 +01:00
										 |  |  | 	} else | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (fmt_change > 0) { | 
					
						
							|  |  |  | 		state->port_info.change_mask |= SPA_PORT_CHANGE_MASK_PARAMS; | 
					
						
							|  |  |  | 		state->port_params[PORT_EnumFormat].user++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int position_to_string(struct channel_map *map, char *val, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-10-24 17:00:11 +02:00
										 |  |  | 	uint32_t i; | 
					
						
							| 
									
										
										
										
											2025-10-22 13:04:53 +02:00
										 |  |  | 	char pos[8]; | 
					
						
							| 
									
										
										
										
											2025-10-24 17:00:11 +02:00
										 |  |  | 	struct spa_strbuf b; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_strbuf_init(&b, val, len); | 
					
						
							|  |  |  | 	spa_strbuf_append(&b, "["); | 
					
						
							| 
									
										
										
										
											2025-10-22 12:47:00 +02:00
										 |  |  | 	for (i = 0; i < map->n_pos; i++) { | 
					
						
							| 
									
										
										
										
											2025-10-24 17:00:11 +02:00
										 |  |  | 		spa_strbuf_append(&b, "%s%s", i == 0 ? " " : ", ", | 
					
						
							| 
									
										
										
										
											2025-10-22 13:04:53 +02:00
										 |  |  | 				spa_type_audio_channel_make_short_name(map->pos[i], | 
					
						
							|  |  |  | 					pos, sizeof(pos), "UNK")); | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-10-24 17:00:11 +02:00
										 |  |  | 	if (spa_strbuf_append(&b, " ]") < 2) | 
					
						
							|  |  |  | 		return -ENOSPC; | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | static int uint32_array_to_string(uint32_t *vals, uint32_t n_vals, char *val, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-10-24 17:00:11 +02:00
										 |  |  | 	uint32_t i; | 
					
						
							|  |  |  | 	struct spa_strbuf b; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_strbuf_init(&b, val, len); | 
					
						
							|  |  |  | 	spa_strbuf_append(&b, "["); | 
					
						
							|  |  |  | 	for (i = 0; i < n_vals; i++) | 
					
						
							|  |  |  | 		spa_strbuf_append(&b, "%s%d", i == 0 ? " " : ", ", vals[i]); | 
					
						
							|  |  |  | 	if (spa_strbuf_append(&b, " ]") < 2) | 
					
						
							|  |  |  | 		return -ENOSPC; | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | static struct spa_pod *enum_bind_ctl_propinfo(struct state *state, uint32_t idx, struct spa_pod_builder *b) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char param_name[1024]; | 
					
						
							|  |  |  | 	char param_desc[1024]; | 
					
						
							|  |  |  | 	snd_ctl_elem_info_t *info = state->bound_ctls[idx].info; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!info) { | 
					
						
							|  |  |  | 		// This will end iteration early, so print a warning
 | 
					
						
							|  |  |  | 		spa_log_warn(state->log, "Don't have prop info for bind ctl, bailing"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	snprintf(param_name, sizeof(param_name), "api.alsa.bind-ctl.%s", | 
					
						
							|  |  |  | 			snd_ctl_elem_info_get_name(info)); | 
					
						
							|  |  |  | 	snprintf(param_desc, sizeof(param_desc), "Value of ALSA control '%s'", | 
					
						
							|  |  |  | 			snd_ctl_elem_info_get_name(info)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// We don't have meaningful default values
 | 
					
						
							|  |  |  | 	switch (snd_ctl_elem_info_get_type(info)) { | 
					
						
							|  |  |  | 		case SND_CTL_ELEM_TYPE_BOOLEAN: | 
					
						
							|  |  |  | 			return spa_pod_builder_add_object(b, | 
					
						
							|  |  |  | 					SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							|  |  |  | 					SPA_PROP_INFO_name, SPA_POD_String(param_name), | 
					
						
							|  |  |  | 					SPA_PROP_INFO_description, SPA_POD_String(param_desc), | 
					
						
							|  |  |  | 					SPA_PROP_INFO_type, SPA_POD_Bool(false), | 
					
						
							|  |  |  | 					SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case SND_CTL_ELEM_TYPE_INTEGER: | 
					
						
							|  |  |  | 			return spa_pod_builder_add_object(b, | 
					
						
							|  |  |  | 					SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							|  |  |  | 					SPA_PROP_INFO_name, SPA_POD_String(param_name), | 
					
						
							|  |  |  | 					SPA_PROP_INFO_description, SPA_POD_String(param_desc), | 
					
						
							|  |  |  | 					SPA_PROP_INFO_type, SPA_POD_Int(0), | 
					
						
							|  |  |  | 					SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case SND_CTL_ELEM_TYPE_INTEGER64: | 
					
						
							|  |  |  | 			return spa_pod_builder_add_object(b, | 
					
						
							|  |  |  | 					SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							|  |  |  | 					SPA_PROP_INFO_name, SPA_POD_String(param_name), | 
					
						
							|  |  |  | 					SPA_PROP_INFO_description, SPA_POD_String(param_desc), | 
					
						
							|  |  |  | 					SPA_PROP_INFO_type, SPA_POD_Long(0), | 
					
						
							|  |  |  | 					SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case SND_CTL_ELEM_TYPE_ENUMERATED: | 
					
						
							|  |  |  | 			return spa_pod_builder_add_object(b, | 
					
						
							|  |  |  | 					SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							|  |  |  | 					SPA_PROP_INFO_name, SPA_POD_String(param_name), | 
					
						
							|  |  |  | 					SPA_PROP_INFO_description, SPA_POD_String(param_desc), | 
					
						
							|  |  |  | 					SPA_PROP_INFO_type, SPA_POD_Int(0), | 
					
						
							|  |  |  | 					SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			// FIXME: we can probably support bytes but the length seems unknown in the API
 | 
					
						
							|  |  |  | 			spa_log_warn(state->log, "%s ctl '%s' not supported", | 
					
						
							|  |  |  | 					snd_ctl_elem_type_name(snd_ctl_elem_info_get_type(info)), | 
					
						
							|  |  |  | 					snd_ctl_elem_info_get_name(info)); | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | struct spa_pod *spa_alsa_enum_propinfo(struct state *state, | 
					
						
							| 
									
										
										
										
											2021-12-01 11:39:30 +01:00
										 |  |  | 		uint32_t idx, struct spa_pod_builder *b) | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct spa_pod *param; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (idx) { | 
					
						
							|  |  |  | 	case 0: | 
					
						
							|  |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							| 
									
										
										
										
											2021-12-01 11:39:30 +01:00
										 |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_name, SPA_POD_String(SPA_KEY_AUDIO_CHANNELS), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Audio Channels"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_type, SPA_POD_Int(state->default_channels), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case 1: | 
					
						
							|  |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							| 
									
										
										
										
											2021-12-01 11:39:30 +01:00
										 |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_name, SPA_POD_String(SPA_KEY_AUDIO_RATE), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Audio Rate"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_type, SPA_POD_Int(state->default_rate), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case 2: | 
					
						
							|  |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							| 
									
										
										
										
											2021-12-01 11:39:30 +01:00
										 |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_name, SPA_POD_String(SPA_KEY_AUDIO_FORMAT), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Audio Format"), | 
					
						
							| 
									
										
										
										
											2021-11-30 18:19:08 +01:00
										 |  |  | 			SPA_PROP_INFO_type, SPA_POD_String( | 
					
						
							|  |  |  | 				spa_debug_type_find_short_name(spa_type_audio_format, | 
					
						
							|  |  |  | 					state->default_format)), | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case 3: | 
					
						
							| 
									
										
										
										
											2021-11-30 18:19:08 +01:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		char buf[1024]; | 
					
						
							|  |  |  | 		position_to_string(&state->default_pos, buf, sizeof(buf)); | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							| 
									
										
										
										
											2021-12-01 11:39:30 +01:00
										 |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_name, SPA_POD_String(SPA_KEY_AUDIO_POSITION), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Audio Position"), | 
					
						
							| 
									
										
										
										
											2021-11-30 18:19:08 +01:00
										 |  |  | 			SPA_PROP_INFO_type, SPA_POD_String(buf), | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2021-11-30 18:19:08 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	case 4: | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		char buf[1024]; | 
					
						
							|  |  |  | 		uint32_array_to_string(state->allowed_rates, state->n_allowed_rates, buf, sizeof(buf)); | 
					
						
							|  |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							|  |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							|  |  |  | 			SPA_PROP_INFO_name, SPA_POD_String(SPA_KEY_AUDIO_ALLOWED_RATES), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Audio Allowed Rates"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_type, SPA_POD_String(buf), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	case 5: | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							| 
									
										
										
										
											2021-12-01 11:39:30 +01:00
										 |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_name, SPA_POD_String("api.alsa.period-size"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Period Size"), | 
					
						
							| 
									
										
										
										
											2022-03-04 17:17:31 +01:00
										 |  |  | 			SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Int(state->default_period_size, 0, 8192), | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 	case 6: | 
					
						
							| 
									
										
										
										
											2021-12-17 16:09:52 +01:00
										 |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							|  |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							|  |  |  | 			SPA_PROP_INFO_name, SPA_POD_String("api.alsa.period-num"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Number of Periods"), | 
					
						
							| 
									
										
										
										
											2022-03-04 17:17:31 +01:00
										 |  |  | 			SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Int(state->default_period_num, 0, 1024), | 
					
						
							| 
									
										
										
										
											2021-12-17 16:09:52 +01:00
										 |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 	case 7: | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							| 
									
										
										
										
											2021-12-01 11:39:30 +01:00
										 |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_name, SPA_POD_String("api.alsa.headroom"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Headroom"), | 
					
						
							| 
									
										
										
										
											2022-03-04 17:17:31 +01:00
										 |  |  | 			SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Int(state->default_headroom, 0, 8192), | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 	case 8: | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							| 
									
										
										
										
											2021-12-01 11:39:30 +01:00
										 |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_name, SPA_POD_String("api.alsa.start-delay"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Start Delay"), | 
					
						
							| 
									
										
										
										
											2022-03-04 17:17:31 +01:00
										 |  |  | 			SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Int(state->default_start_delay, 0, 8192), | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 	case 9: | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							| 
									
										
										
										
											2021-12-01 11:39:30 +01:00
										 |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_name, SPA_POD_String("api.alsa.disable-mmap"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Disable MMAP"), | 
					
						
							| 
									
										
										
										
											2022-03-04 17:17:31 +01:00
										 |  |  | 			SPA_PROP_INFO_type, SPA_POD_CHOICE_Bool(state->disable_mmap), | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 	case 10: | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							| 
									
										
										
										
											2021-12-01 11:39:30 +01:00
										 |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_name, SPA_POD_String("api.alsa.disable-batch"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Disable Batch"), | 
					
						
							| 
									
										
										
										
											2022-03-04 17:17:31 +01:00
										 |  |  | 			SPA_PROP_INFO_type, SPA_POD_CHOICE_Bool(state->disable_batch), | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 	case 11: | 
					
						
							| 
									
										
										
										
											2023-03-28 18:27:53 +02:00
										 |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							|  |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							|  |  |  | 			SPA_PROP_INFO_name, SPA_POD_String("api.alsa.disable-tsched"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Disable timer based scheduling"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_type, SPA_POD_CHOICE_Bool(state->disable_tsched), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case 12: | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							| 
									
										
										
										
											2021-12-01 11:39:30 +01:00
										 |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_name, SPA_POD_String("api.alsa.use-chmap"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Use the driver channelmap"), | 
					
						
							| 
									
										
										
										
											2022-03-04 17:17:31 +01:00
										 |  |  | 			SPA_PROP_INFO_type, SPA_POD_CHOICE_Bool(state->props.use_chmap), | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2023-03-28 18:27:53 +02:00
										 |  |  | 	case 13: | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							| 
									
										
										
										
											2021-12-01 11:39:30 +01:00
										 |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_name, SPA_POD_String("api.alsa.multi-rate"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Support multiple rates"), | 
					
						
							| 
									
										
										
										
											2022-03-04 17:17:31 +01:00
										 |  |  | 			SPA_PROP_INFO_type, SPA_POD_CHOICE_Bool(state->multi_rate), | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2023-03-28 18:27:53 +02:00
										 |  |  | 	case 14: | 
					
						
							| 
									
										
										
										
											2023-06-29 16:23:39 +02:00
										 |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							|  |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							|  |  |  | 			SPA_PROP_INFO_name, SPA_POD_String("api.alsa.htimestamp"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Use hires timestamps"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_type, SPA_POD_CHOICE_Bool(state->htimestamp), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case 15: | 
					
						
							| 
									
										
										
										
											2022-01-18 11:46:08 +01:00
										 |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							|  |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							|  |  |  | 			SPA_PROP_INFO_name, SPA_POD_String("latency.internal.rate"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Internal latency in samples"), | 
					
						
							| 
									
										
										
										
											2022-03-04 17:17:31 +01:00
										 |  |  | 			SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Int(state->process_latency.rate, | 
					
						
							|  |  |  | 				0, 65536), | 
					
						
							| 
									
										
										
										
											2022-01-18 11:46:08 +01:00
										 |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2023-06-29 16:23:39 +02:00
										 |  |  | 	case 16: | 
					
						
							| 
									
										
										
										
											2022-01-18 11:46:08 +01:00
										 |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							|  |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							|  |  |  | 			SPA_PROP_INFO_name, SPA_POD_String("latency.internal.ns"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Internal latency in nanoseconds"), | 
					
						
							| 
									
										
										
										
											2022-03-04 17:17:31 +01:00
										 |  |  | 			SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Long(state->process_latency.ns, | 
					
						
							| 
									
										
										
										
											2022-04-21 22:12:38 +02:00
										 |  |  | 				0LL, 2 * SPA_NSEC_PER_SEC), | 
					
						
							| 
									
										
										
										
											2022-01-18 11:46:08 +01:00
										 |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2023-06-29 16:23:39 +02:00
										 |  |  | 	case 17: | 
					
						
							| 
									
										
										
										
											2021-12-10 11:04:38 +01:00
										 |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							|  |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							|  |  |  | 			SPA_PROP_INFO_name, SPA_POD_String("clock.name"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("The name of the clock"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_type, SPA_POD_String(state->clock_name), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2024-02-20 08:47:03 +01:00
										 |  |  | 	case 18: | 
					
						
							|  |  |  | 		param = spa_pod_builder_add_object(b, | 
					
						
							|  |  |  | 			SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo, | 
					
						
							|  |  |  | 			SPA_PROP_INFO_name, SPA_POD_String("api.alsa.htimestamp.max-errors"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_description, SPA_POD_String("Max errors before disabling htimestamp"), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Int(state->htimestamp_max_errors, 0, INT32_MAX), | 
					
						
							|  |  |  | 			SPA_PROP_INFO_params, SPA_POD_Bool(true)); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 	// While adding params here, update the math in default too
 | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2024-02-20 08:47:03 +01:00
										 |  |  | 		idx -= 18; | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 		if (idx <= state->num_bind_ctls) | 
					
						
							|  |  |  | 			param = enum_bind_ctl_propinfo(state, idx - 1, b); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return param; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | static void add_bind_ctl_param(struct state *state, const snd_ctl_elem_value_t *elem, const snd_ctl_elem_info_t *info, | 
					
						
							|  |  |  | 		struct spa_pod_builder *b) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-03-03 11:03:50 +01:00
										 |  |  | 	struct spa_pod_frame f[1]; | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 	char param_name[1024]; | 
					
						
							| 
									
										
										
										
											2024-03-03 11:03:50 +01:00
										 |  |  | 	unsigned int count = snd_ctl_elem_info_get_count(info); | 
					
						
							|  |  |  | 	int type = snd_ctl_elem_info_get_type(info); | 
					
						
							|  |  |  | 	bool is_array = count > 1 && type != SND_CTL_ELEM_TYPE_BYTES; | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	snprintf(param_name, sizeof(param_name), "api.alsa.bind-ctl.%s", | 
					
						
							|  |  |  | 			snd_ctl_elem_info_get_name(info)); | 
					
						
							|  |  |  | 	spa_pod_builder_string(b, param_name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-03 11:03:50 +01:00
										 |  |  | 	if (is_array) | 
					
						
							|  |  |  | 		spa_pod_builder_push_array(b, &f[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (type) { | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 		case SND_CTL_ELEM_TYPE_BOOLEAN: | 
					
						
							| 
									
										
										
										
											2024-03-03 11:03:50 +01:00
										 |  |  | 			for (unsigned int i = 0; i < count; i++) | 
					
						
							|  |  |  | 				spa_pod_builder_bool(b, snd_ctl_elem_value_get_boolean(elem, i)); | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case SND_CTL_ELEM_TYPE_INTEGER: | 
					
						
							| 
									
										
										
										
											2024-03-03 11:03:50 +01:00
										 |  |  | 			for (unsigned int i = 0; i < count; i++) | 
					
						
							|  |  |  | 				spa_pod_builder_int(b, snd_ctl_elem_value_get_integer(elem, i)); | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case SND_CTL_ELEM_TYPE_INTEGER64: | 
					
						
							| 
									
										
										
										
											2024-03-03 11:03:50 +01:00
										 |  |  | 			for (unsigned int i = 0; i < count; i++) | 
					
						
							|  |  |  | 				spa_pod_builder_long(b, snd_ctl_elem_value_get_integer64(elem, i)); | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case SND_CTL_ELEM_TYPE_ENUMERATED: | 
					
						
							| 
									
										
										
										
											2024-03-03 11:03:50 +01:00
										 |  |  | 			for (unsigned int i = 0; i < count; i++) | 
					
						
							|  |  |  | 				spa_pod_builder_int(b, snd_ctl_elem_value_get_enumerated(elem, i)); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case SND_CTL_ELEM_TYPE_BYTES: | 
					
						
							| 
									
										
										
										
											2024-03-07 13:48:56 +01:00
										 |  |  | 			spa_pod_builder_bytes(b, snd_ctl_elem_value_get_bytes(elem), count); | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			spa_log_warn(state->log, "%s ctl '%s' not supported", | 
					
						
							|  |  |  | 					snd_ctl_elem_type_name(snd_ctl_elem_info_get_type(info)), | 
					
						
							|  |  |  | 					snd_ctl_elem_info_get_name(info)); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-03-03 11:03:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (is_array) | 
					
						
							|  |  |  | 		spa_pod_builder_pop(b, &f[0]); | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void add_bind_ctl_params(struct state *state, struct spa_pod_builder *b) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (unsigned int i = 0; i < state->num_bind_ctls; i++) { | 
					
						
							| 
									
										
										
										
											2024-06-07 06:19:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if(!state->bound_ctls[i].value || !state->bound_ctls[i].info) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 		err = snd_ctl_elem_read(state->ctl, state->bound_ctls[i].value); | 
					
						
							|  |  |  | 		if (err < 0) { | 
					
						
							|  |  |  | 			spa_log_warn(state->log, "Could not read elem value for '%s': %s", | 
					
						
							|  |  |  | 					state->bound_ctls[i].name, snd_strerror(err)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		add_bind_ctl_param(state, state->bound_ctls[i].value, state->bound_ctls[i].info, b); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | int spa_alsa_add_prop_params(struct state *state, struct spa_pod_builder *b) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct spa_pod_frame f[1]; | 
					
						
							| 
									
										
										
										
											2021-11-30 18:19:08 +01:00
										 |  |  | 	char buf[1024]; | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_prop(b, SPA_PROP_params, 0); | 
					
						
							|  |  |  | 	spa_pod_builder_push_struct(b, &f[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_string(b, SPA_KEY_AUDIO_CHANNELS); | 
					
						
							|  |  |  | 	spa_pod_builder_int(b, state->default_channels); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_string(b, SPA_KEY_AUDIO_RATE); | 
					
						
							|  |  |  | 	spa_pod_builder_int(b, state->default_rate); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_string(b, SPA_KEY_AUDIO_FORMAT); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:19:08 +01:00
										 |  |  | 	spa_pod_builder_string(b, | 
					
						
							|  |  |  | 			spa_debug_type_find_short_name(spa_type_audio_format, | 
					
						
							|  |  |  | 					state->default_format)); | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 18:19:08 +01:00
										 |  |  | 	position_to_string(&state->default_pos, buf, sizeof(buf)); | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	spa_pod_builder_string(b, SPA_KEY_AUDIO_POSITION); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:19:08 +01:00
										 |  |  | 	spa_pod_builder_string(b, buf); | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 	uint32_array_to_string(state->allowed_rates, state->n_allowed_rates, | 
					
						
							|  |  |  | 			buf, sizeof(buf)); | 
					
						
							|  |  |  | 	spa_pod_builder_string(b, SPA_KEY_AUDIO_ALLOWED_RATES); | 
					
						
							|  |  |  | 	spa_pod_builder_string(b, buf); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	spa_pod_builder_string(b, "api.alsa.period-size"); | 
					
						
							|  |  |  | 	spa_pod_builder_int(b, state->default_period_size); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-17 16:09:52 +01:00
										 |  |  | 	spa_pod_builder_string(b, "api.alsa.period-num"); | 
					
						
							|  |  |  | 	spa_pod_builder_int(b, state->default_period_num); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	spa_pod_builder_string(b, "api.alsa.headroom"); | 
					
						
							|  |  |  | 	spa_pod_builder_int(b, state->default_headroom); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_string(b, "api.alsa.start-delay"); | 
					
						
							|  |  |  | 	spa_pod_builder_int(b, state->default_start_delay); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_string(b, "api.alsa.disable-mmap"); | 
					
						
							|  |  |  | 	spa_pod_builder_bool(b, state->disable_mmap); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_string(b, "api.alsa.disable-batch"); | 
					
						
							|  |  |  | 	spa_pod_builder_bool(b, state->disable_batch); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-28 18:27:53 +02:00
										 |  |  | 	spa_pod_builder_string(b, "api.alsa.disable-tsched"); | 
					
						
							|  |  |  | 	spa_pod_builder_bool(b, state->disable_tsched); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	spa_pod_builder_string(b, "api.alsa.use-chmap"); | 
					
						
							|  |  |  | 	spa_pod_builder_bool(b, state->props.use_chmap); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_string(b, "api.alsa.multi-rate"); | 
					
						
							|  |  |  | 	spa_pod_builder_bool(b, state->multi_rate); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-29 16:23:39 +02:00
										 |  |  | 	spa_pod_builder_string(b, "api.alsa.htimestamp"); | 
					
						
							|  |  |  | 	spa_pod_builder_bool(b, state->htimestamp); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-20 08:47:03 +01:00
										 |  |  | 	spa_pod_builder_string(b, "api.alsa.htimestamp.max-errors"); | 
					
						
							|  |  |  | 	spa_pod_builder_int(b, state->htimestamp_max_errors); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-18 11:46:08 +01:00
										 |  |  | 	spa_pod_builder_string(b, "latency.internal.rate"); | 
					
						
							|  |  |  | 	spa_pod_builder_int(b, state->process_latency.rate); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_string(b, "latency.internal.ns"); | 
					
						
							|  |  |  | 	spa_pod_builder_long(b, state->process_latency.ns); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-10 11:04:38 +01:00
										 |  |  | 	spa_pod_builder_string(b, "clock.name"); | 
					
						
							|  |  |  | 	spa_pod_builder_string(b, state->clock_name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 	add_bind_ctl_params(state, b); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 	spa_pod_builder_pop(b, &f[0]); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int spa_alsa_parse_prop_params(struct state *state, struct spa_pod *params) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct spa_pod_parser prs; | 
					
						
							|  |  |  | 	struct spa_pod_frame f; | 
					
						
							|  |  |  | 	int changed = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (params == NULL) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_parser_pod(&prs, params); | 
					
						
							|  |  |  | 	if (spa_pod_parser_push_struct(&prs, &f) < 0) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (true) { | 
					
						
							|  |  |  | 		const char *name; | 
					
						
							|  |  |  | 		struct spa_pod *pod; | 
					
						
							|  |  |  | 		char value[512]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (spa_pod_parser_get_string(&prs, &name) < 0) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (spa_pod_parser_get_pod(&prs, &pod) < 0) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		if (spa_pod_is_string(pod)) { | 
					
						
							|  |  |  | 			spa_pod_copy_string(pod, sizeof(value), value); | 
					
						
							|  |  |  | 		} else if (spa_pod_is_int(pod)) { | 
					
						
							| 
									
										
										
										
											2022-01-18 11:46:08 +01:00
										 |  |  | 			snprintf(value, sizeof(value), "%d", | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 					SPA_POD_VALUE(struct spa_pod_int, pod)); | 
					
						
							| 
									
										
										
										
											2022-01-18 11:46:08 +01:00
										 |  |  | 		} else if (spa_pod_is_long(pod)) { | 
					
						
							|  |  |  | 			snprintf(value, sizeof(value), "%"PRIi64, | 
					
						
							|  |  |  | 					SPA_POD_VALUE(struct spa_pod_long, pod)); | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 		} else if (spa_pod_is_bool(pod)) { | 
					
						
							|  |  |  | 			snprintf(value, sizeof(value), "%s", | 
					
						
							|  |  |  | 					SPA_POD_VALUE(struct spa_pod_bool, pod) ? | 
					
						
							|  |  |  | 					"true" : "false"); | 
					
						
							|  |  |  | 		} else | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-04 17:17:31 +01:00
										 |  |  | 		spa_log_info(state->log, "key:'%s' val:'%s'", name, value); | 
					
						
							| 
									
										
										
										
											2021-12-01 12:54:44 +01:00
										 |  |  | 		alsa_set_param(state, name, value); | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 		changed++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (changed > 0) { | 
					
						
							|  |  |  | 		state->info.change_mask |= SPA_NODE_CHANGE_MASK_PARAMS; | 
					
						
							|  |  |  | 		state->params[NODE_Props].user++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return changed; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | static ssize_t log_write(void *cookie, const char *buf, size_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct state *state = cookie; | 
					
						
							|  |  |  | 	int len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (size > 0) { | 
					
						
							|  |  |  | 		len = strcspn(buf, "\n"); | 
					
						
							|  |  |  | 		if (len > 0) | 
					
						
							|  |  |  | 			spa_log_debug(state->log, "%.*s", (int)len, buf); | 
					
						
							|  |  |  | 		buf += len + 1; | 
					
						
							|  |  |  | 		size -= len + 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return size; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static cookie_io_functions_t io_funcs = { | 
					
						
							|  |  |  | 	.write = log_write, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-19 17:20:57 +02:00
										 |  |  | static void silence_error_handler(const char *file, int line, | 
					
						
							|  |  |  | 		const char *function, int err, const char *fmt, ...) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | static void fill_device_name(struct state *state, const char *params, char device_name[], size_t len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	spa_scnprintf(device_name, len, "%s%s%s", | 
					
						
							| 
									
										
										
										
											2024-11-29 10:42:58 -05:00
										 |  |  | 			state->card && state->card->ucm_prefix ? state->card->ucm_prefix : "", | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 			state->props.device, params ? params : ""); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void bind_ctl_event(struct spa_source *source) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct state *state = source->data; | 
					
						
							| 
									
										
										
										
											2024-03-14 10:13:41 -04:00
										 |  |  | 	snd_ctl_event_t *ev; | 
					
						
							|  |  |  | 	snd_ctl_elem_id_t *id, *bound_id; | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 	snd_ctl_elem_value_t *old_value; | 
					
						
							| 
									
										
										
										
											2024-03-14 10:13:41 -04:00
										 |  |  | 	unsigned short revents; | 
					
						
							|  |  |  | 	int err; | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-14 10:13:41 -04:00
										 |  |  | 	// Do the same demangling of revents we do for PCM pollfds
 | 
					
						
							|  |  |  | 	for (int i = 0; i < state->ctl_n_fds; i++) { | 
					
						
							|  |  |  | 		state->ctl_pfds[i].revents = state->ctl_sources[i].rmask; | 
					
						
							|  |  |  | 		state->ctl_sources[i].rmask = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-14 10:13:41 -04:00
										 |  |  | 	err = snd_ctl_poll_descriptors_revents(state->ctl, state->ctl_pfds, state->ctl_n_fds, &revents); | 
					
						
							|  |  |  | 	if (SPA_UNLIKELY(err < 0)) { | 
					
						
							|  |  |  | 		spa_log_warn(state->log, "Could not read ctl revents: %s", snd_strerror(err)); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-14 10:13:41 -04:00
										 |  |  | 	if (!revents) { | 
					
						
							|  |  |  | 		spa_log_trace(state->log, "Got a bind ctl wakeup but no actual event"); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-14 10:13:41 -04:00
										 |  |  | 	snd_ctl_event_alloca(&ev); | 
					
						
							|  |  |  | 	snd_ctl_elem_id_alloca(&id); | 
					
						
							|  |  |  | 	snd_ctl_elem_id_alloca(&bound_id); | 
					
						
							|  |  |  | 	snd_ctl_elem_value_alloca(&old_value); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-25 13:22:32 +03:00
										 |  |  | 	while ((err = snd_ctl_read(state->ctl, ev)) > 0) { | 
					
						
							| 
									
										
										
										
											2024-03-14 10:13:41 -04:00
										 |  |  | 		bool changed = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (snd_ctl_event_get_type(ev) != SND_CTL_EVENT_ELEM) | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 			continue; | 
					
						
							| 
									
										
										
										
											2024-03-14 10:13:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		snd_ctl_event_elem_get_id(ev, id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (unsigned int i = 0; i < state->num_bind_ctls; i++) { | 
					
						
							|  |  |  | 			int err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-07 06:19:00 +00:00
										 |  |  | 			if(!state->bound_ctls[i].value || !state->bound_ctls[i].info) | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-14 10:13:41 -04:00
										 |  |  | 			// Check if we have the right element
 | 
					
						
							|  |  |  | 			snd_ctl_elem_value_get_id(state->bound_ctls[i].value, bound_id); | 
					
						
							|  |  |  | 			if (snd_ctl_elem_id_compare_set(id, bound_id) || | 
					
						
							|  |  |  | 					snd_ctl_elem_id_compare_numid(id, bound_id)) { | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			snd_ctl_elem_value_copy(old_value, state->bound_ctls[i].value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			err = snd_ctl_elem_read(state->ctl, state->bound_ctls[i].value); | 
					
						
							|  |  |  | 			if (err < 0) { | 
					
						
							|  |  |  | 				spa_log_warn(state->log, "Could not read ctl '%s': %s", | 
					
						
							|  |  |  | 						state->bound_ctls[i].name, snd_strerror(err)); | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (snd_ctl_elem_value_compare(old_value, state->bound_ctls[i].value) != 0) { | 
					
						
							|  |  |  | 				// We don't need to check all the ctls, if one changed,
 | 
					
						
							|  |  |  | 				// we'll emit a notification and they'll be read when
 | 
					
						
							|  |  |  | 				// the props are read
 | 
					
						
							|  |  |  | 				spa_log_debug(state->log, "bound ctl '%s' has changed", state->bound_ctls[i].name); | 
					
						
							|  |  |  | 				changed = true; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-14 10:13:41 -04:00
										 |  |  | 		if (changed) { | 
					
						
							|  |  |  | 			state->info.change_mask |= SPA_NODE_CHANGE_MASK_PARAMS; | 
					
						
							|  |  |  | 			state->params[NODE_Props].user++; | 
					
						
							|  |  |  | 			spa_alsa_emit_node_info(state, false); | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-14 10:13:41 -04:00
										 |  |  | 	if (err < 0 && err != -EAGAIN) | 
					
						
							|  |  |  | 		spa_log_warn(state->log, "Could not read ctl: %s", snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-03 10:01:59 +01:00
										 |  |  | static void fetch_bind_ctls(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	snd_ctl_elem_list_t* element_list; | 
					
						
							|  |  |  | 	unsigned int elem_count = 0; | 
					
						
							|  |  |  | 	int err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!state->num_bind_ctls) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	snd_ctl_elem_list_alloca(&element_list); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Get number of elements */ | 
					
						
							|  |  |  | 	err = snd_ctl_elem_list(state->ctl, element_list); | 
					
						
							|  |  |  | 	if (SPA_UNLIKELY(err < 0)) { | 
					
						
							|  |  |  | 		spa_log_warn(state->log, "Couldn't get elem list count. Error: %s", | 
					
						
							|  |  |  | 			     snd_strerror(err)); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	elem_count = snd_ctl_elem_list_get_count(element_list); | 
					
						
							|  |  |  | 	err = snd_ctl_elem_list_alloc_space(element_list, elem_count); | 
					
						
							|  |  |  | 	if (SPA_UNLIKELY(err < 0)) { | 
					
						
							|  |  |  | 		spa_log_error(state->log, "Couldn't allocate elem_list space. Error: %s", | 
					
						
							|  |  |  | 			      snd_strerror(err)); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Get identifiers */ | 
					
						
							|  |  |  | 	err = snd_ctl_elem_list(state->ctl, element_list); | 
					
						
							|  |  |  | 	if (SPA_UNLIKELY(err < 0)) { | 
					
						
							|  |  |  | 		spa_log_warn(state->log, "Couldn't get elem list. Error: %s", | 
					
						
							|  |  |  | 			     snd_strerror(err)); | 
					
						
							|  |  |  | 		goto cleanup; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (unsigned int i = 0; i < state->num_bind_ctls; i++) { | 
					
						
							|  |  |  | 		unsigned int numid = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 16:34:01 -04:00
										 |  |  | 		for (unsigned int j = 0; j < elem_count; j++) { | 
					
						
							| 
									
										
										
										
											2024-03-03 10:01:59 +01:00
										 |  |  | 			const char* element_name = snd_ctl_elem_list_get_name(element_list, j); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (!strcmp(element_name, state->bound_ctls[i].name)) { | 
					
						
							|  |  |  | 				numid = snd_ctl_elem_list_get_numid(element_list, j); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* zero = invalid numid */ | 
					
						
							|  |  |  | 		if (SPA_UNLIKELY(!numid)) { | 
					
						
							|  |  |  | 			spa_log_warn(state->log, "Didn't find ctl: '%s', count: %u", | 
					
						
							|  |  |  | 				     state->bound_ctls[i].name, elem_count); | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		snd_ctl_elem_info_malloc(&state->bound_ctls[i].info); | 
					
						
							|  |  |  | 		snd_ctl_elem_info_set_numid(state->bound_ctls[i].info, numid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		err = snd_ctl_elem_info(state->ctl, state->bound_ctls[i].info); | 
					
						
							|  |  |  | 		if (SPA_UNLIKELY(err < 0)) { | 
					
						
							|  |  |  | 			spa_log_warn(state->log, "Could not read elem info for '%s': %s", | 
					
						
							|  |  |  | 				     state->bound_ctls[i].name, snd_strerror(err)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			snd_ctl_elem_info_free(state->bound_ctls[i].info); | 
					
						
							|  |  |  | 			state->bound_ctls[i].info = NULL; | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		snd_ctl_elem_value_malloc(&state->bound_ctls[i].value); | 
					
						
							|  |  |  | 		snd_ctl_elem_value_set_numid(state->bound_ctls[i].value, numid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		spa_log_debug(state->log, "Binding ctl for '%s'", | 
					
						
							|  |  |  | 			      snd_ctl_elem_info_get_name(state->bound_ctls[i].info)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | cleanup: | 
					
						
							|  |  |  | 	snd_ctl_elem_list_free_space(element_list); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 17:21:36 +03:00
										 |  |  | int open_card_ctl(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int err; | 
					
						
							|  |  |  | 	char card_name[256]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	snprintf(card_name, sizeof(card_name), "hw:%d", state->card_index); | 
					
						
							| 
									
										
										
										
											2024-11-21 17:26:41 -05:00
										 |  |  | 	spa_log_debug(state->log, "Trying to open ctl device '%s'", card_name); | 
					
						
							| 
									
										
										
										
											2024-05-27 17:21:36 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	err = snd_ctl_open(&state->ctl, card_name, SND_CTL_NONBLOCK); | 
					
						
							|  |  |  | 	if (err < 0) { | 
					
						
							|  |  |  | 		spa_log_info(state->log, "%s could not find ctl card: %s", | 
					
						
							|  |  |  | 				card_name, snd_strerror(err)); | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | static void bind_ctls_for_params(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state->num_bind_ctls == 0) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!state->ctl) { | 
					
						
							| 
									
										
										
										
											2024-05-27 17:21:36 +03:00
										 |  |  | 		err = open_card_ctl(state); | 
					
						
							|  |  |  | 		if (err < 0) | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 			return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	state->ctl_n_fds = snd_ctl_poll_descriptors_count(state->ctl); | 
					
						
							|  |  |  | 	if (state->ctl_n_fds > (int)SPA_N_ELEMENTS(state->ctl_sources)) { | 
					
						
							|  |  |  | 		spa_log_warn(state->log, "Too many poll descriptors (%d), listening to a subset", state->ctl_n_fds); | 
					
						
							|  |  |  | 		state->ctl_n_fds = SPA_N_ELEMENTS(state->ctl_sources); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-14 10:13:41 -04:00
										 |  |  | 	if ((err = snd_ctl_poll_descriptors(state->ctl, state->ctl_pfds, state->ctl_n_fds)) < 0) { | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 		spa_log_warn(state->log, "Could not get poll descriptors: %s", snd_strerror(err)); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	snd_ctl_subscribe_events(state->ctl, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < state->ctl_n_fds; i++) { | 
					
						
							|  |  |  | 		state->ctl_sources[i].func = bind_ctl_event; | 
					
						
							|  |  |  | 		state->ctl_sources[i].data = state; | 
					
						
							| 
									
										
										
										
											2024-03-14 10:13:41 -04:00
										 |  |  | 		state->ctl_sources[i].fd = state->ctl_pfds[i].fd; | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 		state->ctl_sources[i].mask = SPA_IO_IN; | 
					
						
							|  |  |  | 		state->ctl_sources[i].rmask = 0; | 
					
						
							| 
									
										
										
										
											2023-11-21 17:05:24 -05:00
										 |  |  | 		spa_loop_add_source(state->main_loop, &state->ctl_sources[i]); | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-03 10:01:59 +01:00
										 |  |  | 	fetch_bind_ctls(state); | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:27:36 +01:00
										 |  |  | int spa_alsa_init(struct state *state, const struct spa_dict *info) | 
					
						
							| 
									
										
										
										
											2021-08-30 17:36:11 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-23 12:27:36 +01:00
										 |  |  | 	uint32_t i; | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | 	int err; | 
					
						
							| 
									
										
										
										
											2023-08-28 10:06:29 +02:00
										 |  |  | 	const char *str; | 
					
						
							| 
									
										
										
										
											2021-11-23 12:27:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 	spa_list_init(&state->followers); | 
					
						
							|  |  |  | 	spa_list_init(&state->rt.followers); | 
					
						
							| 
									
										
										
										
											2023-09-18 09:40:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 11:23:48 +02:00
										 |  |  | 	snd_config_update_free_global(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-14 13:54:23 +03:00
										 |  |  | 	if (info && (str = spa_dict_lookup(info, "device.profile.pro")) != NULL) | 
					
						
							| 
									
										
										
										
											2023-08-28 10:06:29 +02:00
										 |  |  | 		state->is_pro = spa_atob(str); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-23 13:20:12 +03:00
										 |  |  | 	if (info && spa_strstartswith(spa_dict_lookup(info, SPA_KEY_API_ALSA_CARD_NAME), "sof-") && | 
					
						
							|  |  |  | 	    state->stream == SND_PCM_STREAM_PLAYBACK) { | 
					
						
							|  |  |  | 		state->use_period_size_min_as_headroom = true; | 
					
						
							|  |  |  | 		spa_log_info(state->log, | 
					
						
							|  |  |  | 			     "ALSA SOF driver detected: default api.alsa.use-period-size-min-as-headroom=true"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 08:33:34 +01:00
										 |  |  | 	state->multi_rate = true; | 
					
						
							| 
									
										
										
										
											2023-07-26 10:32:53 +02:00
										 |  |  | 	state->htimestamp = false; | 
					
						
							| 
									
										
										
										
											2024-02-20 08:47:03 +01:00
										 |  |  | 	state->htimestamp_max_errors = MAX_HTIMESTAMP_ERROR; | 
					
						
							| 
									
										
										
										
											2024-11-21 17:26:41 -05:00
										 |  |  | 	state->card_index = SPA_ID_INVALID; | 
					
						
							| 
									
										
										
										
											2025-07-18 18:19:16 +02:00
										 |  |  | 	state->dll_bw_max = SPA_DLL_BW_MAX; | 
					
						
							| 
									
										
										
										
											2024-11-21 17:26:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:27:36 +01:00
										 |  |  | 	for (i = 0; info && i < info->n_items; i++) { | 
					
						
							|  |  |  | 		const char *k = info->items[i].key; | 
					
						
							|  |  |  | 		const char *s = info->items[i].value; | 
					
						
							|  |  |  | 		if (spa_streq(k, SPA_KEY_API_ALSA_PATH)) { | 
					
						
							|  |  |  | 			snprintf(state->props.device, 63, "%s", s); | 
					
						
							|  |  |  | 		} else if (spa_streq(k, SPA_KEY_API_ALSA_PCM_CARD)) { | 
					
						
							|  |  |  | 			state->card_index = atoi(s); | 
					
						
							|  |  |  | 		} else if (spa_streq(k, SPA_KEY_API_ALSA_OPEN_UCM)) { | 
					
						
							|  |  |  | 			state->open_ucm = spa_atob(s); | 
					
						
							| 
									
										
										
										
											2024-01-25 10:26:11 +01:00
										 |  |  | 			if (state->open_ucm) | 
					
						
							|  |  |  | 				state->props.use_chmap = true; | 
					
						
							| 
									
										
										
										
											2022-01-12 17:50:12 +01:00
										 |  |  | 		} else if (spa_streq(k, "clock.quantum-limit")) { | 
					
						
							|  |  |  | 			spa_atou32(s, &state->quantum_limit, 0); | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 		} else if (spa_streq(k, SPA_KEY_API_ALSA_BIND_CTLS)) { | 
					
						
							| 
									
										
										
										
											2024-09-13 13:09:54 +02:00
										 |  |  | 			struct spa_json it[1]; | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 			char v[256]; | 
					
						
							|  |  |  | 			unsigned int i = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* Read a list of ALSA control names to bind as params */ | 
					
						
							| 
									
										
										
										
											2024-09-13 13:09:54 +02:00
										 |  |  | 			if (spa_json_begin_array_relax(&it[0], s, strlen(s)) <= 0) | 
					
						
							|  |  |  | 				continue; | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-13 13:09:54 +02:00
										 |  |  | 			while (spa_json_get_string(&it[0], v, sizeof(v)) > 0 && | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 					i < SPA_N_ELEMENTS(state->bound_ctls)) { | 
					
						
							| 
									
										
										
										
											2024-01-10 11:34:27 +01:00
										 |  |  | 				snprintf(state->bound_ctls[i].name, | 
					
						
							|  |  |  | 						sizeof(state->bound_ctls[i].name), "%s", v); | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 				i++; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			state->num_bind_ctls = i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* We'll do the actual binding after checking the card exists */ | 
					
						
							| 
									
										
										
										
											2025-09-06 19:14:22 +03:00
										 |  |  | 		} else if (spa_streq(k, SPA_KEY_DEVICE_BUS)) { | 
					
						
							|  |  |  | 			state->is_firewire = spa_streq(s, "firewire"); | 
					
						
							| 
									
										
										
										
											2021-11-30 17:43:07 +01:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2021-12-01 12:54:44 +01:00
										 |  |  | 			alsa_set_param(state, k, s); | 
					
						
							| 
									
										
										
										
											2021-11-23 12:27:36 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-11-21 17:26:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (state->card_index == SPA_ID_INVALID) { | 
					
						
							|  |  |  | 		/* If we don't have a card index, see if we have a *:<idx> string */ | 
					
						
							|  |  |  | 		sscanf(state->props.device, "%*[^:]:%u", &state->card_index); | 
					
						
							|  |  |  | 		if (state->card_index == SPA_ID_INVALID) { | 
					
						
							| 
									
										
										
										
											2024-11-29 10:42:58 -05:00
										 |  |  | 			spa_log_info(state->log, "Could not determine card index. %s and/or clock.name " | 
					
						
							|  |  |  | 					"may need to be configured manually", SPA_KEY_API_ALSA_PCM_CARD); | 
					
						
							| 
									
										
										
										
											2024-11-21 17:26:41 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 10:42:58 -05:00
										 |  |  | 	if (state->clock_name[0] == '\0' && state->card_index != SPA_ID_INVALID) | 
					
						
							| 
									
										
										
										
											2021-12-10 11:04:38 +01:00
										 |  |  | 		snprintf(state->clock_name, sizeof(state->clock_name), | 
					
						
							| 
									
										
										
										
											2022-06-08 17:03:50 +02:00
										 |  |  | 				"api.alsa.%s-%u", | 
					
						
							|  |  |  | 				state->stream == SND_PCM_STREAM_PLAYBACK ? "p" : "c", | 
					
						
							|  |  |  | 				state->card_index); | 
					
						
							| 
									
										
										
										
											2021-11-23 12:27:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 	if (state->stream == SND_PCM_STREAM_PLAYBACK) { | 
					
						
							|  |  |  | 		state->is_iec958 = spa_strstartswith(state->props.device, "iec958"); | 
					
						
							|  |  |  | 		state->is_hdmi = spa_strstartswith(state->props.device, "hdmi"); | 
					
						
							| 
									
										
										
										
											2021-09-01 11:16:38 +02:00
										 |  |  | 		state->iec958_codecs |= 1ULL << SPA_AUDIO_IEC958_CODEC_PCM; | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 13:27:09 +02:00
										 |  |  | 	state->card = ensure_card(state->card_index, state->open_ucm, state->is_split_parent); | 
					
						
							| 
									
										
										
										
											2024-11-29 10:42:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | 	state->log_file = fopencookie(state, "w", io_funcs); | 
					
						
							|  |  |  | 	if (state->log_file == NULL) { | 
					
						
							|  |  |  | 		spa_log_error(state->log, "can't create log file"); | 
					
						
							|  |  |  | 		return -errno; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	CHECK(snd_output_stdio_attach(&state->output, state->log_file, 0), "attach failed"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 	spa_list_append(&states, &state->link); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 15:52:24 +02:00
										 |  |  | 	state->rate_limit.interval = 2 * SPA_NSEC_PER_SEC; | 
					
						
							|  |  |  | 	state->rate_limit.burst = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 	bind_ctls_for_params(state); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 13:23:08 +02:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int spa_alsa_clear(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | 	int err; | 
					
						
							| 
									
										
										
										
											2024-10-21 15:47:21 +02:00
										 |  |  | 	struct state *follower; | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-18 09:40:52 +02:00
										 |  |  | 	spa_list_remove(&state->link); | 
					
						
							| 
									
										
										
										
											2021-12-20 19:29:05 +01:00
										 |  |  | 	release_card(state->card); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 15:47:21 +02:00
										 |  |  | 	if (state->driver != NULL) { | 
					
						
							|  |  |  | 		spa_list_remove(&state->driver_link); | 
					
						
							|  |  |  | 		state->driver = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (state->rt.driver != NULL) { | 
					
						
							|  |  |  | 		spa_list_remove(&state->rt.driver_link); | 
					
						
							|  |  |  | 		state->rt.driver = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	spa_list_consume(follower, &state->followers, driver_link) { | 
					
						
							|  |  |  | 		spa_list_remove(&follower->driver_link); | 
					
						
							|  |  |  | 		follower->driver = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	spa_list_consume(follower, &state->rt.followers, rt.driver_link) { | 
					
						
							|  |  |  | 		spa_list_remove(&follower->rt.driver_link); | 
					
						
							|  |  |  | 		follower->rt.driver = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 11:28:41 +01:00
										 |  |  | 	state->card = NULL; | 
					
						
							| 
									
										
										
										
											2021-12-20 19:29:05 +01:00
										 |  |  | 	state->card_index = SPA_ID_INVALID; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | 	if ((err = snd_output_close(state->output)) < 0) | 
					
						
							|  |  |  | 		spa_log_warn(state->log, "output close failed: %s", snd_strerror(err)); | 
					
						
							|  |  |  | 	fclose(state->log_file); | 
					
						
							| 
									
										
										
										
											2021-06-02 13:23:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 15:56:52 +02:00
										 |  |  | 	free(state->tag[0]); | 
					
						
							|  |  |  | 	free(state->tag[1]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 	if (state->ctl) { | 
					
						
							| 
									
										
										
										
											2023-11-09 09:23:07 -05:00
										 |  |  | 		for (int i = 0; i < state->ctl_n_fds; i++) { | 
					
						
							| 
									
										
										
										
											2023-11-21 17:05:24 -05:00
										 |  |  | 			spa_loop_remove_source(state->main_loop, &state->ctl_sources[i]); | 
					
						
							| 
									
										
										
										
											2023-11-09 09:23:07 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		snd_ctl_close(state->ctl); | 
					
						
							|  |  |  | 		state->ctl = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 		for (unsigned int i = 0; i < state->num_bind_ctls; i++) { | 
					
						
							|  |  |  | 			if (state->bound_ctls[i].info) { | 
					
						
							|  |  |  | 				snd_ctl_elem_info_free(state->bound_ctls[i].info); | 
					
						
							|  |  |  | 				state->bound_ctls[i].info = NULL; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (state->bound_ctls[i].value) { | 
					
						
							|  |  |  | 				snd_ctl_elem_value_free(state->bound_ctls[i].value); | 
					
						
							|  |  |  | 				state->bound_ctls[i].value = NULL; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | 	return err; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 17:21:36 +03:00
										 |  |  | static int probe_pitch_ctl(struct state *state) | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | { | 
					
						
							|  |  |  | 	snd_ctl_elem_id_t *id; | 
					
						
							|  |  |  | 	/* TODO: Add configuration params for the control name and units */ | 
					
						
							|  |  |  | 	const char *elem_name = | 
					
						
							|  |  |  | 		state->stream == SND_PCM_STREAM_CAPTURE ? | 
					
						
							|  |  |  | 		"Capture Pitch 1000000" : | 
					
						
							|  |  |  | 		"Playback Pitch 1000000"; | 
					
						
							| 
									
										
										
										
											2023-11-09 09:24:09 -05:00
										 |  |  | 	bool opened = false; | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | 	int err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-19 17:20:57 +02:00
										 |  |  | 	snd_lib_error_set_handler(silence_error_handler); | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-09 09:24:09 -05:00
										 |  |  | 	if (!state->ctl) { | 
					
						
							| 
									
										
										
										
											2024-05-27 17:21:36 +03:00
										 |  |  | 		err = open_card_ctl(state); | 
					
						
							|  |  |  | 		if (err < 0) | 
					
						
							| 
									
										
										
										
											2023-11-09 09:24:09 -05:00
										 |  |  | 			goto error; | 
					
						
							| 
									
										
										
										
											2024-05-27 17:21:36 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-09 09:24:09 -05:00
										 |  |  | 		opened = true; | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	snd_ctl_elem_id_alloca(&id); | 
					
						
							|  |  |  | 	snd_ctl_elem_id_set_name(id, elem_name); | 
					
						
							|  |  |  | 	snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_PCM); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	snd_ctl_elem_value_malloc(&state->pitch_elem); | 
					
						
							|  |  |  | 	snd_ctl_elem_value_set_id(state->pitch_elem, id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = snd_ctl_elem_read(state->ctl, state->pitch_elem); | 
					
						
							|  |  |  | 	if (err < 0) { | 
					
						
							| 
									
										
										
										
											2024-05-27 17:21:36 +03:00
										 |  |  | 		spa_log_debug(state->log, "%s: did not find ctl: %s", | 
					
						
							|  |  |  | 				 elem_name, snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		snd_ctl_elem_value_free(state->pitch_elem); | 
					
						
							|  |  |  | 		state->pitch_elem = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-09 09:24:09 -05:00
										 |  |  | 		if (opened) { | 
					
						
							|  |  |  | 			snd_ctl_close(state->ctl); | 
					
						
							|  |  |  | 			state->ctl = NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-11-28 15:19:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		goto error; | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	snd_ctl_elem_value_set_integer(state->pitch_elem, 0, 1000000); | 
					
						
							| 
									
										
										
										
											2023-06-23 11:56:33 -04:00
										 |  |  | 	CHECK(snd_ctl_elem_write(state->ctl, state->pitch_elem), "snd_ctl_elem_write"); | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | 	state->last_rate = 1.0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 17:21:36 +03:00
										 |  |  | 	spa_log_info(state->log, "found ctl %s", elem_name); | 
					
						
							| 
									
										
										
										
											2023-06-19 17:20:57 +02:00
										 |  |  | 	err = 0; | 
					
						
							|  |  |  | error: | 
					
						
							|  |  |  | 	snd_lib_error_set_handler(NULL); | 
					
						
							|  |  |  | 	return err; | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | static int do_link(struct state *driver, struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	snd_pcm_status_t *status; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	snd_pcm_status_alloca(&status); | 
					
						
							|  |  |  | 	snd_pcm_status(driver->hndl, status); | 
					
						
							|  |  |  | 	snd_pcm_status_dump(status, state->output); | 
					
						
							|  |  |  | 	snd_pcm_status(state->hndl, status); | 
					
						
							|  |  |  | 	snd_pcm_status_dump(status, state->output); | 
					
						
							|  |  |  | 	fflush(state->log_file); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res = snd_pcm_link(driver->hndl, state->hndl); | 
					
						
							|  |  |  | 	if (res >= 0 || res == -EALREADY) | 
					
						
							|  |  |  | 		state->linked = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_log_info(state->log, "%p: linked to driver %p: %u (%s)", | 
					
						
							|  |  |  | 			state, driver, state->linked, snd_strerror(res)); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-14 12:42:03 +02:00
										 |  |  | int spa_alsa_open(struct state *state, const char *params) | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	int err; | 
					
						
							|  |  |  | 	struct props *props = &state->props; | 
					
						
							| 
									
										
										
										
											2021-09-14 12:42:03 +02:00
										 |  |  | 	char device_name[256]; | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	if (state->opened) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 	fill_device_name(state, params, device_name, sizeof(device_name)); | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 	spa_scnprintf(state->name, sizeof(state->name), "%s%s", | 
					
						
							|  |  |  | 			props->device, state->stream == SND_PCM_STREAM_CAPTURE ? "c" : "p"); | 
					
						
							| 
									
										
										
										
											2021-09-02 12:33:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 	spa_log_info(state->log, "%p: ALSA device open '%s' %s", state, device_name, | 
					
						
							| 
									
										
										
										
											2019-06-21 16:14:36 +02:00
										 |  |  | 			state->stream == SND_PCM_STREAM_CAPTURE ? "capture" : "playback"); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	CHECK(snd_pcm_open(&state->hndl, | 
					
						
							| 
									
										
										
										
											2021-09-02 12:33:44 +02:00
										 |  |  | 			   device_name, | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 			   state->stream, | 
					
						
							|  |  |  | 			   SND_PCM_NONBLOCK | | 
					
						
							|  |  |  | 			   SND_PCM_NO_AUTO_RESAMPLE | | 
					
						
							| 
									
										
										
										
											2020-07-01 13:16:37 +02:00
										 |  |  | 			   SND_PCM_NO_AUTO_CHANNELS | SND_PCM_NO_AUTO_FORMAT), "'%s': %s open failed", | 
					
						
							| 
									
										
										
										
											2021-09-02 12:33:44 +02:00
										 |  |  | 			device_name, | 
					
						
							| 
									
										
										
										
											2020-07-01 13:16:37 +02:00
										 |  |  | 			state->stream == SND_PCM_STREAM_CAPTURE ? "capture" : "playback"); | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 	if (!state->disable_tsched) { | 
					
						
							|  |  |  | 		if ((err = spa_system_timerfd_create(state->data_system, | 
					
						
							|  |  |  | 				CLOCK_MONOTONIC, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK)) < 0) | 
					
						
							|  |  |  | 			goto error_exit_close; | 
					
						
							| 
									
										
										
										
											2019-06-20 17:31:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 		state->timerfd = err; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		/* ALSA pollfds may only be ready after setting swparams, so
 | 
					
						
							|  |  |  | 		 * these are initialised in spa_alsa_start() */ | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	state->opened = true; | 
					
						
							| 
									
										
										
										
											2018-07-19 13:30:27 +02:00
										 |  |  | 	state->sample_count = 0; | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	state->sample_time = 0; | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 17:21:36 +03:00
										 |  |  | 	probe_pitch_ctl(state); | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2019-06-18 10:47:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-20 17:31:29 +02:00
										 |  |  | error_exit_close: | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 	spa_log_info(state->log, "%p: Device '%s' closing: %s", state, state->name, | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | 			spa_strerror(err)); | 
					
						
							| 
									
										
										
										
											2019-06-18 10:47:12 -04:00
										 |  |  | 	snd_pcm_close(state->hndl); | 
					
						
							|  |  |  | 	return err; | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | static void try_unlink(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct state *follower; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state->driver != NULL && state->linked) { | 
					
						
							|  |  |  | 		snd_pcm_unlink(state->hndl); | 
					
						
							|  |  |  | 		spa_log_info(state->log, "%p: unlinked from driver %p", | 
					
						
							|  |  |  | 				state, state->driver); | 
					
						
							|  |  |  | 		state->linked = false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	spa_list_for_each(follower, &state->followers, driver_link) { | 
					
						
							|  |  |  | 		if (follower->opened && follower->linked) { | 
					
						
							|  |  |  | 			snd_pcm_unlink(follower->hndl); | 
					
						
							|  |  |  | 			spa_log_info(state->log, "%p: follower unlinked from driver %p", | 
					
						
							|  |  |  | 				follower, state); | 
					
						
							|  |  |  | 			follower->linked = false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | int spa_alsa_close(struct state *state) | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	int err = 0; | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	if (!state->opened) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | 	try_unlink(state); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:20:27 +01:00
										 |  |  | 	spa_alsa_pause(state); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 	spa_log_info(state->log, "%p: Device '%s' closing", state, state->name); | 
					
						
							| 
									
										
										
										
											2020-11-25 12:35:33 +01:00
										 |  |  | 	if ((err = snd_pcm_close(state->hndl)) < 0) | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 		spa_log_warn(state->log, "%s: close failed: %s", state->name, | 
					
						
							| 
									
										
										
										
											2020-11-25 12:35:33 +01:00
										 |  |  | 				snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 	if (!state->disable_tsched) | 
					
						
							|  |  |  | 		spa_system_close(state->data_system, state->timerfd); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		state->n_fds = 0; | 
					
						
							| 
									
										
										
										
											2020-11-25 12:35:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 10:42:58 -05:00
										 |  |  | 	if (state->have_format && state->card) | 
					
						
							| 
									
										
										
										
											2021-11-23 12:20:27 +01:00
										 |  |  | 		state->card->format_ref--; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	state->have_format = false; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	state->opened = false; | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | 	state->linked = false; | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | 	if (state->pitch_elem) { | 
					
						
							|  |  |  | 		snd_ctl_elem_value_free(state->pitch_elem); | 
					
						
							|  |  |  | 		state->pitch_elem = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-07 14:02:26 -05:00
										 |  |  | 		// Close it unless we've got some bind_ctls we're listening to
 | 
					
						
							|  |  |  | 		if (state->ctl_n_fds == 0) { | 
					
						
							|  |  |  | 			snd_ctl_close(state->ctl); | 
					
						
							|  |  |  | 			state->ctl = NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	return err; | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 13:28:15 +02:00
										 |  |  | struct format_info { | 
					
						
							| 
									
										
										
										
											2018-08-23 17:47:57 +02:00
										 |  |  | 	uint32_t spa_format; | 
					
						
							| 
									
										
										
										
											2018-09-13 17:03:56 +02:00
										 |  |  | 	uint32_t spa_pformat; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	snd_pcm_format_t format; | 
					
						
							| 
									
										
										
										
											2017-05-25 13:28:15 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-03-21 16:50:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | static const struct format_info format_info[] = { | 
					
						
							| 
									
										
										
										
											2018-09-13 17:03:56 +02:00
										 |  |  | 	{ SPA_AUDIO_FORMAT_UNKNOWN, SPA_AUDIO_FORMAT_UNKNOWN, SND_PCM_FORMAT_UNKNOWN}, | 
					
						
							| 
									
										
										
										
											2019-03-20 13:05:16 +01:00
										 |  |  | 	{ SPA_AUDIO_FORMAT_F32_LE, SPA_AUDIO_FORMAT_F32P, SND_PCM_FORMAT_FLOAT_LE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_F32_BE, SPA_AUDIO_FORMAT_F32P, SND_PCM_FORMAT_FLOAT_BE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_S32_LE, SPA_AUDIO_FORMAT_S32P, SND_PCM_FORMAT_S32_LE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_S32_BE, SPA_AUDIO_FORMAT_S32P, SND_PCM_FORMAT_S32_BE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_S24_32_LE, SPA_AUDIO_FORMAT_S24_32P, SND_PCM_FORMAT_S24_LE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_S24_32_BE, SPA_AUDIO_FORMAT_S24_32P, SND_PCM_FORMAT_S24_BE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_S24_LE, SPA_AUDIO_FORMAT_S24P, SND_PCM_FORMAT_S24_3LE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_S24_BE, SPA_AUDIO_FORMAT_S24P, SND_PCM_FORMAT_S24_3BE}, | 
					
						
							| 
									
										
										
										
											2020-12-06 10:36:31 +01:00
										 |  |  | 	{ SPA_AUDIO_FORMAT_S16_LE, SPA_AUDIO_FORMAT_S16P, SND_PCM_FORMAT_S16_LE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_S16_BE, SPA_AUDIO_FORMAT_S16P, SND_PCM_FORMAT_S16_BE}, | 
					
						
							| 
									
										
										
										
											2019-03-20 13:05:16 +01:00
										 |  |  | 	{ SPA_AUDIO_FORMAT_S8, SPA_AUDIO_FORMAT_UNKNOWN, SND_PCM_FORMAT_S8}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_U8, SPA_AUDIO_FORMAT_U8P, SND_PCM_FORMAT_U8}, | 
					
						
							| 
									
										
										
										
											2018-09-13 17:03:56 +02:00
										 |  |  | 	{ SPA_AUDIO_FORMAT_U16_LE, SPA_AUDIO_FORMAT_UNKNOWN, SND_PCM_FORMAT_U16_LE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_U16_BE, SPA_AUDIO_FORMAT_UNKNOWN, SND_PCM_FORMAT_U16_BE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_U24_32_LE, SPA_AUDIO_FORMAT_UNKNOWN, SND_PCM_FORMAT_U24_LE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_U24_32_BE, SPA_AUDIO_FORMAT_UNKNOWN, SND_PCM_FORMAT_U24_BE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_U24_LE, SPA_AUDIO_FORMAT_UNKNOWN, SND_PCM_FORMAT_U24_3LE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_U24_BE, SPA_AUDIO_FORMAT_UNKNOWN, SND_PCM_FORMAT_U24_3BE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_U32_LE, SPA_AUDIO_FORMAT_UNKNOWN, SND_PCM_FORMAT_U32_LE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_U32_BE, SPA_AUDIO_FORMAT_UNKNOWN, SND_PCM_FORMAT_U32_BE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_F64_LE, SPA_AUDIO_FORMAT_F64P, SND_PCM_FORMAT_FLOAT64_LE}, | 
					
						
							|  |  |  | 	{ SPA_AUDIO_FORMAT_F64_BE, SPA_AUDIO_FORMAT_F64P, SND_PCM_FORMAT_FLOAT64_BE}, | 
					
						
							| 
									
										
										
										
											2017-03-21 16:50:44 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-01 11:42:10 +02:00
										 |  |  | static snd_pcm_format_t spa_format_to_alsa(uint32_t format, bool *planar) | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-30 16:21:28 +02:00
										 |  |  | 	SPA_FOR_EACH_ELEMENT_VAR(format_info, i) { | 
					
						
							|  |  |  | 		*planar = i->spa_pformat == format; | 
					
						
							|  |  |  | 		if (i->spa_format == format || *planar) | 
					
						
							|  |  |  | 			return i->format; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return SND_PCM_FORMAT_UNKNOWN; | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 17:03:56 +02:00
										 |  |  | struct chmap_info { | 
					
						
							|  |  |  | 	enum snd_pcm_chmap_position pos; | 
					
						
							|  |  |  | 	enum spa_audio_channel channel; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-09-11 18:09:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 17:03:56 +02:00
										 |  |  | static const struct chmap_info chmap_info[] = { | 
					
						
							|  |  |  | 	[SND_CHMAP_UNKNOWN] = { SND_CHMAP_UNKNOWN, SPA_AUDIO_CHANNEL_UNKNOWN }, | 
					
						
							|  |  |  | 	[SND_CHMAP_NA] = { SND_CHMAP_NA, SPA_AUDIO_CHANNEL_NA }, | 
					
						
							|  |  |  | 	[SND_CHMAP_MONO] = { SND_CHMAP_MONO, SPA_AUDIO_CHANNEL_MONO }, | 
					
						
							|  |  |  | 	[SND_CHMAP_FL] = { SND_CHMAP_FL, SPA_AUDIO_CHANNEL_FL }, | 
					
						
							|  |  |  | 	[SND_CHMAP_FR] = { SND_CHMAP_FR, SPA_AUDIO_CHANNEL_FR }, | 
					
						
							|  |  |  | 	[SND_CHMAP_RL] = { SND_CHMAP_RL, SPA_AUDIO_CHANNEL_RL }, | 
					
						
							|  |  |  | 	[SND_CHMAP_RR] = { SND_CHMAP_RR, SPA_AUDIO_CHANNEL_RR }, | 
					
						
							|  |  |  | 	[SND_CHMAP_FC] = { SND_CHMAP_FC, SPA_AUDIO_CHANNEL_FC }, | 
					
						
							|  |  |  | 	[SND_CHMAP_LFE] = { SND_CHMAP_LFE, SPA_AUDIO_CHANNEL_LFE }, | 
					
						
							|  |  |  | 	[SND_CHMAP_SL] = { SND_CHMAP_SL, SPA_AUDIO_CHANNEL_SL }, | 
					
						
							|  |  |  | 	[SND_CHMAP_SR] = { SND_CHMAP_SR, SPA_AUDIO_CHANNEL_SR }, | 
					
						
							|  |  |  | 	[SND_CHMAP_RC] = { SND_CHMAP_RC, SPA_AUDIO_CHANNEL_RC }, | 
					
						
							|  |  |  | 	[SND_CHMAP_FLC] = { SND_CHMAP_FLC, SPA_AUDIO_CHANNEL_FLC }, | 
					
						
							|  |  |  | 	[SND_CHMAP_FRC] = { SND_CHMAP_FRC, SPA_AUDIO_CHANNEL_FRC }, | 
					
						
							|  |  |  | 	[SND_CHMAP_RLC] = { SND_CHMAP_RLC, SPA_AUDIO_CHANNEL_RLC }, | 
					
						
							|  |  |  | 	[SND_CHMAP_RRC] = { SND_CHMAP_RRC, SPA_AUDIO_CHANNEL_RRC }, | 
					
						
							|  |  |  | 	[SND_CHMAP_FLW] = { SND_CHMAP_FLW, SPA_AUDIO_CHANNEL_FLW }, | 
					
						
							|  |  |  | 	[SND_CHMAP_FRW] = { SND_CHMAP_FRW, SPA_AUDIO_CHANNEL_FRW }, | 
					
						
							|  |  |  | 	[SND_CHMAP_FLH] = { SND_CHMAP_FLH, SPA_AUDIO_CHANNEL_FLH }, | 
					
						
							|  |  |  | 	[SND_CHMAP_FCH] = { SND_CHMAP_FCH, SPA_AUDIO_CHANNEL_FCH }, | 
					
						
							|  |  |  | 	[SND_CHMAP_FRH] = { SND_CHMAP_FRH, SPA_AUDIO_CHANNEL_FRH }, | 
					
						
							|  |  |  | 	[SND_CHMAP_TC] = { SND_CHMAP_TC, SPA_AUDIO_CHANNEL_TC }, | 
					
						
							|  |  |  | 	[SND_CHMAP_TFL] = { SND_CHMAP_TFL, SPA_AUDIO_CHANNEL_TFL }, | 
					
						
							|  |  |  | 	[SND_CHMAP_TFR] = { SND_CHMAP_TFR, SPA_AUDIO_CHANNEL_TFR }, | 
					
						
							|  |  |  | 	[SND_CHMAP_TFC] = { SND_CHMAP_TFC, SPA_AUDIO_CHANNEL_TFC }, | 
					
						
							|  |  |  | 	[SND_CHMAP_TRL] = { SND_CHMAP_TRL, SPA_AUDIO_CHANNEL_TRL }, | 
					
						
							|  |  |  | 	[SND_CHMAP_TRR] = { SND_CHMAP_TRR, SPA_AUDIO_CHANNEL_TRR }, | 
					
						
							|  |  |  | 	[SND_CHMAP_TRC] = { SND_CHMAP_TRC, SPA_AUDIO_CHANNEL_TRC }, | 
					
						
							|  |  |  | 	[SND_CHMAP_TFLC] = { SND_CHMAP_TFLC, SPA_AUDIO_CHANNEL_TFLC }, | 
					
						
							|  |  |  | 	[SND_CHMAP_TFRC] = { SND_CHMAP_TFRC, SPA_AUDIO_CHANNEL_TFRC }, | 
					
						
							|  |  |  | 	[SND_CHMAP_TSL] = { SND_CHMAP_TSL, SPA_AUDIO_CHANNEL_TSL }, | 
					
						
							|  |  |  | 	[SND_CHMAP_TSR] = { SND_CHMAP_TSR, SPA_AUDIO_CHANNEL_TSR }, | 
					
						
							|  |  |  | 	[SND_CHMAP_LLFE] = { SND_CHMAP_LLFE, SPA_AUDIO_CHANNEL_LLFE }, | 
					
						
							|  |  |  | 	[SND_CHMAP_RLFE] = { SND_CHMAP_RLFE, SPA_AUDIO_CHANNEL_RLFE }, | 
					
						
							|  |  |  | 	[SND_CHMAP_BC] = { SND_CHMAP_BC, SPA_AUDIO_CHANNEL_BC }, | 
					
						
							|  |  |  | 	[SND_CHMAP_BLC] = { SND_CHMAP_BLC, SPA_AUDIO_CHANNEL_BLC }, | 
					
						
							|  |  |  | 	[SND_CHMAP_BRC] = { SND_CHMAP_BRC, SPA_AUDIO_CHANNEL_BRC }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 12:44:05 +02:00
										 |  |  | #define _M(ch)	(1LL << SND_CHMAP_ ##ch)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct def_mask { | 
					
						
							|  |  |  | 	int channels; | 
					
						
							|  |  |  | 	uint64_t mask; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const struct def_mask default_layouts[] = { | 
					
						
							|  |  |  | 	{ 0, 0 }, | 
					
						
							|  |  |  | 	{ 1, _M(MONO) }, | 
					
						
							|  |  |  | 	{ 2, _M(FL) | _M(FR) }, | 
					
						
							|  |  |  | 	{ 3, _M(FL) | _M(FR) | _M(LFE) }, | 
					
						
							|  |  |  | 	{ 4, _M(FL) | _M(FR) | _M(RL) |_M(RR) }, | 
					
						
							|  |  |  | 	{ 5, _M(FL) | _M(FR) | _M(RL) |_M(RR) | _M(FC) }, | 
					
						
							|  |  |  | 	{ 6, _M(FL) | _M(FR) | _M(RL) |_M(RR) | _M(FC) | _M(LFE) }, | 
					
						
							|  |  |  | 	{ 7, _M(FL) | _M(FR) | _M(RL) |_M(RR) | _M(SL) | _M(SR) | _M(FC) }, | 
					
						
							|  |  |  | 	{ 8, _M(FL) | _M(FR) | _M(RL) |_M(RR) | _M(SL) | _M(SR) | _M(FC) | _M(LFE) }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-01 12:41:57 +02:00
										 |  |  | #define _C(ch)	(SPA_AUDIO_CHANNEL_ ##ch)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-28 14:24:59 +01:00
										 |  |  | static const struct channel_map default_map[] = { | 
					
						
							| 
									
										
										
										
											2020-07-01 12:41:57 +02:00
										 |  |  | 	{ 0, { 0, } } , | 
					
						
							|  |  |  | 	{ 1, { _C(MONO), } }, | 
					
						
							|  |  |  | 	{ 2, { _C(FL), _C(FR), } }, | 
					
						
							|  |  |  | 	{ 3, { _C(FL), _C(FR), _C(LFE) } }, | 
					
						
							|  |  |  | 	{ 4, { _C(FL), _C(FR), _C(RL), _C(RR), } }, | 
					
						
							|  |  |  | 	{ 5, { _C(FL), _C(FR), _C(RL), _C(RR), _C(FC) } }, | 
					
						
							|  |  |  | 	{ 6, { _C(FL), _C(FR), _C(RL), _C(RR), _C(FC), _C(LFE), } }, | 
					
						
							|  |  |  | 	{ 7, { _C(FL), _C(FR), _C(RL), _C(RR), _C(FC), _C(SL), _C(SR), } }, | 
					
						
							|  |  |  | 	{ 8, { _C(FL), _C(FR), _C(RL), _C(RR), _C(FC), _C(LFE), _C(SL), _C(SR), } }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 17:03:56 +02:00
										 |  |  | static enum spa_audio_channel chmap_position_to_channel(enum snd_pcm_chmap_position pos) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return chmap_info[pos].channel; | 
					
						
							| 
									
										
										
										
											2018-09-11 18:09:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 12:44:05 +02:00
										 |  |  | static void sanitize_map(snd_pcm_chmap_t* map) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	uint64_t mask = 0, p, dup = 0; | 
					
						
							|  |  |  | 	const struct def_mask *def; | 
					
						
							| 
									
										
										
										
											2019-01-07 15:52:42 +01:00
										 |  |  | 	uint32_t i, j, pos; | 
					
						
							| 
									
										
										
										
											2018-10-17 12:44:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < map->channels; i++) { | 
					
						
							| 
									
										
										
										
											2020-03-17 11:37:56 +01:00
										 |  |  | 		if (map->pos[i] > SND_CHMAP_LAST) | 
					
						
							| 
									
										
										
										
											2018-10-17 12:44:05 +02:00
										 |  |  | 			map->pos[i] = SND_CHMAP_UNKNOWN; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		p = 1LL << map->pos[i]; | 
					
						
							|  |  |  | 		if (mask & p) { | 
					
						
							|  |  |  | 			/* duplicate channel */ | 
					
						
							|  |  |  | 			for (j = 0; j <= i; j++) | 
					
						
							|  |  |  | 				if (map->pos[j] == map->pos[i]) | 
					
						
							|  |  |  | 					map->pos[j] = SND_CHMAP_UNKNOWN; | 
					
						
							|  |  |  | 			dup |= p; | 
					
						
							|  |  |  | 			p = 1LL << SND_CHMAP_UNKNOWN; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		mask |= p; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if ((mask & (1LL << SND_CHMAP_UNKNOWN)) == 0) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	def = &default_layouts[map->channels]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* remove duplicates */ | 
					
						
							|  |  |  | 	mask &= ~dup; | 
					
						
							|  |  |  | 	/* keep unassigned channels */ | 
					
						
							|  |  |  | 	mask = def->mask & ~mask; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pos = 0; | 
					
						
							|  |  |  | 	for (i = 0; i < map->channels; i++) { | 
					
						
							|  |  |  | 		if (map->pos[i] == SND_CHMAP_UNKNOWN) { | 
					
						
							|  |  |  | 			do { | 
					
						
							|  |  |  | 				mask >>= 1; | 
					
						
							|  |  |  | 				pos++; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			while (mask != 0 && (mask & 1) == 0); | 
					
						
							|  |  |  | 			map->pos[i] = mask ? pos : 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | static bool uint32_array_contains(uint32_t *vals, uint32_t n_vals, uint32_t val) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	uint32_t i; | 
					
						
							|  |  |  | 	for (i = 0; i < n_vals; i++) | 
					
						
							|  |  |  | 		if (vals[i] == val) | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 21:58:32 +02:00
										 |  |  | static int add_rate(struct state *state, uint32_t scale, uint32_t interleave, bool all, uint32_t index, uint32_t *next, | 
					
						
							| 
									
										
										
										
											2022-06-13 20:02:33 +02:00
										 |  |  | 		uint32_t min_allowed_rate, snd_pcm_hw_params_t *params, struct spa_pod_builder *b) | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct spa_pod_frame f[1]; | 
					
						
							|  |  |  | 	int err, dir; | 
					
						
							|  |  |  | 	unsigned int min, max; | 
					
						
							|  |  |  | 	struct spa_pod_choice *choice; | 
					
						
							|  |  |  | 	uint32_t rate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_get_rate_min(params, &min, &dir), "get_rate_min"); | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_get_rate_max(params, &max, &dir), "get_rate_max"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 21:58:32 +02:00
										 |  |  | 	spa_log_debug(state->log, "min:%u max:%u min-allowed:%u scale:%u interleave:%u all:%d", | 
					
						
							|  |  |  | 			min, max, min_allowed_rate, scale, interleave, all); | 
					
						
							| 
									
										
										
										
											2022-08-04 14:25:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 23:39:51 +02:00
										 |  |  | 	min = SPA_MAX(min_allowed_rate * scale / interleave, min) * interleave / scale; | 
					
						
							| 
									
										
										
										
											2022-09-07 21:58:32 +02:00
										 |  |  | 	max = max * interleave / scale; | 
					
						
							| 
									
										
										
										
											2022-09-07 19:58:30 +02:00
										 |  |  | 	if (max < min) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2022-06-13 20:02:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 10:42:58 -05:00
										 |  |  | 	if (!state->multi_rate && state->card && state->card->format_ref > 0) | 
					
						
							| 
									
										
										
										
											2021-11-23 11:28:41 +01:00
										 |  |  | 		rate = state->card->rate; | 
					
						
							| 
									
										
										
										
											2022-01-05 11:53:47 +01:00
										 |  |  | 	else | 
					
						
							|  |  |  | 		rate = state->default_rate; | 
					
						
							| 
									
										
										
										
											2021-11-23 11:28:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-05 11:53:47 +01:00
										 |  |  | 	if (rate < min || rate > max) | 
					
						
							|  |  |  | 		rate = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (rate != 0 && !all) | 
					
						
							|  |  |  | 		min = max = rate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (rate == 0) | 
					
						
							| 
									
										
										
										
											2023-09-01 10:02:28 +02:00
										 |  |  | 		rate = state->position ? state->position->clock.target_rate.denom : DEFAULT_RATE; | 
					
						
							| 
									
										
										
										
											2022-01-05 11:53:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	rate = SPA_CLAMP(rate, min, max); | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 10:42:58 -05:00
										 |  |  | 	spa_log_debug(state->log, "rate:%u multi:%d card:%u def:%d", | 
					
						
							|  |  |  | 			rate, state->multi_rate, state->card ? state->card->rate : 0, state->default_rate); | 
					
						
							| 
									
										
										
										
											2022-08-04 14:25:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 	spa_pod_builder_prop(b, SPA_FORMAT_AUDIO_rate, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_push_choice(b, &f[0], SPA_CHOICE_None, 0); | 
					
						
							|  |  |  | 	choice = (struct spa_pod_choice*)spa_pod_builder_frame(b, &f[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 	if (state->n_allowed_rates > 0) { | 
					
						
							|  |  |  | 		uint32_t i, v, last = 0, count = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-05 11:53:47 +01:00
										 |  |  | 		if (uint32_array_contains(state->allowed_rates, state->n_allowed_rates, rate)) { | 
					
						
							| 
									
										
										
										
											2022-09-07 21:42:45 +02:00
										 |  |  | 			spa_pod_builder_int(b, rate * scale); | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 			count++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		for (i = 0; i < state->n_allowed_rates; i++) { | 
					
						
							|  |  |  | 			v = SPA_CLAMP(state->allowed_rates[i], min, max); | 
					
						
							|  |  |  | 			if (v != last && | 
					
						
							|  |  |  | 			    uint32_array_contains(state->allowed_rates, state->n_allowed_rates, v)) { | 
					
						
							| 
									
										
										
										
											2022-09-07 21:42:45 +02:00
										 |  |  | 				spa_pod_builder_int(b, v * scale); | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 				if (count == 0) | 
					
						
							| 
									
										
										
										
											2022-09-07 21:42:45 +02:00
										 |  |  | 					spa_pod_builder_int(b, v * scale); | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 				count++; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			last = v; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (count > 1) | 
					
						
							|  |  |  | 			choice->body.type = SPA_CHOICE_Enum; | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2022-09-07 21:42:45 +02:00
										 |  |  | 		spa_pod_builder_int(b, rate * scale); | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (min != max) { | 
					
						
							| 
									
										
										
										
											2022-09-07 21:42:45 +02:00
										 |  |  | 			spa_pod_builder_int(b, min * scale); | 
					
						
							|  |  |  | 			spa_pod_builder_int(b, max * scale); | 
					
						
							| 
									
										
										
										
											2022-01-03 16:03:23 +01:00
										 |  |  | 			choice->body.type = SPA_CHOICE_Range; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	spa_pod_builder_pop(b, &f[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 13:02:48 +02:00
										 |  |  | static int add_channels(struct state *state, bool all, uint32_t index, uint32_t *next, | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 		snd_pcm_hw_params_t *params, struct spa_pod_builder *b) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct spa_pod_frame f[1]; | 
					
						
							|  |  |  | 	size_t i; | 
					
						
							|  |  |  | 	int err; | 
					
						
							|  |  |  | 	snd_pcm_t *hndl = state->hndl; | 
					
						
							|  |  |  | 	snd_pcm_chmap_query_t **maps; | 
					
						
							|  |  |  | 	unsigned int min, max; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_get_channels_min(params, &min), "get_channels_min"); | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_get_channels_max(params, &max), "get_channels_max"); | 
					
						
							| 
									
										
										
										
											2022-08-04 14:25:24 +02:00
										 |  |  | 	spa_log_debug(state->log, "channels (%d %d) default:%d all:%d", | 
					
						
							|  |  |  | 			min, max, state->default_channels, all); | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-20 15:33:17 +02:00
										 |  |  | 	min = SPA_MIN(min, MAX_CHANNELS); | 
					
						
							|  |  |  | 	max = SPA_MIN(max, MAX_CHANNELS); | 
					
						
							| 
									
										
										
										
											2025-05-08 16:01:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 13:02:48 +02:00
										 |  |  | 	if (state->default_channels != 0 && !all) { | 
					
						
							| 
									
										
										
										
											2025-05-08 18:16:53 +02:00
										 |  |  | 		if (min > state->default_channels || | 
					
						
							|  |  |  | 		    max < state->default_channels) | 
					
						
							|  |  |  | 			spa_log_warn(state->log, "given audio.channels %d out of range:%d-%d", | 
					
						
							|  |  |  | 					state->default_channels, min, max); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			min = max = state->default_channels; | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_prop(b, SPA_FORMAT_AUDIO_channels, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state->props.use_chmap && (maps = snd_pcm_query_chmaps(hndl)) != NULL) { | 
					
						
							|  |  |  | 		uint32_t channel; | 
					
						
							|  |  |  | 		snd_pcm_chmap_t* map; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | skip_channels: | 
					
						
							|  |  |  | 		if (maps[index] == NULL) { | 
					
						
							|  |  |  | 			snd_pcm_free_chmaps(maps); | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		map = &maps[index]->map; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		spa_log_debug(state->log, "map %d channels (%d %d)", map->channels, min, max); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (map->channels < min || map->channels > max) { | 
					
						
							|  |  |  | 			index = (*next)++; | 
					
						
							|  |  |  | 			goto skip_channels; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-25 10:26:11 +01:00
										 |  |  | 		spa_log_debug(state->log, "%p: using chmap", state); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 		sanitize_map(map); | 
					
						
							|  |  |  | 		spa_pod_builder_int(b, map->channels); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		spa_pod_builder_prop(b, SPA_FORMAT_AUDIO_position, 0); | 
					
						
							|  |  |  | 		spa_pod_builder_push_array(b, &f[0]); | 
					
						
							|  |  |  | 		for (i = 0; i < map->channels; i++) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 			spa_log_debug(state->log, "%p: position %zd %d", state, i, map->pos[i]); | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 			channel = chmap_position_to_channel(map->pos[i]); | 
					
						
							|  |  |  | 			spa_pod_builder_id(b, channel); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		spa_pod_builder_pop(b, &f[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		snd_pcm_free_chmaps(maps); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		if (index > 0) | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (min != max) { | 
					
						
							| 
									
										
										
										
											2025-05-08 18:17:45 +02:00
										 |  |  | 			spa_pod_builder_push_choice(b, &f[0], SPA_CHOICE_Range, 0); | 
					
						
							|  |  |  | 			spa_pod_builder_int(b, max); | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 			spa_pod_builder_int(b, min); | 
					
						
							|  |  |  | 			spa_pod_builder_int(b, max); | 
					
						
							| 
									
										
										
										
											2025-05-08 18:17:45 +02:00
										 |  |  | 			spa_pod_builder_pop(b, &f[0]); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			const struct channel_map *map = NULL; | 
					
						
							|  |  |  | 			spa_pod_builder_int(b, min); | 
					
						
							| 
									
										
										
										
											2025-10-22 12:47:00 +02:00
										 |  |  | 			if (state->default_pos.n_pos == min) { | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 				map = &state->default_pos; | 
					
						
							| 
									
										
										
										
											2024-01-25 10:26:11 +01:00
										 |  |  | 				spa_log_debug(state->log, "%p: using provided default", state); | 
					
						
							|  |  |  | 			} else if (min <= 8) { | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 				map = &default_map[min]; | 
					
						
							| 
									
										
										
										
											2024-01-25 10:26:11 +01:00
										 |  |  | 				spa_log_debug(state->log, "%p: using default %d channel map", state, min); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2025-05-08 18:17:45 +02:00
										 |  |  | 			if (map) { | 
					
						
							|  |  |  | 				spa_pod_builder_prop(b, SPA_FORMAT_AUDIO_position, 0); | 
					
						
							|  |  |  | 				spa_pod_builder_push_array(b, &f[0]); | 
					
						
							| 
									
										
										
										
											2025-10-22 12:47:00 +02:00
										 |  |  | 				for (i = 0; i < map->n_pos; i++) { | 
					
						
							| 
									
										
										
										
											2025-05-08 18:17:45 +02:00
										 |  |  | 					spa_log_debug(state->log, "%p: position %zd %d", state, i, map->pos[i]); | 
					
						
							|  |  |  | 					spa_pod_builder_id(b, map->pos[i]); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				spa_pod_builder_pop(b, &f[0]); | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | static void debug_hw_params(struct state *state, const char *prefix, snd_pcm_hw_params_t *params) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-05 14:09:07 +02:00
										 |  |  | 	if (SPA_UNLIKELY(spa_log_level_topic_enabled(state->log, SPA_LOG_TOPIC_DEFAULT, SPA_LOG_LEVEL_DEBUG))) { | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | 		spa_log_debug(state->log, "%s:", prefix); | 
					
						
							|  |  |  | 		snd_pcm_hw_params_dump(params, state->output); | 
					
						
							|  |  |  | 		fflush(state->log_file); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | static int enum_pcm_formats(struct state *state, uint32_t index, uint32_t *next, | 
					
						
							|  |  |  | 		struct spa_pod **result, struct spa_pod_builder *b) | 
					
						
							| 
									
										
										
										
											2017-04-04 19:44:00 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 	int res, err; | 
					
						
							| 
									
										
										
										
											2022-09-30 16:21:28 +02:00
										 |  |  | 	size_t j; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	snd_pcm_t *hndl; | 
					
						
							|  |  |  | 	snd_pcm_hw_params_t *params; | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 	struct spa_pod_frame f[2]; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	snd_pcm_format_mask_t *fmask; | 
					
						
							| 
									
										
										
										
											2018-04-19 20:10:07 +02:00
										 |  |  | 	snd_pcm_access_mask_t *amask; | 
					
						
							| 
									
										
										
										
											2021-02-23 17:17:14 +01:00
										 |  |  | 	unsigned int rrate, rchannels; | 
					
						
							| 
									
										
										
										
											2018-09-05 16:41:07 +02:00
										 |  |  | 	struct spa_pod_choice *choice; | 
					
						
							| 
									
										
										
										
											2017-11-14 10:17:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	hndl = state->hndl; | 
					
						
							|  |  |  | 	snd_pcm_hw_params_alloca(¶ms); | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_any(hndl, params), "Broken configuration: no configurations available"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | 	debug_hw_params(state, __func__, params); | 
					
						
							| 
									
										
										
										
											2022-08-05 13:00:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-23 17:17:14 +01:00
										 |  |  | 	CHECK(snd_pcm_hw_params_set_rate_resample(hndl, params, 0), "set_rate_resample"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state->default_channels != 0) { | 
					
						
							|  |  |  | 		rchannels = state->default_channels; | 
					
						
							|  |  |  | 		CHECK(snd_pcm_hw_params_set_channels_near(hndl, params, &rchannels), "set_channels"); | 
					
						
							|  |  |  | 		if (state->default_channels != rchannels) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 			spa_log_warn(state->log, "%s: Channels doesn't match (requested %u, got %u)", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 				state->name, state->default_channels, rchannels); | 
					
						
							| 
									
										
										
										
											2021-02-23 17:17:14 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (state->default_rate != 0) { | 
					
						
							|  |  |  | 		rrate = state->default_rate; | 
					
						
							|  |  |  | 		CHECK(snd_pcm_hw_params_set_rate_near(hndl, params, &rrate, 0), "set_rate_near"); | 
					
						
							|  |  |  | 		if (state->default_rate != rrate) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 			spa_log_warn(state->log, "%s: Rate doesn't match (requested %u, got %u)", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 				state->name, state->default_rate, rrate); | 
					
						
							| 
									
										
										
										
											2021-02-23 17:17:14 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 	spa_pod_builder_push_object(b, &f[0], SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat); | 
					
						
							|  |  |  | 	spa_pod_builder_add(b, | 
					
						
							| 
									
										
										
										
											2019-01-16 11:05:12 +01:00
										 |  |  | 			SPA_FORMAT_mediaType,    SPA_POD_Id(SPA_MEDIA_TYPE_audio), | 
					
						
							|  |  |  | 			SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw), | 
					
						
							| 
									
										
										
										
											2018-09-05 16:41:07 +02:00
										 |  |  | 			0); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	snd_pcm_format_mask_alloca(&fmask); | 
					
						
							|  |  |  | 	snd_pcm_hw_params_get_format_mask(params, fmask); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 17:03:56 +02:00
										 |  |  | 	snd_pcm_access_mask_alloca(&amask); | 
					
						
							|  |  |  | 	snd_pcm_hw_params_get_access_mask(params, amask); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 	spa_pod_builder_prop(b, SPA_FORMAT_AUDIO_format, 0); | 
					
						
							| 
									
										
										
										
											2018-09-05 16:41:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 	spa_pod_builder_push_choice(b, &f[1], SPA_CHOICE_None, 0); | 
					
						
							|  |  |  | 	choice = (struct spa_pod_choice*)spa_pod_builder_frame(b, &f[1]); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-30 16:21:28 +02:00
										 |  |  | 	j = 0; | 
					
						
							|  |  |  | 	SPA_FOR_EACH_ELEMENT_VAR(format_info, fi) { | 
					
						
							|  |  |  | 		if (fi->format == SND_PCM_FORMAT_UNKNOWN) | 
					
						
							|  |  |  | 			continue; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (snd_pcm_format_mask_test(fmask, fi->format)) { | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 			if ((snd_pcm_access_mask_test(amask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED) || | 
					
						
							|  |  |  | 			    snd_pcm_access_mask_test(amask, SND_PCM_ACCESS_RW_NONINTERLEAVED)) && | 
					
						
							| 
									
										
										
										
											2020-12-09 20:33:57 +01:00
										 |  |  | 			    fi->spa_pformat != SPA_AUDIO_FORMAT_UNKNOWN && | 
					
						
							|  |  |  | 			    (state->default_format == 0 || state->default_format == fi->spa_pformat)) { | 
					
						
							| 
									
										
										
										
											2018-09-13 17:03:56 +02:00
										 |  |  | 				if (j++ == 0) | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 					spa_pod_builder_id(b, fi->spa_pformat); | 
					
						
							|  |  |  | 				spa_pod_builder_id(b, fi->spa_pformat); | 
					
						
							| 
									
										
										
										
											2018-09-13 17:03:56 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-01-15 15:18:39 +01:00
										 |  |  | 			if ((snd_pcm_access_mask_test(amask, SND_PCM_ACCESS_MMAP_INTERLEAVED) || | 
					
						
							|  |  |  | 			    snd_pcm_access_mask_test(amask, SND_PCM_ACCESS_RW_INTERLEAVED)) && | 
					
						
							|  |  |  | 			    (state->default_format == 0 || state->default_format == fi->spa_format)) { | 
					
						
							|  |  |  | 				if (j++ == 0) | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 					spa_pod_builder_id(b, fi->spa_format); | 
					
						
							|  |  |  | 				spa_pod_builder_id(b, fi->spa_format); | 
					
						
							| 
									
										
										
										
											2021-01-15 15:18:39 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-04-20 17:52:27 +02:00
										 |  |  | 	if (j > 1) | 
					
						
							|  |  |  | 		choice->body.type = SPA_CHOICE_Enum; | 
					
						
							|  |  |  | 	spa_pod_builder_pop(b, &f[1]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-03 08:52:59 +01:00
										 |  |  | 	if (j == 0) { | 
					
						
							|  |  |  | 		char buf[1024]; | 
					
						
							| 
									
										
										
										
											2021-07-06 17:56:04 +02:00
										 |  |  | 		int i, r, offs; | 
					
						
							| 
									
										
										
										
											2021-01-03 08:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for (i = 0, offs = 0; i <= SND_PCM_FORMAT_LAST; i++) { | 
					
						
							| 
									
										
										
										
											2021-07-06 17:56:04 +02:00
										 |  |  | 			if (snd_pcm_format_mask_test(fmask, (snd_pcm_format_t)i)) { | 
					
						
							|  |  |  | 				r = snprintf(&buf[offs], sizeof(buf) - offs, | 
					
						
							| 
									
										
										
										
											2021-01-03 08:52:59 +01:00
										 |  |  | 						"%s ", snd_pcm_format_name((snd_pcm_format_t)i)); | 
					
						
							| 
									
										
										
										
											2021-07-06 17:56:04 +02:00
										 |  |  | 				if (r < 0 || r + offs >= (int)sizeof(buf)) | 
					
						
							|  |  |  | 					return -ENOSPC; | 
					
						
							|  |  |  | 				offs += r; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-01-03 08:52:59 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-11-24 17:25:59 +01:00
										 |  |  | 		spa_log_warn(state->log, "%s: no format found (def:%d) formats:%s", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 				state->name, state->default_format, buf); | 
					
						
							| 
									
										
										
										
											2021-01-03 08:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for (i = 0, offs = 0; i <= SND_PCM_ACCESS_LAST; i++) { | 
					
						
							| 
									
										
										
										
											2021-07-06 17:56:04 +02:00
										 |  |  | 			if (snd_pcm_access_mask_test(amask, (snd_pcm_access_t)i)) { | 
					
						
							|  |  |  | 				r = snprintf(&buf[offs], sizeof(buf) - offs, | 
					
						
							| 
									
										
										
										
											2021-01-03 08:52:59 +01:00
										 |  |  | 						"%s ", snd_pcm_access_name((snd_pcm_access_t)i)); | 
					
						
							| 
									
										
										
										
											2021-07-06 17:56:04 +02:00
										 |  |  | 				if (r < 0 || r + offs >= (int)sizeof(buf)) | 
					
						
							|  |  |  | 					return -ENOSPC; | 
					
						
							|  |  |  | 				offs += r; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-01-03 08:52:59 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 		spa_log_warn(state->log, "%s: access:%s", state->name, buf); | 
					
						
							| 
									
										
										
										
											2021-01-03 08:52:59 +01:00
										 |  |  | 		return -ENOTSUP; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-19 20:10:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 21:58:32 +02:00
										 |  |  | 	if ((res = add_rate(state, 1, 1, false, index & 0xffff, next, 0, params, b)) != 1) | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2018-09-11 18:09:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 13:02:48 +02:00
										 |  |  | 	if ((res = add_channels(state, false, index & 0xffff, next, params, b)) != 1) | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2018-09-11 18:09:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 	*result = spa_pod_builder_pop(b, &f[0]); | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-09-11 18:09:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-23 17:01:36 +02:00
										 |  |  | static bool codec_supported(uint32_t codec, unsigned int chmax, unsigned int rmax) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	switch (codec) { | 
					
						
							|  |  |  | 	case SPA_AUDIO_IEC958_CODEC_PCM: | 
					
						
							|  |  |  | 	case SPA_AUDIO_IEC958_CODEC_DTS: | 
					
						
							|  |  |  | 	case SPA_AUDIO_IEC958_CODEC_AC3: | 
					
						
							|  |  |  | 	case SPA_AUDIO_IEC958_CODEC_MPEG: | 
					
						
							|  |  |  | 	case SPA_AUDIO_IEC958_CODEC_MPEG2_AAC: | 
					
						
							|  |  |  | 		if (chmax >= 2) | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case SPA_AUDIO_IEC958_CODEC_EAC3: | 
					
						
							|  |  |  | 		if (rmax >= 48000 * 4 && chmax >= 2) | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case SPA_AUDIO_IEC958_CODEC_TRUEHD: | 
					
						
							|  |  |  | 	case SPA_AUDIO_IEC958_CODEC_DTSHD: | 
					
						
							|  |  |  | 		if (chmax >= 8) | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | static int enum_iec958_formats(struct state *state, uint32_t index, uint32_t *next, | 
					
						
							|  |  |  | 		struct spa_pod **result, struct spa_pod_builder *b) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 	int res, err, dir; | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 	snd_pcm_t *hndl; | 
					
						
							|  |  |  | 	snd_pcm_hw_params_t *params; | 
					
						
							|  |  |  | 	struct spa_pod_frame f[2]; | 
					
						
							|  |  |  | 	unsigned int rmin, rmax; | 
					
						
							|  |  |  | 	unsigned int chmin, chmax; | 
					
						
							| 
									
										
										
										
											2021-08-23 17:01:36 +02:00
										 |  |  | 	uint32_t i, c, codecs[16], n_codecs; | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ((index & 0xffff) > 0) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!(state->is_iec958 || state->is_hdmi)) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	if (state->iec958_codecs == 0) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	hndl = state->hndl; | 
					
						
							|  |  |  | 	snd_pcm_hw_params_alloca(¶ms); | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_any(hndl, params), "Broken configuration: no configurations available"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | 	debug_hw_params(state, __func__, params); | 
					
						
							| 
									
										
										
										
											2022-08-05 13:00:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 	CHECK(snd_pcm_hw_params_set_rate_resample(hndl, params, 0), "set_rate_resample"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_push_object(b, &f[0], SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat); | 
					
						
							|  |  |  | 	spa_pod_builder_add(b, | 
					
						
							|  |  |  | 			SPA_FORMAT_mediaType,    SPA_POD_Id(SPA_MEDIA_TYPE_audio), | 
					
						
							|  |  |  | 			SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_iec958), | 
					
						
							|  |  |  | 			0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_get_channels_min(params, &chmin), "get_channels_min"); | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_get_channels_max(params, &chmax), "get_channels_max"); | 
					
						
							|  |  |  | 	spa_log_debug(state->log, "channels (%d %d)", chmin, chmax); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_get_rate_min(params, &rmin, &dir), "get_rate_min"); | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_get_rate_max(params, &rmax, &dir), "get_rate_max"); | 
					
						
							|  |  |  | 	spa_log_debug(state->log, "rate (%d %d)", rmin, rmax); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state->default_rate != 0) { | 
					
						
							| 
									
										
										
										
											2025-05-08 18:16:53 +02:00
										 |  |  | 		if (rmin > state->default_rate || | 
					
						
							|  |  |  | 		    rmax < state->default_rate) | 
					
						
							|  |  |  | 			spa_log_warn(state->log, "given audio.rate %d out of range:%d-%d", | 
					
						
							|  |  |  | 					state->default_rate, rmin, rmax); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			rmin = rmax = state->default_rate; | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_prop(b, SPA_FORMAT_AUDIO_iec958Codec, 0); | 
					
						
							|  |  |  | 	spa_pod_builder_push_choice(b, &f[1], SPA_CHOICE_Enum, 0); | 
					
						
							| 
									
										
										
										
											2021-08-23 17:01:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	n_codecs = spa_alsa_get_iec958_codecs(state, codecs, SPA_N_ELEMENTS(codecs)); | 
					
						
							|  |  |  | 	for (i = 0, c = 0; i < n_codecs; i++) { | 
					
						
							|  |  |  | 		if (!codec_supported(codecs[i], chmax, rmax)) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		if (c++ == 0) | 
					
						
							|  |  |  | 			spa_pod_builder_id(b, codecs[i]); | 
					
						
							|  |  |  | 		spa_pod_builder_id(b, codecs[i]); | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	spa_pod_builder_pop(b, &f[1]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 21:58:32 +02:00
										 |  |  | 	if ((res = add_rate(state, 1, 1, true, index & 0xffff, next, 0, params, b)) != 1) | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	(*next)++; | 
					
						
							|  |  |  | 	*result = spa_pod_builder_pop(b, &f[0]); | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | static int enum_dsd_formats(struct state *state, uint32_t index, uint32_t *next, | 
					
						
							|  |  |  | 		struct spa_pod **result, struct spa_pod_builder *b) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res, err; | 
					
						
							|  |  |  | 	snd_pcm_t *hndl; | 
					
						
							|  |  |  | 	snd_pcm_hw_params_t *params; | 
					
						
							| 
									
										
										
										
											2021-09-17 18:00:31 +02:00
										 |  |  | 	snd_pcm_format_mask_t *fmask; | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 	struct spa_pod_frame f[2]; | 
					
						
							| 
									
										
										
										
											2021-09-17 18:04:21 +02:00
										 |  |  | 	int32_t interleave; | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ((index & 0xffff) > 0) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	hndl = state->hndl; | 
					
						
							|  |  |  | 	snd_pcm_hw_params_alloca(¶ms); | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_any(hndl, params), "Broken configuration: no configurations available"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | 	debug_hw_params(state, __func__, params); | 
					
						
							| 
									
										
										
										
											2022-08-05 13:00:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-17 18:00:31 +02:00
										 |  |  | 	snd_pcm_format_mask_alloca(&fmask); | 
					
						
							|  |  |  | 	snd_pcm_hw_params_get_format_mask(params, fmask); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-17 18:04:21 +02:00
										 |  |  | 	if (snd_pcm_format_mask_test(fmask, SND_PCM_FORMAT_DSD_U32_BE)) | 
					
						
							|  |  |  | 		interleave = 4; | 
					
						
							|  |  |  | 	else if (snd_pcm_format_mask_test(fmask, SND_PCM_FORMAT_DSD_U32_LE)) | 
					
						
							|  |  |  | 		interleave = -4; | 
					
						
							|  |  |  | 	else if (snd_pcm_format_mask_test(fmask, SND_PCM_FORMAT_DSD_U16_BE)) | 
					
						
							|  |  |  | 		interleave = 2; | 
					
						
							|  |  |  | 	else if (snd_pcm_format_mask_test(fmask, SND_PCM_FORMAT_DSD_U16_LE)) | 
					
						
							|  |  |  | 		interleave = -2; | 
					
						
							|  |  |  | 	else if (snd_pcm_format_mask_test(fmask, SND_PCM_FORMAT_DSD_U8)) | 
					
						
							|  |  |  | 		interleave = 1; | 
					
						
							|  |  |  | 	else | 
					
						
							| 
									
										
										
										
											2021-09-17 18:00:31 +02:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 	CHECK(snd_pcm_hw_params_set_rate_resample(hndl, params, 0), "set_rate_resample"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_push_object(b, &f[0], SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat); | 
					
						
							|  |  |  | 	spa_pod_builder_add(b, | 
					
						
							|  |  |  | 			SPA_FORMAT_mediaType,    SPA_POD_Id(SPA_MEDIA_TYPE_audio), | 
					
						
							|  |  |  | 			SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_dsd), | 
					
						
							|  |  |  | 			0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_prop(b, SPA_FORMAT_AUDIO_bitorder, 0); | 
					
						
							| 
									
										
										
										
											2024-11-27 16:23:24 +01:00
										 |  |  | 	spa_pod_builder_id(b, state->dsd_lsb ? SPA_PARAM_BITORDER_lsb : SPA_PARAM_BITORDER_msb); | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-17 18:04:21 +02:00
										 |  |  | 	spa_pod_builder_prop(b, SPA_FORMAT_AUDIO_interleave, 0); | 
					
						
							|  |  |  | 	spa_pod_builder_int(b, interleave); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-13 20:02:33 +02:00
										 |  |  | 	/* Use a lower rate limit of 352800 (= 44100 * 64 / 8). This is because in
 | 
					
						
							|  |  |  | 	 * PipeWire, DSD rates are given in bytes, not bits, so 352800 corresponds | 
					
						
							|  |  |  | 	 * to the bit rate of DSD64. (The "64" in DSD64 means "64 times the rate | 
					
						
							|  |  |  | 	 * of 44.1 kHz".) Some hardware may report rates lower than that, for example | 
					
						
							|  |  |  | 	 * 176400. This would correspond to "DSD32" (which does not exist). Trying | 
					
						
							|  |  |  | 	 * to use such a rate with DSD hardware does not work and may cause undefined | 
					
						
							|  |  |  | 	 * behavior in said hardware. */ | 
					
						
							| 
									
										
										
										
											2022-09-07 21:58:32 +02:00
										 |  |  | 	if ((res = add_rate(state, 8, SPA_ABS(interleave), true, index & 0xffff, | 
					
						
							|  |  |  | 					next, 44100, params, b)) != 1) | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 		return res; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 13:02:48 +02:00
										 |  |  | 	if ((res = add_channels(state, true, index & 0xffff, next, params, b)) != 1) | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 		return res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*result = spa_pod_builder_pop(b, &f[0]); | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 12:01:06 +02:00
										 |  |  | /* find smaller power of 2 */ | 
					
						
							|  |  |  | static uint32_t flp2(uint32_t x) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	x = x | (x >> 1); | 
					
						
							|  |  |  | 	x = x | (x >> 2); | 
					
						
							|  |  |  | 	x = x | (x >> 4); | 
					
						
							|  |  |  | 	x = x | (x >> 8); | 
					
						
							|  |  |  | 	x = x | (x >> 16); | 
					
						
							|  |  |  | 	return x - (x >> 1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | int | 
					
						
							|  |  |  | spa_alsa_enum_format(struct state *state, int seq, uint32_t start, uint32_t num, | 
					
						
							|  |  |  | 		     const struct spa_pod *filter) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	uint8_t buffer[4096]; | 
					
						
							|  |  |  | 	struct spa_pod_builder b = { 0 }; | 
					
						
							|  |  |  | 	struct spa_pod *fmt; | 
					
						
							|  |  |  | 	int err, res; | 
					
						
							|  |  |  | 	bool opened; | 
					
						
							|  |  |  | 	struct spa_result_node_params result; | 
					
						
							|  |  |  | 	uint32_t count = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-31 10:03:39 +02:00
										 |  |  | 	spa_log_debug(state->log, "opened:%d format:%d started:%d", state->opened, | 
					
						
							|  |  |  | 			state->have_format, state->started); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 	opened = state->opened; | 
					
						
							| 
									
										
										
										
											2022-08-31 10:03:39 +02:00
										 |  |  | 	if (!state->started && state->have_format) | 
					
						
							|  |  |  | 		spa_alsa_close(state); | 
					
						
							| 
									
										
										
										
											2021-09-14 12:42:03 +02:00
										 |  |  | 	if ((err = spa_alsa_open(state, NULL)) < 0) | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 		return err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	result.id = SPA_PARAM_EnumFormat; | 
					
						
							|  |  |  | 	result.next = start; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       next: | 
					
						
							|  |  |  | 	result.index = result.next++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_init(&b, buffer, sizeof(buffer)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (result.index < 0x10000) { | 
					
						
							|  |  |  | 		if ((res = enum_pcm_formats(state, result.index, &result.next, &fmt, &b)) != 1) { | 
					
						
							|  |  |  | 			result.next = 0x10000; | 
					
						
							|  |  |  | 			goto next; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if (result.index < 0x20000) { | 
					
						
							|  |  |  | 		if ((res = enum_iec958_formats(state, result.index, &result.next, &fmt, &b)) != 1) { | 
					
						
							|  |  |  | 			result.next = 0x20000; | 
					
						
							|  |  |  | 			goto next; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-09-10 12:15:43 +02:00
										 |  |  | 	else if (result.index < 0x30000) { | 
					
						
							|  |  |  | 		if ((res = enum_dsd_formats(state, result.index, &result.next, &fmt, &b)) != 1) { | 
					
						
							|  |  |  | 			result.next = 0x30000; | 
					
						
							|  |  |  | 			goto next; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-08-12 14:08:20 +02:00
										 |  |  | 	else | 
					
						
							|  |  |  | 		goto enum_end; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-10 10:50:12 +10:00
										 |  |  | 	if (spa_pod_filter(&b, &result.param, fmt, filter) < 0) | 
					
						
							| 
									
										
										
										
											2017-11-08 15:48:31 +01:00
										 |  |  | 		goto next; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:59:48 +02:00
										 |  |  | 	spa_node_emit_result(&state->hooks, seq, 0, SPA_RESULT_TYPE_NODE_PARAMS, &result); | 
					
						
							| 
									
										
										
										
											2019-02-20 17:51:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (++count != num) | 
					
						
							|  |  |  | 		goto next; | 
					
						
							| 
									
										
										
										
											2017-11-24 12:40:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-20 17:51:05 +01:00
										 |  |  |       enum_end: | 
					
						
							|  |  |  | 	res = 0; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	if (!opened) | 
					
						
							|  |  |  | 		spa_alsa_close(state); | 
					
						
							| 
									
										
										
										
											2017-11-24 12:40:56 +01:00
										 |  |  | 	return res; | 
					
						
							| 
									
										
										
										
											2017-04-04 19:44:00 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-18 14:45:47 +02:00
										 |  |  | static void recalc_headroom(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	uint32_t latency; | 
					
						
							| 
									
										
										
										
											2023-10-18 15:44:07 +02:00
										 |  |  | 	uint32_t rate = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state->position != NULL) | 
					
						
							|  |  |  | 		rate = state->position->clock.target_rate.denom; | 
					
						
							| 
									
										
										
										
											2023-10-18 14:45:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-23 13:20:12 +03:00
										 |  |  | 	if (state->use_period_size_min_as_headroom) | 
					
						
							|  |  |  | 		state->headroom = state->default_headroom ? | 
					
						
							|  |  |  | 				  state->default_headroom : state->period_size_min; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		state->headroom = state->default_headroom; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-18 14:45:47 +02:00
										 |  |  | 	if (!state->disable_tsched || state->resample) { | 
					
						
							|  |  |  | 		/* When using timers, we might miss the pointer update for batch
 | 
					
						
							|  |  |  | 		 * devices so add some extra headroom. With IRQ, we know the pointers | 
					
						
							|  |  |  | 		 * are updated when we wake up and we don't need the headroom. */ | 
					
						
							|  |  |  | 		if (state->is_batch) | 
					
						
							|  |  |  | 			state->headroom += state->period_frames; | 
					
						
							|  |  |  | 		/* Add 32 extra samples of headroom to handle jitter in capture.
 | 
					
						
							|  |  |  | 		 * For IRQ, we don't need this because when we wake up, we have | 
					
						
							|  |  |  | 		 * exactly enough samples to read or write. */ | 
					
						
							|  |  |  | 		if (state->stream == SND_PCM_STREAM_CAPTURE) | 
					
						
							|  |  |  | 			state->headroom = SPA_MAX(state->headroom, 32u); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-03-27 11:48:10 +01:00
										 |  |  | 	if (SPA_LIKELY(state->buffer_frames >= state->threshold)) | 
					
						
							|  |  |  | 		state->headroom = SPA_MIN(state->headroom, state->buffer_frames - state->threshold); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		state->headroom = 0; | 
					
						
							| 
									
										
										
										
											2023-10-18 14:45:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	latency = SPA_MAX(state->min_delay, SPA_MIN(state->max_delay, state->headroom)); | 
					
						
							| 
									
										
										
										
											2023-10-18 15:44:07 +02:00
										 |  |  | 	if (rate != 0 && state->rate != 0) | 
					
						
							|  |  |  | 		latency = SPA_SCALE32_UP(latency, rate, state->rate); | 
					
						
							| 
									
										
										
										
											2023-10-18 14:45:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-06 19:14:22 +03:00
										 |  |  | 	if (state->is_firewire) { | 
					
						
							|  |  |  | 		/* XXX: For ALSA FireWire drivers, unlike for other ALSA drivers, buffer size
 | 
					
						
							|  |  |  | 		 * XXX: contributes extra latency (as of kernel 6.16). | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		latency += state->buffer_frames; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-18 14:45:47 +02:00
										 |  |  | 	state->latency[state->port_direction].min_rate = | 
					
						
							|  |  |  | 		state->latency[state->port_direction].max_rate = latency; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | int spa_alsa_set_format(struct state *state, struct spa_audio_info *fmt, uint32_t flags) | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-09-25 12:29:05 +02:00
										 |  |  | 	unsigned int rrate, rchannels, val, rscale = 1, period_scale = 1; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	snd_pcm_uframes_t period_size; | 
					
						
							|  |  |  | 	int err, dir; | 
					
						
							|  |  |  | 	snd_pcm_hw_params_t *params; | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 	snd_pcm_format_t rformat; | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 	snd_pcm_access_mask_t *amask; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	snd_pcm_t *hndl; | 
					
						
							|  |  |  | 	unsigned int periods; | 
					
						
							| 
									
										
										
										
											2023-10-18 14:45:47 +02:00
										 |  |  | 	bool match = true, planar = false; | 
					
						
							| 
									
										
										
										
											2021-09-14 12:42:03 +02:00
										 |  |  | 	char spdif_params[128] = ""; | 
					
						
							| 
									
										
										
										
											2023-10-18 14:45:47 +02:00
										 |  |  | 	uint32_t default_period; | 
					
						
							| 
									
										
										
										
											2021-09-14 12:42:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-31 10:03:39 +02:00
										 |  |  | 	spa_log_debug(state->log, "opened:%d format:%d started:%d", state->opened, | 
					
						
							|  |  |  | 			state->have_format, state->started); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 	state->use_mmap = !state->disable_mmap; | 
					
						
							| 
									
										
										
										
											2025-09-25 12:29:05 +02:00
										 |  |  | 	state->force_quantum = state->disable_tsched; | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	switch (fmt->media_subtype) { | 
					
						
							|  |  |  | 	case SPA_MEDIA_SUBTYPE_raw: | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		struct spa_audio_info_raw *f = &fmt->info.raw; | 
					
						
							|  |  |  | 		rrate = f->rate; | 
					
						
							|  |  |  | 		rchannels = f->channels; | 
					
						
							|  |  |  | 		rformat = spa_format_to_alsa(f->format, &planar); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	case SPA_MEDIA_SUBTYPE_iec958: | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		struct spa_audio_info_iec958 *f = &fmt->info.iec958; | 
					
						
							| 
									
										
										
										
											2021-09-14 12:42:03 +02:00
										 |  |  | 		unsigned aes3; | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		spa_log_info(state->log, "using IEC958 Codec:%s rate:%d", | 
					
						
							| 
									
										
										
										
											2024-09-18 09:54:34 +02:00
										 |  |  | 				spa_type_audio_iec958_codec_to_short_name(f->codec), | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 				f->rate); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		rformat = SND_PCM_FORMAT_S16_LE; | 
					
						
							|  |  |  | 		rchannels = 2; | 
					
						
							|  |  |  | 		rrate = f->rate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		switch (f->codec) { | 
					
						
							|  |  |  | 		case SPA_AUDIO_IEC958_CODEC_PCM: | 
					
						
							|  |  |  | 		case SPA_AUDIO_IEC958_CODEC_DTS: | 
					
						
							|  |  |  | 		case SPA_AUDIO_IEC958_CODEC_AC3: | 
					
						
							|  |  |  | 		case SPA_AUDIO_IEC958_CODEC_MPEG: | 
					
						
							|  |  |  | 		case SPA_AUDIO_IEC958_CODEC_MPEG2_AAC: | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case SPA_AUDIO_IEC958_CODEC_EAC3: | 
					
						
							| 
									
										
										
										
											2021-12-16 11:02:39 +01:00
										 |  |  | 			/* EAC3 has 3 rates, 32, 44.1 and 48KHz. We need to
 | 
					
						
							|  |  |  | 			 * open the device in 4x that rate. Some clients | 
					
						
							|  |  |  | 			 * already multiply (mpv,..) others don't (vlc). */ | 
					
						
							|  |  |  | 			if (rrate <= 48000) | 
					
						
							|  |  |  | 				rrate *= 4; | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		case SPA_AUDIO_IEC958_CODEC_TRUEHD: | 
					
						
							|  |  |  | 		case SPA_AUDIO_IEC958_CODEC_DTSHD: | 
					
						
							|  |  |  | 			rchannels = 8; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			return -ENOTSUP; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		switch (rrate) { | 
					
						
							| 
									
										
										
										
											2021-09-14 12:42:03 +02:00
										 |  |  | 		case 22050: aes3 = IEC958_AES3_CON_FS_22050; break; | 
					
						
							|  |  |  | 		case 24000: aes3 = IEC958_AES3_CON_FS_24000; break; | 
					
						
							|  |  |  | 		case 32000: aes3 = IEC958_AES3_CON_FS_32000; break; | 
					
						
							|  |  |  | 		case 44100: aes3 = IEC958_AES3_CON_FS_44100; break; | 
					
						
							|  |  |  | 		case 48000: aes3 = IEC958_AES3_CON_FS_48000; break; | 
					
						
							|  |  |  | 		case 88200: aes3 = IEC958_AES3_CON_FS_88200; break; | 
					
						
							|  |  |  | 		case 96000: aes3 = IEC958_AES3_CON_FS_96000; break; | 
					
						
							|  |  |  | 		case 176400: aes3 = IEC958_AES3_CON_FS_176400; break; | 
					
						
							|  |  |  | 		case 192000: aes3 = IEC958_AES3_CON_FS_192000; break; | 
					
						
							|  |  |  | 		case 768000: aes3 = IEC958_AES3_CON_FS_768000; break; | 
					
						
							|  |  |  | 		default: aes3 = IEC958_AES3_CON_FS_NOTID; break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		spa_scnprintf(spdif_params, sizeof(spdif_params), | 
					
						
							|  |  |  | 				",AES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x", | 
					
						
							|  |  |  | 				IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO, | 
					
						
							|  |  |  | 				IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER, | 
					
						
							|  |  |  | 				0, aes3); | 
					
						
							| 
									
										
										
										
											2025-09-25 12:29:05 +02:00
										 |  |  | 		state->force_quantum = true; | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	case SPA_MEDIA_SUBTYPE_dsd: | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		struct spa_audio_info_dsd *f = &fmt->info.dsd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		rrate = f->rate; | 
					
						
							|  |  |  | 		rchannels = f->channels; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		switch (f->interleave) { | 
					
						
							|  |  |  | 		case 4: | 
					
						
							|  |  |  | 			rformat = SND_PCM_FORMAT_DSD_U32_BE; | 
					
						
							|  |  |  | 			rrate /= 4; | 
					
						
							| 
									
										
										
										
											2022-04-28 12:32:08 +02:00
										 |  |  | 			rscale = 4; | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		case -4: | 
					
						
							|  |  |  | 			rformat = SND_PCM_FORMAT_DSD_U32_LE; | 
					
						
							|  |  |  | 			rrate /= 4; | 
					
						
							| 
									
										
										
										
											2022-04-28 12:32:08 +02:00
										 |  |  | 			rscale = 4; | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		case 2: | 
					
						
							|  |  |  | 			rformat = SND_PCM_FORMAT_DSD_U16_BE; | 
					
						
							|  |  |  | 			rrate /= 2; | 
					
						
							| 
									
										
										
										
											2022-04-28 12:32:08 +02:00
										 |  |  | 			rscale = 2; | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		case -2: | 
					
						
							|  |  |  | 			rformat = SND_PCM_FORMAT_DSD_U16_LE; | 
					
						
							|  |  |  | 			rrate /= 2; | 
					
						
							| 
									
										
										
										
											2022-04-28 12:32:08 +02:00
										 |  |  | 			rscale = 2; | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		case 1: | 
					
						
							|  |  |  | 			rformat = SND_PCM_FORMAT_DSD_U8; | 
					
						
							| 
									
										
										
										
											2022-04-28 12:32:08 +02:00
										 |  |  | 			rscale = 1; | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			return -ENOTSUP; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-09-25 12:29:05 +02:00
										 |  |  | 		state->force_quantum = true; | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2021-09-14 12:42:03 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 	default: | 
					
						
							|  |  |  | 		return -ENOTSUP; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (rformat == SND_PCM_FORMAT_UNKNOWN) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 		spa_log_warn(state->log, "%s: unknown format", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 				state->name); | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 		return -EINVAL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-31 10:03:39 +02:00
										 |  |  | 	if (!state->started && state->have_format) | 
					
						
							|  |  |  | 		spa_alsa_close(state); | 
					
						
							| 
									
										
										
										
											2021-09-14 12:42:03 +02:00
										 |  |  | 	if ((err = spa_alsa_open(state, spdif_params)) < 0) | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 		return err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	hndl = state->hndl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	snd_pcm_hw_params_alloca(¶ms); | 
					
						
							|  |  |  | 	/* choose all parameters */ | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_any(hndl, params), "Broken configuration for playback: no configurations available"); | 
					
						
							| 
									
										
										
										
											2022-08-05 13:00:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | 	debug_hw_params(state, __func__, params); | 
					
						
							| 
									
										
										
										
											2022-08-05 13:00:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-01 11:42:10 +02:00
										 |  |  | 	/* set hardware resampling, no resample */ | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	CHECK(snd_pcm_hw_params_set_rate_resample(hndl, params, 0), "set_rate_resample"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-01 11:42:10 +02:00
										 |  |  | 	/* set the interleaved/planar read/write format */ | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 	snd_pcm_access_mask_alloca(&amask); | 
					
						
							|  |  |  | 	snd_pcm_hw_params_get_access_mask(params, amask); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 15:44:22 +01:00
										 |  |  | 	if (state->use_mmap) { | 
					
						
							|  |  |  | 		if ((err = snd_pcm_hw_params_set_access(hndl, params, | 
					
						
							|  |  |  | 					planar ? SND_PCM_ACCESS_MMAP_NONINTERLEAVED | 
					
						
							|  |  |  | 					: SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 			spa_log_debug(state->log, "%p: MMAP not possible: %s", state, | 
					
						
							| 
									
										
										
										
											2021-01-15 15:44:22 +01:00
										 |  |  | 					snd_strerror(err)); | 
					
						
							|  |  |  | 			state->use_mmap = false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (!state->use_mmap) { | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 		if ((err = snd_pcm_hw_params_set_access(hndl, params, | 
					
						
							|  |  |  | 				planar ? SND_PCM_ACCESS_RW_NONINTERLEAVED | 
					
						
							|  |  |  | 				: SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 			spa_log_error(state->log, "%s: RW not possible: %s", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 					state->name, snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 			return err; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-07-01 11:42:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* set the sample format */ | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 	spa_log_debug(state->log, "%p: Stream parameters are %iHz fmt:%s access:%s-%s channels:%i", | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 			state, rrate, snd_pcm_format_name(rformat), | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 			state->use_mmap ? "mmap" : "rw", | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 			planar ? "planar" : "interleaved", rchannels); | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_set_format(hndl, params, rformat), "set_format"); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* set the count of channels */ | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 	val = rchannels; | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_set_channels_near(hndl, params, &val), "set_channels"); | 
					
						
							|  |  |  | 	if (rchannels != val) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 		spa_log_warn(state->log, "%s: Channels doesn't match (requested %u, got %u)", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 				state->name, rchannels, val); | 
					
						
							| 
									
										
										
										
											2020-05-08 17:52:30 +02:00
										 |  |  | 		if (!SPA_FLAG_IS_SET(flags, SPA_NODE_PARAM_FLAG_NEAREST)) | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 			return -EINVAL; | 
					
						
							| 
									
										
										
										
											2022-06-23 10:20:49 +02:00
										 |  |  | 		if (fmt->media_subtype != SPA_MEDIA_SUBTYPE_raw) | 
					
						
							|  |  |  | 			return -EINVAL; | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 		rchannels = val; | 
					
						
							| 
									
										
										
										
											2022-06-23 10:20:49 +02:00
										 |  |  | 		fmt->info.raw.channels = rchannels; | 
					
						
							| 
									
										
										
										
											2020-05-08 17:52:30 +02:00
										 |  |  | 		match = false; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 11:28:41 +01:00
										 |  |  | 	if (!state->multi_rate && | 
					
						
							| 
									
										
										
										
											2024-11-29 10:42:58 -05:00
										 |  |  | 	    state->card && | 
					
						
							| 
									
										
										
										
											2021-11-23 11:28:41 +01:00
										 |  |  | 	    state->card->format_ref > 0 && | 
					
						
							|  |  |  | 	    state->card->rate != rrate) { | 
					
						
							|  |  |  | 		spa_log_error(state->log, "%p: card already opened at rate:%i", | 
					
						
							|  |  |  | 				state, state->card->rate); | 
					
						
							|  |  |  | 		return -EINVAL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	/* set the stream rate */ | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 	val = rrate; | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_set_rate_near(hndl, params, &val, 0), "set_rate_near"); | 
					
						
							|  |  |  | 	if (rrate != val) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 		spa_log_warn(state->log, "%s: Rate doesn't match (requested %iHz, got %iHz)", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 				state->name, rrate, val); | 
					
						
							| 
									
										
										
										
											2020-05-08 17:52:30 +02:00
										 |  |  | 		if (!SPA_FLAG_IS_SET(flags, SPA_NODE_PARAM_FLAG_NEAREST)) | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 			return -EINVAL; | 
					
						
							| 
									
										
										
										
											2022-06-23 10:20:49 +02:00
										 |  |  | 		if (fmt->media_subtype != SPA_MEDIA_SUBTYPE_raw) | 
					
						
							|  |  |  | 			return -EINVAL; | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 		rrate = val; | 
					
						
							| 
									
										
										
										
											2022-06-23 10:20:49 +02:00
										 |  |  | 		fmt->info.raw.rate = rrate; | 
					
						
							| 
									
										
										
										
											2020-05-08 17:52:30 +02:00
										 |  |  | 		match = false; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-01-16 16:05:17 +01:00
										 |  |  | 	if (rchannels == 0 || rrate == 0) { | 
					
						
							|  |  |  | 		spa_log_error(state->log, "%s: invalid channels:%d or rate:%d", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 				state->name, rchannels, rrate); | 
					
						
							| 
									
										
										
										
											2023-01-16 16:05:17 +01:00
										 |  |  | 		return -EIO; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 	state->format = rformat; | 
					
						
							|  |  |  | 	state->channels = rchannels; | 
					
						
							|  |  |  | 	state->rate = rrate; | 
					
						
							|  |  |  | 	state->frame_size = snd_pcm_format_physical_width(rformat) / 8; | 
					
						
							| 
									
										
										
										
											2022-04-28 12:32:08 +02:00
										 |  |  | 	state->frame_scale = rscale; | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 	state->planar = planar; | 
					
						
							| 
									
										
										
										
											2020-07-01 11:42:10 +02:00
										 |  |  | 	state->blocks = 1; | 
					
						
							|  |  |  | 	if (planar) | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 		state->blocks *= rchannels; | 
					
						
							| 
									
										
										
										
											2020-07-01 11:42:10 +02:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2021-09-17 18:05:52 +02:00
										 |  |  | 		state->frame_size *= rchannels; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 15:38:41 +02:00
										 |  |  | 	/* make sure we update threshold in check_position_config() because they depend
 | 
					
						
							| 
									
										
										
										
											2023-09-15 11:08:35 +02:00
										 |  |  | 	 * on the samplerate. */ | 
					
						
							| 
									
										
										
										
											2023-09-20 11:32:34 +02:00
										 |  |  | 	state->driver_duration = 0; | 
					
						
							|  |  |  | 	state->driver_rate.denom = 0; | 
					
						
							| 
									
										
										
										
											2023-09-15 11:08:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:20:27 +01:00
										 |  |  | 	state->have_format = true; | 
					
						
							| 
									
										
										
										
											2024-11-29 10:42:58 -05:00
										 |  |  | 	if (state->card && state->card->format_ref++ == 0) | 
					
						
							| 
									
										
										
										
											2021-11-23 11:28:41 +01:00
										 |  |  | 		state->card->rate = rrate; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	dir = 0; | 
					
						
							| 
									
										
										
										
											2021-12-17 16:08:08 +01:00
										 |  |  | 	period_size = state->default_period_size; | 
					
						
							| 
									
										
										
										
											2023-10-18 14:45:47 +02:00
										 |  |  | 	state->is_batch = snd_pcm_hw_params_is_batch(params) && !state->disable_batch; | 
					
						
							| 
									
										
										
										
											2021-01-21 17:10:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 17:22:38 +02:00
										 |  |  | 	default_period = SPA_SCALE32_UP(DEFAULT_PERIOD, state->rate, DEFAULT_RATE); | 
					
						
							|  |  |  | 	default_period = flp2(2 * default_period - 1); | 
					
						
							| 
									
										
										
										
											2023-08-31 12:01:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-25 12:29:05 +02:00
										 |  |  | 	/* no period size specified. If we are batch or forcing our quantum,
 | 
					
						
							|  |  |  | 	 * use the graph requested quantum scaled by our rate */ | 
					
						
							|  |  |  | 	if (period_size == 0 && (state->is_batch || state->force_quantum) && state->position) { | 
					
						
							|  |  |  | 		period_size = SPA_SCALE32_UP(state->position->clock.target_duration, | 
					
						
							|  |  |  | 				state->rate, state->position->clock.target_rate.denom); | 
					
						
							|  |  |  | 		period_size = flp2(period_size); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-08-23 13:56:20 +02:00
										 |  |  | 	if (period_size == 0) | 
					
						
							| 
									
										
										
										
											2023-08-31 12:01:06 +02:00
										 |  |  | 		period_size = default_period; | 
					
						
							| 
									
										
										
										
											2023-08-23 13:56:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-18 14:45:47 +02:00
										 |  |  | 	if (!state->disable_tsched || state->resample) { | 
					
						
							|  |  |  | 		if (state->is_batch) { | 
					
						
							| 
									
										
										
										
											2023-08-23 13:56:20 +02:00
										 |  |  | 			/* batch devices get their hw pointers updated every period. Make
 | 
					
						
							|  |  |  | 			 * the period smaller and add one period of headroom. Limit the | 
					
						
							|  |  |  | 			 * period size to our default so that we don't create too much | 
					
						
							|  |  |  | 			 * headroom. */ | 
					
						
							| 
									
										
										
										
											2023-08-31 12:01:06 +02:00
										 |  |  | 			period_size = SPA_MIN(period_size, default_period) / 2; | 
					
						
							| 
									
										
										
										
											2025-09-25 12:29:05 +02:00
										 |  |  | 			period_scale = 2; | 
					
						
							| 
									
										
										
										
											2023-08-23 13:56:20 +02:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			/* disable ALSA wakeups */ | 
					
						
							|  |  |  | 			if (snd_pcm_hw_params_can_disable_period_wakeup(params)) | 
					
						
							|  |  |  | 				CHECK(snd_pcm_hw_params_set_period_wakeup(hndl, params, 0), "set_period_wakeup"); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-12-11 17:38:27 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-06 16:06:53 +03:00
										 |  |  | 	if (state->default_period_num != 0) { | 
					
						
							|  |  |  | 		/* period number given use that */ | 
					
						
							|  |  |  | 		periods = state->default_period_num; | 
					
						
							|  |  |  | 	} else if (state->disable_tsched) { | 
					
						
							|  |  |  | 		/* IRQ mode, use 3 periods. This is a bit of a workaround
 | 
					
						
							|  |  |  | 		 * for Firewire devices, which seem to only work with 3 periods. | 
					
						
							|  |  |  | 		 * For PipeWire it does not actually matter how many periods | 
					
						
							|  |  |  | 		 * are used, we will always keep 1 filled, so we can work fine | 
					
						
							|  |  |  | 		 * with anything from 2 periods to MAX. */ | 
					
						
							|  |  |  | 		periods = 3; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		periods = UINT_MAX; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-23 13:20:12 +03:00
										 |  |  | 	/* Query the minimum period size for this configuration
 | 
					
						
							|  |  |  | 	 * This information is used as headroom if use_period_size_min_as_headroom is | 
					
						
							|  |  |  | 	 * set and default_headroom is 0 (not forced by user) | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_get_period_size_min(params, &state->period_size_min, &dir), "snd_pcm_hw_params_get_period_size_min"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-06 16:06:53 +03:00
										 |  |  | 	if (state->default_period_size == 0) { | 
					
						
							|  |  |  | 		/* Some devices (FireWire) don't produce audio if period number is too
 | 
					
						
							|  |  |  | 		 * small, so force a minimum. This will restrict possible period sizes if | 
					
						
							|  |  |  | 		 * the device has small buffer (like FireWire), so force it only if | 
					
						
							|  |  |  | 		 * period size was not set manually. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		snd_pcm_uframes_t period_size_max; | 
					
						
							|  |  |  | 		unsigned int periods_min = (periods == UINT_MAX) ? 3 : periods; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-22 20:27:19 +03:00
										 |  |  | 		err = snd_pcm_hw_params_set_periods_min(hndl, params, &periods_min, &dir); | 
					
						
							|  |  |  | 		if (!err) { | 
					
						
							| 
									
										
										
										
											2025-09-25 12:29:05 +02:00
										 |  |  | 			CHECK(snd_pcm_hw_params_get_period_size_max(params, &period_size_max, &dir), | 
					
						
							|  |  |  | 					"get_period_size_max"); | 
					
						
							| 
									
										
										
										
											2025-09-22 20:27:19 +03:00
										 |  |  | 			if (period_size > period_size_max) | 
					
						
							|  |  |  | 				period_size = SPA_MIN(period_size, flp2(period_size_max)); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			spa_log_debug(state->log, "set_periods_min: %s", snd_strerror(err)); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-09-06 16:06:53 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	CHECK(snd_pcm_hw_params_set_period_size_near(hndl, params, &period_size, &dir), "set_period_size_near"); | 
					
						
							| 
									
										
										
										
											2021-12-17 16:09:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-16 16:05:17 +01:00
										 |  |  | 	if (period_size == 0) { | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 		spa_log_error(state->log, "%s: invalid period_size 0 (driver error?)", state->name); | 
					
						
							| 
									
										
										
										
											2023-01-16 16:05:17 +01:00
										 |  |  | 		return -EIO; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-17 16:09:52 +01:00
										 |  |  | 	state->period_frames = period_size; | 
					
						
							| 
									
										
										
										
											2025-09-25 12:29:05 +02:00
										 |  |  | 	state->duration = period_size * period_scale; | 
					
						
							| 
									
										
										
										
											2021-12-17 16:09:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-06 16:06:53 +03:00
										 |  |  | 	if (periods != UINT_MAX) { | 
					
						
							| 
									
										
										
										
											2021-12-17 16:09:52 +01:00
										 |  |  | 		CHECK(snd_pcm_hw_params_set_periods_near(hndl, params, &periods, &dir), "set_periods"); | 
					
						
							|  |  |  | 		state->buffer_frames = period_size * periods; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		CHECK(snd_pcm_hw_params_get_buffer_size_max(params, &state->buffer_frames), "get_buffer_size_max"); | 
					
						
							| 
									
										
										
										
											2022-04-07 16:01:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-30 13:28:16 +01:00
										 |  |  | 		state->buffer_frames = SPA_MIN(state->buffer_frames, state->quantum_limit * 4 * state->frame_scale); | 
					
						
							| 
									
										
										
										
											2022-04-07 16:01:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		CHECK(snd_pcm_hw_params_set_buffer_size_min(hndl, params, &state->buffer_frames), "set_buffer_size_min"); | 
					
						
							| 
									
										
										
										
											2021-12-17 16:09:52 +01:00
										 |  |  | 		CHECK(snd_pcm_hw_params_set_buffer_size_near(hndl, params, &state->buffer_frames), "set_buffer_size_near"); | 
					
						
							|  |  |  | 		periods = state->buffer_frames / period_size; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-01-16 16:05:17 +01:00
										 |  |  | 	if (state->buffer_frames == 0) { | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 		spa_log_error(state->log, "%s: invalid buffer_frames 0 (driver error?)", state->name); | 
					
						
							| 
									
										
										
										
											2023-01-16 16:05:17 +01:00
										 |  |  | 		return -EIO; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-12-09 17:10:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-03 14:10:33 +01:00
										 |  |  | 	state->max_delay = state->buffer_frames / 2; | 
					
						
							| 
									
										
										
										
											2022-12-14 12:27:33 +01:00
										 |  |  | 	if (spa_strstartswith(state->props.device, "a52") || | 
					
						
							| 
									
										
										
										
											2024-07-05 16:04:23 +03:00
										 |  |  | 			spa_strstartswith(state->props.device, "dca") || | 
					
						
							|  |  |  | 			(spa_strstartswith(state->props.device, "plug:") && | 
					
						
							|  |  |  | 					strstr(state->props.device, "a52:"))) | 
					
						
							| 
									
										
										
										
											2022-12-14 12:27:33 +01:00
										 |  |  | 		state->min_delay = SPA_MIN(2048u, state->buffer_frames); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		state->min_delay = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-29 13:20:14 +02:00
										 |  |  | 	state->start_delay = state->default_start_delay; | 
					
						
							| 
									
										
										
										
											2021-03-29 13:17:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-18 14:45:47 +02:00
										 |  |  | 	recalc_headroom(state); | 
					
						
							| 
									
										
										
										
											2021-05-21 10:01:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 	spa_log_info(state->log, "%s: format:%s access:%s-%s rate:%d channels:%d " | 
					
						
							| 
									
										
										
										
											2025-09-23 13:20:12 +03:00
										 |  |  | 			"buffer frames %lu, period frames %lu (min:%lu), periods %u, frame_size %zd " | 
					
						
							| 
									
										
										
										
											2025-07-15 15:06:24 +02:00
										 |  |  | 			"headroom %u start-delay:%u batch:%u tsched:%u resample:%u", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 			state->name, snd_pcm_format_name(state->format), | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 			state->use_mmap ? "mmap" : "rw", | 
					
						
							|  |  |  | 			planar ? "planar" : "interleaved", | 
					
						
							| 
									
										
										
										
											2020-07-01 11:42:10 +02:00
										 |  |  | 			state->rate, state->channels, state->buffer_frames, state->period_frames, | 
					
						
							| 
									
										
										
										
											2025-09-23 13:20:12 +03:00
										 |  |  | 			state->period_size_min, periods, state->frame_size, state->headroom, | 
					
						
							|  |  |  | 			state->start_delay, state->is_batch, !state->disable_tsched, state->resample); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* write the parameters to device */ | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params(hndl, params), "set_hw_params"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 17:52:30 +02:00
										 |  |  | 	return match ? 0 : 1; | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | int spa_alsa_update_rate_match(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	uint64_t pitch, last_pitch; | 
					
						
							|  |  |  | 	int err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!state->pitch_elem) | 
					
						
							|  |  |  | 		return -ENOENT; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* The rate/pitch defines the rate of input to output (if there were a
 | 
					
						
							|  |  |  | 	 * resampler, it's the ratio of input samples to output samples). This | 
					
						
							|  |  |  | 	 * means that to adjust the playback rate, we need to apply the inverse | 
					
						
							|  |  |  | 	 * of the given rate. */ | 
					
						
							|  |  |  | 	if (state->stream == SND_PCM_STREAM_CAPTURE) { | 
					
						
							| 
									
										
										
										
											2024-06-18 12:17:56 +02:00
										 |  |  | 		pitch = (uint64_t)(1000000 * state->rate_match->rate); | 
					
						
							|  |  |  | 		last_pitch = (uint64_t)(1000000 * state->last_rate); | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2024-06-18 12:17:56 +02:00
										 |  |  | 		pitch = (uint64_t)(1000000 / state->rate_match->rate); | 
					
						
							|  |  |  | 		last_pitch = (uint64_t)(1000000 / state->last_rate); | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-03 11:15:59 -04:00
										 |  |  | 	/* The pitch adjustment is limited to 1 ppm according to the spec, but
 | 
					
						
							|  |  |  | 	 * let's avoid very granular changes so that we don't spam the host | 
					
						
							|  |  |  | 	 * (and ourselves, if bind-ctls are enabled). */ | 
					
						
							|  |  |  | 	if (SPA_ABS((int)pitch - (int)last_pitch) < 10) | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	snd_ctl_elem_value_set_integer(state->pitch_elem, 0, pitch); | 
					
						
							|  |  |  | 	CHECK(snd_ctl_elem_write(state->ctl, state->pitch_elem), "snd_ctl_elem_write"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_log_trace_fp(state->log, "%s %u set rate to %g", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 			state->name, state->stream, state->rate_match->rate); | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	state->last_rate = state->rate_match->rate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | static int set_swparams(struct state *state) | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	snd_pcm_t *hndl = state->hndl; | 
					
						
							|  |  |  | 	int err = 0; | 
					
						
							|  |  |  | 	snd_pcm_sw_params_t *params; | 
					
						
							| 
									
										
										
										
											2017-03-28 19:29:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	snd_pcm_sw_params_alloca(¶ms); | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	/* get the current params */ | 
					
						
							|  |  |  | 	CHECK(snd_pcm_sw_params_current(hndl, params), "sw_params_current"); | 
					
						
							| 
									
										
										
										
											2016-09-20 11:20:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-03 16:21:41 +01:00
										 |  |  | 	CHECK(snd_pcm_sw_params_set_tstamp_mode(hndl, params, SND_PCM_TSTAMP_ENABLE), | 
					
						
							|  |  |  | 			"sw_params_set_tstamp_mode"); | 
					
						
							|  |  |  | 	CHECK(snd_pcm_sw_params_set_tstamp_type(hndl, params, SND_PCM_TSTAMP_TYPE_MONOTONIC), | 
					
						
							|  |  |  | 			"sw_params_set_tstamp_type"); | 
					
						
							| 
									
										
										
										
											2018-11-19 18:03:45 +01:00
										 |  |  | #if 0
 | 
					
						
							|  |  |  | 	snd_pcm_uframes_t boundary; | 
					
						
							|  |  |  | 	CHECK(snd_pcm_sw_params_get_boundary(params, &boundary), "get_boundary"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CHECK(snd_pcm_sw_params_set_stop_threshold(hndl, params, boundary), "set_stop_threshold"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	/* start the transfer */ | 
					
						
							|  |  |  | 	CHECK(snd_pcm_sw_params_set_start_threshold(hndl, params, LONG_MAX), "set_start_threshold"); | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 	if (state->disable_tsched) { | 
					
						
							| 
									
										
										
										
											2024-03-27 11:56:12 +01:00
										 |  |  | 		snd_pcm_uframes_t avail_min = 0; | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (state->stream == SND_PCM_STREAM_PLAYBACK) { | 
					
						
							|  |  |  | 			/* wake up when buffer has target frames or less data (will underrun soon) */ | 
					
						
							| 
									
										
										
										
											2024-03-27 11:56:12 +01:00
										 |  |  | 			if (state->buffer_frames >= (state->threshold + state->headroom)) | 
					
						
							|  |  |  | 				avail_min = state->buffer_frames - (state->threshold + state->headroom); | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			/* wake up when there's target frames or more (enough for us to read and push a buffer) */ | 
					
						
							| 
									
										
										
										
											2024-03-27 11:56:12 +01:00
										 |  |  | 			avail_min = SPA_MIN(state->threshold + state->headroom, state->buffer_frames); | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		CHECK(snd_pcm_sw_params_set_avail_min(hndl, params, avail_min), "set_avail_min"); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	/* write the parameters to the playback device */ | 
					
						
							|  |  |  | 	CHECK(snd_pcm_sw_params(hndl, params), "sw_params"); | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-05 14:09:07 +02:00
										 |  |  | 	if (SPA_UNLIKELY(spa_log_level_topic_enabled(state->log, SPA_LOG_TOPIC_DEFAULT, SPA_LOG_LEVEL_DEBUG))) { | 
					
						
							| 
									
										
										
										
											2022-08-05 14:01:37 +02:00
										 |  |  | 		spa_log_debug(state->log, "state after sw_params:"); | 
					
						
							|  |  |  | 		snd_pcm_dump(hndl, state->output); | 
					
						
							|  |  |  | 		fflush(state->log_file); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | static int set_timeout(struct state *state, uint64_t time) | 
					
						
							| 
									
										
										
										
											2016-11-03 19:41:53 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 	struct itimerspec ts; | 
					
						
							| 
									
										
										
										
											2019-09-19 16:55:02 +02:00
										 |  |  | 	ts.it_value.tv_sec = time / SPA_NSEC_PER_SEC; | 
					
						
							|  |  |  | 	ts.it_value.tv_nsec = time % SPA_NSEC_PER_SEC; | 
					
						
							|  |  |  | 	ts.it_interval.tv_sec = 0; | 
					
						
							|  |  |  | 	ts.it_interval.tv_nsec = 0; | 
					
						
							|  |  |  | 	spa_system_timerfd_settime(state->data_system, | 
					
						
							|  |  |  | 			state->timerfd, SPA_FD_TIMER_ABSTIME, &ts, NULL); | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2017-10-22 13:12:34 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-16 17:38:51 +02:00
										 |  |  | static int spa_alsa_silence(struct state *state, snd_pcm_uframes_t silence) | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	snd_pcm_t *hndl = state->hndl; | 
					
						
							|  |  |  | 	const snd_pcm_channel_area_t *my_areas; | 
					
						
							|  |  |  | 	snd_pcm_uframes_t frames, offset; | 
					
						
							|  |  |  | 	int i, res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state->use_mmap) { | 
					
						
							|  |  |  | 		frames = state->buffer_frames; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (SPA_UNLIKELY((res = snd_pcm_mmap_begin(hndl, &my_areas, &offset, &frames)) < 0)) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 			spa_log_error(state->log, "%s: snd_pcm_mmap_begin error: %s", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 					state->name, snd_strerror(res)); | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 			return res; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		silence = SPA_MIN(silence, frames); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 		spa_log_trace_fp(state->log, "%p: frames:%ld offset:%ld silence %ld", | 
					
						
							| 
									
										
										
										
											2021-03-16 09:48:12 +01:00
										 |  |  | 				state, frames, offset, silence); | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 		snd_pcm_areas_silence(my_areas, offset, state->channels, silence, state->format); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (SPA_UNLIKELY((res = snd_pcm_mmap_commit(hndl, offset, silence)) < 0)) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 			spa_log_error(state->log, "%s: snd_pcm_mmap_commit error: %s", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 					state->name, snd_strerror(res)); | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 			return res; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		uint8_t buffer[silence * state->frame_size]; | 
					
						
							|  |  |  | 		memset(buffer, 0, silence * state->frame_size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (state->planar) { | 
					
						
							|  |  |  | 			void *bufs[state->channels]; | 
					
						
							|  |  |  | 			for (i = 0; i < state->channels; i++) | 
					
						
							|  |  |  | 				bufs[i] = buffer; | 
					
						
							|  |  |  | 			snd_pcm_writen(hndl, bufs, silence); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			snd_pcm_writei(hndl, buffer, silence); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 15:38:41 +02:00
										 |  |  | static void reset_buffers(struct state *this) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	uint32_t i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_list_init(&this->free); | 
					
						
							|  |  |  | 	spa_list_init(&this->ready); | 
					
						
							| 
									
										
										
										
											2023-11-10 16:12:15 +01:00
										 |  |  | 	this->ready_offset = 0; | 
					
						
							| 
									
										
										
										
											2023-09-20 15:38:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < this->n_buffers; i++) { | 
					
						
							|  |  |  | 		struct buffer *b = &this->buffers[i]; | 
					
						
							|  |  |  | 		if (this->stream == SND_PCM_STREAM_PLAYBACK) { | 
					
						
							|  |  |  | 			SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUT); | 
					
						
							|  |  |  | 			spa_node_call_reuse_buffer(&this->callbacks, 0, b->id); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			spa_list_append(&this->free, &b->link); | 
					
						
							|  |  |  | 			SPA_FLAG_CLEAR(b->flags, BUFFER_FLAG_OUT); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int do_prepare(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	state->last_threshold = state->threshold; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_log_debug(state->log, "%p: start threshold:%d duration:%d rate:%d follower:%d match:%d resample:%d", | 
					
						
							|  |  |  | 			state, state->threshold, state->driver_duration, state->driver_rate.denom, | 
					
						
							|  |  |  | 			state->following, state->matching, state->resample); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CHECK(set_swparams(state), "swparams"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-12 21:38:11 +09:00
										 |  |  | 	if ((!state->linked) && (err = snd_pcm_prepare(state->hndl)) < 0 && err != -EBUSY) { | 
					
						
							| 
									
										
										
										
											2023-09-20 15:38:41 +02:00
										 |  |  | 		spa_log_error(state->log, "%s: snd_pcm_prepare error: %s", | 
					
						
							|  |  |  | 				state->name, snd_strerror(err)); | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (state->stream == SND_PCM_STREAM_PLAYBACK) { | 
					
						
							|  |  |  | 		snd_pcm_uframes_t silence = state->start_delay + state->threshold + state->headroom; | 
					
						
							|  |  |  | 		if (state->disable_tsched) | 
					
						
							|  |  |  | 			silence += state->threshold; | 
					
						
							|  |  |  | 		spa_alsa_silence(state, silence); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	reset_buffers(state); | 
					
						
							|  |  |  | 	state->alsa_sync = true; | 
					
						
							|  |  |  | 	state->alsa_sync_warning = false; | 
					
						
							|  |  |  | 	state->alsa_started = false; | 
					
						
							| 
									
										
										
										
											2025-06-11 13:03:18 +02:00
										 |  |  | 	spa_dll_init(&state->dll); | 
					
						
							| 
									
										
										
										
											2023-09-20 15:38:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int do_drop(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							| 
									
										
										
										
											2023-11-15 09:55:36 +01:00
										 |  |  | 	spa_log_debug(state->log, "%p: snd_pcm_drop linked:%u", state, state->linked); | 
					
						
							| 
									
										
										
										
											2023-09-20 15:38:41 +02:00
										 |  |  | 	if (!state->linked && (res = snd_pcm_drop(state->hndl)) < 0) { | 
					
						
							|  |  |  | 		spa_log_error(state->log, "%s: snd_pcm_drop: %s", | 
					
						
							|  |  |  | 				state->name, snd_strerror(res)); | 
					
						
							|  |  |  | 		return res; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | static inline int do_start(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	if (SPA_UNLIKELY(!state->alsa_started)) { | 
					
						
							| 
									
										
										
										
											2023-11-15 09:55:36 +01:00
										 |  |  | 		spa_log_debug(state->log, "%p: snd_pcm_start linked:%u", state, state->linked); | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | 		if (!state->linked && (res = snd_pcm_start(state->hndl)) < 0) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 			spa_log_error(state->log, "%s: snd_pcm_start: %s", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 					state->name, snd_strerror(res)); | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 			return res; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		state->alsa_started = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-15 14:52:25 +02:00
										 |  |  | static inline int check_position_config(struct state *state, bool starting); | 
					
						
							| 
									
										
										
										
											2024-04-16 09:28:49 +02:00
										 |  |  | static void update_sources(struct state *state, bool active); | 
					
						
							| 
									
										
										
										
											2023-09-20 15:38:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-15 09:55:58 +01:00
										 |  |  | static int alsa_recover(struct state *state) | 
					
						
							| 
									
										
										
										
											2018-10-31 15:20:52 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-11-06 17:36:48 +01:00
										 |  |  | 	int res, st, retry = 0; | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 	snd_pcm_status_t *status; | 
					
						
							| 
									
										
										
										
											2023-09-20 15:38:41 +02:00
										 |  |  | 	struct state *driver, *follower; | 
					
						
							| 
									
										
										
										
											2018-10-31 15:20:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	snd_pcm_status_alloca(&status); | 
					
						
							| 
									
										
										
										
											2020-03-16 12:52:28 +01:00
										 |  |  | 	if (SPA_UNLIKELY((res = snd_pcm_status(state->hndl, status)) < 0)) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 		spa_log_error(state->log, "%s: snd_pcm_status error: %s", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 				state->name, snd_strerror(res)); | 
					
						
							| 
									
										
										
										
											2020-10-22 09:16:45 +02:00
										 |  |  | 		goto recover; | 
					
						
							| 
									
										
										
										
											2018-10-31 15:20:52 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	st = snd_pcm_status_get_state(status); | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 	switch (st) { | 
					
						
							|  |  |  | 	case SND_PCM_STATE_XRUN: | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 		struct timeval now, trigger, diff; | 
					
						
							| 
									
										
										
										
											2019-08-22 13:23:48 +02:00
										 |  |  | 		uint64_t delay, missing; | 
					
						
							| 
									
										
										
										
											2018-10-31 15:20:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	        snd_pcm_status_get_tstamp (status, &now); | 
					
						
							|  |  |  | 		snd_pcm_status_get_trigger_tstamp (status, &trigger); | 
					
						
							|  |  |  |                 timersub(&now, &trigger, &diff); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-22 13:23:48 +02:00
										 |  |  | 		delay = SPA_TIMEVAL_TO_USEC(&diff); | 
					
						
							|  |  |  | 		missing = delay * state->rate / SPA_USEC_PER_SEC; | 
					
						
							| 
									
										
										
										
											2023-08-31 14:28:56 +02:00
										 |  |  | 		missing += state->start_delay + state->threshold + state->headroom; | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 		spa_log_trace(state->log, "%p: xrun of %"PRIu64" usec %"PRIu64, | 
					
						
							| 
									
										
										
										
											2021-03-03 15:40:10 +01:00
										 |  |  | 				state, delay, missing); | 
					
						
							| 
									
										
										
										
											2019-08-22 13:23:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 14:28:56 +02:00
										 |  |  | 		if (state->clock) { | 
					
						
							|  |  |  | 			state->clock->xrun += SPA_SCALE32_UP(missing, | 
					
						
							|  |  |  | 					state->clock->rate.denom, state->rate); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-22 13:23:48 +02:00
										 |  |  | 		spa_node_call_xrun(&state->callbacks, | 
					
						
							|  |  |  | 				SPA_TIMEVAL_TO_USEC(&trigger), delay, NULL); | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-26 09:24:57 +01:00
										 |  |  | 	case SND_PCM_STATE_SUSPENDED: | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 		spa_log_info(state->log, "%s: recover from state %s", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 				state->name, snd_pcm_state_name(st)); | 
					
						
							| 
									
										
										
										
											2023-11-15 09:55:58 +01:00
										 |  |  | 		while (retry++ < 5 && (res = snd_pcm_resume(state->hndl)) == -EAGAIN) | 
					
						
							| 
									
										
										
										
											2023-11-06 17:36:48 +01:00
										 |  |  | 			/* wait until suspend flag is released */ | 
					
						
							|  |  |  | 			poll(NULL, 0, 1000); | 
					
						
							| 
									
										
										
										
											2023-11-15 09:55:58 +01:00
										 |  |  | 		if (res >= 0) | 
					
						
							| 
									
										
										
										
											2023-11-06 17:36:48 +01:00
										 |  |  | 			return res; | 
					
						
							|  |  |  | 		/* try to drop and prepare below */ | 
					
						
							| 
									
										
										
										
											2020-11-26 09:24:57 +01:00
										 |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 		spa_log_error(state->log, "%s: recover from error state %s", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 				state->name, snd_pcm_state_name(st)); | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-22 09:16:45 +02:00
										 |  |  | recover: | 
					
						
							| 
									
										
										
										
											2023-09-20 15:38:41 +02:00
										 |  |  | 	if (state->driver && state->linked) | 
					
						
							|  |  |  | 		driver = state->driver; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		driver = state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	do_drop(driver); | 
					
						
							|  |  |  | 	spa_list_for_each(follower, &driver->rt.followers, rt.driver_link) { | 
					
						
							|  |  |  | 		if (follower != driver && follower->linked) { | 
					
						
							|  |  |  | 			do_drop(follower); | 
					
						
							| 
									
										
										
										
											2025-07-15 14:52:25 +02:00
										 |  |  | 			check_position_config(follower, false); | 
					
						
							| 
									
										
										
										
											2023-09-20 15:38:41 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	do_prepare(driver); | 
					
						
							|  |  |  | 	spa_list_for_each(follower, &driver->rt.followers, rt.driver_link) { | 
					
						
							|  |  |  | 		if (follower != driver && follower->linked) | 
					
						
							|  |  |  | 			do_prepare(follower); | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-09-20 15:38:41 +02:00
										 |  |  | 	do_start(driver); | 
					
						
							|  |  |  | 	spa_list_for_each(follower, &driver->rt.followers, rt.driver_link) { | 
					
						
							|  |  |  | 		if (follower != driver && follower->linked) | 
					
						
							|  |  |  | 			do_start(follower); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-04-16 09:28:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	update_sources(state, true); | 
					
						
							| 
									
										
										
										
											2023-11-15 10:10:56 +01:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-14 16:16:48 +02:00
										 |  |  | static inline snd_pcm_sframes_t alsa_avail(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-10-18 14:42:33 +02:00
										 |  |  | 	snd_pcm_sframes_t avail; | 
					
						
							| 
									
										
										
										
											2024-05-03 07:13:41 +02:00
										 |  |  | 	if (!state->matching && state->disable_tsched && !state->resample) | 
					
						
							| 
									
										
										
										
											2023-10-18 14:42:33 +02:00
										 |  |  | 		avail = snd_pcm_avail_update(state->hndl); | 
					
						
							| 
									
										
										
										
											2023-09-14 16:16:48 +02:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2023-10-18 14:42:33 +02:00
										 |  |  | 		avail = snd_pcm_avail(state->hndl); | 
					
						
							|  |  |  | 	return avail; | 
					
						
							| 
									
										
										
										
											2023-09-14 16:16:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-05 16:33:12 +02:00
										 |  |  | static int get_avail(struct state *state, uint64_t current_time, snd_pcm_uframes_t *delay) | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-08 12:18:23 +02:00
										 |  |  | 	int res, suppressed; | 
					
						
							| 
									
										
										
										
											2022-02-10 15:18:37 +01:00
										 |  |  | 	snd_pcm_sframes_t avail; | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-14 16:16:48 +02:00
										 |  |  | 	if (SPA_UNLIKELY((avail = alsa_avail(state)) < 0)) { | 
					
						
							| 
									
										
										
										
											2023-11-15 09:55:58 +01:00
										 |  |  | 		if ((res = alsa_recover(state)) < 0) | 
					
						
							| 
									
										
										
										
											2018-11-29 15:19:33 +01:00
										 |  |  | 			return res; | 
					
						
							| 
									
										
										
										
											2023-09-14 16:16:48 +02:00
										 |  |  | 		if ((avail = alsa_avail(state)) < 0) { | 
					
						
							| 
									
										
										
										
											2023-09-08 12:18:23 +02:00
										 |  |  | 			if ((suppressed = spa_ratelimit_test(&state->rate_limit, current_time)) >= 0) { | 
					
						
							|  |  |  | 				spa_log_warn(state->log, "%s: (%d suppressed) snd_pcm_avail after recover: %s", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 						state->name, suppressed, snd_strerror(avail)); | 
					
						
							| 
									
										
										
										
											2022-09-21 15:52:24 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-03-12 10:48:36 +01:00
										 |  |  | 			avail = state->threshold * 2; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-29 15:19:33 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-05 16:33:12 +02:00
										 |  |  | 	*delay = avail; | 
					
						
							| 
									
										
										
										
											2022-02-10 15:18:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-03 17:08:08 +02:00
										 |  |  | 	if (state->htimestamp) { | 
					
						
							|  |  |  | 		snd_pcm_uframes_t havail; | 
					
						
							|  |  |  | 		snd_htimestamp_t tstamp; | 
					
						
							|  |  |  | 		uint64_t then; | 
					
						
							| 
									
										
										
										
											2022-02-10 15:18:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-03 17:08:08 +02:00
										 |  |  | 		if ((res = snd_pcm_htimestamp(state->hndl, &havail, &tstamp)) < 0) { | 
					
						
							| 
									
										
										
										
											2023-09-08 12:18:23 +02:00
										 |  |  | 			if ((suppressed = spa_ratelimit_test(&state->rate_limit, current_time)) >= 0) { | 
					
						
							|  |  |  | 				spa_log_warn(state->log, "%s: (%d suppressed) snd_pcm_htimestamp error: %s", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 					state->name, suppressed, snd_strerror(res)); | 
					
						
							| 
									
										
										
										
											2022-09-21 15:52:24 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-07-03 17:08:08 +02:00
										 |  |  | 			return avail; | 
					
						
							| 
									
										
										
										
											2022-02-10 15:18:37 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-07-03 17:08:08 +02:00
										 |  |  | 		avail = havail; | 
					
						
							|  |  |  | 		*delay = havail; | 
					
						
							|  |  |  | 		if ((then = SPA_TIMESPEC_TO_NSEC(&tstamp)) != 0) { | 
					
						
							|  |  |  | 			int64_t diff; | 
					
						
							| 
									
										
										
										
											2022-02-10 15:18:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-03 17:08:08 +02:00
										 |  |  | 			if (then < current_time) | 
					
						
							|  |  |  | 				diff = ((int64_t)(current_time - then)) * state->rate / SPA_NSEC_PER_SEC; | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				diff = -((int64_t)(then - current_time)) * state->rate / SPA_NSEC_PER_SEC; | 
					
						
							| 
									
										
										
										
											2023-06-05 16:33:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-03 17:08:08 +02:00
										 |  |  | 			spa_log_trace_fp(state->log, "%"PRIu64" %"PRIu64" %"PRIi64, current_time, then, diff); | 
					
						
							| 
									
										
										
										
											2023-06-05 16:33:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-05 11:31:38 +02:00
										 |  |  | 			if (SPA_ABS(diff) < state->threshold * 3) { | 
					
						
							| 
									
										
										
										
											2023-07-05 12:01:08 +02:00
										 |  |  | 				*delay += SPA_CLAMP(diff, -((int64_t)state->threshold), (int64_t)state->threshold); | 
					
						
							| 
									
										
										
										
											2023-06-29 16:40:21 +02:00
										 |  |  | 				state->htimestamp_error = 0; | 
					
						
							| 
									
										
										
										
											2024-02-20 08:47:03 +01:00
										 |  |  | 			} else if (state->htimestamp_max_errors) { | 
					
						
							|  |  |  | 				if (++state->htimestamp_error > state->htimestamp_max_errors) { | 
					
						
							| 
									
										
										
										
											2023-07-03 17:08:08 +02:00
										 |  |  | 					spa_log_error(state->log, "%s: wrong htimestamps from driver, disabling", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 						state->name); | 
					
						
							| 
									
										
										
										
											2023-07-03 17:08:08 +02:00
										 |  |  | 					state->htimestamp_error = 0; | 
					
						
							|  |  |  | 					state->htimestamp = false; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2023-09-08 12:18:23 +02:00
										 |  |  | 				else if ((suppressed = spa_ratelimit_test(&state->rate_limit, current_time)) >= 0) { | 
					
						
							|  |  |  | 					spa_log_warn(state->log, "%s: (%d suppressed) impossible htimestamp diff:%"PRIi64, | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 						state->name, suppressed, diff); | 
					
						
							| 
									
										
										
										
											2023-07-03 17:08:08 +02:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2023-06-29 16:40:21 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-06-29 15:56:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-02-10 15:18:37 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-03 17:08:08 +02:00
										 |  |  | 	return avail; | 
					
						
							| 
									
										
										
										
											2022-02-10 15:18:37 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-05 16:33:12 +02:00
										 |  |  | static int get_status(struct state *state, uint64_t current_time, snd_pcm_uframes_t *avail, | 
					
						
							| 
									
										
										
										
											2022-02-10 15:18:37 +01:00
										 |  |  | 		snd_pcm_uframes_t *delay, snd_pcm_uframes_t *target) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-05 16:33:12 +02:00
										 |  |  | 	int res; | 
					
						
							|  |  |  | 	snd_pcm_uframes_t a, d; | 
					
						
							| 
									
										
										
										
											2022-02-10 15:18:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-03 17:08:08 +02:00
										 |  |  | 	if ((res = get_avail(state, current_time, &d)) < 0) | 
					
						
							| 
									
										
										
										
											2023-06-05 16:33:12 +02:00
										 |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-05 16:33:12 +02:00
										 |  |  | 	a = SPA_MIN(res, (int)state->buffer_frames); | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-27 15:56:20 +02:00
										 |  |  | 	if (state->resample && state->rate_match) { | 
					
						
							| 
									
										
										
										
											2021-03-14 14:13:37 +01:00
										 |  |  | 		state->delay = state->rate_match->delay; | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 		state->read_size = state->rate_match->size; | 
					
						
							| 
									
										
										
										
											2019-10-23 16:04:03 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2021-03-04 10:13:26 +01:00
										 |  |  | 		state->delay = 0; | 
					
						
							|  |  |  | 		state->read_size = state->threshold; | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (state->stream == SND_PCM_STREAM_PLAYBACK) { | 
					
						
							| 
									
										
										
										
											2023-06-05 16:33:12 +02:00
										 |  |  | 		*avail = state->buffer_frames - a; | 
					
						
							|  |  |  | 		*delay = state->buffer_frames - SPA_MIN(d, state->buffer_frames); | 
					
						
							|  |  |  | 		*target = state->threshold + state->headroom; | 
					
						
							| 
									
										
										
										
											2021-03-14 17:08:04 +01:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2023-06-05 16:33:12 +02:00
										 |  |  | 		*avail = a; | 
					
						
							|  |  |  | 		*delay = d; | 
					
						
							|  |  |  | 		*target = SPA_MAX(state->threshold, state->read_size) + state->headroom; | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-03-03 14:10:33 +01:00
										 |  |  | 	*target = SPA_CLAMP(*target, state->min_delay, state->max_delay); | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 15:18:37 +01:00
										 |  |  | static int update_time(struct state *state, uint64_t current_time, snd_pcm_sframes_t delay, | 
					
						
							| 
									
										
										
										
											2020-02-21 10:47:32 +01:00
										 |  |  | 		snd_pcm_sframes_t target, bool follower) | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-10-04 10:47:47 +02:00
										 |  |  | 	double err, corr, avg; | 
					
						
							| 
									
										
										
										
											2021-03-08 16:12:35 +01:00
										 |  |  | 	int32_t diff; | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-11 13:03:18 +02:00
										 |  |  | 	if (SPA_UNLIKELY(state->dll.bw == 0.0)) { | 
					
						
							| 
									
										
										
										
											2025-07-04 14:51:15 +02:00
										 |  |  | 		spa_dll_set_bw(&state->dll, state->dll_bw_max, state->threshold, state->rate); | 
					
						
							| 
									
										
										
										
											2025-06-11 13:03:18 +02:00
										 |  |  | 		state->next_time = current_time; | 
					
						
							|  |  |  | 		state->base_time = current_time; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-10 10:47:36 +02:00
										 |  |  | 	if (state->disable_tsched && !follower) { | 
					
						
							| 
									
										
										
										
											2023-12-12 10:06:46 +01:00
										 |  |  | 		err = (int64_t)(current_time - state->next_time); | 
					
						
							|  |  |  | 		err = err / 1e9 * state->rate; | 
					
						
							| 
									
										
										
										
											2023-04-10 10:47:36 +02:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		if (state->stream == SND_PCM_STREAM_PLAYBACK) | 
					
						
							|  |  |  | 			err = delay - target; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			err = target - delay; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-03-03 15:40:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 16:12:35 +01:00
										 |  |  | 	diff = (int32_t) (state->last_threshold - state->threshold); | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 16:12:35 +01:00
										 |  |  | 	if (SPA_UNLIKELY(diff != 0)) { | 
					
						
							|  |  |  | 		err -= diff; | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 		spa_log_trace(state->log, "%p: follower:%d quantum change %d -> %d (%d) %f", | 
					
						
							| 
									
										
										
										
											2021-03-08 16:12:35 +01:00
										 |  |  | 				state, follower, state->last_threshold, state->threshold, diff, err); | 
					
						
							| 
									
										
										
										
											2019-09-24 17:23:01 +02:00
										 |  |  | 		state->last_threshold = state->threshold; | 
					
						
							| 
									
										
										
										
											2022-02-16 21:30:35 +01:00
										 |  |  | 		state->alsa_sync = true; | 
					
						
							| 
									
										
										
										
											2022-09-21 16:39:31 +02:00
										 |  |  | 		state->alsa_sync_warning = false; | 
					
						
							| 
									
										
										
										
											2019-04-25 16:15:52 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-04-10 16:28:55 +02:00
										 |  |  | 	if (err > state->max_resync) { | 
					
						
							| 
									
										
										
										
											2022-02-15 16:29:43 +01:00
										 |  |  | 		state->alsa_sync = true; | 
					
						
							| 
									
										
										
										
											2023-04-10 16:28:55 +02:00
										 |  |  | 		if (err > state->max_error) | 
					
						
							|  |  |  | 			err = state->max_error; | 
					
						
							|  |  |  | 	} else if (err < -state->max_resync) { | 
					
						
							| 
									
										
										
										
											2022-02-15 16:29:43 +01:00
										 |  |  | 		state->alsa_sync = true; | 
					
						
							| 
									
										
										
										
											2023-04-10 16:28:55 +02:00
										 |  |  | 		if (err < -state->max_error) | 
					
						
							|  |  |  | 			err = -state->max_error; | 
					
						
							| 
									
										
										
										
											2022-02-15 16:29:43 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-02-15 15:29:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-04 10:47:47 +02:00
										 |  |  | 	if (!follower || state->matching) { | 
					
						
							| 
									
										
										
										
											2022-02-15 15:29:40 +01:00
										 |  |  | 		corr = spa_dll_update(&state->dll, err); | 
					
						
							| 
									
										
										
										
											2024-10-04 10:47:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		avg = (state->err_avg * state->err_wdw + (err - state->err_avg)) / (state->err_wdw + 1.0); | 
					
						
							|  |  |  | 		state->err_var = (state->err_var * state->err_wdw + | 
					
						
							|  |  |  | 				(err - state->err_avg) * (err - avg)) / (state->err_wdw + 1.0); | 
					
						
							|  |  |  | 		state->err_avg = avg; | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2022-02-15 15:29:40 +01:00
										 |  |  | 		corr = 1.0; | 
					
						
							| 
									
										
										
										
											2024-10-04 10:47:47 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-03-08 16:12:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (diff < 0) | 
					
						
							| 
									
										
										
										
											2024-06-18 12:17:56 +02:00
										 |  |  | 		state->next_time += (uint64_t)(diff / corr * 1e9 / state->rate); | 
					
						
							| 
									
										
										
										
											2019-04-25 16:15:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-16 12:52:28 +01:00
										 |  |  | 	if (SPA_UNLIKELY((state->next_time - state->base_time) > BW_PERIOD)) { | 
					
						
							| 
									
										
										
										
											2024-12-03 12:10:04 +01:00
										 |  |  | 		double bw; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 		state->base_time = state->next_time; | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 12:10:04 +01:00
										 |  |  | 		bw = (fabs(state->err_avg) + sqrt(fabs(state->err_var)))/1000.0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 16:44:45 +01:00
										 |  |  | 		spa_log_debug(state->log, "%s: follower:%d match:%d rate:%f " | 
					
						
							| 
									
										
										
										
											2025-02-28 12:30:45 +01:00
										 |  |  | 				"bw:%f thr:%u del:%ld target:%ld err:%f max_err:%f max_resync: %f var:%f:%f:%f", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 				state->name, follower, state->matching, | 
					
						
							| 
									
										
										
										
											2022-02-15 15:29:40 +01:00
										 |  |  | 				corr, state->dll.bw, state->threshold, delay, target, | 
					
						
							| 
									
										
										
										
											2025-02-28 12:30:45 +01:00
										 |  |  | 				err, state->max_error, state->max_resync, state->err_avg, state->err_var, bw); | 
					
						
							| 
									
										
										
										
											2024-10-04 10:47:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		spa_dll_set_bw(&state->dll, | 
					
						
							| 
									
										
										
										
											2025-07-04 14:51:15 +02:00
										 |  |  | 				SPA_CLAMPD(bw, SPA_ALSA_DLL_BW_MIN, state->dll_bw_max), | 
					
						
							| 
									
										
										
										
											2024-10-04 10:47:47 +02:00
										 |  |  | 				state->threshold, state->rate); | 
					
						
							| 
									
										
										
										
											2018-11-09 15:24:24 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-31 15:20:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-21 10:13:56 +02:00
										 |  |  | 	if (state->rate_match) { | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 		if (state->stream == SND_PCM_STREAM_PLAYBACK) | 
					
						
							| 
									
										
										
										
											2021-03-03 15:40:10 +01:00
										 |  |  | 			state->rate_match->rate = corr; | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											2021-03-03 15:40:10 +01:00
										 |  |  | 			state->rate_match->rate = 1.0/corr; | 
					
						
							| 
									
										
										
										
											2019-10-21 10:13:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 17:37:04 -04:00
										 |  |  | 		if (state->pitch_elem && state->matching) | 
					
						
							|  |  |  | 			spa_alsa_update_rate_match(state); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			SPA_FLAG_UPDATE(state->rate_match->flags, SPA_IO_RATE_MATCH_FLAG_ACTIVE, state->matching); | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-09-24 17:23:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-18 12:17:56 +02:00
										 |  |  | 	state->next_time += (uint64_t)(state->threshold / corr * 1e9 / state->rate); | 
					
						
							| 
									
										
										
										
											2019-09-24 17:23:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-09 11:27:23 +02:00
										 |  |  | 	if (SPA_LIKELY(state->clock)) { | 
					
						
							| 
									
										
										
										
											2022-02-10 15:18:37 +01:00
										 |  |  | 		state->clock->nsec = current_time; | 
					
						
							| 
									
										
										
										
											2023-09-20 11:32:34 +02:00
										 |  |  | 		state->clock->rate = state->driver_rate; | 
					
						
							| 
									
										
										
										
											2023-03-24 11:20:01 +01:00
										 |  |  | 		state->clock->position += state->clock->duration; | 
					
						
							| 
									
										
										
										
											2023-09-20 11:32:34 +02:00
										 |  |  | 		state->clock->duration = state->driver_duration; | 
					
						
							| 
									
										
										
										
											2021-03-08 17:12:00 +01:00
										 |  |  | 		state->clock->delay = delay + state->delay; | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 		state->clock->rate_diff = corr; | 
					
						
							| 
									
										
										
										
											2019-08-27 14:41:47 +02:00
										 |  |  | 		state->clock->next_nsec = state->next_time; | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-18 18:19:16 +02:00
										 |  |  | 	spa_log_trace_fp(state->log, "%p: follower:%d %"PRIu64" %"PRIu64" %f %ld %ld %f %f %u %d", | 
					
						
							| 
									
										
										
										
											2025-06-11 13:03:18 +02:00
										 |  |  | 			state, follower, current_time, state->next_time, corr, delay, target, | 
					
						
							| 
									
										
										
										
											2025-07-18 18:19:16 +02:00
										 |  |  | 			err, state->threshold * corr, state->threshold, state->matching); | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-31 15:20:52 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-18 19:20:00 +02:00
										 |  |  | static bool need_resample(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return !state->pitch_elem && | 
					
						
							|  |  |  | 		((state->rate != 0 && state->driver_rate.denom != 0 && | 
					
						
							|  |  |  | 		 (uint32_t)state->rate != state->driver_rate.denom) || state->matching); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 12:09:13 +02:00
										 |  |  | static int setup_matching(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	state->matching = state->following; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state->position == NULL) | 
					
						
							|  |  |  | 		return -ENOTSUP; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-10 11:04:38 +01:00
										 |  |  | 	spa_log_debug(state->log, "driver clock:'%s' our clock:'%s'", | 
					
						
							|  |  |  | 			state->position->clock.name, state->clock_name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (spa_streq(state->position->clock.name, state->clock_name)) | 
					
						
							| 
									
										
										
										
											2021-06-14 12:09:13 +02:00
										 |  |  | 		state->matching = false; | 
					
						
							| 
									
										
										
										
											2021-12-10 11:04:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-18 19:20:00 +02:00
										 |  |  | 	state->resample = need_resample(state); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-15 15:06:24 +02:00
										 |  |  | 	check_position_config(state, false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-18 14:45:47 +02:00
										 |  |  | 	recalc_headroom(state); | 
					
						
							| 
									
										
										
										
											2022-06-08 17:03:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	spa_log_info(state->log, "driver clock:'%s'@%d our clock:'%s'@%d matching:%d resample:%d", | 
					
						
							| 
									
										
										
										
											2023-09-20 11:32:34 +02:00
										 |  |  | 			state->position->clock.name, state->driver_rate.denom, | 
					
						
							| 
									
										
										
										
											2022-06-08 17:03:50 +02:00
										 |  |  | 			state->clock_name, state->rate, | 
					
						
							|  |  |  | 			state->matching, state->resample); | 
					
						
							| 
									
										
										
										
											2021-06-14 12:09:13 +02:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-29 11:30:40 +02:00
										 |  |  | static void update_sources(struct state *state, bool active) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 	if (state->disable_tsched && state->rt.sources_added) { | 
					
						
							| 
									
										
										
										
											2023-03-29 11:30:40 +02:00
										 |  |  | 		for (int i = 0; i < state->n_fds; i++) { | 
					
						
							|  |  |  | 			state->source[i].mask = active ? state->pfds[i].events : 0; | 
					
						
							|  |  |  | 			spa_loop_update_source(state->data_loop, &state->source[i]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-15 14:52:25 +02:00
										 |  |  | static inline int check_position_config(struct state *state, bool starting) | 
					
						
							| 
									
										
										
										
											2021-06-14 12:09:13 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-03-24 11:20:01 +01:00
										 |  |  | 	uint64_t target_duration; | 
					
						
							|  |  |  | 	struct spa_fraction target_rate; | 
					
						
							| 
									
										
										
										
											2023-09-20 11:32:34 +02:00
										 |  |  | 	struct spa_io_position *pos; | 
					
						
							| 
									
										
										
										
											2025-09-25 12:29:05 +02:00
										 |  |  | 	bool can_force; | 
					
						
							| 
									
										
										
										
											2023-03-24 11:20:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 11:32:34 +02:00
										 |  |  | 	if (SPA_UNLIKELY((pos = state->position) == NULL)) | 
					
						
							| 
									
										
										
										
											2023-03-23 18:04:36 +01:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2021-06-14 12:09:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-25 12:29:05 +02:00
										 |  |  | 	/* we can force rate/duration when we are the driver and started/starting */ | 
					
						
							|  |  |  | 	can_force = (starting || state->started) && !state->following; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state->force_quantum && can_force) { | 
					
						
							| 
									
										
										
										
											2023-08-28 12:43:38 +02:00
										 |  |  | 		target_rate = SPA_FRACTION(1, state->rate); | 
					
						
							| 
									
										
										
										
											2025-09-25 12:29:05 +02:00
										 |  |  | 		target_duration = state->duration; | 
					
						
							| 
									
										
										
										
											2023-09-20 11:32:34 +02:00
										 |  |  | 		pos->clock.target_rate = target_rate; | 
					
						
							| 
									
										
										
										
											2025-09-25 12:29:05 +02:00
										 |  |  | 		pos->clock.target_duration = target_duration; | 
					
						
							| 
									
										
										
										
											2023-08-28 12:43:38 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2025-09-25 12:29:05 +02:00
										 |  |  | 		target_rate = pos->clock.target_rate; | 
					
						
							| 
									
										
										
										
											2023-09-20 11:32:34 +02:00
										 |  |  | 		target_duration = pos->clock.target_duration; | 
					
						
							| 
									
										
										
										
											2023-08-28 12:43:38 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-09-15 11:15:26 +02:00
										 |  |  | 	if (target_duration == 0 || target_rate.denom == 0) | 
					
						
							|  |  |  | 		return -EIO; | 
					
						
							| 
									
										
										
										
											2023-03-24 11:20:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 11:32:34 +02:00
										 |  |  | 	if (SPA_UNLIKELY((state->driver_duration != target_duration) || | 
					
						
							|  |  |  | 	    (state->driver_rate.denom != target_rate.denom))) { | 
					
						
							|  |  |  | 		spa_log_info(state->log, "%p: follower:%d duration:%u->%"PRIu64" rate:%d->%d", | 
					
						
							|  |  |  | 				state, state->following, state->driver_duration, target_duration, | 
					
						
							|  |  |  | 				state->driver_rate.denom, target_rate.denom); | 
					
						
							| 
									
										
										
										
											2023-09-20 15:38:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 11:32:34 +02:00
										 |  |  | 		state->driver_duration = target_duration; | 
					
						
							|  |  |  | 		state->driver_rate = target_rate; | 
					
						
							| 
									
										
										
										
											2025-07-15 14:52:25 +02:00
										 |  |  | 		state->threshold = SPA_SCALE32_UP(state->driver_duration, state->rate, state->driver_rate.denom); | 
					
						
							|  |  |  | 		state->max_error = SPA_MAX(256.0f, (state->threshold + state->headroom) / 2.0f); | 
					
						
							|  |  |  | 		state->max_resync = SPA_MIN(state->threshold + state->headroom, state->max_error); | 
					
						
							|  |  |  | 		state->err_wdw = (double)state->driver_rate.denom/state->driver_duration; | 
					
						
							| 
									
										
										
										
											2025-07-18 19:20:00 +02:00
										 |  |  | 		state->resample = need_resample(state); | 
					
						
							| 
									
										
										
										
											2025-07-15 14:52:25 +02:00
										 |  |  | 		state->alsa_sync = true; | 
					
						
							| 
									
										
										
										
											2021-06-14 12:09:13 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-03-23 18:04:36 +01:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2021-06-14 12:09:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | static int alsa_write_sync(struct state *state, uint64_t current_time) | 
					
						
							| 
									
										
										
										
											2017-10-22 13:12:34 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-08 12:18:23 +02:00
										 |  |  | 	int res, suppressed; | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 	snd_pcm_uframes_t avail, delay, target; | 
					
						
							|  |  |  | 	bool following = state->following; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-15 14:52:25 +02:00
										 |  |  | 	if (SPA_UNLIKELY((res = check_position_config(state, false)) < 0)) | 
					
						
							| 
									
										
										
										
											2023-03-23 18:04:36 +01:00
										 |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2018-11-12 10:18:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-12 15:17:48 +02:00
										 |  |  | 	if (SPA_UNLIKELY((res = get_status(state, current_time, &avail, &delay, &target)) < 0)) { | 
					
						
							| 
									
										
										
										
											2023-11-15 10:10:56 +01:00
										 |  |  | 		spa_log_error(state->log, "get_status error: %s", spa_strerror(res)); | 
					
						
							| 
									
										
										
										
											2024-06-18 12:17:56 +02:00
										 |  |  | 		state->next_time += (uint64_t)(state->threshold * 1e9 / state->rate); | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2023-10-12 15:17:48 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-12 10:18:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-12 15:28:53 +02:00
										 |  |  | 	if (SPA_UNLIKELY(!following && state->alsa_started && delay > target + state->max_error)) { | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 		spa_log_trace(state->log, "%p: early wakeup %ld %lu %lu", state, | 
					
						
							|  |  |  | 				avail, delay, target); | 
					
						
							|  |  |  | 		if (delay > target * 3) | 
					
						
							|  |  |  | 			delay = target * 3; | 
					
						
							|  |  |  | 		state->next_time = current_time + (delay - target) * SPA_NSEC_PER_SEC / state->rate; | 
					
						
							|  |  |  | 		return -EAGAIN; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (SPA_UNLIKELY((res = update_time(state, current_time, delay, target, following)) < 0)) | 
					
						
							|  |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2022-05-27 21:20:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-11 13:06:54 +02:00
										 |  |  | 	if (following && state->alsa_started) { | 
					
						
							| 
									
										
										
										
											2020-03-16 12:52:28 +01:00
										 |  |  | 		if (SPA_UNLIKELY(state->alsa_sync)) { | 
					
						
							| 
									
										
										
										
											2022-09-21 15:52:24 +02:00
										 |  |  | 			enum spa_log_level lev; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-11 13:06:54 +02:00
										 |  |  | 			if (!state->linked) { | 
					
						
							|  |  |  | 				if (SPA_UNLIKELY(state->alsa_sync_warning)) | 
					
						
							|  |  |  | 					lev = SPA_LOG_LEVEL_WARN; | 
					
						
							|  |  |  | 				else | 
					
						
							|  |  |  | 					lev = SPA_LOG_LEVEL_INFO; | 
					
						
							| 
									
										
										
										
											2022-09-21 15:52:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-11 13:06:54 +02:00
										 |  |  | 				if ((suppressed = spa_ratelimit_test(&state->rate_limit, current_time)) < 0) | 
					
						
							|  |  |  | 					lev = SPA_LOG_LEVEL_DEBUG; | 
					
						
							| 
									
										
										
										
											2023-10-18 14:49:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-11 13:06:54 +02:00
										 |  |  | 				spa_log_lev(state->log, lev, "%s: follower avail:%lu delay:%ld " | 
					
						
							|  |  |  | 						"target:%ld thr:%u, resync (%d suppressed)", | 
					
						
							|  |  |  | 						state->name, avail, delay, | 
					
						
							|  |  |  | 						target, state->threshold, suppressed); | 
					
						
							| 
									
										
										
										
											2022-05-11 17:41:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-13 12:33:35 +02:00
										 |  |  | 				if (delay > target) | 
					
						
							|  |  |  | 					snd_pcm_rewind(state->hndl, delay - target); | 
					
						
							|  |  |  | 				else if (delay < target) | 
					
						
							|  |  |  | 					spa_alsa_silence(state, target - delay); | 
					
						
							| 
									
										
										
										
											2025-06-11 13:06:54 +02:00
										 |  |  | 				avail = target; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-10-18 14:49:46 +02:00
										 |  |  | 			spa_dll_init(&state->dll); | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 			state->alsa_sync = false; | 
					
						
							| 
									
										
										
										
											2022-09-21 16:39:31 +02:00
										 |  |  | 		} else | 
					
						
							|  |  |  | 			state->alsa_sync_warning = true; | 
					
						
							| 
									
										
										
										
											2018-11-12 10:18:21 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int alsa_write_frames(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	snd_pcm_t *hndl = state->hndl; | 
					
						
							|  |  |  | 	const snd_pcm_channel_area_t *my_areas; | 
					
						
							|  |  |  | 	snd_pcm_uframes_t written, frames, offset, off, to_write, total_written; | 
					
						
							|  |  |  | 	snd_pcm_sframes_t commitres; | 
					
						
							|  |  |  | 	int res = 0; | 
					
						
							|  |  |  | 	size_t frame_size = state->frame_size; | 
					
						
							| 
									
										
										
										
											2018-11-12 10:18:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	total_written = 0; | 
					
						
							|  |  |  | again: | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 	frames = state->buffer_frames; | 
					
						
							| 
									
										
										
										
											2022-02-15 16:29:43 +01:00
										 |  |  | 	if (state->use_mmap && frames > 0) { | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 		if (SPA_UNLIKELY((res = snd_pcm_mmap_begin(hndl, &my_areas, &offset, &frames)) < 0)) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 			spa_log_error(state->log, "%s: snd_pcm_mmap_begin error: %s", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 					state->name, snd_strerror(res)); | 
					
						
							| 
									
										
										
										
											2023-11-15 09:55:58 +01:00
										 |  |  | 			alsa_recover(state); | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 			return res; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-03-30 11:34:30 +02:00
										 |  |  | 		spa_log_trace_fp(state->log, "%p: begin offset:%ld avail:%ld threshold:%d", | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 				state, offset, frames, state->threshold); | 
					
						
							|  |  |  | 		off = offset; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		off = 0; | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	to_write = frames; | 
					
						
							|  |  |  | 	written = 0; | 
					
						
							| 
									
										
										
										
											2017-10-22 13:12:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	while (!spa_list_is_empty(&state->ready) && to_write > 0) { | 
					
						
							| 
									
										
										
										
											2017-11-15 17:25:36 +01:00
										 |  |  | 		size_t n_bytes, n_frames; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 		struct buffer *b; | 
					
						
							|  |  |  | 		struct spa_data *d; | 
					
						
							| 
									
										
										
										
											2022-07-13 09:37:01 +02:00
										 |  |  | 		uint32_t i, offs, size, last_offset; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		b = spa_list_first(&state->ready, struct buffer, link); | 
					
						
							| 
									
										
										
										
											2018-05-11 09:56:46 +02:00
										 |  |  | 		d = b->buf->datas; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-24 10:38:13 +02:00
										 |  |  | 		offs = d[0].chunk->offset + state->ready_offset; | 
					
						
							| 
									
										
										
										
											2022-07-13 09:37:01 +02:00
										 |  |  | 		last_offset = d[0].chunk->size; | 
					
						
							|  |  |  | 		size = last_offset - state->ready_offset; | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-24 10:38:13 +02:00
										 |  |  | 		offs = SPA_MIN(offs, d[0].maxsize); | 
					
						
							|  |  |  | 		size = SPA_MIN(d[0].maxsize - offs, size); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-24 10:38:13 +02:00
										 |  |  | 		n_frames = SPA_MIN(size / frame_size, to_write); | 
					
						
							|  |  |  | 		n_bytes = n_frames * frame_size; | 
					
						
							| 
									
										
										
										
											2017-11-21 12:30:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 		if (SPA_LIKELY(state->use_mmap)) { | 
					
						
							|  |  |  | 			for (i = 0; i < b->buf->n_datas; i++) { | 
					
						
							| 
									
										
										
										
											2023-04-14 19:25:51 +02:00
										 |  |  | 				spa_memcpy(channel_area_addr(&my_areas[i], off), | 
					
						
							| 
									
										
										
										
											2022-06-24 10:38:13 +02:00
										 |  |  | 						SPA_PTROFF(d[i].data, offs, void), n_bytes); | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2022-06-24 10:38:13 +02:00
										 |  |  | 			void *bufs[b->buf->n_datas]; | 
					
						
							|  |  |  | 			for (i = 0; i < b->buf->n_datas; i++) | 
					
						
							|  |  |  | 				bufs[i] = SPA_PTROFF(d[i].data, offs, void); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (state->planar) | 
					
						
							|  |  |  | 				snd_pcm_writen(hndl, bufs, n_frames); | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				snd_pcm_writei(hndl, bufs[0], n_frames); | 
					
						
							| 
									
										
										
										
											2020-07-01 11:42:10 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 12:30:15 +01:00
										 |  |  | 		state->ready_offset += n_bytes; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-13 09:37:01 +02:00
										 |  |  | 		if (state->ready_offset >= last_offset) { | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 			spa_list_remove(&b->link); | 
					
						
							| 
									
										
										
										
											2018-03-08 11:02:21 +01:00
										 |  |  | 			SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUT); | 
					
						
							| 
									
										
										
										
											2019-01-07 17:57:03 +01:00
										 |  |  | 			state->io->buffer_id = b->id; | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 			spa_log_trace_fp(state->log, "%p: reuse buffer %u", state, b->id); | 
					
						
							| 
									
										
										
										
											2019-05-15 11:19:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			spa_node_call_reuse_buffer(&state->callbacks, 0, b->id); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 12:30:15 +01:00
										 |  |  | 			state->ready_offset = 0; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 		written += n_frames; | 
					
						
							| 
									
										
										
										
											2018-07-31 17:40:32 +02:00
										 |  |  | 		off += n_frames; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 		to_write -= n_frames; | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-31 15:59:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-30 11:34:30 +02:00
										 |  |  | 	spa_log_trace_fp(state->log, "%p: commit offset:%ld written:%ld sample_count:%"PRIi64, | 
					
						
							| 
									
										
										
										
											2019-09-23 20:51:34 +02:00
										 |  |  | 			state, offset, written, state->sample_count); | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	total_written += written; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-12 12:09:25 +01:00
										 |  |  | 	if (state->use_mmap && written > 0) { | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 		if (SPA_UNLIKELY((commitres = snd_pcm_mmap_commit(hndl, offset, written)) < 0)) { | 
					
						
							| 
									
										
										
										
											2025-01-20 12:14:14 +00:00
										 |  |  | 			if (commitres == -EPIPE || commitres == -ESTRPIPE) { | 
					
						
							|  |  |  | 				spa_log_warn(state->log, "%s: snd_pcm_mmap_commit error: %s", | 
					
						
							|  |  |  | 						state->name, snd_strerror(commitres)); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				spa_log_error(state->log, "%s: snd_pcm_mmap_commit error: %s", | 
					
						
							|  |  |  | 						state->name, snd_strerror(commitres)); | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 				return res; | 
					
						
							| 
									
										
										
										
											2025-01-20 12:14:14 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (commitres > 0 && written != (snd_pcm_uframes_t) commitres) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 			spa_log_warn(state->log, "%s: mmap_commit wrote %ld instead of %ld", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 				     state->name, commitres, written); | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-12-20 00:23:52 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-02 23:04:09 +02:00
										 |  |  | 	if (!spa_list_is_empty(&state->ready) && written > 0) | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 		goto again; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	state->sample_count += total_written; | 
					
						
							| 
									
										
										
										
											2017-10-24 18:04:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-16 20:45:38 +01:00
										 |  |  | 	if (SPA_UNLIKELY(!state->alsa_started && (total_written > 0 || frames == 0))) | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 		do_start(state); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-29 11:30:40 +02:00
										 |  |  | 	update_sources(state, true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2016-11-03 19:41:53 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | int spa_alsa_write(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 	if (state->following && state->rt.driver == NULL) { | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 		uint64_t current_time = state->position->clock.nsec; | 
					
						
							| 
									
										
										
										
											2023-10-12 11:38:36 +02:00
										 |  |  | 		alsa_write_sync(state, current_time); | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-09-15 13:05:31 +02:00
										 |  |  | 	return alsa_write_frames(state); | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 13:12:49 +01:00
										 |  |  | void spa_alsa_recycle_buffer(struct state *this, uint32_t buffer_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct buffer *b = &this->buffers[buffer_id]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (SPA_FLAG_IS_SET(b->flags, BUFFER_FLAG_OUT)) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 		spa_log_trace_fp(this->log, "%p: recycle buffer %u", this, buffer_id); | 
					
						
							| 
									
										
										
										
											2020-03-19 13:12:49 +01:00
										 |  |  | 		spa_list_append(&this->free, &b->link); | 
					
						
							|  |  |  | 		SPA_FLAG_CLEAR(b->flags, BUFFER_FLAG_OUT); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-03 19:41:53 +01:00
										 |  |  | static snd_pcm_uframes_t | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | push_frames(struct state *state, | 
					
						
							| 
									
										
										
										
											2017-05-26 18:19:51 +02:00
										 |  |  | 	    const snd_pcm_channel_area_t *my_areas, | 
					
						
							|  |  |  | 	    snd_pcm_uframes_t offset, | 
					
						
							| 
									
										
										
										
											2021-01-08 16:35:26 +01:00
										 |  |  | 	    snd_pcm_uframes_t frames) | 
					
						
							| 
									
										
										
										
											2016-09-19 09:16:58 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	snd_pcm_uframes_t total_frames = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (spa_list_is_empty(&state->free)) { | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 		spa_log_warn(state->log, "%s: no more buffers", state->name); | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 		total_frames = frames; | 
					
						
							| 
									
										
										
										
											2021-03-04 10:27:44 +01:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2022-06-24 10:38:13 +02:00
										 |  |  | 		size_t n_bytes, left, frame_size = state->frame_size; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 		struct buffer *b; | 
					
						
							|  |  |  | 		struct spa_data *d; | 
					
						
							| 
									
										
										
										
											2020-07-01 11:42:10 +02:00
										 |  |  | 		uint32_t i, avail, l0, l1; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		b = spa_list_first(&state->free, struct buffer, link); | 
					
						
							|  |  |  | 		spa_list_remove(&b->link); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (b->h) { | 
					
						
							|  |  |  | 			b->h->seq = state->sample_count; | 
					
						
							| 
									
										
										
										
											2019-09-19 16:55:02 +02:00
										 |  |  | 			b->h->pts = state->next_time; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 			b->h->dts_offset = 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-11 09:56:46 +02:00
										 |  |  | 		d = b->buf->datas; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-24 10:38:13 +02:00
										 |  |  | 		avail = d[0].maxsize / frame_size; | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 		total_frames = SPA_MIN(avail, frames); | 
					
						
							| 
									
										
										
										
											2022-06-24 10:38:13 +02:00
										 |  |  | 		n_bytes = total_frames * frame_size; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 		if (my_areas) { | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 			left = state->buffer_frames - offset; | 
					
						
							| 
									
										
										
										
											2022-06-24 10:38:13 +02:00
										 |  |  | 			l0 = SPA_MIN(n_bytes, left * frame_size); | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 			l1 = n_bytes - l0; | 
					
						
							| 
									
										
										
										
											2017-11-21 12:30:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-01 11:42:10 +02:00
										 |  |  | 			for (i = 0; i < b->buf->n_datas; i++) { | 
					
						
							| 
									
										
										
										
											2022-06-24 10:38:13 +02:00
										 |  |  | 				spa_memcpy(d[i].data, | 
					
						
							| 
									
										
										
										
											2023-04-14 19:25:51 +02:00
										 |  |  | 						channel_area_addr(&my_areas[i], offset), | 
					
						
							| 
									
										
										
										
											2022-06-24 10:38:13 +02:00
										 |  |  | 						l0); | 
					
						
							|  |  |  | 				if (SPA_UNLIKELY(l1 > 0)) | 
					
						
							|  |  |  | 					spa_memcpy(SPA_PTROFF(d[i].data, l0, void), | 
					
						
							| 
									
										
										
										
											2023-04-14 19:25:51 +02:00
										 |  |  | 							channel_area_addr(&my_areas[i], 0), | 
					
						
							| 
									
										
										
										
											2022-06-24 10:38:13 +02:00
										 |  |  | 							l1); | 
					
						
							| 
									
										
										
										
											2020-07-01 11:42:10 +02:00
										 |  |  | 				d[i].chunk->offset = 0; | 
					
						
							|  |  |  | 				d[i].chunk->size = n_bytes; | 
					
						
							| 
									
										
										
										
											2022-06-24 10:38:13 +02:00
										 |  |  | 				d[i].chunk->stride = frame_size; | 
					
						
							| 
									
										
										
										
											2020-07-01 11:42:10 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 			void *bufs[b->buf->n_datas]; | 
					
						
							| 
									
										
										
										
											2020-07-01 11:42:10 +02:00
										 |  |  | 			for (i = 0; i < b->buf->n_datas; i++) { | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 				bufs[i] = d[i].data; | 
					
						
							| 
									
										
										
										
											2020-07-01 11:42:10 +02:00
										 |  |  | 				d[i].chunk->offset = 0; | 
					
						
							|  |  |  | 				d[i].chunk->size = n_bytes; | 
					
						
							| 
									
										
										
										
											2022-06-24 10:38:13 +02:00
										 |  |  | 				d[i].chunk->stride = frame_size; | 
					
						
							| 
									
										
										
										
											2020-07-01 11:42:10 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 			if (state->planar) { | 
					
						
							|  |  |  | 				snd_pcm_readn(state->hndl, bufs, total_frames); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				snd_pcm_readi(state->hndl, bufs[0], total_frames); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 		spa_log_trace_fp(state->log, "%p: wrote %ld frames into buffer %d", | 
					
						
							| 
									
										
										
										
											2021-03-24 11:48:32 +01:00
										 |  |  | 				state, total_frames, b->id); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 		spa_list_append(&state->ready, &b->link); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-01-08 16:35:26 +01:00
										 |  |  | 	return total_frames; | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:15 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | static int alsa_read_sync(struct state *state, uint64_t current_time) | 
					
						
							| 
									
										
										
										
											2017-03-28 19:29:08 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-08 12:18:23 +02:00
										 |  |  | 	int res, suppressed; | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 	snd_pcm_uframes_t avail, delay, target, max_read; | 
					
						
							|  |  |  | 	bool following = state->following; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 13:05:31 +02:00
										 |  |  | 	if (SPA_UNLIKELY(!state->alsa_started)) | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-15 14:52:25 +02:00
										 |  |  | 	if (SPA_UNLIKELY((res = check_position_config(state, false)) < 0)) | 
					
						
							| 
									
										
										
										
											2023-03-23 18:04:36 +01:00
										 |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2018-11-09 15:24:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-12 15:17:48 +02:00
										 |  |  | 	if (SPA_UNLIKELY((res = get_status(state, current_time, &avail, &delay, &target)) < 0)) { | 
					
						
							| 
									
										
										
										
											2023-11-15 10:10:56 +01:00
										 |  |  | 		spa_log_error(state->log, "get_status error: %s", spa_strerror(res)); | 
					
						
							| 
									
										
										
										
											2024-06-18 12:17:56 +02:00
										 |  |  | 		state->next_time += (uint64_t)(state->threshold * 1e9 / state->rate); | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2023-10-12 15:17:48 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-02-10 15:18:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 	if (SPA_UNLIKELY(!following && avail < state->read_size)) { | 
					
						
							|  |  |  | 		spa_log_trace(state->log, "%p: early wakeup %ld %ld %ld %d", state, | 
					
						
							|  |  |  | 				delay, avail, target, state->read_size); | 
					
						
							|  |  |  | 		state->next_time = current_time + (state->read_size - avail) * SPA_NSEC_PER_SEC / | 
					
						
							|  |  |  | 			state->rate; | 
					
						
							|  |  |  | 		return -EAGAIN; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 	if (SPA_UNLIKELY((res = update_time(state, current_time, delay, target, following)) < 0)) | 
					
						
							|  |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2022-05-27 21:20:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 	max_read = state->buffer_frames; | 
					
						
							| 
									
										
										
										
											2025-06-11 13:06:54 +02:00
										 |  |  | 	if (following) { | 
					
						
							| 
									
										
										
										
											2019-04-26 15:48:21 +02:00
										 |  |  | 		if (state->alsa_sync) { | 
					
						
							| 
									
										
										
										
											2025-06-11 13:06:54 +02:00
										 |  |  | 			if (!state->linked) { | 
					
						
							|  |  |  | 				enum spa_log_level lev; | 
					
						
							|  |  |  | 				if (SPA_UNLIKELY(state->alsa_sync_warning)) | 
					
						
							|  |  |  | 					lev = SPA_LOG_LEVEL_WARN; | 
					
						
							|  |  |  | 				else | 
					
						
							|  |  |  | 					lev = SPA_LOG_LEVEL_INFO; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if ((suppressed = spa_ratelimit_test(&state->rate_limit, current_time)) < 0) | 
					
						
							|  |  |  | 					lev = SPA_LOG_LEVEL_DEBUG; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				spa_log_lev(state->log, lev, "%s: follower delay:%ld target:%ld thr:%u " | 
					
						
							|  |  |  | 						"resample:%d, resync (%d suppressed)", state->name, delay, | 
					
						
							|  |  |  | 						target, state->threshold, state->resample, suppressed); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (avail < target) | 
					
						
							|  |  |  | 					max_read = target - avail; | 
					
						
							|  |  |  | 				else if (avail > target) { | 
					
						
							|  |  |  | 					snd_pcm_forward(state->hndl, avail - target); | 
					
						
							|  |  |  | 					avail = target; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2023-07-31 12:10:36 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-04-26 15:48:21 +02:00
										 |  |  | 			state->alsa_sync = false; | 
					
						
							| 
									
										
										
										
											2023-10-18 14:49:46 +02:00
										 |  |  | 			spa_dll_init(&state->dll); | 
					
						
							| 
									
										
										
										
											2022-09-21 16:39:31 +02:00
										 |  |  | 		} else | 
					
						
							|  |  |  | 			state->alsa_sync_warning = true; | 
					
						
							| 
									
										
										
										
											2019-04-26 15:48:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-16 21:30:54 +01:00
										 |  |  | 		if (avail < state->read_size) | 
					
						
							| 
									
										
										
										
											2022-02-15 16:29:43 +01:00
										 |  |  | 			max_read = 0; | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 	state->max_read = SPA_MIN(max_read, state->read_size); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | static int alsa_read_frames(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	snd_pcm_t *hndl = state->hndl; | 
					
						
							| 
									
										
										
										
											2023-10-18 14:59:40 +02:00
										 |  |  | 	snd_pcm_uframes_t total_read = 0, avail; | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 	const snd_pcm_channel_area_t *my_areas; | 
					
						
							|  |  |  | 	snd_pcm_uframes_t read, frames, offset; | 
					
						
							|  |  |  | 	snd_pcm_sframes_t commitres; | 
					
						
							|  |  |  | 	int res = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	frames = state->max_read; | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 	if (state->use_mmap) { | 
					
						
							| 
									
										
										
										
											2023-10-18 14:59:40 +02:00
										 |  |  | 		avail = state->buffer_frames; | 
					
						
							|  |  |  | 		if ((res = snd_pcm_mmap_begin(hndl, &my_areas, &offset, &avail)) < 0) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 			spa_log_error(state->log, "%s: snd_pcm_mmap_begin error: %s", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 					state->name, snd_strerror(res)); | 
					
						
							| 
									
										
										
										
											2023-11-15 09:55:58 +01:00
										 |  |  | 			alsa_recover(state); | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 			return res; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-10-18 14:59:40 +02:00
										 |  |  | 		spa_log_trace_fp(state->log, "%p: begin offs:%ld frames:%ld avail:%ld thres:%d", state, | 
					
						
							|  |  |  | 				offset, frames, avail, state->threshold); | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		my_areas = NULL; | 
					
						
							|  |  |  | 		offset = 0; | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 18:07:44 +01:00
										 |  |  | 	if (frames > 0) { | 
					
						
							|  |  |  | 		read = push_frames(state, my_areas, offset, frames); | 
					
						
							|  |  |  | 		total_read += read; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		spa_alsa_skip(state); | 
					
						
							|  |  |  | 		total_read += state->read_size; | 
					
						
							|  |  |  | 		read = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 18:07:44 +01:00
										 |  |  | 	if (state->use_mmap && read > 0) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 		spa_log_trace_fp(state->log, "%p: commit offs:%ld read:%ld count:%"PRIi64, state, | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 				offset, read, state->sample_count); | 
					
						
							|  |  |  | 		if ((commitres = snd_pcm_mmap_commit(hndl, offset, read)) < 0) { | 
					
						
							| 
									
										
										
										
											2023-09-22 11:23:51 +02:00
										 |  |  | 			enum spa_log_level lev; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (SPA_UNLIKELY(state->alsa_sync_warning)) | 
					
						
							|  |  |  | 				lev = SPA_LOG_LEVEL_ERROR; | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				lev = SPA_LOG_LEVEL_INFO; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			spa_log_lev(state->log, lev, "%s: snd_pcm_mmap_commit error %lu %lu %lu: %s", | 
					
						
							| 
									
										
										
										
											2023-10-18 14:59:40 +02:00
										 |  |  | 					state->name, frames, avail, read, snd_strerror(commitres)); | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 			if (commitres != -EPIPE && commitres != -ESTRPIPE) | 
					
						
							|  |  |  | 				return res; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (commitres > 0 && read != (snd_pcm_uframes_t) commitres) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 			spa_log_warn(state->log, "%s: mmap_commit read %ld instead of %ld", | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 				     state->name, commitres, read); | 
					
						
							| 
									
										
										
										
											2021-01-05 17:48:09 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-12-20 00:23:52 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	state->sample_count += total_read; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | int spa_alsa_read(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 	if (state->following && state->rt.driver == NULL) { | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 		uint64_t current_time = state->position->clock.nsec; | 
					
						
							| 
									
										
										
										
											2023-10-12 11:38:36 +02:00
										 |  |  | 		alsa_read_sync(state, current_time); | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-07-22 16:48:43 +02:00
										 |  |  | 	else if (state->resample && state->rate_match) { | 
					
						
							|  |  |  | 		state->read_size = state->rate_match->size; | 
					
						
							|  |  |  | 		state->max_read = SPA_MIN(state->buffer_frames, state->read_size); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-09-15 13:05:31 +02:00
										 |  |  | 	return alsa_read_frames(state); | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-07 11:51:22 +02:00
										 |  |  | int spa_alsa_skip(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct buffer *b; | 
					
						
							|  |  |  | 	struct spa_data *d; | 
					
						
							|  |  |  | 	uint32_t i, avail, total_frames, n_bytes, frames; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 13:05:31 +02:00
										 |  |  | 	if (SPA_UNLIKELY(spa_list_is_empty(&state->free))) { | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 		spa_log_warn(state->log, "%s: no more buffers", state->name); | 
					
						
							| 
									
										
										
										
											2021-05-07 11:51:22 +02:00
										 |  |  | 		return -EPIPE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	frames = state->read_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	b = spa_list_first(&state->free, struct buffer, link); | 
					
						
							|  |  |  | 	spa_list_remove(&b->link); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	d = b->buf->datas; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	avail = d[0].maxsize / state->frame_size; | 
					
						
							|  |  |  | 	total_frames = SPA_MIN(avail, frames); | 
					
						
							|  |  |  | 	n_bytes = total_frames * state->frame_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < b->buf->n_datas; i++) { | 
					
						
							|  |  |  | 		memset(d[i].data, 0, n_bytes); | 
					
						
							|  |  |  | 		d[i].chunk->offset = 0; | 
					
						
							|  |  |  | 		d[i].chunk->size = n_bytes; | 
					
						
							|  |  |  | 		d[i].chunk->stride = state->frame_size; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	spa_list_append(&state->ready, &b->link); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | static int playback_ready(struct state *state) | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-21 11:18:43 +02:00
										 |  |  | 	struct spa_io_buffers *io = state->io; | 
					
						
							| 
									
										
										
										
											2018-11-19 18:03:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-10 10:51:23 +02:00
										 |  |  | 	spa_log_trace_fp(state->log, "%p: %d", state, io ? io->status : 0); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 11:18:43 +02:00
										 |  |  | 	update_sources(state, false); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-10 10:51:23 +02:00
										 |  |  | 	if (io != NULL) | 
					
						
							|  |  |  | 		io->status = SPA_STATUS_NEED_DATA; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 11:18:43 +02:00
										 |  |  | 	return spa_node_call_ready(&state->callbacks, SPA_STATUS_NEED_DATA); | 
					
						
							| 
									
										
										
										
											2017-03-28 19:29:08 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | static int capture_ready(struct state *state) | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	struct spa_io_buffers *io; | 
					
						
							| 
									
										
										
										
											2023-09-15 13:05:31 +02:00
										 |  |  | 	bool have_data; | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 13:05:31 +02:00
										 |  |  | 	have_data = !spa_list_is_empty(&state->ready); | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 13:12:49 +01:00
										 |  |  | 	io = state->io; | 
					
						
							| 
									
										
										
										
											2021-03-24 11:48:32 +01:00
										 |  |  | 	if (io != NULL && | 
					
						
							|  |  |  | 	    (io->status != SPA_STATUS_HAVE_DATA || state->rate_match != NULL)) { | 
					
						
							| 
									
										
										
										
											2020-03-19 13:12:49 +01:00
										 |  |  | 		struct buffer *b; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 13:05:31 +02:00
										 |  |  | 		if (SPA_LIKELY(io->buffer_id < state->n_buffers)) | 
					
						
							| 
									
										
										
										
											2020-03-19 13:12:49 +01:00
										 |  |  | 			spa_alsa_recycle_buffer(state, io->buffer_id); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 13:05:31 +02:00
										 |  |  | 		if (SPA_LIKELY(have_data)) { | 
					
						
							|  |  |  | 			b = spa_list_first(&state->ready, struct buffer, link); | 
					
						
							|  |  |  | 			spa_list_remove(&b->link); | 
					
						
							|  |  |  | 			SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUT); | 
					
						
							| 
									
										
										
										
											2020-03-19 13:12:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 13:05:31 +02:00
										 |  |  | 			io->buffer_id = b->id; | 
					
						
							|  |  |  | 			io->status = SPA_STATUS_HAVE_DATA; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			io->buffer_id = SPA_ID_INVALID; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-09-15 15:32:34 +02:00
										 |  |  | 		spa_log_trace_fp(state->log, "%p: output buffer:%d", state, io->buffer_id); | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-09-15 13:05:31 +02:00
										 |  |  | 	if (have_data) | 
					
						
							|  |  |  | 		spa_node_call_ready(&state->callbacks, SPA_STATUS_HAVE_DATA); | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-03-30 17:30:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-12 10:06:46 +01:00
										 |  |  | static uint64_t get_time_ns(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct timespec now; | 
					
						
							|  |  |  | 	if (spa_system_clock_gettime(state->data_system, CLOCK_MONOTONIC, &now) < 0) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	return SPA_TIMESPEC_TO_NSEC(&now); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-14 16:25:20 +01:00
										 |  |  | static inline int alsa_do_wakeup_work(struct state *state, uint64_t current_time) | 
					
						
							| 
									
										
										
										
											2017-03-30 17:30:28 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-02-14 16:25:20 +01:00
										 |  |  | 	struct state *follower; | 
					
						
							|  |  |  | 	int res; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 	/* first do all the sync */ | 
					
						
							|  |  |  | 	if (state->stream == SND_PCM_STREAM_CAPTURE) | 
					
						
							|  |  |  | 		res = alsa_read_sync(state, current_time); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		res = alsa_write_sync(state, current_time); | 
					
						
							|  |  |  | 	/* we can get -EAGAIN when we need to wait some more */ | 
					
						
							| 
									
										
										
										
											2023-09-15 13:05:31 +02:00
										 |  |  | 	if (SPA_UNLIKELY(res == -EAGAIN)) | 
					
						
							| 
									
										
										
										
											2024-02-14 16:25:20 +01:00
										 |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2019-09-19 16:55:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 	spa_list_for_each(follower, &state->rt.followers, rt.driver_link) { | 
					
						
							|  |  |  | 		if (follower == state) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		if (follower->stream == SND_PCM_STREAM_CAPTURE) | 
					
						
							|  |  |  | 			alsa_read_sync(follower, current_time); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			alsa_write_sync(follower, current_time); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* then read this source, the sinks will be written to when the
 | 
					
						
							|  |  |  | 	 * graph completes. We can't read other follower sources yet because | 
					
						
							|  |  |  | 	 * the resampler first needs to run. */ | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 	if (state->stream == SND_PCM_STREAM_CAPTURE) | 
					
						
							|  |  |  | 		alsa_read_frames(state); | 
					
						
							| 
									
										
										
										
											2018-10-24 10:27:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 	/* and then trigger the graph */ | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	if (state->stream == SND_PCM_STREAM_PLAYBACK) | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 		playback_ready(state); | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2023-09-15 12:21:18 +02:00
										 |  |  | 		capture_ready(state); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-14 16:25:20 +01:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void alsa_irq_wakeup_event(struct spa_source *source) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct state *state = source->data; | 
					
						
							|  |  |  | 	uint64_t current_time; | 
					
						
							|  |  |  | 	int res, err; | 
					
						
							|  |  |  | 	unsigned short revents; | 
					
						
							| 
									
										
										
										
											2024-03-01 21:36:47 +01:00
										 |  |  | 	snd_pcm_uframes_t havail; | 
					
						
							|  |  |  | 	snd_htimestamp_t tstamp; | 
					
						
							| 
									
										
										
										
											2024-02-14 16:25:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-01 21:36:47 +01:00
										 |  |  | 	// First, take a snapshot of the wakeup time
 | 
					
						
							| 
									
										
										
										
											2024-02-14 16:25:20 +01:00
										 |  |  | 	current_time = get_time_ns(state); | 
					
						
							| 
									
										
										
										
											2024-03-01 21:36:47 +01:00
										 |  |  | 	// If the hi-res timestamps are working, we will get a timestamp that
 | 
					
						
							|  |  |  | 	// is earlier then current_time
 | 
					
						
							|  |  |  | 	if ((res = snd_pcm_htimestamp(state->hndl, &havail, &tstamp)) == 0) { | 
					
						
							|  |  |  | 		uint64_t htime = SPA_TIMESPEC_TO_NSEC(&tstamp); | 
					
						
							|  |  |  | 		if (htime < current_time) { | 
					
						
							|  |  |  | 			current_time = htime; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-02-14 16:25:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < state->n_fds; i++) { | 
					
						
							|  |  |  | 		state->pfds[i].revents = state->source[i].rmask; | 
					
						
							|  |  |  | 		/* Reset so that we only handle all our sources' events once */ | 
					
						
							|  |  |  | 		state->source[i].rmask = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ALSA poll fds need to be "demangled" to know whether it's a real wakeup */ | 
					
						
							|  |  |  | 	if (SPA_UNLIKELY(err = snd_pcm_poll_descriptors_revents(state->hndl, | 
					
						
							|  |  |  | 					state->pfds, state->n_fds, &revents))) { | 
					
						
							|  |  |  | 		spa_log_error(state->log, "Could not look up revents: %s", | 
					
						
							|  |  |  | 				snd_strerror(err)); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!revents) { | 
					
						
							|  |  |  | 		spa_log_trace_fp(state->log, "Woken up with no work to do"); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (revents & POLLERR) { | 
					
						
							|  |  |  | 		spa_log_trace_fp(state->log, "poll error"); | 
					
						
							|  |  |  | 		if ((res = alsa_recover(state)) < 0) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	alsa_do_wakeup_work(state, current_time); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void alsa_timer_wakeup_event(struct spa_source *source) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct state *state = source->data; | 
					
						
							|  |  |  | 	uint64_t expire, current_time; | 
					
						
							|  |  |  | 	int res, suppressed; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (SPA_LIKELY(state->started)) { | 
					
						
							|  |  |  | 		if (SPA_UNLIKELY((res = spa_system_timerfd_read(state->data_system, | 
					
						
							|  |  |  | 					state->timerfd, &expire)) < 0)) { | 
					
						
							|  |  |  | 		/* we can get here when the timer is changed since the last
 | 
					
						
							|  |  |  | 			 * timerfd wakeup, for example by do_reassign_follower() executed | 
					
						
							|  |  |  | 			 * in the same epoll wakeup cycle */ | 
					
						
							|  |  |  | 			if (res != -EAGAIN) | 
					
						
							|  |  |  | 				spa_log_warn(state->log, "%p: error reading timerfd: %s", | 
					
						
							|  |  |  | 						state, spa_strerror(res)); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	current_time = state->next_time; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	alsa_do_wakeup_work(state, current_time); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state->next_time > current_time + SPA_NSEC_PER_SEC || | 
					
						
							|  |  |  | 	    current_time > state->next_time + SPA_NSEC_PER_SEC) { | 
					
						
							|  |  |  | 		if ((suppressed = spa_ratelimit_test(&state->rate_limit, current_time)) >= 0) { | 
					
						
							|  |  |  | 			spa_log_error(state->log, "%s: impossible timeout %" | 
					
						
							|  |  |  | 				PRIu64" %"PRIu64" %"PRIi64" %d %"PRIi64" (%d suppressed)", | 
					
						
							|  |  |  | 				state->name, current_time, state->next_time, | 
					
						
							|  |  |  | 				state->next_time - current_time, state->threshold, | 
					
						
							|  |  |  | 				state->sample_count, suppressed); | 
					
						
							| 
									
										
										
										
											2023-06-19 16:58:29 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-06-18 12:17:56 +02:00
										 |  |  | 		state->next_time = (uint64_t)(current_time + state->threshold * 1e9 / state->rate); | 
					
						
							| 
									
										
										
										
											2022-02-15 15:35:40 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-02-14 16:25:20 +01:00
										 |  |  | 	set_timeout(state, state->next_time); | 
					
						
							| 
									
										
										
										
											2017-03-30 17:30:28 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-18 14:46:56 +02:00
										 |  |  | static void remove_sources(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 	if (state->rt.sources_added) { | 
					
						
							| 
									
										
										
										
											2023-09-18 14:46:56 +02:00
										 |  |  | 		for (i = 0; i < state->n_fds; i++) | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 			spa_loop_remove_source(state->data_loop, &state->source[i]); | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 		state->rt.sources_added = false; | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-18 14:46:56 +02:00
										 |  |  | static void add_sources(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 	if (!state->rt.sources_added) { | 
					
						
							| 
									
										
										
										
											2023-09-18 14:46:56 +02:00
										 |  |  | 		for (i = 0; i < state->n_fds; i++) | 
					
						
							|  |  |  | 			spa_loop_add_source(state->data_loop, &state->source[i]); | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 		state->rt.sources_added = true; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-09-18 14:46:56 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int do_state_sync(struct spa_loop *loop, bool async, uint32_t seq, | 
					
						
							|  |  |  | 		const void *data, size_t size, void *user_data) | 
					
						
							| 
									
										
										
										
											2019-04-24 15:38:06 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-15 16:31:55 +02:00
										 |  |  | 	struct state *state = user_data; | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 	struct rt_state *rt = &state->rt; | 
					
						
							| 
									
										
										
										
											2023-09-15 16:31:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-18 14:46:56 +02:00
										 |  |  | 	if (state->started) { | 
					
						
							|  |  |  | 		state->next_time = get_time_ns(state); | 
					
						
							| 
									
										
										
										
											2019-09-19 16:55:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 		if (rt->driver != state->driver) { | 
					
						
							|  |  |  | 			spa_dll_init(&state->dll); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (rt->driver != NULL) | 
					
						
							|  |  |  | 				spa_list_remove(&rt->driver_link); | 
					
						
							|  |  |  | 			if (state->driver != NULL) | 
					
						
							|  |  |  | 				spa_list_append(&state->driver->rt.followers, &rt->driver_link); | 
					
						
							|  |  |  | 			rt->driver = state->driver; | 
					
						
							|  |  |  | 			spa_log_debug(state->log, "state:%p -> driver:%p", state, state->driver); | 
					
						
							| 
									
										
										
										
											2024-09-04 07:29:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-18 18:19:16 +02:00
										 |  |  | 			if (state->linked && state->matching) | 
					
						
							| 
									
										
										
										
											2024-09-04 07:29:30 +00:00
										 |  |  | 				try_unlink(state); | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-09-15 16:31:55 +02:00
										 |  |  | 		if (state->following) { | 
					
						
							| 
									
										
										
										
											2023-09-18 14:46:56 +02:00
										 |  |  | 			remove_sources(state); | 
					
						
							|  |  |  | 			set_timeout(state, 0); | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2023-09-18 14:46:56 +02:00
										 |  |  | 			add_sources(state); | 
					
						
							|  |  |  | 			if (!state->disable_tsched) | 
					
						
							|  |  |  | 				set_timeout(state, state->next_time); | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-09-15 16:31:55 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 		if (rt->driver) { | 
					
						
							|  |  |  | 			spa_list_remove(&rt->driver_link); | 
					
						
							|  |  |  | 			rt->driver = NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-09-18 14:46:56 +02:00
										 |  |  | 		if (!state->disable_tsched) | 
					
						
							|  |  |  | 			set_timeout(state, 0); | 
					
						
							|  |  |  | 		remove_sources(state); | 
					
						
							| 
									
										
										
										
											2023-09-15 16:31:55 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-03-29 12:21:23 +02:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 11:15:26 +02:00
										 |  |  | int spa_alsa_prepare(struct state *state) | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | 	struct state *follower; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	int err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 11:39:45 +02:00
										 |  |  | 	if (!state->opened) | 
					
						
							|  |  |  | 		return -EIO; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 11:15:26 +02:00
										 |  |  | 	spa_alsa_pause(state); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 11:15:26 +02:00
										 |  |  | 	if (state->prepared) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2023-03-23 18:04:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-15 14:52:25 +02:00
										 |  |  | 	if (check_position_config(state, true) < 0) { | 
					
						
							| 
									
										
										
										
											2023-09-15 09:09:04 +02:00
										 |  |  | 		spa_log_error(state->log, "%s: invalid position config", state->name); | 
					
						
							| 
									
										
										
										
											2023-01-16 16:05:17 +01:00
										 |  |  | 		return -EIO; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-09-20 15:38:41 +02:00
										 |  |  | 	if ((err = do_prepare(state)) < 0) | 
					
						
							| 
									
										
										
										
											2021-01-15 18:42:35 +01:00
										 |  |  | 		return err; | 
					
						
							| 
									
										
										
										
											2023-09-15 11:15:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | 	spa_list_for_each(follower, &state->followers, driver_link) { | 
					
						
							|  |  |  | 		if (follower != state && !follower->matching) { | 
					
						
							| 
									
										
										
										
											2023-10-16 10:32:51 +02:00
										 |  |  | 			if (spa_alsa_prepare(follower) < 0) | 
					
						
							|  |  |  | 				continue; | 
					
						
							| 
									
										
										
										
											2023-10-09 10:10:46 +02:00
										 |  |  | 			if (!follower->linked && state->auto_link) | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | 				do_link(state, follower); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 11:15:26 +02:00
										 |  |  | 	state->prepared = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int spa_alsa_start(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | 	struct state *follower; | 
					
						
							| 
									
										
										
										
											2023-09-15 11:15:26 +02:00
										 |  |  | 	int err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state->started) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2023-10-11 11:39:45 +02:00
										 |  |  | 	else if (!state->opened) | 
					
						
							|  |  |  | 		return -EIO; | 
					
						
							| 
									
										
										
										
											2023-09-15 11:15:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	spa_alsa_prepare(state); | 
					
						
							| 
									
										
										
										
											2021-01-15 18:42:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 	if (!state->disable_tsched) { | 
					
						
							|  |  |  | 		/* Timer-based scheduling */ | 
					
						
							| 
									
										
										
										
											2024-02-14 16:25:20 +01:00
										 |  |  | 		state->source[0].func = alsa_timer_wakeup_event; | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 		state->source[0].data = state; | 
					
						
							|  |  |  | 		state->source[0].fd = state->timerfd; | 
					
						
							|  |  |  | 		state->source[0].mask = SPA_IO_IN; | 
					
						
							|  |  |  | 		state->source[0].rmask = 0; | 
					
						
							| 
									
										
										
										
											2023-09-18 14:46:56 +02:00
										 |  |  | 		state->n_fds = 1; | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		/* ALSA period-based scheduling */ | 
					
						
							|  |  |  | 		err = snd_pcm_poll_descriptors_count(state->hndl); | 
					
						
							|  |  |  | 		if (err < 0) { | 
					
						
							|  |  |  | 			spa_log_error(state->log, "Could not get poll descriptor count: %s", | 
					
						
							|  |  |  | 					snd_strerror(err)); | 
					
						
							|  |  |  | 			return err; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (err > MAX_POLL) { | 
					
						
							|  |  |  | 			spa_log_error(state->log, "Unsupported poll descriptor count: %d", err); | 
					
						
							|  |  |  | 			return -EIO; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		state->n_fds = err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if ((err = snd_pcm_poll_descriptors(state->hndl, state->pfds, state->n_fds)) < 0) { | 
					
						
							|  |  |  | 			spa_log_error(state->log, "Could not get poll descriptors: %s", | 
					
						
							|  |  |  | 					snd_strerror(err)); | 
					
						
							|  |  |  | 			return err; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* We only add the source to the data loop if we're driving.
 | 
					
						
							| 
									
										
										
										
											2024-07-17 20:07:57 -04:00
										 |  |  | 		 * This is done in add_sources() */ | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 		for (int i = 0; i < state->n_fds; i++) { | 
					
						
							| 
									
										
										
										
											2024-02-14 16:25:20 +01:00
										 |  |  | 			state->source[i].func = alsa_irq_wakeup_event; | 
					
						
							| 
									
										
										
										
											2023-09-18 14:46:56 +02:00
										 |  |  | 			state->source[i].data = state; | 
					
						
							|  |  |  | 			state->source[i].fd = state->pfds[i].fd; | 
					
						
							|  |  |  | 			state->source[i].mask = state->pfds[i].events; | 
					
						
							| 
									
										
										
										
											2023-02-28 13:55:04 -05:00
										 |  |  | 			state->source[i].rmask = 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | 	spa_list_for_each(follower, &state->followers, driver_link) | 
					
						
							|  |  |  | 		if (follower != state) | 
					
						
							|  |  |  | 			spa_alsa_start(follower); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 16:31:55 +02:00
										 |  |  | 	/* start capture now. We should have some data when the timer or IRQ
 | 
					
						
							|  |  |  | 	 * goes off later */ | 
					
						
							|  |  |  | 	if (state->stream == SND_PCM_STREAM_CAPTURE) { | 
					
						
							|  |  |  | 		if ((err = do_start(state)) < 0) | 
					
						
							|  |  |  | 			return err; | 
					
						
							| 
									
										
										
										
											2023-03-29 15:13:00 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-01-18 11:57:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 16:31:55 +02:00
										 |  |  | 	/* playback will start after first write. Without tsched, we start
 | 
					
						
							|  |  |  | 	 * right away so that the fds become active in poll right away. */ | 
					
						
							|  |  |  | 	if (state->stream == SND_PCM_STREAM_PLAYBACK) { | 
					
						
							| 
									
										
										
										
											2024-03-28 16:22:19 +01:00
										 |  |  | 		if (state->disable_tsched || state->start_delay > 0) | 
					
						
							| 
									
										
										
										
											2023-09-15 16:31:55 +02:00
										 |  |  | 			if ((err = do_start(state)) < 0) | 
					
						
							|  |  |  | 				return err; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-03-28 16:22:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	state->started = true; | 
					
						
							| 
									
										
										
										
											2025-03-10 17:24:13 +01:00
										 |  |  | 	spa_loop_locked(state->data_loop, do_state_sync, 0, NULL, 0, state); | 
					
						
							| 
									
										
										
										
											2024-03-28 16:22:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-13 09:41:41 +01:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | static struct state *find_state(uint32_t id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct state *state; | 
					
						
							|  |  |  | 	spa_list_for_each(state, &states, link) { | 
					
						
							|  |  |  | 		if (state->clock != NULL && state->clock->id == id) | 
					
						
							|  |  |  | 			return state; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-21 10:47:32 +01:00
										 |  |  | int spa_alsa_reassign_follower(struct state *state) | 
					
						
							| 
									
										
										
										
											2019-04-24 15:38:06 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-07 11:51:22 +02:00
										 |  |  | 	bool following, freewheel; | 
					
						
							| 
									
										
										
										
											2023-09-15 12:40:30 +02:00
										 |  |  | 	struct spa_io_position *pos = state->position; | 
					
						
							|  |  |  | 	struct spa_io_clock *clock = state->clock; | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 	struct state *driver; | 
					
						
							| 
									
										
										
										
											2019-04-24 15:38:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:40:30 +02:00
										 |  |  | 	if (clock != NULL) | 
					
						
							|  |  |  | 		spa_scnprintf(clock->name, sizeof(clock->name), "%s", state->clock_name); | 
					
						
							| 
									
										
										
										
											2019-04-24 15:38:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:40:30 +02:00
										 |  |  | 	following = pos && clock && pos->clock.id != clock->id; | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	driver = pos != NULL ? find_state(pos->clock.id) : NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (driver != state->driver) { | 
					
						
							|  |  |  | 		spa_log_debug(state->log, "%p: reassign driver %p->%p", state, state->driver, driver); | 
					
						
							|  |  |  | 		if (state->driver != NULL) | 
					
						
							|  |  |  | 			spa_list_remove(&state->driver_link); | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | 		if (driver != NULL) { | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 			spa_list_append(&driver->followers, &state->driver_link); | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-09-18 14:53:15 +02:00
										 |  |  | 		state->driver = driver; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-02-21 10:47:32 +01:00
										 |  |  | 	if (following != state->following) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 		spa_log_debug(state->log, "%p: reassign follower %d->%d", state, state->following, following); | 
					
						
							| 
									
										
										
										
											2020-02-21 10:47:32 +01:00
										 |  |  | 		state->following = following; | 
					
						
							| 
									
										
										
										
											2019-04-24 15:38:06 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-02-01 10:42:57 +01:00
										 |  |  | 	setup_matching(state); | 
					
						
							| 
									
										
										
										
											2023-09-18 14:46:56 +02:00
										 |  |  | 	if (state->started) | 
					
						
							| 
									
										
										
										
											2025-03-10 17:24:13 +01:00
										 |  |  | 		spa_loop_locked(state->data_loop, do_state_sync, 0, NULL, 0, state); | 
					
						
							| 
									
										
										
										
											2024-11-15 16:09:36 +01:00
										 |  |  | 	else if (state->want_started) | 
					
						
							|  |  |  | 		spa_alsa_start(state); | 
					
						
							| 
									
										
										
										
											2021-05-07 11:51:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:40:30 +02:00
										 |  |  | 	freewheel = pos != NULL && SPA_FLAG_IS_SET(pos->clock.flags, SPA_IO_CLOCK_FLAG_FREEWHEEL); | 
					
						
							| 
									
										
										
										
											2021-05-07 11:51:22 +02:00
										 |  |  | 	if (state->freewheel != freewheel) { | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 		spa_log_debug(state->log, "%p: freewheel %d->%d", state, state->freewheel, freewheel); | 
					
						
							| 
									
										
										
										
											2021-05-07 11:51:22 +02:00
										 |  |  | 		state->freewheel = freewheel; | 
					
						
							| 
									
										
										
										
											2023-09-14 17:15:06 +02:00
										 |  |  | 		if (state->started) { | 
					
						
							|  |  |  | 			if (freewheel) | 
					
						
							|  |  |  | 				snd_pcm_pause(state->hndl, 1); | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				snd_pcm_pause(state->hndl, 0); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-07 11:51:22 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-12-13 14:47:04 +01:00
										 |  |  | 	state->alsa_sync_warning = false; | 
					
						
							| 
									
										
										
										
											2019-04-24 15:38:06 +02:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | int spa_alsa_pause(struct state *state) | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | 	struct state *follower; | 
					
						
							| 
									
										
										
										
											2016-10-28 16:56:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	if (!state->started) | 
					
						
							| 
									
										
										
										
											2017-11-13 09:41:41 +01:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2016-10-25 11:15:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 08:05:40 +02:00
										 |  |  | 	spa_log_debug(state->log, "%p: pause", state); | 
					
						
							| 
									
										
										
										
											2017-03-30 17:30:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-18 14:46:56 +02:00
										 |  |  | 	state->started = false; | 
					
						
							| 
									
										
										
										
											2025-03-10 17:24:13 +01:00
										 |  |  | 	spa_loop_locked(state->data_loop, do_state_sync, 0, NULL, 0, state); | 
					
						
							| 
									
										
										
										
											2016-10-28 16:56:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 11:04:49 +02:00
										 |  |  | 	spa_list_for_each(follower, &state->followers, driver_link) | 
					
						
							|  |  |  | 		spa_alsa_pause(follower); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 15:38:41 +02:00
										 |  |  | 	do_drop(state); | 
					
						
							| 
									
										
										
										
											2016-08-24 16:26:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 11:15:26 +02:00
										 |  |  | 	state->prepared = false; | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-13 09:41:41 +01:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-11-07 13:01:52 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | void spa_alsa_emit_node_info(struct state *state, bool full) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	uint64_t old = full ? state->info.change_mask : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (full) | 
					
						
							|  |  |  | 		state->info.change_mask = state->info_all; | 
					
						
							|  |  |  | 	if (state->info.change_mask) { | 
					
						
							|  |  |  | 		struct spa_dict_item items[7]; | 
					
						
							|  |  |  | 		uint32_t i, n_items = 0; | 
					
						
							| 
									
										
										
										
											2023-11-27 16:20:21 +01:00
										 |  |  | 		char latency[64] = "", period[64] = "", nperiods[64] = "", headroom[64] = ""; | 
					
						
							| 
									
										
										
										
											2023-11-07 13:01:52 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_API, "alsa"); | 
					
						
							| 
									
										
										
										
											2024-12-07 13:27:09 +02:00
										 |  |  | 		items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_MEDIA_CLASS, state->props.media_class); | 
					
						
							| 
									
										
										
										
											2023-11-07 13:01:52 -05:00
										 |  |  | 		items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_NODE_DRIVER, "true"); | 
					
						
							| 
									
										
										
										
											2023-11-27 16:20:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-02 18:52:38 +02:00
										 |  |  | 		if (state->have_format && !state->disable_tsched) | 
					
						
							| 
									
										
										
										
											2024-01-22 10:16:23 +01:00
										 |  |  | 			snprintf(latency, sizeof(latency), "%lu/%d", | 
					
						
							| 
									
										
										
										
											2024-01-22 12:10:51 +01:00
										 |  |  | 					state->buffer_frames / (2 * state->frame_scale), state->rate); | 
					
						
							| 
									
										
										
										
											2023-11-27 16:20:21 +01:00
										 |  |  | 		items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_NODE_MAX_LATENCY, latency[0] ? latency : NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (state->have_format) | 
					
						
							| 
									
										
										
										
											2023-11-07 13:01:52 -05:00
										 |  |  | 			snprintf(period, sizeof(period), "%lu", state->period_frames); | 
					
						
							| 
									
										
										
										
											2023-11-27 16:20:21 +01:00
										 |  |  | 		else if (state->default_period_size) | 
					
						
							|  |  |  | 			snprintf(period, sizeof(period), "%u", state->default_period_size); | 
					
						
							|  |  |  | 		items[n_items++] = SPA_DICT_ITEM_INIT("api.alsa.period-size", period[0] ? period : NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (state->have_format) | 
					
						
							| 
									
										
										
										
											2023-11-07 13:01:52 -05:00
										 |  |  | 			snprintf(nperiods, sizeof(nperiods), "%lu", | 
					
						
							|  |  |  | 					state->period_frames != 0 ? state->buffer_frames / state->period_frames : 0); | 
					
						
							| 
									
										
										
										
											2023-11-27 16:20:21 +01:00
										 |  |  | 		else if (state->default_period_num) | 
					
						
							| 
									
										
										
										
											2025-08-09 12:53:41 +03:00
										 |  |  | 			snprintf(nperiods, sizeof(nperiods), "%u", state->default_period_num); | 
					
						
							| 
									
										
										
										
											2023-11-27 16:20:21 +01:00
										 |  |  | 		items[n_items++] = SPA_DICT_ITEM_INIT("api.alsa.period-num", nperiods[0] ? nperiods : NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (state->have_format) | 
					
						
							| 
									
										
										
										
											2023-11-07 13:01:52 -05:00
										 |  |  | 			snprintf(headroom, sizeof(headroom), "%u", state->headroom); | 
					
						
							| 
									
										
										
										
											2023-11-27 16:20:21 +01:00
										 |  |  | 		else if (state->default_headroom) | 
					
						
							|  |  |  | 			snprintf(headroom, sizeof(headroom), "%u", state->default_headroom); | 
					
						
							|  |  |  | 		items[n_items++] = SPA_DICT_ITEM_INIT("api.alsa.headroom", headroom[0] ? headroom : NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-07 13:01:52 -05:00
										 |  |  | 		state->info.props = &SPA_DICT_INIT(items, n_items); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (state->info.change_mask & SPA_NODE_CHANGE_MASK_PARAMS) { | 
					
						
							|  |  |  | 			for (i = 0; i < state->info.n_params; i++) { | 
					
						
							|  |  |  | 				if (state->params[i].user > 0) { | 
					
						
							|  |  |  | 					state->params[i].flags ^= SPA_PARAM_INFO_SERIAL; | 
					
						
							|  |  |  | 					state->params[i].user = 0; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		spa_node_emit_info(&state->hooks, &state->info); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		state->info.change_mask = old; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void spa_alsa_emit_port_info(struct state *state, bool full) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	uint64_t old = full ? state->port_info.change_mask : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (full) | 
					
						
							|  |  |  | 		state->port_info.change_mask = state->port_info_all; | 
					
						
							|  |  |  | 	if (state->port_info.change_mask) { | 
					
						
							|  |  |  | 		uint32_t i; | 
					
						
							| 
									
										
										
										
											2024-06-24 12:34:38 +02:00
										 |  |  | 		static const struct spa_dict_item info_items[] = { | 
					
						
							|  |  |  | 			{ SPA_KEY_PORT_GROUP, "stream.0" }, | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		state->port_info.props = &SPA_DICT_INIT_ARRAY(info_items); | 
					
						
							| 
									
										
										
										
											2023-11-07 13:01:52 -05:00
										 |  |  | 		if (state->port_info.change_mask & SPA_PORT_CHANGE_MASK_PARAMS) { | 
					
						
							|  |  |  | 			for (i = 0; i < state->port_info.n_params; i++) { | 
					
						
							|  |  |  | 				if (state->port_params[i].user > 0) { | 
					
						
							|  |  |  | 					state->port_params[i].flags ^= SPA_PARAM_INFO_SERIAL; | 
					
						
							|  |  |  | 					state->port_params[i].user = 0; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		spa_node_emit_port_info(&state->hooks, | 
					
						
							|  |  |  | 				state->stream == SND_PCM_STREAM_PLAYBACK ? SPA_DIRECTION_INPUT : SPA_DIRECTION_OUTPUT, | 
					
						
							|  |  |  | 				0, &state->port_info); | 
					
						
							|  |  |  | 		state->port_info.change_mask = old; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |