diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:28:17 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:28:17 +0000 |
commit | 7a46c07230b8d8108c0e8e80df4522d0ac116538 (patch) | |
tree | d483300dab478b994fe199a5d19d18d74153718a /src/modules/module-session-manager | |
parent | Initial commit. (diff) | |
download | pipewire-upstream/0.3.65.tar.xz pipewire-upstream/0.3.65.zip |
Adding upstream version 0.3.65.upstream/0.3.65upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/modules/module-session-manager')
18 files changed, 8027 insertions, 0 deletions
diff --git a/src/modules/module-session-manager/client-endpoint/client-endpoint.c b/src/modules/module-session-manager/client-endpoint/client-endpoint.c new file mode 100644 index 0000000..b2f2d98 --- /dev/null +++ b/src/modules/module-session-manager/client-endpoint/client-endpoint.c @@ -0,0 +1,296 @@ +/* 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 <spa/utils/result.h> +#include <pipewire/impl.h> + +#include <pipewire/extensions/session-manager.h> + +#include "client-endpoint.h" +#include "endpoint.h" +#include "endpoint-stream.h" + +#define NAME "client-endpoint" + +struct factory_data { + struct pw_impl_module *module; + struct spa_hook module_listener; + + struct pw_impl_factory *factory; + struct spa_hook factory_listener; +}; + +static struct endpoint_stream *find_stream(struct client_endpoint *this, uint32_t id) +{ + struct endpoint_stream *s; + spa_list_for_each(s, &this->streams, link) { + if (s->id == id) + return s; + } + return NULL; +} + +static int client_endpoint_update(void *object, + uint32_t change_mask, + uint32_t n_params, + const struct spa_pod **params, + const struct pw_endpoint_info *info) +{ + struct client_endpoint *this = object; + struct endpoint *endpoint = &this->endpoint; + + return endpoint_update(endpoint, change_mask, n_params, params, info); +} + +static int client_endpoint_stream_update(void *object, + uint32_t stream_id, + uint32_t change_mask, + uint32_t n_params, + const struct spa_pod **params, + const struct pw_endpoint_stream_info *info) +{ + struct client_endpoint *this = object; + struct endpoint *endpoint = &this->endpoint; + struct endpoint_stream *stream = find_stream(this, stream_id); + struct pw_properties *props = NULL; + + if (!stream) { + static const char * const keys[] = { + PW_KEY_FACTORY_ID, + PW_KEY_CLIENT_ID, + PW_KEY_ENDPOINT_ID, + PW_KEY_PRIORITY_SESSION, + PW_KEY_ENDPOINT_MONITOR, + PW_KEY_ENDPOINT_STREAM_NAME, + PW_KEY_ENDPOINT_STREAM_DESCRIPTION, + NULL + }; + + struct pw_context *context = pw_global_get_context(endpoint->global); + + stream = calloc(1, sizeof(struct endpoint_stream)); + if (!stream) + goto no_mem; + + props = pw_properties_new(NULL, NULL); + if (!props) + goto no_mem; + + pw_properties_update_keys(props, &endpoint->props->dict, keys); + if (info && info->props) + pw_properties_update_keys(props, info->props, keys); + + if (endpoint_stream_init(stream, stream_id, endpoint->info.id, + this, context, props) < 0) + goto no_mem; + + spa_list_append(&this->streams, &stream->link); + } + else if (change_mask & PW_CLIENT_ENDPOINT_STREAM_UPDATE_DESTROYED) { + endpoint_stream_clear(stream); + spa_list_remove(&stream->link); + free(stream); + stream = NULL; + } + + return stream ? + endpoint_stream_update(stream, change_mask, n_params, params, info) + : 0; + + no_mem: + pw_properties_free(props); + free(stream); + pw_log_error(NAME" %p: cannot update stream: no memory", this); + pw_resource_errorf(this->resource, -ENOMEM, + NAME" %p: cannot update stream: no memory", this); + return -ENOMEM; +} + +static const struct pw_client_endpoint_methods methods = { + PW_VERSION_CLIENT_ENDPOINT_METHODS, + .update = client_endpoint_update, + .stream_update = client_endpoint_stream_update, +}; + +static void client_endpoint_destroy(void *data) +{ + struct client_endpoint *this = data; + struct endpoint_stream *s; + + pw_log_debug(NAME" %p: destroy", this); + + spa_list_consume(s, &this->streams, link) { + endpoint_stream_clear(s); + spa_list_remove(&s->link); + free(s); + } + endpoint_clear(&this->endpoint); + spa_hook_remove(&this->resource_listener); + + free(this); +} + +static const struct pw_resource_events resource_events = { + PW_VERSION_RESOURCE_EVENTS, + .destroy = client_endpoint_destroy, +}; + +static void *create_object(void *data, + struct pw_resource *owner_resource, + const char *type, + uint32_t version, + struct pw_properties *properties, + uint32_t new_id) +{ + struct factory_data *d = data; + struct pw_impl_factory *factory = d->factory; + struct client_endpoint *this; + struct pw_impl_client *owner = pw_resource_get_client(owner_resource); + struct pw_context *context = pw_impl_client_get_context(owner); + + this = calloc(1, sizeof(struct client_endpoint)); + if (this == NULL) + goto no_mem; + + spa_list_init(&this->streams); + + pw_log_debug(NAME" %p: new", this); + + if (!properties) + properties = pw_properties_new(NULL, NULL); + if (!properties) + goto no_mem; + + pw_properties_setf(properties, PW_KEY_CLIENT_ID, "%d", + pw_impl_client_get_info(owner)->id); + pw_properties_setf(properties, PW_KEY_FACTORY_ID, "%d", + pw_impl_factory_get_info(factory)->id); + + this->resource = pw_resource_new(owner, new_id, PW_PERM_ALL, type, version, 0); + if (this->resource == NULL) + goto no_mem; + + if (endpoint_init(&this->endpoint, this, context, properties) < 0) + goto no_mem; + + pw_resource_add_listener(this->resource, &this->resource_listener, + &resource_events, this); + pw_resource_add_object_listener(this->resource, &this->object_listener, + &methods, this); + + return this; + + no_mem: + pw_properties_free(properties); + if (this && this->resource) + pw_resource_destroy(this->resource); + free(this); + pw_log_error("can't create client endpoint: no memory"); + pw_resource_error(owner_resource, -ENOMEM, + "can't create client endpoint: no memory"); + return NULL; +} + +static const struct pw_impl_factory_implementation impl_factory = { + PW_VERSION_IMPL_FACTORY_IMPLEMENTATION, + .create_object = create_object, +}; + +static void factory_destroy(void *data) +{ + struct factory_data *d = data; + spa_hook_remove(&d->factory_listener); + d->factory = NULL; + if (d->module) + pw_impl_module_destroy(d->module); +} + +static const struct pw_impl_factory_events factory_events = { + PW_VERSION_IMPL_FACTORY_EVENTS, + .destroy = factory_destroy, +}; + +static void module_destroy(void *data) +{ + struct factory_data *d = data; + spa_hook_remove(&d->module_listener); + d->module = NULL; + if (d->factory) + pw_impl_factory_destroy(d->factory); +} + +static void module_registered(void *data) +{ + struct factory_data *d = data; + struct pw_impl_module *module = d->module; + struct pw_impl_factory *factory = d->factory; + struct spa_dict_item items[1]; + char id[16]; + int res; + + snprintf(id, sizeof(id), "%d", pw_impl_module_get_info(module)->id); + items[0] = SPA_DICT_ITEM_INIT(PW_KEY_MODULE_ID, id); + pw_impl_factory_update_properties(factory, &SPA_DICT_INIT(items, 1)); + + if ((res = pw_impl_factory_register(factory, NULL)) < 0) { + pw_log_error(NAME" %p: can't register factory: %s", factory, spa_strerror(res)); + } +} + +static const struct pw_impl_module_events module_events = { + PW_VERSION_IMPL_MODULE_EVENTS, + .destroy = module_destroy, + .registered = module_registered, +}; + +int client_endpoint_factory_init(struct pw_impl_module *module) +{ + struct pw_context *context = pw_impl_module_get_context(module); + struct pw_impl_factory *factory; + struct factory_data *data; + + factory = pw_context_create_factory(context, + "client-endpoint", + PW_TYPE_INTERFACE_ClientEndpoint, + PW_VERSION_CLIENT_ENDPOINT, + NULL, + sizeof(*data)); + if (factory == NULL) + return -ENOMEM; + + data = pw_impl_factory_get_user_data(factory); + data->factory = factory; + data->module = module; + + pw_impl_factory_set_implementation(factory, &impl_factory, data); + pw_impl_factory_add_listener(factory, &data->factory_listener, &factory_events, data); + + pw_impl_module_add_listener(module, &data->module_listener, &module_events, data); + + return 0; +} diff --git a/src/modules/module-session-manager/client-endpoint/client-endpoint.h b/src/modules/module-session-manager/client-endpoint/client-endpoint.h new file mode 100644 index 0000000..bc5630d --- /dev/null +++ b/src/modules/module-session-manager/client-endpoint/client-endpoint.h @@ -0,0 +1,62 @@ +/* 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. + */ + +#ifndef MODULE_SESSION_MANAGER_CLIENT_ENDPOINT_H +#define MODULE_SESSION_MANAGER_CLIENT_ENDPOINT_H + +#include "endpoint.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct client_endpoint { + struct pw_resource *resource; + struct spa_hook resource_listener; + struct spa_hook object_listener; + struct endpoint endpoint; + struct spa_list streams; +}; + +#define pw_client_endpoint_resource(r,m,v,...) \ + pw_resource_call_res(r,struct pw_client_endpoint_events,m,v,__VA_ARGS__) +#define pw_client_endpoint_resource_set_id(r,...) \ + pw_client_endpoint_resource(r,set_id,0,__VA_ARGS__) +#define pw_client_endpoint_resource_set_session_id(r,...) \ + pw_client_endpoint_resource(r,set_session_id,0,__VA_ARGS__) +#define pw_client_endpoint_resource_set_param(r,...) \ + pw_client_endpoint_resource(r,set_param,0,__VA_ARGS__) +#define pw_client_endpoint_resource_stream_set_param(r,...) \ + pw_client_endpoint_resource(r,stream_set_param,0,__VA_ARGS__) +#define pw_client_endpoint_resource_create_link(r,...) \ + pw_client_endpoint_resource(r,create_link,0,__VA_ARGS__) + +int client_endpoint_factory_init(struct pw_impl_module *module); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* MODULE_SESSION_MANAGER_CLIENT_ENDPOINT_H */ diff --git a/src/modules/module-session-manager/client-endpoint/endpoint-stream.c b/src/modules/module-session-manager/client-endpoint/endpoint-stream.c new file mode 100644 index 0000000..8dde6f7 --- /dev/null +++ b/src/modules/module-session-manager/client-endpoint/endpoint-stream.c @@ -0,0 +1,352 @@ +/* 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/impl.h> +#include <pipewire/extensions/session-manager.h> + +#include <spa/pod/filter.h> + +#include "endpoint-stream.h" +#include "client-endpoint.h" + +#define NAME "endpoint-stream" + +struct resource_data { + struct endpoint_stream *stream; + struct spa_hook object_listener; + uint32_t n_subscribe_ids; + uint32_t subscribe_ids[32]; +}; + +#define pw_endpoint_stream_resource(r,m,v,...) \ + pw_resource_call(r,struct pw_endpoint_stream_events,m,v,__VA_ARGS__) +#define pw_endpoint_stream_resource_info(r,...) \ + pw_endpoint_stream_resource(r,info,0,__VA_ARGS__) +#define pw_endpoint_stream_resource_param(r,...) \ + pw_endpoint_stream_resource(r,param,0,__VA_ARGS__) + +static int endpoint_stream_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_stream *this = data->stream; + 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; + + 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_stream_resource_param(resource, seq, id, index, next, result); + + if (++count == num) + break; + } + return 0; +} + +static int endpoint_stream_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->stream, pw_resource_get_id(resource), ids[i]); + endpoint_stream_enum_params(resource, 1, ids[i], 0, UINT32_MAX, NULL); + } + return 0; +} + +static int endpoint_stream_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_stream *this = data->stream; + + pw_client_endpoint_resource_set_param(this->client_ep->resource, + id, flags, param); + + return 0; +} + +static const struct pw_endpoint_stream_methods methods = { + PW_VERSION_ENDPOINT_STREAM_METHODS, + .subscribe_params = endpoint_stream_subscribe_params, + .enum_params = endpoint_stream_enum_params, + .set_param = endpoint_stream_set_param, +}; + +struct emit_param_data { + struct endpoint_stream *this; + struct spa_pod *param; + uint32_t id; + uint32_t index; + uint32_t next; +}; + +static int emit_param(void *_data, struct pw_resource *resource) +{ + struct emit_param_data *d = _data; + struct resource_data *data; + uint32_t i; + + data = pw_resource_get_user_data(resource); + for (i = 0; i < data->n_subscribe_ids; i++) { + if (data->subscribe_ids[i] == d->id) { + pw_endpoint_stream_resource_param(resource, 1, + d->id, d->index, d->next, d->param); + } + } + return 0; +} + +static void endpoint_stream_notify_subscribed(struct endpoint_stream *this, + uint32_t index, uint32_t next) +{ + struct pw_global *global = this->global; + struct emit_param_data data; + struct spa_pod *param = this->params[index]; + + if (!param || !spa_pod_is_object (param)) + return; + + data.this = this; + data.param = param; + data.id = SPA_POD_OBJECT_ID (param); + data.index = index; + data.next = next; + + pw_global_for_each_resource(global, emit_param, &data); +} + +static int emit_info(void *data, struct pw_resource *resource) +{ + struct endpoint_stream *this = data; + pw_endpoint_stream_resource_info(resource, &this->info); + return 0; +} + +int endpoint_stream_update(struct endpoint_stream *this, + uint32_t change_mask, + uint32_t n_params, + const struct spa_pod **params, + const struct pw_endpoint_stream_info *info) +{ + if (change_mask & PW_CLIENT_ENDPOINT_UPDATE_PARAMS) { + uint32_t i; + + pw_log_debug(NAME" %p: update %d params", this, n_params); + + for (i = 0; i < this->n_params; i++) + free(this->params[i]); + this->n_params = n_params; + if (this->n_params == 0) { + free(this->params); + this->params = NULL; + } else { + void *p; + p = pw_reallocarray(this->params, n_params, sizeof(struct spa_pod*)); + if (p == NULL) { + free(this->params); + this->params = NULL; + this->n_params = 0; + goto no_mem; + } + this->params = p; + } + for (i = 0; i < this->n_params; i++) { + this->params[i] = params[i] ? spa_pod_copy(params[i]) : NULL; + endpoint_stream_notify_subscribed(this, i, i+1); + } + } + + if (change_mask & PW_CLIENT_ENDPOINT_UPDATE_INFO) { + if (info->change_mask & PW_ENDPOINT_STREAM_CHANGE_MASK_LINK_PARAMS) { + free(this->info.link_params); + this->info.link_params = spa_pod_copy(info->link_params); + } + + if (info->change_mask & PW_ENDPOINT_STREAM_CHANGE_MASK_PROPS) + pw_properties_update(this->props, info->props); + + if (info->change_mask & PW_ENDPOINT_STREAM_CHANGE_MASK_PARAMS) { + this->info.n_params = info->n_params; + if (info->n_params == 0) { + free(this->info.params); + this->info.params = NULL; + } else { + void *p; + p = pw_reallocarray(this->info.params, info->n_params, sizeof(struct spa_param_info)); + if (p == NULL) { + free(this->info.params); + this->info.params = NULL; + this->info.n_params = 0; + goto no_mem; + } + this->info.params = p; + memcpy(this->info.params, info->params, info->n_params * sizeof(struct spa_param_info)); + } + } + + if (!this->info.name) + this->info.name = info->name ? strdup(info->name) : NULL; + + this->info.change_mask = info->change_mask; + pw_global_for_each_resource(this->global, emit_info, this); + 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 int endpoint_stream_bind(void *_data, struct pw_impl_client *client, + uint32_t permissions, uint32_t version, uint32_t id) +{ + struct endpoint_stream *this = _data; + struct pw_global *global = this->global; + struct pw_resource *resource; + struct resource_data *data; + + resource = pw_resource_new(client, id, permissions, + pw_global_get_type(global), version, sizeof(*data)); + if (resource == NULL) + goto no_mem; + + data = pw_resource_get_user_data(resource); + data->stream = this; + pw_resource_add_object_listener(resource, &data->object_listener, + &methods, resource); + + pw_log_debug(NAME" %p: bound to %d", this, pw_resource_get_id(resource)); + pw_global_add_resource(global, resource); + + this->info.change_mask = PW_ENDPOINT_STREAM_CHANGE_MASK_ALL; + pw_endpoint_stream_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_stream_init(struct endpoint_stream *this, + uint32_t id, uint32_t endpoint_id, + struct client_endpoint *client_ep, + struct pw_context *context, + struct pw_properties *properties) +{ + pw_log_debug(NAME" %p: new", this); + + this->client_ep = client_ep; + this->id = id; + this->props = properties; + + pw_properties_setf(properties, PW_KEY_ENDPOINT_ID, "%u", endpoint_id); + + properties = pw_properties_copy(properties); + if (!properties) + goto no_mem; + + this->global = pw_global_new (context, + PW_TYPE_INTERFACE_EndpointStream, + PW_VERSION_ENDPOINT_STREAM, + properties, endpoint_stream_bind, this); + if (!this->global) + goto no_mem; + + pw_properties_setf(this->props, PW_KEY_OBJECT_ID, "%u", + pw_global_get_id(this->global)); + pw_properties_setf(this->props, PW_KEY_OBJECT_SERIAL, "%"PRIu64, + pw_global_get_serial(this->global)); + + this->info.version = PW_VERSION_ENDPOINT_STREAM_INFO; + this->info.id = pw_global_get_id(this->global); + this->info.endpoint_id = endpoint_id; + this->info.props = &this->props->dict; + + return pw_global_register(this->global); + + no_mem: + pw_log_error(NAME" - can't create - out of memory"); + return -ENOMEM; +} + +void endpoint_stream_clear(struct endpoint_stream *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.link_params); + free(this->info.params); + + pw_properties_free(this->props); +} diff --git a/src/modules/module-session-manager/client-endpoint/endpoint-stream.h b/src/modules/module-session-manager/client-endpoint/endpoint-stream.h new file mode 100644 index 0000000..7172287 --- /dev/null +++ b/src/modules/module-session-manager/client-endpoint/endpoint-stream.h @@ -0,0 +1,64 @@ +/* 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. + */ + +#ifndef MODULE_SESSION_MANAGER_ENDPOINT_STREAM_H +#define MODULE_SESSION_MANAGER_ENDPOINT_STREAM_H + +#ifdef __cplusplus +extern "C" { +#endif + +struct client_endpoint; + +struct endpoint_stream { + struct spa_list link; + struct client_endpoint *client_ep; + struct pw_global *global; + uint32_t id; /* endpoint-local stream id */ + uint32_t n_params; + struct spa_pod **params; + struct pw_endpoint_stream_info info; + struct pw_properties *props; /* wrapper of info.props */ +}; + +int endpoint_stream_init(struct endpoint_stream *this, + uint32_t id, uint32_t endpoint_id, + struct client_endpoint *client_ep, + struct pw_context *context, + struct pw_properties *properties); + +void endpoint_stream_clear(struct endpoint_stream *this); + +int endpoint_stream_update(struct endpoint_stream *this, + uint32_t change_mask, + uint32_t n_params, + const struct spa_pod **params, + const struct pw_endpoint_stream_info *info); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* MODULE_SESSION_MANAGER_ENDPOINT_STREAM_H */ diff --git a/src/modules/module-session-manager/client-endpoint/endpoint.c b/src/modules/module-session-manager/client-endpoint/endpoint.c new file mode 100644 index 0000000..aa13989 --- /dev/null +++ b/src/modules/module-session-manager/client-endpoint/endpoint.c @@ -0,0 +1,385 @@ +/* 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/impl.h> +#include <pipewire/extensions/session-manager.h> + +#include <spa/pod/filter.h> + +#include "endpoint.h" +#include "client-endpoint.h" + +#define NAME "endpoint" + +struct resource_data { + struct endpoint *endpoint; + 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_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, pw_resource_get_id(resource), 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_methods methods = { + PW_VERSION_ENDPOINT_METHODS, + .subscribe_params = endpoint_subscribe_params, + .enum_params = endpoint_enum_params, + .set_param = endpoint_set_param, + .create_link = endpoint_create_link, +}; + +struct emit_param_data { + struct endpoint *this; + struct spa_pod *param; + uint32_t id; + uint32_t index; + uint32_t next; +}; + +static int emit_param(void *_data, struct pw_resource *resource) +{ + struct emit_param_data *d = _data; + struct resource_data *data; + uint32_t i; + + data = pw_resource_get_user_data(resource); + for (i = 0; i < data->n_subscribe_ids; i++) { + if (data->subscribe_ids[i] == d->id) { + pw_endpoint_resource_param(resource, 1, + d->id, d->index, d->next, d->param); + } + } + return 0; +} + +static void endpoint_notify_subscribed(struct endpoint *this, + uint32_t index, uint32_t next) +{ + struct pw_global *global = this->global; + struct emit_param_data data; + struct spa_pod *param = this->params[index]; + + if (!param || !spa_pod_is_object (param)) + return; + + data.this = this; + data.param = param; + data.id = SPA_POD_OBJECT_ID (param); + data.index = index; + data.next = next; + + pw_global_for_each_resource(global, emit_param, &data); +} + +static int emit_info(void *data, struct pw_resource *resource) +{ + struct endpoint *this = data; + pw_endpoint_resource_info(resource, &this->info); + return 0; +} + +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; + + pw_log_debug(NAME" %p: update %d params", this, n_params); + + for (i = 0; i < this->n_params; i++) + free(this->params[i]); + this->n_params = n_params; + if (this->n_params == 0) { + free(this->params); + this->params = NULL; + } else { + void *p; + p = pw_reallocarray(this->params, n_params, sizeof(struct spa_pod*)); + if (p == NULL) { + free(this->params); + this->params = NULL; + this->n_params = 0; + goto no_mem; + } + this->params = p; + } + 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) { + 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) { + this->info.n_params = info->n_params; + if (info->n_params == 0) { + free(this->info.params); + this->info.params = NULL; + } else { + void *p; + p = pw_reallocarray(this->info.params, info->n_params, sizeof(struct spa_param_info)); + if (p == NULL) { + free(this->info.params); + this->info.params = NULL; + this->info.n_params = 0; + goto no_mem; + } + this->info.params = p; + memcpy(this->info.params, info->params, info->n_params * sizeof(struct spa_param_info)); + } + } + + 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; + pw_global_for_each_resource(this->global, emit_info, this); + 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 int endpoint_bind(void *_data, struct pw_impl_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, + pw_global_get_type(global), version, sizeof(*data)); + if (resource == NULL) + goto no_mem; + + data = pw_resource_get_user_data(resource); + data->endpoint = this; + pw_resource_add_object_listener(resource, &data->object_listener, + &methods, resource); + + pw_log_debug(NAME" %p: bound to %d", this, pw_resource_get_id(resource)); + pw_global_add_resource(global, resource); + + 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) +{ + static const char * const keys[] = { + PW_KEY_OBJECT_SERIAL, + 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; + + this->global = pw_global_new (context, + PW_TYPE_INTERFACE_Endpoint, + PW_VERSION_ENDPOINT, + NULL, endpoint_bind, this); + if (!this->global) + goto no_mem; + + pw_properties_setf(this->props, PW_KEY_OBJECT_ID, "%u", + pw_global_get_id(this->global)); + pw_properties_setf(this->props, PW_KEY_OBJECT_SERIAL, "%"PRIu64, + pw_global_get_serial(this->global)); + + this->info.version = PW_VERSION_ENDPOINT_INFO; + this->info.id = pw_global_get_id(this->global); + this->info.props = &this->props->dict; + + pw_global_update_keys(this->global, &this->props->dict, keys); + + pw_resource_set_bound_id(client_ep->resource, this->info.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); + + pw_properties_free(this->props); +} diff --git a/src/modules/module-session-manager/client-endpoint/endpoint.h b/src/modules/module-session-manager/client-endpoint/endpoint.h new file mode 100644 index 0000000..5ceff39 --- /dev/null +++ b/src/modules/module-session-manager/client-endpoint/endpoint.h @@ -0,0 +1,61 @@ +/* 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. + */ + +#ifndef MODULE_SESSION_MANAGER_ENDPOINT_H +#define MODULE_SESSION_MANAGER_ENDPOINT_H + +#ifdef __cplusplus +extern "C" { +#endif + +struct client_endpoint; + +struct endpoint { + struct client_endpoint *client_ep; + struct pw_global *global; + uint32_t n_params; + struct spa_pod **params; + struct pw_endpoint_info info; + struct pw_properties *props; /* wrapper of info.props */ +}; + +int endpoint_init(struct endpoint *this, + struct client_endpoint *client_ep, + struct pw_context *context, + struct pw_properties *properties); + +void endpoint_clear(struct endpoint *this); + +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); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* MODULE_SESSION_MANAGER_ENDPOINT_H */ diff --git a/src/modules/module-session-manager/client-session/client-session.c b/src/modules/module-session-manager/client-session/client-session.c new file mode 100644 index 0000000..89997c9 --- /dev/null +++ b/src/modules/module-session-manager/client-session/client-session.c @@ -0,0 +1,295 @@ +/* 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 <spa/utils/result.h> + +#include <pipewire/impl.h> +#include <pipewire/extensions/session-manager.h> + +#include "client-session.h" +#include "session.h" +#include "endpoint-link.h" + +#define NAME "client-session" + +struct factory_data { + struct pw_impl_module *module; + struct spa_hook module_listener; + + struct pw_impl_factory *factory; + struct spa_hook factory_listener; +}; + +static struct endpoint_link *find_link(struct client_session *this, uint32_t id) +{ + struct endpoint_link *l; + spa_list_for_each(l, &this->links, link) { + if (l->id == id) + return l; + } + return NULL; +} + +static int client_session_update(void *object, + uint32_t change_mask, + uint32_t n_params, + const struct spa_pod **params, + const struct pw_session_info *info) +{ + struct client_session *this = object; + struct session *session = &this->session; + + return session_update(session, change_mask, n_params, params, info); +} + +static int client_session_link_update(void *object, + uint32_t link_id, + uint32_t change_mask, + uint32_t n_params, + const struct spa_pod **params, + const struct pw_endpoint_link_info *info) +{ + struct client_session *this = object; + struct session *session = &this->session; + struct endpoint_link *link = find_link(this, link_id); + struct pw_properties *props = NULL; + + if (!link) { + static const char * const keys[] = { + PW_KEY_FACTORY_ID, + PW_KEY_CLIENT_ID, + PW_KEY_SESSION_ID, + PW_KEY_ENDPOINT_LINK_OUTPUT_ENDPOINT, + PW_KEY_ENDPOINT_LINK_OUTPUT_STREAM, + PW_KEY_ENDPOINT_LINK_INPUT_ENDPOINT, + PW_KEY_ENDPOINT_LINK_INPUT_STREAM, + NULL + }; + + struct pw_context *context = pw_global_get_context(session->global); + + link = calloc(1, sizeof(struct endpoint_link)); + if (!link) + goto no_mem; + + props = pw_properties_new(NULL, NULL); + if (!props) + goto no_mem; + pw_properties_update_keys(props, &session->props->dict, keys); + if (info && info->props) + pw_properties_update_keys(props, info->props, keys); + + if (endpoint_link_init(link, link_id, session->info.id, + this, context, props) < 0) + goto no_mem; + + spa_list_append(&this->links, &link->link); + } + else if (change_mask & PW_CLIENT_SESSION_LINK_UPDATE_DESTROYED) { + endpoint_link_clear(link); + spa_list_remove(&link->link); + free(link); + link = NULL; + } + + return link ? + endpoint_link_update(link, change_mask, n_params, params, info) + : 0; + + no_mem: + pw_properties_free(props); + free(link); + pw_log_error(NAME" %p: cannot update link: no memory", this); + pw_resource_error(this->resource, -ENOMEM, + "cannot update link: no memory"); + return -ENOMEM; +} + +static const struct pw_client_session_methods methods = { + PW_VERSION_CLIENT_SESSION_METHODS, + .update = client_session_update, + .link_update = client_session_link_update, +}; + +static void client_session_destroy(void *data) +{ + struct client_session *this = data; + struct endpoint_link *l; + + pw_log_debug(NAME" %p: destroy", this); + + spa_list_consume(l, &this->links, link) { + endpoint_link_clear(l); + spa_list_remove(&l->link); + free(l); + } + session_clear(&this->session); + spa_hook_remove(&this->resource_listener); + + free(this); +} + +static const struct pw_resource_events resource_events = { + PW_VERSION_RESOURCE_EVENTS, + .destroy = client_session_destroy, +}; + +static void *create_object(void *data, + struct pw_resource *owner_resource, + const char *type, + uint32_t version, + struct pw_properties *properties, + uint32_t new_id) +{ + struct factory_data *d = data; + struct pw_impl_factory *factory = d->factory; + struct client_session *this; + struct pw_impl_client *owner = pw_resource_get_client(owner_resource); + struct pw_context *context = pw_impl_client_get_context(owner); + + this = calloc(1, sizeof(struct client_session)); + if (this == NULL) + goto no_mem; + + spa_list_init(&this->links); + + pw_log_debug(NAME" %p: new", this); + + if (!properties) + properties = pw_properties_new(NULL, NULL); + if (!properties) + goto no_mem; + + pw_properties_setf(properties, PW_KEY_CLIENT_ID, "%d", + pw_impl_client_get_info(owner)->id); + pw_properties_setf(properties, PW_KEY_FACTORY_ID, "%d", + pw_impl_factory_get_info(factory)->id); + + this->resource = pw_resource_new(owner, new_id, PW_PERM_ALL, type, version, 0); + if (this->resource == NULL) + goto no_mem; + + if (session_init(&this->session, this, context, properties) < 0) + goto no_mem; + + pw_resource_add_listener(this->resource, &this->resource_listener, + &resource_events, this); + pw_resource_add_object_listener(this->resource, &this->object_listener, + &methods, this); + + return this; + + no_mem: + pw_properties_free(properties); + if (this && this->resource) + pw_resource_destroy(this->resource); + free(this); + pw_log_error("can't create client session: no memory"); + pw_resource_error(owner_resource, -ENOMEM, + "can't create client session: no memory"); + return NULL; +} + +static const struct pw_impl_factory_implementation impl_factory = { + PW_VERSION_IMPL_FACTORY_IMPLEMENTATION, + .create_object = create_object, +}; + +static void factory_destroy(void *data) +{ + struct factory_data *d = data; + spa_hook_remove(&d->factory_listener); + d->factory = NULL; + if (d->module) + pw_impl_module_destroy(d->module); +} + +static const struct pw_impl_factory_events factory_events = { + PW_VERSION_IMPL_FACTORY_EVENTS, + .destroy = factory_destroy, +}; + +static void module_destroy(void *data) +{ + struct factory_data *d = data; + spa_hook_remove(&d->module_listener); + d->module = NULL; + if (d->factory) + pw_impl_factory_destroy(d->factory); +} + +static void module_registered(void *data) +{ + struct factory_data *d = data; + struct pw_impl_module *module = d->module; + struct pw_impl_factory *factory = d->factory; + struct spa_dict_item items[1]; + char id[16]; + int res; + + snprintf(id, sizeof(id), "%d", pw_impl_module_get_info(module)->id); + items[0] = SPA_DICT_ITEM_INIT(PW_KEY_MODULE_ID, id); + pw_impl_factory_update_properties(factory, &SPA_DICT_INIT(items, 1)); + + if ((res = pw_impl_factory_register(factory, NULL)) < 0) { + pw_log_error(NAME" %p: can't register factory: %s", factory, spa_strerror(res)); + } +} + +static const struct pw_impl_module_events module_events = { + PW_VERSION_IMPL_MODULE_EVENTS, + .destroy = module_destroy, + .registered = module_registered, +}; + +int client_session_factory_init(struct pw_impl_module *module) +{ + struct pw_context *context = pw_impl_module_get_context(module); + struct pw_impl_factory *factory; + struct factory_data *data; + + factory = pw_context_create_factory(context, + "client-session", + PW_TYPE_INTERFACE_ClientSession, + PW_VERSION_CLIENT_SESSION, + NULL, + sizeof(*data)); + if (factory == NULL) + return -ENOMEM; + + data = pw_impl_factory_get_user_data(factory); + data->factory = factory; + data->module = module; + + pw_impl_factory_set_implementation(factory, &impl_factory, data); + pw_impl_factory_add_listener(factory, &data->factory_listener, &factory_events, data); + + pw_impl_module_add_listener(module, &data->module_listener, &module_events, data); + + return 0; +} diff --git a/src/modules/module-session-manager/client-session/client-session.h b/src/modules/module-session-manager/client-session/client-session.h new file mode 100644 index 0000000..fc6124c --- /dev/null +++ b/src/modules/module-session-manager/client-session/client-session.h @@ -0,0 +1,62 @@ +/* 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. + */ + +#ifndef MODULE_SESSION_MANAGER_CLIENT_SESSION_H +#define MODULE_SESSION_MANAGER_CLIENT_SESSION_H + +#include "session.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct client_session { + struct pw_resource *resource; + struct spa_hook resource_listener; + struct spa_hook object_listener; + struct session session; + struct spa_list links; +}; + +#define pw_client_session_resource(r,m,v,...) \ + pw_resource_call_res(r,struct pw_client_session_events,m,v,__VA_ARGS__) +#define pw_client_session_resource_set_id(r,...) \ + pw_client_session_resource(r,set_id,0,__VA_ARGS__) +#define pw_client_session_resource_set_param(r,...) \ + pw_client_session_resource(r,set_param,0,__VA_ARGS__) +#define pw_client_session_resource_link_set_param(r,...) \ + pw_client_session_resource(r,link_set_param,0,__VA_ARGS__) +#define pw_client_session_resource_create_link(r,...) \ + pw_client_session_resource(r,create_link,0,__VA_ARGS__) +#define pw_client_session_resource_destroy_link(r,...) \ + pw_client_session_resource(r,destroy_link,0,__VA_ARGS__) +#define pw_client_session_resource_link_request_state(r,...) \ + pw_client_session_resource(r,link_request_state,0,__VA_ARGS__) + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* MODULE_SESSION_MANAGER_CLIENT_SESSION_H */ diff --git a/src/modules/module-session-manager/client-session/endpoint-link.c b/src/modules/module-session-manager/client-session/endpoint-link.c new file mode 100644 index 0000000..0bdbfc9 --- /dev/null +++ b/src/modules/module-session-manager/client-session/endpoint-link.c @@ -0,0 +1,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/impl.h> +#include <pipewire/extensions/session-manager.h> + +#include <spa/pod/filter.h> + +#include "endpoint-link.h" +#include "client-session.h" + +#define NAME "endpoint-link" + +struct resource_data { + struct endpoint_link *link; + struct spa_hook object_listener; + uint32_t n_subscribe_ids; + uint32_t subscribe_ids[32]; +}; + +#define pw_endpoint_link_resource(r,m,v,...) \ + pw_resource_call(r,struct pw_endpoint_link_events,m,v,__VA_ARGS__) +#define pw_endpoint_link_resource_info(r,...) \ + pw_endpoint_link_resource(r,info,0,__VA_ARGS__) +#define pw_endpoint_link_resource_param(r,...) \ + pw_endpoint_link_resource(r,param,0,__VA_ARGS__) + +static int endpoint_link_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_link *this = data->link; + 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; + + 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_link_resource_param(resource, seq, id, index, next, result); + + if (++count == num) + break; + } + return 0; +} + +static int endpoint_link_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->link, pw_resource_get_id(resource), ids[i]); + endpoint_link_enum_params(resource, 1, ids[i], 0, UINT32_MAX, NULL); + } + return 0; +} + +static int endpoint_link_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_link *this = data->link; + + pw_client_session_resource_set_param(this->client_sess->resource, + id, flags, param); + + return 0; +} + +static int endpoint_link_request_state(void *object, enum pw_endpoint_link_state state) +{ + struct pw_resource *resource = object; + struct resource_data *data = pw_resource_get_user_data(resource); + struct endpoint_link *this = data->link; + + pw_client_session_resource_link_request_state(this->client_sess->resource, + this->id, state); + + return 0; +} + +static const struct pw_endpoint_link_methods methods = { + PW_VERSION_ENDPOINT_LINK_METHODS, + .subscribe_params = endpoint_link_subscribe_params, + .enum_params = endpoint_link_enum_params, + .set_param = endpoint_link_set_param, + .request_state = endpoint_link_request_state, +}; + +struct emit_param_data { + struct endpoint_link *this; + struct spa_pod *param; + uint32_t id; + uint32_t index; + uint32_t next; +}; + +static int emit_param(void *_data, struct pw_resource *resource) +{ + struct emit_param_data *d = _data; + struct resource_data *data; + uint32_t i; + + data = pw_resource_get_user_data(resource); + for (i = 0; i < data->n_subscribe_ids; i++) { + if (data->subscribe_ids[i] == d->id) { + pw_endpoint_link_resource_param(resource, 1, + d->id, d->index, d->next, d->param); + } + } + return 0; +} + +static void endpoint_link_notify_subscribed(struct endpoint_link *this, + uint32_t index, uint32_t next) +{ + struct pw_global *global = this->global; + struct emit_param_data data; + struct spa_pod *param = this->params[index]; + + if (!param || !spa_pod_is_object (param)) + return; + + data.this = this; + data.param = param; + data.id = SPA_POD_OBJECT_ID (param); + data.index = index; + data.next = next; + + pw_global_for_each_resource(global, emit_param, &data); +} + +static int emit_info(void *data, struct pw_resource *resource) +{ + struct endpoint_link *this = data; + pw_endpoint_link_resource_info(resource, &this->info); + return 0; +} + +int endpoint_link_update(struct endpoint_link *this, + uint32_t change_mask, + uint32_t n_params, + const struct spa_pod **params, + const struct pw_endpoint_link_info *info) +{ + if (change_mask & PW_CLIENT_SESSION_UPDATE_PARAMS) { + uint32_t i; + + pw_log_debug(NAME" %p: update %d params", this, n_params); + + for (i = 0; i < this->n_params; i++) + free(this->params[i]); + this->n_params = n_params; + if (this->n_params == 0) { + free(this->params); + this->params = NULL; + } else { + void *p; + p = pw_reallocarray(this->params, n_params, sizeof(struct spa_pod*)); + if (p == NULL) { + free(this->params); + this->params = NULL; + this->n_params = 0; + goto no_mem; + } + this->params = p; + } + for (i = 0; i < this->n_params; i++) { + this->params[i] = params[i] ? spa_pod_copy(params[i]) : NULL; + endpoint_link_notify_subscribed(this, i, i+1); + } + } + + if (change_mask & PW_CLIENT_SESSION_UPDATE_INFO) { + if (info->change_mask & PW_ENDPOINT_LINK_CHANGE_MASK_STATE) { + this->info.state = info->state; + free(this->info.error); + this->info.error = info->error ? strdup(info->error) : NULL; + } + + if (info->change_mask & PW_ENDPOINT_LINK_CHANGE_MASK_PROPS) + pw_properties_update(this->props, info->props); + + if (info->change_mask & PW_ENDPOINT_LINK_CHANGE_MASK_PARAMS) { + this->info.n_params = info->n_params; + if (info->n_params == 0) { + free(this->info.params); + this->info.params = NULL; + } else { + void *p; + p = pw_reallocarray(this->info.params, info->n_params, sizeof(struct spa_param_info)); + if (p == NULL) { + free(this->info.params); + this->info.params = NULL; + this->info.n_params = 0; + goto no_mem; + } + this->info.params = p; + memcpy(this->info.params, info->params, info->n_params * sizeof(struct spa_param_info)); + } + } + + if (!this->info.output_endpoint_id) { + this->info.output_endpoint_id = info->output_endpoint_id; + this->info.output_stream_id = info->output_stream_id; + this->info.input_endpoint_id = info->input_endpoint_id; + this->info.input_stream_id = info->input_stream_id; + } + + this->info.change_mask = info->change_mask; + pw_global_for_each_resource(this->global, emit_info, this); + this->info.change_mask = 0; + } + + return 0; + + no_mem: + pw_log_error(NAME" %p: can't update: no memory", this); + pw_resource_error(this->client_sess->resource, -ENOMEM, + "can't update: no memory"); + return -ENOMEM; +} + +static int endpoint_link_bind(void *_data, struct pw_impl_client *client, + uint32_t permissions, uint32_t version, uint32_t id) +{ + struct endpoint_link *this = _data; + struct pw_global *global = this->global; + struct pw_resource *resource; + struct resource_data *data; + + resource = pw_resource_new(client, id, permissions, + pw_global_get_type(global), version, sizeof(*data)); + if (resource == NULL) + goto no_mem; + + data = pw_resource_get_user_data(resource); + data->link = this; + pw_resource_add_object_listener(resource, &data->object_listener, + &methods, resource); + + pw_log_debug(NAME" %p: bound to %d", this, pw_resource_get_id(resource)); + pw_global_add_resource(global, resource); + + this->info.change_mask = PW_ENDPOINT_LINK_CHANGE_MASK_ALL; + pw_endpoint_link_resource_info(resource, &this->info); + this->info.change_mask = 0; + + return 0; + + no_mem: + pw_log_error(NAME" %p: can't create resource: no memory", this); + pw_resource_error(this->client_sess->resource, -ENOMEM, + "can't create resource: no memory"); + return -ENOMEM; +} + +int endpoint_link_init(struct endpoint_link *this, + uint32_t id, uint32_t session_id, + struct client_session *client_sess, + struct pw_context *context, + struct pw_properties *properties) +{ + pw_log_debug(NAME" %p: new", this); + + this->client_sess = client_sess; + this->id = id; + this->props = properties; + + pw_properties_setf(properties, PW_KEY_SESSION_ID, "%u", session_id); + + properties = pw_properties_copy(properties); + if (!properties) + goto no_mem; + + this->global = pw_global_new(context, + PW_TYPE_INTERFACE_EndpointLink, + PW_VERSION_ENDPOINT_LINK, + properties, endpoint_link_bind, this); + if (!this->global) + goto no_mem; + + pw_properties_setf(this->props, PW_KEY_OBJECT_ID, "%u", + pw_global_get_id(this->global)); + pw_properties_setf(this->props, PW_KEY_OBJECT_SERIAL, "%"PRIu64, + pw_global_get_serial(this->global)); + + this->info.version = PW_VERSION_ENDPOINT_LINK_INFO; + this->info.id = pw_global_get_id(this->global); + this->info.session_id = session_id; + this->info.props = &this->props->dict; + + return pw_global_register(this->global); + + no_mem: + pw_log_error(NAME" - can't create - out of memory"); + return -ENOMEM; +} + +void endpoint_link_clear(struct endpoint_link *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.error); + free(this->info.params); + + pw_properties_free(this->props); +} diff --git a/src/modules/module-session-manager/client-session/endpoint-link.h b/src/modules/module-session-manager/client-session/endpoint-link.h new file mode 100644 index 0000000..c75f975 --- /dev/null +++ b/src/modules/module-session-manager/client-session/endpoint-link.h @@ -0,0 +1,65 @@ +/* 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. + */ + +#ifndef MODULE_SESSION_MANAGER_ENDPOINT_LINK_H +#define MODULE_SESSION_MANAGER_ENDPOINT_LINK_H +#include <stdint.h> + +#ifdef __cplusplus +extern "C" { +#endif + +struct client_session; + +struct endpoint_link { + struct spa_list link; + struct client_session *client_sess; + struct pw_global *global; + uint32_t id; /* session-local link id */ + uint32_t n_params; + struct spa_pod **params; + struct pw_endpoint_link_info info; + struct pw_properties *props; /* wrapper of info.props */ +}; + +int endpoint_link_init(struct endpoint_link *this, + uint32_t id, uint32_t session_id, + struct client_session *client_sess, + struct pw_context *context, + struct pw_properties *properties); + +void endpoint_link_clear(struct endpoint_link *this); + +int endpoint_link_update(struct endpoint_link *this, + uint32_t change_mask, + uint32_t n_params, + const struct spa_pod **params, + const struct pw_endpoint_link_info *info); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* MODULE_SESSION_MANAGER_ENDPOINT_LINK_H */ diff --git a/src/modules/module-session-manager/client-session/session.c b/src/modules/module-session-manager/client-session/session.c new file mode 100644 index 0000000..87c1b96 --- /dev/null +++ b/src/modules/module-session-manager/client-session/session.c @@ -0,0 +1,344 @@ +/* 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/impl.h> +#include <pipewire/extensions/session-manager.h> + +#include <spa/pod/filter.h> + +#include "session.h" +#include "client-session.h" + +#define NAME "session" + +struct resource_data { + struct session *session; + struct spa_hook object_listener; + uint32_t n_subscribe_ids; + uint32_t subscribe_ids[32]; +}; + +#define pw_session_resource(r,m,v,...) \ + pw_resource_call(r,struct pw_session_events,m,v,__VA_ARGS__) +#define pw_session_resource_info(r,...) \ + pw_session_resource(r,info,0,__VA_ARGS__) +#define pw_session_resource_param(r,...) \ + pw_session_resource(r,param,0,__VA_ARGS__) + +static int session_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 session *this = data->session; + 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; + + 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_session_resource_param(resource, seq, id, index, next, result); + + if (++count == num) + break; + } + return 0; +} + +static int session_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->session, pw_resource_get_id(resource), ids[i]); + session_enum_params(resource, 1, ids[i], 0, UINT32_MAX, NULL); + } + return 0; +} + +static int session_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 session *this = data->session; + + pw_client_session_resource_set_param(this->client_sess->resource, + id, flags, param); + + return 0; +} + +static const struct pw_session_methods methods = { + PW_VERSION_SESSION_METHODS, + .subscribe_params = session_subscribe_params, + .enum_params = session_enum_params, + .set_param = session_set_param, +}; + +struct emit_param_data { + struct session *this; + struct spa_pod *param; + uint32_t id; + uint32_t index; + uint32_t next; +}; + +static int emit_param(void *_data, struct pw_resource *resource) +{ + struct emit_param_data *d = _data; + struct resource_data *data; + uint32_t i; + + data = pw_resource_get_user_data(resource); + for (i = 0; i < data->n_subscribe_ids; i++) { + if (data->subscribe_ids[i] == d->id) { + pw_session_resource_param(resource, 1, + d->id, d->index, d->next, d->param); + } + } + return 0; +} + +static void session_notify_subscribed(struct session *this, + uint32_t index, uint32_t next) +{ + struct pw_global *global = this->global; + struct emit_param_data data; + struct spa_pod *param = this->params[index]; + + if (!param || !spa_pod_is_object (param)) + return; + + data.this = this; + data.param = param; + data.id = SPA_POD_OBJECT_ID (param); + data.index = index; + data.next = next; + + pw_global_for_each_resource(global, emit_param, &data); +} + +static int emit_info(void *data, struct pw_resource *resource) +{ + struct session *this = data; + pw_session_resource_info(resource, &this->info); + return 0; +} + +int session_update(struct session *this, + uint32_t change_mask, + uint32_t n_params, + const struct spa_pod **params, + const struct pw_session_info *info) +{ + if (change_mask & PW_CLIENT_SESSION_UPDATE_PARAMS) { + uint32_t i; + + pw_log_debug(NAME" %p: update %d params", this, n_params); + + for (i = 0; i < this->n_params; i++) + free(this->params[i]); + this->n_params = n_params; + if (this->n_params == 0) { + free(this->params); + this->params = NULL; + } else { + void *p; + p = pw_reallocarray(this->params, n_params, sizeof(struct spa_pod*)); + if (p == NULL) { + free(this->params); + this->params = NULL; + this->n_params = 0; + goto no_mem; + } + this->params = p; + } + for (i = 0; i < this->n_params; i++) { + this->params[i] = params[i] ? spa_pod_copy(params[i]) : NULL; + session_notify_subscribed(this, i, i+1); + } + } + + if (change_mask & PW_CLIENT_SESSION_UPDATE_INFO) { + if (info->change_mask & PW_SESSION_CHANGE_MASK_PROPS) + pw_properties_update(this->props, info->props); + + if (info->change_mask & PW_SESSION_CHANGE_MASK_PARAMS) { + this->info.n_params = info->n_params; + if (info->n_params == 0) { + free(this->info.params); + this->info.params = NULL; + } else { + void *p; + p = pw_reallocarray(this->info.params, info->n_params, sizeof(struct spa_param_info)); + if (p == NULL) { + free(this->info.params); + this->info.params = NULL; + this->info.n_params = 0; + goto no_mem; + } + this->info.params = p; + memcpy(this->info.params, info->params, info->n_params * sizeof(struct spa_param_info)); + } + } + this->info.change_mask = info->change_mask; + pw_global_for_each_resource(this->global, emit_info, this); + this->info.change_mask = 0; + } + + return 0; + + no_mem: + pw_log_error(NAME" can't update: no memory"); + pw_resource_error(this->client_sess->resource, -ENOMEM, + NAME" can't update: no memory"); + return -ENOMEM; +} + +static int session_bind(void *_data, struct pw_impl_client *client, + uint32_t permissions, uint32_t version, uint32_t id) +{ + struct session *this = _data; + struct pw_global *global = this->global; + struct pw_resource *resource; + struct resource_data *data; + + resource = pw_resource_new(client, id, permissions, + pw_global_get_type(global), version, sizeof(*data)); + if (resource == NULL) + goto no_mem; + + data = pw_resource_get_user_data(resource); + data->session = this; + + pw_resource_add_object_listener(resource, &data->object_listener, + &methods, resource); + + pw_log_debug(NAME" %p: bound to %d", this, pw_resource_get_id(resource)); + pw_global_add_resource(global, resource); + + this->info.change_mask = PW_SESSION_CHANGE_MASK_ALL; + pw_session_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_sess->resource, -ENOMEM, + NAME" can't create resource: no memory"); + return -ENOMEM; +} + +int session_init(struct session *this, + struct client_session *client_sess, + struct pw_context *context, + struct pw_properties *properties) +{ + static const char * const keys[] = { + PW_KEY_OBJECT_SERIAL, + PW_KEY_FACTORY_ID, + PW_KEY_CLIENT_ID, + NULL + }; + + pw_log_debug(NAME" %p: new", this); + + this->client_sess = client_sess; + this->props = properties; + + this->global = pw_global_new (context, + PW_TYPE_INTERFACE_Session, + PW_VERSION_SESSION, + NULL, session_bind, this); + if (!this->global) + goto no_mem; + + pw_properties_setf(this->props, PW_KEY_OBJECT_ID, "%u", + pw_global_get_id(this->global)); + pw_properties_setf(this->props, PW_KEY_OBJECT_SERIAL, "%"PRIu64, + pw_global_get_serial(this->global)); + + this->info.version = PW_VERSION_SESSION_INFO; + this->info.id = pw_global_get_id(this->global); + this->info.props = &this->props->dict; + + pw_global_update_keys(this->global, &this->props->dict, keys); + + pw_resource_set_bound_id(client_sess->resource, this->info.id); + + return pw_global_register(this->global); + + no_mem: + pw_log_error(NAME" - can't create - out of memory"); + return -ENOMEM; +} + +void session_clear(struct session *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.params); + + pw_properties_free(this->props); +} diff --git a/src/modules/module-session-manager/client-session/session.h b/src/modules/module-session-manager/client-session/session.h new file mode 100644 index 0000000..a94b18f --- /dev/null +++ b/src/modules/module-session-manager/client-session/session.h @@ -0,0 +1,62 @@ +/* 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. + */ + +#ifndef MODULE_SESSION_MANAGER_SESSION_H +#define MODULE_SESSION_MANAGER_SESSION_H +#include <stdint.h> + +#ifdef __cplusplus +extern "C" { +#endif + +struct client_session; + +struct session { + struct client_session *client_sess; + struct pw_global *global; + uint32_t n_params; + struct spa_pod **params; + struct pw_session_info info; + struct pw_properties *props; /* wrapper of info.props */ +}; + +int session_init(struct session *this, + struct client_session *client_sess, + struct pw_context *context, + struct pw_properties *properties); + +void session_clear(struct session *this); + +int session_update(struct session *this, + uint32_t change_mask, + uint32_t n_params, + const struct spa_pod **params, + const struct pw_session_info *info); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* MODULE_SESSION_MANAGER_SESSION_H */ diff --git a/src/modules/module-session-manager/endpoint-link.c b/src/modules/module-session-manager/endpoint-link.c new file mode 100644 index 0000000..55ff580 --- /dev/null +++ b/src/modules/module-session-manager/endpoint-link.c @@ -0,0 +1,590 @@ +/* PipeWire + * + * Copyright © 2020 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 <pipewire/impl.h> +#include <pipewire/extensions/session-manager.h> +#include <pipewire/extensions/session-manager/introspect-funcs.h> + +#include <spa/utils/result.h> +#include <spa/pod/builder.h> +#include <spa/pod/filter.h> + +#define MAX_PARAMS 32 + +#define NAME "endpoint-link" + +struct pw_proxy *pw_core_endpoint_link_export(struct pw_core *core, + const char *type, const struct spa_dict *props, void *object, + size_t user_data_size); + +struct impl +{ + struct pw_global *global; + struct spa_hook global_listener; + + union { + struct pw_endpoint_link *link; + struct pw_resource *resource; + }; + struct spa_hook resource_listener; + struct spa_hook link_listener; + + struct pw_endpoint_link_info *cached_info; + struct spa_list cached_params; + + int ping_seq; + bool registered; +}; + +struct param_data +{ + struct spa_list link; + uint32_t id; + struct pw_array params; +}; + +struct resource_data +{ + struct impl *impl; + + struct pw_resource *resource; + struct spa_hook object_listener; + + uint32_t n_subscribe_ids; + uint32_t subscribe_ids[32]; +}; + +struct factory_data +{ + struct pw_impl_factory *factory; + struct spa_hook factory_listener; + + struct pw_impl_module *module; + struct spa_hook module_listener; + + struct pw_export_type export; +}; + +#define pw_endpoint_link_resource(r,m,v,...) \ + pw_resource_call(r,struct pw_endpoint_link_events,m,v,__VA_ARGS__) + +#define pw_endpoint_link_resource_info(r,...) \ + pw_endpoint_link_resource(r,info,0,__VA_ARGS__) +#define pw_endpoint_link_resource_param(r,...) \ + pw_endpoint_link_resource(r,param,0,__VA_ARGS__) + +static int method_enum_params(void *object, int seq, + uint32_t id, uint32_t start, uint32_t num, + const struct spa_pod *filter) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + struct param_data *pdata; + 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", impl, id, start, num); + + spa_list_for_each(pdata, &impl->cached_params, link) { + if (pdata->id != id) + continue; + + while (true) { + index = next++; + if (index >= pw_array_get_len(&pdata->params, void*)) + return 0; + + param = *pw_array_get_unchecked(&pdata->params, index, struct spa_pod*); + + 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", impl, seq, index); + + pw_endpoint_link_resource_param(d->resource, seq, id, index, next, result); + + if (++count == num) + return 0; + } + } + + return 0; +} + +static int method_subscribe_params(void *object, uint32_t *ids, uint32_t n_ids) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + uint32_t i; + + n_ids = SPA_MIN(n_ids, SPA_N_ELEMENTS(d->subscribe_ids)); + d->n_subscribe_ids = n_ids; + + for (i = 0; i < n_ids; i++) { + d->subscribe_ids[i] = ids[i]; + pw_log_debug(NAME" %p: resource %d subscribe param %u", + impl, pw_resource_get_id(d->resource), ids[i]); + method_enum_params(object, 1, ids[i], 0, UINT32_MAX, NULL); + } + return 0; +} + +static int method_set_param(void *object, uint32_t id, uint32_t flags, + const struct spa_pod *param) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + /* store only on the implementation; our cache will be updated + by the param event, since we are subscribed */ + pw_endpoint_link_set_param(impl->link, id, flags, param); + return 0; +} + +static int method_request_state(void *object, enum pw_endpoint_link_state state) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + pw_endpoint_link_request_state(impl->link, state); + return 0; +} + +static const struct pw_endpoint_link_methods link_methods = { + PW_VERSION_ENDPOINT_LINK_METHODS, + .subscribe_params = method_subscribe_params, + .enum_params = method_enum_params, + .set_param = method_set_param, + .request_state = method_request_state, +}; + +static int global_bind(void *object, struct pw_impl_client *client, + uint32_t permissions, uint32_t version, uint32_t id) +{ + struct impl *impl = object; + struct pw_resource *resource; + struct resource_data *data; + + resource = pw_resource_new(client, id, permissions, + PW_TYPE_INTERFACE_EndpointLink, + version, sizeof(*data)); + if (resource == NULL) + return -errno; + + data = pw_resource_get_user_data(resource); + data->impl = impl; + data->resource = resource; + + pw_global_add_resource(impl->global, resource); + + /* resource methods -> implementation */ + pw_resource_add_object_listener(resource, + &data->object_listener, + &link_methods, data); + + impl->cached_info->change_mask = PW_ENDPOINT_LINK_CHANGE_MASK_ALL; + pw_endpoint_link_resource_info(resource, impl->cached_info); + impl->cached_info->change_mask = 0; + + return 0; +} + +static void global_destroy(void *data) +{ + struct impl *impl = data; + spa_hook_remove(&impl->global_listener); + impl->global = NULL; + if (impl->resource) + pw_resource_destroy(impl->resource); + free(impl); +} + +static const struct pw_global_events global_events = { + PW_VERSION_GLOBAL_EVENTS, + .destroy = global_destroy, +}; + +static void impl_resource_destroy(void *data) +{ + struct impl *impl = data; + struct param_data *pdata, *tmp; + + spa_hook_remove(&impl->resource_listener); + spa_hook_remove(&impl->link_listener); + impl->resource = NULL; + + /* clear cache */ + if (impl->cached_info) + pw_endpoint_link_info_free(impl->cached_info); + spa_list_for_each_safe(pdata, tmp, &impl->cached_params, link) { + struct spa_pod **pod; + pw_array_for_each(pod, &pdata->params) + free(*pod); + pw_array_clear(&pdata->params); + spa_list_remove(&pdata->link); + free(pdata); + } + + if (impl->global) + pw_global_destroy(impl->global); +} + +static void register_global(struct impl *impl) +{ + impl->cached_info->id = pw_global_get_id (impl->global); + pw_resource_set_bound_id(impl->resource, impl->cached_info->id); + pw_global_register(impl->global); + impl->registered = true; +} + +static void impl_resource_pong (void *data, int seq) +{ + struct impl *impl = data; + + /* complete registration, if this was the initial sync */ + if (!impl->registered && seq == impl->ping_seq) { + register_global(impl); + } +} + +static const struct pw_resource_events impl_resource_events = { + PW_VERSION_RESOURCE_EVENTS, + .destroy = impl_resource_destroy, + .pong = impl_resource_pong, +}; + +static int emit_info(void *data, struct pw_resource *resource) +{ + const struct pw_endpoint_link_info *info = data; + pw_endpoint_link_resource_info(resource, info); + return 0; +} + +static void event_info(void *data, const struct pw_endpoint_link_info *info) +{ + struct impl *impl = data; + uint32_t changed_ids[MAX_PARAMS], n_changed_ids = 0; + uint32_t i; + + /* figure out changes to params */ + if (info->change_mask & PW_ENDPOINT_LINK_CHANGE_MASK_PARAMS) { + for (i = 0; i < info->n_params; i++) { + if ((!impl->cached_info || + info->params[i].flags != impl->cached_info->params[i].flags) + && info->params[i].flags & SPA_PARAM_INFO_READ) + changed_ids[n_changed_ids++] = info->params[i].id; + } + } + + /* cache for new clients */ + impl->cached_info = pw_endpoint_link_info_update (impl->cached_info, info); + + /* notify existing clients */ + pw_global_for_each_resource(impl->global, emit_info, (void*) info); + + /* cache params & register */ + if (n_changed_ids > 0) { + /* prepare params storage */ + for (i = 0; i < n_changed_ids; i++) { + struct param_data *pdata = calloc(1, sizeof(struct param_data)); + pdata->id = changed_ids[i]; + pw_array_init(&pdata->params, sizeof(void*)); + spa_list_append(&impl->cached_params, &pdata->link); + } + + /* subscribe to impl */ + pw_endpoint_link_subscribe_params(impl->link, changed_ids, n_changed_ids); + + /* register asynchronously on the pong event */ + impl->ping_seq = pw_resource_ping(impl->resource, 0); + } + else if (!impl->registered) { + register_global(impl); + } +} + +struct param_event_args +{ + uint32_t id, index, next; + const struct spa_pod *param; +}; + +static int emit_param(void *_data, struct pw_resource *resource) +{ + struct param_event_args *args = _data; + struct resource_data *data; + uint32_t i; + + data = pw_resource_get_user_data(resource); + for (i = 0; i < data->n_subscribe_ids; i++) { + if (data->subscribe_ids[i] == args->id) { + pw_endpoint_link_resource_param(resource, 1, + args->id, args->index, args->next, args->param); + } + } + return 0; +} + +static void event_param(void *data, int seq, + uint32_t id, uint32_t index, uint32_t next, + const struct spa_pod *param) +{ + struct impl *impl = data; + struct param_data *pdata; + struct spa_pod **pod; + struct param_event_args args = { id, index, next, param }; + + /* cache for new requests */ + spa_list_for_each(pdata, &impl->cached_params, link) { + if (pdata->id != id) + continue; + + if (!pw_array_check_index(&pdata->params, index, void*)) { + while (pw_array_get_len(&pdata->params, void*) <= index) + pw_array_add_ptr(&pdata->params, NULL); + } + + pod = pw_array_get_unchecked(&pdata->params, index, struct spa_pod*); + free(*pod); + *pod = spa_pod_copy(param); + } + + /* notify existing clients */ + pw_global_for_each_resource(impl->global, emit_param, &args); +} + +static const struct pw_endpoint_link_events link_events = { + PW_VERSION_ENDPOINT_LINK_EVENTS, + .info = event_info, + .param = event_param, +}; + +static void *link_new(struct pw_context *context, + struct pw_resource *resource, + struct pw_properties *properties) +{ + struct impl *impl; + char serial_str[32]; + struct spa_dict_item items[1] = { + SPA_DICT_ITEM_INIT(PW_KEY_OBJECT_SERIAL, serial_str), + }; + struct spa_dict extra_props = SPA_DICT_INIT_ARRAY(items); + static const char * const keys[] = { + PW_KEY_OBJECT_SERIAL, + NULL + }; + + impl = calloc(1, sizeof(*impl)); + if (impl == NULL) { + pw_properties_free(properties); + return NULL; + } + + impl->global = pw_global_new(context, + PW_TYPE_INTERFACE_EndpointLink, + PW_VERSION_ENDPOINT_LINK, + properties, + global_bind, impl); + if (impl->global == NULL) { + free(impl); + return NULL; + } + impl->resource = resource; + + spa_scnprintf(serial_str, sizeof(serial_str), "%"PRIu64, + pw_global_get_serial(impl->global)); + pw_global_update_keys(impl->global, &extra_props, keys); + + spa_list_init(&impl->cached_params); + + /* handle destroy events */ + pw_global_add_listener(impl->global, + &impl->global_listener, + &global_events, impl); + pw_resource_add_listener(impl->resource, + &impl->resource_listener, + &impl_resource_events, impl); + + /* handle implementation events -> cache + client resources */ + pw_endpoint_link_add_listener(impl->link, + &impl->link_listener, + &link_events, impl); + + /* global is not registered here on purpose; + we first cache info + params and then expose the global */ + + return impl; +} + +static void *create_object(void *data, + struct pw_resource *resource, + const char *type, + uint32_t version, + struct pw_properties *properties, + uint32_t new_id) +{ + struct factory_data *d = data; + struct pw_resource *impl_resource; + struct pw_impl_client *client = pw_resource_get_client(resource); + void *result; + int res; + + impl_resource = pw_resource_new(client, new_id, PW_PERM_ALL, type, version, 0); + if (impl_resource == NULL) { + res = -errno; + goto error_resource; + } + + pw_resource_install_marshal(impl_resource, true); + + if (properties == NULL) + properties = pw_properties_new(NULL, NULL); + if (properties == NULL) { + res = -ENOMEM; + goto error_link; + } + + pw_properties_setf(properties, PW_KEY_CLIENT_ID, "%d", + pw_impl_client_get_info(client)->id); + pw_properties_setf(properties, PW_KEY_FACTORY_ID, "%d", + pw_impl_factory_get_info(d->factory)->id); + + result = link_new(pw_impl_client_get_context(client), impl_resource, properties); + if (result == NULL) { + res = -errno; + goto error_link; + } + return result; + +error_resource: + pw_log_error("can't create resource: %s", spa_strerror(res)); + pw_resource_errorf_id(resource, new_id, res, "can't create resource: %s", spa_strerror(res)); + goto error_exit; +error_link: + pw_log_error("can't create endpoint link: %s", spa_strerror(res)); + pw_resource_errorf_id(resource, new_id, res, "can't create endpoint link: %s", spa_strerror(res)); + goto error_exit_free; + +error_exit_free: + pw_resource_remove(impl_resource); +error_exit: + errno = -res; + return NULL; +} + +static const struct pw_impl_factory_implementation impl_factory = { + PW_VERSION_IMPL_FACTORY_IMPLEMENTATION, + .create_object = create_object, +}; + +static void factory_destroy(void *data) +{ + struct factory_data *d = data; + spa_hook_remove(&d->factory_listener); + d->factory = NULL; + if (d->module) + pw_impl_module_destroy(d->module); +} + +static const struct pw_impl_factory_events factory_events = { + PW_VERSION_IMPL_FACTORY_EVENTS, + .destroy = factory_destroy, +}; + +static void module_destroy(void *data) +{ + struct factory_data *d = data; + + spa_hook_remove(&d->module_listener); + spa_list_remove(&d->export.link); + d->module = NULL; + + if (d->factory) + pw_impl_factory_destroy(d->factory); +} + +static void module_registered(void *data) +{ + struct factory_data *d = data; + struct pw_impl_module *module = d->module; + struct pw_impl_factory *factory = d->factory; + struct spa_dict_item items[1]; + char id[16]; + int res; + + snprintf(id, sizeof(id), "%d", pw_impl_module_get_info(module)->id); + items[0] = SPA_DICT_ITEM_INIT(PW_KEY_MODULE_ID, id); + pw_impl_factory_update_properties(factory, &SPA_DICT_INIT(items, 1)); + + if ((res = pw_impl_factory_register(factory, NULL)) < 0) { + pw_log_error(NAME" %p: can't register factory: %s", factory, spa_strerror(res)); + } +} + +static const struct pw_impl_module_events module_events = { + PW_VERSION_IMPL_MODULE_EVENTS, + .destroy = module_destroy, + .registered = module_registered, +}; + +int endpoint_link_factory_init(struct pw_impl_module *module) +{ + struct pw_context *context = pw_impl_module_get_context(module); + struct pw_impl_factory *factory; + struct factory_data *data; + int res; + + factory = pw_context_create_factory(context, + "endpoint-link", + PW_TYPE_INTERFACE_EndpointLink, + PW_VERSION_ENDPOINT_LINK, + NULL, + sizeof(*data)); + if (factory == NULL) + return -errno; + + data = pw_impl_factory_get_user_data(factory); + data->factory = factory; + data->module = module; + + pw_impl_factory_set_implementation(factory, &impl_factory, data); + + data->export.type = PW_TYPE_INTERFACE_EndpointLink; + data->export.func = pw_core_endpoint_link_export; + if ((res = pw_context_register_export_type(context, &data->export)) < 0) + goto error; + + pw_impl_factory_add_listener(factory, &data->factory_listener, &factory_events, data); + pw_impl_module_add_listener(module, &data->module_listener, &module_events, data); + + return 0; +error: + pw_impl_factory_destroy(data->factory); + return res; +} diff --git a/src/modules/module-session-manager/endpoint-stream.c b/src/modules/module-session-manager/endpoint-stream.c new file mode 100644 index 0000000..ce2abdb --- /dev/null +++ b/src/modules/module-session-manager/endpoint-stream.c @@ -0,0 +1,581 @@ +/* PipeWire + * + * Copyright © 2020 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 <pipewire/impl.h> +#include <pipewire/extensions/session-manager.h> +#include <pipewire/extensions/session-manager/introspect-funcs.h> + +#include <spa/utils/result.h> +#include <spa/pod/builder.h> +#include <spa/pod/filter.h> + +#define MAX_PARAMS 32 + +#define NAME "endpoint-stream" + +struct pw_proxy *pw_core_endpoint_stream_export(struct pw_core *core, + const char *type, const struct spa_dict *props, void *object, + size_t user_data_size); + +struct impl +{ + struct pw_global *global; + struct spa_hook global_listener; + + union { + struct pw_endpoint_stream *stream; + struct pw_resource *resource; + }; + struct spa_hook resource_listener; + struct spa_hook stream_listener; + + struct pw_endpoint_stream_info *cached_info; + struct spa_list cached_params; + + int ping_seq; + bool registered; +}; + +struct param_data +{ + struct spa_list link; + uint32_t id; + struct pw_array params; +}; + +struct resource_data +{ + struct impl *impl; + + struct pw_resource *resource; + struct spa_hook object_listener; + + uint32_t n_subscribe_ids; + uint32_t subscribe_ids[32]; +}; + +struct factory_data +{ + struct pw_impl_module *module; + struct spa_hook module_listener; + + struct pw_impl_factory *factory; + struct spa_hook factory_listener; + + struct pw_export_type export; +}; + +#define pw_endpoint_stream_resource(r,m,v,...) \ + pw_resource_call(r,struct pw_endpoint_stream_events,m,v,__VA_ARGS__) + +#define pw_endpoint_stream_resource_info(r,...) \ + pw_endpoint_stream_resource(r,info,0,__VA_ARGS__) +#define pw_endpoint_stream_resource_param(r,...) \ + pw_endpoint_stream_resource(r,param,0,__VA_ARGS__) + +static int method_enum_params(void *object, int seq, + uint32_t id, uint32_t start, uint32_t num, + const struct spa_pod *filter) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + struct param_data *pdata; + 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", impl, id, start, num); + + spa_list_for_each(pdata, &impl->cached_params, link) { + if (pdata->id != id) + continue; + + while (true) { + index = next++; + if (index >= pw_array_get_len(&pdata->params, void*)) + return 0; + + param = *pw_array_get_unchecked(&pdata->params, index, struct spa_pod*); + + 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", impl, seq, index); + + pw_endpoint_stream_resource_param(d->resource, seq, id, index, next, result); + + if (++count == num) + return 0; + } + } + + return 0; +} + +static int method_subscribe_params(void *object, uint32_t *ids, uint32_t n_ids) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + uint32_t i; + + n_ids = SPA_MIN(n_ids, SPA_N_ELEMENTS(d->subscribe_ids)); + d->n_subscribe_ids = n_ids; + + for (i = 0; i < n_ids; i++) { + d->subscribe_ids[i] = ids[i]; + pw_log_debug(NAME" %p: resource %d subscribe param %u", + impl, pw_resource_get_id(d->resource), ids[i]); + method_enum_params(object, 1, ids[i], 0, UINT32_MAX, NULL); + } + return 0; +} + +static int method_set_param(void *object, uint32_t id, uint32_t flags, + const struct spa_pod *param) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + /* store only on the implementation; our cache will be updated + by the param event, since we are subscribed */ + pw_endpoint_stream_set_param(impl->stream, id, flags, param); + return 0; +} + +static const struct pw_endpoint_stream_methods stream_methods = { + PW_VERSION_ENDPOINT_STREAM_METHODS, + .subscribe_params = method_subscribe_params, + .enum_params = method_enum_params, + .set_param = method_set_param, +}; + +static int global_bind(void *object, struct pw_impl_client *client, + uint32_t permissions, uint32_t version, uint32_t id) +{ + struct impl *impl = object; + struct pw_resource *resource; + struct resource_data *data; + + resource = pw_resource_new(client, id, permissions, + PW_TYPE_INTERFACE_EndpointStream, + version, sizeof(*data)); + if (resource == NULL) + return -errno; + + data = pw_resource_get_user_data(resource); + data->impl = impl; + data->resource = resource; + + pw_global_add_resource(impl->global, resource); + + /* resource methods -> implementation */ + pw_resource_add_object_listener(resource, + &data->object_listener, + &stream_methods, data); + + impl->cached_info->change_mask = PW_ENDPOINT_STREAM_CHANGE_MASK_ALL; + pw_endpoint_stream_resource_info(resource, impl->cached_info); + impl->cached_info->change_mask = 0; + + return 0; +} + +static void global_destroy(void *data) +{ + struct impl *impl = data; + spa_hook_remove(&impl->global_listener); + impl->global = NULL; + if (impl->resource) + pw_resource_destroy(impl->resource); + free(impl); +} + +static const struct pw_global_events global_events = { + PW_VERSION_GLOBAL_EVENTS, + .destroy = global_destroy, +}; + +static void impl_resource_destroy(void *data) +{ + struct impl *impl = data; + struct param_data *pdata, *tmp; + + spa_hook_remove(&impl->resource_listener); + spa_hook_remove(&impl->stream_listener); + impl->resource = NULL; + + /* clear cache */ + if (impl->cached_info) + pw_endpoint_stream_info_free(impl->cached_info); + spa_list_for_each_safe(pdata, tmp, &impl->cached_params, link) { + struct spa_pod **pod; + pw_array_for_each(pod, &pdata->params) + free(*pod); + pw_array_clear(&pdata->params); + spa_list_remove(&pdata->link); + free(pdata); + } + + if (impl->global) + pw_global_destroy(impl->global); +} + +static void register_global(struct impl *impl) +{ + impl->cached_info->id = pw_global_get_id (impl->global); + pw_resource_set_bound_id(impl->resource, impl->cached_info->id); + pw_global_register(impl->global); + impl->registered = true; +} + +static void impl_resource_pong (void *data, int seq) +{ + struct impl *impl = data; + + /* complete registration, if this was the initial sync */ + if (!impl->registered && seq == impl->ping_seq) { + register_global(impl); + } +} + +static const struct pw_resource_events impl_resource_events = { + PW_VERSION_RESOURCE_EVENTS, + .destroy = impl_resource_destroy, + .pong = impl_resource_pong, +}; + +static int emit_info(void *data, struct pw_resource *resource) +{ + const struct pw_endpoint_stream_info *info = data; + pw_endpoint_stream_resource_info(resource, info); + return 0; +} + +static void event_info(void *data, const struct pw_endpoint_stream_info *info) +{ + struct impl *impl = data; + uint32_t changed_ids[MAX_PARAMS], n_changed_ids = 0; + uint32_t i; + + /* figure out changes to params */ + if (info->change_mask & PW_ENDPOINT_STREAM_CHANGE_MASK_PARAMS) { + for (i = 0; i < info->n_params; i++) { + if ((!impl->cached_info || + info->params[i].flags != impl->cached_info->params[i].flags) + && info->params[i].flags & SPA_PARAM_INFO_READ) + changed_ids[n_changed_ids++] = info->params[i].id; + } + } + + /* cache for new clients */ + impl->cached_info = pw_endpoint_stream_info_update (impl->cached_info, info); + + /* notify existing clients */ + pw_global_for_each_resource(impl->global, emit_info, (void*) info); + + /* cache params & register */ + if (n_changed_ids > 0) { + /* prepare params storage */ + for (i = 0; i < n_changed_ids; i++) { + struct param_data *pdata = calloc(1, sizeof(struct param_data)); + pdata->id = changed_ids[i]; + pw_array_init(&pdata->params, sizeof(void*)); + spa_list_append(&impl->cached_params, &pdata->link); + } + + /* subscribe to impl */ + pw_endpoint_stream_subscribe_params(impl->stream, changed_ids, n_changed_ids); + + /* register asynchronously on the pong event */ + impl->ping_seq = pw_resource_ping(impl->resource, 0); + } + else if (!impl->registered) { + register_global(impl); + } +} + +struct param_event_args +{ + uint32_t id, index, next; + const struct spa_pod *param; +}; + +static int emit_param(void *_data, struct pw_resource *resource) +{ + struct param_event_args *args = _data; + struct resource_data *data; + uint32_t i; + + data = pw_resource_get_user_data(resource); + for (i = 0; i < data->n_subscribe_ids; i++) { + if (data->subscribe_ids[i] == args->id) { + pw_endpoint_stream_resource_param(resource, 1, + args->id, args->index, args->next, args->param); + } + } + return 0; +} + +static void event_param(void *data, int seq, + uint32_t id, uint32_t index, uint32_t next, + const struct spa_pod *param) +{ + struct impl *impl = data; + struct param_data *pdata; + struct spa_pod **pod; + struct param_event_args args = { id, index, next, param }; + + /* cache for new requests */ + spa_list_for_each(pdata, &impl->cached_params, link) { + if (pdata->id != id) + continue; + + if (!pw_array_check_index(&pdata->params, index, void*)) { + while (pw_array_get_len(&pdata->params, void*) <= index) + pw_array_add_ptr(&pdata->params, NULL); + } + + pod = pw_array_get_unchecked(&pdata->params, index, struct spa_pod*); + free(*pod); + *pod = spa_pod_copy(param); + } + + /* notify existing clients */ + pw_global_for_each_resource(impl->global, emit_param, &args); +} + +static const struct pw_endpoint_stream_events stream_events = { + PW_VERSION_ENDPOINT_STREAM_EVENTS, + .info = event_info, + .param = event_param, +}; + +static void *stream_new(struct pw_context *context, + struct pw_resource *resource, + struct pw_properties *properties) +{ + struct impl *impl; + char serial_str[32]; + struct spa_dict_item items[1] = { + SPA_DICT_ITEM_INIT(PW_KEY_OBJECT_SERIAL, serial_str), + }; + struct spa_dict extra_props = SPA_DICT_INIT_ARRAY(items); + static const char * const keys[] = { + PW_KEY_OBJECT_SERIAL, + NULL + }; + + impl = calloc(1, sizeof(*impl)); + if (impl == NULL) { + pw_properties_free(properties); + return NULL; + } + + impl->global = pw_global_new(context, + PW_TYPE_INTERFACE_EndpointStream, + PW_VERSION_ENDPOINT_STREAM, + properties, + global_bind, impl); + if (impl->global == NULL) { + free(impl); + return NULL; + } + impl->resource = resource; + + spa_scnprintf(serial_str, sizeof(serial_str), "%"PRIu64, + pw_global_get_serial(impl->global)); + pw_global_update_keys(impl->global, &extra_props, keys); + + spa_list_init(&impl->cached_params); + + /* handle destroy events */ + pw_global_add_listener(impl->global, + &impl->global_listener, + &global_events, impl); + pw_resource_add_listener(impl->resource, + &impl->resource_listener, + &impl_resource_events, impl); + + /* handle implementation events -> cache + client resources */ + pw_endpoint_stream_add_listener(impl->stream, + &impl->stream_listener, + &stream_events, impl); + + /* global is not registered here on purpose; + we first cache info + params and then expose the global */ + + return impl; +} + +static void *create_object(void *data, + struct pw_resource *resource, + const char *type, + uint32_t version, + struct pw_properties *properties, + uint32_t new_id) +{ + struct factory_data *d = data; + struct pw_resource *impl_resource; + struct pw_impl_client *client = pw_resource_get_client(resource); + void *result; + int res; + + impl_resource = pw_resource_new(client, new_id, PW_PERM_ALL, type, version, 0); + if (impl_resource == NULL) { + res = -errno; + goto error_resource; + } + + pw_resource_install_marshal(impl_resource, true); + + if (properties == NULL) + properties = pw_properties_new(NULL, NULL); + if (properties == NULL) { + res = -ENOMEM; + goto error_stream; + } + + pw_properties_setf(properties, PW_KEY_CLIENT_ID, "%d", + pw_impl_client_get_info(client)->id); + pw_properties_setf(properties, PW_KEY_FACTORY_ID, "%d", + pw_impl_factory_get_info(d->factory)->id); + + result = stream_new(pw_impl_client_get_context(client), impl_resource, properties); + if (result == NULL) { + res = -errno; + goto error_stream; + } + return result; + +error_resource: + pw_log_error("can't create resource: %s", spa_strerror(res)); + pw_resource_errorf_id(resource, new_id, res, "can't create resource: %s", spa_strerror(res)); + goto error_exit; +error_stream: + pw_log_error("can't create endpoint stream: %s", spa_strerror(res)); + pw_resource_errorf_id(resource, new_id, res, "can't create endpoint stream: %s", spa_strerror(res)); + goto error_exit_free; + +error_exit_free: + pw_resource_remove(impl_resource); +error_exit: + errno = -res; + return NULL; +} + +static const struct pw_impl_factory_implementation impl_factory = { + PW_VERSION_IMPL_FACTORY_IMPLEMENTATION, + .create_object = create_object, +}; + +static void factory_destroy(void *data) +{ + struct factory_data *d = data; + spa_hook_remove(&d->factory_listener); + d->factory = NULL; + if (d->module) + pw_impl_module_destroy(d->module); +} + +static const struct pw_impl_factory_events factory_events = { + PW_VERSION_IMPL_FACTORY_EVENTS, + .destroy = factory_destroy, +}; + +static void module_destroy(void *data) +{ + struct factory_data *d = data; + + spa_hook_remove(&d->module_listener); + spa_list_remove(&d->export.link); + d->module = NULL; + + if (d->factory) + pw_impl_factory_destroy(d->factory); +} + +static void module_registered(void *data) +{ + struct factory_data *d = data; + struct pw_impl_module *module = d->module; + struct pw_impl_factory *factory = d->factory; + struct spa_dict_item items[1]; + char id[16]; + int res; + + snprintf(id, sizeof(id), "%d", pw_impl_module_get_info(module)->id); + items[0] = SPA_DICT_ITEM_INIT(PW_KEY_MODULE_ID, id); + pw_impl_factory_update_properties(factory, &SPA_DICT_INIT(items, 1)); + + if ((res = pw_impl_factory_register(factory, NULL)) < 0) { + pw_log_error(NAME" %p: can't register factory: %s", factory, spa_strerror(res)); + } +} + +static const struct pw_impl_module_events module_events = { + PW_VERSION_IMPL_MODULE_EVENTS, + .destroy = module_destroy, + .registered = module_registered, +}; + +int endpoint_stream_factory_init(struct pw_impl_module *module) +{ + struct pw_context *context = pw_impl_module_get_context(module); + struct pw_impl_factory *factory; + struct factory_data *data; + int res; + + factory = pw_context_create_factory(context, + "endpoint-stream", + PW_TYPE_INTERFACE_EndpointStream, + PW_VERSION_ENDPOINT_STREAM, + NULL, + sizeof(*data)); + if (factory == NULL) + return -errno; + + data = pw_impl_factory_get_user_data(factory); + data->factory = factory; + data->module = module; + + pw_impl_factory_set_implementation(factory, &impl_factory, data); + + data->export.type = PW_TYPE_INTERFACE_EndpointStream; + data->export.func = pw_core_endpoint_stream_export; + if ((res = pw_context_register_export_type(context, &data->export)) < 0) + goto error; + + pw_impl_factory_add_listener(factory, &data->factory_listener, &factory_events, data); + pw_impl_module_add_listener(module, &data->module_listener, &module_events, data); + + return 0; +error: + pw_impl_factory_destroy(data->factory); + return res; +} diff --git a/src/modules/module-session-manager/endpoint.c b/src/modules/module-session-manager/endpoint.c new file mode 100644 index 0000000..752a529 --- /dev/null +++ b/src/modules/module-session-manager/endpoint.c @@ -0,0 +1,590 @@ +/* PipeWire + * + * Copyright © 2020 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 <pipewire/impl.h> +#include <pipewire/extensions/session-manager.h> +#include <pipewire/extensions/session-manager/introspect-funcs.h> + +#include <spa/utils/result.h> +#include <spa/pod/builder.h> +#include <spa/pod/filter.h> + +#define MAX_PARAMS 32 + +#define NAME "endpoint" + +struct pw_proxy *pw_core_endpoint_export(struct pw_core *core, + const char *type, const struct spa_dict *props, void *object, + size_t user_data_size); + +struct impl +{ + struct pw_global *global; + struct spa_hook global_listener; + + union { + struct pw_endpoint *endpoint; + struct pw_resource *resource; + }; + struct spa_hook resource_listener; + struct spa_hook endpoint_listener; + + struct pw_endpoint_info *cached_info; + struct spa_list cached_params; + + int ping_seq; + bool registered; +}; + +struct param_data +{ + struct spa_list link; + uint32_t id; + struct pw_array params; +}; + +struct resource_data +{ + struct impl *impl; + + struct pw_resource *resource; + struct spa_hook object_listener; + + uint32_t n_subscribe_ids; + uint32_t subscribe_ids[32]; +}; + +struct factory_data +{ + struct pw_impl_module *module; + struct spa_hook module_listener; + + struct pw_impl_factory *factory; + struct spa_hook factory_listener; + + struct pw_export_type export; +}; + +#define pw_endpoint_resource(r,m,v,...) \ + pw_resource_call(r,struct pw_endpoint_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 method_enum_params(void *object, int seq, + uint32_t id, uint32_t start, uint32_t num, + const struct spa_pod *filter) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + struct param_data *pdata; + 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", impl, id, start, num); + + spa_list_for_each(pdata, &impl->cached_params, link) { + if (pdata->id != id) + continue; + + while (true) { + index = next++; + if (index >= pw_array_get_len(&pdata->params, void*)) + return 0; + + param = *pw_array_get_unchecked(&pdata->params, index, struct spa_pod*); + + 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", impl, seq, index); + + pw_endpoint_resource_param(d->resource, seq, id, index, next, result); + + if (++count == num) + return 0; + } + } + + return 0; +} + +static int method_subscribe_params(void *object, uint32_t *ids, uint32_t n_ids) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + uint32_t i; + + n_ids = SPA_MIN(n_ids, SPA_N_ELEMENTS(d->subscribe_ids)); + d->n_subscribe_ids = n_ids; + + for (i = 0; i < n_ids; i++) { + d->subscribe_ids[i] = ids[i]; + pw_log_debug(NAME" %p: resource %d subscribe param %u", + impl, pw_resource_get_id(d->resource), ids[i]); + method_enum_params(object, 1, ids[i], 0, UINT32_MAX, NULL); + } + return 0; +} + +static int method_set_param(void *object, uint32_t id, uint32_t flags, + const struct spa_pod *param) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + /* store only on the implementation; our cache will be updated + by the param event, since we are subscribed */ + pw_endpoint_set_param(impl->endpoint, id, flags, param); + return 0; +} + +static int method_create_link(void *object, const struct spa_dict *props) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + pw_endpoint_create_link(impl->endpoint, props); + return 0; +} + +static const struct pw_endpoint_methods endpoint_methods = { + PW_VERSION_ENDPOINT_METHODS, + .subscribe_params = method_subscribe_params, + .enum_params = method_enum_params, + .set_param = method_set_param, + .create_link = method_create_link, +}; + +static int global_bind(void *object, struct pw_impl_client *client, + uint32_t permissions, uint32_t version, uint32_t id) +{ + struct impl *impl = object; + struct pw_resource *resource; + struct resource_data *data; + + resource = pw_resource_new(client, id, permissions, + PW_TYPE_INTERFACE_Endpoint, + version, sizeof(*data)); + if (resource == NULL) + return -errno; + + data = pw_resource_get_user_data(resource); + data->impl = impl; + data->resource = resource; + + pw_global_add_resource(impl->global, resource); + + /* resource methods -> implementation */ + pw_resource_add_object_listener(resource, + &data->object_listener, + &endpoint_methods, data); + + impl->cached_info->change_mask = PW_ENDPOINT_CHANGE_MASK_ALL; + pw_endpoint_resource_info(resource, impl->cached_info); + impl->cached_info->change_mask = 0; + + return 0; +} + +static void global_destroy(void *data) +{ + struct impl *impl = data; + spa_hook_remove(&impl->global_listener); + impl->global = NULL; + if (impl->resource) + pw_resource_destroy(impl->resource); + free(impl); +} + +static const struct pw_global_events global_events = { + PW_VERSION_GLOBAL_EVENTS, + .destroy = global_destroy, +}; + +static void impl_resource_destroy(void *data) +{ + struct impl *impl = data; + struct param_data *pdata, *tmp; + + spa_hook_remove(&impl->resource_listener); + spa_hook_remove(&impl->endpoint_listener); + impl->resource = NULL; + + /* clear cache */ + if (impl->cached_info) + pw_endpoint_info_free(impl->cached_info); + spa_list_for_each_safe(pdata, tmp, &impl->cached_params, link) { + struct spa_pod **pod; + pw_array_for_each(pod, &pdata->params) + free(*pod); + pw_array_clear(&pdata->params); + spa_list_remove(&pdata->link); + free(pdata); + } + + if (impl->global) + pw_global_destroy(impl->global); +} + +static void register_global(struct impl *impl) +{ + impl->cached_info->id = pw_global_get_id (impl->global); + pw_resource_set_bound_id(impl->resource, impl->cached_info->id); + pw_global_register(impl->global); + impl->registered = true; +} + +static void impl_resource_pong (void *data, int seq) +{ + struct impl *impl = data; + + /* complete registration, if this was the initial sync */ + if (!impl->registered && seq == impl->ping_seq) { + register_global(impl); + } +} + +static const struct pw_resource_events impl_resource_events = { + PW_VERSION_RESOURCE_EVENTS, + .destroy = impl_resource_destroy, + .pong = impl_resource_pong, +}; + +static int emit_info(void *data, struct pw_resource *resource) +{ + const struct pw_endpoint_info *info = data; + pw_endpoint_resource_info(resource, info); + return 0; +} + +static void event_info(void *data, const struct pw_endpoint_info *info) +{ + struct impl *impl = data; + uint32_t changed_ids[MAX_PARAMS], n_changed_ids = 0; + uint32_t i; + + /* figure out changes to params */ + if (info->change_mask & PW_ENDPOINT_CHANGE_MASK_PARAMS) { + for (i = 0; i < info->n_params; i++) { + if ((!impl->cached_info || + info->params[i].flags != impl->cached_info->params[i].flags) + && info->params[i].flags & SPA_PARAM_INFO_READ) + changed_ids[n_changed_ids++] = info->params[i].id; + } + } + + /* cache for new clients */ + impl->cached_info = pw_endpoint_info_update (impl->cached_info, info); + + /* notify existing clients */ + pw_global_for_each_resource(impl->global, emit_info, (void*) info); + + /* cache params & register */ + if (n_changed_ids > 0) { + /* prepare params storage */ + for (i = 0; i < n_changed_ids; i++) { + struct param_data *pdata = calloc(1, sizeof(struct param_data)); + pdata->id = changed_ids[i]; + pw_array_init(&pdata->params, sizeof(void*)); + spa_list_append(&impl->cached_params, &pdata->link); + } + + /* subscribe to impl */ + pw_endpoint_subscribe_params(impl->endpoint, changed_ids, n_changed_ids); + + /* register asynchronously on the pong event */ + impl->ping_seq = pw_resource_ping(impl->resource, 0); + } + else if (!impl->registered) { + register_global(impl); + } +} + +struct param_event_args +{ + uint32_t id, index, next; + const struct spa_pod *param; +}; + +static int emit_param(void *_data, struct pw_resource *resource) +{ + struct param_event_args *args = _data; + struct resource_data *data; + uint32_t i; + + data = pw_resource_get_user_data(resource); + for (i = 0; i < data->n_subscribe_ids; i++) { + if (data->subscribe_ids[i] == args->id) { + pw_endpoint_resource_param(resource, 1, + args->id, args->index, args->next, args->param); + } + } + return 0; +} + +static void event_param(void *data, int seq, + uint32_t id, uint32_t index, uint32_t next, + const struct spa_pod *param) +{ + struct impl *impl = data; + struct param_data *pdata; + struct spa_pod **pod; + struct param_event_args args = { id, index, next, param }; + + /* cache for new requests */ + spa_list_for_each(pdata, &impl->cached_params, link) { + if (pdata->id != id) + continue; + + if (!pw_array_check_index(&pdata->params, index, void*)) { + while (pw_array_get_len(&pdata->params, void*) <= index) + pw_array_add_ptr(&pdata->params, NULL); + } + + pod = pw_array_get_unchecked(&pdata->params, index, struct spa_pod*); + free(*pod); + *pod = spa_pod_copy(param); + } + + /* notify existing clients */ + pw_global_for_each_resource(impl->global, emit_param, &args); +} + +static const struct pw_endpoint_events endpoint_events = { + PW_VERSION_ENDPOINT_EVENTS, + .info = event_info, + .param = event_param, +}; + +static void *endpoint_new(struct pw_context *context, + struct pw_resource *resource, + struct pw_properties *properties) +{ + struct impl *impl; + char serial_str[32]; + struct spa_dict_item items[1] = { + SPA_DICT_ITEM_INIT(PW_KEY_OBJECT_SERIAL, serial_str), + }; + struct spa_dict extra_props = SPA_DICT_INIT_ARRAY(items); + static const char * const keys[] = { + PW_KEY_OBJECT_SERIAL, + NULL + }; + + impl = calloc(1, sizeof(*impl)); + if (impl == NULL) { + pw_properties_free(properties); + return NULL; + } + + impl->global = pw_global_new(context, + PW_TYPE_INTERFACE_Endpoint, + PW_VERSION_ENDPOINT, + properties, + global_bind, impl); + if (impl->global == NULL) { + free(impl); + return NULL; + } + impl->resource = resource; + + spa_scnprintf(serial_str, sizeof(serial_str), "%"PRIu64, + pw_global_get_serial(impl->global)); + pw_global_update_keys(impl->global, &extra_props, keys); + + spa_list_init(&impl->cached_params); + + /* handle destroy events */ + pw_global_add_listener(impl->global, + &impl->global_listener, + &global_events, impl); + pw_resource_add_listener(impl->resource, + &impl->resource_listener, + &impl_resource_events, impl); + + /* handle implementation events -> cache + client resources */ + pw_endpoint_add_listener(impl->endpoint, + &impl->endpoint_listener, + &endpoint_events, impl); + + /* global is not registered here on purpose; + we first cache info + params and then expose the global */ + + return impl; +} + +static void *create_object(void *data, + struct pw_resource *resource, + const char *type, + uint32_t version, + struct pw_properties *properties, + uint32_t new_id) +{ + struct factory_data *d = data; + struct pw_resource *impl_resource; + struct pw_impl_client *client = pw_resource_get_client(resource); + void *result; + int res; + + impl_resource = pw_resource_new(client, new_id, PW_PERM_ALL, type, version, 0); + if (impl_resource == NULL) { + res = -errno; + goto error_resource; + } + + pw_resource_install_marshal(impl_resource, true); + + if (properties == NULL) + properties = pw_properties_new(NULL, NULL); + if (properties == NULL) { + res = -ENOMEM; + goto error_endpoint; + } + + pw_properties_setf(properties, PW_KEY_CLIENT_ID, "%d", + pw_impl_client_get_info(client)->id); + pw_properties_setf(properties, PW_KEY_FACTORY_ID, "%d", + pw_impl_factory_get_info(d->factory)->id); + + result = endpoint_new(pw_impl_client_get_context(client), impl_resource, properties); + if (result == NULL) { + res = -errno; + goto error_endpoint; + } + return result; + +error_resource: + pw_log_error("can't create resource: %s", spa_strerror(res)); + pw_resource_errorf_id(resource, new_id, res, "can't create resource: %s", spa_strerror(res)); + goto error_exit; +error_endpoint: + pw_log_error("can't create endpoint: %s", spa_strerror(res)); + pw_resource_errorf_id(resource, new_id, res, "can't create endpoint: %s", spa_strerror(res)); + goto error_exit_free; + +error_exit_free: + pw_resource_remove(impl_resource); +error_exit: + errno = -res; + return NULL; +} + +static const struct pw_impl_factory_implementation impl_factory = { + PW_VERSION_IMPL_FACTORY_IMPLEMENTATION, + .create_object = create_object, +}; + +static void factory_destroy(void *data) +{ + struct factory_data *d = data; + spa_hook_remove(&d->factory_listener); + d->factory = NULL; + if (d->module) + pw_impl_module_destroy(d->module); +} + +static const struct pw_impl_factory_events factory_events = { + PW_VERSION_IMPL_FACTORY_EVENTS, + .destroy = factory_destroy, +}; + +static void module_destroy(void *data) +{ + struct factory_data *d = data; + + spa_hook_remove(&d->module_listener); + spa_list_remove(&d->export.link); + + d->module = NULL; + if (d->factory) + pw_impl_factory_destroy(d->factory); +} + +static void module_registered(void *data) +{ + struct factory_data *d = data; + struct pw_impl_module *module = d->module; + struct pw_impl_factory *factory = d->factory; + struct spa_dict_item items[1]; + char id[16]; + int res; + + snprintf(id, sizeof(id), "%d", pw_impl_module_get_info(module)->id); + items[0] = SPA_DICT_ITEM_INIT(PW_KEY_MODULE_ID, id); + pw_impl_factory_update_properties(factory, &SPA_DICT_INIT(items, 1)); + + if ((res = pw_impl_factory_register(factory, NULL)) < 0) { + pw_log_error(NAME" %p: can't register factory: %s", factory, spa_strerror(res)); + } +} + +static const struct pw_impl_module_events module_events = { + PW_VERSION_IMPL_MODULE_EVENTS, + .destroy = module_destroy, + .registered = module_registered, +}; + +int endpoint_factory_init(struct pw_impl_module *module) +{ + struct pw_context *context = pw_impl_module_get_context(module); + struct pw_impl_factory *factory; + struct factory_data *data; + int res; + + factory = pw_context_create_factory(context, + "endpoint", + PW_TYPE_INTERFACE_Endpoint, + PW_VERSION_ENDPOINT, + NULL, + sizeof(*data)); + if (factory == NULL) + return -errno; + + data = pw_impl_factory_get_user_data(factory); + data->factory = factory; + data->module = module; + + pw_impl_factory_set_implementation(factory, &impl_factory, data); + + data->export.type = PW_TYPE_INTERFACE_Endpoint; + data->export.func = pw_core_endpoint_export; + if ((res = pw_context_register_export_type(context, &data->export)) < 0) + goto error; + + pw_impl_factory_add_listener(factory, &data->factory_listener, &factory_events, data); + pw_impl_module_add_listener(module, &data->module_listener, &module_events, data); + + return 0; +error: + pw_impl_factory_destroy(data->factory); + return res; +} diff --git a/src/modules/module-session-manager/protocol-native.c b/src/modules/module-session-manager/protocol-native.c new file mode 100644 index 0000000..6981252 --- /dev/null +++ b/src/modules/module-session-manager/protocol-native.c @@ -0,0 +1,3083 @@ +/* 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 <pipewire/pipewire.h> + +#include <spa/utils/result.h> +#include <spa/pod/builder.h> +#include <spa/pod/parser.h> + +#include <pipewire/extensions/session-manager.h> +#include <pipewire/extensions/protocol-native.h> + +#define MAX_DICT 1024 +#define MAX_PARAMS 4096 +#define MAX_PARAM_INFO 128 + +static void push_dict(struct spa_pod_builder *b, const struct spa_dict *dict) +{ + struct spa_pod_frame f; + uint32_t n_items; + uint32_t i; + + n_items = dict ? dict->n_items : 0; + + spa_pod_builder_push_struct(b, &f); + spa_pod_builder_add(b, SPA_POD_Int(n_items), NULL); + for (i = 0; i < n_items; i++) { + spa_pod_builder_add(b, + SPA_POD_String(dict->items[i].key), + SPA_POD_String(dict->items[i].value), + NULL); + } + spa_pod_builder_pop(b, &f); +} + +/* macro because of alloca() */ +#define parse_dict(p, f, dict) \ +do { \ + uint32_t i; \ + \ + if (spa_pod_parser_push_struct(p, f) < 0 || \ + spa_pod_parser_get(p, SPA_POD_Int(&(dict)->n_items), NULL) < 0) \ + return -EINVAL; \ + \ + if ((dict)->n_items > 0) { \ + if ((dict)->n_items > MAX_DICT) \ + return -ENOSPC; \ + (dict)->items = alloca((dict)->n_items * sizeof(struct spa_dict_item)); \ + for (i = 0; i < (dict)->n_items; i++) { \ + if (spa_pod_parser_get(p, \ + SPA_POD_String(&(dict)->items[i].key), \ + SPA_POD_String(&(dict)->items[i].value), \ + NULL) < 0) \ + return -EINVAL; \ + } \ + } \ + spa_pod_parser_pop(p, f); \ +} while(0) + +static void push_param_infos(struct spa_pod_builder *b, uint32_t n_params, + const struct spa_param_info *params) +{ + struct spa_pod_frame f; + uint32_t i; + + spa_pod_builder_push_struct(b, &f); + spa_pod_builder_add(b, SPA_POD_Int(n_params), NULL); + for (i = 0; i < n_params; i++) { + spa_pod_builder_add(b, + SPA_POD_Id(params[i].id), + SPA_POD_Int(params[i].flags), + NULL); + } + spa_pod_builder_pop(b, &f); +} + +/* macro because of alloca() */ +#define parse_param_infos(p, f, n_params_p, params_p) \ +do { \ + uint32_t i; \ + \ + if (spa_pod_parser_push_struct(p, f) < 0 || \ + spa_pod_parser_get(p, SPA_POD_Int(n_params_p), NULL) < 0) \ + return -EINVAL; \ + \ + if (*(n_params_p) > 0) { \ + if (*(n_params_p) > MAX_PARAM_INFO) \ + return -ENOSPC; \ + *(params_p) = alloca(*(n_params_p) * sizeof(struct spa_param_info)); \ + for (i = 0; i < *(n_params_p); i++) { \ + if (spa_pod_parser_get(p, \ + SPA_POD_Id(&(*(params_p))[i].id), \ + SPA_POD_Int(&(*(params_p))[i].flags), \ + NULL) < 0) \ + return -EINVAL; \ + } \ + } \ + spa_pod_parser_pop(p, f); \ +} while(0) + +/*********************************************** + * INFO STRUCTURES + ***********************************************/ + +static void +marshal_pw_session_info(struct spa_pod_builder *b, + const struct pw_session_info *info) +{ + struct spa_pod_frame f; + + spa_pod_builder_push_struct(b, &f); + spa_pod_builder_add(b, + SPA_POD_Int(info->version), + SPA_POD_Int(info->id), + SPA_POD_Long(info->change_mask), + NULL); + push_dict(b, info->props); + push_param_infos(b, info->n_params, info->params); + spa_pod_builder_pop(b, &f); +} + +/* macro because of alloca() */ +#define demarshal_pw_session_info(p, f, info) \ +do { \ + struct spa_pod_frame sub_f; \ + uint32_t version; \ + \ + if (spa_pod_parser_push_struct(p, f) < 0 || \ + spa_pod_parser_get(p, \ + SPA_POD_Int(&version), \ + SPA_POD_Int(&(info)->id), \ + SPA_POD_Long(&(info)->change_mask), \ + NULL) < 0) \ + return -EINVAL; \ + \ + (info)->change_mask &= PW_SESSION_CHANGE_MASK_ALL; \ + \ + parse_dict(p, &sub_f, (info)->props); \ + parse_param_infos(p, &sub_f, &(info)->n_params, &(info)->params); \ + \ + spa_pod_parser_pop(p, f); \ +} while(0) + +static void +marshal_pw_endpoint_info(struct spa_pod_builder *b, + const struct pw_endpoint_info *info) +{ + struct spa_pod_frame f; + + spa_pod_builder_push_struct(b, &f); + spa_pod_builder_add(b, + SPA_POD_Int(info->version), + SPA_POD_Int(info->id), + SPA_POD_String(info->name), + SPA_POD_String(info->media_class), + SPA_POD_Int(info->direction), + SPA_POD_Int(info->flags), + SPA_POD_Long(info->change_mask), + SPA_POD_Int(info->n_streams), + SPA_POD_Int(info->session_id), + NULL); + push_dict(b, info->props); + push_param_infos(b, info->n_params, info->params); + spa_pod_builder_pop(b, &f); +} + +/* macro because of alloca() */ +#define demarshal_pw_endpoint_info(p, f, info) \ +do { \ + struct spa_pod_frame sub_f; \ + uint32_t version; \ + \ + if (spa_pod_parser_push_struct(p, f) < 0 || \ + spa_pod_parser_get(p, \ + SPA_POD_Int(&version), \ + SPA_POD_Int(&(info)->id), \ + SPA_POD_String(&(info)->name), \ + SPA_POD_String(&(info)->media_class), \ + SPA_POD_Int(&(info)->direction), \ + SPA_POD_Int(&(info)->flags), \ + SPA_POD_Long(&(info)->change_mask), \ + SPA_POD_Int(&(info)->n_streams), \ + SPA_POD_Int(&(info)->session_id), \ + NULL) < 0) \ + return -EINVAL; \ + \ + (info)->change_mask &= PW_ENDPOINT_CHANGE_MASK_ALL; \ + \ + parse_dict(p, &sub_f, (info)->props); \ + parse_param_infos(p, &sub_f, &(info)->n_params, &(info)->params); \ + \ + spa_pod_parser_pop(p, f); \ +} while(0) + +static void +marshal_pw_endpoint_stream_info(struct spa_pod_builder *b, + const struct pw_endpoint_stream_info *info) +{ + struct spa_pod_frame f; + + spa_pod_builder_push_struct(b, &f); + spa_pod_builder_add(b, + SPA_POD_Int(info->version), + SPA_POD_Int(info->id), + SPA_POD_Int(info->endpoint_id), + SPA_POD_String(info->name), + SPA_POD_Long(info->change_mask), + SPA_POD_Pod(info->link_params), + NULL); + push_dict(b, info->props); + push_param_infos(b, info->n_params, info->params); + spa_pod_builder_pop(b, &f); +} + +/* macro because of alloca() */ +#define demarshal_pw_endpoint_stream_info(p, f, info) \ +do { \ + struct spa_pod_frame sub_f; \ + uint32_t version; \ + \ + if (spa_pod_parser_push_struct(p, f) < 0 || \ + spa_pod_parser_get(p, \ + SPA_POD_Int(&version), \ + SPA_POD_Int(&(info)->id), \ + SPA_POD_Int(&(info)->endpoint_id), \ + SPA_POD_String(&(info)->name), \ + SPA_POD_Long(&(info)->change_mask), \ + SPA_POD_Pod(&(info)->link_params), \ + NULL) < 0) \ + return -EINVAL; \ + \ + (info)->change_mask &= PW_ENDPOINT_STREAM_CHANGE_MASK_ALL; \ + \ + parse_dict(p, &sub_f, (info)->props); \ + parse_param_infos(p, &sub_f, &(info)->n_params, &(info)->params); \ + \ + spa_pod_parser_pop(p, f); \ +} while(0) + +static void +marshal_pw_endpoint_link_info(struct spa_pod_builder *b, + const struct pw_endpoint_link_info *info) +{ + struct spa_pod_frame f; + + spa_pod_builder_push_struct(b, &f); + spa_pod_builder_add(b, + SPA_POD_Int(info->version), + SPA_POD_Int(info->id), + SPA_POD_Int(info->session_id), + SPA_POD_Int(info->output_endpoint_id), + SPA_POD_Int(info->output_stream_id), + SPA_POD_Int(info->input_endpoint_id), + SPA_POD_Int(info->input_stream_id), + SPA_POD_Long(info->change_mask), + SPA_POD_Int(info->state), + SPA_POD_String(info->error), + NULL); + push_dict(b, info->props); + push_param_infos(b, info->n_params, info->params); + spa_pod_builder_pop(b, &f); +} + +/* macro because of alloca() */ +#define demarshal_pw_endpoint_link_info(p, f, info) \ +do { \ + struct spa_pod_frame sub_f; \ + uint32_t version; \ + \ + if (spa_pod_parser_push_struct(p, f) < 0 || \ + spa_pod_parser_get(p, \ + SPA_POD_Int(&version), \ + SPA_POD_Int(&(info)->id), \ + SPA_POD_Int(&(info)->session_id), \ + SPA_POD_Int(&(info)->output_endpoint_id), \ + SPA_POD_Int(&(info)->output_stream_id), \ + SPA_POD_Int(&(info)->input_endpoint_id), \ + SPA_POD_Int(&(info)->input_stream_id), \ + SPA_POD_Long(&(info)->change_mask), \ + SPA_POD_Int(&(info)->state), \ + SPA_POD_String(&(info)->error), \ + NULL) < 0) \ + return -EINVAL; \ + \ + (info)->change_mask &= PW_ENDPOINT_LINK_CHANGE_MASK_ALL; \ + \ + parse_dict(p, &sub_f, (info)->props); \ + parse_param_infos(p, &sub_f, &(info)->n_params, &(info)->params); \ + \ + spa_pod_parser_pop(p, f); \ +} while(0) + + +/*********************************************** + * COMMON + ***********************************************/ + +static int demarshal_add_listener_enotsup(void *object, + const struct pw_protocol_native_message *msg) +{ + return -ENOTSUP; +} + +/*********************************************** + * CLIENT ENDPOINT + ***********************************************/ + +static int client_endpoint_marshal_set_session_id (void *object, uint32_t id) +{ + struct pw_resource *resource = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_CLIENT_ENDPOINT_EVENT_SET_SESSION_ID, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Int(id)); + + return pw_protocol_native_end_resource(resource, b); +} + +static int client_endpoint_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, + PW_CLIENT_ENDPOINT_EVENT_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 client_endpoint_marshal_stream_set_param (void *object, + uint32_t stream_id, 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, + PW_CLIENT_ENDPOINT_EVENT_STREAM_SET_PARAM, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Int(stream_id), + SPA_POD_Id(id), + SPA_POD_Int(flags), + SPA_POD_Pod(param)); + + return pw_protocol_native_end_resource(resource, b); +} + +static int client_endpoint_marshal_create_link (void *object, + const struct spa_dict *props) +{ + struct pw_resource *resource = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_CLIENT_ENDPOINT_EVENT_CREATE_LINK, NULL); + + push_dict(b, props); + + return pw_protocol_native_end_resource(resource, b); +} + +static int client_endpoint_marshal_add_listener(void *object, + struct spa_hook *listener, + const struct pw_client_endpoint_events *events, + void *data) +{ + struct pw_proxy *proxy = object; + pw_proxy_add_object_listener(proxy, listener, events, data); + return 0; +} + +static int client_endpoint_marshal_update(void *object, + uint32_t change_mask, + uint32_t n_params, + const struct spa_pod **params, + const struct pw_endpoint_info *info) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + struct spa_pod_frame f; + uint32_t i; + + b = pw_protocol_native_begin_proxy(proxy, + PW_CLIENT_ENDPOINT_METHOD_UPDATE, NULL); + + spa_pod_builder_push_struct(b, &f); + spa_pod_builder_add(b, + SPA_POD_Int(change_mask), + SPA_POD_Int(n_params), + NULL); + + for (i = 0; i < n_params; i++) + spa_pod_builder_add(b, SPA_POD_Pod(params[i]), NULL); + + if (info) + marshal_pw_endpoint_info(b, info); + else + spa_pod_builder_add(b, SPA_POD_Pod(NULL), NULL); + + spa_pod_builder_pop(b, &f); + + return pw_protocol_native_end_proxy(proxy, b); +} + +static int client_endpoint_marshal_stream_update(void *object, + uint32_t stream_id, + uint32_t change_mask, + uint32_t n_params, + const struct spa_pod **params, + const struct pw_endpoint_stream_info *info) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + struct spa_pod_frame f; + uint32_t i; + + b = pw_protocol_native_begin_proxy(proxy, + PW_CLIENT_ENDPOINT_METHOD_STREAM_UPDATE, NULL); + + spa_pod_builder_push_struct(b, &f); + spa_pod_builder_add(b, + SPA_POD_Int(stream_id), + SPA_POD_Int(change_mask), + SPA_POD_Int(n_params), + NULL); + + for (i = 0; i < n_params; i++) + spa_pod_builder_add(b, SPA_POD_Pod(params[i]), NULL); + + if (info) + marshal_pw_endpoint_stream_info(b, info); + else + spa_pod_builder_add(b, SPA_POD_Pod(NULL), NULL); + + spa_pod_builder_pop(b, &f); + + return pw_protocol_native_end_proxy(proxy, b); +} + +static int client_endpoint_demarshal_set_session_id(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = data; + struct spa_pod_parser prs; + uint32_t id; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, + SPA_POD_Int(&id)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_client_endpoint_events, + set_session_id, 0, id); +} + +static int client_endpoint_demarshal_set_param(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = data; + struct spa_pod_parser prs; + uint32_t id, flags; + const struct spa_pod *param = NULL; + + 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_PodObject(¶m)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_client_endpoint_events, + set_param, 0, id, flags, param); +} + +static int client_endpoint_demarshal_stream_set_param(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = data; + struct spa_pod_parser prs; + uint32_t stream_id, id, flags; + const struct spa_pod *param = NULL; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, + SPA_POD_Int(&stream_id), + SPA_POD_Id(&id), + SPA_POD_Int(&flags), + SPA_POD_PodObject(¶m)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_client_endpoint_events, + stream_set_param, 0, stream_id, id, flags, param); +} + +static int client_endpoint_demarshal_create_link(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = data; + struct spa_pod_parser prs; + struct spa_pod_frame f; + struct spa_dict props = SPA_DICT_INIT(NULL, 0); + + spa_pod_parser_init(&prs, msg->data, msg->size); + + parse_dict(&prs, &f, &props); + + return pw_proxy_notify(proxy, struct pw_client_endpoint_events, + create_link, 0, &props); +} + +static int client_endpoint_demarshal_update(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs[2]; + struct spa_pod_frame f[2]; + uint32_t change_mask, n_params; + const struct spa_pod **params = NULL; + struct spa_dict props = SPA_DICT_INIT(NULL, 0); + struct pw_endpoint_info info = { .props = &props }, *infop = NULL; + struct spa_pod *ipod; + uint32_t i; + + spa_pod_parser_init(&prs[0], msg->data, msg->size); + if (spa_pod_parser_push_struct(&prs[0], &f[0]) < 0 || + spa_pod_parser_get(&prs[0], + SPA_POD_Int(&change_mask), + SPA_POD_Int(&n_params), NULL) < 0) + return -EINVAL; + + if (n_params > MAX_PARAMS) + return -ENOSPC; + if (n_params > 0) + params = alloca(n_params * sizeof(struct spa_pod *)); + for (i = 0; i < n_params; i++) + if (spa_pod_parser_get(&prs[0], + SPA_POD_PodObject(¶ms[i]), NULL) < 0) + return -EINVAL; + + if (spa_pod_parser_get(&prs[0], SPA_POD_PodStruct(&ipod), NULL) < 0) + return -EINVAL; + if (ipod) { + infop = &info; + spa_pod_parser_pod(&prs[1], ipod); + demarshal_pw_endpoint_info(&prs[1], &f[1], infop); + } + + return pw_resource_notify(resource, struct pw_client_endpoint_methods, + update, 0, change_mask, n_params, params, infop); +} + +static int client_endpoint_demarshal_stream_update(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs[2]; + struct spa_pod_frame f[2]; + uint32_t stream_id, change_mask, n_params; + const struct spa_pod **params = NULL; + struct spa_dict props = SPA_DICT_INIT(NULL, 0); + struct pw_endpoint_stream_info info = { .props = &props }, *infop = NULL; + struct spa_pod *ipod; + uint32_t i; + + spa_pod_parser_init(&prs[0], msg->data, msg->size); + if (spa_pod_parser_push_struct(&prs[0], &f[0]) < 0 || + spa_pod_parser_get(&prs[0], + SPA_POD_Int(&stream_id), + SPA_POD_Int(&change_mask), + SPA_POD_Int(&n_params), NULL) < 0) + return -EINVAL; + + if (n_params > MAX_PARAMS) + return -ENOSPC; + if (n_params > 0) + params = alloca(n_params * sizeof(struct spa_pod *)); + for (i = 0; i < n_params; i++) + if (spa_pod_parser_get(&prs[0], + SPA_POD_PodObject(¶ms[i]), NULL) < 0) + return -EINVAL; + + if (spa_pod_parser_get(&prs[0], SPA_POD_PodStruct(&ipod), NULL) < 0) + return -EINVAL; + if (ipod) { + infop = &info; + spa_pod_parser_pod(&prs[1], ipod); + demarshal_pw_endpoint_stream_info(&prs[1], &f[1], infop); + } + + return pw_resource_notify(resource, struct pw_client_endpoint_methods, + stream_update, 0, stream_id, change_mask, n_params, params, infop); +} + +static const struct pw_client_endpoint_events pw_protocol_native_client_endpoint_event_marshal = { + PW_VERSION_CLIENT_ENDPOINT_EVENTS, + .set_session_id = client_endpoint_marshal_set_session_id, + .set_param = client_endpoint_marshal_set_param, + .stream_set_param = client_endpoint_marshal_stream_set_param, + .create_link = client_endpoint_marshal_create_link, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_client_endpoint_event_demarshal[PW_CLIENT_ENDPOINT_EVENT_NUM] = +{ + [PW_CLIENT_ENDPOINT_EVENT_SET_SESSION_ID] = { client_endpoint_demarshal_set_session_id, 0 }, + [PW_CLIENT_ENDPOINT_EVENT_SET_PARAM] = { client_endpoint_demarshal_set_param, 0 }, + [PW_CLIENT_ENDPOINT_EVENT_STREAM_SET_PARAM] = { client_endpoint_demarshal_stream_set_param, 0 }, + [PW_CLIENT_ENDPOINT_EVENT_CREATE_LINK] = { client_endpoint_demarshal_create_link, 0 }, +}; + +static const struct pw_client_endpoint_methods pw_protocol_native_client_endpoint_method_marshal = { + PW_VERSION_CLIENT_ENDPOINT_METHODS, + .add_listener = client_endpoint_marshal_add_listener, + .update = client_endpoint_marshal_update, + .stream_update = client_endpoint_marshal_stream_update, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_client_endpoint_method_demarshal[PW_CLIENT_ENDPOINT_METHOD_NUM] = +{ + [PW_CLIENT_ENDPOINT_METHOD_ADD_LISTENER] = { NULL, 0 }, + [PW_CLIENT_ENDPOINT_METHOD_UPDATE] = { client_endpoint_demarshal_update, 0 }, + [PW_CLIENT_ENDPOINT_METHOD_STREAM_UPDATE] = { client_endpoint_demarshal_stream_update, 0 }, +}; + +static const struct pw_protocol_marshal pw_protocol_native_client_endpoint_marshal = { + PW_TYPE_INTERFACE_ClientEndpoint, + PW_VERSION_CLIENT_ENDPOINT, + 0, + PW_CLIENT_ENDPOINT_METHOD_NUM, + PW_CLIENT_ENDPOINT_EVENT_NUM, + &pw_protocol_native_client_endpoint_method_marshal, + &pw_protocol_native_client_endpoint_method_demarshal, + &pw_protocol_native_client_endpoint_event_marshal, + &pw_protocol_native_client_endpoint_event_demarshal, +}; + +/*********************************************** + * CLIENT SESSION + ***********************************************/ + +static int client_session_marshal_set_param (void *data, + uint32_t id, uint32_t flags, + const struct spa_pod *param) +{ + struct pw_resource *resource = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_CLIENT_SESSION_EVENT_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 client_session_marshal_link_set_param (void *data, + uint32_t link_id, uint32_t id, + uint32_t flags, const struct spa_pod *param) +{ + struct pw_resource *resource = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_CLIENT_SESSION_EVENT_LINK_SET_PARAM, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Int(link_id), + SPA_POD_Id(id), + SPA_POD_Int(flags), + SPA_POD_Pod(param)); + + return pw_protocol_native_end_resource(resource, b); +} + +static int client_session_marshal_link_request_state (void *data, + uint32_t link_id, uint32_t state) +{ + struct pw_resource *resource = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_CLIENT_SESSION_EVENT_LINK_REQUEST_STATE, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Int(link_id), + SPA_POD_Int(state)); + + return pw_protocol_native_end_resource(resource, b); +} + +static int client_session_marshal_add_listener(void *object, + struct spa_hook *listener, + const struct pw_client_session_events *events, + void *data) +{ + struct pw_proxy *proxy = object; + pw_proxy_add_object_listener(proxy, listener, events, data); + return 0; +} + +static int client_session_marshal_update(void *object, + uint32_t change_mask, + uint32_t n_params, + const struct spa_pod **params, + const struct pw_session_info *info) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + struct spa_pod_frame f; + uint32_t i; + + b = pw_protocol_native_begin_proxy(proxy, + PW_CLIENT_SESSION_METHOD_UPDATE, NULL); + + spa_pod_builder_push_struct(b, &f); + spa_pod_builder_add(b, + SPA_POD_Int(change_mask), + SPA_POD_Int(n_params), + NULL); + + for (i = 0; i < n_params; i++) + spa_pod_builder_add(b, SPA_POD_Pod(params[i]), NULL); + + if (info) + marshal_pw_session_info(b, info); + else + spa_pod_builder_add(b, SPA_POD_Pod(NULL), NULL); + + spa_pod_builder_pop(b, &f); + + return pw_protocol_native_end_proxy(proxy, b); +} + +static int client_session_marshal_link_update(void *object, + uint32_t link_id, + uint32_t change_mask, + uint32_t n_params, + const struct spa_pod **params, + const struct pw_endpoint_link_info *info) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + struct spa_pod_frame f; + uint32_t i; + + b = pw_protocol_native_begin_proxy(proxy, + PW_CLIENT_SESSION_METHOD_LINK_UPDATE, NULL); + + spa_pod_builder_push_struct(b, &f); + spa_pod_builder_add(b, + SPA_POD_Int(link_id), + SPA_POD_Int(change_mask), + SPA_POD_Int(n_params), + NULL); + + for (i = 0; i < n_params; i++) + spa_pod_builder_add(b, SPA_POD_Pod(params[i]), NULL); + + if (info) + marshal_pw_endpoint_link_info(b, info); + else + spa_pod_builder_add(b, SPA_POD_Pod(NULL), NULL); + + spa_pod_builder_pop(b, &f); + + return pw_protocol_native_end_proxy(proxy, b); +} + +static int client_session_demarshal_set_param(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = data; + struct spa_pod_parser prs; + uint32_t id, flags; + const struct spa_pod *param = NULL; + + 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_PodObject(¶m)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_client_session_events, + set_param, 0, id, flags, param); +} + +static int client_session_demarshal_link_set_param(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = data; + struct spa_pod_parser prs; + uint32_t link_id, id, flags; + const struct spa_pod *param = NULL; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, + SPA_POD_Int(&link_id), + SPA_POD_Id(&id), + SPA_POD_Int(&flags), + SPA_POD_PodObject(¶m)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_client_session_events, + link_set_param, 0, link_id, id, flags, param); +} + +static int client_session_demarshal_link_request_state(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = data; + struct spa_pod_parser prs; + uint32_t link_id, state; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, + SPA_POD_Int(&link_id), + SPA_POD_Int(&state)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_client_session_events, + link_request_state, 0, link_id, state); +} + +static int client_session_demarshal_update(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs[2]; + struct spa_pod_frame f[2]; + uint32_t change_mask, n_params; + const struct spa_pod **params = NULL; + struct spa_dict props = SPA_DICT_INIT(NULL, 0); + struct pw_session_info info = { .props = &props }, *infop = NULL; + struct spa_pod *ipod; + uint32_t i; + + spa_pod_parser_init(&prs[0], msg->data, msg->size); + if (spa_pod_parser_push_struct(&prs[0], &f[0]) < 0 || + spa_pod_parser_get(&prs[0], + SPA_POD_Int(&change_mask), + SPA_POD_Int(&n_params), NULL) < 0) + return -EINVAL; + + if (n_params > MAX_PARAMS) + return -ENOSPC; + if (n_params > 0) + params = alloca(n_params * sizeof(struct spa_pod *)); + for (i = 0; i < n_params; i++) + if (spa_pod_parser_get(&prs[0], + SPA_POD_PodObject(¶ms[i]), NULL) < 0) + return -EINVAL; + + if (spa_pod_parser_get(&prs[0], SPA_POD_PodStruct(&ipod), NULL) < 0) + return -EINVAL; + if (ipod) { + infop = &info; + spa_pod_parser_pod(&prs[1], ipod); + demarshal_pw_session_info(&prs[1], &f[1], infop); + } + + return pw_resource_notify(resource, struct pw_client_session_methods, + update, 0, change_mask, n_params, params, infop); +} + +static int client_session_demarshal_link_update(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs[2]; + struct spa_pod_frame f[2]; + uint32_t link_id, change_mask, n_params; + const struct spa_pod **params = NULL; + struct spa_dict props = SPA_DICT_INIT(NULL, 0); + struct pw_endpoint_link_info info = { .props = &props }, *infop = NULL; + struct spa_pod *ipod; + uint32_t i; + + spa_pod_parser_init(&prs[0], msg->data, msg->size); + if (spa_pod_parser_push_struct(&prs[0], &f[0]) < 0 || + spa_pod_parser_get(&prs[0], + SPA_POD_Int(&link_id), + SPA_POD_Int(&change_mask), + SPA_POD_Int(&n_params), NULL) < 0) + return -EINVAL; + + if (n_params > MAX_PARAMS) + return -ENOSPC; + if (n_params > 0) + params = alloca(n_params * sizeof(struct spa_pod *)); + for (i = 0; i < n_params; i++) + if (spa_pod_parser_get(&prs[0], + SPA_POD_PodObject(¶ms[i]), NULL) < 0) + return -EINVAL; + + if (spa_pod_parser_get(&prs[0], SPA_POD_PodStruct(&ipod), NULL) < 0) + return -EINVAL; + if (ipod) { + infop = &info; + spa_pod_parser_pod(&prs[1], ipod); + demarshal_pw_endpoint_link_info(&prs[1], &f[1], infop); + } + + return pw_resource_notify(resource, struct pw_client_session_methods, + link_update, 0, link_id, change_mask, n_params, params, infop); +} + +static const struct pw_client_session_events pw_protocol_native_client_session_event_marshal = { + PW_VERSION_CLIENT_SESSION_EVENTS, + .set_param = client_session_marshal_set_param, + .link_set_param = client_session_marshal_link_set_param, + .link_request_state = client_session_marshal_link_request_state, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_client_session_event_demarshal[PW_CLIENT_SESSION_EVENT_NUM] = +{ + [PW_CLIENT_SESSION_EVENT_SET_PARAM] = { client_session_demarshal_set_param, 0 }, + [PW_CLIENT_SESSION_EVENT_LINK_SET_PARAM] = { client_session_demarshal_link_set_param, 0 }, + [PW_CLIENT_SESSION_EVENT_LINK_REQUEST_STATE] = { client_session_demarshal_link_request_state, 0 }, +}; + +static const struct pw_client_session_methods pw_protocol_native_client_session_method_marshal = { + PW_VERSION_CLIENT_SESSION_METHODS, + .add_listener = client_session_marshal_add_listener, + .update = client_session_marshal_update, + .link_update = client_session_marshal_link_update, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_client_session_method_demarshal[PW_CLIENT_SESSION_METHOD_NUM] = +{ + [PW_CLIENT_SESSION_METHOD_ADD_LISTENER] = { NULL, 0 }, + [PW_CLIENT_SESSION_METHOD_UPDATE] = { client_session_demarshal_update, 0 }, + [PW_CLIENT_SESSION_METHOD_LINK_UPDATE] = { client_session_demarshal_link_update, 0 }, +}; + +static const struct pw_protocol_marshal pw_protocol_native_client_session_marshal = { + PW_TYPE_INTERFACE_ClientSession, + PW_VERSION_CLIENT_SESSION, + 0, + PW_CLIENT_SESSION_METHOD_NUM, + PW_CLIENT_SESSION_EVENT_NUM, + &pw_protocol_native_client_session_method_marshal, + &pw_protocol_native_client_session_method_demarshal, + &pw_protocol_native_client_session_event_marshal, + &pw_protocol_native_client_session_event_demarshal, +}; + +/*********************************************** + * ENDPOINT LINK + ***********************************************/ + +static void endpoint_link_proxy_marshal_info (void *data, + const struct pw_endpoint_link_info *info) +{ + struct pw_proxy *proxy = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_LINK_EVENT_INFO, NULL); + + marshal_pw_endpoint_link_info(b, info); + + pw_protocol_native_end_proxy(proxy, b); +} + +static void endpoint_link_resource_marshal_info (void *data, + const struct pw_endpoint_link_info *info) +{ + struct pw_resource *resource = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_ENDPOINT_LINK_EVENT_INFO, NULL); + + marshal_pw_endpoint_link_info(b, info); + + pw_protocol_native_end_resource(resource, b); +} + +static void endpoint_link_proxy_marshal_param (void *data, int seq, uint32_t id, + uint32_t index, uint32_t next, + const struct spa_pod *param) +{ + struct pw_proxy *proxy = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_LINK_EVENT_PARAM, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Int(seq), + SPA_POD_Id(id), + SPA_POD_Int(index), + SPA_POD_Int(next), + SPA_POD_Pod(param)); + + pw_protocol_native_end_proxy(proxy, b); +} +static void endpoint_link_resource_marshal_param (void *data, int seq, uint32_t id, + uint32_t index, uint32_t next, + const struct spa_pod *param) +{ + struct pw_resource *resource = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_ENDPOINT_LINK_EVENT_PARAM, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Int(seq), + SPA_POD_Id(id), + SPA_POD_Int(index), + SPA_POD_Int(next), + SPA_POD_Pod(param)); + + pw_protocol_native_end_resource(resource, b); +} + +static int endpoint_link_proxy_marshal_add_listener(void *object, + struct spa_hook *listener, + const struct pw_endpoint_link_events *events, + void *data) +{ + struct pw_proxy *proxy = object; + pw_proxy_add_object_listener(proxy, listener, events, data); + return 0; +} + +static int endpoint_link_resource_marshal_add_listener(void *object, + struct spa_hook *listener, + const struct pw_endpoint_link_events *events, + void *data) +{ + struct pw_resource *resource = object; + pw_resource_add_object_listener(resource, listener, events, data); + return 0; +} + +static int endpoint_link_proxy_marshal_subscribe_params(void *object, + uint32_t *ids, uint32_t n_ids) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_LINK_METHOD_SUBSCRIBE_PARAMS, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Array(sizeof(uint32_t), SPA_TYPE_Id, n_ids, ids)); + + return pw_protocol_native_end_proxy(proxy, b); +} + +static int endpoint_link_resource_marshal_subscribe_params(void *object, + uint32_t *ids, uint32_t n_ids) +{ + struct pw_resource *resource = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_ENDPOINT_LINK_METHOD_SUBSCRIBE_PARAMS, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Array(sizeof(uint32_t), SPA_TYPE_Id, n_ids, ids)); + + return pw_protocol_native_end_resource(resource, b); +} + +static int endpoint_link_proxy_marshal_enum_params(void *object, + int seq, uint32_t id, + uint32_t index, uint32_t num, + const struct spa_pod *filter) +{ + struct pw_protocol_native_message *msg; + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_LINK_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(num), + SPA_POD_Pod(filter)); + + return pw_protocol_native_end_proxy(proxy, b); +} + +static int endpoint_link_resource_marshal_enum_params(void *object, + int seq, uint32_t id, + uint32_t index, uint32_t num, + 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, + PW_ENDPOINT_LINK_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(num), + SPA_POD_Pod(filter)); + + return pw_protocol_native_end_resource(resource, b); +} + +static int endpoint_link_proxy_marshal_set_param(void *object, + uint32_t id, uint32_t flags, + const struct spa_pod *param) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_LINK_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_proxy(proxy, b); +} + +static int endpoint_link_resource_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, + PW_ENDPOINT_LINK_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 endpoint_link_proxy_marshal_request_state(void *object, + enum pw_endpoint_link_state state) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_LINK_METHOD_REQUEST_STATE, NULL); + + spa_pod_builder_add_struct(b, SPA_POD_Int(state)); + + return pw_protocol_native_end_proxy(proxy, b); +} + +static int endpoint_link_resource_marshal_request_state(void *object, + enum pw_endpoint_link_state state) +{ + struct pw_resource *resource = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_ENDPOINT_LINK_METHOD_REQUEST_STATE, NULL); + + spa_pod_builder_add_struct(b, SPA_POD_Int(state)); + + return pw_protocol_native_end_resource(resource, b); +} + +static int endpoint_link_proxy_demarshal_info(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = data; + struct spa_pod_parser prs; + struct spa_pod_frame f; + struct spa_dict props = SPA_DICT_INIT(NULL, 0); + struct pw_endpoint_link_info info = { .props = &props }; + + spa_pod_parser_init(&prs, msg->data, msg->size); + + demarshal_pw_endpoint_link_info(&prs, &f, &info); + + return pw_proxy_notify(proxy, struct pw_endpoint_link_events, + info, 0, &info); +} + +static int endpoint_link_resource_demarshal_info(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = data; + struct spa_pod_parser prs; + struct spa_pod_frame f; + struct spa_dict props = SPA_DICT_INIT(NULL, 0); + struct pw_endpoint_link_info info = { .props = &props }; + + spa_pod_parser_init(&prs, msg->data, msg->size); + + demarshal_pw_endpoint_link_info(&prs, &f, &info); + + return pw_resource_notify(resource, struct pw_endpoint_link_events, + info, 0, &info); +} + +static int endpoint_link_proxy_demarshal_param(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = data; + struct spa_pod_parser prs; + uint32_t id, index, next; + int seq; + struct spa_pod *param; + + 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(&next), + SPA_POD_Pod(¶m)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_endpoint_link_events, + param, 0, seq, id, index, next, param); +} + +static int endpoint_link_resource_demarshal_param(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = data; + struct spa_pod_parser prs; + uint32_t id, index, next; + int seq; + struct spa_pod *param; + + 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(&next), + SPA_POD_Pod(¶m)) < 0) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_endpoint_link_events, + param, 0, seq, id, index, next, param); +} + +static int endpoint_link_proxy_demarshal_subscribe_params(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = object; + struct spa_pod_parser prs; + uint32_t csize, ctype, n_ids; + uint32_t *ids; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, + SPA_POD_Array(&csize, &ctype, &n_ids, &ids)) < 0) + return -EINVAL; + + if (ctype != SPA_TYPE_Id) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_endpoint_link_methods, + subscribe_params, 0, ids, n_ids); +} + +static int endpoint_link_resource_demarshal_subscribe_params(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs; + uint32_t csize, ctype, n_ids; + uint32_t *ids; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, + SPA_POD_Array(&csize, &ctype, &n_ids, &ids)) < 0) + return -EINVAL; + + if (ctype != SPA_TYPE_Id) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_endpoint_link_methods, + subscribe_params, 0, ids, n_ids); +} + +static int endpoint_link_proxy_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, num; + 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(&num), + SPA_POD_Pod(&filter)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_endpoint_link_methods, + enum_params, 0, seq, id, index, num, filter); +} + +static int endpoint_link_resource_demarshal_enum_params(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs; + uint32_t id, index, num; + 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(&num), + SPA_POD_Pod(&filter)) < 0) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_endpoint_link_methods, + enum_params, 0, seq, id, index, num, filter); +} + +static int endpoint_link_proxy_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(¶m)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_endpoint_link_methods, + set_param, 0, id, flags, param); +} + +static int endpoint_link_resource_demarshal_set_param(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = 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(¶m)) < 0) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_endpoint_link_methods, + set_param, 0, id, flags, param); +} + +static int endpoint_link_proxy_demarshal_request_state(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = object; + struct spa_pod_parser prs; + enum pw_endpoint_link_state state; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, + SPA_POD_Int(&state)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_endpoint_link_methods, + request_state, 0, state); +} + +static int endpoint_link_resource_demarshal_request_state(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs; + enum pw_endpoint_link_state state; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, + SPA_POD_Int(&state)) < 0) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_endpoint_link_methods, + request_state, 0, state); +} + +static const struct pw_endpoint_link_events pw_protocol_native_endpoint_link_client_event_marshal = { + PW_VERSION_ENDPOINT_LINK_EVENTS, + .info = endpoint_link_proxy_marshal_info, + .param = endpoint_link_proxy_marshal_param, +}; + +static const struct pw_endpoint_link_events pw_protocol_native_endpoint_link_server_event_marshal = { + PW_VERSION_ENDPOINT_LINK_EVENTS, + .info = endpoint_link_resource_marshal_info, + .param = endpoint_link_resource_marshal_param, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_endpoint_link_client_event_demarshal[PW_ENDPOINT_LINK_EVENT_NUM] = +{ + [PW_ENDPOINT_LINK_EVENT_INFO] = { endpoint_link_proxy_demarshal_info, 0 }, + [PW_ENDPOINT_LINK_EVENT_PARAM] = { endpoint_link_proxy_demarshal_param, 0 }, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_endpoint_link_server_event_demarshal[PW_ENDPOINT_LINK_EVENT_NUM] = +{ + [PW_ENDPOINT_LINK_EVENT_INFO] = { endpoint_link_resource_demarshal_info, 0 }, + [PW_ENDPOINT_LINK_EVENT_PARAM] = { endpoint_link_resource_demarshal_param, 0 }, +}; + +static const struct pw_endpoint_link_methods pw_protocol_native_endpoint_link_client_method_marshal = { + PW_VERSION_ENDPOINT_LINK_METHODS, + .add_listener = endpoint_link_proxy_marshal_add_listener, + .subscribe_params = endpoint_link_proxy_marshal_subscribe_params, + .enum_params = endpoint_link_proxy_marshal_enum_params, + .set_param = endpoint_link_proxy_marshal_set_param, + .request_state = endpoint_link_proxy_marshal_request_state, +}; + +static const struct pw_endpoint_link_methods pw_protocol_native_endpoint_link_server_method_marshal = { + PW_VERSION_ENDPOINT_LINK_METHODS, + .add_listener = endpoint_link_resource_marshal_add_listener, + .subscribe_params = endpoint_link_resource_marshal_subscribe_params, + .enum_params = endpoint_link_resource_marshal_enum_params, + .set_param = endpoint_link_resource_marshal_set_param, + .request_state = endpoint_link_resource_marshal_request_state, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_endpoint_link_client_method_demarshal[PW_ENDPOINT_LINK_METHOD_NUM] = +{ + [PW_ENDPOINT_LINK_METHOD_ADD_LISTENER] = { demarshal_add_listener_enotsup, 0 }, + [PW_ENDPOINT_LINK_METHOD_SUBSCRIBE_PARAMS] = { endpoint_link_proxy_demarshal_subscribe_params, 0 }, + [PW_ENDPOINT_LINK_METHOD_ENUM_PARAMS] = { endpoint_link_proxy_demarshal_enum_params, 0 }, + [PW_ENDPOINT_LINK_METHOD_SET_PARAM] = { endpoint_link_proxy_demarshal_set_param, PW_PERM_W }, + [PW_ENDPOINT_LINK_METHOD_REQUEST_STATE] = { endpoint_link_proxy_demarshal_request_state, PW_PERM_W }, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_endpoint_link_server_method_demarshal[PW_ENDPOINT_LINK_METHOD_NUM] = +{ + [PW_ENDPOINT_LINK_METHOD_ADD_LISTENER] = { demarshal_add_listener_enotsup, 0 }, + [PW_ENDPOINT_LINK_METHOD_SUBSCRIBE_PARAMS] = { endpoint_link_resource_demarshal_subscribe_params, 0 }, + [PW_ENDPOINT_LINK_METHOD_ENUM_PARAMS] = { endpoint_link_resource_demarshal_enum_params, 0 }, + [PW_ENDPOINT_LINK_METHOD_SET_PARAM] = { endpoint_link_resource_demarshal_set_param, PW_PERM_W }, + [PW_ENDPOINT_LINK_METHOD_REQUEST_STATE] = { endpoint_link_resource_demarshal_request_state, PW_PERM_W }, +}; + +static const struct pw_protocol_marshal pw_protocol_native_endpoint_link_marshal = { + PW_TYPE_INTERFACE_EndpointLink, + PW_VERSION_ENDPOINT_LINK, + 0, + PW_ENDPOINT_LINK_METHOD_NUM, + PW_ENDPOINT_LINK_EVENT_NUM, + .client_marshal = &pw_protocol_native_endpoint_link_client_method_marshal, + .server_demarshal = pw_protocol_native_endpoint_link_server_method_demarshal, + .server_marshal = &pw_protocol_native_endpoint_link_server_event_marshal, + .client_demarshal = pw_protocol_native_endpoint_link_client_event_demarshal, +}; + +static const struct pw_protocol_marshal pw_protocol_native_endpoint_link_impl_marshal = { + PW_TYPE_INTERFACE_EndpointLink, + PW_VERSION_ENDPOINT_LINK, + PW_PROTOCOL_MARSHAL_FLAG_IMPL, + PW_ENDPOINT_LINK_EVENT_NUM, + PW_ENDPOINT_LINK_METHOD_NUM, + .client_marshal = &pw_protocol_native_endpoint_link_client_event_marshal, + .server_demarshal = pw_protocol_native_endpoint_link_server_event_demarshal, + .server_marshal = &pw_protocol_native_endpoint_link_server_method_marshal, + .client_demarshal = pw_protocol_native_endpoint_link_client_method_demarshal, +}; + +/*********************************************** + * ENDPOINT STREAM + ***********************************************/ + +static void endpoint_stream_proxy_marshal_info (void *data, + const struct pw_endpoint_stream_info *info) +{ + struct pw_proxy *proxy = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_STREAM_EVENT_INFO, NULL); + + marshal_pw_endpoint_stream_info(b, info); + + pw_protocol_native_end_proxy(proxy, b); +} + +static void endpoint_stream_resource_marshal_info (void *data, + const struct pw_endpoint_stream_info *info) +{ + struct pw_resource *resource = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_ENDPOINT_STREAM_EVENT_INFO, NULL); + + marshal_pw_endpoint_stream_info(b, info); + + pw_protocol_native_end_resource(resource, b); +} + +static void endpoint_stream_proxy_marshal_param (void *data, int seq, uint32_t id, + uint32_t index, uint32_t next, + const struct spa_pod *param) +{ + struct pw_proxy *proxy = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_STREAM_EVENT_PARAM, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Int(seq), + SPA_POD_Id(id), + SPA_POD_Int(index), + SPA_POD_Int(next), + SPA_POD_Pod(param)); + + pw_protocol_native_end_proxy(proxy, b); +} + +static void endpoint_stream_resource_marshal_param (void *data, int seq, uint32_t id, + uint32_t index, uint32_t next, + const struct spa_pod *param) +{ + struct pw_resource *resource = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_ENDPOINT_STREAM_EVENT_PARAM, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Int(seq), + SPA_POD_Id(id), + SPA_POD_Int(index), + SPA_POD_Int(next), + SPA_POD_Pod(param)); + + pw_protocol_native_end_resource(resource, b); +} + +static int endpoint_stream_proxy_marshal_add_listener(void *object, + struct spa_hook *listener, + const struct pw_endpoint_stream_events *events, + void *data) +{ + struct pw_proxy *proxy = object; + pw_proxy_add_object_listener(proxy, listener, events, data); + return 0; +} + +static int endpoint_stream_resource_marshal_add_listener(void *object, + struct spa_hook *listener, + const struct pw_endpoint_stream_events *events, + void *data) +{ + struct pw_resource *resource = object; + pw_resource_add_object_listener(resource, listener, events, data); + return 0; +} + +static int endpoint_stream_proxy_marshal_subscribe_params(void *object, + uint32_t *ids, uint32_t n_ids) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_STREAM_METHOD_SUBSCRIBE_PARAMS, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Array(sizeof(uint32_t), SPA_TYPE_Id, n_ids, ids)); + + return pw_protocol_native_end_proxy(proxy, b); +} + +static int endpoint_stream_resource_marshal_subscribe_params(void *object, + uint32_t *ids, uint32_t n_ids) +{ + struct pw_resource *resource = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_ENDPOINT_STREAM_METHOD_SUBSCRIBE_PARAMS, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Array(sizeof(uint32_t), SPA_TYPE_Id, n_ids, ids)); + + return pw_protocol_native_end_resource(resource, b); +} + +static int endpoint_stream_proxy_marshal_enum_params(void *object, + int seq, uint32_t id, + uint32_t index, uint32_t num, + const struct spa_pod *filter) +{ + struct pw_protocol_native_message *msg; + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_STREAM_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(num), + SPA_POD_Pod(filter)); + + return pw_protocol_native_end_proxy(proxy, b); +} + +static int endpoint_stream_resource_marshal_enum_params(void *object, + int seq, uint32_t id, + uint32_t index, uint32_t num, + 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, + PW_ENDPOINT_STREAM_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(num), + SPA_POD_Pod(filter)); + + return pw_protocol_native_end_resource(resource, b); +} + +static int endpoint_stream_proxy_marshal_set_param(void *object, + uint32_t id, uint32_t flags, + const struct spa_pod *param) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_STREAM_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_proxy(proxy, b); +} + +static int endpoint_stream_resource_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, + PW_ENDPOINT_STREAM_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 endpoint_stream_proxy_demarshal_info(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = data; + struct spa_pod_parser prs; + struct spa_pod_frame f; + struct spa_dict props = SPA_DICT_INIT(NULL, 0); + struct pw_endpoint_stream_info info = { .props = &props }; + + spa_pod_parser_init(&prs, msg->data, msg->size); + + demarshal_pw_endpoint_stream_info(&prs, &f, &info); + + return pw_proxy_notify(proxy, struct pw_endpoint_stream_events, + info, 0, &info); +} + +static int endpoint_stream_resource_demarshal_info(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = data; + struct spa_pod_parser prs; + struct spa_pod_frame f; + struct spa_dict props = SPA_DICT_INIT(NULL, 0); + struct pw_endpoint_stream_info info = { .props = &props }; + + spa_pod_parser_init(&prs, msg->data, msg->size); + + demarshal_pw_endpoint_stream_info(&prs, &f, &info); + + return pw_resource_notify(resource, struct pw_endpoint_stream_events, + info, 0, &info); +} + +static int endpoint_stream_proxy_demarshal_param(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = data; + struct spa_pod_parser prs; + uint32_t id, index, next; + int seq; + struct spa_pod *param; + + 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(&next), + SPA_POD_Pod(¶m)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_endpoint_stream_events, + param, 0, seq, id, index, next, param); +} + +static int endpoint_stream_resource_demarshal_param(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = data; + struct spa_pod_parser prs; + uint32_t id, index, next; + int seq; + struct spa_pod *param; + + 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(&next), + SPA_POD_Pod(¶m)) < 0) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_endpoint_stream_events, + param, 0, seq, id, index, next, param); +} + +static int endpoint_stream_proxy_demarshal_subscribe_params(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = object; + struct spa_pod_parser prs; + uint32_t csize, ctype, n_ids; + uint32_t *ids; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, + SPA_POD_Array(&csize, &ctype, &n_ids, &ids)) < 0) + return -EINVAL; + + if (ctype != SPA_TYPE_Id) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_endpoint_stream_methods, + subscribe_params, 0, ids, n_ids); +} + +static int endpoint_stream_resource_demarshal_subscribe_params(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs; + uint32_t csize, ctype, n_ids; + uint32_t *ids; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, + SPA_POD_Array(&csize, &ctype, &n_ids, &ids)) < 0) + return -EINVAL; + + if (ctype != SPA_TYPE_Id) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_endpoint_stream_methods, + subscribe_params, 0, ids, n_ids); +} + +static int endpoint_stream_proxy_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, num; + 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(&num), + SPA_POD_Pod(&filter)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_endpoint_stream_methods, + enum_params, 0, seq, id, index, num, filter); +} + +static int endpoint_stream_resource_demarshal_enum_params(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs; + uint32_t id, index, num; + 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(&num), + SPA_POD_Pod(&filter)) < 0) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_endpoint_stream_methods, + enum_params, 0, seq, id, index, num, filter); +} + +static int endpoint_stream_proxy_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(¶m)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_endpoint_stream_methods, + set_param, 0, id, flags, param); +} + +static int endpoint_stream_resource_demarshal_set_param(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = 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(¶m)) < 0) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_endpoint_stream_methods, + set_param, 0, id, flags, param); +} + +static const struct pw_endpoint_stream_events pw_protocol_native_endpoint_stream_client_event_marshal = { + PW_VERSION_ENDPOINT_STREAM_EVENTS, + .info = endpoint_stream_proxy_marshal_info, + .param = endpoint_stream_proxy_marshal_param, +}; + +static const struct pw_endpoint_stream_events pw_protocol_native_endpoint_stream_server_event_marshal = { + PW_VERSION_ENDPOINT_STREAM_EVENTS, + .info = endpoint_stream_resource_marshal_info, + .param = endpoint_stream_resource_marshal_param, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_endpoint_stream_client_event_demarshal[PW_ENDPOINT_STREAM_EVENT_NUM] = +{ + [PW_ENDPOINT_STREAM_EVENT_INFO] = { endpoint_stream_proxy_demarshal_info, 0 }, + [PW_ENDPOINT_STREAM_EVENT_PARAM] = { endpoint_stream_proxy_demarshal_param, 0 }, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_endpoint_stream_server_event_demarshal[PW_ENDPOINT_STREAM_EVENT_NUM] = +{ + [PW_ENDPOINT_STREAM_EVENT_INFO] = { endpoint_stream_resource_demarshal_info, 0 }, + [PW_ENDPOINT_STREAM_EVENT_PARAM] = { endpoint_stream_resource_demarshal_param, 0 }, +}; + +static const struct pw_endpoint_stream_methods pw_protocol_native_endpoint_stream_client_method_marshal = { + PW_VERSION_ENDPOINT_STREAM_METHODS, + .add_listener = endpoint_stream_proxy_marshal_add_listener, + .subscribe_params = endpoint_stream_proxy_marshal_subscribe_params, + .enum_params = endpoint_stream_proxy_marshal_enum_params, + .set_param = endpoint_stream_proxy_marshal_set_param, +}; + +static const struct pw_endpoint_stream_methods pw_protocol_native_endpoint_stream_server_method_marshal = { + PW_VERSION_ENDPOINT_STREAM_METHODS, + .add_listener = endpoint_stream_resource_marshal_add_listener, + .subscribe_params = endpoint_stream_resource_marshal_subscribe_params, + .enum_params = endpoint_stream_resource_marshal_enum_params, + .set_param = endpoint_stream_resource_marshal_set_param, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_endpoint_stream_client_method_demarshal[PW_ENDPOINT_STREAM_METHOD_NUM] = +{ + [PW_ENDPOINT_STREAM_METHOD_ADD_LISTENER] = { demarshal_add_listener_enotsup, 0 }, + [PW_ENDPOINT_STREAM_METHOD_SUBSCRIBE_PARAMS] = { endpoint_stream_proxy_demarshal_subscribe_params, 0 }, + [PW_ENDPOINT_STREAM_METHOD_ENUM_PARAMS] = { endpoint_stream_proxy_demarshal_enum_params, 0 }, + [PW_ENDPOINT_STREAM_METHOD_SET_PARAM] = { endpoint_stream_proxy_demarshal_set_param, PW_PERM_W }, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_endpoint_stream_server_method_demarshal[PW_ENDPOINT_STREAM_METHOD_NUM] = +{ + [PW_ENDPOINT_STREAM_METHOD_ADD_LISTENER] = { demarshal_add_listener_enotsup, 0 }, + [PW_ENDPOINT_STREAM_METHOD_SUBSCRIBE_PARAMS] = { endpoint_stream_resource_demarshal_subscribe_params, 0 }, + [PW_ENDPOINT_STREAM_METHOD_ENUM_PARAMS] = { endpoint_stream_resource_demarshal_enum_params, 0 }, + [PW_ENDPOINT_STREAM_METHOD_SET_PARAM] = { endpoint_stream_resource_demarshal_set_param, PW_PERM_W }, +}; + +static const struct pw_protocol_marshal pw_protocol_native_endpoint_stream_marshal = { + PW_TYPE_INTERFACE_EndpointStream, + PW_VERSION_ENDPOINT_STREAM, + 0, + PW_ENDPOINT_STREAM_METHOD_NUM, + PW_ENDPOINT_STREAM_EVENT_NUM, + .client_marshal = &pw_protocol_native_endpoint_stream_client_method_marshal, + .server_demarshal = pw_protocol_native_endpoint_stream_server_method_demarshal, + .server_marshal = &pw_protocol_native_endpoint_stream_server_event_marshal, + .client_demarshal = pw_protocol_native_endpoint_stream_client_event_demarshal, +}; + +static const struct pw_protocol_marshal pw_protocol_native_endpoint_stream_impl_marshal = { + PW_TYPE_INTERFACE_EndpointStream, + PW_VERSION_ENDPOINT_STREAM, + PW_PROTOCOL_MARSHAL_FLAG_IMPL, + PW_ENDPOINT_STREAM_EVENT_NUM, + PW_ENDPOINT_STREAM_METHOD_NUM, + .client_marshal = &pw_protocol_native_endpoint_stream_client_event_marshal, + .server_demarshal = pw_protocol_native_endpoint_stream_server_event_demarshal, + .server_marshal = &pw_protocol_native_endpoint_stream_server_method_marshal, + .client_demarshal = pw_protocol_native_endpoint_stream_client_method_demarshal, +}; + +/*********************************************** + * ENDPOINT + ***********************************************/ + +static void endpoint_proxy_marshal_info (void *data, + const struct pw_endpoint_info *info) +{ + struct pw_proxy *proxy = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_EVENT_INFO, NULL); + + marshal_pw_endpoint_info(b, info); + + pw_protocol_native_end_proxy(proxy, b); +} + +static void endpoint_resource_marshal_info (void *data, + const struct pw_endpoint_info *info) +{ + struct pw_resource *resource = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_ENDPOINT_EVENT_INFO, NULL); + + marshal_pw_endpoint_info(b, info); + + pw_protocol_native_end_resource(resource, b); +} + +static void endpoint_proxy_marshal_param (void *data, int seq, uint32_t id, + uint32_t index, uint32_t next, + const struct spa_pod *param) +{ + struct pw_proxy *proxy = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_EVENT_PARAM, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Int(seq), + SPA_POD_Id(id), + SPA_POD_Int(index), + SPA_POD_Int(next), + SPA_POD_Pod(param)); + + pw_protocol_native_end_proxy(proxy, b); +} + +static void endpoint_resource_marshal_param (void *data, int seq, uint32_t id, + uint32_t index, uint32_t next, + const struct spa_pod *param) +{ + struct pw_resource *resource = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_ENDPOINT_EVENT_PARAM, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Int(seq), + SPA_POD_Id(id), + SPA_POD_Int(index), + SPA_POD_Int(next), + SPA_POD_Pod(param)); + + pw_protocol_native_end_resource(resource, b); +} + +static int endpoint_proxy_marshal_add_listener(void *object, + struct spa_hook *listener, + const struct pw_endpoint_events *events, + void *data) +{ + struct pw_proxy *proxy = object; + pw_proxy_add_object_listener(proxy, listener, events, data); + return 0; +} + +static int endpoint_resource_marshal_add_listener(void *object, + struct spa_hook *listener, + const struct pw_endpoint_events *events, + void *data) +{ + struct pw_resource *resource = object; + pw_resource_add_object_listener(resource, listener, events, data); + return 0; +} + +static int endpoint_proxy_marshal_subscribe_params(void *object, + uint32_t *ids, uint32_t n_ids) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_METHOD_SUBSCRIBE_PARAMS, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Array(sizeof(uint32_t), SPA_TYPE_Id, n_ids, ids)); + + return pw_protocol_native_end_proxy(proxy, b); +} + +static int endpoint_resource_marshal_subscribe_params(void *object, + uint32_t *ids, uint32_t n_ids) +{ + struct pw_resource *resource = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_ENDPOINT_METHOD_SUBSCRIBE_PARAMS, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Array(sizeof(uint32_t), SPA_TYPE_Id, n_ids, ids)); + + return pw_protocol_native_end_resource(resource, b); +} + +static int endpoint_proxy_marshal_enum_params(void *object, + int seq, uint32_t id, + uint32_t index, uint32_t num, + const struct spa_pod *filter) +{ + struct pw_protocol_native_message *msg; + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_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(num), + SPA_POD_Pod(filter)); + + return pw_protocol_native_end_proxy(proxy, b); +} + +static int endpoint_resource_marshal_enum_params(void *object, + int seq, uint32_t id, + uint32_t index, uint32_t num, + 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, + PW_ENDPOINT_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(num), + SPA_POD_Pod(filter)); + + return pw_protocol_native_end_resource(resource, b); +} + +static int endpoint_proxy_marshal_set_param(void *object, + uint32_t id, uint32_t flags, + const struct spa_pod *param) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_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_proxy(proxy, b); +} + +static int endpoint_resource_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, + PW_ENDPOINT_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 endpoint_proxy_marshal_create_link(void *object, + const struct spa_dict *props) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_ENDPOINT_METHOD_CREATE_LINK, NULL); + + push_dict(b, props); + + return pw_protocol_native_end_proxy(proxy, b); +} + +static int endpoint_resource_marshal_create_link(void *object, + const struct spa_dict *props) +{ + struct pw_resource *resource = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_ENDPOINT_METHOD_CREATE_LINK, NULL); + + push_dict(b, props); + + return pw_protocol_native_end_resource(resource, b); +} + +static int endpoint_proxy_demarshal_info(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = data; + struct spa_pod_parser prs; + struct spa_pod_frame f; + struct spa_dict props = SPA_DICT_INIT(NULL, 0); + struct pw_endpoint_info info = { .props = &props }; + + spa_pod_parser_init(&prs, msg->data, msg->size); + + demarshal_pw_endpoint_info(&prs, &f, &info); + + return pw_proxy_notify(proxy, struct pw_endpoint_events, + info, 0, &info); +} + +static int endpoint_resource_demarshal_info(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = data; + struct spa_pod_parser prs; + struct spa_pod_frame f; + struct spa_dict props = SPA_DICT_INIT(NULL, 0); + struct pw_endpoint_info info = { .props = &props }; + + spa_pod_parser_init(&prs, msg->data, msg->size); + + demarshal_pw_endpoint_info(&prs, &f, &info); + + return pw_resource_notify(resource, struct pw_endpoint_events, + info, 0, &info); +} + +static int endpoint_proxy_demarshal_param(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = data; + struct spa_pod_parser prs; + uint32_t id, index, next; + int seq; + struct spa_pod *param; + + 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(&next), + SPA_POD_Pod(¶m)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_endpoint_events, + param, 0, seq, id, index, next, param); +} + +static int endpoint_resource_demarshal_param(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = data; + struct spa_pod_parser prs; + uint32_t id, index, next; + int seq; + struct spa_pod *param; + + 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(&next), + SPA_POD_Pod(¶m)) < 0) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_endpoint_events, + param, 0, seq, id, index, next, param); +} + +static int endpoint_proxy_demarshal_subscribe_params(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = object; + struct spa_pod_parser prs; + uint32_t csize, ctype, n_ids; + uint32_t *ids; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, + SPA_POD_Array(&csize, &ctype, &n_ids, &ids)) < 0) + return -EINVAL; + + if (ctype != SPA_TYPE_Id) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_endpoint_methods, + subscribe_params, 0, ids, n_ids); +} + +static int endpoint_resource_demarshal_subscribe_params(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs; + uint32_t csize, ctype, n_ids; + uint32_t *ids; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, + SPA_POD_Array(&csize, &ctype, &n_ids, &ids)) < 0) + return -EINVAL; + + if (ctype != SPA_TYPE_Id) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_endpoint_methods, + subscribe_params, 0, ids, n_ids); +} + +static int endpoint_proxy_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, num; + 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(&num), + SPA_POD_Pod(&filter)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_endpoint_methods, + enum_params, 0, seq, id, index, num, filter); +} + +static int endpoint_resource_demarshal_enum_params(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs; + uint32_t id, index, num; + 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(&num), + SPA_POD_Pod(&filter)) < 0) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_endpoint_methods, + enum_params, 0, seq, id, index, num, filter); +} + +static int endpoint_proxy_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(¶m)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_endpoint_methods, + set_param, 0, id, flags, param); +} + +static int endpoint_resource_demarshal_set_param(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = 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(¶m)) < 0) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_endpoint_methods, + set_param, 0, id, flags, param); +} + +static int endpoint_proxy_demarshal_create_link(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = object; + struct spa_pod_parser prs; + struct spa_pod_frame f; + struct spa_dict props = SPA_DICT_INIT(NULL, 0); + + spa_pod_parser_init(&prs, msg->data, msg->size); + + parse_dict(&prs, &f, &props); + + return pw_proxy_notify(proxy, struct pw_endpoint_methods, + create_link, 0, &props); +} + +static int endpoint_resource_demarshal_create_link(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs; + struct spa_pod_frame f; + struct spa_dict props = SPA_DICT_INIT(NULL, 0); + + spa_pod_parser_init(&prs, msg->data, msg->size); + + parse_dict(&prs, &f, &props); + + return pw_resource_notify(resource, struct pw_endpoint_methods, + create_link, 0, &props); +} + +static const struct pw_endpoint_events pw_protocol_native_endpoint_client_event_marshal = { + PW_VERSION_ENDPOINT_EVENTS, + .info = endpoint_proxy_marshal_info, + .param = endpoint_proxy_marshal_param, +}; + +static const struct pw_endpoint_events pw_protocol_native_endpoint_server_event_marshal = { + PW_VERSION_ENDPOINT_EVENTS, + .info = endpoint_resource_marshal_info, + .param = endpoint_resource_marshal_param, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_endpoint_client_event_demarshal[PW_ENDPOINT_EVENT_NUM] = +{ + [PW_ENDPOINT_EVENT_INFO] = { endpoint_proxy_demarshal_info, 0 }, + [PW_ENDPOINT_EVENT_PARAM] = { endpoint_proxy_demarshal_param, 0 }, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_endpoint_server_event_demarshal[PW_ENDPOINT_EVENT_NUM] = +{ + [PW_ENDPOINT_EVENT_INFO] = { endpoint_resource_demarshal_info, 0 }, + [PW_ENDPOINT_EVENT_PARAM] = { endpoint_resource_demarshal_param, 0 }, +}; + +static const struct pw_endpoint_methods pw_protocol_native_endpoint_client_method_marshal = { + PW_VERSION_ENDPOINT_METHODS, + .add_listener = endpoint_proxy_marshal_add_listener, + .subscribe_params = endpoint_proxy_marshal_subscribe_params, + .enum_params = endpoint_proxy_marshal_enum_params, + .set_param = endpoint_proxy_marshal_set_param, + .create_link = endpoint_proxy_marshal_create_link, +}; + +static const struct pw_endpoint_methods pw_protocol_native_endpoint_server_method_marshal = { + PW_VERSION_ENDPOINT_METHODS, + .add_listener = endpoint_resource_marshal_add_listener, + .subscribe_params = endpoint_resource_marshal_subscribe_params, + .enum_params = endpoint_resource_marshal_enum_params, + .set_param = endpoint_resource_marshal_set_param, + .create_link = endpoint_resource_marshal_create_link, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_endpoint_client_method_demarshal[PW_ENDPOINT_METHOD_NUM] = +{ + [PW_ENDPOINT_METHOD_ADD_LISTENER] = { demarshal_add_listener_enotsup, 0 }, + [PW_ENDPOINT_METHOD_SUBSCRIBE_PARAMS] = { endpoint_proxy_demarshal_subscribe_params, 0 }, + [PW_ENDPOINT_METHOD_ENUM_PARAMS] = { endpoint_proxy_demarshal_enum_params, 0 }, + [PW_ENDPOINT_METHOD_SET_PARAM] = { endpoint_proxy_demarshal_set_param, PW_PERM_W }, + [PW_ENDPOINT_METHOD_CREATE_LINK] = { endpoint_proxy_demarshal_create_link, PW_PERM_X }, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_endpoint_server_method_demarshal[PW_ENDPOINT_METHOD_NUM] = +{ + [PW_ENDPOINT_METHOD_ADD_LISTENER] = { demarshal_add_listener_enotsup, 0 }, + [PW_ENDPOINT_METHOD_SUBSCRIBE_PARAMS] = { endpoint_resource_demarshal_subscribe_params, 0 }, + [PW_ENDPOINT_METHOD_ENUM_PARAMS] = { endpoint_resource_demarshal_enum_params, 0 }, + [PW_ENDPOINT_METHOD_SET_PARAM] = { endpoint_resource_demarshal_set_param, PW_PERM_W }, + [PW_ENDPOINT_METHOD_CREATE_LINK] = { endpoint_resource_demarshal_create_link, PW_PERM_X }, +}; + +static const struct pw_protocol_marshal pw_protocol_native_endpoint_marshal = { + PW_TYPE_INTERFACE_Endpoint, + PW_VERSION_ENDPOINT, + 0, + PW_ENDPOINT_METHOD_NUM, + PW_ENDPOINT_EVENT_NUM, + .client_marshal = &pw_protocol_native_endpoint_client_method_marshal, + .server_demarshal = pw_protocol_native_endpoint_server_method_demarshal, + .server_marshal = &pw_protocol_native_endpoint_server_event_marshal, + .client_demarshal = pw_protocol_native_endpoint_client_event_demarshal, +}; + +static const struct pw_protocol_marshal pw_protocol_native_endpoint_impl_marshal = { + PW_TYPE_INTERFACE_Endpoint, + PW_VERSION_ENDPOINT, + PW_PROTOCOL_MARSHAL_FLAG_IMPL, + PW_ENDPOINT_EVENT_NUM, + PW_ENDPOINT_METHOD_NUM, + .client_marshal = &pw_protocol_native_endpoint_client_event_marshal, + .server_demarshal = pw_protocol_native_endpoint_server_event_demarshal, + .server_marshal = &pw_protocol_native_endpoint_server_method_marshal, + .client_demarshal = pw_protocol_native_endpoint_client_method_demarshal, +}; + +/*********************************************** + * SESSION + ***********************************************/ + +static void session_proxy_marshal_info (void *data, + const struct pw_session_info *info) +{ + struct pw_proxy *proxy = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_SESSION_EVENT_INFO, NULL); + + marshal_pw_session_info(b, info); + + pw_protocol_native_end_proxy(proxy, b); +} + +static void session_resource_marshal_info (void *data, + const struct pw_session_info *info) +{ + struct pw_resource *resource = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_SESSION_EVENT_INFO, NULL); + + marshal_pw_session_info(b, info); + + pw_protocol_native_end_resource(resource, b); +} + +static void session_proxy_marshal_param (void *data, int seq, uint32_t id, + uint32_t index, uint32_t next, + const struct spa_pod *param) +{ + struct pw_proxy *proxy = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_SESSION_EVENT_PARAM, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Int(seq), + SPA_POD_Id(id), + SPA_POD_Int(index), + SPA_POD_Int(next), + SPA_POD_Pod(param)); + + pw_protocol_native_end_proxy(proxy, b); +} + +static void session_resource_marshal_param (void *data, int seq, uint32_t id, + uint32_t index, uint32_t next, + const struct spa_pod *param) +{ + struct pw_resource *resource = data; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_SESSION_EVENT_PARAM, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Int(seq), + SPA_POD_Id(id), + SPA_POD_Int(index), + SPA_POD_Int(next), + SPA_POD_Pod(param)); + + pw_protocol_native_end_resource(resource, b); +} + +static int session_proxy_marshal_add_listener(void *object, + struct spa_hook *listener, + const struct pw_session_events *events, + void *data) +{ + struct pw_proxy *proxy = object; + pw_proxy_add_object_listener(proxy, listener, events, data); + return 0; +} + +static int session_resource_marshal_add_listener(void *object, + struct spa_hook *listener, + const struct pw_session_events *events, + void *data) +{ + struct pw_resource *resource = object; + pw_resource_add_object_listener(resource, listener, events, data); + return 0; +} + +static int session_proxy_marshal_subscribe_params(void *object, + uint32_t *ids, uint32_t n_ids) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_SESSION_METHOD_SUBSCRIBE_PARAMS, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Array(sizeof(uint32_t), SPA_TYPE_Id, n_ids, ids)); + + return pw_protocol_native_end_proxy(proxy, b); +} + +static int session_resource_marshal_subscribe_params(void *object, + uint32_t *ids, uint32_t n_ids) +{ + struct pw_resource *resource = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_resource(resource, + PW_SESSION_METHOD_SUBSCRIBE_PARAMS, NULL); + + spa_pod_builder_add_struct(b, + SPA_POD_Array(sizeof(uint32_t), SPA_TYPE_Id, n_ids, ids)); + + return pw_protocol_native_end_resource(resource, b); +} + +static int session_proxy_marshal_enum_params(void *object, + int seq, uint32_t id, + uint32_t index, uint32_t num, + const struct spa_pod *filter) +{ + struct pw_protocol_native_message *msg; + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_SESSION_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(num), + SPA_POD_Pod(filter)); + + return pw_protocol_native_end_proxy(proxy, b); +} + +static int session_resource_marshal_enum_params(void *object, + int seq, uint32_t id, + uint32_t index, uint32_t num, + 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, + PW_SESSION_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(num), + SPA_POD_Pod(filter)); + + return pw_protocol_native_end_resource(resource, b); +} + +static int session_proxy_marshal_set_param(void *object, + uint32_t id, uint32_t flags, + const struct spa_pod *param) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + + b = pw_protocol_native_begin_proxy(proxy, + PW_SESSION_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_proxy(proxy, b); +} + +static int session_resource_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, + PW_SESSION_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 session_proxy_demarshal_info(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = data; + struct spa_pod_parser prs; + struct spa_pod_frame f; + struct spa_dict props = SPA_DICT_INIT(NULL, 0); + struct pw_session_info info = { .props = &props }; + + spa_pod_parser_init(&prs, msg->data, msg->size); + + demarshal_pw_session_info(&prs, &f, &info); + + return pw_proxy_notify(proxy, struct pw_session_events, + info, 0, &info); +} + +static int session_resource_demarshal_info(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = data; + struct spa_pod_parser prs; + struct spa_pod_frame f; + struct spa_dict props = SPA_DICT_INIT(NULL, 0); + struct pw_session_info info = { .props = &props }; + + spa_pod_parser_init(&prs, msg->data, msg->size); + + demarshal_pw_session_info(&prs, &f, &info); + + return pw_resource_notify(resource, struct pw_session_events, + info, 0, &info); +} + +static int session_proxy_demarshal_param(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = data; + struct spa_pod_parser prs; + uint32_t id, index, next; + int seq; + struct spa_pod *param; + + 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(&next), + SPA_POD_Pod(¶m)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_session_events, + param, 0, seq, id, index, next, param); +} + +static int session_resource_demarshal_param(void *data, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = data; + struct spa_pod_parser prs; + uint32_t id, index, next; + int seq; + struct spa_pod *param; + + 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(&next), + SPA_POD_Pod(¶m)) < 0) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_session_events, + param, 0, seq, id, index, next, param); +} + +static int session_proxy_demarshal_subscribe_params(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = object; + struct spa_pod_parser prs; + uint32_t csize, ctype, n_ids; + uint32_t *ids; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, + SPA_POD_Array(&csize, &ctype, &n_ids, &ids)) < 0) + return -EINVAL; + + if (ctype != SPA_TYPE_Id) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_session_methods, + subscribe_params, 0, ids, n_ids); +} + +static int session_resource_demarshal_subscribe_params(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs; + uint32_t csize, ctype, n_ids; + uint32_t *ids; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, + SPA_POD_Array(&csize, &ctype, &n_ids, &ids)) < 0) + return -EINVAL; + + if (ctype != SPA_TYPE_Id) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_session_methods, + subscribe_params, 0, ids, n_ids); +} + +static int session_proxy_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, num; + 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(&num), + SPA_POD_Pod(&filter)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_session_methods, + enum_params, 0, seq, id, index, num, filter); +} + +static int session_resource_demarshal_enum_params(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs; + uint32_t id, index, num; + 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(&num), + SPA_POD_Pod(&filter)) < 0) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_session_methods, + enum_params, 0, seq, id, index, num, filter); +} + +static int session_proxy_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(¶m)) < 0) + return -EINVAL; + + return pw_proxy_notify(proxy, struct pw_session_methods, + set_param, 0, id, flags, param); +} + +static int session_resource_demarshal_set_param(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = 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(¶m)) < 0) + return -EINVAL; + + return pw_resource_notify(resource, struct pw_session_methods, + set_param, 0, id, flags, param); +} + +static const struct pw_session_events pw_protocol_native_session_client_event_marshal = { + PW_VERSION_SESSION_EVENTS, + .info = session_proxy_marshal_info, + .param = session_proxy_marshal_param, +}; + +static const struct pw_session_events pw_protocol_native_session_server_event_marshal = { + PW_VERSION_SESSION_EVENTS, + .info = session_resource_marshal_info, + .param = session_resource_marshal_param, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_session_client_event_demarshal[PW_SESSION_EVENT_NUM] = +{ + [PW_SESSION_EVENT_INFO] = { session_proxy_demarshal_info, 0 }, + [PW_SESSION_EVENT_PARAM] = { session_proxy_demarshal_param, 0 }, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_session_server_event_demarshal[PW_SESSION_EVENT_NUM] = +{ + [PW_SESSION_EVENT_INFO] = { session_resource_demarshal_info, 0 }, + [PW_SESSION_EVENT_PARAM] = { session_resource_demarshal_param, 0 }, +}; + +static const struct pw_session_methods pw_protocol_native_session_client_method_marshal = { + PW_VERSION_SESSION_METHODS, + .add_listener = session_proxy_marshal_add_listener, + .subscribe_params = session_proxy_marshal_subscribe_params, + .enum_params = session_proxy_marshal_enum_params, + .set_param = session_proxy_marshal_set_param, +}; + +static const struct pw_session_methods pw_protocol_native_session_server_method_marshal = { + PW_VERSION_SESSION_METHODS, + .add_listener = session_resource_marshal_add_listener, + .subscribe_params = session_resource_marshal_subscribe_params, + .enum_params = session_resource_marshal_enum_params, + .set_param = session_resource_marshal_set_param, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_session_client_method_demarshal[PW_SESSION_METHOD_NUM] = +{ + [PW_SESSION_METHOD_ADD_LISTENER] = { demarshal_add_listener_enotsup, 0 }, + [PW_SESSION_METHOD_SUBSCRIBE_PARAMS] = { session_proxy_demarshal_subscribe_params, 0 }, + [PW_SESSION_METHOD_ENUM_PARAMS] = { session_proxy_demarshal_enum_params, 0 }, + [PW_SESSION_METHOD_SET_PARAM] = { session_proxy_demarshal_set_param, PW_PERM_W }, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_session_server_method_demarshal[PW_SESSION_METHOD_NUM] = +{ + [PW_SESSION_METHOD_ADD_LISTENER] = { demarshal_add_listener_enotsup, 0 }, + [PW_SESSION_METHOD_SUBSCRIBE_PARAMS] = { session_resource_demarshal_subscribe_params, 0 }, + [PW_SESSION_METHOD_ENUM_PARAMS] = { session_resource_demarshal_enum_params, 0 }, + [PW_SESSION_METHOD_SET_PARAM] = { session_resource_demarshal_set_param, PW_PERM_W }, +}; + +static const struct pw_protocol_marshal pw_protocol_native_session_marshal = { + PW_TYPE_INTERFACE_Session, + PW_VERSION_SESSION, + 0, + PW_SESSION_METHOD_NUM, + PW_SESSION_EVENT_NUM, + .client_marshal = &pw_protocol_native_session_client_method_marshal, + .server_demarshal = pw_protocol_native_session_server_method_demarshal, + .server_marshal = &pw_protocol_native_session_server_event_marshal, + .client_demarshal = pw_protocol_native_session_client_event_demarshal, +}; + +static const struct pw_protocol_marshal pw_protocol_native_session_impl_marshal = { + PW_TYPE_INTERFACE_Session, + PW_VERSION_SESSION, + PW_PROTOCOL_MARSHAL_FLAG_IMPL, + PW_SESSION_EVENT_NUM, + PW_SESSION_METHOD_NUM, + .client_marshal = &pw_protocol_native_session_client_event_marshal, + .server_demarshal = pw_protocol_native_session_server_event_demarshal, + .server_marshal = &pw_protocol_native_session_server_method_marshal, + .client_demarshal = pw_protocol_native_session_client_method_demarshal, +}; + +int pw_protocol_native_ext_session_manager_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; + + /* deprecated */ + pw_protocol_add_marshal(protocol, &pw_protocol_native_client_endpoint_marshal); + pw_protocol_add_marshal(protocol, &pw_protocol_native_client_session_marshal); + + /* client <-> server */ + pw_protocol_add_marshal(protocol, &pw_protocol_native_endpoint_link_marshal); + pw_protocol_add_marshal(protocol, &pw_protocol_native_endpoint_stream_marshal); + pw_protocol_add_marshal(protocol, &pw_protocol_native_endpoint_marshal); + pw_protocol_add_marshal(protocol, &pw_protocol_native_session_marshal); + + /* impl <-> server */ + pw_protocol_add_marshal(protocol, &pw_protocol_native_endpoint_link_impl_marshal); + pw_protocol_add_marshal(protocol, &pw_protocol_native_endpoint_stream_impl_marshal); + pw_protocol_add_marshal(protocol, &pw_protocol_native_endpoint_impl_marshal); + pw_protocol_add_marshal(protocol, &pw_protocol_native_session_impl_marshal); + + return 0; +} diff --git a/src/modules/module-session-manager/proxy-session-manager.c b/src/modules/module-session-manager/proxy-session-manager.c new file mode 100644 index 0000000..cd418e1 --- /dev/null +++ b/src/modules/module-session-manager/proxy-session-manager.c @@ -0,0 +1,188 @@ +/* PipeWire + * + * Copyright © 2020 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 "pipewire/pipewire.h" +#include "pipewire/extensions/session-manager.h" + +struct object_data { + struct spa_hook object_listener; + struct spa_hook object_methods; + struct spa_hook proxy_listener; +}; + +static void proxy_object_destroy(void *_data) +{ + struct object_data *data = _data; + spa_hook_remove(&data->object_listener); +} + +static const struct pw_proxy_events proxy_events = { + PW_VERSION_PROXY_EVENTS, + .destroy = proxy_object_destroy, +}; + +struct pw_proxy *pw_core_endpoint_export(struct pw_core *core, + const char *type, const struct spa_dict *props, void *object, + size_t user_data_size) +{ + struct pw_endpoint *endpoint = object; + struct spa_interface *remote_iface, *local_iface; + struct pw_proxy *proxy; + struct object_data *data; + + proxy = pw_core_create_object(core, + "endpoint", + PW_TYPE_INTERFACE_Endpoint, + PW_VERSION_ENDPOINT, + props, + user_data_size + sizeof(struct object_data)); + if (proxy == NULL) + return NULL; + + data = pw_proxy_get_user_data(proxy); + data = SPA_PTROFF(data, user_data_size, struct object_data); + + remote_iface = (struct spa_interface*)proxy; + local_iface = (struct spa_interface*)endpoint; + + pw_proxy_install_marshal(proxy, true); + + pw_proxy_add_listener(proxy, &data->proxy_listener, &proxy_events, data); + + pw_proxy_add_object_listener(proxy, &data->object_methods, + local_iface->cb.funcs, local_iface->cb.data); + pw_endpoint_add_listener(endpoint, &data->object_listener, + remote_iface->cb.funcs, remote_iface->cb.data); + + return proxy; +} + +struct pw_proxy *pw_core_endpoint_stream_export(struct pw_core *core, + const char *type, const struct spa_dict *props, void *object, + size_t user_data_size) +{ + struct pw_endpoint_stream *endpoint_stream = object; + struct spa_interface *remote_iface, *local_iface; + struct pw_proxy *proxy; + struct object_data *data; + + proxy = pw_core_create_object(core, + "endpoint-stream", + PW_TYPE_INTERFACE_EndpointStream, + PW_VERSION_ENDPOINT_STREAM, + props, + user_data_size + sizeof(struct object_data)); + if (proxy == NULL) + return NULL; + + data = pw_proxy_get_user_data(proxy); + data = SPA_PTROFF(data, user_data_size, struct object_data); + + remote_iface = (struct spa_interface*)proxy; + local_iface = (struct spa_interface*)endpoint_stream; + + pw_proxy_install_marshal(proxy, true); + + pw_proxy_add_listener(proxy, &data->proxy_listener, &proxy_events, data); + + pw_proxy_add_object_listener(proxy, &data->object_methods, + local_iface->cb.funcs, local_iface->cb.data); + pw_endpoint_stream_add_listener(endpoint_stream, &data->object_listener, + remote_iface->cb.funcs, remote_iface->cb.data); + + return proxy; +} + +struct pw_proxy *pw_core_endpoint_link_export(struct pw_core *core, + const char *type, const struct spa_dict *props, void *object, + size_t user_data_size) +{ + struct pw_endpoint_link *endpoint_link = object; + struct spa_interface *remote_iface, *local_iface; + struct pw_proxy *proxy; + struct object_data *data; + + proxy = pw_core_create_object(core, + "endpoint-link", + PW_TYPE_INTERFACE_EndpointLink, + PW_VERSION_ENDPOINT_LINK, + props, + user_data_size + sizeof(struct object_data)); + if (proxy == NULL) + return NULL; + + data = pw_proxy_get_user_data(proxy); + data = SPA_PTROFF(data, user_data_size, struct object_data); + + remote_iface = (struct spa_interface*)proxy; + local_iface = (struct spa_interface*)endpoint_link; + + pw_proxy_install_marshal(proxy, true); + + pw_proxy_add_listener(proxy, &data->proxy_listener, &proxy_events, data); + + pw_proxy_add_object_listener(proxy, &data->object_methods, + local_iface->cb.funcs, local_iface->cb.data); + pw_endpoint_link_add_listener(endpoint_link, &data->object_listener, + remote_iface->cb.funcs, remote_iface->cb.data); + + return proxy; +} + +struct pw_proxy *pw_core_session_export(struct pw_core *core, + const char *type, const struct spa_dict *props, void *object, + size_t user_data_size) +{ + struct pw_session *session = object; + struct spa_interface *remote_iface, *local_iface; + struct pw_proxy *proxy; + struct object_data *data; + + proxy = pw_core_create_object(core, + "session", + PW_TYPE_INTERFACE_Session, + PW_VERSION_SESSION, + props, + user_data_size + sizeof(struct object_data)); + if (proxy == NULL) + return NULL; + + data = pw_proxy_get_user_data(proxy); + data = SPA_PTROFF(data, user_data_size, struct object_data); + + remote_iface = (struct spa_interface*)proxy; + local_iface = (struct spa_interface*)session; + + pw_proxy_install_marshal(proxy, true); + + pw_proxy_add_listener(proxy, &data->proxy_listener, &proxy_events, data); + + pw_proxy_add_object_listener(proxy, &data->object_methods, + local_iface->cb.funcs, local_iface->cb.data); + pw_session_add_listener(session, &data->object_listener, + remote_iface->cb.funcs, remote_iface->cb.data); + + return proxy; +} diff --git a/src/modules/module-session-manager/session.c b/src/modules/module-session-manager/session.c new file mode 100644 index 0000000..e3d7210 --- /dev/null +++ b/src/modules/module-session-manager/session.c @@ -0,0 +1,578 @@ +/* PipeWire + * + * Copyright © 2020 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 <pipewire/impl.h> +#include <pipewire/extensions/session-manager.h> +#include <pipewire/extensions/session-manager/introspect-funcs.h> + +#include <spa/utils/result.h> +#include <spa/pod/builder.h> +#include <spa/pod/filter.h> + +#define MAX_PARAMS 32 + +#define NAME "session" + +struct pw_proxy *pw_core_session_export(struct pw_core *core, + const char *type, const struct spa_dict *props, void *object, + size_t user_data_size); + +struct impl +{ + struct pw_global *global; + struct spa_hook global_listener; + + union { + struct pw_session *session; + struct pw_resource *resource; + }; + struct spa_hook resource_listener; + struct spa_hook session_listener; + + struct pw_session_info *cached_info; + struct spa_list cached_params; + + int ping_seq; + bool registered; +}; + +struct param_data +{ + struct spa_list link; + uint32_t id; + struct pw_array params; +}; + +struct resource_data +{ + struct impl *impl; + + struct pw_resource *resource; + struct spa_hook object_listener; + + uint32_t n_subscribe_ids; + uint32_t subscribe_ids[32]; +}; + +struct factory_data +{ + struct pw_impl_module *module; + struct spa_hook module_listener; + + struct pw_impl_factory *factory; + struct spa_hook factory_listener; + + struct pw_export_type export; +}; + +#define pw_session_resource(r,m,v,...) \ + pw_resource_call(r,struct pw_session_events,m,v,__VA_ARGS__) + +#define pw_session_resource_info(r,...) \ + pw_session_resource(r,info,0,__VA_ARGS__) +#define pw_session_resource_param(r,...) \ + pw_session_resource(r,param,0,__VA_ARGS__) + +static int method_enum_params(void *object, int seq, + uint32_t id, uint32_t start, uint32_t num, + const struct spa_pod *filter) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + struct param_data *pdata; + 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", impl, id, start, num); + + spa_list_for_each(pdata, &impl->cached_params, link) { + if (pdata->id != id) + continue; + + while (true) { + index = next++; + if (index >= pw_array_get_len(&pdata->params, void*)) + return 0; + + param = *pw_array_get_unchecked(&pdata->params, index, struct spa_pod*); + + 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", impl, seq, index); + + pw_session_resource_param(d->resource, seq, id, index, next, result); + + if (++count == num) + return 0; + } + } + + return 0; +} + +static int method_subscribe_params(void *object, uint32_t *ids, uint32_t n_ids) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + uint32_t i; + + n_ids = SPA_MIN(n_ids, SPA_N_ELEMENTS(d->subscribe_ids)); + d->n_subscribe_ids = n_ids; + + for (i = 0; i < n_ids; i++) { + d->subscribe_ids[i] = ids[i]; + pw_log_debug(NAME" %p: resource %d subscribe param %u", + impl, pw_resource_get_id(d->resource), ids[i]); + method_enum_params(object, 1, ids[i], 0, UINT32_MAX, NULL); + } + return 0; +} + +static int method_set_param(void *object, uint32_t id, uint32_t flags, + const struct spa_pod *param) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + /* store only on the implementation; our cache will be updated + by the param event, since we are subscribed */ + pw_session_set_param(impl->session, id, flags, param); + return 0; +} + +static const struct pw_session_methods session_methods = { + PW_VERSION_SESSION_METHODS, + .subscribe_params = method_subscribe_params, + .enum_params = method_enum_params, + .set_param = method_set_param, +}; + +static int global_bind(void *object, struct pw_impl_client *client, + uint32_t permissions, uint32_t version, uint32_t id) +{ + struct impl *impl = object; + struct pw_resource *resource; + struct resource_data *data; + + resource = pw_resource_new(client, id, permissions, + PW_TYPE_INTERFACE_Session, + version, sizeof(*data)); + if (resource == NULL) + return -errno; + + data = pw_resource_get_user_data(resource); + data->impl = impl; + data->resource = resource; + + pw_global_add_resource(impl->global, resource); + + /* resource methods -> implementation */ + pw_resource_add_object_listener(resource, + &data->object_listener, + &session_methods, data); + + impl->cached_info->change_mask = PW_SESSION_CHANGE_MASK_ALL; + pw_session_resource_info(resource, impl->cached_info); + impl->cached_info->change_mask = 0; + + return 0; +} + +static void global_destroy(void *data) +{ + struct impl *impl = data; + spa_hook_remove(&impl->global_listener); + impl->global = NULL; + if (impl->resource) + pw_resource_destroy(impl->resource); +} + +static const struct pw_global_events global_events = { + PW_VERSION_GLOBAL_EVENTS, + .destroy = global_destroy, +}; + +static void impl_resource_destroy(void *data) +{ + struct impl *impl = data; + struct param_data *pdata, *tmp; + + spa_hook_remove(&impl->resource_listener); + impl->resource = NULL; + + /* clear cache */ + if (impl->cached_info) + pw_session_info_free(impl->cached_info); + spa_list_for_each_safe(pdata, tmp, &impl->cached_params, link) { + struct spa_pod **pod; + pw_array_for_each(pod, &pdata->params) + free(*pod); + pw_array_clear(&pdata->params); + spa_list_remove(&pdata->link); + free(pdata); + } + + if (impl->global) + pw_global_destroy(impl->global); +} + +static void register_global(struct impl *impl) +{ + impl->cached_info->id = pw_global_get_id (impl->global); + pw_resource_set_bound_id(impl->resource, impl->cached_info->id); + pw_global_register(impl->global); + impl->registered = true; +} + +static void impl_resource_pong (void *data, int seq) +{ + struct impl *impl = data; + + /* complete registration, if this was the initial sync */ + if (!impl->registered && seq == impl->ping_seq) { + register_global(impl); + } +} + +static const struct pw_resource_events impl_resource_events = { + PW_VERSION_RESOURCE_EVENTS, + .destroy = impl_resource_destroy, + .pong = impl_resource_pong, +}; + +static int emit_info(void *data, struct pw_resource *resource) +{ + const struct pw_session_info *info = data; + pw_session_resource_info(resource, info); + return 0; +} + +static void event_info(void *data, const struct pw_session_info *info) +{ + struct impl *impl = data; + uint32_t changed_ids[MAX_PARAMS], n_changed_ids = 0; + uint32_t i; + + /* figure out changes to params */ + if (info->change_mask & PW_SESSION_CHANGE_MASK_PARAMS) { + for (i = 0; i < info->n_params; i++) { + if ((!impl->cached_info || + info->params[i].flags != impl->cached_info->params[i].flags) + && info->params[i].flags & SPA_PARAM_INFO_READ) + changed_ids[n_changed_ids++] = info->params[i].id; + } + } + + /* cache for new clients */ + impl->cached_info = pw_session_info_update (impl->cached_info, info); + + /* notify existing clients */ + pw_global_for_each_resource(impl->global, emit_info, (void*) info); + + /* cache params & register */ + if (n_changed_ids > 0) { + /* prepare params storage */ + for (i = 0; i < n_changed_ids; i++) { + struct param_data *pdata = calloc(1, sizeof(struct param_data)); + pdata->id = changed_ids[i]; + pw_array_init(&pdata->params, sizeof(void*)); + spa_list_append(&impl->cached_params, &pdata->link); + } + + /* subscribe to impl */ + pw_session_subscribe_params(impl->session, changed_ids, n_changed_ids); + + /* register asynchronously on the pong event */ + impl->ping_seq = pw_resource_ping(impl->resource, 0); + } + else if (!impl->registered) { + register_global(impl); + } +} + +struct param_event_args +{ + uint32_t id, index, next; + const struct spa_pod *param; +}; + +static int emit_param(void *_data, struct pw_resource *resource) +{ + struct param_event_args *args = _data; + struct resource_data *data; + uint32_t i; + + data = pw_resource_get_user_data(resource); + for (i = 0; i < data->n_subscribe_ids; i++) { + if (data->subscribe_ids[i] == args->id) { + pw_session_resource_param(resource, 1, + args->id, args->index, args->next, args->param); + } + } + return 0; +} + +static void event_param(void *data, int seq, + uint32_t id, uint32_t index, uint32_t next, + const struct spa_pod *param) +{ + struct impl *impl = data; + struct param_data *pdata; + struct spa_pod **pod; + struct param_event_args args = { id, index, next, param }; + + /* cache for new requests */ + spa_list_for_each(pdata, &impl->cached_params, link) { + if (pdata->id != id) + continue; + + if (!pw_array_check_index(&pdata->params, index, void*)) { + while (pw_array_get_len(&pdata->params, void*) <= index) + pw_array_add_ptr(&pdata->params, NULL); + } + + pod = pw_array_get_unchecked(&pdata->params, index, struct spa_pod*); + free(*pod); + *pod = spa_pod_copy(param); + } + + /* notify existing clients */ + pw_global_for_each_resource(impl->global, emit_param, &args); +} + +static const struct pw_session_events session_events = { + PW_VERSION_SESSION_EVENTS, + .info = event_info, + .param = event_param, +}; + +static void *session_new(struct pw_context *context, + struct pw_resource *resource, + struct pw_properties *properties) +{ + struct impl *impl; + char serial_str[32]; + struct spa_dict_item items[1] = { + SPA_DICT_ITEM_INIT(PW_KEY_OBJECT_SERIAL, serial_str), + }; + struct spa_dict extra_props = SPA_DICT_INIT_ARRAY(items); + static const char * const keys[] = { + PW_KEY_OBJECT_SERIAL, + NULL + }; + + impl = calloc(1, sizeof(*impl)); + if (impl == NULL) { + pw_properties_free(properties); + return NULL; + } + + impl->global = pw_global_new(context, + PW_TYPE_INTERFACE_Session, + PW_VERSION_SESSION, + properties, + global_bind, impl); + if (impl->global == NULL) { + free(impl); + return NULL; + } + impl->resource = resource; + + spa_scnprintf(serial_str, sizeof(serial_str), "%"PRIu64, + pw_global_get_serial(impl->global)); + pw_global_update_keys(impl->global, &extra_props, keys); + + spa_list_init(&impl->cached_params); + + /* handle destroy events */ + pw_global_add_listener(impl->global, + &impl->global_listener, + &global_events, impl); + pw_resource_add_listener(impl->resource, + &impl->resource_listener, + &impl_resource_events, impl); + + /* handle implementation events -> cache + client resources */ + pw_session_add_listener(impl->session, + &impl->session_listener, + &session_events, impl); + + /* global is not registered here on purpose; + we first cache info + params and then expose the global */ + + return impl; +} + +static void *create_object(void *data, + struct pw_resource *resource, + const char *type, + uint32_t version, + struct pw_properties *properties, + uint32_t new_id) +{ + struct factory_data *d = data; + struct pw_resource *impl_resource; + struct pw_impl_client *client = pw_resource_get_client(resource); + void *result; + int res; + + impl_resource = pw_resource_new(client, new_id, PW_PERM_ALL, type, version, 0); + if (impl_resource == NULL) { + res = -errno; + goto error_resource; + } + + pw_resource_install_marshal(impl_resource, true); + + if (properties == NULL) + properties = pw_properties_new(NULL, NULL); + if (properties == NULL) { + res = -ENOMEM; + goto error_session; + } + + pw_properties_setf(properties, PW_KEY_CLIENT_ID, "%d", + pw_impl_client_get_info(client)->id); + pw_properties_setf(properties, PW_KEY_FACTORY_ID, "%d", + pw_impl_factory_get_info(d->factory)->id); + + result = session_new(pw_impl_client_get_context(client), impl_resource, properties); + if (result == NULL) { + res = -errno; + goto error_session; + } + return result; + +error_resource: + pw_log_error("can't create resource: %s", spa_strerror(res)); + pw_resource_errorf_id(resource, new_id, res, "can't create resource: %s", spa_strerror(res)); + goto error_exit; +error_session: + pw_log_error("can't create session: %s", spa_strerror(res)); + pw_resource_errorf_id(resource, new_id, res, "can't create session: %s", spa_strerror(res)); + goto error_exit_free; + +error_exit_free: + pw_resource_remove(impl_resource); +error_exit: + errno = -res; + return NULL; +} + +static const struct pw_impl_factory_implementation impl_factory = { + PW_VERSION_IMPL_FACTORY_IMPLEMENTATION, + .create_object = create_object, +}; + +static void factory_destroy(void *data) +{ + struct factory_data *d = data; + spa_hook_remove(&d->factory_listener); + d->factory = NULL; + if (d->module) + pw_impl_module_destroy(d->module); +} + +static const struct pw_impl_factory_events factory_events = { + PW_VERSION_IMPL_FACTORY_EVENTS, + .destroy = factory_destroy, +}; + +static void module_destroy(void *data) +{ + struct factory_data *d = data; + + spa_hook_remove(&d->module_listener); + spa_list_remove(&d->export.link); + d->module = NULL; + if (d->factory) + pw_impl_factory_destroy(d->factory); +} + +static void module_registered(void *data) +{ + struct factory_data *d = data; + struct pw_impl_module *module = d->module; + struct pw_impl_factory *factory = d->factory; + struct spa_dict_item items[1]; + char id[16]; + int res; + + snprintf(id, sizeof(id), "%d", pw_impl_module_get_info(module)->id); + items[0] = SPA_DICT_ITEM_INIT(PW_KEY_MODULE_ID, id); + pw_impl_factory_update_properties(factory, &SPA_DICT_INIT(items, 1)); + + if ((res = pw_impl_factory_register(factory, NULL)) < 0) { + pw_log_error(NAME" %p: can't register factory: %s", factory, spa_strerror(res)); + } +} + +static const struct pw_impl_module_events module_events = { + PW_VERSION_IMPL_MODULE_EVENTS, + .destroy = module_destroy, + .registered = module_registered, +}; + +int session_factory_init(struct pw_impl_module *module) +{ + struct pw_context *context = pw_impl_module_get_context(module); + struct pw_impl_factory *factory; + struct factory_data *data; + int res; + + factory = pw_context_create_factory(context, + "session", + PW_TYPE_INTERFACE_Session, + PW_VERSION_SESSION, + NULL, + sizeof(*data)); + if (factory == NULL) + return -errno; + + data = pw_impl_factory_get_user_data(factory); + data->factory = factory; + data->module = module; + + pw_impl_factory_set_implementation(factory, &impl_factory, data); + + data->export.type = PW_TYPE_INTERFACE_Session; + data->export.func = pw_core_session_export; + if ((res = pw_context_register_export_type(context, &data->export)) < 0) + goto error; + + pw_impl_factory_add_listener(factory, &data->factory_listener, &factory_events, data); + pw_impl_module_add_listener(module, &data->module_listener, &module_events, data); + + return 0; +error: + pw_impl_factory_destroy(data->factory); + return res; +} |