| 
									
										
										
										
											2023-02-08 18:12:00 +01:00
										 |  |  | /* PipeWire */ | 
					
						
							|  |  |  | /* SPDX-FileCopyrightText: Copyright © 2021 Wim Taymans */ | 
					
						
							|  |  |  | /* SPDX-License-Identifier: MIT */ | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | #include <sys/stat.h>
 | 
					
						
							|  |  |  | #include <fcntl.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <signal.h>
 | 
					
						
							|  |  |  | #include <limits.h>
 | 
					
						
							|  |  |  | #include <math.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "config.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <spa/utils/result.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-18 11:36:13 +10:00
										 |  |  | #include <spa/utils/string.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | #include <spa/utils/json.h>
 | 
					
						
							|  |  |  | #include <spa/utils/ringbuffer.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | #include <spa/utils/dll.h>
 | 
					
						
							| 
									
										
										
										
											2023-07-11 19:25:13 +02:00
										 |  |  | #include <spa/utils/ratelimit.h>
 | 
					
						
							| 
									
										
										
										
											2023-01-18 13:00:06 +01:00
										 |  |  | #include <spa/debug/types.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | #include <spa/pod/builder.h>
 | 
					
						
							|  |  |  | #include <spa/param/audio/format-utils.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-28 11:19:05 +02:00
										 |  |  | #include <spa/param/latency-utils.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | #include <spa/param/audio/raw.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <pipewire/impl.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-12 16:49:19 +02:00
										 |  |  | #include <pipewire/i18n.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <pulse/pulseaudio.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-13 12:26:20 +01:00
										 |  |  | #include "module-protocol-pulse/defs.h"
 | 
					
						
							| 
									
										
										
										
											2021-10-06 22:04:50 +02:00
										 |  |  | #include "module-protocol-pulse/format.h"
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-24 14:06:30 +10:00
										 |  |  | /** \page page_module_pulse_tunnel PipeWire Module: Pulse Tunnel
 | 
					
						
							| 
									
										
										
										
											2022-04-22 16:44:15 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * The pulse-tunnel module provides a source or sink that tunnels all audio to | 
					
						
							|  |  |  |  * a remote PulseAudio connection. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * It is usually used with the PulseAudio or module-protocol-pulse on the remote | 
					
						
							|  |  |  |  * end to accept the connection. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This module is usually used together with module-zeroconf-discover that will | 
					
						
							|  |  |  |  * automatically load the tunnel with the right parameters based on zeroconf | 
					
						
							|  |  |  |  * information. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ## Module Options | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2022-06-03 10:51:46 +02:00
										 |  |  |  * - `tunnel.mode`: the desired tunnel to create, must be `source` or `sink`. | 
					
						
							|  |  |  |  *                  (Default `sink`) | 
					
						
							| 
									
										
										
										
											2022-04-22 16:44:15 +02:00
										 |  |  |  * - `pulse.server.address`: the address of the PulseAudio server to tunnel to. | 
					
						
							|  |  |  |  * - `pulse.latency`: the latency to end-to-end latency in milliseconds to | 
					
						
							| 
									
										
										
										
											2022-11-17 19:04:46 +01:00
										 |  |  |  *                    maintain (Default 200). | 
					
						
							| 
									
										
										
										
											2022-04-22 16:44:15 +02:00
										 |  |  |  * - `stream.props`: Extra properties for the local stream. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ## General options | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Options with well-known behavior. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * - \ref PW_KEY_REMOTE_NAME | 
					
						
							| 
									
										
										
										
											2022-06-23 12:48:45 +02:00
										 |  |  |  * - \ref PW_KEY_AUDIO_FORMAT | 
					
						
							| 
									
										
										
										
											2022-04-22 16:44:15 +02:00
										 |  |  |  * - \ref PW_KEY_AUDIO_RATE | 
					
						
							|  |  |  |  * - \ref PW_KEY_AUDIO_CHANNELS | 
					
						
							|  |  |  |  * - \ref SPA_KEY_AUDIO_POSITION | 
					
						
							|  |  |  |  * - \ref PW_KEY_NODE_LATENCY | 
					
						
							|  |  |  |  * - \ref PW_KEY_NODE_NAME | 
					
						
							|  |  |  |  * - \ref PW_KEY_NODE_DESCRIPTION | 
					
						
							|  |  |  |  * - \ref PW_KEY_NODE_GROUP | 
					
						
							|  |  |  |  * - \ref PW_KEY_NODE_VIRTUAL | 
					
						
							|  |  |  |  * - \ref PW_KEY_MEDIA_CLASS | 
					
						
							| 
									
										
										
										
											2022-07-11 13:52:35 +02:00
										 |  |  |  * - \ref PW_KEY_TARGET_OBJECT to specify the remote node.name or serial.id to link to | 
					
						
							| 
									
										
										
										
											2022-04-22 16:44:15 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * ## Example configuration of a virtual sink | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *\code{.unparsed} | 
					
						
							|  |  |  |  * context.modules = [ | 
					
						
							|  |  |  |  * {   name = libpipewire-module-pulse-tunnel | 
					
						
							|  |  |  |  *     args = { | 
					
						
							| 
									
										
										
										
											2022-06-03 10:51:46 +02:00
										 |  |  |  *         tunnel.mode = sink | 
					
						
							| 
									
										
										
										
											2022-04-22 16:44:15 +02:00
										 |  |  |  *         # Set the remote address to tunnel to | 
					
						
							|  |  |  |  *         pulse.server.address = "tcp:192.168.1.126" | 
					
						
							| 
									
										
										
										
											2022-11-17 19:04:46 +01:00
										 |  |  |  *         #pulse.latency = 200 | 
					
						
							| 
									
										
										
										
											2022-04-22 16:44:15 +02:00
										 |  |  |  *         #audio.rate=<sample rate> | 
					
						
							|  |  |  |  *         #audio.channels=<number of channels> | 
					
						
							|  |  |  |  *         #audio.position=<channel map> | 
					
						
							| 
									
										
										
										
											2022-07-11 13:52:35 +02:00
										 |  |  |  *         #target.object=<remote target name> | 
					
						
							| 
									
										
										
										
											2022-04-22 16:44:15 +02:00
										 |  |  |  *         stream.props = { | 
					
						
							|  |  |  |  *             # extra sink properties | 
					
						
							|  |  |  |  *         } | 
					
						
							|  |  |  |  *     } | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  * ] | 
					
						
							|  |  |  |  *\endcode | 
					
						
							| 
									
										
										
										
											2021-06-24 14:06:30 +10:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | #define NAME "pulse-tunnel"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-16 15:35:10 +10:00
										 |  |  | PW_LOG_TOPIC_STATIC(mod_topic, "mod." NAME); | 
					
						
							|  |  |  | #define PW_LOG_TOPIC_DEFAULT mod_topic
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-23 12:39:51 +02:00
										 |  |  | #define DEFAULT_FORMAT "S16"
 | 
					
						
							|  |  |  | #define DEFAULT_RATE 48000
 | 
					
						
							|  |  |  | #define DEFAULT_CHANNELS 2
 | 
					
						
							|  |  |  | #define DEFAULT_POSITION "[ FL FR ]"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 16:35:55 +01:00
										 |  |  | #define MODULE_USAGE	"( remote.name=<remote> ] "				\
 | 
					
						
							|  |  |  | 			"( node.latency=<latency as fraction> ] "		\ | 
					
						
							|  |  |  | 			"( node.name=<name of the nodes> ] "			\ | 
					
						
							|  |  |  | 			"( node.description=<description of the nodes> ] "	\ | 
					
						
							|  |  |  | 			"( node.target=<remote node target name or serial> ] "	\ | 
					
						
							|  |  |  | 			"( audio.format=<sample format> ] "			\ | 
					
						
							|  |  |  | 			"( audio.rate=<sample rate> ] "				\ | 
					
						
							|  |  |  | 			"( audio.channels=<number of channels> ] "		\ | 
					
						
							|  |  |  | 			"( audio.position=<channel map> ] "			\ | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 			"pulse.server.address=<address> "			\ | 
					
						
							| 
									
										
										
										
											2023-03-22 16:35:55 +01:00
										 |  |  | 			"( pulse.latency=<latency in msec, default 200> ) "	\ | 
					
						
							|  |  |  | 			"( tunnel.mode=source|sink, default sink ) "				\ | 
					
						
							|  |  |  | 			"( stream.props=<properties> ) " | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const struct spa_dict_item module_props[] = { | 
					
						
							|  |  |  | 	{ PW_KEY_MODULE_AUTHOR, "Wim Taymans <wim.taymans@gmail.com>" }, | 
					
						
							|  |  |  | 	{ PW_KEY_MODULE_DESCRIPTION, "Create a PulseAudio tunnel" }, | 
					
						
							|  |  |  | 	{ PW_KEY_MODULE_USAGE, MODULE_USAGE }, | 
					
						
							|  |  |  | 	{ PW_KEY_MODULE_VERSION, PACKAGE_VERSION }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define RINGBUFFER_SIZE		(1u << 22)
 | 
					
						
							|  |  |  | #define RINGBUFFER_MASK		(RINGBUFFER_SIZE-1)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | #define DEFAULT_LATENCY_MSEC	(200)
 | 
					
						
							| 
									
										
										
										
											2021-05-28 11:19:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | struct impl { | 
					
						
							|  |  |  | 	struct pw_context *context; | 
					
						
							| 
									
										
										
										
											2023-03-27 11:08:08 +02:00
										 |  |  | 	struct pw_loop *main_loop; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 10:51:46 +02:00
										 |  |  | #define MODE_SINK	0
 | 
					
						
							|  |  |  | #define MODE_SOURCE	1
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	uint32_t mode; | 
					
						
							|  |  |  | 	struct pw_properties *props; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct pw_impl_module *module; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct spa_hook module_listener; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct pw_core *core; | 
					
						
							|  |  |  | 	struct spa_hook core_proxy_listener; | 
					
						
							|  |  |  | 	struct spa_hook core_listener; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 09:35:49 +02:00
										 |  |  | 	uint32_t latency_msec; | 
					
						
							| 
									
										
										
										
											2021-09-23 13:11:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	struct pw_properties *stream_props; | 
					
						
							|  |  |  | 	struct pw_stream *stream; | 
					
						
							|  |  |  | 	struct spa_hook stream_listener; | 
					
						
							|  |  |  | 	struct spa_audio_info_raw info; | 
					
						
							|  |  |  | 	uint32_t frame_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct spa_ringbuffer ring; | 
					
						
							|  |  |  | 	void *buffer; | 
					
						
							|  |  |  | 	uint8_t empty[8192]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 17:53:23 +02:00
										 |  |  | 	bool mute; | 
					
						
							|  |  |  | 	pa_cvolume volume; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	pa_threaded_mainloop *pa_mainloop; | 
					
						
							|  |  |  | 	pa_context *pa_context; | 
					
						
							|  |  |  | 	pa_stream *pa_stream; | 
					
						
							| 
									
										
										
										
											2023-04-17 17:53:23 +02:00
										 |  |  | 	uint32_t pa_index; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 19:25:13 +02:00
										 |  |  | 	struct spa_ratelimit rate_limit; | 
					
						
							| 
									
										
										
										
											2022-10-20 09:24:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	uint32_t target_latency; | 
					
						
							|  |  |  | 	uint32_t current_latency; | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 	uint32_t target_buffer; | 
					
						
							| 
									
										
										
										
											2022-12-08 11:01:24 +01:00
										 |  |  | 	struct spa_io_rate_match *rate_match; | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 	struct spa_dll dll; | 
					
						
							|  |  |  | 	float max_error; | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	unsigned resync:1; | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	unsigned int do_disconnect:1; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void cork_stream(struct impl *impl, bool cork) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	pa_operation *operation; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pa_threaded_mainloop_lock(impl->pa_mainloop); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-19 09:58:18 +02:00
										 |  |  | 	pw_log_debug("corking: %d", cork); | 
					
						
							| 
									
										
										
										
											2022-06-03 10:51:46 +02:00
										 |  |  | 	if (cork && impl->mode == MODE_SINK) { | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		/* When the sink becomes suspended (which is the only case where we
 | 
					
						
							|  |  |  | 		 * cork the stream), we don't want to keep any old data around, because | 
					
						
							|  |  |  | 		 * the old data is most likely unrelated to the audio that will be | 
					
						
							|  |  |  | 		 * played at the time when the sink starts running again. */ | 
					
						
							|  |  |  | 		if ((operation = pa_stream_flush(impl->pa_stream, NULL, NULL))) | 
					
						
							|  |  |  | 			pa_operation_unref(operation); | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		spa_ringbuffer_init(&impl->ring); | 
					
						
							| 
									
										
										
										
											2022-04-19 10:51:45 +02:00
										 |  |  | 		memset(impl->buffer, 0, RINGBUFFER_SIZE); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	if (!cork) | 
					
						
							|  |  |  | 		impl->resync = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	if ((operation = pa_stream_cork(impl->pa_stream, cork, NULL, NULL))) | 
					
						
							|  |  |  | 		pa_operation_unref(operation); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pa_threaded_mainloop_unlock(impl->pa_mainloop); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void stream_destroy(void *d) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = d; | 
					
						
							|  |  |  | 	spa_hook_remove(&impl->stream_listener); | 
					
						
							|  |  |  | 	impl->stream = NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void stream_state_changed(void *d, enum pw_stream_state old, | 
					
						
							|  |  |  | 		enum pw_stream_state state, const char *error) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = d; | 
					
						
							|  |  |  | 	switch (state) { | 
					
						
							|  |  |  | 	case PW_STREAM_STATE_ERROR: | 
					
						
							|  |  |  | 	case PW_STREAM_STATE_UNCONNECTED: | 
					
						
							| 
									
										
										
										
											2023-05-04 20:28:10 +02:00
										 |  |  | 		if (impl->module) | 
					
						
							|  |  |  | 			pw_impl_module_schedule_destroy(impl->module); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	case PW_STREAM_STATE_PAUSED: | 
					
						
							|  |  |  | 		cork_stream(impl, true); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case PW_STREAM_STATE_STREAMING: | 
					
						
							|  |  |  | 		cork_stream(impl, false); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 17:53:23 +02:00
										 |  |  | static void stream_param_changed(void *d, uint32_t id, const struct spa_pod *param) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = d; | 
					
						
							|  |  |  | 	char buf[1024]; | 
					
						
							|  |  |  | 	struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buf, sizeof(buf)); | 
					
						
							|  |  |  | 	struct spa_pod_frame f[1]; | 
					
						
							|  |  |  | 	struct spa_pod_object *obj = (struct spa_pod_object *) param; | 
					
						
							|  |  |  | 	struct spa_pod_prop *prop; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (param == NULL || id != SPA_PARAM_Props) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_push_object(&b, &f[0], SPA_TYPE_OBJECT_Props, SPA_PARAM_Props); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	SPA_POD_OBJECT_FOREACH(obj, prop) { | 
					
						
							|  |  |  | 		switch (prop->key) { | 
					
						
							|  |  |  | 		case SPA_PROP_mute: | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			bool mute; | 
					
						
							|  |  |  | 			if (spa_pod_get_bool(&prop->value, &mute) == 0) { | 
					
						
							|  |  |  | 				pa_threaded_mainloop_lock(impl->pa_mainloop); | 
					
						
							|  |  |  | 				if (impl->mode == MODE_SOURCE) { | 
					
						
							|  |  |  | 					pa_context_set_source_output_mute(impl->pa_context, | 
					
						
							|  |  |  | 							impl->pa_index, mute, | 
					
						
							|  |  |  | 							NULL, impl); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					pa_context_set_sink_input_mute(impl->pa_context, | 
					
						
							|  |  |  | 							impl->pa_index, mute, | 
					
						
							|  |  |  | 							NULL, impl); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				pa_threaded_mainloop_unlock(impl->pa_mainloop); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		case SPA_PROP_channelVolumes: | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			struct pa_cvolume volume; | 
					
						
							|  |  |  | 			uint32_t n; | 
					
						
							|  |  |  | 			float vols[SPA_AUDIO_MAX_CHANNELS]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if ((n = spa_pod_copy_array(&prop->value, SPA_TYPE_Float, | 
					
						
							|  |  |  | 					vols, SPA_AUDIO_MAX_CHANNELS)) > 0) { | 
					
						
							|  |  |  | 				volume.channels = n; | 
					
						
							|  |  |  | 				for (n = 0; n < volume.channels; n++) | 
					
						
							|  |  |  | 					volume.values[n] = pa_sw_volume_from_linear(vols[n]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				pa_threaded_mainloop_lock(impl->pa_mainloop); | 
					
						
							|  |  |  | 				if (impl->mode == MODE_SOURCE) { | 
					
						
							|  |  |  | 					pa_context_set_source_output_volume(impl->pa_context, | 
					
						
							|  |  |  | 							impl->pa_index, &volume, | 
					
						
							|  |  |  | 							NULL, impl); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					pa_context_set_sink_input_volume(impl->pa_context, | 
					
						
							|  |  |  | 							impl->pa_index, &volume, | 
					
						
							|  |  |  | 							NULL, impl); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				pa_threaded_mainloop_unlock(impl->pa_mainloop); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		case SPA_PROP_softVolumes: | 
					
						
							|  |  |  | 		case SPA_PROP_softMute: | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			spa_pod_builder_raw_padded(&b, prop, SPA_POD_PROP_SIZE(prop)); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	param = spa_pod_builder_pop(&b, &f[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pw_stream_set_param(impl->stream, id, param); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-16 11:20:31 +01:00
										 |  |  | static void update_rate(struct impl *impl, uint32_t filled) | 
					
						
							| 
									
										
										
										
											2022-12-08 11:01:24 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	float error, corr; | 
					
						
							| 
									
										
										
										
											2023-03-16 11:20:31 +01:00
										 |  |  | 	uint32_t current_latency; | 
					
						
							| 
									
										
										
										
											2022-12-08 11:01:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (impl->rate_match == NULL) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-16 11:20:31 +01:00
										 |  |  | 	current_latency = impl->current_latency + filled; | 
					
						
							|  |  |  | 	error = (float)impl->target_latency - (float)(current_latency); | 
					
						
							| 
									
										
										
										
											2022-12-08 11:01:24 +01:00
										 |  |  | 	error = SPA_CLAMP(error, -impl->max_error, impl->max_error); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	corr = spa_dll_update(&impl->dll, error); | 
					
						
							|  |  |  | 	pw_log_debug("error:%f corr:%f current:%u target:%u", | 
					
						
							|  |  |  | 			error, corr, | 
					
						
							| 
									
										
										
										
											2023-03-16 11:20:31 +01:00
										 |  |  | 			current_latency, impl->target_latency); | 
					
						
							| 
									
										
										
										
											2022-12-08 11:01:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	SPA_FLAG_SET(impl->rate_match->flags, SPA_IO_RATE_MATCH_FLAG_ACTIVE); | 
					
						
							| 
									
										
										
										
											2022-12-29 12:22:56 +01:00
										 |  |  | 	impl->rate_match->rate = 1.0f / corr; | 
					
						
							| 
									
										
										
										
											2022-12-08 11:01:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | static void playback_stream_process(void *d) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = d; | 
					
						
							|  |  |  | 	struct pw_buffer *buf; | 
					
						
							|  |  |  | 	struct spa_data *bd; | 
					
						
							|  |  |  | 	int32_t filled; | 
					
						
							| 
									
										
										
										
											2022-06-04 11:47:48 +02:00
										 |  |  | 	uint32_t write_index, offs, size; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ((buf = pw_stream_dequeue_buffer(impl->stream)) == NULL) { | 
					
						
							| 
									
										
										
										
											2021-07-19 18:30:22 +02:00
										 |  |  | 		pw_log_debug("out of buffers: %m"); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bd = &buf->buffer->datas[0]; | 
					
						
							| 
									
										
										
										
											2022-06-04 11:47:48 +02:00
										 |  |  | 	offs = SPA_MIN(bd->chunk->offset, bd->maxsize); | 
					
						
							|  |  |  | 	size = SPA_MIN(bd->chunk->size, bd->maxsize - offs); | 
					
						
							|  |  |  | 	size = SPA_MIN(size, RINGBUFFER_SIZE); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	filled = spa_ringbuffer_get_write_index(&impl->ring, &write_index); | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	if (filled < 0) { | 
					
						
							| 
									
										
										
										
											2021-09-16 15:35:10 +10:00
										 |  |  | 		pw_log_warn("%p: underrun write:%u filled:%d", | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 				impl, write_index, filled); | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	} else if ((uint32_t)filled + size > RINGBUFFER_SIZE) { | 
					
						
							|  |  |  | 		pw_log_warn("%p: overrun write:%u filled:%d + size:%u > max:%u", | 
					
						
							|  |  |  |                                         impl, write_index, filled, | 
					
						
							|  |  |  |                                         size, RINGBUFFER_SIZE); | 
					
						
							|  |  |  | 		impl->resync = true; | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2023-03-16 11:20:31 +01:00
										 |  |  | 		update_rate(impl, filled / impl->frame_size); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	spa_ringbuffer_write_data(&impl->ring, | 
					
						
							|  |  |  | 				impl->buffer, RINGBUFFER_SIZE, | 
					
						
							| 
									
										
										
										
											2022-06-04 11:47:48 +02:00
										 |  |  | 				write_index & RINGBUFFER_MASK, | 
					
						
							|  |  |  | 				SPA_PTROFF(bd->data, offs, void), | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 				size); | 
					
						
							|  |  |  | 	write_index += size; | 
					
						
							|  |  |  | 	spa_ringbuffer_write_update(&impl->ring, write_index); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pw_stream_queue_buffer(impl->stream, buf); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void capture_stream_process(void *d) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = d; | 
					
						
							|  |  |  | 	struct pw_buffer *buf; | 
					
						
							|  |  |  | 	struct spa_data *bd; | 
					
						
							|  |  |  | 	int32_t avail; | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	uint32_t size, req, index; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ((buf = pw_stream_dequeue_buffer(impl->stream)) == NULL) { | 
					
						
							| 
									
										
										
										
											2021-07-19 18:30:22 +02:00
										 |  |  | 		pw_log_debug("out of buffers: %m"); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bd = &buf->buffer->datas[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 	if ((req = buf->requested * impl->frame_size) == 0) | 
					
						
							|  |  |  | 		req = 4096 * impl->frame_size; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	size = SPA_MIN(bd->maxsize, req); | 
					
						
							| 
									
										
										
										
											2023-08-30 18:27:19 +02:00
										 |  |  | 	size = SPA_ROUND_DOWN(size, impl->frame_size); | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	avail = spa_ringbuffer_get_read_index(&impl->ring, &index); | 
					
						
							| 
									
										
										
										
											2023-08-30 18:27:19 +02:00
										 |  |  | 	if (avail < (int32_t)size) | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		memset(bd->data, 0, size); | 
					
						
							| 
									
										
										
										
											2023-08-30 18:27:19 +02:00
										 |  |  | 	if (avail > (int32_t)RINGBUFFER_SIZE) { | 
					
						
							|  |  |  | 		index += avail - impl->target_buffer; | 
					
						
							| 
									
										
										
										
											2023-08-31 11:39:54 +02:00
										 |  |  | 		avail = impl->target_buffer; | 
					
						
							| 
									
										
										
										
											2023-08-30 18:27:19 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (avail > 0) { | 
					
						
							|  |  |  | 		avail = SPA_ROUND_DOWN(avail, impl->frame_size); | 
					
						
							|  |  |  | 		update_rate(impl, avail / impl->frame_size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		avail = SPA_MIN(size, (uint32_t)avail); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		spa_ringbuffer_read_data(&impl->ring, | 
					
						
							|  |  |  | 				impl->buffer, RINGBUFFER_SIZE, | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 				index & RINGBUFFER_MASK, | 
					
						
							| 
									
										
										
										
											2023-08-30 18:27:19 +02:00
										 |  |  | 				bd->data, avail); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-30 18:27:19 +02:00
										 |  |  | 		index += avail; | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 		spa_ringbuffer_read_update(&impl->ring, index); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	bd->chunk->offset = 0; | 
					
						
							|  |  |  | 	bd->chunk->size = size; | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	bd->chunk->stride = impl->frame_size; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	pw_stream_queue_buffer(impl->stream, buf); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-08 11:01:24 +01:00
										 |  |  | static void stream_io_changed(void *data, uint32_t id, void *area, uint32_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = data; | 
					
						
							|  |  |  | 	switch (id) { | 
					
						
							|  |  |  | 	case SPA_IO_RateMatch: | 
					
						
							|  |  |  | 		impl->rate_match = area; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | static const struct pw_stream_events playback_stream_events = { | 
					
						
							|  |  |  | 	PW_VERSION_STREAM_EVENTS, | 
					
						
							|  |  |  | 	.destroy = stream_destroy, | 
					
						
							|  |  |  | 	.state_changed = stream_state_changed, | 
					
						
							| 
									
										
										
										
											2022-12-08 11:01:24 +01:00
										 |  |  | 	.io_changed = stream_io_changed, | 
					
						
							| 
									
										
										
										
											2023-04-17 17:53:23 +02:00
										 |  |  | 	.param_changed = stream_param_changed, | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	.process = playback_stream_process | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const struct pw_stream_events capture_stream_events = { | 
					
						
							|  |  |  | 	PW_VERSION_STREAM_EVENTS, | 
					
						
							|  |  |  | 	.destroy = stream_destroy, | 
					
						
							|  |  |  | 	.state_changed = stream_state_changed, | 
					
						
							| 
									
										
										
										
											2022-12-08 11:01:24 +01:00
										 |  |  | 	.io_changed = stream_io_changed, | 
					
						
							| 
									
										
										
										
											2023-04-17 17:53:23 +02:00
										 |  |  | 	.param_changed = stream_param_changed, | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	.process = capture_stream_process | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int create_stream(struct impl *impl) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	uint32_t n_params; | 
					
						
							| 
									
										
										
										
											2021-05-28 11:19:05 +02:00
										 |  |  | 	const struct spa_pod *params[2]; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	uint8_t buffer[1024]; | 
					
						
							|  |  |  | 	struct spa_pod_builder b; | 
					
						
							| 
									
										
										
										
											2021-05-28 11:19:05 +02:00
										 |  |  | 	struct spa_latency_info latency; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	impl->stream = pw_stream_new(impl->core, "pulse", impl->stream_props); | 
					
						
							|  |  |  | 	impl->stream_props = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (impl->stream == NULL) | 
					
						
							|  |  |  | 		return -errno; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 10:51:46 +02:00
										 |  |  | 	if (impl->mode == MODE_SOURCE) { | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		pw_stream_add_listener(impl->stream, | 
					
						
							|  |  |  | 				&impl->stream_listener, | 
					
						
							|  |  |  | 				&capture_stream_events, impl); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		pw_stream_add_listener(impl->stream, | 
					
						
							|  |  |  | 				&impl->stream_listener, | 
					
						
							|  |  |  | 				&playback_stream_events, impl); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	n_params = 0; | 
					
						
							|  |  |  | 	spa_pod_builder_init(&b, buffer, sizeof(buffer)); | 
					
						
							|  |  |  | 	params[n_params++] = spa_format_audio_raw_build(&b, | 
					
						
							|  |  |  | 			SPA_PARAM_EnumFormat, &impl->info); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-28 11:19:05 +02:00
										 |  |  | 	spa_zero(latency); | 
					
						
							| 
									
										
										
										
											2022-06-03 10:51:46 +02:00
										 |  |  | 	latency.direction = impl->mode == MODE_SOURCE ? PW_DIRECTION_OUTPUT : PW_DIRECTION_INPUT; | 
					
						
							| 
									
										
										
										
											2021-09-30 09:35:49 +02:00
										 |  |  | 	latency.min_ns = latency.max_ns = impl->latency_msec * SPA_NSEC_PER_MSEC; | 
					
						
							| 
									
										
										
										
											2021-05-28 11:19:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	params[n_params++] = spa_latency_build(&b, | 
					
						
							|  |  |  | 			SPA_PARAM_Latency, &latency); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	if ((res = pw_stream_connect(impl->stream, | 
					
						
							| 
									
										
										
										
											2022-06-03 10:51:46 +02:00
										 |  |  | 			impl->mode == MODE_SOURCE ? PW_DIRECTION_OUTPUT : PW_DIRECTION_INPUT, | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 			PW_ID_ANY, | 
					
						
							|  |  |  | 			PW_STREAM_FLAG_AUTOCONNECT | | 
					
						
							| 
									
										
										
										
											2021-06-25 15:51:30 +02:00
										 |  |  | 			PW_STREAM_FLAG_MAP_BUFFERS | | 
					
						
							|  |  |  | 			PW_STREAM_FLAG_RT_PROCESS, | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 			params, n_params)) < 0) | 
					
						
							|  |  |  | 		return res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-27 11:08:08 +02:00
										 |  |  | static int | 
					
						
							|  |  |  | do_schedule_destroy(struct spa_loop *loop, | 
					
						
							|  |  |  | 	bool async, uint32_t seq, const void *data, size_t size, void *user_data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = user_data; | 
					
						
							| 
									
										
										
										
											2023-05-04 20:28:10 +02:00
										 |  |  | 	if (impl->module) | 
					
						
							|  |  |  | 		pw_impl_module_schedule_destroy(impl->module); | 
					
						
							| 
									
										
										
										
											2023-03-27 11:08:08 +02:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-03 04:34:59 +02:00
										 |  |  | static void module_schedule_destroy(struct impl *impl) | 
					
						
							| 
									
										
										
										
											2023-03-27 11:08:08 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	pw_loop_invoke(impl->main_loop, do_schedule_destroy, 1, NULL, 0, false, impl); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | static void context_state_cb(pa_context *c, void *userdata) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = userdata; | 
					
						
							| 
									
										
										
										
											2022-08-30 16:09:04 +02:00
										 |  |  | 	bool do_destroy = false; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	switch (pa_context_get_state(c)) { | 
					
						
							|  |  |  | 	case PA_CONTEXT_TERMINATED: | 
					
						
							|  |  |  | 	case PA_CONTEXT_FAILED: | 
					
						
							| 
									
										
										
										
											2022-08-30 16:09:04 +02:00
										 |  |  | 		do_destroy = true; | 
					
						
							|  |  |  | 		SPA_FALLTHROUGH; | 
					
						
							|  |  |  | 	case PA_CONTEXT_READY: | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		pa_threaded_mainloop_signal(impl->pa_mainloop, 0); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case PA_CONTEXT_UNCONNECTED: | 
					
						
							| 
									
										
										
										
											2022-08-30 16:09:04 +02:00
										 |  |  | 		do_destroy = true; | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	case PA_CONTEXT_CONNECTING: | 
					
						
							|  |  |  | 	case PA_CONTEXT_AUTHORIZING: | 
					
						
							|  |  |  | 	case PA_CONTEXT_SETTING_NAME: | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-08-30 16:09:04 +02:00
										 |  |  | 	if (do_destroy) | 
					
						
							| 
									
										
										
										
											2023-03-27 11:08:08 +02:00
										 |  |  | 		module_schedule_destroy(impl); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void stream_state_cb(pa_stream *s, void * userdata) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = userdata; | 
					
						
							| 
									
										
										
										
											2022-08-30 16:09:04 +02:00
										 |  |  | 	bool do_destroy = false; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	switch (pa_stream_get_state(s)) { | 
					
						
							|  |  |  | 	case PA_STREAM_FAILED: | 
					
						
							|  |  |  | 	case PA_STREAM_TERMINATED: | 
					
						
							| 
									
										
										
										
											2022-08-30 16:09:04 +02:00
										 |  |  | 		do_destroy = true; | 
					
						
							|  |  |  | 		SPA_FALLTHROUGH; | 
					
						
							|  |  |  | 	case PA_STREAM_READY: | 
					
						
							| 
									
										
										
										
											2023-04-17 17:53:23 +02:00
										 |  |  | 		impl->pa_index = pa_stream_get_index(impl->pa_stream); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		pa_threaded_mainloop_signal(impl->pa_mainloop, 0); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case PA_STREAM_UNCONNECTED: | 
					
						
							| 
									
										
										
										
											2022-08-30 16:09:04 +02:00
										 |  |  | 		do_destroy = true; | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	case PA_STREAM_CREATING: | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-08-30 16:09:04 +02:00
										 |  |  | 	if (do_destroy) | 
					
						
							| 
									
										
										
										
											2023-03-27 11:08:08 +02:00
										 |  |  | 		module_schedule_destroy(impl); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void stream_read_request_cb(pa_stream *s, size_t length, void *userdata) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = userdata; | 
					
						
							|  |  |  | 	int32_t filled; | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	uint32_t index; | 
					
						
							|  |  |  | 	pa_usec_t latency; | 
					
						
							|  |  |  | 	int negative; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	filled = spa_ringbuffer_get_write_index(&impl->ring, &index); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (filled < 0) { | 
					
						
							| 
									
										
										
										
											2021-09-16 15:35:10 +10:00
										 |  |  | 		pw_log_warn("%p: underrun write:%u filled:%d", | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 				impl, index, filled); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	} else if (filled + length > RINGBUFFER_SIZE) { | 
					
						
							| 
									
										
										
										
											2021-09-16 15:35:10 +10:00
										 |  |  | 		pw_log_warn("%p: overrun write:%u filled:%d", | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 				impl, index, filled); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	while (length > 0) { | 
					
						
							|  |  |  | 		const void *p; | 
					
						
							|  |  |  | 		size_t nbytes = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-13 14:52:58 +02:00
										 |  |  | 		if (SPA_UNLIKELY(pa_stream_peek(impl->pa_stream, &p, &nbytes) != 0)) { | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 			pw_log_error("pa_stream_peek() failed: %s", | 
					
						
							|  |  |  | 					pa_strerror(pa_context_errno(impl->pa_context))); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		pw_log_debug("read %zd nbytes:%zd", length, nbytes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (length < nbytes) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 09:51:51 +02:00
										 |  |  | 		while (nbytes > 0) { | 
					
						
							|  |  |  | 			uint32_t to_write = SPA_MIN(nbytes, sizeof(impl->empty)); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 09:51:51 +02:00
										 |  |  | 			spa_ringbuffer_write_data(&impl->ring, | 
					
						
							|  |  |  | 					impl->buffer, RINGBUFFER_SIZE, | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 					index & RINGBUFFER_MASK, | 
					
						
							| 
									
										
										
										
											2021-05-14 09:51:51 +02:00
										 |  |  | 					p ? p : impl->empty, to_write); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 			index += to_write; | 
					
						
							| 
									
										
										
										
											2021-05-14 09:51:51 +02:00
										 |  |  | 			p = p ? SPA_PTROFF(p, to_write, void) : NULL; | 
					
						
							|  |  |  | 			nbytes -= to_write; | 
					
						
							|  |  |  | 			length -= to_write; | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 			filled += to_write; | 
					
						
							| 
									
										
										
										
											2021-05-14 09:51:51 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		pa_stream_drop(impl->pa_stream); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	pa_stream_get_latency(impl->pa_stream, &latency, &negative); | 
					
						
							|  |  |  | 	impl->current_latency = latency * impl->info.rate / SPA_USEC_PER_SEC; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_ringbuffer_write_update(&impl->ring, index); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void stream_write_request_cb(pa_stream *s, size_t length, void *userdata) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = userdata; | 
					
						
							|  |  |  | 	int32_t avail; | 
					
						
							| 
									
										
										
										
											2022-10-18 13:14:32 +02:00
										 |  |  | 	uint32_t index; | 
					
						
							|  |  |  | 	size_t size; | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	pa_usec_t latency; | 
					
						
							| 
									
										
										
										
											2022-10-18 13:14:32 +02:00
										 |  |  | 	int negative, res; | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (impl->resync) { | 
					
						
							|  |  |  | 		impl->resync = false; | 
					
						
							|  |  |  | 		avail = length + impl->target_buffer; | 
					
						
							|  |  |  | 		spa_ringbuffer_get_write_index(&impl->ring, &index); | 
					
						
							|  |  |  | 		index -= avail; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		avail = spa_ringbuffer_get_read_index(&impl->ring, &index); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	pa_stream_get_latency(impl->pa_stream, &latency, &negative); | 
					
						
							|  |  |  | 	impl->current_latency = latency * impl->info.rate / SPA_USEC_PER_SEC; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	while (avail < (int32_t)length) { | 
					
						
							| 
									
										
										
										
											2022-10-18 13:14:32 +02:00
										 |  |  | 		uint32_t maxsize = SPA_ROUND_DOWN(sizeof(impl->empty), impl->frame_size); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		/* send silence for the data we don't have */ | 
					
						
							| 
									
										
										
										
											2022-10-18 13:14:32 +02:00
										 |  |  | 		size = SPA_MIN(length - avail, maxsize); | 
					
						
							|  |  |  | 		if ((res = pa_stream_write(impl->pa_stream, | 
					
						
							|  |  |  | 				impl->empty, size, | 
					
						
							|  |  |  | 				NULL, 0, PA_SEEK_RELATIVE)) != 0) | 
					
						
							|  |  |  | 			pw_log_warn("error writing stream: %s", pa_strerror(res)); | 
					
						
							|  |  |  | 		length -= size; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-10-18 13:14:32 +02:00
										 |  |  | 	while (length > 0 && avail >= (int32_t)length) { | 
					
						
							|  |  |  | 		void *data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		size = length; | 
					
						
							|  |  |  | 		pa_stream_begin_write(impl->pa_stream, &data, &size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		spa_ringbuffer_read_data(&impl->ring, | 
					
						
							|  |  |  | 				impl->buffer, RINGBUFFER_SIZE, | 
					
						
							|  |  |  | 				index & RINGBUFFER_MASK, | 
					
						
							|  |  |  | 				data, size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if ((res = pa_stream_write(impl->pa_stream, | 
					
						
							|  |  |  | 			data, size, NULL, 0, PA_SEEK_RELATIVE)) != 0) | 
					
						
							|  |  |  | 			pw_log_warn("error writing stream: %zd %s", size, | 
					
						
							|  |  |  | 					pa_strerror(res)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		index += size; | 
					
						
							|  |  |  | 		length -= size; | 
					
						
							|  |  |  | 		avail -= size; | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 		spa_ringbuffer_read_update(&impl->ring, index); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | static void stream_underflow_cb(pa_stream *s, void *userdata) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = userdata; | 
					
						
							| 
									
										
										
										
											2022-10-20 09:24:54 +02:00
										 |  |  | 	struct timespec ts; | 
					
						
							| 
									
										
										
										
											2023-09-08 12:18:23 +02:00
										 |  |  | 	int suppressed; | 
					
						
							| 
									
										
										
										
											2022-10-20 09:24:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	clock_gettime(CLOCK_MONOTONIC, &ts); | 
					
						
							| 
									
										
										
										
											2023-09-08 12:18:23 +02:00
										 |  |  | 	if ((suppressed = spa_ratelimit_test(&impl->rate_limit, SPA_TIMESPEC_TO_NSEC(&ts))) >= 0) | 
					
						
							|  |  |  | 		pw_log_warn("underflow (%d suppressed)", suppressed); | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	impl->resync = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | static void stream_overflow_cb(pa_stream *s, void *userdata) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = userdata; | 
					
						
							| 
									
										
										
										
											2022-10-20 09:24:54 +02:00
										 |  |  | 	struct timespec ts; | 
					
						
							| 
									
										
										
										
											2023-09-08 12:18:23 +02:00
										 |  |  | 	int suppressed; | 
					
						
							| 
									
										
										
										
											2023-07-11 19:25:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-20 09:24:54 +02:00
										 |  |  | 	clock_gettime(CLOCK_MONOTONIC, &ts); | 
					
						
							| 
									
										
										
										
											2023-09-08 12:18:23 +02:00
										 |  |  | 	if ((suppressed = spa_ratelimit_test(&impl->rate_limit, SPA_TIMESPEC_TO_NSEC(&ts))) >= 0) | 
					
						
							|  |  |  | 		pw_log_warn("overflow (%d suppressed)", suppressed); | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	impl->resync = true; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void stream_latency_update_cb(pa_stream *s, void *userdata) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = userdata; | 
					
						
							| 
									
										
										
										
											2021-05-28 11:19:05 +02:00
										 |  |  | 	pa_usec_t usec; | 
					
						
							|  |  |  | 	int negative; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pa_stream_get_latency(s, &usec, &negative); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pw_log_debug("latency %ld negative %d", usec, negative); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	pa_threaded_mainloop_signal(impl->pa_mainloop, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 17:53:23 +02:00
										 |  |  | static int | 
					
						
							|  |  |  | do_stream_sync_volumes(struct spa_loop *loop, | 
					
						
							|  |  |  | 	bool async, uint32_t seq, const void *data, size_t size, void *user_data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = user_data; | 
					
						
							|  |  |  | 	char buf[1024]; | 
					
						
							|  |  |  | 	struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buf, sizeof(buf)); | 
					
						
							|  |  |  | 	struct spa_pod_frame f[1]; | 
					
						
							|  |  |  | 	struct spa_pod *param; | 
					
						
							|  |  |  | 	uint32_t i; | 
					
						
							|  |  |  | 	float vols[SPA_AUDIO_MAX_CHANNELS]; | 
					
						
							|  |  |  | 	float soft_vols[SPA_AUDIO_MAX_CHANNELS]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < impl->volume.channels; i++) { | 
					
						
							|  |  |  | 		vols[i] = pa_sw_volume_to_linear(impl->volume.values[i]); | 
					
						
							|  |  |  | 		soft_vols[i] = 1.0f; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_push_object(&b, &f[0], SPA_TYPE_OBJECT_Props, SPA_PARAM_Props); | 
					
						
							|  |  |  | 	spa_pod_builder_prop(&b, SPA_PROP_softMute, 0); | 
					
						
							|  |  |  | 	spa_pod_builder_bool(&b, impl->mute); | 
					
						
							|  |  |  | 	spa_pod_builder_prop(&b, SPA_PROP_mute, 0); | 
					
						
							|  |  |  | 	spa_pod_builder_bool(&b, impl->mute); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_pod_builder_prop(&b, SPA_PROP_channelVolumes, 0); | 
					
						
							|  |  |  | 	spa_pod_builder_array(&b, sizeof(float), SPA_TYPE_Float, | 
					
						
							|  |  |  | 			impl->volume.channels, vols); | 
					
						
							|  |  |  | 	spa_pod_builder_prop(&b, SPA_PROP_softVolumes, 0); | 
					
						
							|  |  |  | 	spa_pod_builder_array(&b, sizeof(float), SPA_TYPE_Float, | 
					
						
							|  |  |  | 			impl->volume.channels, soft_vols); | 
					
						
							|  |  |  | 	param = spa_pod_builder_pop(&b, &f[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pw_stream_set_param(impl->stream, SPA_PARAM_Props, param); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void stream_sync_volumes(struct impl *impl, const struct pa_cvolume *volume, bool mute) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	impl->mute = mute; | 
					
						
							|  |  |  | 	impl->volume = *volume; | 
					
						
							|  |  |  | 	pw_loop_invoke(impl->main_loop, do_stream_sync_volumes, 1, NULL, 0, false, impl); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void source_output_info_cb(pa_context *c, const pa_source_output_info *i, int eol, void *userdata) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = userdata; | 
					
						
							|  |  |  | 	if (i != NULL) | 
					
						
							|  |  |  | 		stream_sync_volumes(impl, &i->volume, i->mute); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void sink_input_info_cb(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = userdata; | 
					
						
							|  |  |  | 	if (i != NULL) | 
					
						
							|  |  |  | 		stream_sync_volumes(impl, &i->volume, i->mute); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void context_subscribe_cb(pa_context *c, pa_subscription_event_type_t t, | 
					
						
							|  |  |  | 		uint32_t idx, void *userdata) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = userdata; | 
					
						
							|  |  |  | 	if (idx != impl->pa_index) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (impl->mode == MODE_SOURCE) | 
					
						
							|  |  |  | 		pa_context_get_source_output_info(impl->pa_context, | 
					
						
							|  |  |  | 				idx, source_output_info_cb, impl); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		pa_context_get_sink_input_info(impl->pa_context, | 
					
						
							|  |  |  | 				idx, sink_input_info_cb, impl); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | static pa_proplist* tunnel_new_proplist(struct impl *impl) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	pa_proplist *proplist = pa_proplist_new(); | 
					
						
							|  |  |  | 	pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, "PipeWire"); | 
					
						
							|  |  |  | 	pa_proplist_sets(proplist, PA_PROP_APPLICATION_ID, "org.pipewire.PipeWire"); | 
					
						
							|  |  |  | 	pa_proplist_sets(proplist, PA_PROP_APPLICATION_VERSION, PACKAGE_VERSION); | 
					
						
							|  |  |  | 	return proplist; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int create_pulse_stream(struct impl *impl) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	pa_sample_spec ss; | 
					
						
							| 
									
										
										
										
											2022-10-17 18:58:15 +02:00
										 |  |  | 	pa_channel_map map; | 
					
						
							| 
									
										
										
										
											2021-05-12 16:49:19 +02:00
										 |  |  | 	const char *server_address, *remote_node_target; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	pa_proplist *props = NULL; | 
					
						
							|  |  |  | 	pa_mainloop_api *api; | 
					
						
							| 
									
										
										
										
											2021-05-12 16:49:19 +02:00
										 |  |  | 	char stream_name[1024]; | 
					
						
							| 
									
										
										
										
											2021-05-28 11:19:05 +02:00
										 |  |  | 	pa_buffer_attr bufferattr; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	int res = -EIO; | 
					
						
							| 
									
										
										
										
											2022-10-17 18:58:15 +02:00
										 |  |  | 	uint32_t latency_bytes, i, aux = 0; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ((impl->pa_mainloop = pa_threaded_mainloop_new()) == NULL) | 
					
						
							|  |  |  | 		goto error; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	api = pa_threaded_mainloop_get_api(impl->pa_mainloop); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	props = tunnel_new_proplist(impl); | 
					
						
							|  |  |  | 	impl->pa_context = pa_context_new_with_proplist(api, "PipeWire", props); | 
					
						
							|  |  |  | 	pa_proplist_free(props); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (impl->pa_context == NULL) | 
					
						
							|  |  |  | 		goto error; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pa_context_set_state_callback(impl->pa_context, context_state_cb, impl); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	server_address = pw_properties_get(impl->props, "pulse.server.address"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (pa_context_connect(impl->pa_context, server_address, 0, NULL) < 0) { | 
					
						
							|  |  |  | 		res = pa_context_errno(impl->pa_context); | 
					
						
							|  |  |  | 		goto error; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pa_threaded_mainloop_lock(impl->pa_mainloop); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 17:53:23 +02:00
										 |  |  | 	pa_context_set_subscribe_callback(impl->pa_context, context_subscribe_cb, impl); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	if (pa_threaded_mainloop_start(impl->pa_mainloop) < 0) | 
					
						
							|  |  |  | 		goto error_unlock; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (;;) { | 
					
						
							|  |  |  | 		pa_context_state_t state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		state = pa_context_get_state(impl->pa_context); | 
					
						
							|  |  |  | 		if (state == PA_CONTEXT_READY) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!PA_CONTEXT_IS_GOOD(state)) { | 
					
						
							|  |  |  | 			res = pa_context_errno(impl->pa_context); | 
					
						
							|  |  |  | 			goto error_unlock; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		/* Wait until the context is ready */ | 
					
						
							|  |  |  | 		pa_threaded_mainloop_wait(impl->pa_mainloop); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 22:04:50 +02:00
										 |  |  | 	ss.format = (pa_sample_format_t) format_id2pa(impl->info.format); | 
					
						
							|  |  |  | 	ss.channels = impl->info.channels; | 
					
						
							|  |  |  | 	ss.rate = impl->info.rate; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-17 18:58:15 +02:00
										 |  |  | 	map.channels = impl->info.channels; | 
					
						
							|  |  |  | 	for (i = 0; i < map.channels; i++) | 
					
						
							|  |  |  | 		map.map[i] = (pa_channel_position_t)channel_id2pa(impl->info.position[i], &aux); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 16:49:19 +02:00
										 |  |  | 	snprintf(stream_name, sizeof(stream_name), _("Tunnel for %s@%s"), | 
					
						
							|  |  |  | 			pw_get_user_name(), pw_get_host_name()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-17 18:58:15 +02:00
										 |  |  | 	if (!(impl->pa_stream = pa_stream_new(impl->pa_context, stream_name, &ss, &map))) { | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		res = pa_context_errno(impl->pa_context); | 
					
						
							|  |  |  | 		goto error_unlock; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pa_stream_set_state_callback(impl->pa_stream, stream_state_cb, impl); | 
					
						
							|  |  |  | 	pa_stream_set_read_callback(impl->pa_stream, stream_read_request_cb, impl); | 
					
						
							|  |  |  | 	pa_stream_set_write_callback(impl->pa_stream, stream_write_request_cb, impl); | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	pa_stream_set_underflow_callback(impl->pa_stream, stream_underflow_cb, impl); | 
					
						
							|  |  |  | 	pa_stream_set_overflow_callback(impl->pa_stream, stream_overflow_cb, impl); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	pa_stream_set_latency_update_callback(impl->pa_stream, stream_latency_update_cb, impl); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-11 13:52:35 +02:00
										 |  |  | 	remote_node_target = pw_properties_get(impl->props, PW_KEY_TARGET_OBJECT); | 
					
						
							| 
									
										
										
										
											2021-05-12 16:49:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-28 11:19:05 +02:00
										 |  |  | 	bufferattr.fragsize = (uint32_t) -1; | 
					
						
							|  |  |  | 	bufferattr.minreq = (uint32_t) -1; | 
					
						
							|  |  |  | 	bufferattr.maxlength = (uint32_t) -1; | 
					
						
							|  |  |  | 	bufferattr.prebuf = (uint32_t) -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 	latency_bytes = pa_usec_to_bytes(impl->latency_msec * SPA_USEC_PER_MSEC, &ss); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	impl->target_latency = latency_bytes / impl->frame_size; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 	/* half in our buffer, half in the network + remote */ | 
					
						
							|  |  |  | 	impl->target_buffer = latency_bytes / 2; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 10:51:46 +02:00
										 |  |  | 	if (impl->mode == MODE_SOURCE) { | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 		bufferattr.fragsize = latency_bytes / 2; | 
					
						
							| 
									
										
										
										
											2021-05-28 11:19:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 17:53:23 +02:00
										 |  |  | 		pa_context_subscribe(impl->pa_context, | 
					
						
							|  |  |  | 				PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT, NULL, impl); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		res = pa_stream_connect_record(impl->pa_stream, | 
					
						
							| 
									
										
										
										
											2021-05-28 11:19:05 +02:00
										 |  |  | 				remote_node_target, &bufferattr, | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 				PA_STREAM_DONT_MOVE | | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 				PA_STREAM_INTERPOLATE_TIMING | | 
					
						
							|  |  |  | 				PA_STREAM_ADJUST_LATENCY | | 
					
						
							|  |  |  | 				PA_STREAM_AUTO_TIMING_UPDATE); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 		bufferattr.tlength = latency_bytes / 2; | 
					
						
							| 
									
										
										
										
											2022-10-12 12:18:14 +02:00
										 |  |  | 		bufferattr.minreq = bufferattr.tlength / 4; | 
					
						
							|  |  |  | 		bufferattr.prebuf = bufferattr.tlength; | 
					
						
							| 
									
										
										
										
											2021-05-28 11:19:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 17:53:23 +02:00
										 |  |  | 		pa_context_subscribe(impl->pa_context, | 
					
						
							|  |  |  | 				PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, impl); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		res = pa_stream_connect_playback(impl->pa_stream, | 
					
						
							| 
									
										
										
										
											2021-05-28 11:19:05 +02:00
										 |  |  | 				remote_node_target, &bufferattr, | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 				PA_STREAM_DONT_MOVE | | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 				PA_STREAM_INTERPOLATE_TIMING | | 
					
						
							|  |  |  | 				PA_STREAM_ADJUST_LATENCY | | 
					
						
							|  |  |  | 				PA_STREAM_AUTO_TIMING_UPDATE, | 
					
						
							|  |  |  | 				NULL, NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (res < 0) { | 
					
						
							|  |  |  | 		res = pa_context_errno(impl->pa_context); | 
					
						
							|  |  |  | 		goto error_unlock; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (;;) { | 
					
						
							|  |  |  | 		pa_stream_state_t state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		state = pa_stream_get_state(impl->pa_stream); | 
					
						
							|  |  |  | 		if (state == PA_STREAM_READY) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!PA_STREAM_IS_GOOD(state)) { | 
					
						
							|  |  |  | 			res = pa_context_errno(impl->pa_context); | 
					
						
							|  |  |  | 			goto error_unlock; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* Wait until the stream is ready */ | 
					
						
							|  |  |  | 		pa_threaded_mainloop_wait(impl->pa_mainloop); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pa_threaded_mainloop_unlock(impl->pa_mainloop); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | error_unlock: | 
					
						
							|  |  |  | 	pa_threaded_mainloop_unlock(impl->pa_mainloop); | 
					
						
							|  |  |  | error: | 
					
						
							|  |  |  | 	pw_log_error("failed to connect: %s", pa_strerror(res)); | 
					
						
							| 
									
										
										
										
											2023-03-13 12:26:20 +01:00
										 |  |  | 	return err_to_res(res); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void core_error(void *data, uint32_t id, int seq, int res, const char *message) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pw_log_error("error id:%u seq:%d res:%d (%s): %s", | 
					
						
							|  |  |  | 			id, seq, res, spa_strerror(res), message); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-04 20:28:10 +02:00
										 |  |  | 	if (id == PW_ID_CORE && res == -EPIPE) { | 
					
						
							|  |  |  | 		if (impl->module) | 
					
						
							|  |  |  | 			pw_impl_module_schedule_destroy(impl->module); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const struct pw_core_events core_events = { | 
					
						
							|  |  |  | 	PW_VERSION_CORE_EVENTS, | 
					
						
							|  |  |  | 	.error = core_error, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void core_destroy(void *d) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = d; | 
					
						
							|  |  |  | 	spa_hook_remove(&impl->core_listener); | 
					
						
							|  |  |  | 	impl->core = NULL; | 
					
						
							| 
									
										
										
										
											2023-05-04 20:28:10 +02:00
										 |  |  | 	if (impl->module) | 
					
						
							|  |  |  | 		pw_impl_module_schedule_destroy(impl->module); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const struct pw_proxy_events core_proxy_events = { | 
					
						
							|  |  |  | 	.destroy = core_destroy, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void impl_destroy(struct impl *impl) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (impl->pa_mainloop) | 
					
						
							|  |  |  | 		pa_threaded_mainloop_stop(impl->pa_mainloop); | 
					
						
							|  |  |  | 	if (impl->pa_stream) | 
					
						
							|  |  |  | 		pa_stream_unref(impl->pa_stream); | 
					
						
							|  |  |  | 	if (impl->pa_context) { | 
					
						
							|  |  |  | 		pa_context_disconnect(impl->pa_context); | 
					
						
							|  |  |  | 		pa_context_unref(impl->pa_context); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (impl->pa_mainloop) | 
					
						
							|  |  |  | 		pa_threaded_mainloop_free(impl->pa_mainloop); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (impl->stream) | 
					
						
							|  |  |  | 		pw_stream_destroy(impl->stream); | 
					
						
							|  |  |  | 	if (impl->core && impl->do_disconnect) | 
					
						
							|  |  |  | 		pw_core_disconnect(impl->core); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-04 20:28:10 +02:00
										 |  |  | 	pw_loop_invoke(impl->main_loop, NULL, 0, NULL, 0, false, impl); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-01 11:21:17 +10:00
										 |  |  | 	pw_properties_free(impl->stream_props); | 
					
						
							|  |  |  | 	pw_properties_free(impl->props); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	free(impl->buffer); | 
					
						
							|  |  |  | 	free(impl); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void module_destroy(void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct impl *impl = data; | 
					
						
							|  |  |  | 	spa_hook_remove(&impl->module_listener); | 
					
						
							| 
									
										
										
										
											2023-05-04 20:28:10 +02:00
										 |  |  | 	impl->module = NULL; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	impl_destroy(impl); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const struct pw_impl_module_events module_events = { | 
					
						
							|  |  |  | 	PW_VERSION_IMPL_MODULE_EVENTS, | 
					
						
							|  |  |  | 	.destroy = module_destroy, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static uint32_t channel_from_name(const char *name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	for (i = 0; spa_type_audio_channel[i].name; i++) { | 
					
						
							| 
									
										
										
										
											2021-05-18 11:36:13 +10:00
										 |  |  | 		if (spa_streq(name, spa_debug_type_short_name(spa_type_audio_channel[i].name))) | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 			return spa_type_audio_channel[i].type; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return SPA_AUDIO_CHANNEL_UNKNOWN; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void parse_position(struct spa_audio_info_raw *info, const char *val, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct spa_json it[2]; | 
					
						
							|  |  |  | 	char v[256]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spa_json_init(&it[0], val, len); | 
					
						
							|  |  |  |         if (spa_json_enter_array(&it[0], &it[1]) <= 0) | 
					
						
							|  |  |  |                 spa_json_init(&it[1], val, len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	info->channels = 0; | 
					
						
							|  |  |  | 	while (spa_json_get_string(&it[1], v, sizeof(v)) > 0 && | 
					
						
							|  |  |  | 	    info->channels < SPA_AUDIO_MAX_CHANNELS) { | 
					
						
							|  |  |  | 		info->position[info->channels++] = channel_from_name(v); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 22:04:50 +02:00
										 |  |  | static inline uint32_t format_from_name(const char *name, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	for (i = 0; spa_type_audio_format[i].name; i++) { | 
					
						
							|  |  |  | 		if (strncmp(name, spa_debug_type_short_name(spa_type_audio_format[i].name), len) == 0) | 
					
						
							|  |  |  | 			return spa_type_audio_format[i].type; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return SPA_AUDIO_FORMAT_UNKNOWN; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-23 12:39:51 +02:00
										 |  |  | static void parse_audio_info(const struct pw_properties *props, struct spa_audio_info_raw *info) | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	const char *str; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-23 12:39:51 +02:00
										 |  |  | 	spa_zero(*info); | 
					
						
							|  |  |  | 	if ((str = pw_properties_get(props, PW_KEY_AUDIO_FORMAT)) == NULL) | 
					
						
							|  |  |  | 		str = DEFAULT_FORMAT; | 
					
						
							|  |  |  | 	info->format = format_from_name(str, strlen(str)); | 
					
						
							| 
									
										
										
										
											2021-10-06 22:04:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-12 14:29:57 +10:00
										 |  |  | 	info->rate = pw_properties_get_uint32(props, PW_KEY_AUDIO_RATE, info->rate); | 
					
						
							| 
									
										
										
										
											2022-06-23 12:39:51 +02:00
										 |  |  | 	if (info->rate == 0) | 
					
						
							|  |  |  | 		info->rate = DEFAULT_RATE; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-12 14:29:57 +10:00
										 |  |  | 	info->channels = pw_properties_get_uint32(props, PW_KEY_AUDIO_CHANNELS, info->channels); | 
					
						
							| 
									
										
										
										
											2022-06-23 12:39:51 +02:00
										 |  |  | 	info->channels = SPA_MIN(info->channels, SPA_AUDIO_MAX_CHANNELS); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	if ((str = pw_properties_get(props, SPA_KEY_AUDIO_POSITION)) != NULL) | 
					
						
							|  |  |  | 		parse_position(info, str, strlen(str)); | 
					
						
							| 
									
										
										
										
											2022-06-23 12:39:51 +02:00
										 |  |  | 	if (info->channels == 0) | 
					
						
							|  |  |  | 		parse_position(info, DEFAULT_POSITION, strlen(DEFAULT_POSITION)); | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int calc_frame_size(struct spa_audio_info_raw *info) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res = info->channels; | 
					
						
							|  |  |  | 	switch (info->format) { | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_U8: | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_S8: | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_ALAW: | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_ULAW: | 
					
						
							|  |  |  | 		return res; | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_S16: | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_S16_OE: | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_U16: | 
					
						
							|  |  |  | 		return res * 2; | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_S24: | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_S24_OE: | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_U24: | 
					
						
							|  |  |  | 		return res * 3; | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_S24_32: | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_S24_32_OE: | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_S32: | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_S32_OE: | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_U32: | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_U32_OE: | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_F32: | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_F32_OE: | 
					
						
							|  |  |  | 		return res * 4; | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_F64: | 
					
						
							|  |  |  | 	case SPA_AUDIO_FORMAT_F64_OE: | 
					
						
							|  |  |  | 		return res * 8; | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void copy_props(struct impl *impl, struct pw_properties *props, const char *key) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const char *str; | 
					
						
							|  |  |  | 	if ((str = pw_properties_get(props, key)) != NULL) { | 
					
						
							|  |  |  | 		if (pw_properties_get(impl->stream_props, key) == NULL) | 
					
						
							|  |  |  | 			pw_properties_set(impl->stream_props, key, str); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SPA_EXPORT | 
					
						
							|  |  |  | int pipewire__module_init(struct pw_impl_module *module, const char *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct pw_context *context = pw_impl_module_get_context(module); | 
					
						
							|  |  |  | 	struct pw_properties *props = NULL; | 
					
						
							|  |  |  | 	struct impl *impl; | 
					
						
							|  |  |  | 	const char *str; | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-16 15:35:10 +10:00
										 |  |  | 	PW_LOG_TOPIC_INIT(mod_topic); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	impl = calloc(1, sizeof(struct impl)); | 
					
						
							|  |  |  | 	if (impl == NULL) | 
					
						
							|  |  |  | 		return -errno; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pw_log_debug("module %p: new %s", impl, args); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (args == NULL) | 
					
						
							|  |  |  | 		args = ""; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	props = pw_properties_new_string(args); | 
					
						
							|  |  |  | 	if (props == NULL) { | 
					
						
							|  |  |  | 		res = -errno; | 
					
						
							|  |  |  | 		pw_log_error( "can't create properties: %m"); | 
					
						
							|  |  |  | 		goto error; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	impl->props = props; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	impl->stream_props = pw_properties_new(NULL, NULL); | 
					
						
							|  |  |  | 	if (impl->stream_props == NULL) { | 
					
						
							|  |  |  | 		res = -errno; | 
					
						
							|  |  |  | 		pw_log_error( "can't create properties: %m"); | 
					
						
							|  |  |  | 		goto error; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	impl->module = module; | 
					
						
							|  |  |  | 	impl->context = context; | 
					
						
							| 
									
										
										
										
											2023-03-27 11:08:08 +02:00
										 |  |  | 	impl->main_loop = pw_context_get_main_loop(context); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	spa_ringbuffer_init(&impl->ring); | 
					
						
							|  |  |  | 	impl->buffer = calloc(1, RINGBUFFER_SIZE); | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 	spa_dll_init(&impl->dll); | 
					
						
							| 
									
										
										
										
											2022-10-20 09:24:54 +02:00
										 |  |  | 	impl->rate_limit.interval = 2 * SPA_NSEC_PER_SEC; | 
					
						
							|  |  |  | 	impl->rate_limit.burst = 1; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ((str = pw_properties_get(props, "tunnel.mode")) != NULL) { | 
					
						
							| 
									
										
										
										
											2022-06-03 10:51:46 +02:00
										 |  |  | 		if (spa_streq(str, "source")) { | 
					
						
							|  |  |  | 			impl->mode = MODE_SOURCE; | 
					
						
							|  |  |  | 		} else if (spa_streq(str, "sink")) { | 
					
						
							|  |  |  | 			impl->mode = MODE_SINK; | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			pw_log_error("invalid tunnel.mode '%s'", str); | 
					
						
							|  |  |  | 			res = -EINVAL; | 
					
						
							|  |  |  | 			goto error; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-09-23 13:11:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-12 14:29:57 +10:00
										 |  |  | 	impl->latency_msec = pw_properties_get_uint32(props, "pulse.latency", DEFAULT_LATENCY_MSEC); | 
					
						
							| 
									
										
										
										
											2021-09-23 13:11:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	if (pw_properties_get(props, PW_KEY_NODE_VIRTUAL) == NULL) | 
					
						
							|  |  |  | 		pw_properties_set(props, PW_KEY_NODE_VIRTUAL, "true"); | 
					
						
							| 
									
										
										
										
											2021-10-14 13:45:58 +02:00
										 |  |  | 	if (pw_properties_get(props, PW_KEY_NODE_NETWORK) == NULL) | 
					
						
							|  |  |  | 		pw_properties_set(props, PW_KEY_NODE_NETWORK, "true"); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (pw_properties_get(props, PW_KEY_MEDIA_CLASS) == NULL) | 
					
						
							|  |  |  | 		pw_properties_set(props, PW_KEY_MEDIA_CLASS, | 
					
						
							| 
									
										
										
										
											2022-06-03 10:51:46 +02:00
										 |  |  | 				impl->mode == MODE_SINK ? | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 					"Audio/Sink" : "Audio/Source"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((str = pw_properties_get(props, "stream.props")) != NULL) | 
					
						
							|  |  |  | 		pw_properties_update_string(impl->stream_props, str, strlen(str)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 22:04:50 +02:00
										 |  |  | 	copy_props(impl, props, PW_KEY_AUDIO_FORMAT); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	copy_props(impl, props, PW_KEY_AUDIO_RATE); | 
					
						
							|  |  |  | 	copy_props(impl, props, PW_KEY_AUDIO_CHANNELS); | 
					
						
							|  |  |  | 	copy_props(impl, props, SPA_KEY_AUDIO_POSITION); | 
					
						
							|  |  |  | 	copy_props(impl, props, PW_KEY_NODE_NAME); | 
					
						
							|  |  |  | 	copy_props(impl, props, PW_KEY_NODE_DESCRIPTION); | 
					
						
							|  |  |  | 	copy_props(impl, props, PW_KEY_NODE_GROUP); | 
					
						
							|  |  |  | 	copy_props(impl, props, PW_KEY_NODE_LATENCY); | 
					
						
							|  |  |  | 	copy_props(impl, props, PW_KEY_NODE_VIRTUAL); | 
					
						
							| 
									
										
										
										
											2022-11-17 15:55:02 +01:00
										 |  |  | 	copy_props(impl, props, PW_KEY_NODE_NETWORK); | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	copy_props(impl, props, PW_KEY_MEDIA_CLASS); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	parse_audio_info(impl->stream_props, &impl->info); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 	impl->frame_size = calc_frame_size(&impl->info); | 
					
						
							|  |  |  | 	if (impl->frame_size == 0) { | 
					
						
							|  |  |  | 		pw_log_error("unsupported audio format:%d channels:%d", | 
					
						
							|  |  |  | 				impl->info.format, impl->info.channels); | 
					
						
							|  |  |  | 		res = -EINVAL; | 
					
						
							|  |  |  | 		goto error; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	spa_dll_set_bw(&impl->dll, SPA_DLL_BW_MIN, 128, impl->info.rate); | 
					
						
							| 
									
										
										
										
											2022-04-19 09:41:28 +02:00
										 |  |  | 	impl->max_error = 256.0f; | 
					
						
							| 
									
										
										
										
											2022-03-30 17:23:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 15:55:15 +02:00
										 |  |  | 	impl->core = pw_context_get_object(impl->context, PW_TYPE_INTERFACE_Core); | 
					
						
							|  |  |  | 	if (impl->core == NULL) { | 
					
						
							|  |  |  | 		str = pw_properties_get(props, PW_KEY_REMOTE_NAME); | 
					
						
							|  |  |  | 		impl->core = pw_context_connect(impl->context, | 
					
						
							|  |  |  | 				pw_properties_new( | 
					
						
							|  |  |  | 					PW_KEY_REMOTE_NAME, str, | 
					
						
							|  |  |  | 					NULL), | 
					
						
							|  |  |  | 				0); | 
					
						
							|  |  |  | 		impl->do_disconnect = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (impl->core == NULL) { | 
					
						
							|  |  |  | 		res = -errno; | 
					
						
							|  |  |  | 		pw_log_error("can't connect: %m"); | 
					
						
							|  |  |  | 		goto error; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pw_proxy_add_listener((struct pw_proxy*)impl->core, | 
					
						
							|  |  |  | 			&impl->core_proxy_listener, | 
					
						
							|  |  |  | 			&core_proxy_events, impl); | 
					
						
							|  |  |  | 	pw_core_add_listener(impl->core, | 
					
						
							|  |  |  | 			&impl->core_listener, | 
					
						
							|  |  |  | 			&core_events, impl); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((res = create_pulse_stream(impl)) < 0) | 
					
						
							|  |  |  | 		goto error; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((res = create_stream(impl)) < 0) | 
					
						
							|  |  |  | 		goto error; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pw_impl_module_add_listener(module, &impl->module_listener, &module_events, impl); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pw_impl_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | error: | 
					
						
							|  |  |  | 	impl_destroy(impl); | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } |