module-client-node.c 5.99 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1
2
/* PipeWire
 *
Wim Taymans's avatar
Wim Taymans committed
3
 * Copyright © 2018 Wim Taymans
Wim Taymans's avatar
Wim Taymans committed
4
 *
Wim Taymans's avatar
Wim Taymans committed
5
6
7
8
9
10
 * 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:
Wim Taymans's avatar
Wim Taymans committed
11
 *
Wim Taymans's avatar
Wim Taymans committed
12
13
14
15
16
17
18
19
20
21
22
 * 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.
Wim Taymans's avatar
Wim Taymans committed
23
24
25
26
27
28
29
30
31
 */

#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <dlfcn.h>

#include "config.h"

Wim Taymans's avatar
Wim Taymans committed
32
33
#include <spa/utils/result.h>

34
#include <pipewire/impl.h>
Wim Taymans's avatar
Wim Taymans committed
35

Wim Taymans's avatar
Wim Taymans committed
36
#include "module-client-node/v0/client-node.h"
37
#include "module-client-node/client-node.h"
Wim Taymans's avatar
Wim Taymans committed
38

39
40
41
/** \page page_module_client_node PipeWire Module: Client Node
 */

42
43
#define NAME "client-node"

44
45
46
PW_LOG_TOPIC(mod_topic, "mod." NAME);
#define PW_LOG_TOPIC_DEFAULT mod_topic

Wim Taymans's avatar
Wim Taymans committed
47
static const struct spa_dict_item module_props[] = {
48
49
50
	{ PW_KEY_MODULE_AUTHOR, "Wim Taymans <wim.taymans@gmail.com>" },
	{ PW_KEY_MODULE_DESCRIPTION, "Allow clients to create and control remote nodes" },
	{ PW_KEY_MODULE_VERSION, PACKAGE_VERSION },
Wim Taymans's avatar
Wim Taymans committed
51
52
};

Wim Taymans's avatar
Wim Taymans committed
53
struct pw_proxy *pw_core_node_export(struct pw_core *core,
Wim Taymans's avatar
Wim Taymans committed
54
		const char *type, const struct spa_dict *props, void *object, size_t user_data_size);
Wim Taymans's avatar
Wim Taymans committed
55
struct pw_proxy *pw_core_spa_node_export(struct pw_core *core,
Wim Taymans's avatar
Wim Taymans committed
56
		const char *type, const struct spa_dict *props, void *object, size_t user_data_size);
57

Wim Taymans's avatar
Wim Taymans committed
58
59
struct pw_protocol *pw_protocol_native_ext_client_node_init(struct pw_context *context);
struct pw_protocol *pw_protocol_native_ext_client_node0_init(struct pw_context *context);
Wim Taymans's avatar
Wim Taymans committed
60

Wim Taymans's avatar
Wim Taymans committed
61
struct factory_data {
Wim Taymans's avatar
Wim Taymans committed
62
	struct pw_impl_factory *this;
Wim Taymans's avatar
Wim Taymans committed
63

Wim Taymans's avatar
Wim Taymans committed
64
	struct pw_impl_module *module;
Wim Taymans's avatar
Wim Taymans committed
65
	struct spa_hook module_listener;
66
67
68

	struct pw_export_type export_node;
	struct pw_export_type export_spanode;
Wim Taymans's avatar
Wim Taymans committed
69
70
};

Wim Taymans's avatar
Wim Taymans committed
71
72
static void *create_object(void *_data,
			   struct pw_resource *resource,
Wim Taymans's avatar
Wim Taymans committed
73
			   const char *type,
Wim Taymans's avatar
Wim Taymans committed
74
75
76
			   uint32_t version,
			   struct pw_properties *properties,
			   uint32_t new_id)
