| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2021 Asymptotic |
| 4 |
* @author Arun Raghavan <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "module.h" |
| 10 |
#include "log.h" |
| 11 |
|
| 12 |
#include <pipewire/impl.h> |
| 13 |
|
| 14 |
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-module") |
| 15 |
|
| 16 |
/*! \defgroup wpimplmodule WpImplModule */ |
| 17 |
/*! |
| 18 |
* \struct WpImplModule |
| 19 |
* \since 0.4.2 |
| 20 |
* |
| 21 |
* Used to load PipeWire modules within the WirePlumber process. This is |
| 22 |
* slightly different from other objects in that the module is not exported to |
| 23 |
* PipeWire, but it may create an export objects itself. |
| 24 |
*/ |
| 25 |
|
| 26 |
struct _WpImplModule |
| 27 |
{ |
| 28 |
GObject parent; |
| 29 |
|
| 30 |
GWeakRef core; |
| 31 |
gchar *name; |
| 32 |
gchar *args; |
| 33 |
WpProperties *props; /* only used during module load */ |
| 34 |
|
| 35 |
struct pw_impl_module *pw_impl_module; |
| 36 |
struct spa_hook impl_module_listener; |
| 37 |
}; |
| 38 |
|
| 39 |
G_DEFINE_TYPE (WpImplModule, wp_impl_module, G_TYPE_OBJECT); |
| 40 |
|
| 41 |
enum { |
| 42 |
PROP_0, |
| 43 |
PROP_CORE, |
| 44 |
PROP_NAME, |
| 45 |
PROP_ARGUMENTS, |
| 46 |
PROP_PROPERTIES, |
| 47 |
PROP_PW_IMPL_MODULE, |
| 48 |
}; |
| 49 |
|
| 50 |
static void impl_module_free (void *data) |
| 51 |
{ |
| 52 |
WpImplModule *self = WP_IMPL_MODULE (data); |
| 53 |
self->pw_impl_module = NULL; |
| 54 |
} |
| 55 |
|
| 56 |
static const struct pw_impl_module_events impl_module_events = { |
| 57 |
PW_VERSION_IMPL_MODULE_EVENTS, |
| 58 |
.free = impl_module_free, |
| 59 |
}; |
| 60 |
|
| 61 |
static void |
| 62 |
wp_impl_module_init (WpImplModule * self) |
| 63 |
{ |
| 64 |
g_weak_ref_init (&self->core, NULL); |
| 65 |
self->name = NULL; |
| 66 |
self->args = NULL; |
| 67 |
self->props = NULL; |
| 68 |
self->pw_impl_module = NULL; |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
wp_impl_module_constructed (GObject * object) |
| 73 |
{ |
| 74 |
WpImplModule *self = WP_IMPL_MODULE (object); |
| 75 |
WpCore *core = g_weak_ref_get (&self->core); |
| 76 |
struct pw_context *context = core ? wp_core_get_pw_context (core) : NULL; |
| 77 |
struct pw_properties *props = NULL; |
| 78 |
|
| 79 |
if (!core || !context) { |
| 80 |
g_warning ("Tried to load module on unconnected core"); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
if (!self->name) { |
| 85 |
g_warning ("Invalid name while loading warnings"); |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
if (self->props) |
| 90 |
props = wp_properties_to_pw_properties (self->props); |
| 91 |
|
| 92 |
self->pw_impl_module = |
| 93 |
pw_context_load_module (context, self->name, self->args, props); |
| 94 |
|
| 95 |
if (self->pw_impl_module) { |
| 96 |
if (self->props) { |
| 97 |
/* With the module loaded, properties are just passthrough now */ |
| 98 |
wp_properties_unref (self->props); |
| 99 |
self->props = NULL; |
| 100 |
} |
| 101 |
|
| 102 |
pw_impl_module_add_listener (self->pw_impl_module, |
| 103 |
&self->impl_module_listener, &impl_module_events, self); |
| 104 |
} |
| 105 |
|
| 106 |
G_OBJECT_CLASS (wp_impl_module_parent_class)->constructed (object); |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
wp_impl_module_finalize (GObject * object) |
| 111 |
{ |
| 112 |
WpImplModule *self = WP_IMPL_MODULE (object); |
| 113 |
|
| 114 |
g_weak_ref_clear (&self->core); |
| 115 |
|
| 116 |
if (self->pw_impl_module) |
| 117 |
pw_impl_module_destroy (self->pw_impl_module); |
| 118 |
|
| 119 |
g_free (self->name); |
| 120 |
g_free (self->args); |
| 121 |
|
| 122 |
if (self->props) |
| 123 |
wp_properties_unref (self->props); |
| 124 |
} |
| 125 |
|
| 126 |
static void |
| 127 |
wp_impl_module_get_property (GObject * object, guint prop_id, |
| 128 |
GValue * value, GParamSpec * pspec) |
| 129 |
{ |
| 130 |
WpImplModule *self = WP_IMPL_MODULE (object); |
| 131 |
|
| 132 |
switch (prop_id) { |
| 133 |
case PROP_CORE: |
| 134 |
g_value_set_pointer (value, g_weak_ref_get (&self->core)); |
| 135 |
break; |
| 136 |
|
| 137 |
case PROP_NAME: |
| 138 |
g_value_set_string (value, self->name); |
| 139 |
break; |
| 140 |
|
| 141 |
case PROP_ARGUMENTS: |
| 142 |
g_value_set_string (value, self->args); |
| 143 |
break; |
| 144 |
|
| 145 |
case PROP_PROPERTIES: |
| 146 |
if (self->pw_impl_module) { |
| 147 |
const struct pw_properties *props = |
| 148 |
pw_impl_module_get_properties (self->pw_impl_module); |
| 149 |
|
| 150 |
/* Should we just wrap instead of copying? */ |
| 151 |
if (props) |
| 152 |
g_value_set_boxed (value, wp_properties_new_copy (props)); |
| 153 |
else |
| 154 |
g_value_set_boxed (value, NULL); |
| 155 |
} else { |
| 156 |
g_value_set_boxed (value, self->props); |
| 157 |
} |
| 158 |
break; |
| 159 |
|
| 160 |
case PROP_PW_IMPL_MODULE: |
| 161 |
g_value_set_pointer (value, self->pw_impl_module); |
| 162 |
break; |
| 163 |
|
| 164 |
default: |
| 165 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 166 |
break; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
static void |
| 171 |
wp_impl_module_set_property (GObject * object, guint prop_id, |
| 172 |
const GValue * value, GParamSpec * pspec) |
| 173 |
{ |
| 174 |
WpImplModule *self = WP_IMPL_MODULE (object); |
| 175 |
WpProperties *props; |
| 176 |
|
| 177 |
switch (prop_id) { |
| 178 |
case PROP_CORE: |
| 179 |
g_weak_ref_set (&self->core, g_value_get_pointer (value)); |
| 180 |
break; |
| 181 |
|
| 182 |
case PROP_NAME: |
| 183 |
g_free (self->name); |
| 184 |
self->name = g_value_dup_string (value); |
| 185 |
break; |
| 186 |
|
| 187 |
case PROP_ARGUMENTS: |
| 188 |
g_free (self->args); |
| 189 |
self->args = g_value_dup_string (value); |
| 190 |
break; |
| 191 |
|
| 192 |
case PROP_PROPERTIES: |
| 193 |
props = g_value_get_boxed (value); |
| 194 |
|
| 195 |
if (props && self->pw_impl_module) { |
| 196 |
pw_impl_module_update_properties (self->pw_impl_module, |
| 197 |
wp_properties_peek_dict (props)); |
| 198 |
} else { |
| 199 |
if (props) |
| 200 |
self->props = wp_properties_ref (props); |
| 201 |
else |
| 202 |
self->props = NULL; |
| 203 |
} |
| 204 |
break; |
| 205 |
|
| 206 |
default: |
| 207 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
static void |
| 213 |
wp_impl_module_class_init (WpImplModuleClass * klass) |
| 214 |
{ |
| 215 |
GObjectClass *gobject_class = (GObjectClass *) klass; |
| 216 |
|
| 217 |
gobject_class->constructed = wp_impl_module_constructed; |
| 218 |
gobject_class->finalize = wp_impl_module_finalize; |
| 219 |
gobject_class->get_property = wp_impl_module_get_property; |
| 220 |
gobject_class->set_property = wp_impl_module_set_property; |
| 221 |
|
| 222 |
g_object_class_install_property (gobject_class, PROP_CORE, |
| 223 |
g_param_spec_pointer ("core", "Core", "The WirePlumber core", |
| 224 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
| 225 |
|
| 226 |
g_object_class_install_property (gobject_class, PROP_NAME, |
| 227 |
g_param_spec_string ("name", "Name", "The name of the PipeWire module", |
| 228 |
NULL, |
| 229 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
| 230 |
|
| 231 |
g_object_class_install_property (gobject_class, PROP_ARGUMENTS, |
| 232 |
g_param_spec_string ("arguments", "Arguments", |
| 233 |
"The arguments to provide to the module while loading", NULL, |
| 234 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
| 235 |
|
| 236 |
g_object_class_install_property (gobject_class, PROP_PROPERTIES, |
| 237 |
g_param_spec_boxed ("properties", "Properties", |
| 238 |
"Properties of the module", WP_TYPE_PROPERTIES, |
| 239 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 240 |
|
| 241 |
g_object_class_install_property (gobject_class, PROP_PW_IMPL_MODULE, |
| 242 |
g_param_spec_pointer ("pw-impl-module", "Underlying pw_impl_module", |
| 243 |
"Pointer to the underlying pw_impl_module structure for the module", |
| 244 |
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 245 |
} |
| 246 |
|
| 247 |
/*! |
| 248 |
* \brief Loads a PipeWire module into the WirePlumber process |
| 249 |
* |
| 250 |
* \ingroup wpimplmodule |
| 251 |
* \since 0.4.2 |
| 252 |
* \param core (transfer none): The WirePlumber core |
| 253 |
* \param name (transfer none): the name of the module to load |
| 254 |
* \param arguments (nullable) (transfer none): arguments to be passed to the module |
| 255 |
* \param properties (nullable) (transfer none): additional properties to be |
| 256 |
* provided to the module |
| 257 |
* \returns (nullable) (transfer full): the WpImplModule for the module that |
| 258 |
* was loaded on success, %NULL on failure. |
| 259 |
*/ |
| 260 |
WpImplModule * |
| 261 |
wp_impl_module_load (WpCore * core, const gchar * name, |
| 262 |
const gchar * arguments, WpProperties * properties) |
| 263 |
{ |
| 264 |
WpImplModule *module = WP_IMPL_MODULE ( |
| 265 |
g_object_new (WP_TYPE_IMPL_MODULE, |
| 266 |
"core", core, |
| 267 |
"name", name, |
| 268 |
"arguments", arguments, |
| 269 |
"properties", properties, |
| 270 |
NULL) |
| 271 |
); |
| 272 |
|
| 273 |
if (!module->pw_impl_module) { |
| 274 |
/* Module loading failed, free and return */ |
| 275 |
g_object_unref (module); |
| 276 |
return NULL; |
| 277 |
} |
| 278 |
|
| 279 |
return module; |
| 280 |
} |