mirror of
				https://gitlab.freedesktop.org/pipewire/pipewire.git
				synced 2025-11-03 09:01:54 -05:00 
			
		
		
		
	modules: make sure we deactivate the graph safely
Use a blocking invoke to ensure the loop is not using the graph anymore when we deactivate or reset it. See #4750, #4700
This commit is contained in:
		
							parent
							
								
									512db73823
								
							
						
					
					
						commit
						12cfaa7d50
					
				
					 1 changed files with 63 additions and 12 deletions
				
			
		| 
						 | 
					@ -844,6 +844,7 @@ struct impl {
 | 
				
			||||||
	struct spa_hook graph_listener;
 | 
						struct spa_hook graph_listener;
 | 
				
			||||||
	uint32_t n_inputs;
 | 
						uint32_t n_inputs;
 | 
				
			||||||
	uint32_t n_outputs;
 | 
						uint32_t n_outputs;
 | 
				
			||||||
 | 
						bool graph_active;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void capture_destroy(void *d)
 | 
					static void capture_destroy(void *d)
 | 
				
			||||||
| 
						 | 
					@ -931,6 +932,7 @@ static void playback_process(void *d)
 | 
				
			||||||
	pw_log_trace_fp("%p: stride:%d size:%d requested:%"PRIu64" (%"PRIu64")", impl,
 | 
						pw_log_trace_fp("%p: stride:%d size:%d requested:%"PRIu64" (%"PRIu64")", impl,
 | 
				
			||||||
			stride, data_size, out->requested, out->requested * stride);
 | 
								stride, data_size, out->requested, out->requested * stride);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (impl->graph_active)
 | 
				
			||||||
		spa_filter_graph_process(impl->graph, cin, cout, data_size / sizeof(float));
 | 
							spa_filter_graph_process(impl->graph, cin, cout, data_size / sizeof(float));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
done:
 | 
					done:
 | 
				
			||||||
| 
						 | 
					@ -940,6 +942,61 @@ done:
 | 
				
			||||||
		pw_stream_queue_buffer(impl->playback, out);
 | 
							pw_stream_queue_buffer(impl->playback, out);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int do_deactivate(struct spa_loop *loop, bool async, uint32_t seq,
 | 
				
			||||||
 | 
					                const void *data, size_t size, void *user_data)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct impl *impl = user_data;
 | 
				
			||||||
 | 
						impl->graph_active = false;
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int activate_graph(struct impl *impl)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						char rate[64];
 | 
				
			||||||
 | 
						int res;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (impl->graph_active)
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						snprintf(rate, sizeof(rate), "%lu", impl->rate);
 | 
				
			||||||
 | 
						res = spa_filter_graph_activate(impl->graph, &SPA_DICT_ITEMS(
 | 
				
			||||||
 | 
									SPA_DICT_ITEM(SPA_KEY_AUDIO_RATE, rate)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (res >= 0)
 | 
				
			||||||
 | 
							impl->graph_active = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return res;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int deactivate_graph(struct impl *impl)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct pw_loop *data_loop;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!impl->graph_active)
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data_loop = pw_stream_get_data_loop(impl->playback);
 | 
				
			||||||
 | 
						pw_loop_invoke(data_loop, do_deactivate, 0, NULL, 0, true, impl);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return spa_filter_graph_deactivate(impl->graph);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int reset_graph(struct impl *impl)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct pw_loop *data_loop;
 | 
				
			||||||
 | 
						int res;
 | 
				
			||||||
 | 
						bool old_active = impl->graph_active;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data_loop = pw_stream_get_data_loop(impl->playback);
 | 
				
			||||||
 | 
						pw_loop_invoke(data_loop, do_deactivate, 0, NULL, 0, true, impl);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						res = spa_filter_graph_reset(impl->graph);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						impl->graph_active = old_active;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return res;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void param_latency_changed(struct impl *impl, const struct spa_pod *param)
 | 
					static void param_latency_changed(struct impl *impl, const struct spa_pod *param)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct spa_latency_info latency;
 | 
						struct spa_latency_info latency;
 | 
				
			||||||
| 
						 | 
					@ -1013,7 +1070,6 @@ static void param_changed(void *data, uint32_t id, const struct spa_pod *param,
 | 
				
			||||||
		bool capture)
 | 
							bool capture)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct impl *impl = data;
 | 
						struct impl *impl = data;
 | 
				
			||||||
	struct spa_filter_graph *graph = impl->graph;
 | 
					 | 
				
			||||||
	int res;
 | 
						int res;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	switch (id) {
 | 
						switch (id) {
 | 
				
			||||||
| 
						 | 
					@ -1024,7 +1080,7 @@ static void param_changed(void *data, uint32_t id, const struct spa_pod *param,
 | 
				
			||||||
		if (param == NULL) {
 | 
							if (param == NULL) {
 | 
				
			||||||
			pw_log_info("module %p: filter deactivate", impl);
 | 
								pw_log_info("module %p: filter deactivate", impl);
 | 
				
			||||||
			if (!capture)
 | 
								if (!capture)
 | 
				
			||||||
				spa_filter_graph_deactivate(graph);
 | 
									deactivate_graph(impl);
 | 
				
			||||||
			impl->rate = 0;
 | 
								impl->rate = 0;
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			if ((res = spa_format_audio_raw_parse(param, &info)) < 0)
 | 
								if ((res = spa_format_audio_raw_parse(param, &info)) < 0)
 | 
				
			||||||
| 
						 | 
					@ -1071,13 +1127,12 @@ static void playback_state_changed(void *data, enum pw_stream_state old,
 | 
				
			||||||
		enum pw_stream_state state, const char *error)
 | 
							enum pw_stream_state state, const char *error)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct impl *impl = data;
 | 
						struct impl *impl = data;
 | 
				
			||||||
	struct spa_filter_graph *graph = impl->graph;
 | 
					 | 
				
			||||||
	int res;
 | 
						int res;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	switch (state) {
 | 
						switch (state) {
 | 
				
			||||||
	case PW_STREAM_STATE_PAUSED:
 | 
						case PW_STREAM_STATE_PAUSED:
 | 
				
			||||||
		pw_stream_flush(impl->playback, false);
 | 
							pw_stream_flush(impl->playback, false);
 | 
				
			||||||
		spa_filter_graph_reset(graph);
 | 
							reset_graph(impl);
 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
	case PW_STREAM_STATE_UNCONNECTED:
 | 
						case PW_STREAM_STATE_UNCONNECTED:
 | 
				
			||||||
		pw_log_info("module %p: unconnected", impl);
 | 
							pw_log_info("module %p: unconnected", impl);
 | 
				
			||||||
| 
						 | 
					@ -1097,15 +1152,11 @@ static void playback_state_changed(void *data, enum pw_stream_state old,
 | 
				
			||||||
			goto error;
 | 
								goto error;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if (impl->rate != target) {
 | 
							if (impl->rate != target) {
 | 
				
			||||||
			char rate[64];
 | 
					 | 
				
			||||||
			impl->rate = target;
 | 
								impl->rate = target;
 | 
				
			||||||
			snprintf(rate, sizeof(rate), "%lu", impl->rate);
 | 
								deactivate_graph(impl);
 | 
				
			||||||
			spa_filter_graph_deactivate(graph);
 | 
					 | 
				
			||||||
			if ((res = spa_filter_graph_activate(graph,
 | 
					 | 
				
			||||||
					&SPA_DICT_ITEMS(
 | 
					 | 
				
			||||||
						SPA_DICT_ITEM(SPA_KEY_AUDIO_RATE, rate)))) < 0)
 | 
					 | 
				
			||||||
				goto error;
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							if ((res = activate_graph(impl)) < 0)
 | 
				
			||||||
 | 
								goto error;
 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue