summaryrefslogtreecommitdiffstats
path: root/src/modules/module-metadata/protocol-native.c
blob: 54958db21c0f5cb93496a3659f7ea5cbdb852172 (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
/* 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/pipewire.h>

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

static int metadata_resource_marshal_add_listener(void *object,
			struct spa_hook *listener,
			const struct pw_metadata_events *events,
			void *data)
{
	struct pw_resource *resource = object;
	struct spa_pod_builder *b;

	pw_resource_add_object_listener(resource, listener, events, data);

	b = pw_protocol_native_begin_resource(resource, PW_METADATA_METHOD_ADD_LISTENER, NULL);
	return pw_protocol_native_end_resource(resource, b);
}

static int metadata_proxy_marshal_add_listener(void *object,
			struct spa_hook *listener,
			const struct pw_metadata_events *events,
			void *data)
{
	struct pw_proxy *proxy = object;
	pw_proxy_add_object_listener(proxy, listener, events, data);
	return 0;
}

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

static const struct pw_metadata_events pw_protocol_native_metadata_client_event_marshal;

static int metadata_proxy_demarshal_add_listener(void *object,
			const struct pw_protocol_native_message *msg)
{
	struct pw_proxy *proxy = object;
	struct spa_hook listener;
	int res;

	spa_zero(listener);
	res = pw_proxy_notify(proxy, struct pw_metadata_methods, add_listener, 0,
			&listener, &pw_protocol_native_metadata_client_event_marshal, object);
	spa_hook_remove(&listener);

	return res;
}

static void metadata_marshal_set_property(struct spa_pod_builder *b, uint32_t subject,
		const char *key, const char *type, const char *value)
{
	spa_pod_builder_add_struct(b,
			SPA_POD_Int(subject),
			SPA_POD_String(key),
			SPA_POD_String(type),
			SPA_POD_String(value));
}

static int metadata_proxy_marshal_set_property(void *object, uint32_t subject,
		const char *key, const char *type, const char *value)
{
	struct pw_proxy *proxy = object;
	struct spa_pod_builder *b;
	b = pw_protocol_native_begin_proxy(proxy, PW_METADATA_METHOD_SET_PROPERTY, NULL);
	metadata_marshal_set_property(b, subject, key, type, value);
	return pw_protocol_native_end_proxy(proxy, b);
}
static int metadata_resource_marshal_set_property(void *object, uint32_t subject,
		const char *key, const char *type, const char *value)
{
	struct pw_resource *resource = object;
	struct spa_pod_builder *b;
	b = pw_protocol_native_begin_resource(resource, PW_METADATA_METHOD_SET_PROPERTY, NULL);
	metadata_marshal_set_property(b, subject, key, type, value);
	return pw_protocol_native_end_resource(resource, b);
}

static int metadata_demarshal_set_property(struct spa_pod_parser *prs, uint32_t *subject,
		char **key, char **type, char **value)
{
	return spa_pod_parser_get_struct(prs,
			SPA_POD_Int(subject),
			SPA_POD_String(key),
			SPA_POD_String(type),
			SPA_POD_String(value));
}

static int metadata_proxy_demarshal_set_property(void *object,
			const struct pw_protocol_native_message *msg)
{
	struct pw_proxy *proxy = object;
	struct spa_pod_parser prs;
	uint32_t subject;
	char *key, *type, *value;

	spa_pod_parser_init(&prs, msg->data, msg->size);
	if (metadata_demarshal_set_property(&prs, &subject, &key, &type, &value) < 0)
		return -EINVAL;
	return pw_proxy_notify(proxy, struct pw_metadata_methods, set_property, 0, subject, key, type, value);
}

static int metadata_resource_demarshal_set_property(void *object,
			const struct pw_protocol_native_message *msg)
{
	struct pw_resource *resource = object;
	struct spa_pod_parser prs;
	uint32_t subject;
	char *key, *type, *value;

	spa_pod_parser_init(&prs, msg->data, msg->size);
	if (metadata_demarshal_set_property(&prs, &subject, &key, &type, &value) < 0)
		return -EINVAL;
	return pw_resource_notify(resource, struct pw_metadata_methods, set_property, 0, subject, key, type, value);
}

static void metadata_marshal_clear(struct spa_pod_builder *b)
{
	spa_pod_builder_add_struct(b, SPA_POD_None());
}

static int metadata_proxy_marshal_clear(void *object)
{
	struct pw_proxy *proxy = object;
	struct spa_pod_builder *b;
	b = pw_protocol_native_begin_proxy(proxy, PW_METADATA_METHOD_CLEAR, NULL);
	metadata_marshal_clear(b);
	return pw_protocol_native_end_proxy(proxy, b);
}
static int metadata_resource_marshal_clear(void *object)
{
	struct pw_resource *resource = object;
	struct spa_pod_builder *b;
	b = pw_protocol_native_begin_resource(resource, PW_METADATA_METHOD_CLEAR, NULL);
	metadata_marshal_clear(b);
	return pw_protocol_native_end_resource(resource, b);
}

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

	spa_pod_parser_init(&prs, msg->data, msg->size);
	if (spa_pod_parser_get_struct(&prs, SPA_POD_None()) < 0)
		return -EINVAL;
	pw_proxy_notify(proxy, struct pw_metadata_methods, clear, 0);
	return 0;
}

static int metadata_resource_demarshal_clear(void *object, const struct pw_protocol_native_message *msg)
{
	struct pw_resource *resource = object;
	struct spa_pod_parser prs;

	spa_pod_parser_init(&prs, msg->data, msg->size);
	if (spa_pod_parser_get_struct(&prs, SPA_POD_None()) < 0)
		return -EINVAL;
	pw_resource_notify(resource, struct pw_metadata_methods, clear, 0);
	return 0;
}

static void metadata_marshal_property(struct spa_pod_builder *b, uint32_t subject,
		const char *key, const char *type, const char *value)
{
	spa_pod_builder_add_struct(b,
			SPA_POD_Int(subject),
			SPA_POD_String(key),
			SPA_POD_String(type),
			SPA_POD_String(value));
}

static int metadata_proxy_marshal_property(void *object, uint32_t subject,
		const char *key, const char *type, const char *value)
{
	struct pw_proxy *proxy = object;
	struct spa_pod_builder *b;
	b = pw_protocol_native_begin_proxy(proxy, PW_METADATA_EVENT_PROPERTY, NULL);
	metadata_marshal_property(b, subject, key, type, value);
	return pw_protocol_native_end_proxy(proxy, b);
}

static int metadata_resource_marshal_property(void *object, uint32_t subject,
		const char *key, const char *type, const char *value)
{
	struct pw_resource *resource = object;
	struct spa_pod_builder *b;
	b = pw_protocol_native_begin_resource(resource, PW_METADATA_EVENT_PROPERTY, NULL);
	metadata_marshal_property(b, subject, key, type, value);
	return pw_protocol_native_end_resource(resource, b);
}

static int metadata_demarshal_property(struct spa_pod_parser *prs,
		uint32_t *subject, char **key, char **type, char **value)
{
	return spa_pod_parser_get_struct(prs,
			SPA_POD_Int(subject),
			SPA_POD_String(key),
			SPA_POD_String(type),
			SPA_POD_String(value));
}

static int metadata_proxy_demarshal_property(void *object,
		const struct pw_protocol_native_message *msg)
{
	struct pw_proxy *proxy = object;
	struct spa_pod_parser prs;
	uint32_t subject;
	char *key, *type, *value;

	spa_pod_parser_init(&prs, msg->data, msg->size);
	if (metadata_demarshal_property(&prs,
				&subject, &key, &type, &value) < 0)
		return -EINVAL;
	pw_proxy_notify(proxy, struct pw_metadata_events, property, 0, subject, key, type, value);
	return 0;
}

static int metadata_resource_demarshal_property(void *object,
		const struct pw_protocol_native_message *msg)
{
	struct pw_resource *resource = object;
	struct spa_pod_parser prs;
	uint32_t subject;
	char *key, *type, *value;

	spa_pod_parser_init(&prs, msg->data, msg->size);
	if (metadata_demarshal_property(&prs,
				&subject, &key, &type, &value) < 0)
		return -EINVAL;
	pw_resource_notify(resource, struct pw_metadata_events, property, 0, subject, key, type, value);
	return 0;
}

static const struct pw_metadata_methods pw_protocol_native_metadata_client_method_marshal = {
	PW_VERSION_METADATA_METHODS,
	.add_listener = &metadata_proxy_marshal_add_listener,
	.set_property = &metadata_proxy_marshal_set_property,
	.clear = &metadata_proxy_marshal_clear,
};
static const struct pw_metadata_methods pw_protocol_native_metadata_server_method_marshal = {
	PW_VERSION_METADATA_METHODS,
	.add_listener = &metadata_resource_marshal_add_listener,
	.set_property = &metadata_resource_marshal_set_property,
	.clear = &metadata_resource_marshal_clear,
};

static const struct pw_protocol_native_demarshal
pw_protocol_native_metadata_client_method_demarshal[PW_METADATA_METHOD_NUM] =
{
	[PW_METADATA_METHOD_ADD_LISTENER] = { &metadata_proxy_demarshal_add_listener, 0 },
	[PW_METADATA_METHOD_SET_PROPERTY] = { &metadata_proxy_demarshal_set_property, PW_PERM_W },
	[PW_METADATA_METHOD_CLEAR] = { &metadata_proxy_demarshal_clear, PW_PERM_W },
};

static const struct pw_protocol_native_demarshal
pw_protocol_native_metadata_server_method_demarshal[PW_METADATA_METHOD_NUM] =
{
	[PW_METADATA_METHOD_ADD_LISTENER] = { &metadata_resource_demarshal_add_listener, 0 },
	[PW_METADATA_METHOD_SET_PROPERTY] = { &metadata_resource_demarshal_set_property, PW_PERM_W },
	[PW_METADATA_METHOD_CLEAR] = { &metadata_resource_demarshal_clear, PW_PERM_W },
};

static const struct pw_metadata_events pw_protocol_native_metadata_client_event_marshal = {
	PW_VERSION_METADATA_EVENTS,
	.property = &metadata_proxy_marshal_property,
};

static const struct pw_metadata_events pw_protocol_native_metadata_server_event_marshal = {
	PW_VERSION_METADATA_EVENTS,
	.property = &metadata_resource_marshal_property,
};

static const struct pw_protocol_native_demarshal
pw_protocol_native_metadata_client_event_demarshal[PW_METADATA_EVENT_NUM] =
{
	[PW_METADATA_EVENT_PROPERTY] = { &metadata_proxy_demarshal_property, 0 },
};

static const struct pw_protocol_native_demarshal
pw_protocol_native_metadata_server_event_demarshal[PW_METADATA_EVENT_NUM] =
{
	[PW_METADATA_EVENT_PROPERTY] = { &metadata_resource_demarshal_property, 0 },
};

static const struct pw_protocol_marshal pw_protocol_native_metadata_marshal = {
	PW_TYPE_INTERFACE_Metadata,
	PW_VERSION_METADATA,
	0,
	PW_METADATA_METHOD_NUM,
	PW_METADATA_EVENT_NUM,
	.client_marshal = &pw_protocol_native_metadata_client_method_marshal,
	.server_demarshal = pw_protocol_native_metadata_server_method_demarshal,
	.server_marshal = &pw_protocol_native_metadata_server_event_marshal,
	.client_demarshal = pw_protocol_native_metadata_client_event_demarshal,
};

static const struct pw_protocol_marshal pw_protocol_native_metadata_impl_marshal = {
	PW_TYPE_INTERFACE_Metadata,
	PW_VERSION_METADATA,
	PW_PROTOCOL_MARSHAL_FLAG_IMPL,
	PW_METADATA_EVENT_NUM,
	PW_METADATA_METHOD_NUM,
	.client_marshal = &pw_protocol_native_metadata_client_event_marshal,
	.server_demarshal = pw_protocol_native_metadata_server_event_demarshal,
	.server_marshal = &pw_protocol_native_metadata_server_method_marshal,
	.client_demarshal = pw_protocol_native_metadata_client_method_demarshal,
};

int pw_protocol_native_ext_metadata_init(struct pw_context *context)
{
	struct pw_protocol *protocol;

	protocol = pw_context_find_protocol(context, PW_TYPE_INFO_PROTOCOL_Native);
	if (protocol == NULL)
		return -EPROTO;

	pw_protocol_add_marshal(protocol, &pw_protocol_native_metadata_marshal);
	pw_protocol_add_marshal(protocol, &pw_protocol_native_metadata_impl_marshal);
	return 0;
}