Wim Taymans's avatar
Wim Taymans committed
77
{
Wim Taymans's avatar
Wim Taymans committed
78
	void *result;
Wim Taymans's avatar
Wim Taymans committed
79
	struct pw_resource *node_resource;
80
	struct pw_impl_client *client;
Wim Taymans's avatar
Wim Taymans committed
81
	int res;
Wim Taymans's avatar
Wim Taymans committed
82

83
84
85
86
87
88
	if (resource == NULL) {
		res = -EINVAL;
		goto error_exit;
	}

	client = pw_resource_get_client(resource);
Wim Taymans's avatar
Wim Taymans committed
89
	node_resource = pw_resource_new(client, new_id, PW_PERM_ALL, type, version, 0);
Wim Taymans's avatar
Wim Taymans committed
90
91
92
93
	if (node_resource == NULL) {
		res = -errno;
		goto error_resource;
	}
Wim Taymans's avatar
Wim Taymans committed
94

Wim Taymans's avatar
Wim Taymans committed
95
	if (version == 0) {
96
		result = pw_impl_client_node0_new(node_resource, properties);
Wim Taymans's avatar
Wim Taymans committed
97
	} else {
98
		result = pw_impl_client_node_new(node_resource, properties, true);
Wim Taymans's avatar
Wim Taymans committed
99
	}
Wim Taymans's avatar
Wim Taymans committed
100
101
102
103
	if (result == NULL) {
		res = -errno;
		goto error_node;
	}
Wim Taymans's avatar
Wim Taymans committed
104
	return result;
Wim Taymans's avatar
Wim Taymans committed
105

Wim Taymans's avatar
Wim Taymans committed
106
107
error_resource:
	pw_log_error("can't create resource: %s", spa_strerror(res));
Wim Taymans's avatar
Wim Taymans committed
108
	pw_resource_errorf_id(resource, new_id, res, "can't create resource: %s", spa_strerror(res));
Wim Taymans's avatar
Wim Taymans committed
109
110
111
	goto error_exit;
error_node:
	pw_log_error("can't create node: %s", spa_strerror(res));
112
113
	pw_resource_errorf_id(resource, new_id, res, "can't create node: %s", spa_strerror(res));
	goto error_exit;
Wim Taymans's avatar
Wim Taymans committed
114
115
error_exit:
	errno = -res;
Wim Taymans's avatar
Wim Taymans committed
116
117
118
	return NULL;
}

Wim Taymans's avatar
Wim Taymans committed
119
120
static const struct pw_impl_factory_implementation impl_factory = {
	PW_VERSION_IMPL_FACTORY_IMPLEMENTATION,
Wim Taymans's avatar
Wim Taymans committed
121
	.create_object = create_object,
Wim Taymans's avatar
Wim Taymans committed
122
123
};

Wim Taymans's avatar
Wim Taymans committed
124
125
126
127
128
129
static void module_destroy(void *data)
{
	struct factory_data *d = data;

	spa_hook_remove(&d->module_listener);

130
131
132
	spa_list_remove(&d->export_node.link);
	spa_list_remove(&d->export_spanode.link);

Wim Taymans's avatar
Wim Taymans committed
133
	pw_impl_factory_destroy(d->this);
Wim Taymans's avatar
Wim Taymans committed
134
135
}

136
137
138
static void module_registered(void *data)
{
	struct factory_data *d = data;
Wim Taymans's avatar
Wim Taymans committed
139
	struct pw_impl_module *module = d->module;
Wim Taymans's avatar
Wim Taymans committed
140
	struct pw_impl_factory *factory = d->this;
141
142
143
144
	struct spa_dict_item items[1];
	char id[16];
	int res;

Wim Taymans's avatar
Wim Taymans committed
145
	snprintf(id, sizeof(id), "%d", pw_global_get_id(pw_impl_module_get_global(module)));
146
	items[0] = SPA_DICT_ITEM_INIT(PW_KEY_MODULE_ID, id);
Wim Taymans's avatar
Wim Taymans committed
147
	pw_impl_factory_update_properties(factory, &SPA_DICT_INIT(items, 1));
148

Wim Taymans's avatar
Wim Taymans committed
149
	if ((res = pw_impl_factory_register(factory, NULL)) < 0) {
150
		pw_log_error("%p: can't register factory: %s", factory, spa_strerror(res));
151
152
153
	}
}

