Debian Codegraph @

lib/wp/link.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2019-2020 Collabora Ltd.
4 * @author Julian Bouzas <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "link.h"
10 #include "log.h"
11 #include "wpenums.h"
12 #include "private/pipewire-object-mixin.h"
13
14 WP_DEFINE_LOCAL_LOG_TOPIC ("wp-link")
15
16 /*! \defgroup wplink WpLink */
17 /*!
18 * \struct WpLink
19 *
20 * The WpLink class allows accessing the properties and methods of a
21 * PipeWire link object (`struct pw_link`).
22 *
23 * A WpLink is constructed internally when a new link appears on the
24 * PipeWire registry and it is made available through the WpObjectManager API.
25 * Alternatively, a WpLink can also be constructed using
26 * wp_link_new_from_factory(), which creates a new link object
27 * on the remote PipeWire server by calling into a factory.
28 *
29 * \gproperties
30 *
31 * \gproperty{state, WpLinkState, G_PARAM_READABLE, The current state of the link}
32 *
33 * \gsignals
34 *
35 * \par state-changed
36 * \parblock
37 * \code
38 * void
39 * state_changed_callback (WpLink * self,
40 * WpLinkState old_state,
41 * WpLinkState new_state,
42 * gpointer user_data)
43 * \endcode
44 *
45 * Emitted when the link changes state. This is only emitted when
46 * WP_PIPEWIRE_OBJECT_FEATURE_INFO is enabled.
47 *
48 * Parameters:
49 * - `old_state` - the old state
50 * - `new_state` - the new state
51 *
52 * Flags: G_SIGNAL_RUN_LAST
53 * \endparblock
54 */
55
56 enum {
57 PROP_STATE = WP_PW_OBJECT_MIXIN_PROP_CUSTOM_START,
58 };
59
60 enum {
61 SIGNAL_STATE_CHANGED,
62 N_SIGNALS,
63 };
64
65 static guint32 signals[N_SIGNALS] = {0};
66
67 struct _WpLink
68 {
69 WpGlobalProxy parent;
70 };
71
72 static void wp_link_pw_object_mixin_priv_interface_init (
73 WpPwObjectMixinPrivInterface * iface);
74
75 G_DEFINE_TYPE_WITH_CODE (WpLink, wp_link, WP_TYPE_GLOBAL_PROXY,
76 G_IMPLEMENT_INTERFACE (WP_TYPE_PIPEWIRE_OBJECT,
77 wp_pw_object_mixin_object_interface_init)
78 G_IMPLEMENT_INTERFACE (WP_TYPE_PW_OBJECT_MIXIN_PRIV,
79 wp_link_pw_object_mixin_priv_interface_init))
80
81 static void
82 wp_link_init (WpLink * self)
83 {
84 }
85
86 static void
87 wp_link_get_property (GObject * object, guint property_id,
88 GValue * value, GParamSpec * pspec)
89 {
90 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (object);
91
92 switch (property_id) {
93 case PROP_STATE:
94 g_value_set_enum (value, d->info ?
95 ((struct pw_link_info *) d->info)->state : 0);
96 break;
97 default:
98 wp_pw_object_mixin_get_property (object, property_id, value, pspec);
99 break;
100 }
101 }
102
103 static void
104 wp_link_activate_execute_step (WpObject * object,
105 WpFeatureActivationTransition * transition, guint step,
106 WpObjectFeatures missing)
107 {
108 switch (step) {
109 case WP_PW_OBJECT_MIXIN_STEP_BIND:
110 case WP_TRANSITION_STEP_ERROR:
111 /* base class can handle BIND and ERROR */
112 WP_OBJECT_CLASS (wp_link_parent_class)->
113 activate_execute_step (object, transition, step, missing);
114 break;
115 case WP_PW_OBJECT_MIXIN_STEP_WAIT_INFO:
116 /* just wait, info will be emitted anyway after binding */
117 break;
118 default:
119 g_assert_not_reached ();
120 }
121 }
122
123 static const struct pw_link_events link_events = {
124 PW_VERSION_LINK_EVENTS,
125 .info = (HandleEventInfoFunc(link)) wp_pw_object_mixin_handle_event_info,
126 };
127
128 static void
129 wp_link_pw_proxy_created (WpProxy * proxy, struct pw_proxy * pw_proxy)
130 {
131 wp_pw_object_mixin_handle_pw_proxy_created (proxy, pw_proxy,
132 link, &link_events);
133 }
134
135 static void
136 wp_link_pw_proxy_destroyed (WpProxy * proxy)
137 {
138 wp_pw_object_mixin_handle_pw_proxy_destroyed (proxy);
139
140 WP_PROXY_CLASS (wp_link_parent_class)->pw_proxy_destroyed (proxy);
141 }
142
143 static void
144 wp_link_class_init (WpLinkClass * klass)
145 {
146 GObjectClass *object_class = (GObjectClass *) klass;
147 WpObjectClass *wpobject_class = (WpObjectClass *) klass;
148 WpProxyClass *proxy_class = (WpProxyClass *) klass;
149
150 object_class->get_property = wp_link_get_property;
151
152 wpobject_class->get_supported_features =
153 wp_pw_object_mixin_get_supported_features;
154 wpobject_class->activate_get_next_step =
155 wp_pw_object_mixin_activate_get_next_step;
156 wpobject_class->activate_execute_step = wp_link_activate_execute_step;
157
158 proxy_class->pw_iface_type = PW_TYPE_INTERFACE_Link;
159 proxy_class->pw_iface_version = PW_VERSION_LINK;
160 proxy_class->pw_proxy_created = wp_link_pw_proxy_created;
161 proxy_class->pw_proxy_destroyed = wp_link_pw_proxy_destroyed;
162
163 wp_pw_object_mixin_class_override_properties (object_class);
164
165 g_object_class_install_property (object_class, PROP_STATE,
166 g_param_spec_enum ("state", "state", "state", WP_TYPE_LINK_STATE, 0,
167 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
168
169 signals[SIGNAL_STATE_CHANGED] = g_signal_new (
170 "state-changed", G_TYPE_FROM_CLASS (klass),
171 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 2,
172 WP_TYPE_LINK_STATE, WP_TYPE_LINK_STATE);
173
174 }
175
176 static void
177 wp_link_process_info (gpointer instance, gpointer old_info, gpointer i)
178 {
179 const struct pw_link_info *info = i;
180
181 if (info->change_mask & PW_LINK_CHANGE_MASK_STATE) {
182 enum pw_link_state old_state = old_info ?
183 ((struct pw_link_info *) old_info)->state : PW_LINK_STATE_INIT;
184 g_signal_emit (instance, signals[SIGNAL_STATE_CHANGED], 0,
185 old_state, info->state);
186 }
187 }
188
189 static void
190 wp_link_pw_object_mixin_priv_interface_init (
191 WpPwObjectMixinPrivInterface * iface)
192 {
193 wp_pw_object_mixin_priv_interface_info_init_no_params (iface, link, LINK);
194 iface->process_info = wp_link_process_info;
195 }
196
197 /*!
198 * \brief Constructs a link on the PipeWire server by asking the remote factory
199 * \a factory_name to create it.
200 *
201 * Because of the nature of the PipeWire protocol, this operation completes
202 * asynchronously at some point in the future. In order to find out when
203 * this is done, you should call wp_object_activate(), requesting at least
204 * WP_PROXY_FEATURE_BOUND. When this feature is ready, the link is ready for
205 * use on the server. If the link cannot be created, this activation operation
206 * will fail.
207 *
208 * \ingroup wplink
209 * \param core the wireplumber core
210 * \param factory_name the pipewire factory name to construct the link
211 * \param properties (nullable) (transfer full): the properties to pass to the
212 * factory
213 * \returns (nullable) (transfer full): the new link or NULL if the core
214 * is not connected and therefore the link cannot be created
215 */
216 WpLink *
217 wp_link_new_from_factory (WpCore * core,
218 const gchar * factory_name, WpProperties * properties)
219 {
220 g_autoptr (WpProperties) props = properties;
221 return g_object_new (WP_TYPE_LINK,
222 "core", core,
223 "factory-name", factory_name,
224 "global-properties", props,
225 NULL);
226 }
227
228 /*!
229 * \brief Retrieves the ids of the objects that are linked by this link
230 *
231 * \remark Requires WP_PIPEWIRE_OBJECT_FEATURE_INFO
232 *
233 * \ingroup wplink
234 * \param self the link
235 * \param output_node (out) (optional): the bound id of the output (source) node
236 * \param output_port (out) (optional): the bound id of the output (source) port
237 * \param input_node (out) (optional): the bound id of the input (sink) node
238 * \param input_port (out) (optional): the bound id of the input (sink) port
239 */
240 void
241 wp_link_get_linked_object_ids (WpLink * self,
242 guint32 * output_node, guint32 * output_port,
243 guint32 * input_node, guint32 * input_port)
244 {
245 g_return_if_fail (WP_IS_LINK (self));
246
247 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (self);
248 struct pw_link_info *info = d->info;
249 g_return_if_fail (info);
250
251 if (output_node)
252 *output_node = info->output_node_id;
253 if (output_port)
254 *output_port = info->output_port_id;
255 if (input_node)
256 *input_node = info->input_node_id;
257 if (input_port)
258 *input_port = info->input_port_id;
259 }
260
261 /*!
262 * \brief Gets the current state of the link
263 * \ingroup wplink
264 * \param self the link
265 * \param error (out) (optional) (transfer none): the error
266 * \returns the current state of the link
267 * \since 0.4.11
268 */
269 WpLinkState
270 wp_link_get_state (WpLink * self, const gchar ** error)
271 {
272 g_return_val_if_fail (WP_IS_LINK (self), WP_LINK_STATE_ERROR);
273 g_return_val_if_fail (wp_object_test_active_features (WP_OBJECT (self),
274 WP_PIPEWIRE_OBJECT_FEATURE_INFO), WP_LINK_STATE_ERROR);
275
276 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (self);
277 const struct pw_link_info *info = d->info;
278
279 if (error)
280 *error = info->error;
281 return (WpLinkState) info->state;
282 }