Debian Codegraph @

modules/module-si-node.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 <wp/wp.h>
10 #include <pipewire/keys.h>
11 #include <pipewire/properties.h>
12 #include <pipewire/extensions/session-manager/keys.h>
13
14 WP_DEFINE_LOCAL_LOG_TOPIC ("m-si-node")
15
16 #define SI_FACTORY_NAME "si-node"
17
18 struct _WpSiNode
19 {
20 WpSessionItem parent;
21
22 /* configuration */
23 WpNode *node;
24 };
25
26 static void si_node_linkable_init (WpSiLinkableInterface * iface);
27
28 G_DECLARE_FINAL_TYPE(WpSiNode, si_node, WP, SI_NODE, WpSessionItem)
29 G_DEFINE_TYPE_WITH_CODE (WpSiNode, si_node, WP_TYPE_SESSION_ITEM,
30 G_IMPLEMENT_INTERFACE (WP_TYPE_SI_LINKABLE, si_node_linkable_init))
31
32 static void
33 si_node_init (WpSiNode * self)
34 {
35 }
36
37 static void
38 si_node_soft_reset (WpSiNode * self)
39 {
40 wp_object_deactivate (WP_OBJECT (self), WP_SESSION_ITEM_FEATURE_ACTIVE);
41 }
42
43 static void
44 si_node_reset (WpSessionItem * item)
45 {
46 WpSiNode *self = WP_SI_NODE (item);
47
48 si_node_soft_reset (self);
49
50 /* reset */
51 g_clear_object (&self->node);
52
53 WP_SESSION_ITEM_CLASS (si_node_parent_class)->reset (item);
54 }
55
56 static void
57 on_proxy_destroyed (WpNode * proxy, WpSiNode * self)
58 {
59 if (self->node == proxy) {
60 wp_object_abort_activation (WP_OBJECT (self), "proxy destroyed");
61 si_node_soft_reset (self);
62 }
63 }
64
65 static gboolean
66 si_node_configure (WpSessionItem * item, WpProperties *p)
67 {
68 WpSiNode *self = WP_SI_NODE (item);
69 g_autoptr (WpProperties) si_props = wp_properties_ensure_unique_owner (p);
70 WpNode *node = NULL;
71 const gchar *str;
72
73 /* reset previous config */
74 si_node_reset (item);
75
76 str = wp_properties_get (si_props, "item.node");
77 if (!str || sscanf(str, "%p", &node) != 1 || !WP_IS_NODE (node))
78 return FALSE;
79
80 self->node = g_object_ref (node);
81 g_signal_connect_object (self->node, "pw-proxy-destroyed",
82 G_CALLBACK (on_proxy_destroyed), self, 0);
83
84 wp_properties_set (si_props, "item.factory.name", SI_FACTORY_NAME);
85 wp_session_item_set_properties (WP_SESSION_ITEM (self),
86 g_steal_pointer (&si_props));
87 return TRUE;
88 }
89
90 static gpointer
91 si_node_get_associated_proxy (WpSessionItem * item, GType proxy_type)
92 {
93 WpSiNode *self = WP_SI_NODE (item);
94
95 if (proxy_type == WP_TYPE_NODE)
96 return self->node ? g_object_ref (self->node) : NULL;
97
98 return NULL;
99 }
100
101 static void
102 si_node_disable_active (WpSessionItem *si)
103 {
104 WpSiNode *self = WP_SI_NODE (si);
105
106 wp_object_update_features (WP_OBJECT (self), 0,
107 WP_SESSION_ITEM_FEATURE_ACTIVE);
108 }
109
110 static void
111 on_node_activated (WpObject * node, GAsyncResult * res,
112 WpTransition * transition)
113 {
114 WpSiNode *self = wp_transition_get_source_object (transition);
115 g_autoptr (GError) error = NULL;
116
117 if (!wp_object_activate_finish (node, res, &error)) {
118 wp_transition_return_error (transition, g_steal_pointer (&error));
119 return;
120 }
121
122 wp_object_update_features (WP_OBJECT (self),
123 WP_SESSION_ITEM_FEATURE_ACTIVE, 0);
124 }
125
126 static void
127 si_node_enable_active (WpSessionItem *si, WpTransition *transition)
128 {
129 WpSiNode *self = WP_SI_NODE (si);
130
131 if (!wp_session_item_is_configured (si)) {
132 wp_transition_return_error (transition,
133 g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
134 "si-node: item is not configured"));
135 return;
136 }
137
138 wp_object_activate (WP_OBJECT (self->node),
139 WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL | WP_NODE_FEATURE_PORTS,
140 NULL, (GAsyncReadyCallback) on_node_activated, transition);
141 }
142
143 static WpObjectFeatures
144 si_node_get_supported_features (WpObject * self)
145 {
146 return WP_SESSION_ITEM_FEATURE_ACTIVE;
147 }
148
149 static void
150 si_node_class_init (WpSiNodeClass * klass)
151 {
152 WpObjectClass * wpobject_class = (WpObjectClass *) klass;
153 WpSessionItemClass *si_class = (WpSessionItemClass *) klass;
154
155 wpobject_class->get_supported_features = si_node_get_supported_features;
156
157 si_class->reset = si_node_reset;
158 si_class->configure = si_node_configure;
159 si_class->get_associated_proxy = si_node_get_associated_proxy;
160 si_class->disable_active = si_node_disable_active;
161 si_class->enable_active = si_node_enable_active;
162 }
163
164 static GVariant *
165 si_node_get_ports (WpSiLinkable * item, const gchar * context)
166 {
167 WpSiNode *self = WP_SI_NODE (item);
168 g_auto (GVariantBuilder) b = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_ARRAY);
169 g_autoptr (WpIterator) it = NULL;
170 g_auto (GValue) val = G_VALUE_INIT;
171 WpDirection direction;
172 guint32 node_id;
173
174 if (!g_strcmp0 (context, "output")) {
175 direction = WP_DIRECTION_OUTPUT;
176 }
177 else if (!g_strcmp0 (context, "input")) {
178 direction = WP_DIRECTION_INPUT;
179 }
180 else {
181 /* on any other context, return an empty list of ports */
182 return g_variant_new_array (G_VARIANT_TYPE ("(uuu)"), NULL, 0);
183 }
184
185 g_variant_builder_init (&b, G_VARIANT_TYPE ("a(uuu)"));
186 node_id = wp_proxy_get_bound_id (WP_PROXY (self->node));
187
188 for (it = wp_node_new_ports_iterator (self->node);
189 wp_iterator_next (it, &val);
190 g_value_unset (&val))
191 {
192 WpPort *port = g_value_get_object (&val);
193 g_autoptr (WpProperties) props = NULL;
194 const gchar *channel;
195 guint32 port_id, channel_id = 0;
196
197 if (wp_port_get_direction (port) != direction)
198 continue;
199
200 port_id = wp_proxy_get_bound_id (WP_PROXY (port));
201 props = wp_pipewire_object_get_properties (WP_PIPEWIRE_OBJECT (port));
202
203 /* skip control ports for now */
204 if (spa_atob (wp_properties_get (props, PW_KEY_PORT_CONTROL)))
205 continue;
206
207 /* try to find the audio channel; if channel is NULL, this will silently
208 leave the channel_id to its default value, 0 */
209 channel = wp_properties_get (props, PW_KEY_AUDIO_CHANNEL);
210 if (channel) {
211 WpSpaIdValue idval = wp_spa_id_value_from_short_name (
212 "Spa:Enum:AudioChannel", channel);
213 if (idval)
214 channel_id = wp_spa_id_value_number (idval);
215 }
216
217 g_variant_builder_add (&b, "(uuu)", node_id, port_id, channel_id);
218 }
219
220 return g_variant_builder_end (&b);
221 }
222
223 static void
224 si_node_linkable_init (WpSiLinkableInterface * iface)
225 {
226 iface->get_ports = si_node_get_ports;
227 }
228
229 WP_PLUGIN_EXPORT GObject *
230 wireplumber__module_init (WpCore * core, WpSpaJson * args, GError ** error)
231 {
232 return G_OBJECT (wp_si_factory_new_simple (SI_FACTORY_NAME,
233 si_node_get_type ()));
234 }