Wim Taymans's avatar
Wim Taymans committed
154
155
static const struct pw_impl_module_events module_events = {
	PW_VERSION_IMPL_MODULE_EVENTS,
Wim Taymans's avatar
Wim Taymans committed
156
	.destroy = module_destroy,
157
	.registered = module_registered,
Wim Taymans's avatar
Wim Taymans committed
158
159
};

160
SPA_EXPORT
Wim Taymans's avatar
Wim Taymans committed
161
int pipewire__module_init(struct pw_impl_module *module, const char *args)
Wim Taymans's avatar
Wim Taymans committed
162
{
Wim Taymans's avatar
Wim Taymans committed
163
	struct pw_context *context = pw_impl_module_get_context(module);
Wim Taymans's avatar
Wim Taymans committed
164
	struct pw_impl_factory *factory;
Wim Taymans's avatar
Wim Taymans committed
165
	struct factory_data *data;
Wim Taymans's avatar
Wim Taymans committed
166

167
168
	PW_LOG_TOPIC_INIT(mod_topic);

169
	factory = pw_context_create_factory(context,
Wim Taymans's avatar
Wim Taymans committed
170
				 "client-node",
Wim Taymans's avatar
Wim Taymans committed
171
				 PW_TYPE_INTERFACE_ClientNode,
Wim Taymans's avatar
Wim Taymans committed
172
173
174
				 PW_VERSION_CLIENT_NODE,
				 NULL,
				 sizeof(*data));
Wim Taymans's avatar
Wim Taymans committed
175
	if (factory == NULL)
Wim Taymans's avatar
Wim Taymans committed
176
		return -errno;
Wim Taymans's avatar
Wim Taymans committed
177

Wim Taymans's avatar
Wim Taymans committed
178
	data = pw_impl_factory_get_user_data(factory);
Wim Taymans's avatar
Wim Taymans committed
179
	data->this = factory;
180
	data->module = module;
Wim Taymans's avatar
Wim Taymans committed
181

Wim Taymans's avatar
Wim Taymans committed
182
	pw_log_debug("module %p: new", module);
Wim Taymans's avatar
Wim Taymans committed
183

Wim Taymans's avatar
Wim Taymans committed
184
	pw_impl_factory_set_implementation(factory,
Wim Taymans's avatar
Wim Taymans committed
185
186
				      &impl_factory,
				      data);
Wim Taymans's avatar
Wim Taymans committed
187

Wim Taymans's avatar
Wim Taymans committed
188
189
	pw_protocol_native_ext_client_node_init(context);
	pw_protocol_native_ext_client_node0_init(context);
Wim Taymans's avatar
Wim Taymans committed
190

191
	data->export_node.type = PW_TYPE_INTERFACE_Node;
Wim Taymans's avatar
Wim Taymans committed
192
	data->export_node.func = pw_core_node_export;
Wim Taymans's avatar
Wim Taymans committed
193
	pw_context_register_export_type(context, &data->export_node);
194
195

	data->export_spanode.type = SPA_TYPE_INTERFACE_Node;
Wim Taymans's avatar
Wim Taymans committed
196
	data->export_spanode.func = pw_core_spa_node_export;
Wim Taymans's avatar
Wim Taymans committed
197
	pw_context_register_export_type(context, &data->export_spanode);
198

Wim Taymans's avatar
Wim Taymans committed
199
	pw_impl_module_add_listener(module, &data->module_listener, &module_events, data);
Wim Taymans's avatar
Wim Taymans committed
200

Wim Taymans's avatar
Wim Taymans committed
201
	pw_impl_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props));
Wim Taymans's avatar
Wim Taymans committed
202

203
	return 0;
Wim Taymans's avatar
Wim Taymans committed
204
}