summaryrefslogtreecommitdiffstats
path: root/src/modules/module-client-device/protocol-native.c
blob: 2734aaceee73fb5fbf6eadeda31052ded4f2bd1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/* PipeWire
 *
 * Copyright © 2019 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.
 */

#include <errno.h>

#include <spa/pod/builder.h>
#include <spa/pod/parser.h>
#include <spa/utils/result.h>

#include <pipewire/impl.h>

#include <pipewire/extensions/protocol-native.h>

#define MAX_DICT	1024
#define MAX_PARAM_INFO	128

static inline void push_item(struct spa_pod_builder *b, const struct spa_dict_item *item)
{
	const char *str;
	spa_pod_builder_string(b, item->key);
	str = item->value;
	if (spa_strstartswith(str, "pointer:"))
		str = "";
	spa_pod_builder_string(b, str);
}

static inline int parse_item(struct spa_pod_parser *prs, struct spa_dict_item *item)
{
	int res;
	if ((res = spa_pod_parser_get(prs,
		       SPA_POD_String(&item->key),
		       SPA_POD_String(&item->value),
		       NULL)) < 0)
		return res;
	if (spa_strstartswith(item->value, "pointer:"))
		item->value = "";
	return 0;
}

#define parse_dict(prs,d)									\
do {												\
	uint32_t i;										\
	if (spa_pod_parser_get(prs,								\
			 SPA_POD_Int(&(d)->n_items), NULL) < 0)					\
		return -EINVAL;									\
	if ((d)->n_items > 0) {									\
		if ((d)->n_items > MAX_DICT) 							\
			return -ENOSPC;								\
		(d)->items = alloca((d)->n_items * sizeof(struct spa_dict_item));		\
		for (i = 0; i < (d)->n_items; i++) {						\
			if (parse_item(prs, (struct spa_dict_item *) &(d)->items[i]) < 0)	\
				return -EINVAL;							\
		}										\
	}											\
} while(0)

#define parse_param_info(prs,n_params,params)							\
do {												\
	uint32_t i;										\
	if (spa_pod_parser_get(prs,								\
			SPA_POD_Int(&(n_params)), NULL) < 0)					\
		return -EINVAL;									\
	if (n_params > 0) {									\
		if (n_params > MAX_PARAM_INFO)							\
			return -ENOSPC;								\
		params = alloca(n_params * sizeof(struct spa_param_info));			\
		for (i = 0; i < n_params; i++) {						\
			if (spa_pod_parser_get(prs,						\
					SPA_POD_Id(&(params[i]).id),				\
					SPA_POD_Int(&(params[i]).flags), NULL) < 0)		\
				return -EINVAL;							\
		}										\
	}											\
} while(0)

static int device_marshal_add_listener(void *object,
			struct spa_hook *listener,
			const struct spa_device_events *events,
			void *data)
{
	struct pw_resource *resource = object;
	pw_resource_add_object_listener(resource, listener, events, data);
	return 0;
}

static int device_demarshal_add_listener(void *object,
			const struct pw_protocol_native_message *msg)
{
	return -ENOTSUP;
}

static int device_marshal_sync(void *object, int seq)
{
	struct pw_protocol_native_message *msg;
	struct pw_resource *resource = object;
	struct spa_pod_builder *b;

	b = pw_protocol_native_begin_resource(resource, SPA_DEVICE_METHOD_SYNC, &msg);

	spa_pod_builder_add_struct(b,
			SPA_POD_Int(SPA_RESULT_RETURN_ASYNC(msg->seq)));

	return pw_protocol_native_end_resource(resource, b);
}

static int device_demarshal_sync(void *object,
			const struct pw_protocol_native_message *msg)
{
	struct pw_proxy *proxy = object;
	struct spa_pod_parser prs;
	int seq;

	spa_pod_parser_init(&prs, msg->data, msg->size);
	if (spa_pod_parser_get_struct(&prs,
			SPA_POD_Int(&seq)) < 0)
		return -EINVAL;

	pw_proxy_notify(proxy, struct spa_device_methods, sync, 0, seq);
	return 0;
}

static int device_marshal_enum_params(void *object, int seq,
                            uint32_t id, uint32_t index, uint32_t max,
                            const struct spa_pod *filter)
{
	struct pw_protocol_native_message *msg;
	struct pw_resource *resource = object;
	struct spa_pod_builder *b;

	b = pw_protocol_native_begin_resource(resource, SPA_DEVICE_METHOD_ENUM_PARAMS, &msg);

	spa_pod_builder_add_struct(b,
			SPA_POD_Int(SPA_RESULT_RETURN_ASYNC(msg->seq)),
			SPA_POD_Id(id),
			SPA_POD_Int(index),
			SPA_POD_Int(max),
			SPA_POD_Pod(filter));

	return pw_protocol_native_end_resource(resource, b);
}

