1 /* PipeWire
2  *
3  * Copyright © 2018 Wim Taymans
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <string.h>
26 
27 #include <spa/pod/builder.h>
28 
29 #include "pipewire/pipewire.h"
30 
31 #include "pipewire/core.h"
32 
33 SPA_EXPORT
pw_node_state_as_string(enum pw_node_state state)34 const char *pw_node_state_as_string(enum pw_node_state state)
35 {
36 	switch (state) {
37 	case PW_NODE_STATE_ERROR:
38 		return "error";
39 	case PW_NODE_STATE_CREATING:
40 		return "creating";
41 	case PW_NODE_STATE_SUSPENDED:
42 		return "suspended";
43 	case PW_NODE_STATE_IDLE:
44 		return "idle";
45 	case PW_NODE_STATE_RUNNING:
46 		return "running";
47 	}
48 	return "invalid-state";
49 }
50 
51 SPA_EXPORT
pw_direction_as_string(enum pw_direction direction)52 const char *pw_direction_as_string(enum pw_direction direction)
53 {
54 	switch (direction) {
55 	case PW_DIRECTION_INPUT:
56 		return "input";
57 	case PW_DIRECTION_OUTPUT:
58 		return "output";
59 	}
60 	return "invalid";
61 }
62 
63 SPA_EXPORT
pw_link_state_as_string(enum pw_link_state state)64 const char *pw_link_state_as_string(enum pw_link_state state)
65 {
66 	switch (state) {
67 	case PW_LINK_STATE_ERROR:
68 		return "error";
69 	case PW_LINK_STATE_UNLINKED:
70 		return "unlinked";
71 	case PW_LINK_STATE_INIT:
72 		return "init";
73 	case PW_LINK_STATE_NEGOTIATING:
74 		return "negotiating";
75 	case PW_LINK_STATE_ALLOCATING:
76 		return "allocating";
77 	case PW_LINK_STATE_PAUSED:
78 		return "paused";
79 	case PW_LINK_STATE_ACTIVE:
80 		return "active";
81 	}
82 	return "invalid-state";
83 }
84 
pw_spa_dict_destroy(struct spa_dict * dict)85 static void pw_spa_dict_destroy(struct spa_dict *dict)
86 {
87 	const struct spa_dict_item *item;
88 
89 	spa_dict_for_each(item, dict) {
90 		free((void *) item->key);
91 		free((void *) item->value);
92 	}
93 	free((void*)dict->items);
94 	free(dict);
95 }
96 
pw_spa_dict_copy(struct spa_dict * dict)97 static struct spa_dict *pw_spa_dict_copy(struct spa_dict *dict)
98 {
99 	struct spa_dict *copy;
100 	struct spa_dict_item *items;
101 	uint32_t i;
102 
103 	if (dict == NULL)
104 		return NULL;
105 
106 	copy = calloc(1, sizeof(struct spa_dict));
107 	if (copy == NULL)
108 		goto no_mem;
109 	copy->items = items = calloc(dict->n_items, sizeof(struct spa_dict_item));
110 	if (copy->items == NULL)
111 		goto no_items;
112 	copy->n_items = dict->n_items;
113 
114 	for (i = 0; i < dict->n_items; i++) {
115 		items[i].key = strdup(dict->items[i].key);
116 		items[i].value = dict->items[i].value ? strdup(dict->items[i].value) : NULL;
117 	}
118 	return copy;
119 
120       no_items:
121 	free(copy);
122       no_mem:
123 	return NULL;
124 }
125 
126 SPA_EXPORT
pw_core_info_merge(struct pw_core_info * info,const struct pw_core_info * update,bool reset)127 struct pw_core_info *pw_core_info_merge(struct pw_core_info *info,
128 		const struct pw_core_info *update, bool reset)
129 {
130 	if (update == NULL)
131 		return info;
132 
133 	if (info == NULL) {
134 		info = calloc(1, sizeof(struct pw_core_info));
135 		if (info == NULL)
136 			return NULL;
137 
138 		info->id = update->id;
139 		info->cookie = update->cookie;
140 		info->user_name = update->user_name ? strdup(update->user_name) : NULL;
141 		info->host_name = update->host_name ? strdup(update->host_name) : NULL;
142 		info->version = update->version ? strdup(update->version) : NULL;
143 		info->name = update->name ? strdup(update->name) : NULL;
144 	}
145 	if (reset)
146 		info->change_mask = 0;
147 	info->change_mask |= update->change_mask;
148 
149 	if (update->change_mask & PW_CORE_CHANGE_MASK_PROPS) {
150 		if (info->props)
151 			pw_spa_dict_destroy(info->props);
152 		info->props = pw_spa_dict_copy(update->props);
153 	}
154 	return info;
155 }
156 
157 SPA_EXPORT
pw_core_info_update(struct pw_core_info * info,const struct pw_core_info * update)158 struct pw_core_info *pw_core_info_update(struct pw_core_info *info,
159 		const struct pw_core_info *update)
160 {
161 	return pw_core_info_merge(info, update, true);
162 }
163 
164 SPA_EXPORT
pw_core_info_free(struct pw_core_info * info)165 void pw_core_info_free(struct pw_core_info *info)
166 {
167 	free((void *) info->user_name);
168 	free((void *) info->host_name);
169 	free((void *) info->version);
170 	free((void *) info->name);
171 	if (info->props)
172 		pw_spa_dict_destroy(info->props);
173 	free(info);
174 }
175 
176 SPA_EXPORT
pw_node_info_merge(struct pw_node_info * info,const struct pw_node_info * update,bool reset)177 struct pw_node_info *pw_node_info_merge(struct pw_node_info *info,
178 		const struct pw_node_info *update, bool reset)
179 {
180 	if (update == NULL)
181 		return info;
182 
183 	if (info == NULL) {
184 		info = calloc(1, sizeof(struct pw_node_info));
185 		if (info == NULL)
186 			return NULL;
187 
188 		info->id = update->id;
189 		info->max_input_ports = update->max_input_ports;
190 		info->max_output_ports = update->max_output_ports;
191 	}
192 	if (reset)
193 		info->change_mask = 0;
194 	info->change_mask |= update->change_mask;
195 
196 	if (update->change_mask & PW_NODE_CHANGE_MASK_INPUT_PORTS) {
197 		info->n_input_ports = update->n_input_ports;
198 	}
199 	if (update->change_mask & PW_NODE_CHANGE_MASK_OUTPUT_PORTS) {
200 		info->n_output_ports = update->n_output_ports;
201 	}
202 	if (update->change_mask & PW_NODE_CHANGE_MASK_STATE) {
203 		info->state = update->state;
204 		free((void *) info->error);
205 		info->error = update->error ? strdup(update->error) : NULL;
206 	}
207 	if (update->change_mask & PW_NODE_CHANGE_MASK_PROPS) {
208 		if (info->props)
209 			pw_spa_dict_destroy(info->props);
210 		info->props = pw_spa_dict_copy(update->props);
211 	}
212 	if (update->change_mask & PW_NODE_CHANGE_MASK_PARAMS) {
213 		uint32_t i, user, n_params = update->n_params;
214 
215 		info->params = realloc(info->params, n_params * sizeof(struct spa_param_info));
216 		if (info->params == NULL)
217 			n_params = 0;
218 
219 		for (i = 0; i < SPA_MIN(info->n_params, n_params); i++) {
220 			user = reset ? 0 : info->params[i].user;
221 			if (info->params[i].flags != update->params[i].flags)
222 				user++;
223 			info->params[i] = update->params[i];
224 			info->params[i].user = user;
225 		}
226 		info->n_params = n_params;
227 		for (; i < info->n_params; i++) {
228 			info->params[i] = update->params[i];
229 			info->params[i].user = 1;
230 		}
231 	}
232 	return info;
233 }
234 
235 SPA_EXPORT
pw_node_info_update(struct pw_node_info * info,const struct pw_node_info * update)236 struct pw_node_info *pw_node_info_update(struct pw_node_info *info,
237 		const struct pw_node_info *update)
238 {
239 	return pw_node_info_merge(info, update, true);
240 }
241 
242 SPA_EXPORT
pw_node_info_free(struct pw_node_info * info)243 void pw_node_info_free(struct pw_node_info *info)
244 {
245 	free((void *) info->error);
246 	if (info->props)
247 		pw_spa_dict_destroy(info->props);
248 	free((void *) info->params);
249 	free(info);
250 }
251 
252 SPA_EXPORT
pw_port_info_merge(struct pw_port_info * info,const struct pw_port_info * update,bool reset)253 struct pw_port_info *pw_port_info_merge(struct pw_port_info *info,
254 		const struct pw_port_info *update, bool reset)
255 {
256 
257 	if (update == NULL)
258 		return info;
259 
260 	if (info == NULL) {
261 		info = calloc(1, sizeof(struct pw_port_info));
262 		if (info == NULL)
263 			return NULL;
264 
265 		info->id = update->id;
266 		info->direction = update->direction;
267 	}
268 	if (reset)
269 		info->change_mask = 0;
270 	info->change_mask |= update->change_mask;
271 
272 	if (update->change_mask & PW_PORT_CHANGE_MASK_PROPS) {
273 		if (info->props)
274 			pw_spa_dict_destroy(info->props);
275 		info->props = pw_spa_dict_copy(update->props);
276 	}
277 	if (update->change_mask & PW_PORT_CHANGE_MASK_PARAMS) {
278 		uint32_t i, user, n_params = update->n_params;
279 
280 		info->params = realloc(info->params, n_params * sizeof(struct spa_param_info));
281 		if (info->params == NULL)
282 			n_params = 0;
283 
284 		for (i = 0; i < SPA_MIN(info->n_params, n_params); i++) {
285 			user = reset ? 0 : info->params[i].user;
286 			if (info->params[i].flags != update->params[i].flags)
287 				user++;
288 			info->params[i] = update->params[i];
289 			info->params[i].user = user;
290 		}
291 		info->n_params = n_params;
292 		for (; i < info->n_params; i++) {
293 			info->params[i] = update->params[i];
294 			info->params[i].user = 1;
295 		}
296 	}
297 	return info;
298 }
299 
300 SPA_EXPORT
pw_port_info_update(struct pw_port_info * info,const struct pw_port_info * update)301 struct pw_port_info *pw_port_info_update(struct pw_port_info *info,
302 		const struct pw_port_info *update)
303 {
304 	return pw_port_info_merge(info, update, true);
305 }
306 
307 SPA_EXPORT
pw_port_info_free(struct pw_port_info * info)308 void pw_port_info_free(struct pw_port_info *info)
309 {
310 	if (info->props)
311 		pw_spa_dict_destroy(info->props);
312 	free((void *) info->params);
313 	free(info);
314 }
315 
316 SPA_EXPORT
pw_factory_info_merge(struct pw_factory_info * info,const struct pw_factory_info * update,bool reset)317 struct pw_factory_info *pw_factory_info_merge(struct pw_factory_info *info,
318 		const struct pw_factory_info *update, bool reset)
319 {
320 	if (update == NULL)
321 		return info;
322 
323 	if (info == NULL) {
324 		info = calloc(1, sizeof(struct pw_factory_info));
325 		if (info == NULL)
326 			return NULL;
327 
328 		info->id = update->id;
329 		info->name = update->name ? strdup(update->name) : NULL;
330 		info->type = update->type ? strdup(update->type) : NULL;
331 		info->version = update->version;
332 	}
333 	if (reset)
334 		info->change_mask = 0;
335 	info->change_mask |= update->change_mask;
336 
337 	if (update->change_mask & PW_FACTORY_CHANGE_MASK_PROPS) {
338 		if (info->props)
339 			pw_spa_dict_destroy(info->props);
340 		info->props = pw_spa_dict_copy(update->props);
341 	}
342 	return info;
343 }
344 
345 SPA_EXPORT
pw_factory_info_update(struct pw_factory_info * info,const struct pw_factory_info * update)346 struct pw_factory_info *pw_factory_info_update(struct pw_factory_info *info,
347 					       const struct pw_factory_info *update)
348 {
349 	return pw_factory_info_merge(info, update, true);
350 }
351 
352 SPA_EXPORT
pw_factory_info_free(struct pw_factory_info * info)353 void pw_factory_info_free(struct pw_factory_info *info)
354 {
355 	free((void *) info->name);
356 	free((void *) info->type);
357 	if (info->props)
358 		pw_spa_dict_destroy(info->props);
359 	free(info);
360 }
361 
362 SPA_EXPORT
pw_module_info_merge(struct pw_module_info * info,const struct pw_module_info * update,bool reset)363 struct pw_module_info *pw_module_info_merge(struct pw_module_info *info,
364 		const struct pw_module_info *update, bool reset)
365 {
366 	if (update == NULL)
367 		return info;
368 
369 	if (info == NULL) {
370 		info = calloc(1, sizeof(struct pw_module_info));
371 		if (info == NULL)
372 			return NULL;
373 
374 		info->id = update->id;
375 		info->name = update->name ? strdup(update->name) : NULL;
376 		info->filename = update->filename ? strdup(update->filename) : NULL;
377 		info->args = update->args ? strdup(update->args) : NULL;
378 	}
379 	if (reset)
380 		info->change_mask = 0;
381 	info->change_mask |= update->change_mask;
382 
383 	if (update->change_mask & PW_MODULE_CHANGE_MASK_PROPS) {
384 		if (info->props)
385 			pw_spa_dict_destroy(info->props);
386 		info->props = pw_spa_dict_copy(update->props);
387 	}
388 	return info;
389 }
390 
391 SPA_EXPORT
pw_module_info_update(struct pw_module_info * info,const struct pw_module_info * update)392 struct pw_module_info *pw_module_info_update(struct pw_module_info *info,
393 		const struct pw_module_info *update)
394 {
395 	return pw_module_info_merge(info, update, true);
396 }
397 
398 SPA_EXPORT
pw_module_info_free(struct pw_module_info * info)399 void pw_module_info_free(struct pw_module_info *info)
400 {
401 	free((void *) info->name);
402 	free((void *) info->filename);
403 	free((void *) info->args);
404 	if (info->props)
405 		pw_spa_dict_destroy(info->props);
406 	free(info);
407 }
408 
409 SPA_EXPORT
pw_device_info_merge(struct pw_device_info * info,const struct pw_device_info * update,bool reset)410 struct pw_device_info *pw_device_info_merge(struct pw_device_info *info,
411 		const struct pw_device_info *update, bool reset)
412 {
413 	if (update == NULL)
414 		return info;
415 
416 	if (info == NULL) {
417 		info = calloc(1, sizeof(struct pw_device_info));
418 		if (info == NULL)
419 			return NULL;
420 
421 		info->id = update->id;
422 	}
423 	if (reset)
424 		info->change_mask = 0;
425 	info->change_mask |= update->change_mask;
426 
427 	if (update->change_mask & PW_DEVICE_CHANGE_MASK_PROPS) {
428 		if (info->props)
429 			pw_spa_dict_destroy(info->props);
430 		info->props = pw_spa_dict_copy(update->props);
431 	}
432 	if (update->change_mask & PW_DEVICE_CHANGE_MASK_PARAMS) {
433 		uint32_t i, user, n_params = update->n_params;
434 
435 		info->params = realloc(info->params, n_params * sizeof(struct spa_param_info));
436 		if (info->params == NULL)
437 			n_params = 0;
438 
439 		for (i = 0; i < SPA_MIN(info->n_params, n_params); i++) {
440 			user = reset ? 0 : info->params[i].user;
441 			if (info->params[i].flags != update->params[i].flags)
442 				user++;
443 			info->params[i] = update->params[i];
444 			info->params[i].user = user;
445 		}
446 		info->n_params = n_params;
447 		for (; i < info->n_params; i++) {
448 			info->params[i] = update->params[i];
449 			info->params[i].user = 1;
450 		}
451 	}
452 	return info;
453 }
454 
455 SPA_EXPORT
pw_device_info_update(struct pw_device_info * info,const struct pw_device_info * update)456 struct pw_device_info *pw_device_info_update(struct pw_device_info *info,
457 		const struct pw_device_info *update)
458 {
459 	return pw_device_info_merge(info, update, true);
460 }
461 
462 SPA_EXPORT
pw_device_info_free(struct pw_device_info * info)463 void pw_device_info_free(struct pw_device_info *info)
464 {
465 	if (info->props)
466 		pw_spa_dict_destroy(info->props);
467 	free((void *) info->params);
468 	free(info);
469 }
470 
471 SPA_EXPORT
pw_client_info_merge(struct pw_client_info * info,const struct pw_client_info * update,bool reset)472 struct pw_client_info *pw_client_info_merge(struct pw_client_info *info,
473 		const struct pw_client_info *update, bool reset)
474 {
475 	if (update == NULL)
476 		return info;
477 
478 	if (info == NULL) {
479 		info = calloc(1, sizeof(struct pw_client_info));
480 		if (info == NULL)
481 			return NULL;
482 
483 		info->id = update->id;
484 	}
485 	if (reset)
486 		info->change_mask = 0;
487 	info->change_mask |= update->change_mask;
488 
489 	if (update->change_mask & PW_CLIENT_CHANGE_MASK_PROPS) {
490 		if (info->props)
491 			pw_spa_dict_destroy(info->props);
492 		info->props = pw_spa_dict_copy(update->props);
493 	}
494 	return info;
495 }
496 
497 SPA_EXPORT
pw_client_info_update(struct pw_client_info * info,const struct pw_client_info * update)498 struct pw_client_info *pw_client_info_update(struct pw_client_info *info,
499 		const struct pw_client_info *update)
500 {
501 	return pw_client_info_merge(info, update, true);
502 }
503 
504 SPA_EXPORT
pw_client_info_free(struct pw_client_info * info)505 void pw_client_info_free(struct pw_client_info *info)
506 {
507 	if (info->props)
508 		pw_spa_dict_destroy(info->props);
509 	free(info);
510 }
511 
512 SPA_EXPORT
pw_link_info_merge(struct pw_link_info * info,const struct pw_link_info * update,bool reset)513 struct pw_link_info *pw_link_info_merge(struct pw_link_info *info,
514 		const struct pw_link_info *update, bool reset)
515 {
516 	if (update == NULL)
517 		return info;
518 
519 	if (info == NULL) {
520 		info = calloc(1, sizeof(struct pw_link_info));
521 		if (info == NULL)
522 			return NULL;
523 
524 		info->id = update->id;
525 		info->output_node_id = update->output_node_id;
526 		info->output_port_id = update->output_port_id;
527 		info->input_node_id = update->input_node_id;
528 		info->input_port_id = update->input_port_id;
529 	}
530 	if (reset)
531 		info->change_mask = 0;
532 	info->change_mask |= update->change_mask;
533 
534 	if (update->change_mask & PW_LINK_CHANGE_MASK_STATE) {
535 		info->state = update->state;
536 		free((void *) info->error);
537 		info->error = update->error ? strdup(update->error) : NULL;
538 	}
539 	if (update->change_mask & PW_LINK_CHANGE_MASK_FORMAT) {
540 		free(info->format);
541 		info->format = update->format ? spa_pod_copy(update->format) : NULL;
542 	}
543 	if (update->change_mask & PW_LINK_CHANGE_MASK_PROPS) {
544 		if (info->props)
545 			pw_spa_dict_destroy(info->props);
546 		info->props = pw_spa_dict_copy(update->props);
547 	}
548 	return info;
549 }
550 
551 SPA_EXPORT
pw_link_info_update(struct pw_link_info * info,const struct pw_link_info * update)552 struct pw_link_info *pw_link_info_update(struct pw_link_info *info,
553 		const struct pw_link_info *update)
554 {
555 	return pw_link_info_merge(info, update, true);
556 }
557 
558 SPA_EXPORT
pw_link_info_free(struct pw_link_info * info)559 void pw_link_info_free(struct pw_link_info *info)
560 {
561 	free((void *) info->error);
562 	free(info->format);
563 	if (info->props)
564 		pw_spa_dict_destroy(info->props);
565 	free(info);
566 }
567