summaryrefslogtreecommitdiffstats
path: root/src/modules/module-client-device
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/modules/module-client-device.c232
-rw-r--r--src/modules/module-client-device/client-device.h44
-rw-r--r--src/modules/module-client-device/protocol-native.c556
-rw-r--r--src/modules/module-client-device/proxy-device.c90
-rw-r--r--src/modules/module-client-device/resource-device.c155
5 files changed, 1077 insertions, 0 deletions
diff --git a/src/modules/module-client-device.c b/src/modules/module-client-device.c
new file mode 100644
index 0000000..cf3bae3
--- /dev/null
+++ b/src/modules/module-client-device.c
@@ -0,0 +1,232 @@
+/* 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 "module-client-device/client-device.h"
+
+/** \page page_module_client_device PipeWire Module: Client Device
+ */
+
+#define NAME "client-device"
+
+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 and control remote devices" },
+ { PW_KEY_MODULE_VERSION, PACKAGE_VERSION },
+};
+
+struct pw_proxy *pw_core_spa_device_export(struct pw_core *core,
+ const char *type, const struct spa_dict *props, void *object,
+ size_t user_data_size);
+
+struct pw_protocol *pw_protocol_native_ext_client_device_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_spadevice;
+};
+
+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_impl_factory *factory = data->factory;
+ void *result;
+ struct pw_resource *device_resource;
+ struct pw_impl_client *client;
+ int res;
+
+ if (resource == NULL) {
+ res = -EINVAL;
+ goto error_exit;
+ }
+
+ client = pw_resource_get_client(resource);
+ device_resource = pw_resource_new(client, new_id, PW_PERM_ALL, type, version, 0);
+ if (device_resource == NULL) {
+ res = -errno;
+ goto error_resource;
+ }
+
+ if (properties == NULL)
+ properties = pw_properties_new(NULL, NULL);
+ if (properties == NULL) {
+ res = -errno;
+ goto error_properties;
+ }
+
+ pw_properties_setf(properties, PW_KEY_FACTORY_ID, "%d",
+ pw_global_get_id(pw_impl_factory_get_global(factory)));
+ pw_properties_setf(properties, PW_KEY_CLIENT_ID, "%d",
+ pw_global_get_id(pw_impl_client_get_global(client)));
+
+ result = pw_client_device_new(device_resource, properties);
+ if (result == NULL) {
+ res = -errno;
+ goto error_device;
+ }
+ 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_properties:
+ pw_log_error("can't create properties: %s", spa_strerror(res));
+ pw_resource_errorf_id(resource, new_id, res, "can't create properties: %s", spa_strerror(res));
+ goto error_exit_free;
+error_device:
+ pw_log_error("can't create device: %s", spa_strerror(res));
+ pw_resource_errorf_id(resource, new_id, res, "can't create device: %s", spa_strerror(res));
+ goto error_exit_free;
+
+error_exit_free:
+ pw_resource_remove(device_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_spadevice.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);
+ factory = pw_context_create_factory(context,
+ "client-device",
+ SPA_TYPE_INTERFACE_Device,
+ SPA_VERSION_DEVICE,
+ pw_properties_new(
+ PW_KEY_FACTORY_USAGE, CLIENT_DEVICE_USAGE,
+ 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_spadevice.type = SPA_TYPE_INTERFACE_Device;
+ data->export_spadevice.func = pw_core_spa_device_export;
+ if ((res = pw_context_register_export_type(context, &data->export_spadevice)) < 0)
+ goto error;
+
+ pw_protocol_native_ext_client_device_init(context);
+
+ 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-client-device/client-device.h b/src/modules/module-client-device/client-device.h
new file mode 100644
index 0000000..ada2b7d
--- /dev/null
+++ b/src/modules/module-client-device/client-device.h
@@ -0,0 +1,44 @@
+/* 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.
+ */
+
+#ifndef PIPEWIRE_CLIENT_DEVICE_H
+#define PIPEWIRE_CLIENT_DEVICE_H
+
+#include <pipewire/impl.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define CLIENT_DEVICE_USAGE "["PW_KEY_DEVICE_NAME"=<string>]"
+
+struct pw_device *
+pw_client_device_new(struct pw_resource *resource,
+ struct pw_properties *properties);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* PIPEWIRE_CLIENT_DEVICE_H */
diff --git a/src/modules/module-client-device/protocol-native.c b/src/modules/module-client-device/protocol-native.c
new file mode 100644
index 0000000..2734aac
--- /dev/null
+++ b/src/modules/module-client-device/protocol-native.c
@@ -0,0 +1,556 @@
+/* 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/impl.h>
+
+#include <pipewire/extensions/protocol-native.h>
+
+#define MAX_DICT 1024
+#define MAX_PARAM_INFO 128
+
+static inline void push_item(struct spa_pod_builder *b, const struct spa_dict_item *item)
+{
+ const char *str;
+ spa_pod_builder_string(b, item->key);
+ str = item->value;
+ if (spa_strstartswith(str, "pointer:"))
+ str = "";
+ spa_pod_builder_string(b, str);
+}
+
+static inline int parse_item(struct spa_pod_parser *prs, struct spa_dict_item *item)
+{
+ int res;
+ if ((res = spa_pod_parser_get(prs,
+ SPA_POD_String(&item->key),
+ SPA_POD_String(&item->value),
+ NULL)) < 0)
+ return res;
+ if (spa_strstartswith(item->value, "pointer:"))
+ item->value = "";
+ return 0;
+}
+
+#define parse_dict(prs,d) \
+do { \
+ uint32_t i; \
+ if (spa_pod_parser_get(prs, \
+ SPA_POD_Int(&(d)->n_items), NULL) < 0) \
+ return -EINVAL; \
+ if ((d)->n_items > 0) { \
+ if ((d)->n_items > MAX_DICT) \
+ return -ENOSPC; \
+ (d)->items = alloca((d)->n_items * sizeof(struct spa_dict_item)); \
+ for (i = 0; i < (d)->n_items; i++) { \
+ if (parse_item(prs, (struct spa_dict_item *) &(d)->items[i]) < 0) \
+ return -EINVAL; \
+ } \
+ } \
+} while(0)
+
+#define parse_param_info(prs,n_params,params) \
+do { \
+ uint32_t i; \
+ if (spa_pod_parser_get(prs, \
+ SPA_POD_Int(&(n_params)), NULL) < 0) \
+ return -EINVAL; \
+ if (n_params > 0) { \
+ if (n_params > MAX_PARAM_INFO) \
+ return -ENOSPC; \
+ params = alloca(n_params * sizeof(struct spa_param_info)); \
+ for (i = 0; i < n_params; i++) { \
+ if (spa_pod_parser_get(prs, \
+ SPA_POD_Id(&(params[i]).id), \
+ SPA_POD_Int(&(params[i]).flags), NULL) < 0) \
+ return -EINVAL; \
+ } \
+ } \
+} while(0)
+
+static int device_marshal_add_listener(void *object,
+ struct spa_hook *listener,
+ const struct spa_device_events *events,
+ void *data)
+{
+ struct pw_resource *resource = object;
+ pw_resource_add_object_listener(resource, listener, events, data);
+ return 0;
+}
+
+static int device_demarshal_add_listener(void *object,
+ const struct pw_protocol_native_message *msg)
+{
+ return -ENOTSUP;
+}
+
+static int device_marshal_sync(void *object, int seq)
+{
+ struct pw_protocol_native_message *msg;
+ struct pw_resource *resource = object;
+ struct spa_pod_builder *b;
+
+ b = pw_protocol_native_begin_resource(resource, SPA_DEVICE_METHOD_SYNC, &msg);
+
+ spa_pod_builder_add_struct(b,
+ SPA_POD_Int(SPA_RESULT_RETURN_ASYNC(msg->seq)));
+
+ return pw_protocol_native_end_resource(resource, b);
+}
+
+static int device_demarshal_sync(void *object,
+ const struct pw_protocol_native_message *msg)
+{
+ struct pw_proxy *proxy = object;
+ struct spa_pod_parser prs;
+ int seq;
+
+ spa_pod_parser_init(&prs, msg->data, msg->size);
+ if (spa_pod_parser_get_struct(&prs,
+ SPA_POD_Int(&seq)) < 0)
+ return -EINVAL;
+
+ pw_proxy_notify(proxy, struct spa_device_methods, sync, 0, seq);
+ return 0;
+}
+
+static int device_marshal_enum_params(void *object, int seq,
+ uint32_t id, uint32_t index, uint32_t max,
+ 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, SPA_DEVICE_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(max),
+ SPA_POD_Pod(filter));
+
+ return pw_protocol_native_end_resource(resource, b);
+}
+
+static int device_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, max;
+ 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(&max),
+ SPA_POD_Pod(&filter)) < 0)
+ return -EINVAL;
+
+ pw_proxy_notify(proxy, struct spa_device_methods, enum_params, 0,
+ seq, id, index, max, filter);
+ return 0;
+}
+
+static int device_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, SPA_DEVICE_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 device_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(&param)) < 0)
+ return -EINVAL;
+
+ pw_proxy_notify(proxy, struct spa_device_methods, set_param, 0,
+ id, flags, param);
+ return 0;
+}
+
+static void device_marshal_info(void *data,
+ const struct spa_device_info *info)
+{
+ struct pw_proxy *proxy = data;
+ struct spa_pod_builder *b;
+ struct spa_pod_frame f[2];
+ uint32_t i, n_items;
+
+ b = pw_protocol_native_begin_proxy(proxy, SPA_DEVICE_EVENT_INFO, NULL);
+
+ spa_pod_builder_push_struct(b, &f[0]);
+ if (info) {
+ uint64_t change_mask = info->change_mask;
+
+ change_mask &= SPA_DEVICE_CHANGE_MASK_FLAGS |
+ SPA_DEVICE_CHANGE_MASK_PROPS |
+ SPA_DEVICE_CHANGE_MASK_PARAMS;
+
+ n_items = info->props ? info->props->n_items : 0;
+
+ spa_pod_builder_push_struct(b, &f[1]);
+ spa_pod_builder_add(b,
+ SPA_POD_Long(change_mask),
+ SPA_POD_Long(info->flags),
+ SPA_POD_Int(n_items), NULL);
+ for (i = 0; i < n_items; i++)
+ push_item(b, &info->props->items[i]);
+ spa_pod_builder_add(b,
+ SPA_POD_Int(info->n_params), NULL);
+ for (i = 0; i < info->n_params; i++) {
+ spa_pod_builder_add(b,
+ SPA_POD_Id(info->params[i].id),
+ SPA_POD_Int(info->params[i].flags), NULL);
+ }
+ spa_pod_builder_pop(b, &f[1]);
+ } else {
+ spa_pod_builder_add(b,
+ SPA_POD_Pod(NULL), NULL);
+ }
+ spa_pod_builder_pop(b, &f[0]);
+
+ pw_protocol_native_end_proxy(proxy, b);
+}
+
+static int device_demarshal_info(void *data,
+ const struct pw_protocol_native_message *msg)
+{
+ struct pw_resource *resource = data;
+ struct spa_pod_parser prs;
+ struct spa_pod *ipod;
+ struct spa_device_info info = SPA_DEVICE_INFO_INIT(), *infop;
+ struct spa_dict props = SPA_DICT_INIT(NULL, 0);
+
+ spa_pod_parser_init(&prs, msg->data, msg->size);
+
+ if (spa_pod_parser_get_struct(&prs,
+ SPA_POD_PodStruct(&ipod)) < 0)
+ return -EINVAL;
+
+ if (ipod) {
+ struct spa_pod_parser p2;
+ struct spa_pod_frame f2;
+ infop = &info;
+
+ spa_pod_parser_pod(&p2, ipod);
+ if (spa_pod_parser_push_struct(&p2, &f2) < 0 ||
+ spa_pod_parser_get(&p2,
+ SPA_POD_Long(&info.change_mask),
+ SPA_POD_Long(&info.flags), NULL) < 0)
+ return -EINVAL;
+
+ info.change_mask &= SPA_DEVICE_CHANGE_MASK_FLAGS |
+ SPA_DEVICE_CHANGE_MASK_PROPS |
+ SPA_DEVICE_CHANGE_MASK_PARAMS;
+
+ parse_dict(&p2, &props);
+ if (props.n_items > 0)
+ info.props = &props;
+
+ parse_param_info(&p2, info.n_params, info.params);
+ }
+ else {
+ infop = NULL;
+ }
+ pw_resource_notify(resource, struct spa_device_events, info, 0, infop);
+ return 0;
+}
+
+static void device_marshal_result(void *data,
+ int seq, int res, uint32_t type, const void *result)
+{
+ struct pw_proxy *proxy = data;
+ struct spa_pod_builder *b;
+ struct spa_pod_frame f[2];
+
+ b = pw_protocol_native_begin_proxy(proxy, SPA_DEVICE_EVENT_RESULT, NULL);
+ spa_pod_builder_push_struct(b, &f[0]);
+ spa_pod_builder_add(b,
+ SPA_POD_Int(seq),
+ SPA_POD_Int(res),
+ SPA_POD_Id(type),
+ NULL);
+
+ switch (type) {
+ case SPA_RESULT_TYPE_DEVICE_PARAMS:
+ {
+ const struct spa_result_device_params *r = result;
+ spa_pod_builder_add(b,
+ SPA_POD_Id(r->id),
+ SPA_POD_Int(r->index),
+ SPA_POD_Int(r->next),
+ SPA_POD_Pod(r->param),
+ NULL);
+ break;
+ }
+ default:
+ break;
+ }
+
+ spa_pod_builder_pop(b, &f[0]);
+
+ pw_protocol_native_end_proxy(proxy, b);
+}
+
+static int device_demarshal_result(void *data,
+ const struct pw_protocol_native_message *msg)
+{
+ struct pw_resource *resource = data;
+ struct spa_pod_parser prs;
+ struct spa_pod_frame f[1];
+ int seq, res;
+ uint32_t type;
+ const void *result;
+ struct spa_result_device_params params;
+
+ spa_pod_parser_init(&prs, msg->data, msg->size);
+ if (spa_pod_parser_push_struct(&prs, &f[0]) < 0 ||
+ spa_pod_parser_get(&prs,
+ SPA_POD_Int(&seq),
+ SPA_POD_Int(&res),
+ SPA_POD_Id(&type),
+ NULL) < 0)
+ return -EINVAL;
+
+ switch (type) {
+ case SPA_RESULT_TYPE_DEVICE_PARAMS:
+ if (spa_pod_parser_get(&prs,
+ SPA_POD_Id(&params.id),
+ SPA_POD_Int(&params.index),
+ SPA_POD_Int(&params.next),
+ SPA_POD_PodObject(&params.param),
+ NULL) < 0)
+ return -EINVAL;
+
+ result = &params;
+ break;
+
+ default:
+ result = NULL;
+ break;
+ }
+
+ pw_resource_notify(resource, struct spa_device_events, result, 0, seq, res, type, result);
+ return 0;
+}
+
+static void device_marshal_event(void *data, const struct spa_event *event)
+{
+ struct pw_proxy *proxy = data;
+ struct spa_pod_builder *b;
+
+ b = pw_protocol_native_begin_proxy(proxy, SPA_DEVICE_EVENT_EVENT, NULL);
+
+ spa_pod_builder_add_struct(b,
+ SPA_POD_Pod(event));
+
+ pw_protocol_native_end_proxy(proxy, b);
+}
+
+static int device_demarshal_event(void *data,
+ const struct pw_protocol_native_message *msg)
+{
+ struct pw_resource *resource = data;
+ struct spa_pod_parser prs;
+ struct spa_event *event;
+
+ spa_pod_parser_init(&prs, msg->data, msg->size);
+ if (spa_pod_parser_get_struct(&prs,
+ SPA_POD_PodObject(&event)) < 0)
+ return -EINVAL;
+
+ pw_resource_notify(resource, struct spa_device_events, event, 0, event);
+ return 0;
+}
+
+static void device_marshal_object_info(void *data, uint32_t id,
+ const struct spa_device_object_info *info)
+{
+ struct pw_proxy *proxy = data;
+ struct spa_pod_builder *b;
+ struct spa_pod_frame f[2];
+ uint32_t i, n_items;
+
+ b = pw_protocol_native_begin_proxy(proxy, SPA_DEVICE_EVENT_OBJECT_INFO, NULL);
+ spa_pod_builder_push_struct(b, &f[0]);
+ spa_pod_builder_add(b,
+ SPA_POD_Int(id),
+ NULL);
+ if (info) {
+ uint64_t change_mask = info->change_mask;
+
+ change_mask &= SPA_DEVICE_OBJECT_CHANGE_MASK_FLAGS |
+ SPA_DEVICE_OBJECT_CHANGE_MASK_PROPS;
+
+ n_items = info->props ? info->props->n_items : 0;
+
+ spa_pod_builder_push_struct(b, &f[1]);
+ spa_pod_builder_add(b,
+ SPA_POD_String(info->type),
+ SPA_POD_Long(change_mask),
+ SPA_POD_Long(info->flags),
+ SPA_POD_Int(n_items), NULL);
+ for (i = 0; i < n_items; i++)
+ push_item(b, &info->props->items[i]);
+ spa_pod_builder_pop(b, &f[1]);
+ } else {
+ spa_pod_builder_add(b,
+ SPA_POD_Pod(NULL), NULL);
+ }
+ spa_pod_builder_pop(b, &f[0]);
+
+ pw_protocol_native_end_proxy(proxy, b);
+}
+
+static int device_demarshal_object_info(void *data,
+ const struct pw_protocol_native_message *msg)
+{
+ struct pw_resource *resource = data;
+ struct spa_pod_parser prs;
+ struct spa_device_object_info info = SPA_DEVICE_OBJECT_INFO_INIT(), *infop;
+ struct spa_pod *ipod;
+ struct spa_dict props = SPA_DICT_INIT(NULL, 0);
+ uint32_t id;
+
+ spa_pod_parser_init(&prs, msg->data, msg->size);
+ if (spa_pod_parser_get_struct(&prs,
+ SPA_POD_Int(&id),
+ SPA_POD_PodStruct(&ipod)) < 0)
+ return -EINVAL;
+
+ if (ipod) {
+ struct spa_pod_parser p2;
+ struct spa_pod_frame f2;
+ infop = &info;
+
+ spa_pod_parser_pod(&p2, ipod);
+ if (spa_pod_parser_push_struct(&p2, &f2) < 0 ||
+ spa_pod_parser_get(&p2,
+ SPA_POD_String(&info.type),
+ SPA_POD_Long(&info.change_mask),
+ SPA_POD_Long(&info.flags), NULL) < 0)
+ return -EINVAL;
+
+ info.change_mask &= SPA_DEVICE_OBJECT_CHANGE_MASK_FLAGS |
+ SPA_DEVICE_CHANGE_MASK_PROPS;
+
+ parse_dict(&p2, &props);
+ if (props.n_items > 0)
+ info.props = &props;
+ } else {
+ infop = NULL;
+ }
+
+ pw_resource_notify(resource, struct spa_device_events, object_info, 0, id, infop);
+ return 0;
+}
+
+static const struct spa_device_methods pw_protocol_native_device_method_marshal = {
+ SPA_VERSION_DEVICE_METHODS,
+ .add_listener = &device_marshal_add_listener,
+ .sync = &device_marshal_sync,
+ .enum_params = &device_marshal_enum_params,
+ .set_param = &device_marshal_set_param
+};
+
+static const struct pw_protocol_native_demarshal
+pw_protocol_native_device_method_demarshal[SPA_DEVICE_METHOD_NUM] =
+{
+ [SPA_DEVICE_METHOD_ADD_LISTENER] = { &device_demarshal_add_listener, 0 },
+ [SPA_DEVICE_METHOD_SYNC] = { &device_demarshal_sync, 0 },
+ [SPA_DEVICE_METHOD_ENUM_PARAMS] = { &device_demarshal_enum_params, 0 },
+ [SPA_DEVICE_METHOD_SET_PARAM] = { &device_demarshal_set_param, 0 },
+};
+
+static const struct spa_device_events pw_protocol_native_device_event_marshal = {
+ SPA_VERSION_DEVICE_EVENTS,
+ .info = &device_marshal_info,
+ .result = &device_marshal_result,
+ .event = &device_marshal_event,
+ .object_info = &device_marshal_object_info,
+};
+
+static const struct pw_protocol_native_demarshal
+pw_protocol_native_device_event_demarshal[SPA_DEVICE_EVENT_NUM] =
+{
+ [SPA_DEVICE_EVENT_INFO] = { &device_demarshal_info, 0 },
+ [SPA_DEVICE_EVENT_RESULT] = { &device_demarshal_result, 0 },
+ [SPA_DEVICE_EVENT_EVENT] = { &device_demarshal_event, 0 },
+ [SPA_DEVICE_EVENT_OBJECT_INFO] = { &device_demarshal_object_info, 0 },
+};
+
+static const struct pw_protocol_marshal pw_protocol_native_client_device_marshal = {
+ SPA_TYPE_INTERFACE_Device,
+ SPA_VERSION_DEVICE,
+ PW_PROTOCOL_MARSHAL_FLAG_IMPL,
+ SPA_DEVICE_EVENT_NUM,
+ SPA_DEVICE_METHOD_NUM,
+ .client_marshal = &pw_protocol_native_device_event_marshal,
+ .server_demarshal = pw_protocol_native_device_event_demarshal,
+ .server_marshal = &pw_protocol_native_device_method_marshal,
+ .client_demarshal = pw_protocol_native_device_method_demarshal,
+};
+
+struct pw_protocol *pw_protocol_native_ext_client_device_init(struct pw_context *context)
+{
+ struct pw_protocol *protocol;
+
+ protocol = pw_context_find_protocol(context, PW_TYPE_INFO_PROTOCOL_Native);
+
+ if (protocol == NULL)
+ return NULL;
+
+ pw_protocol_add_marshal(protocol, &pw_protocol_native_client_device_marshal);
+
+ return protocol;
+}
diff --git a/src/modules/module-client-device/proxy-device.c b/src/modules/module-client-device/proxy-device.c
new file mode 100644
index 0000000..43bdf8e
--- /dev/null
+++ b/src/modules/module-client-device/proxy-device.c
@@ -0,0 +1,90 @@
+/* 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 <spa/monitor/device.h>
+
+#include "pipewire/pipewire.h"
+
+struct device_data {
+ struct spa_device *device;
+ struct spa_hook device_listener;
+ struct spa_hook device_methods;
+
+ struct pw_proxy *proxy;
+ struct spa_hook proxy_listener;
+};
+
+static void proxy_device_destroy(void *_data)
+{
+ struct device_data *data = _data;
+ spa_hook_remove(&data->device_listener);
+ spa_hook_remove(&data->device_methods);
+ spa_hook_remove(&data->proxy_listener);
+}
+
+static const struct pw_proxy_events proxy_events = {
+ PW_VERSION_PROXY_EVENTS,
+ .destroy = proxy_device_destroy,
+};
+
+struct pw_proxy *pw_core_spa_device_export(struct pw_core *core,
+ const char *type, const struct spa_dict *props, void *object,
+ size_t user_data_size)
+{
+ struct spa_device *device = object;
+ struct spa_interface *iface, *diface;
+ struct pw_proxy *proxy;
+ struct device_data *data;
+
+ proxy = pw_core_create_object(core,
+ "client-device",
+ SPA_TYPE_INTERFACE_Device,
+ SPA_VERSION_DEVICE,
+ props,
+ user_data_size + sizeof(struct device_data));
+ if (proxy == NULL)
+ return NULL;
+
+ data = pw_proxy_get_user_data(proxy);
+ data = SPA_PTROFF(data, user_data_size, struct device_data);
+ data->device = device;
+ data->proxy = proxy;
+
+ iface = (struct spa_interface*)proxy;
+ diface = (struct spa_interface*)device;
+
+ pw_proxy_add_listener(proxy, &data->proxy_listener, &proxy_events, data);
+
+ pw_proxy_add_object_listener(proxy, &data->device_methods,
+ diface->cb.funcs, diface->cb.data);
+ spa_device_add_listener(device, &data->device_listener,
+ iface->cb.funcs, iface->cb.data);
+
+ return proxy;
+}
diff --git a/src/modules/module-client-device/resource-device.c b/src/modules/module-client-device/resource-device.c
new file mode 100644
index 0000000..0f5a40b
--- /dev/null
+++ b/src/modules/module-client-device/resource-device.c
@@ -0,0 +1,155 @@
+/* PipeWire
+ *
+ * Copyright © 2018 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 <stddef.h>
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <time.h>
+
+#include <spa/monitor/device.h>
+#include <spa/monitor/utils.h>
+#include <spa/pod/filter.h>
+#include <spa/pod/parser.h>
+#include <spa/debug/types.h>
+
+#include <pipewire/impl.h>
+
+struct impl {
+ struct pw_context *context;
+ struct pw_impl_device *device;
+ struct spa_hook device_listener;
+
+ struct pw_resource *resource;
+ struct spa_hook resource_listener;
+ struct spa_hook object_listener;
+
+ unsigned int registered:1;
+};
+
+static void device_info(void *data, const struct spa_device_info *info)
+{
+ struct impl *impl = data;
+ if (!impl->registered) {
+ pw_impl_device_set_implementation(impl->device,
+ (struct spa_device*)impl->resource);
+ pw_impl_device_register(impl->device, NULL);
+ impl->registered = true;
+ }
+}
+
+static const struct spa_device_events object_events = {
+ SPA_VERSION_DEVICE_EVENTS,
+ .info = device_info,
+};
+
+static void device_resource_destroy(void *data)
+{
+ struct impl *impl = data;
+
+ pw_log_debug("client-device %p: destroy", impl);
+
+ impl->resource = NULL;
+ spa_hook_remove(&impl->device_listener);
+ spa_hook_remove(&impl->resource_listener);
+ spa_hook_remove(&impl->object_listener);
+ pw_impl_device_destroy(impl->device);
+}
+
+static const struct pw_resource_events resource_events = {
+ PW_VERSION_RESOURCE_EVENTS,
+ .destroy = device_resource_destroy,
+};
+
+static void device_destroy(void *data)
+{
+ struct impl *impl = data;
+
+ pw_log_debug("client-device %p: destroy", impl);
+
+ impl->device = NULL;
+ spa_hook_remove(&impl->device_listener);
+ spa_hook_remove(&impl->resource_listener);
+ spa_hook_remove(&impl->object_listener);
+ pw_resource_destroy(impl->resource);
+}
+
+static void device_initialized(void *data)
+{
+ struct impl *impl = data;
+ struct pw_impl_device *device = impl->device;
+ struct pw_global *global = pw_impl_device_get_global(device);
+ uint32_t id = pw_global_get_id(global);
+
+ pw_log_debug("client-device %p: initialized global:%d", impl, id);
+ pw_resource_set_bound_id(impl->resource, id);
+}
+
+static const struct pw_impl_device_events device_events = {
+ PW_VERSION_IMPL_DEVICE_EVENTS,
+ .destroy = device_destroy,
+ .initialized = device_initialized,
+};
+
+struct pw_impl_device *pw_client_device_new(struct pw_resource *resource,
+ struct pw_properties *properties)
+{
+ struct impl *impl;
+ struct pw_impl_device *device;
+ struct pw_impl_client *client = pw_resource_get_client(resource);
+ struct pw_context *context = pw_impl_client_get_context(client);
+
+ if (properties == NULL)
+ properties = pw_properties_new(NULL, NULL);
+ if (properties == NULL)
+ return NULL;
+
+ pw_properties_setf(properties, PW_KEY_CLIENT_ID, "%d",
+ pw_impl_client_get_info(client)->id);
+
+ device = pw_context_create_device(context, properties, sizeof(struct impl));
+ if (device == NULL)
+ return NULL;
+
+ impl = pw_impl_device_get_user_data(device);
+ impl->device = device;
+ impl->context = context;
+ impl->resource = resource;
+
+ pw_impl_device_add_listener(impl->device,
+ &impl->device_listener,
+ &device_events, impl);
+
+ pw_resource_add_listener(impl->resource,
+ &impl->resource_listener,
+ &resource_events,
+ impl);
+ pw_resource_add_object_listener(impl->resource,
+ &impl->object_listener,
+ &object_events,
+ impl);
+
+ return device;
+}