static int device_demarshal_enum_params(void *object, const struct pw_protocol_native_message *msg)
{
	struct pw_proxy *proxy = object;
	struct spa_pod_parser prs;
	uint32_t id, index, max;
	int seq;
	struct spa_pod *filter;

	spa_pod_parser_init(&prs, msg->data, msg->size);
	if (spa_pod_parser_get_struct(&prs,
			SPA_POD_Int(&seq),
			SPA_POD_Id(&id),
			SPA_POD_Int(&index),
			SPA_POD_Int(&max),
			SPA_POD_Pod(&filter)) < 0)
		return -EINVAL;

	pw_proxy_notify(proxy, struct spa_device_methods, enum_params, 0,
					seq, id, index, max, filter);
	return 0;
}

static int device_marshal_set_param(void *object,
                            uint32_t id, uint32_t flags,
                            const struct spa_pod *param)
{
	struct pw_resource *resource = object;
	struct spa_pod_builder *b;

	b = pw_protocol_native_begin_resource(resource, SPA_DEVICE_METHOD_SET_PARAM, NULL);

	spa_pod_builder_add_struct(b,
			SPA_POD_Id(id),
			SPA_POD_Int(flags),
			SPA_POD_Pod(param));

	return pw_protocol_native_end_resource(resource, b);
}

static int device_demarshal_set_param(void *object, const struct pw_protocol_native_message *msg)
{
	struct pw_proxy *proxy = object;
	struct spa_pod_parser prs;
	uint32_t id, flags;
	struct spa_pod *param;

	spa_pod_parser_init(&prs, msg->data, msg->size);
	if (spa_pod_parser_get_struct(&prs,
			SPA_POD_Id(&id),
			SPA_POD_Int(&flags),
			SPA_POD_Pod(&param)) < 0)
		return -EINVAL;

	pw_proxy_notify(proxy, struct spa_device_methods, set_param, 0,
					id, flags, param);
	return 0;
}

static void device_marshal_info(void *data,
		const struct spa_device_info *info)
{
	struct pw_proxy *proxy = data;
	struct spa_pod_builder *b;
	struct spa_pod_frame f[2];
	uint32_t i, n_items;

	b = pw_protocol_native_begin_proxy(proxy, SPA_DEVICE_EVENT_INFO, NULL);

	spa_pod_builder_push_struct(b, &f[0]);
	if (info) {
		uint64_t change_mask = info->change_mask;

		change_mask &= SPA_DEVICE_CHANGE_MASK_FLAGS |
				SPA_DEVICE_CHANGE_MASK_PROPS |
				SPA_DEVICE_CHANGE_MASK_PARAMS;

		n_items = info->props ? info->props->n_items : 0;

		spa_pod_builder_push_struct(b, &f[1]);
		spa_pod_builder_add(b,
			    SPA_POD_Long(change_mask),
			    SPA_POD_Long(info->flags),
			    SPA_POD_Int(n_items), NULL);
		for (i = 0; i < n_items; i++)
			push_item(b, &info->props->items[i]);
		spa_pod_builder_add(b,
				    SPA_POD_Int(info->n_params), NULL);
		for (i = 0; i < info->n_params; i++) {
			spa_pod_builder_add(b,
					    SPA_POD_Id(info->params[i].id),
					    SPA_POD_Int(info->params[i].flags), NULL);
		}
		spa_pod_builder_pop(b, &f[1]);
	} else {
		spa_pod_builder_add(b,
				SPA_POD_Pod(NULL), NULL);
	}
	spa_pod_builder_pop(b, &f[0]);

	pw_protocol_native_end_proxy(proxy, b);
}

static int device_demarshal_info(void *data,
		const struct pw_protocol_native_message *msg)
{
	struct pw_resource *resource = data;
	struct spa_pod_parser prs;
	struct spa_pod *ipod;
	struct spa_device_info info = SPA_DEVICE_INFO_INIT(), *infop;
	struct spa_dict props = SPA_DICT_INIT(NULL, 0);

	spa_pod_parser_init(&prs, msg->data, msg->size);

	if (spa_pod_parser_get_struct(&prs,
			SPA_POD_PodStruct(&ipod)) < 0)
		return -EINVAL;

	if (ipod) {
		struct spa_pod_parser p2;
		struct spa_pod_frame f2;
		infop = &info;

		spa_pod_parser_pod(&p2, ipod);
		if (spa_pod_parser_push_struct(&p2, &f2) < 0 ||
		    spa_pod_parser_get(&p2,
				SPA_POD_Long(&info.change_mask),
				SPA_POD_Long(&info.flags), NULL) < 0)
			return -EINVAL;

		info.change_mask &= SPA_DEVICE_CHANGE_MASK_FLAGS |
				SPA_DEVICE_CHANGE_MASK_PROPS |
				SPA_DEVICE_CHANGE_MASK_PARAMS;

		parse_dict(&p2, &props);
		if (props.n_items > 0)
			info.props = &props;

		parse_param_info(&p2, info.n_params, info.params);
	}
	else {
		infop = NULL;
	}
	pw_resource_notify(resource, struct spa_device_events, info, 0, infop);
	return 0;
}

