mirror of
				https://gitlab.freedesktop.org/pipewire/pipewire.git
				synced 2025-11-03 09:01:54 -05:00 
			
		
		
		
	spa: add dynamic builder helper
Resizes the buffer dynamically. Be careful with getting the address of a pod in the buffer, it might not be valid after building more stuff with the builder.
This commit is contained in:
		
							parent
							
								
									23db50630d
								
							
						
					
					
						commit
						d3a1b9fe31
					
				
					 2 changed files with 92 additions and 25 deletions
				
			
		
							
								
								
									
										81
									
								
								spa/include/spa/pod/dynamic.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								spa/include/spa/pod/dynamic.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,81 @@
 | 
				
			||||||
 | 
					/* Simple Plugin API
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Copyright © 2018 Wim Taymans
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Permission is hereby granted, free of charge, to any person obtaining a
 | 
				
			||||||
 | 
					 * copy of this software and associated documentation files (the "Software"),
 | 
				
			||||||
 | 
					 * to deal in the Software without restriction, including without limitation
 | 
				
			||||||
 | 
					 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 | 
				
			||||||
 | 
					 * and/or sell copies of the Software, and to permit persons to whom the
 | 
				
			||||||
 | 
					 * Software is furnished to do so, subject to the following conditions:
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * The above copyright notice and this permission notice (including the next
 | 
				
			||||||
 | 
					 * paragraph) shall be included in all copies or substantial portions of the
 | 
				
			||||||
 | 
					 * Software.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
				
			||||||
 | 
					 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 | 
				
			||||||
 | 
					 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
				
			||||||
 | 
					 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 | 
				
			||||||
 | 
					 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 | 
				
			||||||
 | 
					 * DEALINGS IN THE SOFTWARE.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef SPA_POD_DYNAMIC_H
 | 
				
			||||||
 | 
					#define SPA_POD_DYNAMIC_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					extern "C" {
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <spa/pod/builder.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct spa_pod_dynamic_builder {
 | 
				
			||||||
 | 
						struct spa_pod_builder b;
 | 
				
			||||||
 | 
						void *data;
 | 
				
			||||||
 | 
						uint32_t extend;
 | 
				
			||||||
 | 
						uint32_t _padding;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int spa_pod_dynamic_builder_overflow(void *data, uint32_t size)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct spa_pod_dynamic_builder *d = data;
 | 
				
			||||||
 | 
						int32_t old_size = d->b.size;
 | 
				
			||||||
 | 
						int32_t new_size = SPA_ROUND_UP_N(size, d->extend);
 | 
				
			||||||
 | 
						void *old_data = d->b.data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (old_data == d->data)
 | 
				
			||||||
 | 
							d->b.data = NULL;
 | 
				
			||||||
 | 
						if ((d->b.data = realloc(d->b.data, new_size)) == NULL)
 | 
				
			||||||
 | 
							return -errno;
 | 
				
			||||||
 | 
						if (old_data == d->data && d->b.data != old_data && old_size > 0)
 | 
				
			||||||
 | 
							memcpy(d->b.data, old_data, old_size);
 | 
				
			||||||
 | 
						d->b.size = new_size;
 | 
				
			||||||
 | 
					        return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static inline void spa_pod_dynamic_builder_init(struct spa_pod_dynamic_builder *builder,
 | 
				
			||||||
 | 
							void *data, uint32_t size, uint32_t extend)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						static const struct spa_pod_builder_callbacks spa_pod_dynamic_builder_callbacks = {
 | 
				
			||||||
 | 
							SPA_VERSION_POD_BUILDER_CALLBACKS,
 | 
				
			||||||
 | 
							.overflow = spa_pod_dynamic_builder_overflow
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
						builder->b = SPA_POD_BUILDER_INIT(data, size);
 | 
				
			||||||
 | 
						spa_pod_builder_set_callbacks(&builder->b, &spa_pod_dynamic_builder_callbacks, builder);
 | 
				
			||||||
 | 
						builder->extend = extend;
 | 
				
			||||||
 | 
						builder->data = data;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static inline void spa_pod_dynamic_builder_clean(struct spa_pod_dynamic_builder *builder)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						if (builder->data != builder->b.data)
 | 
				
			||||||
 | 
							free(builder->b.data);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					}  /* extern "C" */
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif /* SPA_POD_DYNAMIC_H */
 | 
				
			||||||
| 
						 | 
					@ -39,6 +39,7 @@
 | 
				
			||||||
#include <spa/utils/string.h>
 | 
					#include <spa/utils/string.h>
 | 
				
			||||||
#include <spa/utils/json.h>
 | 
					#include <spa/utils/json.h>
 | 
				
			||||||
#include <spa/param/profiler.h>
 | 
					#include <spa/param/profiler.h>
 | 
				
			||||||
 | 
					#include <spa/pod/dynamic.h>
 | 
				
			||||||
#include <spa/debug/pod.h>
 | 
					#include <spa/debug/pod.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <pipewire/utils.h>
 | 
					#include <pipewire/utils.h>
 | 
				
			||||||
