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/client-endpoint | |
parent | Initial commit. (diff) | |
download | pipewire-upstream.tar.xz pipewire-upstream.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/client-endpoint')
6 files changed, 1220 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 */ |