1 /* PipeWire
2  *
3  * Copyright © 2020 Collabora Ltd.
4  *   @author George Kiagiadakis <george.kiagiadakis@collabora.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include <unistd.h>
27 
28 #include <pipewire/pipewire.h>
29 #include <pipewire/extensions/session-manager.h>
30 
31 #include <spa/utils/string.h>
32 #include <spa/pod/builder.h>
33 #include <spa/pod/parser.h>
34 #include <spa/pod/filter.h>
35 
36 #include <valgrind/valgrind.h>
37 
38 struct props
39 {
40 	float volume;
41 	bool mute;
42 };
43 
44 struct endpoint
45 {
46 	struct spa_interface iface;
47 	struct spa_hook_list hooks;
48 	struct pw_properties *properties;
49 	struct pw_endpoint_info info;
50 	uint32_t n_subscribe_ids;
51 	uint32_t subscribe_ids[2];
52 	struct props props;
53 };
54 
55 #define pw_endpoint_emit(hooks,method,version,...) \
56 	spa_hook_list_call_simple(hooks, struct pw_endpoint_events, \
57 				  method, version, ##__VA_ARGS__)
58 
59 #define pw_endpoint_emit_info(hooks,...)	pw_endpoint_emit(hooks, info, 0, ##__VA_ARGS__)
60 #define pw_endpoint_emit_param(hooks,...)	pw_endpoint_emit(hooks, param, 0, ##__VA_ARGS__)
61 
62 static int
endpoint_add_listener(void * object,struct spa_hook * listener,const struct pw_endpoint_events * events,void * data)63 endpoint_add_listener(void *object,
64 		struct spa_hook *listener,
65 		const struct pw_endpoint_events *events,
66 		void *data)
67 {
68 	struct endpoint *self = object;
69 	struct spa_hook_list save;
70 
71 	spa_hook_list_isolate(&self->hooks, &save, listener, events, data);
72 	pw_endpoint_emit_info(&self->hooks, &self->info);
73 	spa_hook_list_join(&self->hooks, &save);
74 	return 0;
75 }
76 
77 static int
endpoint_enum_params(void * object,int seq,uint32_t id,uint32_t start,uint32_t num,const struct spa_pod * filter)78 endpoint_enum_params (void *object, int seq,
79 		uint32_t id, uint32_t start, uint32_t num,
80 		const struct spa_pod *filter)
81 {
82 	struct endpoint *self = object;
83 	struct spa_pod *param, *result;
84 	struct spa_pod_builder b = { 0 };
85 	uint8_t buffer[1024];
86 	uint32_t count = 0, index, next = start;
87 
88 	next = start;
89       next:
90 	index = next++;
91 
92 	spa_pod_builder_init(&b, buffer, sizeof(buffer));
93 
94 	switch (id) {
95 	case SPA_PARAM_PropInfo:
96 	{
97 		struct props *p = &self->props;
98 
99 		switch (index) {
100 		case 0:
101 			param = spa_pod_builder_add_object(&b,
102 				SPA_TYPE_OBJECT_PropInfo, id,
103 				SPA_PROP_INFO_id,   SPA_POD_Id(SPA_PROP_volume),
104 				SPA_PROP_INFO_name, SPA_POD_String("volume"),
105 				SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Float(p->volume, 0.0, 1.0));
106 			break;
107 		case 1:
108 			param = spa_pod_builder_add_object(&b,
109 				SPA_TYPE_OBJECT_PropInfo, id,
110 				SPA_PROP_INFO_id,   SPA_POD_Id(SPA_PROP_mute),
111 				SPA_PROP_INFO_name, SPA_POD_String("mute"),
112 				SPA_PROP_INFO_type, SPA_POD_CHOICE_Bool(p->mute));
113 			break;
114 		default:
115 			return 0;
116 		}
117 		break;
118 	}
119 	case SPA_PARAM_Props:
120 	{
121 		struct props *p = &self->props;
122 
123 		switch (index) {
124 		case 0:
125 			param = spa_pod_builder_add_object(&b,
126 				SPA_TYPE_OBJECT_Props, id,
127 				SPA_PROP_volume,  SPA_POD_Float(p->volume),
128 				SPA_PROP_mute,    SPA_POD_Bool(p->mute));
129 			break;
130 		default:
131 			return 0;
132 		}
133 		break;
134 	}
135 	default:
136 		return -ENOENT;
137 	}
138 
139 	if (spa_pod_filter(&b, &result, param, filter) < 0)
140 		goto next;
141 
142 	pw_endpoint_emit_param(&self->hooks, seq, id, index, next, result);
143 
144 	if (++count != num)
145 		goto next;
146 
147 	return 0;
148 }
149 
150 static int
endpoint_subscribe_params(void * object,uint32_t * ids,uint32_t n_ids)151 endpoint_subscribe_params (void *object, uint32_t *ids, uint32_t n_ids)
152 {
153 	struct endpoint *self = object;
154 
155 	n_ids = SPA_MIN(n_ids, SPA_N_ELEMENTS(self->subscribe_ids));
156 	self->n_subscribe_ids = n_ids;
157 
158 	for (uint32_t i = 0; i < n_ids; i++) {
159 		self->subscribe_ids[i] = ids[i];
160 		endpoint_enum_params(object, 1, ids[i], 0, UINT32_MAX, NULL);
161 	}
162 	return 0;
163 }
164 
165 static int
endpoint_set_param(void * object,uint32_t id,uint32_t flags,const struct spa_pod * param)166 endpoint_set_param (void *object, uint32_t id, uint32_t flags,
167 		const struct spa_pod *param)
168 {
169 	struct endpoint *self = object;
170 
171 	if (id == SPA_PARAM_Props) {
172 		struct props *p = &self->props;
173 		spa_pod_parse_object(param,
174 			SPA_TYPE_OBJECT_Props, NULL,
175 			SPA_PROP_volume, SPA_POD_OPT_Float(&p->volume),
176 			SPA_PROP_mute,   SPA_POD_OPT_Bool(&p->mute));
177 	}
178 	else {
179 		spa_assert_not_reached();
180 		return -ENOENT;
181 	}
182 
183 	for (uint32_t i = 0; i < self->n_subscribe_ids; i++) {
184 		if (id == self->subscribe_ids[i])
185 			endpoint_enum_params (self, 1, id, 0, UINT32_MAX, NULL);
186 	}
187 
188 	return 0;
189 }
190 
191 static int
endpoint_create_link(void * object,const struct spa_dict * props)192 endpoint_create_link (void *object, const struct spa_dict *props)
193 {
194 	spa_assert_not_reached();
195 	return -ENOTSUP;
196 }
197 
198 static const struct pw_endpoint_methods endpoint_methods = {
199 	PW_VERSION_ENDPOINT_METHODS,
200 	.add_listener = endpoint_add_listener,
201 	.subscribe_params = endpoint_subscribe_params,
202 	.enum_params = endpoint_enum_params,
203 	.set_param = endpoint_set_param,
204 	.create_link = endpoint_create_link,
205 };
206 
207 static struct spa_param_info param_info[] = {
208 	SPA_PARAM_INFO (SPA_PARAM_Props, SPA_PARAM_INFO_READWRITE),
209 	SPA_PARAM_INFO (SPA_PARAM_PropInfo, SPA_PARAM_INFO_READ)
210 };
211 
212 static void
endpoint_init(struct endpoint * self)213 endpoint_init(struct endpoint * self)
214 {
215 	self->iface = SPA_INTERFACE_INIT (
216 		PW_TYPE_INTERFACE_Endpoint,
217 		PW_VERSION_ENDPOINT,
218 		&endpoint_methods, self);
219 	spa_hook_list_init (&self->hooks);
220 
221 	self->info.version = PW_VERSION_ENDPOINT_INFO;
222 	self->info.change_mask = PW_ENDPOINT_CHANGE_MASK_ALL;
223 	self->info.name = "test-endpoint";
224 	self->info.media_class = "Audio/Sink";
225 	self->info.direction = PW_DIRECTION_OUTPUT;
226 	self->info.n_streams = 0;
227 	self->info.session_id = SPA_ID_INVALID;
228 
229 	self->properties = pw_properties_new(
230 		PW_KEY_ENDPOINT_NAME, self->info.name,
231 		PW_KEY_MEDIA_CLASS, self->info.media_class,
232 		NULL);
233 	self->info.props = &self->properties->dict;
234 
235 	self->info.params = param_info;
236 	self->info.n_params = SPA_N_ELEMENTS (param_info);
237 
238 	self->props.volume = 0.9;
239 	self->props.mute = false;
240 }
241 
242 static void
endpoint_clear(struct endpoint * self)243 endpoint_clear(struct endpoint * self)
244 {
245 	spa_hook_list_clean(&self->hooks);
246 	pw_properties_free(self->properties);
247 }
248 
249 struct test_endpoint_data
250 {
251 	struct pw_main_loop *loop;
252 	struct pw_context *context;
253 	struct pw_core *core;
254 
255 	struct pw_registry *registry;
256 	struct spa_hook registry_listener;
257 
258 	struct endpoint endpoint;
259 	struct pw_proxy *export_proxy;
260 	struct pw_proxy *bound_proxy;
261 	struct spa_hook object_listener;
262 	struct spa_hook proxy_listener;
263 
264 	struct props props;
265 	bool info_received;
266 	int params_received;
267 };
268 
269 static void
endpoint_event_info(void * object,const struct pw_endpoint_info * info)270 endpoint_event_info(void *object, const struct pw_endpoint_info *info)
271 {
272 	struct test_endpoint_data *d = object;
273 	const char *val;
274 
275 	spa_assert_se(info);
276 	spa_assert_se(info->version == PW_VERSION_ENDPOINT_INFO);
277 	spa_assert_se(info->id == pw_proxy_get_bound_id(d->bound_proxy));
278 	spa_assert_se(info->id == pw_proxy_get_bound_id(d->export_proxy));
279 	spa_assert_se(info->change_mask == PW_ENDPOINT_CHANGE_MASK_ALL);
280 	spa_assert_se(spa_streq(info->name, "test-endpoint"));
281 	spa_assert_se(spa_streq(info->media_class, "Audio/Sink"));
282 	spa_assert_se(info->direction == PW_DIRECTION_OUTPUT);
283 	spa_assert_se(info->n_streams == 0);
284 	spa_assert_se(info->session_id == SPA_ID_INVALID);
285 	spa_assert_se(info->n_params == SPA_N_ELEMENTS (param_info));
286 	spa_assert_se(info->n_params == 2);
287 	spa_assert_se(info->params[0].id == param_info[0].id);
288 	spa_assert_se(info->params[0].flags == param_info[0].flags);
289 	spa_assert_se(info->params[1].id == param_info[1].id);
290 	spa_assert_se(info->params[1].flags == param_info[1].flags);
291 	spa_assert_se(info->props != NULL);
292 	val = spa_dict_lookup(info->props, PW_KEY_ENDPOINT_NAME);
293 	spa_assert_se(val && spa_streq(val, "test-endpoint"));
294 	val = spa_dict_lookup(info->props, PW_KEY_MEDIA_CLASS);
295 	spa_assert_se(val && spa_streq(val, "Audio/Sink"));
296 
297 	d->info_received = true;
298 	pw_main_loop_quit(d->loop);
299 }
300 
301 static void
endpoint_event_param(void * object,int seq,uint32_t id,uint32_t index,uint32_t next,const struct spa_pod * param)302 endpoint_event_param(void *object, int seq,
303 		uint32_t id, uint32_t index, uint32_t next,
304 		const struct spa_pod *param)
305 {
306 	struct test_endpoint_data *d = object;
307 
308 	if (id == SPA_PARAM_Props) {
309 		struct props *p = &d->props;
310 		spa_assert_se(param);
311 		spa_pod_parse_object(param,
312 			SPA_TYPE_OBJECT_Props, &id,
313 			SPA_PROP_volume, SPA_POD_OPT_Float(&p->volume),
314 			SPA_PROP_mute,   SPA_POD_OPT_Bool(&p->mute));
315 		spa_assert_se(id == SPA_PARAM_Props);
316 	}
317 
318 	d->params_received++;
319 	pw_main_loop_quit(d->loop);
320 }
321 
322 static const struct pw_endpoint_events endpoint_events = {
323 	PW_VERSION_ENDPOINT_EVENTS,
324 	.info = endpoint_event_info,
325 	.param = endpoint_event_param,
326 };
327 
328 static void
endpoint_proxy_destroy(void * object)329 endpoint_proxy_destroy(void *object)
330 {
331 	struct test_endpoint_data *d = object;
332 	d->bound_proxy = NULL;
333 	pw_main_loop_quit(d->loop);
334 }
335 
336 static const struct pw_proxy_events proxy_events = {
337 	PW_VERSION_PROXY_EVENTS,
338 	.destroy = endpoint_proxy_destroy,
339 };
340 
341 static void
test_endpoint_global(void * object,uint32_t id,uint32_t permissions,const char * type,uint32_t version,const struct spa_dict * props)342 test_endpoint_global(void *object, uint32_t id,
343 		uint32_t permissions, const char *type, uint32_t version,
344 		const struct spa_dict *props)
345 {
346 	struct test_endpoint_data *d = object;
347 	const char *val;
348 
349 	if (!spa_streq(type, PW_TYPE_INTERFACE_Endpoint))
350 		return;
351 
352 	d->bound_proxy = pw_registry_bind(d->registry, id, type,
353 					PW_VERSION_ENDPOINT, 0);
354 	spa_assert_se(d->bound_proxy != NULL);
355 
356 	spa_assert_se(props != NULL);
357 	val = spa_dict_lookup(props, PW_KEY_ENDPOINT_NAME);
358 	spa_assert_se(val && spa_streq(val, "test-endpoint"));
359 	val = spa_dict_lookup(props, PW_KEY_MEDIA_CLASS);
360 	spa_assert_se(val && spa_streq(val, "Audio/Sink"));
361 
362 	pw_endpoint_add_listener(d->bound_proxy, &d->object_listener,
363 				 &endpoint_events, d);
364 	pw_proxy_add_listener(d->bound_proxy, &d->proxy_listener,
365 				 &proxy_events, d);
366 }
367 
368 static void
test_endpoint_global_remove(void * object,uint32_t id)369 test_endpoint_global_remove(void *object, uint32_t id)
370 {
371 	struct test_endpoint_data *d = object;
372 	if (d->bound_proxy && id == pw_proxy_get_bound_id(d->bound_proxy))
373 		pw_proxy_destroy(d->bound_proxy);
374 }
375 
376 static const struct pw_registry_events registry_events = {
377 	PW_VERSION_REGISTRY_EVENTS,
378 	.global = test_endpoint_global,
379 	.global_remove = test_endpoint_global_remove,
380 };
381 
test_endpoint(void)382 static void test_endpoint(void)
383 {
384 	struct test_endpoint_data d;
385 	uint32_t ids[] = { SPA_PARAM_Props };
386 	uint8_t buffer[1024];
387 	struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, 1024);
388 
389 	d.loop = pw_main_loop_new(NULL);
390 	d.context = pw_context_new(pw_main_loop_get_loop(d.loop), NULL, 0);
391 	spa_assert_se(d.context != NULL);
392 
393 	d.core = pw_context_connect_self(d.context, NULL, 0);
394 	spa_assert_se(d.core != NULL);
395 
396 	d.registry = pw_core_get_registry(d.core, PW_VERSION_REGISTRY, 0);
397 	pw_registry_add_listener(d.registry,
398 				&d.registry_listener,
399 				&registry_events, &d);
400 
401 	/* export and expect to get a global on the registry, along with info */
402 	d.info_received = false;
403 	endpoint_init(&d.endpoint);
404 	d.export_proxy = pw_core_export(d.core, PW_TYPE_INTERFACE_Endpoint,
405 				d.endpoint.info.props, &d.endpoint.iface, 0);
406 	spa_assert_se(d.export_proxy != NULL);
407 	pw_main_loop_run(d.loop);
408 	spa_assert_se(d.bound_proxy);
409 	spa_assert_se(d.info_received == true);
410 
411 	/* request params */
412 	d.params_received = 0;
413 	d.props.volume = 0.0;
414 	d.props.mute = true;
415 	pw_endpoint_subscribe_params(d.bound_proxy, ids, SPA_N_ELEMENTS(ids));
416 	pw_main_loop_run(d.loop);
417 	spa_assert_se(d.params_received == 1);
418 	spa_assert_se(d.props.volume > 0.89 && d.props.volume < 0.91);
419 	spa_assert_se(d.props.mute == false);
420 
421 	/* set param from the client */
422 	d.params_received = 0;
423 	pw_endpoint_set_param(d.bound_proxy, SPA_PARAM_Props, 0,
424 		spa_pod_builder_add_object(&b,
425 				SPA_TYPE_OBJECT_Props, SPA_PARAM_Props,
426 				SPA_PROP_volume, SPA_POD_Float(0.5)));
427 	pw_main_loop_run(d.loop);
428 	spa_assert_se(d.params_received == 1);
429 	spa_assert_se(d.props.volume > 0.49 && d.props.volume < 0.51);
430 	spa_assert_se(d.props.mute == false);
431 
432 	/* set param from the impl */
433 	d.params_received = 0;
434 	pw_endpoint_set_param(&d.endpoint.iface, SPA_PARAM_Props, 0,
435 		spa_pod_builder_add_object(&b,
436 				SPA_TYPE_OBJECT_Props, SPA_PARAM_Props,
437 				SPA_PROP_volume, SPA_POD_Float(0.2),
438 				SPA_PROP_mute, SPA_POD_Bool(true)));
439 	pw_main_loop_run(d.loop);
440 	spa_assert_se(d.params_received == 1);
441 	spa_assert_se(d.props.volume > 0.19 && d.props.volume < 0.21);
442 	spa_assert_se(d.props.mute == true);
443 
444 	/* stop exporting and expect to see that reflected on the registry */
445 	pw_proxy_destroy(d.export_proxy);
446 	pw_main_loop_run(d.loop);
447 	spa_assert_se(!d.bound_proxy);
448 
449 	endpoint_clear(&d.endpoint);
450 	pw_proxy_destroy((struct pw_proxy*)d.registry);
451 	pw_context_destroy(d.context);
452 	pw_main_loop_destroy(d.loop);
453 }
454 
main(int argc,char * argv[])455 int main(int argc, char *argv[])
456 {
457 	/* FIXME: This test has a leak and a use of uninitialized buffer
458 	 * that needs to be debugged and fixed (or excluded). Meanwhile -
459 	 * skip it from valgrind so we can at least use the others. */
460 	if (RUNNING_ON_VALGRIND)
461 		return 77;
462 
463 	pw_init(&argc, &argv);
464 
465 	alarm(5); /* watchdog; terminate after 5 seconds */
466 	test_endpoint();
467 
468 	return 0;
469 }
470