1 /* PipeWire
2 *
3 * Copyright © 2018 Wim Taymans
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <string.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <math.h>
29 #include <time.h>
30
31 #include "config.h"
32
33 #include <spa/node/node.h>
34 #include <spa/node/utils.h>
35 #include <spa/utils/hook.h>
36 #include <spa/utils/result.h>
37 #include <spa/utils/names.h>
38 #include <spa/utils/string.h>
39 #include <spa/utils/type-info.h>
40 #include <spa/param/format.h>
41 #include <spa/param/format-utils.h>
42 #include <spa/debug/types.h>
43
44 #include "pipewire/pipewire.h"
45
46 #include "modules/spa/spa-node.h"
47
48 #define NAME "adapter"
49
50 PW_LOG_TOPIC_EXTERN(mod_topic);
51 #define PW_LOG_TOPIC_DEFAULT mod_topic
52
53 struct buffer {
54 struct spa_buffer buf;
55 struct spa_data datas[1];
56 struct spa_chunk chunk[1];
57 };
58
59 struct node {
60 struct pw_context *context;
61
62 struct pw_impl_node *node;
63 struct spa_hook node_listener;
64
65 struct pw_impl_node *follower;
66
67 void *user_data;
68 enum pw_direction direction;
69 struct pw_properties *props;
70
71 uint32_t media_type;
72 uint32_t media_subtype;
73
74 struct spa_list ports;
75 };
76
77 /** \endcond */
node_free(void * data)78 static void node_free(void *data)
79 {
80 struct node *n = data;
81 spa_hook_remove(&n->node_listener);
82 pw_properties_free(n->props);
83 }
84
node_port_init(void * data,struct pw_impl_port * port)85 static void node_port_init(void *data, struct pw_impl_port *port)
86 {
87 struct node *n = data;
88 const struct pw_properties *old;
89 enum pw_direction direction;
90 struct pw_properties *new;
91 const char *str, *path, *desc, *nick, *name, *node_name, *media_class;
92 char position[8], *prefix;
93 bool is_monitor, is_device, is_duplex, is_virtual;
94
95 direction = pw_impl_port_get_direction(port);
96
97 old = pw_impl_port_get_properties(port);
98
99 is_monitor = pw_properties_get_bool(old, PW_KEY_PORT_MONITOR, false);
100 if (!is_monitor && direction != n->direction)
101 return;
102
103 path = pw_properties_get(n->props, PW_KEY_OBJECT_PATH);
104 media_class = pw_properties_get(n->props, PW_KEY_MEDIA_CLASS);
105
106 if (media_class != NULL &&
107 (strstr(media_class, "Sink") != NULL ||
108 strstr(media_class, "Source") != NULL))
109 is_device = true;
110 else
111 is_device = false;
112
113 is_duplex = media_class != NULL && strstr(media_class, "Duplex") != NULL;
114 is_virtual = media_class != NULL && strstr(media_class, "Virtual") != NULL;
115
116 new = pw_properties_new(NULL, NULL);
117
118 if (is_duplex)
119 prefix = direction == PW_DIRECTION_INPUT ?
120 "playback" : "capture";
121 else if (is_virtual)
122 prefix = direction == PW_DIRECTION_INPUT ?
123 "input" : "capture";
124 else if (is_device)
125 prefix = direction == PW_DIRECTION_INPUT ?
126 "playback" : is_monitor ? "monitor" : "capture";
127 else
128 prefix = direction == PW_DIRECTION_INPUT ?
129 "input" : is_monitor ? "monitor" : "output";
130
131 if ((str = pw_properties_get(old, PW_KEY_AUDIO_CHANNEL)) == NULL ||
132 spa_streq(str, "UNK")) {
133 snprintf(position, sizeof(position), "%d", pw_impl_port_get_id(port) + 1);
134 str = position;
135 }
136 if (direction == n->direction) {
137 if (is_device) {
138 pw_properties_set(new, PW_KEY_PORT_PHYSICAL, "true");
139 pw_properties_set(new, PW_KEY_PORT_TERMINAL, "true");
140 }
141 }
142
143 desc = pw_properties_get(n->props, PW_KEY_NODE_DESCRIPTION);
144 nick = pw_properties_get(n->props, PW_KEY_NODE_NICK);
145 name = pw_properties_get(n->props, PW_KEY_NODE_NAME);
146
147 if ((node_name = desc) == NULL && (node_name = nick) == NULL &&
148 (node_name = name) == NULL)
149 node_name = "node";
150
151 pw_properties_setf(new, PW_KEY_OBJECT_PATH, "%s:%s_%d",
152 path ? path : node_name, prefix, pw_impl_port_get_id(port));
153
154 pw_properties_setf(new, PW_KEY_PORT_NAME, "%s_%s", prefix, str);
155
156 if ((node_name = nick) == NULL && (node_name = desc) == NULL &&
157 (node_name = name) == NULL)
158 node_name = "node";
159
160 pw_properties_setf(new, PW_KEY_PORT_ALIAS, "%s:%s_%s",
161 node_name, prefix, str);
162
163 pw_impl_port_update_properties(port, &new->dict);
164 pw_properties_free(new);
165 }
166
167 static const struct pw_impl_node_events node_events = {
168 PW_VERSION_IMPL_NODE_EVENTS,
169 .free = node_free,
170 .port_init = node_port_init,
171 };
172
173
find_format(struct pw_impl_node * node,enum pw_direction direction,uint32_t * media_type,uint32_t * media_subtype)174 static int find_format(struct pw_impl_node *node, enum pw_direction direction,
175 uint32_t *media_type, uint32_t *media_subtype)
176 {
177 uint32_t state = 0;
178 uint8_t buffer[4096];
179 struct spa_pod_builder b;
180 int res;
181 struct spa_pod *format;
182
183 spa_pod_builder_init(&b, buffer, sizeof(buffer));
184 if ((res = spa_node_port_enum_params_sync(pw_impl_node_get_implementation(node),
185 direction == PW_DIRECTION_INPUT ?
186 SPA_DIRECTION_INPUT :
187 SPA_DIRECTION_OUTPUT, 0,
188 SPA_PARAM_EnumFormat, &state,
189 NULL, &format, &b)) != 1) {
190 res = res < 0 ? res : -ENOENT;
191 pw_log_warn("%p: can't get format: %s", node, spa_strerror(res));
192 return res;
193 }
194
195 if ((res = spa_format_parse(format, media_type, media_subtype)) < 0)
196 return res;
197
198 pw_log_debug("%p: %s/%s", node,
199 spa_debug_type_find_name(spa_type_media_type, *media_type),
200 spa_debug_type_find_name(spa_type_media_subtype, *media_subtype));
201 return 0;
202 }
203
204
pw_adapter_new(struct pw_context * context,struct pw_impl_node * follower,struct pw_properties * props,size_t user_data_size)205 struct pw_impl_node *pw_adapter_new(struct pw_context *context,
206 struct pw_impl_node *follower,
207 struct pw_properties *props,
208 size_t user_data_size)
209 {
210 struct pw_impl_node *node;
211 struct node *n;
212 const char *str, *factory_name;
213 const struct pw_node_info *info;
214 enum pw_direction direction;
215 int res;
216 uint32_t media_type, media_subtype;
217
218 info = pw_impl_node_get_info(follower);
219 if (info == NULL) {
220 res = -EINVAL;
221 goto error;
222 }
223
224 pw_log_debug("%p: in %d/%d out %d/%d", follower,
225 info->n_input_ports, info->max_input_ports,
226 info->n_output_ports, info->max_output_ports);
227
228 pw_properties_update(props, info->props);
229
230 if (info->n_output_ports > 0) {
231 direction = PW_DIRECTION_OUTPUT;
232 } else if (info->n_input_ports > 0) {
233 direction = PW_DIRECTION_INPUT;
234 } else {
235 res = -EINVAL;
236 goto error;
237 }
238
239 if ((str = pw_properties_get(props, PW_KEY_NODE_ID)) != NULL)
240 pw_properties_set(props, PW_KEY_NODE_SESSION, str);
241
242 if (pw_properties_get(props, "factory.mode") == NULL) {
243 if (direction == PW_DIRECTION_INPUT)
244 str = "merge";
245 else
246 str = "split";
247 pw_properties_set(props, "factory.mode", str);
248 }
249
250 if ((res = find_format(follower, direction, &media_type, &media_subtype)) < 0)
251 goto error;
252
253 if (media_type == SPA_MEDIA_TYPE_audio) {
254 pw_properties_setf(props, "audio.adapt.follower", "pointer:%p",
255 pw_impl_node_get_implementation(follower));
256 pw_properties_set(props, SPA_KEY_LIBRARY_NAME, "audioconvert/libspa-audioconvert");
257 if (pw_properties_get(props, PW_KEY_MEDIA_CLASS) == NULL)
258 pw_properties_setf(props, PW_KEY_MEDIA_CLASS, "Audio/%s",
259 direction == PW_DIRECTION_INPUT ? "Sink" : "Source");
260 factory_name = SPA_NAME_AUDIO_ADAPT;
261 }
262 else if (media_type == SPA_MEDIA_TYPE_video) {
263 pw_properties_setf(props, "video.adapt.follower", "pointer:%p",
264 pw_impl_node_get_implementation(follower));
265 pw_properties_set(props, SPA_KEY_LIBRARY_NAME, "videoconvert/libspa-videoconvert");
266 if (pw_properties_get(props, PW_KEY_MEDIA_CLASS) == NULL)
267 pw_properties_setf(props, PW_KEY_MEDIA_CLASS, "Video/%s",
268 direction == PW_DIRECTION_INPUT ? "Sink" : "Source");
269 factory_name = SPA_NAME_VIDEO_ADAPT;
270 } else {
271 res = -ENOTSUP;
272 goto error;
273 }
274
275 node = pw_spa_node_load(context,
276 factory_name,
277 PW_SPA_NODE_FLAG_ACTIVATE | PW_SPA_NODE_FLAG_NO_REGISTER,
278 pw_properties_copy(props),
279 sizeof(struct node) + user_data_size);
280 if (node == NULL) {
281 res = -errno;
282 pw_log_error("can't load spa node: %m");
283 goto error;
284 }
285
286 n = pw_spa_node_get_user_data(node);
287 n->context = context;
288 n->node = node;
289 n->follower = follower;
290 n->direction = direction;
291 n->props = props;
292 n->media_type = media_type;
293 n->media_subtype = media_subtype;
294 spa_list_init(&n->ports);
295
296 if (user_data_size > 0)
297 n->user_data = SPA_PTROFF(n, sizeof(struct node), void);
298
299 pw_impl_node_add_listener(node, &n->node_listener, &node_events, n);
300
301 return node;
302
303 error:
304 pw_properties_free(props);
305 errno = -res;
306 return NULL;
307 }
308
pw_adapter_get_user_data(struct pw_impl_node * node)309 void *pw_adapter_get_user_data(struct pw_impl_node *node)
310 {
311 struct node *n = pw_spa_node_get_user_data(node);
312 return n->user_data;
313 }
314