| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											2019-06-06 15:21:40 +02:00
										 |  |  | #include <spa/support/system.h>
 | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | #include <spa/control/control.h>
 | 
					
						
							| 
									
										
										
										
											2017-06-20 12:30:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 09:16:58 +02:00
										 |  |  | #include "alsa-utils.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-13 09:41:41 +01:00
										 |  |  | #define CHECK(s,msg) if ((err = (s)) < 0) { spa_log_error(state->log, msg ": %s", snd_strerror(err)); return err; }
 | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | static int spa_alsa_open(struct state *state) | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	int err; | 
					
						
							|  |  |  | 	struct props *props = &state->props; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	CHECK(snd_output_stdio_attach(&state->output, stderr, 0), "attach failed"); | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-21 16:14:36 +02:00
										 |  |  | 	spa_log_info(state->log, "%p: ALSA device open '%s' %s", state, props->device, | 
					
						
							|  |  |  | 			state->stream == SND_PCM_STREAM_CAPTURE ? "capture" : "playback"); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	CHECK(snd_pcm_open(&state->hndl, | 
					
						
							|  |  |  | 			   props->device, | 
					
						
							|  |  |  | 			   state->stream, | 
					
						
							|  |  |  | 			   SND_PCM_NONBLOCK | | 
					
						
							|  |  |  | 			   SND_PCM_NO_AUTO_RESAMPLE | | 
					
						
							|  |  |  | 			   SND_PCM_NO_AUTO_CHANNELS | SND_PCM_NO_AUTO_FORMAT), "open failed"); | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-20 17:31:29 +02:00
										 |  |  | 	if ((err = spa_system_timerfd_create(state->data_system, | 
					
						
							|  |  |  | 			CLOCK_MONOTONIC, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK)) < 0) | 
					
						
							|  |  |  | 		goto error_exit_close; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	state->timerfd = err; | 
					
						
							| 
									
										
										
										
											2019-06-18 10:47:12 -04: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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: | 
					
						
							| 
									
										
										
										
											2019-06-18 10:47:12 -04:00
										 |  |  | 	snd_pcm_close(state->hndl); | 
					
						
							|  |  |  | 	return err; | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-13 15:02:04 +01:00
										 |  |  | 	spa_log_info(state->log, "%p: Device '%s' closing", state, state->props.device); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	CHECK(snd_pcm_close(state->hndl), "close failed"); | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 15:21:40 +02:00
										 |  |  | 	spa_system_close(state->data_system, state->timerfd); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	state->opened = false; | 
					
						
							| 
									
										
										
										
											2016-08-18 12:43:25 +02: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}, | 
					
						
							| 
									
										
										
										
											2018-09-13 17:03:56 +02: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_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}, | 
					
						
							|  |  |  | 	{ 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
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 17:47:57 +02:00
										 |  |  | static snd_pcm_format_t spa_format_to_alsa(uint32_t format) | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-07 15:52:42 +01:00
										 |  |  | 	size_t i; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < SPA_N_ELEMENTS(format_info); i++) { | 
					
						
							| 
									
										
										
										
											2018-08-23 17:47:57 +02:00
										 |  |  | 		if (format_info[i].spa_format == format) | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 			return format_info[i].format; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	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) }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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++) { | 
					
						
							|  |  |  | 		if (map->pos[i] < 0 || map->pos[i] > SND_CHMAP_LAST) | 
					
						
							|  |  |  | 			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; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 13:28:15 +02:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2019-02-25 12:29:57 +01:00
										 |  |  | spa_alsa_enum_format(struct state *state, int seq, uint32_t start, uint32_t num, | 
					
						
							|  |  |  | 		     const struct spa_pod *filter) | 
					
						
							| 
									
										
										
										
											2017-04-04 19:44:00 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	snd_pcm_t *hndl; | 
					
						
							|  |  |  | 	snd_pcm_hw_params_t *params; | 
					
						
							|  |  |  | 	snd_pcm_format_mask_t *fmask; | 
					
						
							| 
									
										
										
										
											2018-04-19 20:10:07 +02:00
										 |  |  | 	snd_pcm_access_mask_t *amask; | 
					
						
							| 
									
										
										
										
											2018-09-11 18:09:45 +02:00
										 |  |  | 	snd_pcm_chmap_query_t **maps; | 
					
						
							| 
									
										
										
										
											2019-01-07 15:52:42 +01:00
										 |  |  | 	size_t i, j; | 
					
						
							|  |  |  | 	int err, dir; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	unsigned int min, max; | 
					
						
							|  |  |  | 	uint8_t buffer[4096]; | 
					
						
							| 
									
										
										
										
											2017-11-14 10:17:45 +01:00
										 |  |  | 	struct spa_pod_builder b = { 0 }; | 
					
						
							| 
									
										
										
										
											2018-09-05 16:41:07 +02:00
										 |  |  | 	struct spa_pod_choice *choice; | 
					
						
							| 
									
										
										
										
											2017-11-13 17:57:38 +01:00
										 |  |  | 	struct spa_pod *fmt; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	int res; | 
					
						
							|  |  |  | 	bool opened; | 
					
						
							| 
									
										
										
										
											2019-01-22 17:38:23 +01:00
										 |  |  | 	struct spa_pod_frame f[2]; | 
					
						
							| 
									
										
										
										
											2019-02-25 12:29:57 +01:00
										 |  |  | 	struct spa_result_node_params result; | 
					
						
							| 
									
										
										
										
											2019-02-20 17:51:05 +01:00
										 |  |  | 	uint32_t count = 0; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	opened = state->opened; | 
					
						
							|  |  |  | 	if ((err = spa_alsa_open(state)) < 0) | 
					
						
							| 
									
										
										
										
											2017-10-26 15:13:33 +02:00
										 |  |  | 		return err; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-25 20:19:33 +01:00
										 |  |  | 	result.id = SPA_PARAM_EnumFormat; | 
					
						
							| 
									
										
										
										
											2019-02-20 17:51:05 +01:00
										 |  |  | 	result.next = start; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 15:48:31 +01:00
										 |  |  |       next: | 
					
						
							| 
									
										
										
										
											2019-02-25 12:29:57 +01:00
										 |  |  | 	result.index = result.next++; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 10:17:45 +01:00
										 |  |  | 	spa_pod_builder_init(&b, buffer, sizeof(buffer)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 17:38:23 +01:00
										 |  |  | 	spa_pod_builder_push_object(&b, &f[0], SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat); | 
					
						
							| 
									
										
										
										
											2019-01-16 11:05:12 +01:00
										 |  |  | 	spa_pod_builder_add(&b, | 
					
						
							|  |  |  | 			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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-05 16:41:07 +02:00
										 |  |  | 	spa_pod_builder_prop(&b, SPA_FORMAT_AUDIO_format, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 17:38:23 +01: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
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 1, j = 0; i < SPA_N_ELEMENTS(format_info); i++) { | 
					
						
							|  |  |  | 		const struct format_info *fi = &format_info[i]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (snd_pcm_format_mask_test(fmask, fi->format)) { | 
					
						
							| 
									
										
										
										
											2018-09-13 17:03:56 +02:00
										 |  |  | 			if (snd_pcm_access_mask_test(amask, SND_PCM_ACCESS_MMAP_INTERLEAVED)) { | 
					
						
							|  |  |  | 				if (j++ == 0) | 
					
						
							|  |  |  | 					spa_pod_builder_id(&b, fi->spa_format); | 
					
						
							| 
									
										
										
										
											2018-09-05 16:41:07 +02:00
										 |  |  | 				spa_pod_builder_id(&b, fi->spa_format); | 
					
						
							| 
									
										
										
										
											2018-09-13 17:03:56 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			if (snd_pcm_access_mask_test(amask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED) && | 
					
						
							|  |  |  | 					fi->spa_pformat != SPA_AUDIO_FORMAT_UNKNOWN) { | 
					
						
							|  |  |  | 				if (j++ == 0) | 
					
						
							|  |  |  | 					spa_pod_builder_id(&b, fi->spa_pformat); | 
					
						
							|  |  |  | 				spa_pod_builder_id(&b, fi->spa_pformat); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (j > 1) | 
					
						
							| 
									
										
										
										
											2018-09-05 16:41:07 +02:00
										 |  |  | 		choice->body.type = SPA_CHOICE_Enum; | 
					
						
							| 
									
										
										
										
											2019-01-22 17:38:23 +01:00
										 |  |  | 	spa_pod_builder_pop(&b, &f[1]); | 
					
						
							| 
									
										
										
										
											2018-04-19 20:10:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-05 16:41:07 +02:00
										 |  |  | 	spa_pod_builder_prop(&b, SPA_FORMAT_AUDIO_rate, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 17:38:23 +01: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-15 11:26:03 +02:00
										 |  |  | 	spa_pod_builder_int(&b, SPA_CLAMP(DEFAULT_RATE, min, max)); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	if (min != max) { | 
					
						
							|  |  |  | 		spa_pod_builder_int(&b, min); | 
					
						
							|  |  |  | 		spa_pod_builder_int(&b, max); | 
					
						
							| 
									
										
										
										
											2018-09-05 16:41:07 +02:00
										 |  |  | 		choice->body.type = SPA_CHOICE_Range; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-01-22 17:38:23 +01:00
										 |  |  | 	spa_pod_builder_pop(&b, &f[1]); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-05 16:41:07 +02:00
										 |  |  | 	spa_pod_builder_prop(&b, SPA_FORMAT_AUDIO_channels, 0); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:09:45 +02:00
										 |  |  | 	if ((maps = snd_pcm_query_chmaps(hndl)) != NULL) { | 
					
						
							| 
									
										
										
										
											2018-09-13 17:03:56 +02:00
										 |  |  | 		uint32_t channel; | 
					
						
							| 
									
										
										
										
											2018-10-16 18:10:43 +02:00
										 |  |  | 		snd_pcm_chmap_t* map; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-25 12:29:57 +01:00
										 |  |  | 		if (maps[result.index] == NULL) { | 
					
						
							| 
									
										
										
										
											2019-01-29 12:10:15 +01:00
										 |  |  | 			snd_pcm_free_chmaps(maps); | 
					
						
							| 
									
										
										
										
											2019-02-20 17:51:05 +01:00
										 |  |  | 			goto enum_end; | 
					
						
							| 
									
										
										
										
											2018-10-16 18:10:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-02-25 12:29:57 +01:00
										 |  |  | 		map = &maps[result.index]->map; | 
					
						
							| 
									
										
										
										
											2018-10-16 18:10:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		spa_log_debug(state->log, "map %d channels", map->channels); | 
					
						
							| 
									
										
										
										
											2018-10-17 12:44:05 +02:00
										 |  |  | 		sanitize_map(map); | 
					
						
							| 
									
										
										
										
											2018-10-16 18:10:43 +02:00
										 |  |  | 		spa_pod_builder_int(&b, map->channels); | 
					
						
							| 
									
										
										
										
											2018-09-11 18:09:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 17:03:56 +02:00
										 |  |  | 		spa_pod_builder_prop(&b, SPA_FORMAT_AUDIO_position, 0); | 
					
						
							| 
									
										
										
										
											2019-01-22 17:38:23 +01:00
										 |  |  | 		spa_pod_builder_push_array(&b, &f[1]); | 
					
						
							| 
									
										
										
										
											2018-10-16 18:10:43 +02:00
										 |  |  | 		for (j = 0; j < map->channels; j++) { | 
					
						
							| 
									
										
										
										
											2019-01-07 15:52:42 +01:00
										 |  |  | 			spa_log_debug(state->log, "position %zd %d", j, map->pos[j]); | 
					
						
							| 
									
										
										
										
											2018-10-16 18:10:43 +02:00
										 |  |  | 			channel = chmap_position_to_channel(map->pos[j]); | 
					
						
							|  |  |  | 			spa_pod_builder_id(&b, channel); | 
					
						
							| 
									
										
										
										
											2018-09-11 18:09:45 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-01-22 17:38:23 +01:00
										 |  |  | 		spa_pod_builder_pop(&b, &f[1]); | 
					
						
							| 
									
										
										
										
											2018-09-11 18:09:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		snd_pcm_free_chmaps(maps); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-16 18:10:43 +02:00
										 |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2019-02-25 12:29:57 +01:00
										 |  |  | 		if (result.index > 0) | 
					
						
							| 
									
										
										
										
											2019-02-20 17:51:05 +01:00
										 |  |  | 			goto enum_end; | 
					
						
							| 
									
										
										
										
											2018-10-16 18:10:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 17:38:23 +01: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]); | 
					
						
							| 
									
										
										
										
											2018-10-16 18:10:43 +02:00
										 |  |  | 		spa_pod_builder_int(&b, SPA_CLAMP(DEFAULT_CHANNELS, min, max)); | 
					
						
							|  |  |  | 		if (min != max) { | 
					
						
							|  |  |  | 			spa_pod_builder_int(&b, min); | 
					
						
							|  |  |  | 			spa_pod_builder_int(&b, max); | 
					
						
							|  |  |  | 			choice->body.type = SPA_CHOICE_Range; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-01-22 17:38:23 +01:00
										 |  |  | 		spa_pod_builder_pop(&b, &f[1]); | 
					
						
							| 
									
										
										
										
											2018-10-16 18:10:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-11 18:09:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 17:38:23 +01:00
										 |  |  | 	fmt = spa_pod_builder_pop(&b, &f[0]); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-20 17:51:05 +01:00
										 |  |  | 	if ((res = 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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	unsigned int rrate, rchannels; | 
					
						
							|  |  |  | 	snd_pcm_uframes_t period_size; | 
					
						
							|  |  |  | 	int err, dir; | 
					
						
							|  |  |  | 	snd_pcm_hw_params_t *params; | 
					
						
							|  |  |  | 	snd_pcm_format_t format; | 
					
						
							|  |  |  | 	struct spa_audio_info_raw *info = &fmt->info.raw; | 
					
						
							|  |  |  | 	snd_pcm_t *hndl; | 
					
						
							|  |  |  | 	unsigned int periods; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((err = spa_alsa_open(state)) < 0) | 
					
						
							|  |  |  | 		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"); | 
					
						
							|  |  |  | 	/* set hardware resampling */ | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_set_rate_resample(hndl, params, 0), "set_rate_resample"); | 
					
						
							|  |  |  | 	/* set the interleaved read/write format */ | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_set_access(hndl, params, SND_PCM_ACCESS_MMAP_INTERLEAVED), "set_access"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* disable ALSA wakeups, we use a timer */ | 
					
						
							|  |  |  | 	if (snd_pcm_hw_params_can_disable_period_wakeup(params)) | 
					
						
							|  |  |  | 		CHECK(snd_pcm_hw_params_set_period_wakeup(hndl, params, 0), "set_period_wakeup"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* set the sample format */ | 
					
						
							| 
									
										
										
										
											2018-08-23 17:47:57 +02:00
										 |  |  | 	format = spa_format_to_alsa(info->format); | 
					
						
							| 
									
										
										
										
											2019-01-22 17:38:23 +01:00
										 |  |  | 	if (format == SND_PCM_FORMAT_UNKNOWN) { | 
					
						
							| 
									
										
										
										
											2019-03-13 15:02:04 +01:00
										 |  |  | 		spa_log_warn(state->log, "%p: unknown format %u", state, info->format); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 		return -EINVAL; | 
					
						
							| 
									
										
										
										
											2019-01-22 17:38:23 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-13 15:02:04 +01:00
										 |  |  | 	spa_log_info(state->log, "%p: Stream parameters are %iHz, %s, %i channels", | 
					
						
							|  |  |  | 			state, info->rate, snd_pcm_format_name(format), info->channels); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	CHECK(snd_pcm_hw_params_set_format(hndl, params, format), "set_format"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* set the count of channels */ | 
					
						
							|  |  |  | 	rchannels = info->channels; | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_set_channels_near(hndl, params, &rchannels), "set_channels"); | 
					
						
							|  |  |  | 	if (rchannels != info->channels) { | 
					
						
							| 
									
										
										
										
											2018-05-10 11:23:51 +02:00
										 |  |  | 		spa_log_warn(state->log, "Channels doesn't match (requested %u, get %u", info->channels, rchannels); | 
					
						
							| 
									
										
										
										
											2017-11-07 17:39:31 +01:00
										 |  |  | 		if (flags & SPA_NODE_PARAM_FLAG_NEAREST) | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 			info->channels = rchannels; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			return -EINVAL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* set the stream rate */ | 
					
						
							|  |  |  | 	rrate = info->rate; | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_set_rate_near(hndl, params, &rrate, 0), "set_rate_near"); | 
					
						
							|  |  |  | 	if (rrate != info->rate) { | 
					
						
							| 
									
										
										
										
											2018-05-10 11:23:51 +02:00
										 |  |  | 		spa_log_warn(state->log, "Rate doesn't match (requested %iHz, get %iHz)", info->rate, rrate); | 
					
						
							| 
									
										
										
										
											2017-11-07 17:39:31 +01:00
										 |  |  | 		if (flags & SPA_NODE_PARAM_FLAG_NEAREST) | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 			info->rate = rrate; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			return -EINVAL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	state->format = format; | 
					
						
							|  |  |  | 	state->channels = info->channels; | 
					
						
							|  |  |  | 	state->rate = info->rate; | 
					
						
							|  |  |  | 	state->frame_size = info->channels * (snd_pcm_format_physical_width(format) / 8); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dir = 0; | 
					
						
							| 
									
										
										
										
											2018-12-03 15:56:53 +01:00
										 |  |  | 	period_size = 1024; | 
					
						
							| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 	CHECK(snd_pcm_hw_params_get_buffer_size_max(params, &state->buffer_frames), "get_buffer_size_max"); | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params_set_buffer_size_near(hndl, params, &state->buffer_frames), "set_buffer_size_near"); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	state->period_frames = period_size; | 
					
						
							|  |  |  | 	periods = state->buffer_frames / state->period_frames; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-07 17:16:02 +02:00
										 |  |  | 	spa_log_info(state->log, "%p: buffer frames %lu, period frames %lu, periods %u, frame_size %zd", | 
					
						
							| 
									
										
										
										
											2019-03-13 15:02:04 +01:00
										 |  |  | 			state, state->buffer_frames, state->period_frames, | 
					
						
							|  |  |  | 			periods, state->frame_size); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* write the parameters to device */ | 
					
						
							|  |  |  | 	CHECK(snd_pcm_hw_params(hndl, params), "set_hw_params"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	CHECK(snd_pcm_sw_params_set_tstamp_mode(hndl, params, SND_PCM_TSTAMP_ENABLE), "sw_params_set_tstamp_mode"); | 
					
						
							| 
									
										
										
										
											2016-09-20 11:20:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	CHECK(snd_pcm_sw_params_set_period_event(hndl, params, 0), "set_period_event"); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 15:52:11 +01:00
										 |  |  | 	if (!state->slaved) { | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 		ts.it_value.tv_sec = time / SPA_NSEC_PER_SEC; | 
					
						
							|  |  |  | 		ts.it_value.tv_nsec = time % SPA_NSEC_PER_SEC; | 
					
						
							| 
									
										
										
										
											2018-11-12 15:52:11 +01:00
										 |  |  | 		ts.it_interval.tv_sec = 0; | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 		ts.it_interval.tv_nsec = 0; | 
					
						
							| 
									
										
										
										
											2019-06-06 15:21:40 +02:00
										 |  |  | 		spa_system_timerfd_settime(state->data_system, | 
					
						
							|  |  |  | 				state->timerfd, SPA_FD_TIMER_ABSTIME, &ts, NULL); | 
					
						
							| 
									
										
										
										
											2018-11-12 15:52:11 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2017-10-22 13:12:34 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | static void init_loop(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	state->bw = 0.0; | 
					
						
							|  |  |  | 	state->z1 = state->z2 = state->z3 = 0.0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void set_loop(struct state *state, double bw) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	double w = 2 * M_PI * bw * state->threshold / state->rate; | 
					
						
							|  |  |  | 	state->w0 = 1.0 - exp (-20.0 * w); | 
					
						
							|  |  |  | 	state->w1 = w * 1.5 / state->threshold; | 
					
						
							|  |  |  | 	state->w2 = w / 1.5; | 
					
						
							|  |  |  | 	state->bw = bw; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | static int alsa_recover(struct state *state, int err) | 
					
						
							| 
									
										
										
										
											2018-10-31 15:20:52 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	int res, st; | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 	snd_pcm_status_t *status; | 
					
						
							| 
									
										
										
										
											2018-10-31 15:20:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	snd_pcm_status_alloca(&status); | 
					
						
							|  |  |  | 	if ((res = snd_pcm_status(state->hndl, status)) < 0) { | 
					
						
							|  |  |  | 		spa_log_error(state->log, "snd_pcm_status error: %s", snd_strerror(res)); | 
					
						
							|  |  |  | 		return res; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-13 15:02:04 +01:00
										 |  |  | 		spa_log_error(state->log, "%p: xrun of %"PRIu64" usec %"PRIu64" %f", | 
					
						
							| 
									
										
										
										
											2019-08-22 13:23:48 +02:00
										 |  |  | 				state, delay, missing, state->safety); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		spa_node_call_xrun(&state->callbacks, | 
					
						
							|  |  |  | 				SPA_TIMEVAL_TO_USEC(&trigger), delay, NULL); | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		state->sample_count += missing ? missing : state->threshold; | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		spa_log_error(state->log, "recover from error state %d", st); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 	if ((res = snd_pcm_recover(state->hndl, err, true)) < 0) { | 
					
						
							|  |  |  | 		spa_log_error(state->log, "snd_pcm_recover error: %s", snd_strerror(res)); | 
					
						
							|  |  |  | 		return res; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 	init_loop(state); | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 	if (state->stream == SND_PCM_STREAM_CAPTURE) { | 
					
						
							|  |  |  | 		if ((res = snd_pcm_start(state->hndl)) < 0) { | 
					
						
							|  |  |  | 			spa_log_error(state->log, "snd_pcm_start: %s", snd_strerror(res)); | 
					
						
							|  |  |  | 			return res; | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 		state->alsa_started = true; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		state->alsa_started = false; | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 		spa_alsa_write(state, state->threshold * 2); | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | static int get_status(struct state *state, snd_pcm_uframes_t *delay, snd_pcm_uframes_t *target) | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	snd_pcm_sframes_t avail; | 
					
						
							| 
									
										
										
										
											2018-11-29 15:19:33 +01:00
										 |  |  | 	int res; | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	if ((avail = snd_pcm_avail(state->hndl)) < 0) { | 
					
						
							|  |  |  | 		if ((res = alsa_recover(state, avail)) < 0) | 
					
						
							| 
									
										
										
										
											2018-11-29 15:19:33 +01:00
										 |  |  | 			return res; | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 		if ((avail = snd_pcm_avail(state->hndl)) < 0) | 
					
						
							|  |  |  | 			return avail; | 
					
						
							| 
									
										
										
										
											2018-11-29 15:19:33 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 	*target = state->last_threshold; | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 	if (state->rate_match) { | 
					
						
							|  |  |  | 		state->delay = state->rate_match->delay; | 
					
						
							|  |  |  | 		state->read_size = state->rate_match->size; | 
					
						
							| 
									
										
										
										
											2019-08-30 16:31:51 +02:00
										 |  |  | 		/* We try to compensate for the latency introduced by rate matching
 | 
					
						
							|  |  |  | 		 * by moving a little closer to the device read/write pointers. | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 		 * Don't try to get closer than 48 samples but instead increase the | 
					
						
							| 
									
										
										
										
											2019-08-30 16:31:51 +02:00
										 |  |  | 		 * reported latency on the port (TODO). */ | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 		if (*target <= state->delay + 48) | 
					
						
							|  |  |  | 			state->delay = SPA_MAX(0, (int)(*target - 48 - state->delay)); | 
					
						
							|  |  |  | 		*target -= state->delay; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state->stream == SND_PCM_STREAM_PLAYBACK) { | 
					
						
							|  |  |  | 		*delay = state->buffer_frames - avail; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		*delay = avail; | 
					
						
							|  |  |  | 		*target = SPA_MAX(*target, state->read_size); | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int update_time(struct state *state, uint64_t nsec, snd_pcm_sframes_t delay, | 
					
						
							|  |  |  | 		snd_pcm_sframes_t target, bool slave) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	double err, corr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-25 16:15:52 +02:00
										 |  |  | 	if (state->stream == SND_PCM_STREAM_PLAYBACK) | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 		err = delay - target; | 
					
						
							| 
									
										
										
										
											2019-04-25 16:15:52 +02:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 		err = (target + 128) - delay; | 
					
						
							| 
									
										
										
										
											2019-04-25 16:15:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 	if (state->bw == 0.0) { | 
					
						
							|  |  |  | 		set_loop(state, BW_MAX); | 
					
						
							|  |  |  | 		state->next_time = nsec; | 
					
						
							|  |  |  | 		state->base_time = nsec; | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 	state->z1 += state->w0 * (state->w1 * err - state->z1); | 
					
						
							|  |  |  | 	state->z2 += state->w0 * (state->z1 - state->z2); | 
					
						
							|  |  |  | 	state->z3 += state->w2 * state->z2; | 
					
						
							| 
									
										
										
										
											2018-11-19 18:03:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 	corr = 1.0 - (state->z2 + state->z3); | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-25 16:15:52 +02:00
										 |  |  | 	if (state->last_threshold != state->threshold) { | 
					
						
							|  |  |  | 		int32_t diff = (int32_t) (state->last_threshold - state->threshold); | 
					
						
							|  |  |  | 		spa_log_trace(state->log, "slave:%d quantum change %d", slave, diff); | 
					
						
							|  |  |  | 		state->next_time += diff / corr * 1e9 / state->rate; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-25 12:53:23 +02:00
										 |  |  | 	if ((state->next_time - state->base_time) > BW_PERIOD) { | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 		state->base_time = state->next_time; | 
					
						
							|  |  |  | 		if (state->bw == BW_MAX) | 
					
						
							|  |  |  | 			set_loop(state, BW_MED); | 
					
						
							|  |  |  | 		else if (state->bw == BW_MED) | 
					
						
							|  |  |  | 			set_loop(state, BW_MIN); | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 		spa_log_debug(state->log, "slave:%d rate:%f bw:%f del:%d target:%ld err:%f (%f %f %f)", | 
					
						
							|  |  |  | 				slave, corr, state->bw, state->delay, target, | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 				err, state->z1, state->z2, state->z3); | 
					
						
							| 
									
										
										
										
											2018-11-09 15:24:24 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-31 15:20:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 	if (slave && state->rate_match) { | 
					
						
							|  |  |  | 		if (state->stream == SND_PCM_STREAM_PLAYBACK) | 
					
						
							|  |  |  | 			state->rate_match->rate = SPA_CLAMP(corr, 0.95, 1.05); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			state->rate_match->rate = SPA_CLAMP(1.0/corr, 0.95, 1.05); | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (!slave && state->clock) { | 
					
						
							| 
									
										
										
										
											2019-08-27 14:41:47 +02:00
										 |  |  | 		state->clock->nsec = nsec; | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 		state->clock->rate = SPA_FRACTION(1, state->rate); | 
					
						
							| 
									
										
										
										
											2019-08-27 14:41:47 +02:00
										 |  |  | 		state->clock->position += state->duration; | 
					
						
							|  |  |  | 		state->clock->duration = state->duration; | 
					
						
							|  |  |  | 		state->clock->count = state->clock->position; | 
					
						
							|  |  |  | 		state->clock->delay = state->duration * corr; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-25 16:15:52 +02:00
										 |  |  | 	spa_log_trace_fp(state->log, "slave:%d %"PRIu64" %f %ld %f %f %d", slave, nsec, | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 			corr, delay, err, state->threshold * corr, | 
					
						
							|  |  |  | 			state->threshold); | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 	state->next_time += state->threshold / corr * 1e9 / state->rate; | 
					
						
							| 
									
										
										
										
											2019-04-25 16:15:52 +02:00
										 |  |  | 	state->last_threshold = state->threshold; | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-31 15:20:52 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | int spa_alsa_write(struct state *state, snd_pcm_uframes_t silence) | 
					
						
							| 
									
										
										
										
											2017-10-22 13:12:34 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 	snd_pcm_t *hndl = state->hndl; | 
					
						
							|  |  |  | 	const snd_pcm_channel_area_t *my_areas; | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	snd_pcm_uframes_t written, frames, offset, off, to_write, total_written; | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 	int res; | 
					
						
							| 
									
										
										
										
											2017-10-22 13:12:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-27 14:41:47 +02:00
										 |  |  | 	if (state->position && state->duration != state->position->clock.duration) { | 
					
						
							|  |  |  | 		state->duration = state->position->clock.duration; | 
					
						
							|  |  |  | 		state->threshold = (state->duration * state->rate + state->rate_denom-1) / state->rate_denom; | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-12 10:18:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 	if (state->slaved && state->alsa_started) { | 
					
						
							|  |  |  | 		uint64_t nsec; | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 		snd_pcm_uframes_t delay, target; | 
					
						
							| 
									
										
										
										
											2018-11-12 10:18:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 		if ((res = get_status(state, &delay, &target)) < 0) | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 			return res; | 
					
						
							| 
									
										
										
										
											2018-11-12 10:18:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 		if (delay > target + state->threshold) { | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 			spa_log_warn(state->log, "slave delay:%ld resync %f %f %f", delay, | 
					
						
							| 
									
										
										
										
											2019-04-25 12:53:23 +02:00
										 |  |  | 					state->z1, state->z2, state->z3); | 
					
						
							|  |  |  | 			init_loop(state); | 
					
						
							|  |  |  | 			state->alsa_sync = true; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 		if (state->alsa_sync) { | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 			if (delay > target) | 
					
						
							|  |  |  | 				snd_pcm_rewind(state->hndl, delay - target); | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 			else | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 				snd_pcm_forward(state->hndl, target - delay); | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 			delay = target; | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 			state->alsa_sync = false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 15:48:21 +02:00
										 |  |  | 		nsec = state->position->clock.nsec; | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 		if ((res = update_time(state, nsec, delay, target, true)) < 0) | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 			return res; | 
					
						
							| 
									
										
										
										
											2018-11-12 10:18:21 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	total_written = 0; | 
					
						
							|  |  |  | again: | 
					
						
							| 
									
										
										
										
											2018-11-12 10:18:21 +01:00
										 |  |  | 	frames = state->buffer_frames; | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 	if ((res = snd_pcm_mmap_begin(hndl, &my_areas, &offset, &frames)) < 0) { | 
					
						
							|  |  |  | 		spa_log_error(state->log, "snd_pcm_mmap_begin error: %s", snd_strerror(res)); | 
					
						
							|  |  |  | 		return res; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 	spa_log_trace_fp(state->log, "begin %ld %ld %d %ld", offset, frames, state->threshold, silence); | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	silence = SPA_MIN(silence, frames); | 
					
						
							|  |  |  | 	to_write = frames; | 
					
						
							| 
									
										
										
										
											2018-07-31 17:40:32 +02:00
										 |  |  | 	off = offset; | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 	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-21 12:30:15 +01:00
										 |  |  | 		uint8_t *dst, *src; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 		uint32_t index, offs, avail, size, maxsize, l0, l1; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 17:40:32 +02:00
										 |  |  | 		dst = SPA_MEMBER(my_areas[0].addr, off * state->frame_size, uint8_t); | 
					
						
							| 
									
										
										
										
											2017-11-21 12:30:15 +01:00
										 |  |  | 		src = d[0].data; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 		size = d[0].chunk->size; | 
					
						
							|  |  |  | 		maxsize = d[0].maxsize; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 12:30:15 +01:00
										 |  |  | 		index = d[0].chunk->offset + state->ready_offset; | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 		avail = size - state->ready_offset; | 
					
						
							| 
									
										
										
										
											2017-11-15 17:25:36 +01:00
										 |  |  | 		avail /= state->frame_size; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 17:25:36 +01:00
										 |  |  | 		n_frames = SPA_MIN(avail, to_write); | 
					
						
							|  |  |  | 		n_bytes = n_frames * state->frame_size; | 
					
						
							| 
									
										
										
										
											2017-10-31 15:59:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 		offs = index % maxsize; | 
					
						
							|  |  |  | 		l0 = SPA_MIN(n_bytes, maxsize - offs); | 
					
						
							| 
									
										
										
										
											2017-11-21 12:30:15 +01:00
										 |  |  | 		l1 = n_bytes - l0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-20 19:27:46 +01:00
										 |  |  | 		spa_memcpy(dst, src + offs, l0); | 
					
						
							| 
									
										
										
										
											2017-11-21 12:30:15 +01:00
										 |  |  | 		if (l1 > 0) | 
					
						
							| 
									
										
										
										
											2019-03-20 19:27:46 +01:00
										 |  |  | 			spa_memcpy(dst + l0, src, l1); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 12:30:15 +01:00
										 |  |  | 		state->ready_offset += n_bytes; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 		if (state->ready_offset >= size) { | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2019-03-21 11:31:53 +01:00
										 |  |  | 			spa_log_trace_fp(state->log, "alsa-util %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
										 |  |  | 		if (silence > n_frames) | 
					
						
							|  |  |  | 			silence -= n_frames; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			silence = 0; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-31 15:59:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 	if (silence > 0) { | 
					
						
							| 
									
										
										
										
											2019-03-21 11:31:53 +01:00
										 |  |  | 		spa_log_trace_fp(state->log, "silence %ld", silence); | 
					
						
							| 
									
										
										
										
											2018-07-31 17:40:32 +02:00
										 |  |  | 		snd_pcm_areas_silence(my_areas, off, state->channels, silence, state->format); | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 		written += silence; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-31 15:59:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 11:31:53 +01:00
										 |  |  | 	spa_log_trace_fp(state->log, "commit %ld %ld %"PRIi64, offset, written, state->sample_count); | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	total_written += written; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 	if ((res = snd_pcm_mmap_commit(hndl, offset, written)) < 0) { | 
					
						
							|  |  |  | 		spa_log_error(state->log, "snd_pcm_mmap_commit error: %s", snd_strerror(res)); | 
					
						
							|  |  |  | 		if (res != -EPIPE && res != -ESTRPIPE) | 
					
						
							|  |  |  | 			return res; | 
					
						
							| 
									
										
										
										
											2017-10-24 18:04:03 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	if (!state->alsa_started && total_written > 0) { | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 		spa_log_trace(state->log, "snd_pcm_start %lu", written); | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 		if ((res = snd_pcm_start(hndl)) < 0) { | 
					
						
							|  |  |  | 			spa_log_error(state->log, "snd_pcm_start: %s", snd_strerror(res)); | 
					
						
							|  |  |  | 			return res; | 
					
						
							| 
									
										
										
										
											2017-10-24 18:04:03 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 		state->alsa_started = true; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 	    snd_pcm_uframes_t frames, | 
					
						
							|  |  |  | 	    snd_pcm_uframes_t keep) | 
					
						
							| 
									
										
										
										
											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)) { | 
					
						
							| 
									
										
										
										
											2019-03-13 15:02:04 +01:00
										 |  |  | 		spa_log_warn(state->log, "%p: no more buffers", state); | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 		total_frames = frames; | 
					
						
							|  |  |  | 	} else if (frames > 0) { | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 		uint8_t *src; | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 		size_t n_bytes, left; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 		struct buffer *b; | 
					
						
							|  |  |  | 		struct spa_data *d; | 
					
						
							| 
									
										
										
										
											2019-04-26 15:48:21 +02:00
										 |  |  | 		uint32_t 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; | 
					
						
							| 
									
										
										
										
											2018-11-07 09:56:24 +01:00
										 |  |  | 			b->h->pts = SPA_TIMESPEC_TO_NSEC(&state->now); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 12:30:15 +01:00
										 |  |  | 		avail = d[0].maxsize / state->frame_size; | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 		total_frames = SPA_MIN(avail, frames); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 		n_bytes = total_frames * state->frame_size; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 		if (my_areas) { | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 			left = state->buffer_frames - offset; | 
					
						
							|  |  |  | 			l0 = SPA_MIN(n_bytes, left * state->frame_size); | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 			l1 = n_bytes - l0; | 
					
						
							| 
									
										
										
										
											2017-11-21 12:30:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 			src = SPA_MEMBER(my_areas[0].addr, offset * state->frame_size, uint8_t); | 
					
						
							|  |  |  | 			spa_memcpy(d[0].data, src, l0); | 
					
						
							|  |  |  | 			if (l1 > 0) | 
					
						
							| 
									
										
										
										
											2019-09-05 09:17:14 +02:00
										 |  |  | 				spa_memcpy(SPA_MEMBER(d[0].data, l0, void), my_areas[0].addr, l1); | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			memset(d[0].data, 0, n_bytes); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 15:48:21 +02:00
										 |  |  | 		d[0].chunk->offset = 0; | 
					
						
							| 
									
										
										
										
											2017-11-21 12:30:15 +01:00
										 |  |  | 		d[0].chunk->size = n_bytes; | 
					
						
							| 
									
										
										
										
											2017-11-15 17:25:36 +01:00
										 |  |  | 		d[0].chunk->stride = state->frame_size; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 11:02:21 +01:00
										 |  |  | 		SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUT); | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 		spa_list_append(&state->ready, &b->link); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 	return total_frames - keep; | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:15 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | int spa_alsa_read(struct state *state, snd_pcm_uframes_t silence) | 
					
						
							| 
									
										
										
										
											2017-03-28 19:29:08 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	snd_pcm_t *hndl = state->hndl; | 
					
						
							|  |  |  | 	snd_pcm_uframes_t total_read = 0, to_read; | 
					
						
							|  |  |  | 	const snd_pcm_channel_area_t *my_areas; | 
					
						
							|  |  |  | 	snd_pcm_uframes_t read, frames, offset; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	int res; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-30 17:32:56 +02:00
										 |  |  | 	if (state->position) { | 
					
						
							|  |  |  | 		if (state->duration != state->position->clock.duration) { | 
					
						
							|  |  |  | 			state->duration = state->position->clock.duration; | 
					
						
							|  |  |  | 			state->threshold = (state->duration * state->rate + state->rate_denom-1) / state->rate_denom; | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-30 17:32:56 +02:00
										 |  |  | 		if (!state->slaved) { | 
					
						
							|  |  |  | 			uint64_t position; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			position = state->position->clock.position; | 
					
						
							|  |  |  | 			if (state->last_position && state->last_position + state->last_duration != position) { | 
					
						
							|  |  |  | 				state->alsa_sync = true; | 
					
						
							|  |  |  | 				spa_log_warn(state->log, "discont, resync %"PRIu64" %"PRIu64" %d", | 
					
						
							|  |  |  | 						state->last_position, position, state->last_duration); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			state->last_position = position; | 
					
						
							|  |  |  | 			state->last_duration = state->duration; | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-09 15:24:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 15:48:21 +02:00
										 |  |  | 	if (state->slaved && state->alsa_started) { | 
					
						
							|  |  |  | 		uint64_t nsec; | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 		snd_pcm_uframes_t delay, target; | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 		uint32_t threshold = state->threshold; | 
					
						
							| 
									
										
										
										
											2018-10-31 15:20:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 		if ((res = get_status(state, &delay, &target)) < 0) | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 			return res; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 		if (delay < target) { | 
					
						
							| 
									
										
										
										
											2019-06-07 17:16:02 +02:00
										 |  |  | 			spa_log_warn(state->log, "slave delay:%lu resync %f %f %f", delay, | 
					
						
							| 
									
										
										
										
											2019-04-25 12:53:23 +02:00
										 |  |  | 					state->z1, state->z2, state->z3); | 
					
						
							|  |  |  | 			init_loop(state); | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 			state->alsa_sync = true; | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-04-26 15:48:21 +02:00
										 |  |  | 		if (state->alsa_sync) { | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 			spa_log_warn(state->log, "slave resync %ld %d %ld", delay, threshold, target); | 
					
						
							|  |  |  | 			if (delay < target) | 
					
						
							|  |  |  | 				snd_pcm_rewind(state->hndl, target - delay); | 
					
						
							|  |  |  | 			else if (delay > target) | 
					
						
							|  |  |  | 				snd_pcm_forward(state->hndl, delay - target); | 
					
						
							| 
									
										
										
										
											2019-04-25 12:53:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 			delay = target; | 
					
						
							| 
									
										
										
										
											2019-04-26 15:48:21 +02:00
										 |  |  | 			state->alsa_sync = false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		nsec = state->position->clock.nsec; | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 		if ((res = update_time(state, nsec, delay, target, true)) < 0) | 
					
						
							| 
									
										
										
										
											2019-04-25 12:53:23 +02:00
										 |  |  | 			return res; | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 	frames = state->read_size; | 
					
						
							|  |  |  | 	if (frames == 0) | 
					
						
							|  |  |  | 		frames = state->threshold + state->delay; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	to_read = state->buffer_frames; | 
					
						
							|  |  |  | 	if ((res = snd_pcm_mmap_begin(hndl, &my_areas, &offset, &to_read)) < 0) { | 
					
						
							|  |  |  | 		spa_log_error(state->log, "snd_pcm_mmap_begin error: %s", snd_strerror(res)); | 
					
						
							|  |  |  | 		return res; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-29 10:00:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	spa_log_trace_fp(state->log, "begin %ld %ld %ld %d", offset, frames, to_read, state->threshold); | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 	read = push_frames(state, my_areas, offset, frames, state->delay); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	spa_log_trace_fp(state->log, "commit %ld %ld %"PRIi64, offset, read, state->sample_count); | 
					
						
							|  |  |  | 	total_read += read; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 	if ((res = snd_pcm_mmap_commit(hndl, offset, read)) < 0) { | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 		spa_log_error(state->log, "snd_pcm_mmap_commit error: %s", snd_strerror(res)); | 
					
						
							|  |  |  | 		if (res != -EPIPE && res != -ESTRPIPE) | 
					
						
							|  |  |  | 			return res; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	state->sample_count += total_read; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | static int handle_play(struct state *state, uint64_t nsec, | 
					
						
							|  |  |  | 		snd_pcm_uframes_t delay, snd_pcm_uframes_t target) | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							| 
									
										
										
										
											2018-11-19 11:51:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 	if (delay > target + state->last_threshold) { | 
					
						
							|  |  |  | 		spa_log_trace(state->log, "early wakeup %ld %ld", delay, target); | 
					
						
							|  |  |  | 		state->next_time = nsec + (delay - target) * SPA_NSEC_PER_SEC / state->rate; | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 		return -EAGAIN; | 
					
						
							| 
									
										
										
										
											2018-11-19 11:51:51 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-09 15:24:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 	if ((res = update_time(state, nsec, delay, target, false)) < 0) | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2018-11-19 18:03:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	if (spa_list_is_empty(&state->ready)) { | 
					
						
							|  |  |  | 		struct spa_io_buffers *io = state->io; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 11:31:53 +01:00
										 |  |  | 		spa_log_trace_fp(state->log, "alsa-util %p: %d", state, io->status); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 		io->status = SPA_STATUS_NEED_BUFFER; | 
					
						
							| 
									
										
										
										
											2019-05-16 16:42:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-15 11:19:23 +02:00
										 |  |  | 		res = spa_node_call_ready(&state->callbacks, SPA_STATUS_NEED_BUFFER); | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 		res = spa_alsa_write(state, 0); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	return res; | 
					
						
							| 
									
										
										
										
											2017-03-28 19:29:08 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | static int handle_capture(struct state *state, uint64_t nsec, | 
					
						
							|  |  |  | 		snd_pcm_uframes_t delay, snd_pcm_uframes_t target) | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	int res; | 
					
						
							|  |  |  | 	struct spa_io_buffers *io; | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 	if (delay < target) { | 
					
						
							|  |  |  | 		spa_log_trace(state->log, "early wakeup %ld %ld", delay, target); | 
					
						
							|  |  |  | 		state->next_time = nsec + (target - delay) * SPA_NSEC_PER_SEC / | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 			state->rate; | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 	if ((res = update_time(state, nsec, delay, target, false)) < 0) | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 	if ((res = spa_alsa_read(state, target)) < 0) | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2018-11-20 16:08:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	if (!spa_list_is_empty(&state->ready)) { | 
					
						
							|  |  |  | 		io = state->io; | 
					
						
							|  |  |  | 		if (io != NULL && io->status != SPA_STATUS_HAVE_BUFFER) { | 
					
						
							|  |  |  | 			struct buffer *b = spa_list_first(&state->ready, struct buffer, link); | 
					
						
							|  |  |  | 			spa_list_remove(&b->link); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			io->buffer_id = b->id; | 
					
						
							|  |  |  | 			io->status = SPA_STATUS_HAVE_BUFFER; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 	spa_node_call_ready(&state->callbacks, SPA_STATUS_HAVE_BUFFER); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | static void alsa_on_timeout_event(struct spa_source *source) | 
					
						
							| 
									
										
										
										
											2017-03-30 17:30:28 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	struct state *state = source->data; | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 	snd_pcm_uframes_t delay, target; | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	uint64_t nsec; | 
					
						
							|  |  |  | 	uint64_t expire; | 
					
						
							|  |  |  | 	int res; | 
					
						
							| 
									
										
										
										
											2018-04-27 17:29:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 15:21:40 +02:00
										 |  |  | 	if (state->started && spa_system_timerfd_read(state->data_system, state->timerfd, &expire) < 0) | 
					
						
							| 
									
										
										
										
											2019-06-20 17:31:29 +02:00
										 |  |  | 		spa_log_warn(state->log, "error reading timerfd: %m"); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 	if (state->position) { | 
					
						
							| 
									
										
										
										
											2019-08-27 14:41:47 +02:00
										 |  |  | 		state->duration = state->position->clock.duration; | 
					
						
							|  |  |  | 		state->threshold = (state->duration * state->rate + state->rate_denom-1) / state->rate_denom; | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 15:21:40 +02:00
										 |  |  | 	spa_system_clock_gettime(state->data_system, CLOCK_MONOTONIC, &state->now); | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 	if ((res = get_status(state, &delay, &target)) < 0) | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 18:03:45 +01:00
										 |  |  | 	nsec = SPA_TIMESPEC_TO_NSEC(&state->now); | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 	spa_log_trace_fp(state->log, "timeout %lu %lu %"PRIu64" %"PRIu64" %"PRIi64" %d %"PRIi64, | 
					
						
							|  |  |  | 			delay, target, nsec, state->next_time, nsec - state->next_time, | 
					
						
							| 
									
										
										
										
											2019-03-21 11:31:53 +01:00
										 |  |  | 			state->threshold, state->sample_count); | 
					
						
							| 
									
										
										
										
											2018-10-24 10:27:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	if (state->stream == SND_PCM_STREAM_PLAYBACK) | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 		handle_play(state, nsec, delay, target); | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2019-09-05 13:41:59 +02:00
										 |  |  | 		handle_capture(state, nsec, delay, target); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	set_timeout(state, state->next_time); | 
					
						
							| 
									
										
										
										
											2017-03-30 17:30:28 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-05 11:23:30 +01:00
										 |  |  | static void reset_buffers(struct state *this) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-07 15:52:42 +01:00
										 |  |  | 	uint32_t i; | 
					
						
							| 
									
										
										
										
											2018-12-05 11:23:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	spa_list_init(&this->free); | 
					
						
							|  |  |  | 	spa_list_init(&this->ready); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			spa_list_append(&this->free, &b->link); | 
					
						
							|  |  |  | 			SPA_FLAG_UNSET(b->flags, BUFFER_FLAG_OUT); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 15:38:06 +02:00
										 |  |  | static int set_timers(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct itimerspec ts; | 
					
						
							|  |  |  | 	ts.it_value.tv_sec = 0; | 
					
						
							|  |  |  | 	if (state->slaved) | 
					
						
							|  |  |  | 		ts.it_value.tv_nsec = 0; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		ts.it_value.tv_nsec = 1; | 
					
						
							|  |  |  | 	ts.it_interval.tv_sec = 0; | 
					
						
							|  |  |  | 	ts.it_interval.tv_nsec = 0; | 
					
						
							| 
									
										
										
										
											2019-06-06 15:21:40 +02:00
										 |  |  | 	spa_system_timerfd_settime(state->data_system, state->timerfd, 0, &ts, NULL); | 
					
						
							| 
									
										
										
										
											2019-04-24 15:38:06 +02:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline bool is_slaved(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return state->position && state->clock && state->position->clock.id != state->clock->id; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | int spa_alsa_start(struct state *state) | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	int err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state->started) | 
					
						
							| 
									
										
										
										
											2017-11-13 09:41:41 +01:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 15:38:06 +02:00
										 |  |  | 	state->slaved = is_slaved(state); | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (state->position) { | 
					
						
							| 
									
										
										
										
											2019-08-27 14:41:47 +02:00
										 |  |  | 		state->duration = state->position->clock.duration; | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 		state->rate_denom = state->position->clock.rate.denom; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2019-08-27 14:41:47 +02:00
										 |  |  | 		state->duration = state->props.min_latency; | 
					
						
							| 
									
										
										
										
											2019-07-09 16:56:05 +02:00
										 |  |  | 		state->rate_denom = state->rate; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-27 14:41:47 +02:00
										 |  |  | 	state->threshold = (state->duration * state->rate + state->rate_denom-1) / state->rate_denom; | 
					
						
							| 
									
										
										
										
											2019-04-25 16:15:52 +02:00
										 |  |  | 	state->last_threshold = state->threshold; | 
					
						
							| 
									
										
										
										
											2018-11-12 10:18:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 	init_loop(state); | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	state->safety = 0.0; | 
					
						
							| 
									
										
										
										
											2018-11-09 15:24:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 	spa_log_debug(state->log, "alsa %p: start %d slave:%d", state, state->threshold, state->slaved); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	CHECK(set_swparams(state), "swparams"); | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | 	snd_pcm_dump(state->hndl, state->output); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ((err = snd_pcm_prepare(state->hndl)) < 0) { | 
					
						
							|  |  |  | 		spa_log_error(state->log, "snd_pcm_prepare error: %s", snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											2017-11-13 09:41:41 +01:00
										 |  |  | 		return err; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 15:38:06 +02:00
										 |  |  | 	state->source.func = alsa_on_timeout_event; | 
					
						
							|  |  |  | 	state->source.data = state; | 
					
						
							|  |  |  | 	state->source.fd = state->timerfd; | 
					
						
							|  |  |  | 	state->source.mask = SPA_IO_IN; | 
					
						
							|  |  |  | 	state->source.rmask = 0; | 
					
						
							|  |  |  | 	spa_loop_add_source(state->data_loop, &state->source); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-05 11:23:30 +01:00
										 |  |  | 	reset_buffers(state); | 
					
						
							| 
									
										
										
										
											2019-04-23 17:31:29 +02:00
										 |  |  | 	state->alsa_sync = true; | 
					
						
							| 
									
										
										
										
											2018-12-05 11:23:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	if (state->stream == SND_PCM_STREAM_PLAYBACK) { | 
					
						
							|  |  |  | 		state->alsa_started = false; | 
					
						
							| 
									
										
										
										
											2019-04-11 16:44:07 +02:00
										 |  |  | 		spa_alsa_write(state, state->threshold * 2); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		if ((err = snd_pcm_start(state->hndl)) < 0) { | 
					
						
							|  |  |  | 			spa_log_error(state->log, "snd_pcm_start: %s", snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											2017-11-13 09:41:41 +01:00
										 |  |  | 			return err; | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		state->alsa_started = true; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-01-18 11:57:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 15:38:06 +02:00
										 |  |  | 	set_timers(state); | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	state->started = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-13 09:41:41 +01:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 15:38:06 +02:00
										 |  |  | static int do_reslave(struct spa_loop *loop, | 
					
						
							|  |  |  | 			    bool async, | 
					
						
							|  |  |  | 			    uint32_t seq, | 
					
						
							|  |  |  | 			    const void *data, | 
					
						
							|  |  |  | 			    size_t size, | 
					
						
							|  |  |  | 			    void *user_data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct state *state = user_data; | 
					
						
							|  |  |  | 	set_timers(state); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int spa_alsa_reslave(struct state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	bool slaved; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!state->started) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	slaved = is_slaved(state); | 
					
						
							|  |  |  | 	if (slaved != state->slaved) { | 
					
						
							|  |  |  | 		spa_log_debug(state->log, "alsa %p: reslave %d->%d", state, state->slaved, slaved); | 
					
						
							|  |  |  | 		state->slaved = slaved; | 
					
						
							|  |  |  | 		spa_loop_invoke(state->data_loop, do_reslave, 0, NULL, 0, true, state); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 16:56:44 +02:00
										 |  |  | static int do_remove_source(struct spa_loop *loop, | 
					
						
							|  |  |  | 			    bool async, | 
					
						
							|  |  |  | 			    uint32_t seq, | 
					
						
							|  |  |  | 			    const void *data, | 
					
						
							| 
									
										
										
										
											2017-11-20 15:26:44 +01:00
										 |  |  | 			    size_t size, | 
					
						
							| 
									
										
										
										
											2017-10-17 16:56:44 +02:00
										 |  |  | 			    void *user_data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct state *state = user_data; | 
					
						
							| 
									
										
										
										
											2018-01-18 11:57:23 +01:00
										 |  |  | 	struct itimerspec ts; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 15:38:06 +02:00
										 |  |  | 	spa_loop_remove_source(state->data_loop, &state->source); | 
					
						
							|  |  |  | 	ts.it_value.tv_sec = 0; | 
					
						
							|  |  |  | 	ts.it_value.tv_nsec = 0; | 
					
						
							|  |  |  | 	ts.it_interval.tv_sec = 0; | 
					
						
							|  |  |  | 	ts.it_interval.tv_nsec = 0; | 
					
						
							| 
									
										
										
										
											2019-06-06 15:21:40 +02:00
										 |  |  | 	spa_system_timerfd_settime(state->data_system, state->timerfd, 0, &ts, NULL); | 
					
						
							| 
									
										
										
										
											2018-01-18 11:57:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-13 09:41:41 +01:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2017-10-17 16:56:44 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 17:00:40 +01:00
										 |  |  | int spa_alsa_pause(struct state *state) | 
					
						
							| 
									
										
										
										
											2016-06-28 12:21:56 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	int err; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-20 15:48:47 +01:00
										 |  |  | 	spa_log_debug(state->log, "alsa %p: pause", state); | 
					
						
							| 
									
										
										
										
											2017-03-30 17:30:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-20 15:26:44 +01:00
										 |  |  | 	spa_loop_invoke(state->data_loop, do_remove_source, 0, NULL, 0, true, state); | 
					
						
							| 
									
										
										
										
											2016-10-28 16:56:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	if ((err = snd_pcm_drop(state->hndl)) < 0) | 
					
						
							|  |  |  | 		spa_log_error(state->log, "snd_pcm_drop %s", snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											2016-08-24 16:26:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | 	state->started = 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
										 |  |  | } |