static void device_marshal_result(void *data,
		int seq, int res, uint32_t type, const void *result)
{
	struct pw_proxy *proxy = data;
	struct spa_pod_builder *b;
	struct spa_pod_frame f[2];

	b = pw_protocol_native_begin_proxy(proxy, SPA_DEVICE_EVENT_RESULT, NULL);
	spa_pod_builder_push_struct(b, &f[0]);
	spa_pod_builder_add(b,
			    SPA_POD_Int(seq),
			    SPA_POD_Int(res),
			    SPA_POD_Id(type),
			    NULL);

	switch (type) {
	case SPA_RESULT_TYPE_DEVICE_PARAMS:
	{
		const struct spa_result_device_params *r = result;
		spa_pod_builder_add(b,
			    SPA_POD_Id(r->id),
			    SPA_POD_Int(r->index),
			    SPA_POD_Int(r->next),
			    SPA_POD_Pod(r->param),
			    NULL);
		break;
	}
	default:
		break;
	}

	spa_pod_builder_pop(b, &f[0]);

	pw_protocol_native_end_proxy(proxy, b);
}

static int device_demarshal_result(void *data,
		const struct pw_protocol_native_message *msg)
{
	struct pw_resource *resource = data;
	struct spa_pod_parser prs;
	struct spa_pod_frame f[1];
	int seq, res;
	uint32_t type;
	const void *result;
	struct spa_result_device_params params;

	spa_pod_parser_init(&prs, msg->data, msg->size);
	if (spa_pod_parser_push_struct(&prs, &f[0]) < 0 ||
	    spa_pod_parser_get(&prs,
			SPA_POD_Int(&seq),
			SPA_POD_Int(&res),
			SPA_POD_Id(&type),
			NULL) < 0)
		return -EINVAL;

	switch (type) {
	case SPA_RESULT_TYPE_DEVICE_PARAMS:
		if (spa_pod_parser_get(&prs,
				SPA_POD_Id(&params.id),
				SPA_POD_Int(&params.index),
				SPA_POD_Int(&params.next),
				SPA_POD_PodObject(&params.param),
				NULL) < 0)
			return -EINVAL;

		result = &params;
		break;

	default:
		result = NULL;
		break;
	}

	pw_resource_notify(resource, struct spa_device_events, result, 0, seq, res, type, result);
	return 0;
}

static void device_marshal_event(void *data, const struct spa_event *event)
{
	struct pw_proxy *proxy = data;
	struct spa_pod_builder *b;

	b = pw_protocol_native_begin_proxy(proxy, SPA_DEVICE_EVENT_EVENT, NULL);

	spa_pod_builder_add_struct(b,
			    SPA_POD_Pod(event));

	pw_protocol_native_end_proxy(proxy, b);
}

static int device_demarshal_event(void *data,
		const struct pw_protocol_native_message *msg)
{
	struct pw_resource *resource = data;
	struct spa_pod_parser prs;
	struct spa_event *event;

	spa_pod_parser_init(&prs, msg->data, msg->size);
	if (spa_pod_parser_get_struct(&prs,
			SPA_POD_PodObject(&event)) < 0)
		return -EINVAL;

	pw_resource_notify(resource, struct spa_device_events, event, 0, event);
	return 0;
}

static void device_marshal_object_info(void *data, uint32_t id,
                const struct spa_device_object_info *info)
{
	struct pw_proxy *proxy = data;
	struct spa_pod_builder *b;
	struct spa_pod_frame f[2];
	uint32_t i, n_items;

	b = pw_protocol_native_begin_proxy(proxy, SPA_DEVICE_EVENT_OBJECT_INFO, NULL);
	spa_pod_builder_push_struct(b, &f[0]);
	spa_pod_builder_add(b,
			    SPA_POD_Int(id),
			    NULL);
	if (info) {
		uint64_t change_mask = info->change_mask;

		change_mask &= SPA_DEVICE_OBJECT_CHANGE_MASK_FLAGS |
				SPA_DEVICE_OBJECT_CHANGE_MASK_PROPS;

		n_items = info->props ? info->props->n_items : 0;

		spa_pod_builder_push_struct(b, &f[1]);
		spa_pod_builder_add(b,
			    SPA_POD_String(info->type),
			    SPA_POD_Long(change_mask),
			    SPA_POD_Long(info->flags),
			    SPA_POD_Int(n_items), NULL);
		for (i = 0; i < n_items; i++)
			push_item(b, &info->props->items[i]);
		spa_pod_builder_pop(b, &f[1]);
	} else {
		spa_pod_builder_add(b,
				SPA_POD_Pod(NULL), NULL);
	}
	spa_pod_builder_pop(b, &f[0]);

	pw_protocol_native_end_proxy(proxy, b);
}

