| 
									
										
										
										
											2016-09-20 11:20:43 +02:00
										 |  |  | /* Spa ALSA Sink
 | 
					
						
							|  |  |  |  * Copyright (C) 2016 Wim Taymans <wim.taymans@gmail.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This library is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU Library General Public | 
					
						
							|  |  |  |  * License as published by the Free Software Foundation; either | 
					
						
							|  |  |  |  * version 2 of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This library is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
					
						
							|  |  |  |  * Library General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Library General Public | 
					
						
							|  |  |  |  * License along with this library; if not, write to the | 
					
						
							|  |  |  |  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, | 
					
						
							|  |  |  |  * Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef __SPA_ALSA_UTILS_H__
 | 
					
						
							|  |  |  | #define __SPA_ALSA_UTILS_H__
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | extern "C" { | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stddef.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <asoundlib.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 13:36:14 +01:00
										 |  |  | #include <spa/support/loop.h>
 | 
					
						
							|  |  |  | #include <spa/support/log.h>
 | 
					
						
							|  |  |  | #include <spa/utils/list.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <spa/node/node.h>
 | 
					
						
							| 
									
										
										
										
											2017-11-21 19:34:37 +01:00
										 |  |  | #include <spa/node/io.h>
 | 
					
						
							| 
									
										
										
										
											2018-08-25 12:08:29 +02:00
										 |  |  | #include <spa/param/param.h>
 | 
					
						
							| 
									
										
										
										
											2017-11-10 13:36:14 +01:00
										 |  |  | #include <spa/param/audio/format-utils.h>
 | 
					
						
							| 
									
										
										
										
											2016-09-20 11:20:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-15 11:26:03 +02:00
										 |  |  | #define DEFAULT_RATE		48000
 | 
					
						
							|  |  |  | #define DEFAULT_CHANNELS	2
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 17:25:58 +02:00
										 |  |  | struct props { | 
					
						
							|  |  |  | 	char device[64]; | 
					
						
							|  |  |  | 	char device_name[128]; | 
					
						
							|  |  |  | 	char card_name[128]; | 
					
						
							|  |  |  | 	uint32_t min_latency; | 
					
						
							| 
									
										
										
										
											2017-10-31 15:59:08 +01:00
										 |  |  | 	uint32_t max_latency; | 
					
						
							| 
									
										
										
										
											2017-05-25 13:28:15 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-09-20 11:20:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 15:59:08 +01:00
										 |  |  | #define MAX_BUFFERS 32
 | 
					
						
							| 
									
										
										
										
											2016-09-29 18:18:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 17:25:58 +02:00
										 |  |  | struct buffer { | 
					
						
							| 
									
										
										
										
											2018-05-11 09:56:46 +02:00
										 |  |  | 	struct spa_buffer *buf; | 
					
						
							| 
									
										
										
										
											2017-05-26 17:25:58 +02:00
										 |  |  | 	struct spa_meta_header *h; | 
					
						
							| 
									
										
										
										
											2018-03-08 11:02:21 +01:00
										 |  |  | #define BUFFER_FLAG_OUT	(1<<0)
 | 
					
						
							|  |  |  | 	uint32_t flags; | 
					
						
							| 
									
										
										
										
											2017-05-26 17:25:58 +02:00
										 |  |  | 	struct spa_list link; | 
					
						
							| 
									
										
										
										
											2016-09-20 11:20:43 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 17:25:58 +02:00
										 |  |  | struct state { | 
					
						
							|  |  |  | 	struct spa_handle handle; | 
					
						
							|  |  |  | 	struct spa_node node; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	uint32_t seq; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct spa_log *log; | 
					
						
							|  |  |  | 	struct spa_loop *main_loop; | 
					
						
							|  |  |  | 	struct spa_loop *data_loop; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	snd_pcm_stream_t stream; | 
					
						
							|  |  |  | 	snd_output_t *output; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-14 18:32:39 +02:00
										 |  |  | 	const struct spa_node_callbacks *callbacks; | 
					
						
							| 
									
										
										
										
											2017-08-06 06:42:26 +02:00
										 |  |  | 	void *callbacks_data; | 
					
						
							| 
									
										
										
										
											2017-05-26 17:25:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	struct props props; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool opened; | 
					
						
							|  |  |  | 	snd_pcm_t *hndl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool have_format; | 
					
						
							|  |  |  | 	struct spa_audio_info current_format; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	snd_pcm_uframes_t buffer_frames; | 
					
						
							|  |  |  | 	snd_pcm_uframes_t period_frames; | 
					
						
							|  |  |  | 	snd_pcm_format_t format; | 
					
						
							|  |  |  | 	int rate; | 
					
						
							|  |  |  | 	int channels; | 
					
						
							|  |  |  | 	size_t frame_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct spa_port_info info; | 
					
						
							| 
									
										
										
										
											2017-11-21 19:34:37 +01:00
										 |  |  | 	struct spa_io_buffers *io; | 
					
						
							| 
									
										
										
										
											2018-08-28 18:16:41 +02:00
										 |  |  | 	struct spa_io_range *range; | 
					
						
							| 
									
										
										
										
											2018-07-17 09:50:51 +02:00
										 |  |  | 	struct spa_io_clock *clock; | 
					
						
							| 
									
										
										
										
											2017-05-26 17:25:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	struct buffer buffers[MAX_BUFFERS]; | 
					
						
							|  |  |  | 	unsigned int n_buffers; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct spa_list free; | 
					
						
							|  |  |  | 	struct spa_list ready; | 
					
						
							| 
									
										
										
										
											2017-10-31 15:59:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 12:30:15 +01:00
										 |  |  | 	size_t ready_offset; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 17:25:58 +02:00
										 |  |  | 	bool started; | 
					
						
							|  |  |  | 	struct spa_source source; | 
					
						
							|  |  |  | 	int timerfd; | 
					
						
							|  |  |  | 	bool alsa_started; | 
					
						
							|  |  |  | 	int threshold; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 12:30:15 +01:00
										 |  |  | 	snd_htimestamp_t now; | 
					
						
							| 
									
										
										
										
											2017-05-26 17:25:58 +02:00
										 |  |  | 	int64_t sample_count; | 
					
						
							| 
									
										
										
										
											2017-11-21 12:30:15 +01:00
										 |  |  | 	int64_t filled; | 
					
						
							| 
									
										
										
										
											2017-10-24 18:04:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	uint64_t underrun; | 
					
						
							| 
									
										
										
										
											2016-09-20 11:20:43 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 13:28:15 +02:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2017-05-26 17:25:58 +02:00
										 |  |  | spa_alsa_enum_format(struct state *state, | 
					
						
							| 
									
										
										
										
											2017-11-07 17:39:31 +01:00
										 |  |  | 		     uint32_t *index, | 
					
						
							| 
									
										
										
										
											2017-11-13 17:57:38 +01:00
										 |  |  | 		     const struct spa_pod *filter, | 
					
						
							|  |  |  | 		     struct spa_pod **result, | 
					
						
							| 
									
										
										
										
											2017-11-07 17:39:31 +01:00
										 |  |  | 		     struct spa_pod_builder *builder); | 
					
						
							| 
									
										
										
										
											2017-04-04 19:44:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 17:25:58 +02:00
										 |  |  | int spa_alsa_set_format(struct state *state, struct spa_audio_info *info, uint32_t flags); | 
					
						
							| 
									
										
										
										
											2016-09-20 11:20:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 17:25:58 +02:00
										 |  |  | int spa_alsa_start(struct state *state, bool xrun_recover); | 
					
						
							|  |  |  | int spa_alsa_pause(struct state *state, bool xrun_recover); | 
					
						
							|  |  |  | int spa_alsa_close(struct state *state); | 
					
						
							| 
									
										
										
										
											2016-09-20 11:20:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 22:00:20 +02:00
										 |  |  | int spa_alsa_write(struct state *state, snd_pcm_uframes_t silence); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 11:20:43 +02:00
										 |  |  | #ifdef __cplusplus
 | 
					
						
							| 
									
										
										
										
											2017-05-26 08:05:01 +02:00
										 |  |  | } /* extern "C" */ | 
					
						
							| 
									
										
										
										
											2016-09-20 11:20:43 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* __SPA_ALSA_UTILS_H__ */
 |