| 
						 | 
					@ -613,26 +614,12 @@ static const struct pw_stream_events out_stream_events = {
 | 
				
			||||||
	.param_changed = param_changed
 | 
						.param_changed = param_changed
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int builder_overflow(void *data, uint32_t size)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	struct spa_pod_builder *b = data;
 | 
					 | 
				
			||||||
	b->size = SPA_ROUND_UP_N(size, 4096);
 | 
					 | 
				
			||||||
	if ((b->data = realloc(b->data, b->size)) == NULL)
 | 
					 | 
				
			||||||
		return -errno;
 | 
					 | 
				
			||||||
        return 0;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static const struct spa_pod_builder_callbacks builder_callbacks = {
 | 
					 | 
				
			||||||
	SPA_VERSION_POD_BUILDER_CALLBACKS,
 | 
					 | 
				
			||||||
	.overflow = builder_overflow
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static int setup_streams(struct impl *impl)
 | 
					static int setup_streams(struct impl *impl)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	int res;
 | 
						int res;
 | 
				
			||||||
	uint32_t i, n_params;
 | 
						uint32_t i, n_params;
 | 
				
			||||||
	const struct spa_pod *params[256];
 | 
						const struct spa_pod *params[256];
 | 
				
			||||||
	struct spa_pod_builder b;
 | 
						struct spa_pod_dynamic_builder b;
 | 
				
			||||||
	struct graph *graph = &impl->graph;
 | 
						struct graph *graph = &impl->graph;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	impl->capture = pw_stream_new(impl->core,
 | 
						impl->capture = pw_stream_new(impl->core,
 | 
				
			||||||
| 
						 | 
					@ -656,16 +643,15 @@ static int setup_streams(struct impl *impl)
 | 
				
			||||||
			&out_stream_events, impl);
 | 
								&out_stream_events, impl);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	n_params = 0;
 | 
						n_params = 0;
 | 
				
			||||||
	spa_pod_builder_init(&b, NULL, 0);
 | 
						spa_pod_dynamic_builder_init(&b, NULL, 0, 4096);
 | 
				
			||||||
	spa_pod_builder_set_callbacks(&b, &builder_callbacks, &b);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	params[n_params++] = spa_format_audio_raw_build(&b,
 | 
						params[n_params++] = spa_format_audio_raw_build(&b.b,
 | 
				
			||||||
			SPA_PARAM_EnumFormat, &impl->capture_info);
 | 
								SPA_PARAM_EnumFormat, &impl->capture_info);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for (i = 0; i < graph->n_control; i++)
 | 
						for (i = 0; i < graph->n_control; i++)
 | 
				
			||||||
		params[n_params++] = get_prop_info(graph, &b, i);
 | 
							params[n_params++] = get_prop_info(graph, &b.b, i);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	params[n_params++] = get_props_param(graph, &b);
 | 
						params[n_params++] = get_props_param(graph, &b.b);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	res = pw_stream_connect(impl->capture,
 | 
						res = pw_stream_connect(impl->capture,
 | 
				
			||||||
			PW_DIRECTION_INPUT,
 | 
								PW_DIRECTION_INPUT,
 | 
				
			||||||
| 
						 | 
					@ -674,14 +660,14 @@ static int setup_streams(struct impl *impl)
 | 
				
			||||||
			PW_STREAM_FLAG_MAP_BUFFERS |
 | 
								PW_STREAM_FLAG_MAP_BUFFERS |
 | 
				
			||||||
			PW_STREAM_FLAG_RT_PROCESS,
 | 
								PW_STREAM_FLAG_RT_PROCESS,
 | 
				
			||||||
			params, n_params);
 | 
								params, n_params);
 | 
				
			||||||
	free(b.data);
 | 
					
 | 
				
			||||||
 | 
						spa_pod_dynamic_builder_clean(&b);
 | 
				
			||||||
	if (res < 0)
 | 
						if (res < 0)
 | 
				
			||||||
		return res;
 | 
							return res;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	n_params = 0;
 | 
						n_params = 0;
 | 
				
			||||||
	spa_pod_builder_init(&b, NULL, 0);
 | 
						spa_pod_dynamic_builder_init(&b, NULL, 0, 4096);
 | 
				
			||||||
	spa_pod_builder_set_callbacks(&b, &builder_callbacks, &b);
 | 
						params[n_params++] = spa_format_audio_raw_build(&b.b,
 | 
				
			||||||
	params[n_params++] = spa_format_audio_raw_build(&b,
 | 
					 | 
				
			||||||
			SPA_PARAM_EnumFormat, &impl->playback_info);
 | 
								SPA_PARAM_EnumFormat, &impl->playback_info);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	res = pw_stream_connect(impl->playback,
 | 
						res = pw_stream_connect(impl->playback,
 | 
				
			||||||
| 
						 | 
					@ -692,7 +678,7 @@ static int setup_streams(struct impl *impl)
 | 
				
			||||||
			PW_STREAM_FLAG_RT_PROCESS  |
 | 
								PW_STREAM_FLAG_RT_PROCESS  |
 | 
				
			||||||
			PW_STREAM_FLAG_TRIGGER,
 | 
								PW_STREAM_FLAG_TRIGGER,
 | 
				
			||||||
			params, n_params);
 | 
								params, n_params);
 | 
				
			||||||
	free(b.data);
 | 
						spa_pod_dynamic_builder_clean(&b);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (res < 0)
 | 
						if (res < 0)
 | 
				
			||||||
		return res;
 | 
							return res;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue