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
|
/* PipeWire
*
* Copyright © 2019 Collabora Ltd.
* @author George Kiagiadakis <george.kiagiadakis@collabora.com>
*
* 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 <stdbool.h>
#include <string.h>
#include <pipewire/pipewire.h>
#include <extensions/session-manager.h>
#include <spa/pod/filter.h>
#include "endpoint.h"
#include "client-endpoint.h"
#include <pipewire/private.h>
#define NAME "endpoint"
struct resource_data {
struct endpoint *endpoint;
struct spa_hook resource_listener;
struct spa_hook object_listener;
uint32_t n_subscribe_ids;
uint32_t subscribe_ids[32];
};
#define pw_endpoint_resource(r,m,v,...) \
pw_resource_call(r,struct pw_endpoint_proxy_events,m,v,__VA_ARGS__)
#define pw_endpoint_resource_info(r,...) \
pw_endpoint_resource(r,info,0,__VA_ARGS__)
#define pw_endpoint_resource_param(r,...) \
pw_endpoint_resource(r,param,0,__VA_ARGS__)
static int endpoint_enum_params (void *object, int seq,
uint32_t id, uint32_t start, uint32_t num,
const struct spa_pod *filter)
{
struct pw_resource *resource = object;
struct resource_data *data = pw_resource_get_user_data(resource);
struct endpoint *this = data->endpoint;
struct spa_pod *result;
struct spa_pod *param;
uint8_t buffer[1024];
struct spa_pod_builder b = { 0 };
uint32_t index;
uint32_t next = start;
uint32_t count = 0;
pw_log_debug(NAME" %p: param %u %d/%d", this, id, start, num);
while (true) {
index = next++;
if (index >= this->n_params)
break;
param = this->params[index];
if (param == NULL || !spa_pod_is_object_id(param, id))
continue;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
if (spa_pod_filter(&b, &result, param, filter) != 0)
continue;
pw_log_debug(NAME" %p: %d param %u", this, seq, index);
pw_endpoint_resource_param(resource, seq, id, index, next, result);
if (++count == num)
break;
}
return 0;
}
static int endpoint_subscribe_params (void *object, uint32_t *ids, uint32_t n_ids)
{
struct pw_resource *resource = object;
struct resource_data *data = pw_resource_get_user_data(resource);
uint32_t i;
n_ids = SPA_MIN(n_ids, SPA_N_ELEMENTS(data->subscribe_ids));
data->n_subscribe_ids = n_ids;
for (i = 0; i < n_ids; i++) {
data->subscribe_ids[i] = ids[i];
pw_log_debug(NAME" %p: resource %d subscribe param %u",
data->endpoint, resource->id, ids[i]);
endpoint_enum_params(resource, 1, ids[i], 0, UINT32_MAX, NULL);
}
return 0;
}
static int endpoint_set_param (void *object, uint32_t id, uint32_t flags,
const struct spa_pod *param)
{
struct pw_resource *resource = object;
struct resource_data *data = pw_resource_get_user_data(resource);
struct endpoint *this = data->endpoint;
pw_log_debug("%p", this);
pw_client_endpoint_resource_set_param(this->client_ep->resource,
id, flags, param);
return 0;
}
static int endpoint_create_link(void *object, const struct spa_dict *props)
{
struct pw_resource *resource = object;
struct resource_data *data = pw_resource_get_user_data(resource);
struct endpoint *this = data->endpoint;
pw_log_debug("%p", this);
pw_client_endpoint_resource_create_link(this->client_ep->resource,
props);
return 0;
}
static const struct pw_endpoint_proxy_methods methods = {
PW_VERSION_ENDPOINT_PROXY_METHODS,
.subscribe_params = endpoint_subscribe_params,
.enum_params = endpoint_enum_params,
.set_param = endpoint_set_param,
.create_link = endpoint_create_link,
};
static void endpoint_notify_subscribed(struct endpoint *this,
uint32_t index, uint32_t next)
{
struct pw_global *global = this->global;
struct pw_resource *resource;
struct resource_data *data;
struct spa_pod *param = this->params[index];
uint32_t id;
uint32_t i;
if (!param || !spa_pod_is_object (param))
return;
id = SPA_POD_OBJECT_ID (param);
spa_list_for_each(resource, &global->resource_list, link) {
data = pw_resource_get_user_data(resource);
for (i = 0; i < data->n_subscribe_ids; i++) {
if (data->subscribe_ids[i] == id) {
pw_endpoint_resource_param(resource, 1, id,
index, next, param);
}
}
}
}
int endpoint_update(struct endpoint *this,
uint32_t change_mask,
uint32_t n_params,
const struct spa_pod **params,
const struct pw_endpoint_info *info)
{
if (change_mask & PW_CLIENT_ENDPOINT_UPDATE_PARAMS) {
uint32_t i;
size_t size = n_params * sizeof(struct spa_pod *);
pw_log_debug(NAME" %p: update %d params", this, n_params);
for (i = 0; i < this->n_params; i++)
free(this->params[i]);
this->params = realloc(this->params, size);
if (size > 0 && !this->params) {
this->n_params = 0;
goto no_mem;
}
this->n_params = n_params;
for (i = 0; i < this->n_params; i++) {
this->params[i] = params[i] ? spa_pod_copy(params[i]) : NULL;
endpoint_notify_subscribed(this, i, i+1);
}
}
if (change_mask & PW_CLIENT_ENDPOINT_UPDATE_INFO) {
struct pw_resource *resource;
if (info->change_mask & PW_ENDPOINT_CHANGE_MASK_STREAMS)
this->info.n_streams = info->n_streams;
if (info->change_mask & PW_ENDPOINT_CHANGE_MASK_SESSION)
this->info.session_id = info->session_id;
if (info->change_mask & PW_ENDPOINT_CHANGE_MASK_PROPS)
pw_properties_update(this->props, info->props);
if (info->change_mask & PW_ENDPOINT_CHANGE_MASK_PARAMS) {
size_t size = info->n_params * sizeof(struct spa_param_info);
this->info.params = realloc(this->info.params, size);
if (size > 0 && !this->info.params) {
this->info.n_params = 0;
goto no_mem;
}
this->info.n_params = info->n_params;
memcpy(this->info.params, info->params, size);
}
if (!this->info.name) {
this->info.name = info->name ? strdup(info->name) : NULL;
this->info.media_class = info->media_class ? strdup(info->media_class) : NULL;
this->info.direction = info->direction;
this->info.flags = info->flags;
}
this->info.change_mask = info->change_mask;
spa_list_for_each(resource, &this->global->resource_list, link) {
pw_endpoint_resource_info(resource, &this->info);
}
this->info.change_mask = 0;
}
return 0;
no_mem:
pw_log_error(NAME" can't update: no memory");
pw_resource_error(this->client_ep->resource, -ENOMEM,
NAME" can't update: no memory");
return -ENOMEM;
}
static void endpoint_unbind(void *data)
{
struct pw_resource *resource = data;
spa_list_remove(&resource->link);
}
static const struct pw_resource_events resource_events = {
PW_VERSION_RESOURCE_EVENTS,
.destroy = endpoint_unbind,
};
static int endpoint_bind(void *_data, struct pw_client *client,
uint32_t permissions, uint32_t version, uint32_t id)
{
struct endpoint *this = _data;
struct pw_global *global = this->global;
struct pw_resource *resource;
struct resource_data *data;
resource = pw_resource_new(client, id, permissions, global->type, version, sizeof(*data));
if (resource == NULL)
goto no_mem;
data = pw_resource_get_user_data(resource);
data->endpoint = this;
pw_resource_add_listener(resource, &data->resource_listener,
&resource_events, resource);
pw_resource_add_object_listener(resource, &data->object_listener,
&methods, resource);
pw_log_debug(NAME" %p: bound to %d", this, resource->id);
spa_list_append(&global->resource_list, &resource->link);
this->info.change_mask = PW_ENDPOINT_CHANGE_MASK_ALL;
pw_endpoint_resource_info(resource, &this->info);
this->info.change_mask = 0;
return 0;
no_mem:
pw_log_error(NAME" can't create resource: no memory");
pw_resource_error(this->client_ep->resource, -ENOMEM,
NAME" can't create resource: no memory");
return -ENOMEM;
}
int endpoint_init(struct endpoint *this,
struct client_endpoint *client_ep,
struct pw_context *context,
struct pw_properties *properties)
{
const char *keys[] = {
PW_KEY_FACTORY_ID,
PW_KEY_CLIENT_ID,
PW_KEY_DEVICE_ID,
PW_KEY_NODE_ID,
PW_KEY_MEDIA_CLASS,
PW_KEY_SESSION_ID,
PW_KEY_PRIORITY_SESSION,
PW_KEY_ENDPOINT_NAME,
PW_KEY_ENDPOINT_CLIENT_ID,
PW_KEY_ENDPOINT_ICON_NAME,
PW_KEY_ENDPOINT_MONITOR,
NULL
};
pw_log_debug(NAME" %p: new", this);
this->client_ep = client_ep;
this->props = properties;
properties = pw_properties_new(NULL, NULL);
if (!properties)
goto no_mem;
pw_properties_update_keys(properties, &this->props->dict, keys);
this->global = pw_global_new (context,
PW_TYPE_INTERFACE_Endpoint,
PW_VERSION_ENDPOINT_PROXY,
properties, endpoint_bind, this);
if (!this->global)
goto no_mem;
pw_properties_setf(this->props, PW_KEY_OBJECT_ID, "%u", this->global->id);
this->info.version = PW_VERSION_ENDPOINT_INFO;
this->info.id = this->global->id;
this->info.props = &this->props->dict;
pw_resource_set_bound_id(client_ep->resource, this->global->id);
return pw_global_register(this->global);
no_mem:
pw_log_error(NAME" - can't create - out of memory");
return -ENOMEM;
}
void endpoint_clear(struct endpoint *this)
{
uint32_t i;
pw_log_debug(NAME" %p: destroy", this);
pw_global_destroy(this->global);
for (i = 0; i < this->n_params; i++)
free(this->params[i]);
free(this->params);
free(this->info.name);
free(this->info.media_class);
free(this->info.params);
if (this->props)
pw_properties_free(this->props);
}
|