Debian Codegraph @

tests/modules/si-standard-link.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2020 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "../common/base-test-fixture.h"
10
11 typedef struct {
12 WpBaseTestFixture base;
13
14 WpSessionItem *src_item;
15 WpSessionItem *sink_item;
16
17 } TestFixture;
18
19 static WpSessionItem *
20 load_node (TestFixture * f, const gchar * factory, const gchar * media_class,
21 const gchar * type)
22 {
23 g_autoptr (WpNode) node = NULL;
24 g_autoptr (WpSessionItem) adapter = NULL;
25
26 /* create audiotestsrc adapter node */
27 node = wp_node_new_from_factory (f->base.core,
28 "adapter",
29 wp_properties_new (
30 "factory.name", factory,
31 "node.name", factory,
32 "media.class", media_class,
33 "audio.channels", "2",
34 "audio.position", "[ FL, FR ]",
35 NULL));
36 g_assert_nonnull (node);
37 wp_object_activate (WP_OBJECT (node), WP_OBJECT_FEATURES_ALL,
38 NULL, (GAsyncReadyCallback) test_object_activate_finish_cb, f);
39 g_main_loop_run (f->base.loop);
40
41 /* create adapter */
42 adapter = wp_session_item_make (f->base.core, "si-audio-adapter");
43 g_assert_nonnull (adapter);
44 g_assert_true (WP_IS_SI_LINKABLE (adapter));
45
46 /* configure */
47 {
48 WpProperties *props = wp_properties_new_empty ();
49 wp_properties_setf (props, "item.node", "%p", node);
50 wp_properties_set (props, "media.class", media_class);
51 wp_properties_set (props, "item.node.type", type);
52 g_assert_true (wp_session_item_configure (adapter, props));
53 g_assert_true (wp_session_item_is_configured (adapter));
54 }
55
56 /* activate adapter */
57
58 wp_object_activate (WP_OBJECT (adapter),
59 WP_SESSION_ITEM_FEATURE_ACTIVE,
60 NULL, (GAsyncReadyCallback) test_object_activate_finish_cb, f);
61 g_main_loop_run (f->base.loop);
62 g_assert_cmphex (wp_object_get_active_features (WP_OBJECT (adapter)), ==,
63 WP_SESSION_ITEM_FEATURE_ACTIVE);
64
65 return g_steal_pointer (&adapter);
66 }
67
68 static void
69 on_plugin_loaded (WpCore * core, GAsyncResult * res, TestFixture *f)
70 {
71 gboolean loaded;
72 GError *error = NULL;
73
74 loaded = wp_core_load_component_finish (core, res, &error);
75 g_assert_no_error (error);
76 g_assert_true (loaded);
77
78 g_main_loop_quit (f->base.loop);
79 }
80
81 static void
82 test_si_standard_link_setup (TestFixture * f, gconstpointer user_data)
83 {
84 wp_base_test_fixture_setup (&f->base, 0);
85
86 /* load modules */
87 {
88 g_autoptr (WpTestServerLocker) lock =
89 wp_test_server_locker_new (&f->base.server);
90
91 g_assert_cmpint (pw_context_add_spa_lib (f->base.server.context,
92 "audiotestsrc", "audiotestsrc/libspa-audiotestsrc"), ==, 0);
93 if (!test_is_spa_lib_installed (&f->base, "audiotestsrc")) {
94 g_test_skip ("The pipewire audiotestsrc factory was not found");
95 return;
96 }
97 g_assert_nonnull (pw_context_load_module (f->base.server.context,
98 "libpipewire-module-adapter", NULL, NULL));
99 g_assert_nonnull (pw_context_load_module (f->base.server.context,
100 "libpipewire-module-link-factory", NULL, NULL));
101 }
102 {
103 wp_core_load_component (f->base.core,
104 "libwireplumber-module-si-audio-adapter", "module", NULL, NULL, NULL,
105 (GAsyncReadyCallback) on_plugin_loaded, f);
106 g_main_loop_run (f->base.loop);
107
108 wp_core_load_component (f->base.core,
109 "libwireplumber-module-si-standard-link", "module", NULL, NULL, NULL,
110 (GAsyncReadyCallback) on_plugin_loaded, f);
111 g_main_loop_run (f->base.loop);
112 }
113
114 if (test_is_spa_lib_installed (&f->base, "audiotestsrc"))
115 f->src_item = load_node (f, "audiotestsrc", "Stream/Output/Audio", "stream");
116 if (test_is_spa_lib_installed (&f->base, "support.null-audio-sink"))
117 f->sink_item = load_node (f, "support.null-audio-sink", "Audio/Sink", "device");
118 }
119
120 static void
121 test_si_standard_link_teardown (TestFixture * f, gconstpointer user_data)
122 {
123 g_clear_object (&f->sink_item);
124 g_clear_object (&f->src_item);
125 wp_base_test_fixture_teardown (&f->base);
126 }
127
128 static void
129 test_si_standard_link_main (TestFixture * f, gconstpointer user_data)
130 {
131 g_autoptr (WpSessionItem) link = NULL;
132
133 /* skip the test if audiotestsrc could not be loaded */
134 if (!f->src_item) {
135 g_test_skip ("The pipewire audiotestsrc factory was not found");
136 return;
137 }
138
139 /* skip the test if null-audio-sink could not be loaded */
140 if (!f->sink_item) {
141 g_test_skip ("The pipewire null-audio-sink factory was not found");
142 return;
143 }
144
145 /* create the link */
146 link = wp_session_item_make (f->base.core, "si-standard-link");
147 g_assert_nonnull (link);
148
149 /* configure the link */
150 {
151 g_autoptr (WpProperties) props = wp_properties_new_empty ();
152 wp_properties_setf (props, "out.item", "%p", f->src_item);
153 wp_properties_setf (props, "in.item", "%p", f->sink_item);
154 wp_properties_set (props, "out.item.port.context", "output");
155 wp_properties_set (props, "in.item.port.context", "input");
156 g_assert_true (wp_session_item_configure (link, g_steal_pointer (&props)));
157 }
158
159 /* activate */
160 wp_object_activate (WP_OBJECT (link), WP_SESSION_ITEM_FEATURE_ACTIVE,
161 NULL, (GAsyncReadyCallback) test_object_activate_finish_cb, f);
162 g_main_loop_run (f->base.loop);
163
164 /* verify the graph state */
165 {
166 g_autoptr (WpNode) out_node = NULL;
167 g_autoptr (WpNode) in_node = NULL;
168 g_autoptr (WpIterator) it = NULL;
169 g_auto (GValue) val = G_VALUE_INIT;
170 g_autoptr (WpObjectManager) om = wp_object_manager_new ();
171 guint total_links = 0;
172
173 wp_object_manager_add_interest (om, WP_TYPE_NODE, NULL);
174 wp_object_manager_add_interest (om, WP_TYPE_PORT, NULL);
175 wp_object_manager_add_interest (om, WP_TYPE_LINK, NULL);
176 wp_object_manager_request_object_features (om, WP_TYPE_PROXY,
177 WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL);
178 test_ensure_object_manager_is_installed (om, f->base.core,
179 f->base.loop);
180
181 out_node = wp_object_manager_lookup (om, WP_TYPE_NODE,
182 WP_CONSTRAINT_TYPE_PW_PROPERTY, "node.name", "=s", "audiotestsrc",
183 NULL);
184 g_assert_nonnull (out_node);
185 in_node = wp_object_manager_lookup (om, WP_TYPE_NODE,
186 WP_CONSTRAINT_TYPE_PW_PROPERTY, "node.name", "=s", "support.null-audio-sink",
187 NULL);
188 g_assert_nonnull (in_node);
189 g_assert_cmpuint (wp_object_manager_get_n_objects (om), ==, 8);
190
191 it = wp_object_manager_new_filtered_iterator (om, WP_TYPE_LINK, NULL);
192 for (; wp_iterator_next (it, &val); g_value_unset (&val)) {
193 guint32 out_nd_id, out_pt_id, in_nd_id, in_pt_id;
194 g_autoptr (WpPort) out_port = NULL;
195 g_autoptr (WpPort) in_port = NULL;
196 WpLink *link = g_value_get_object (&val);
197 wp_link_get_linked_object_ids (link, &out_nd_id, &out_pt_id, &in_nd_id,
198 &in_pt_id);
199 g_assert_cmpuint (out_nd_id, ==, wp_proxy_get_bound_id (WP_PROXY (out_node)));
200 g_assert_cmpuint (in_nd_id, ==, wp_proxy_get_bound_id (WP_PROXY (in_node)));
201 out_port = wp_object_manager_lookup (om, WP_TYPE_PORT,
202 WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", "=u", out_pt_id,
203 WP_CONSTRAINT_TYPE_PW_PROPERTY, "node.id", "=u", out_nd_id,
204 NULL);
205 g_assert_nonnull (out_port);
206 in_port = wp_object_manager_lookup (om, WP_TYPE_PORT,
207 WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", "=u", in_pt_id,
208 WP_CONSTRAINT_TYPE_PW_PROPERTY, "node.id", "=u", in_nd_id,
209 NULL);
210 g_assert_nonnull (in_port);
211 total_links++;
212 }
213 g_assert_cmpuint (total_links, ==, 2);
214 }
215
216 /* deactivate */
217 wp_object_deactivate (WP_OBJECT (link), WP_SESSION_ITEM_FEATURE_ACTIVE);
218
219 /* verify the graph state */
220 {
221 g_autoptr (WpNode) out_node = NULL;
222 g_autoptr (WpNode) in_node = NULL;
223 g_autoptr (WpPort) out_port = NULL;
224 g_autoptr (WpPort) in_port = NULL;
225 g_autoptr (WpLink) link = NULL;
226 g_autoptr (WpObjectManager) om = wp_object_manager_new ();
227
228 wp_object_manager_add_interest (om, WP_TYPE_NODE, NULL);
229 wp_object_manager_add_interest (om, WP_TYPE_PORT, NULL);
230 wp_object_manager_add_interest (om, WP_TYPE_LINK, NULL);
231 wp_object_manager_request_object_features (om, WP_TYPE_PROXY,
232 WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL);
233 test_ensure_object_manager_is_installed (om, f->base.core,
234 f->base.loop);
235
236 out_node = wp_object_manager_lookup (om, WP_TYPE_NODE,
237 WP_CONSTRAINT_TYPE_PW_PROPERTY, "node.name", "=s", "audiotestsrc",
238 NULL);
239 g_assert_nonnull (out_node);
240 in_node = wp_object_manager_lookup (om, WP_TYPE_NODE,
241 WP_CONSTRAINT_TYPE_PW_PROPERTY, "node.name", "=s", "support.null-audio-sink",
242 NULL);
243 g_assert_nonnull (in_node);
244 out_port = wp_object_manager_lookup (om, WP_TYPE_PORT,
245 WP_CONSTRAINT_TYPE_PW_PROPERTY, "port.direction", "=s", "out",
246 NULL);
247 g_assert_nonnull (out_port);
248 in_port = wp_object_manager_lookup (om, WP_TYPE_PORT,
249 WP_CONSTRAINT_TYPE_PW_PROPERTY, "port.direction", "=s", "in",
250 NULL);
251 g_assert_nonnull (in_port);
252 link = wp_object_manager_lookup (om, WP_TYPE_LINK, NULL);
253 g_assert_null (link);
254 g_assert_cmpuint (wp_object_manager_get_n_objects (om), ==, 6);
255 }
256 }
257
258 gint
259 main (gint argc, gchar *argv[])
260 {
261 g_test_init (&argc, &argv, NULL);
262 wp_init (WP_INIT_ALL);
263
264 g_test_add ("/modules/si-standard-link/main",
265 TestFixture, NULL,
266 test_si_standard_link_setup,
267 test_si_standard_link_main,
268 test_si_standard_link_teardown);
269
270 return g_test_run ();
271 }