diff options
Diffstat (limited to '')
-rw-r--r-- | src/modules/module-metadata.c | 241 | ||||
-rw-r--r-- | src/modules/module-metadata/metadata.c | 316 | ||||
-rw-r--r-- | src/modules/module-metadata/protocol-native.c | 354 | ||||
-rw-r--r-- | src/modules/module-metadata/proxy-metadata.c | 92 |
4 files changed, 1003 insertions, 0 deletions
diff --git a/src/modules/module-metadata.c b/src/modules/module-metadata.c new file mode 100644 index 0000000..b8619a4 --- /dev/null +++ b/src/modules/module-metadata.c @@ -0,0 +1,241 @@ +/* PipeWire + * + * Copyright © 2019 Wim Taymans + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include <string.h> +#include <stdio.h> +#include <errno.h> +#include <dlfcn.h> + +#include "config.h" + +#include <spa/utils/result.h> + +#include <pipewire/impl.h> +#include <pipewire/extensions/metadata.h> + +/** \page page_module_metadata PipeWire Module: Metadata + */ + +#define NAME "metadata" + +PW_LOG_TOPIC_STATIC(mod_topic, "mod." NAME); +#define PW_LOG_TOPIC_DEFAULT mod_topic + +static const struct spa_dict_item module_props[] = { + { PW_KEY_MODULE_AUTHOR, "Wim Taymans <wim.taymans@gmail.com>" }, + { PW_KEY_MODULE_DESCRIPTION, "Allow clients to create metadata store" }, + { PW_KEY_MODULE_VERSION, PACKAGE_VERSION }, +}; + + +void * pw_metadata_new(struct pw_context *context, struct pw_resource *resource, + struct pw_properties *properties); + +struct pw_proxy *pw_core_metadata_export(struct pw_core *core, + const char *type, const struct spa_dict *props, void *object, size_t user_data_size); + +int pw_protocol_native_ext_metadata_init(struct pw_context *context); + +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_metadata; +}; + +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 *data = _data; + struct pw_context *context = pw_impl_module_get_context(data->module); + void *result; + struct pw_resource *metadata_resource = NULL; + struct pw_impl_client *client = resource ? pw_resource_get_client(resource) : NULL; + int res; + + if (properties == NULL) + properties = pw_properties_new(NULL, NULL); + if (properties == NULL) + return NULL; + + pw_properties_setf(properties, PW_KEY_FACTORY_ID, "%d", + pw_impl_factory_get_info(data->factory)->id); + pw_properties_setf(properties, PW_KEY_MODULE_ID, "%d", + pw_impl_module_get_info(data->module)->id); + + if (pw_properties_get(properties, PW_KEY_METADATA_NAME) == NULL) + pw_properties_set(properties, PW_KEY_METADATA_NAME, "default"); + + if (client) { + metadata_resource = pw_resource_new(client, new_id, PW_PERM_ALL, type, version, 0); + if (metadata_resource == NULL) { + res = -errno; + goto error_resource; + } + + pw_properties_setf(properties, PW_KEY_CLIENT_ID, "%d", + pw_impl_client_get_info(client)->id); + + result = pw_metadata_new(context, metadata_resource, properties); + if (result == NULL) { + properties = NULL; + res = -errno; + goto error_node; + } + } else { + result = pw_context_create_metadata(context, NULL, properties, 0); + if (result == NULL) { + properties = NULL; + res = -errno; + goto error_node; + } + pw_impl_metadata_register(result, NULL); + } + return result; + +error_resource: + pw_resource_errorf_id(resource, new_id, res, + "can't create resource: %s", spa_strerror(res)); + goto error_exit; +error_node: + pw_resource_errorf_id(resource, new_id, res, + "can't create metadata: %s", spa_strerror(res)); + goto error_exit_free; + +error_exit_free: + if (metadata_resource) + pw_resource_remove(metadata_resource); +error_exit: + pw_properties_free(properties); + 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_metadata.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_global_get_id(pw_impl_module_get_global(module))); + 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("%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, +}; + +SPA_EXPORT +int pipewire__module_init(struct pw_impl_module *module, const char *args) +{ + struct pw_context *context = pw_impl_module_get_context(module); + struct pw_impl_factory *factory; + struct factory_data *data; + int res; + + PW_LOG_TOPIC_INIT(mod_topic); + + if ((res = pw_protocol_native_ext_metadata_init(context)) < 0) + return res; + + factory = pw_context_create_factory(context, + "metadata", + PW_TYPE_INTERFACE_Metadata, + PW_VERSION_METADATA, + NULL, + sizeof(*data)); + if (factory == NULL) + return -errno; + + data = pw_impl_factory_get_user_data(factory); + data->factory = factory; + data->module = module; + + pw_log_debug("module %p: new", module); + + pw_impl_factory_set_implementation(factory, + &impl_factory, + data); + + data->export_metadata.type = PW_TYPE_INTERFACE_Metadata; + data->export_metadata.func = pw_core_metadata_export; + if ((res = pw_context_register_export_type(context, &data->export_metadata)) < 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); + + pw_impl_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props)); + + return 0; +error: + pw_impl_factory_destroy(data->factory); + return res; +} diff --git a/src/modules/module-metadata/metadata.c b/src/modules/module-metadata/metadata.c new file mode 100644 index 0000000..d863bea --- /dev/null +++ b/src/modules/module-metadata/metadata.c @@ -0,0 +1,316 @@ +/* PipeWire + * + * Copyright © 2019 Wim Taymans + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include <spa/utils/result.h> + +#include <pipewire/impl.h> + +#include <pipewire/extensions/metadata.h> + +#define NAME "metadata" + +struct impl { + struct spa_hook context_listener; + + struct pw_global *global; + struct spa_hook global_listener; + + struct pw_metadata *metadata; + struct pw_resource *resource; + struct spa_hook resource_listener; + int pending; +}; + +struct resource_data { + struct impl *impl; + + struct pw_resource *resource; + struct spa_hook resource_listener; + struct spa_hook object_listener; + struct spa_hook metadata_listener; + struct spa_hook impl_resource_listener; + int pong_seq; +}; + +#define pw_metadata_resource(r,m,v,...) \ + pw_resource_call_res(r,struct pw_metadata_events,m,v,__VA_ARGS__) + +#define pw_metadata_resource_property(r,...) \ + pw_metadata_resource(r,property,0,__VA_ARGS__) + +static int metadata_property(void *data, + uint32_t subject, + const char *key, + const char *type, + const char *value) +{ + struct resource_data *d = data; + struct pw_resource *resource = d->resource; + struct pw_impl_client *client = pw_resource_get_client(resource); + struct impl *impl = d->impl; + + if (impl->pending == 0 || d->pong_seq != 0) { + if (pw_impl_client_check_permissions(client, subject, PW_PERM_R) >= 0) + pw_metadata_resource_property(d->resource, subject, key, type, value); + } + return 0; +} + +static const struct pw_metadata_events metadata_events = { + PW_VERSION_METADATA_EVENTS, + .property = metadata_property, +}; + +static int metadata_set_property(void *object, + uint32_t subject, + const char *key, + const char *type, + const char *value) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + struct pw_resource *resource = d->resource; + struct pw_impl_client *client = pw_resource_get_client(resource); + int res; + + if ((res = pw_impl_client_check_permissions(client, subject, PW_PERM_R | PW_PERM_M)) < 0) + goto error; + + pw_metadata_set_property(impl->metadata, subject, key, type, value); + return 0; + +error: + pw_resource_errorf(resource, res, "set property error for id %d: %s", + subject, spa_strerror(res)); + return res; +} + +static int metadata_clear(void *object) +{ + struct resource_data *d = object; + struct impl *impl = d->impl; + pw_metadata_clear(impl->metadata); + return 0; +} + +static const struct pw_metadata_methods metadata_methods = { + PW_VERSION_METADATA_METHODS, + .set_property = metadata_set_property, + .clear = metadata_clear, +}; + + +static void global_unbind(void *data) +{ + struct resource_data *d = data; + if (d->resource) { + spa_hook_remove(&d->resource_listener); + spa_hook_remove(&d->object_listener); + spa_hook_remove(&d->metadata_listener); + spa_hook_remove(&d->impl_resource_listener); + } +} + +static const struct pw_resource_events resource_events = { + PW_VERSION_RESOURCE_EVENTS, + .destroy = global_unbind, +}; + +static void remove_pending(struct resource_data *d) +{ + if (d->pong_seq != 0) { + pw_impl_client_set_busy(pw_resource_get_client(d->resource), false); + d->pong_seq = 0; + d->impl->pending--; + } +} + +static void impl_resource_destroy(void *data) +{ + struct resource_data *d = data; + remove_pending(d); +} + +static void impl_resource_pong(void *data, int seq) +{ + struct resource_data *d = data; + if (d->pong_seq == seq) + remove_pending(d); +} + +static const struct pw_resource_events impl_resource_events = { + PW_VERSION_RESOURCE_EVENTS, + .destroy = impl_resource_destroy, + .pong = impl_resource_pong, +}; + +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_Metadata, 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); + + /* listen for when the resource goes away */ + pw_resource_add_listener(resource, + &data->resource_listener, + &resource_events, data); + + /* resource methods -> implementation */ + pw_resource_add_object_listener(resource, + &data->object_listener, + &metadata_methods, data); + + pw_impl_client_set_busy(client, true); + + /* implementation events -> resource */ + pw_metadata_add_listener(impl->metadata, + &data->metadata_listener, + &metadata_events, data); + + pw_resource_add_listener(impl->resource, + &data->impl_resource_listener, + &impl_resource_events, data); + + data->pong_seq = pw_resource_ping(impl->resource, data->pong_seq); + impl->pending++; + + 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 context_global_removed(void *data, struct pw_global *global) +{ + struct impl *impl = data; + pw_metadata_set_property(impl->metadata, + pw_global_get_id(global), NULL, NULL, NULL); +} + +static const struct pw_context_events context_events = { + PW_VERSION_CONTEXT_EVENTS, + .global_removed = context_global_removed, +}; + +static void global_resource_destroy(void *data) +{ + struct impl *impl = data; + spa_hook_remove(&impl->context_listener); + spa_hook_remove(&impl->resource_listener); + impl->resource = NULL; + impl->metadata = NULL; + if (impl->global) + pw_global_destroy(impl->global); + free(impl); +} + +static const struct pw_resource_events global_resource_events = { + PW_VERSION_RESOURCE_EVENTS, + .destroy = global_resource_destroy, +}; + +void * +pw_metadata_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 + }; + + if (properties == NULL) + properties = pw_properties_new(NULL, NULL); + if (properties == NULL) + return NULL; + + impl = calloc(1, sizeof(*impl)); + if (impl == NULL) { + pw_properties_free(properties); + return NULL; + } + + pw_resource_install_marshal(resource, true); + + impl->global = pw_global_new(context, + PW_TYPE_INTERFACE_Metadata, + PW_VERSION_METADATA, + properties, + global_bind, impl); + if (impl->global == NULL) { + free(impl); + return NULL; + } + impl->resource = resource; + impl->metadata = (struct pw_metadata*)resource; + + spa_scnprintf(serial_str, sizeof(serial_str), "%"PRIu64, + pw_global_get_serial(impl->global)); + pw_global_update_keys(impl->global, &extra_props, keys); + + pw_context_add_listener(context, &impl->context_listener, + &context_events, impl); + + pw_global_add_listener(impl->global, + &impl->global_listener, + &global_events, impl); + + pw_resource_set_bound_id(resource, pw_global_get_id(impl->global)); + pw_global_register(impl->global); + + pw_resource_add_listener(resource, + &impl->resource_listener, + &global_resource_events, impl); + + return impl; +} diff --git a/src/modules/module-metadata/protocol-native.c b/src/modules/module-metadata/protocol-native.c new file mode 100644 index 0000000..54958db --- /dev/null +++ b/src/modules/module-metadata/protocol-native.c @@ -0,0 +1,354 @@ +/* PipeWire + * + * Copyright © 2019 Wim Taymans + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include <errno.h> + +#include <spa/pod/builder.h> +#include <spa/pod/parser.h> +#include <spa/utils/result.h> + +#include <pipewire/pipewire.h> + +#include <pipewire/extensions/protocol-native.h> +#include <pipewire/extensions/metadata.h> + +static int metadata_resource_marshal_add_listener(void *object, + struct spa_hook *listener, + const struct pw_metadata_events *events, + void *data) +{ + struct pw_resource *resource = object; + struct spa_pod_builder *b; + + pw_resource_add_object_listener(resource, listener, events, data); + + b = pw_protocol_native_begin_resource(resource, PW_METADATA_METHOD_ADD_LISTENER, NULL); + return pw_protocol_native_end_resource(resource, b); +} + +static int metadata_proxy_marshal_add_listener(void *object, + struct spa_hook *listener, + const struct pw_metadata_events *events, + void *data) +{ + struct pw_proxy *proxy = object; + pw_proxy_add_object_listener(proxy, listener, events, data); + return 0; +} + +static int metadata_resource_demarshal_add_listener(void *object, + const struct pw_protocol_native_message *msg) +{ + return -ENOTSUP; +} + +static const struct pw_metadata_events pw_protocol_native_metadata_client_event_marshal; + +static int metadata_proxy_demarshal_add_listener(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = object; + struct spa_hook listener; + int res; + + spa_zero(listener); + res = pw_proxy_notify(proxy, struct pw_metadata_methods, add_listener, 0, + &listener, &pw_protocol_native_metadata_client_event_marshal, object); + spa_hook_remove(&listener); + + return res; +} + +static void metadata_marshal_set_property(struct spa_pod_builder *b, uint32_t subject, + const char *key, const char *type, const char *value) +{ + spa_pod_builder_add_struct(b, + SPA_POD_Int(subject), + SPA_POD_String(key), + SPA_POD_String(type), + SPA_POD_String(value)); +} + +static int metadata_proxy_marshal_set_property(void *object, uint32_t subject, + const char *key, const char *type, const char *value) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + b = pw_protocol_native_begin_proxy(proxy, PW_METADATA_METHOD_SET_PROPERTY, NULL); + metadata_marshal_set_property(b, subject, key, type, value); + return pw_protocol_native_end_proxy(proxy, b); +} +static int metadata_resource_marshal_set_property(void *object, uint32_t subject, + const char *key, const char *type, const char *value) +{ + struct pw_resource *resource = object; + struct spa_pod_builder *b; + b = pw_protocol_native_begin_resource(resource, PW_METADATA_METHOD_SET_PROPERTY, NULL); + metadata_marshal_set_property(b, subject, key, type, value); + return pw_protocol_native_end_resource(resource, b); +} + +static int metadata_demarshal_set_property(struct spa_pod_parser *prs, uint32_t *subject, + char **key, char **type, char **value) +{ + return spa_pod_parser_get_struct(prs, + SPA_POD_Int(subject), + SPA_POD_String(key), + SPA_POD_String(type), + SPA_POD_String(value)); +} + +static int metadata_proxy_demarshal_set_property(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = object; + struct spa_pod_parser prs; + uint32_t subject; + char *key, *type, *value; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (metadata_demarshal_set_property(&prs, &subject, &key, &type, &value) < 0) + return -EINVAL; + return pw_proxy_notify(proxy, struct pw_metadata_methods, set_property, 0, subject, key, type, value); +} + +static int metadata_resource_demarshal_set_property(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs; + uint32_t subject; + char *key, *type, *value; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (metadata_demarshal_set_property(&prs, &subject, &key, &type, &value) < 0) + return -EINVAL; + return pw_resource_notify(resource, struct pw_metadata_methods, set_property, 0, subject, key, type, value); +} + +static void metadata_marshal_clear(struct spa_pod_builder *b) +{ + spa_pod_builder_add_struct(b, SPA_POD_None()); +} + +static int metadata_proxy_marshal_clear(void *object) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + b = pw_protocol_native_begin_proxy(proxy, PW_METADATA_METHOD_CLEAR, NULL); + metadata_marshal_clear(b); + return pw_protocol_native_end_proxy(proxy, b); +} +static int metadata_resource_marshal_clear(void *object) +{ + struct pw_resource *resource = object; + struct spa_pod_builder *b; + b = pw_protocol_native_begin_resource(resource, PW_METADATA_METHOD_CLEAR, NULL); + metadata_marshal_clear(b); + return pw_protocol_native_end_resource(resource, b); +} + +static int metadata_proxy_demarshal_clear(void *object, const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = object; + struct spa_pod_parser prs; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, SPA_POD_None()) < 0) + return -EINVAL; + pw_proxy_notify(proxy, struct pw_metadata_methods, clear, 0); + return 0; +} + +static int metadata_resource_demarshal_clear(void *object, const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (spa_pod_parser_get_struct(&prs, SPA_POD_None()) < 0) + return -EINVAL; + pw_resource_notify(resource, struct pw_metadata_methods, clear, 0); + return 0; +} + +static void metadata_marshal_property(struct spa_pod_builder *b, uint32_t subject, + const char *key, const char *type, const char *value) +{ + spa_pod_builder_add_struct(b, + SPA_POD_Int(subject), + SPA_POD_String(key), + SPA_POD_String(type), + SPA_POD_String(value)); +} + +static int metadata_proxy_marshal_property(void *object, uint32_t subject, + const char *key, const char *type, const char *value) +{ + struct pw_proxy *proxy = object; + struct spa_pod_builder *b; + b = pw_protocol_native_begin_proxy(proxy, PW_METADATA_EVENT_PROPERTY, NULL); + metadata_marshal_property(b, subject, key, type, value); + return pw_protocol_native_end_proxy(proxy, b); +} + +static int metadata_resource_marshal_property(void *object, uint32_t subject, + const char *key, const char *type, const char *value) +{ + struct pw_resource *resource = object; + struct spa_pod_builder *b; + b = pw_protocol_native_begin_resource(resource, PW_METADATA_EVENT_PROPERTY, NULL); + metadata_marshal_property(b, subject, key, type, value); + return pw_protocol_native_end_resource(resource, b); +} + +static int metadata_demarshal_property(struct spa_pod_parser *prs, + uint32_t *subject, char **key, char **type, char **value) +{ + return spa_pod_parser_get_struct(prs, + SPA_POD_Int(subject), + SPA_POD_String(key), + SPA_POD_String(type), + SPA_POD_String(value)); +} + +static int metadata_proxy_demarshal_property(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_proxy *proxy = object; + struct spa_pod_parser prs; + uint32_t subject; + char *key, *type, *value; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (metadata_demarshal_property(&prs, + &subject, &key, &type, &value) < 0) + return -EINVAL; + pw_proxy_notify(proxy, struct pw_metadata_events, property, 0, subject, key, type, value); + return 0; +} + +static int metadata_resource_demarshal_property(void *object, + const struct pw_protocol_native_message *msg) +{ + struct pw_resource *resource = object; + struct spa_pod_parser prs; + uint32_t subject; + char *key, *type, *value; + + spa_pod_parser_init(&prs, msg->data, msg->size); + if (metadata_demarshal_property(&prs, + &subject, &key, &type, &value) < 0) + return -EINVAL; + pw_resource_notify(resource, struct pw_metadata_events, property, 0, subject, key, type, value); + return 0; +} + +static const struct pw_metadata_methods pw_protocol_native_metadata_client_method_marshal = { + PW_VERSION_METADATA_METHODS, + .add_listener = &metadata_proxy_marshal_add_listener, + .set_property = &metadata_proxy_marshal_set_property, + .clear = &metadata_proxy_marshal_clear, +}; +static const struct pw_metadata_methods pw_protocol_native_metadata_server_method_marshal = { + PW_VERSION_METADATA_METHODS, + .add_listener = &metadata_resource_marshal_add_listener, + .set_property = &metadata_resource_marshal_set_property, + .clear = &metadata_resource_marshal_clear, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_metadata_client_method_demarshal[PW_METADATA_METHOD_NUM] = +{ + [PW_METADATA_METHOD_ADD_LISTENER] = { &metadata_proxy_demarshal_add_listener, 0 }, + [PW_METADATA_METHOD_SET_PROPERTY] = { &metadata_proxy_demarshal_set_property, PW_PERM_W }, + [PW_METADATA_METHOD_CLEAR] = { &metadata_proxy_demarshal_clear, PW_PERM_W }, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_metadata_server_method_demarshal[PW_METADATA_METHOD_NUM] = +{ + [PW_METADATA_METHOD_ADD_LISTENER] = { &metadata_resource_demarshal_add_listener, 0 }, + [PW_METADATA_METHOD_SET_PROPERTY] = { &metadata_resource_demarshal_set_property, PW_PERM_W }, + [PW_METADATA_METHOD_CLEAR] = { &metadata_resource_demarshal_clear, PW_PERM_W }, +}; + +static const struct pw_metadata_events pw_protocol_native_metadata_client_event_marshal = { + PW_VERSION_METADATA_EVENTS, + .property = &metadata_proxy_marshal_property, +}; + +static const struct pw_metadata_events pw_protocol_native_metadata_server_event_marshal = { + PW_VERSION_METADATA_EVENTS, + .property = &metadata_resource_marshal_property, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_metadata_client_event_demarshal[PW_METADATA_EVENT_NUM] = +{ + [PW_METADATA_EVENT_PROPERTY] = { &metadata_proxy_demarshal_property, 0 }, +}; + +static const struct pw_protocol_native_demarshal +pw_protocol_native_metadata_server_event_demarshal[PW_METADATA_EVENT_NUM] = +{ + [PW_METADATA_EVENT_PROPERTY] = { &metadata_resource_demarshal_property, 0 }, +}; + +static const struct pw_protocol_marshal pw_protocol_native_metadata_marshal = { + PW_TYPE_INTERFACE_Metadata, + PW_VERSION_METADATA, + 0, + PW_METADATA_METHOD_NUM, + PW_METADATA_EVENT_NUM, + .client_marshal = &pw_protocol_native_metadata_client_method_marshal, + .server_demarshal = pw_protocol_native_metadata_server_method_demarshal, + .server_marshal = &pw_protocol_native_metadata_server_event_marshal, + .client_demarshal = pw_protocol_native_metadata_client_event_demarshal, +}; + +static const struct pw_protocol_marshal pw_protocol_native_metadata_impl_marshal = { + PW_TYPE_INTERFACE_Metadata, + PW_VERSION_METADATA, + PW_PROTOCOL_MARSHAL_FLAG_IMPL, + PW_METADATA_EVENT_NUM, + PW_METADATA_METHOD_NUM, + .client_marshal = &pw_protocol_native_metadata_client_event_marshal, + .server_demarshal = pw_protocol_native_metadata_server_event_demarshal, + .server_marshal = &pw_protocol_native_metadata_server_method_marshal, + .client_demarshal = pw_protocol_native_metadata_client_method_demarshal, +}; + +int pw_protocol_native_ext_metadata_init(struct pw_context *context) +{ + struct pw_protocol *protocol; + + protocol = pw_context_find_protocol(context, PW_TYPE_INFO_PROTOCOL_Native); + if (protocol == NULL) + return -EPROTO; + + pw_protocol_add_marshal(protocol, &pw_protocol_native_metadata_marshal); + pw_protocol_add_marshal(protocol, &pw_protocol_native_metadata_impl_marshal); + return 0; +} diff --git a/src/modules/module-metadata/proxy-metadata.c b/src/modules/module-metadata/proxy-metadata.c new file mode 100644 index 0000000..e8b747f --- /dev/null +++ b/src/modules/module-metadata/proxy-metadata.c @@ -0,0 +1,92 @@ +/* PipeWire + * + * Copyright © 2019 Wim Taymans + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include <stdio.h> +#include <unistd.h> +#include <errno.h> + +#include <spa/pod/parser.h> + +#include "pipewire/pipewire.h" +#include "pipewire/extensions/metadata.h" + +struct object_data { + struct pw_metadata *object; + struct spa_hook object_listener; + struct spa_hook object_methods; + + struct pw_proxy *proxy; + struct spa_hook proxy_listener; +}; + +static void proxy_object_destroy(void *_data) +{ + struct object_data *data = _data; + spa_hook_remove(&data->proxy_listener); + spa_hook_remove(&data->object_listener); + spa_hook_remove(&data->object_methods); +} + +static const struct pw_proxy_events proxy_events = { + PW_VERSION_PROXY_EVENTS, + .destroy = proxy_object_destroy, +}; + +struct pw_proxy *pw_core_metadata_export(struct pw_core *core, + const char *type, const struct spa_dict *props, void *object, + size_t user_data_size) +{ + struct pw_metadata *meta = object; + struct spa_interface *iface, *miface; + struct pw_proxy *proxy; + struct object_data *data; + + proxy = pw_core_create_object(core, + "metadata", + PW_TYPE_INTERFACE_Metadata, + PW_VERSION_METADATA, + 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); + data->object = object; + data->proxy = proxy; + + iface = (struct spa_interface*)proxy; + miface = (struct spa_interface*)meta; + + 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, + miface->cb.funcs, miface->cb.data); + pw_metadata_add_listener(meta, &data->object_listener, + iface->cb.funcs, iface->cb.data); + + return proxy; +} |