static int device_demarshal_object_info(void *data,
		const struct pw_protocol_native_message *msg)
{
	struct pw_resource *resource = data;
	struct spa_pod_parser prs;
	struct spa_device_object_info info = SPA_DEVICE_OBJECT_INFO_INIT(), *infop;
	struct spa_pod *ipod;
	struct spa_dict props = SPA_DICT_INIT(NULL, 0);
	uint32_t id;

	spa_pod_parser_init(&prs, msg->data, msg->size);
	if (spa_pod_parser_get_struct(&prs,
			SPA_POD_Int(&id),
			SPA_POD_PodStruct(&ipod)) < 0)
		return -EINVAL;

	if (ipod) {
		struct spa_pod_parser p2;
		struct spa_pod_frame f2;
		infop = &info;

		spa_pod_parser_pod(&p2, ipod);
		if (spa_pod_parser_push_struct(&p2, &f2) < 0 ||
		    spa_pod_parser_get(&p2,
				SPA_POD_String(&info.type),
				SPA_POD_Long(&info.change_mask),
				SPA_POD_Long(&info.flags), NULL) < 0)
			return -EINVAL;

		info.change_mask &= SPA_DEVICE_OBJECT_CHANGE_MASK_FLAGS |
				SPA_DEVICE_CHANGE_MASK_PROPS;

		parse_dict(&p2, &props);
		if (props.n_items > 0)
			info.props = &props;
	} else {
		infop = NULL;
	}

	pw_resource_notify(resource, struct spa_device_events, object_info, 0, id, infop);
	return 0;
}

static const struct spa_device_methods pw_protocol_native_device_method_marshal = {
	SPA_VERSION_DEVICE_METHODS,
	.add_listener = &device_marshal_add_listener,
	.sync = &device_marshal_sync,
	.enum_params = &device_marshal_enum_params,
	.set_param = &device_marshal_set_param
};

static const struct pw_protocol_native_demarshal
pw_protocol_native_device_method_demarshal[SPA_DEVICE_METHOD_NUM] =
{
	[SPA_DEVICE_METHOD_ADD_LISTENER] = { &device_demarshal_add_listener, 0 },
	[SPA_DEVICE_METHOD_SYNC] = { &device_demarshal_sync, 0 },
	[SPA_DEVICE_METHOD_ENUM_PARAMS] = { &device_demarshal_enum_params, 0 },
	[SPA_DEVICE_METHOD_SET_PARAM] = { &device_demarshal_set_param, 0 },
};

static const struct spa_device_events pw_protocol_native_device_event_marshal = {
	SPA_VERSION_DEVICE_EVENTS,
	.info = &device_marshal_info,
	.result = &device_marshal_result,
	.event = &device_marshal_event,
	.object_info = &device_marshal_object_info,
};

static const struct pw_protocol_native_demarshal
pw_protocol_native_device_event_demarshal[SPA_DEVICE_EVENT_NUM] =
{
	[SPA_DEVICE_EVENT_INFO] = { &device_demarshal_info, 0 },
	[SPA_DEVICE_EVENT_RESULT] = { &device_demarshal_result, 0 },
	[SPA_DEVICE_EVENT_EVENT] = { &device_demarshal_event, 0 },
	[SPA_DEVICE_EVENT_OBJECT_INFO] = { &device_demarshal_object_info, 0 },
};

static const struct pw_protocol_marshal pw_protocol_native_client_device_marshal = {
	SPA_TYPE_INTERFACE_Device,
	SPA_VERSION_DEVICE,
	PW_PROTOCOL_MARSHAL_FLAG_IMPL,
	SPA_DEVICE_EVENT_NUM,
	SPA_DEVICE_METHOD_NUM,
	.client_marshal = &pw_protocol_native_device_event_marshal,
	.server_demarshal = pw_protocol_native_device_event_demarshal,
	.server_marshal = &pw_protocol_native_device_method_marshal,
	.client_demarshal = pw_protocol_native_device_method_demarshal,
};

struct pw_protocol *pw_protocol_native_ext_client_device_init(struct pw_context *context)
{
	struct pw_protocol *protocol;

	protocol = pw_context_find_protocol(context, PW_TYPE_INFO_PROTOCOL_Native);

	if (protocol == NULL)
		return NULL;

	pw_protocol_add_marshal(protocol, &pw_protocol_native_client_device_marshal);

	return protocol;
}