| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2024 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
* |
| 8 |
* This is an example that shows how to correctly set additional node properties |
| 9 |
* that reside in a special "Props" field that is called "params". These show up |
| 10 |
* in pw-dump as an array, like this: |
| 11 |
* |
| 12 |
* "Props": [ |
| 13 |
* { |
| 14 |
* ... |
| 15 |
* "params": [ |
| 16 |
* "key1", |
| 17 |
* value1, |
| 18 |
* "key2", |
| 19 |
* value2, |
| 20 |
* ... |
| 21 |
* ] |
| 22 |
* }, |
| 23 |
* { |
| 24 |
* "params": [ |
| 25 |
* "additional_key", |
| 26 |
* additional_value, |
| 27 |
* ] |
| 28 |
* } |
| 29 |
* ], |
| 30 |
* |
| 31 |
* The correct way to set them is to construct a Props object that has a "params" |
| 32 |
* property and inside that property add a POD structure with the key/value |
| 33 |
* pairs listed in order. |
| 34 |
* |
| 35 |
* This example also parses the key/value pairs from a JSON array that is provided |
| 36 |
* on the command line, so you can call this like this: |
| 37 |
* |
| 38 |
* $ ./filter-chain-params NODE_ID '["key1", value1, "key2", value2]' |
| 39 |
* |
| 40 |
*/ |
| 41 |
|
| 42 |
#include <wp/wp.h> |
| 43 |
#include <stdio.h> |
| 44 |
|
| 45 |
typedef struct |
| 46 |
{ |
| 47 |
GMainLoop *loop; |
| 48 |
WpCore *core; |
| 49 |
WpObjectManager *om; |
| 50 |
|
| 51 |
gint arg_id; |
| 52 |
WpSpaJson *arg_params; |
| 53 |
} Data; |
| 54 |
|
| 55 |
static void |
| 56 |
async_quit (WpCore * core, GAsyncResult * res, Data * data) |
| 57 |
{ |
| 58 |
g_main_loop_quit (data->loop); |
| 59 |
} |
| 60 |
|
| 61 |
static WpSpaPod * |
| 62 |
construct_params_pod (WpSpaJson * params_j) |
| 63 |
{ |
| 64 |
// the inner POD is a struct |
| 65 |
g_autoptr (WpSpaPodBuilder) b_struct = wp_spa_pod_builder_new_struct (); |
| 66 |
|
| 67 |
// iterate the JSON array and fill the inner POD |
| 68 |
int state = 0; |
| 69 |
g_autoptr (WpIterator) it = wp_spa_json_new_iterator (params_j); |
| 70 |
g_auto (GValue) val = G_VALUE_INIT; |
| 71 |
|
| 72 |
for (; wp_iterator_next (it, &val); g_value_unset (&val)) { |
| 73 |
WpSpaJson *val_json = g_value_get_boxed (&val); |
| 74 |
switch (state) { |
| 75 |
case 0: { //parsing key |
| 76 |
g_autofree gchar * key = wp_spa_json_parse_string (val_json); |
| 77 |
wp_spa_pod_builder_add_string (b_struct, key); |
| 78 |
break; |
| 79 |
} |
| 80 |
case 1: //parsing value |
| 81 |
if (wp_spa_json_is_int (val_json)) { |
| 82 |
gint value = 0; |
| 83 |
wp_spa_json_parse_int (val_json, &value); |
| 84 |
wp_spa_pod_builder_add_int (b_struct, value); |
| 85 |
} |
| 86 |
else if (wp_spa_json_is_float (val_json)) { |
| 87 |
gfloat value = 0.0; |
| 88 |
wp_spa_json_parse_float (val_json, &value); |
| 89 |
wp_spa_pod_builder_add_float (b_struct, value); |
| 90 |
} |
| 91 |
else if (wp_spa_json_is_boolean (val_json)) { |
| 92 |
gboolean value = FALSE; |
| 93 |
wp_spa_json_parse_boolean (val_json, &value); |
| 94 |
wp_spa_pod_builder_add_boolean (b_struct, value); |
| 95 |
} |
| 96 |
else { |
| 97 |
g_autofree gchar * value = wp_spa_json_parse_string (val_json); |
| 98 |
wp_spa_pod_builder_add_string (b_struct, value); |
| 99 |
} |
| 100 |
break; |
| 101 |
default: |
| 102 |
break; |
| 103 |
} |
| 104 |
state = (state + 1) % 2; |
| 105 |
} |
| 106 |
|
| 107 |
if (state == 1) |
| 108 |
printf ("WARNING: last key didn't have a value!\n"); |
| 109 |
|
| 110 |
g_autoptr (WpSpaPod) pod_struct = wp_spa_pod_builder_end (b_struct); |
| 111 |
|
| 112 |
// now fill the outer POD, which is an object of type Props |
| 113 |
g_autoptr (WpSpaPodBuilder) b_obj = |
| 114 |
wp_spa_pod_builder_new_object ("Spa:Pod:Object:Param:Props", "Props"); |
| 115 |
wp_spa_pod_builder_add (b_obj, "params", "P", pod_struct, NULL); |
| 116 |
|
| 117 |
return wp_spa_pod_builder_end (b_obj); |
| 118 |
} |
| 119 |
|
| 120 |
static void |
| 121 |
on_om_installed (WpObjectManager * om, Data * data) |
| 122 |
{ |
| 123 |
g_autoptr (WpPipewireObject) pwobj = wp_object_manager_lookup (om, WP_TYPE_NODE, |
| 124 |
WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", "=u", data->arg_id, NULL); |
| 125 |
|
| 126 |
wp_pipewire_object_set_param (pwobj, "Props", 0, |
| 127 |
construct_params_pod (data->arg_params)); |
| 128 |
|
| 129 |
wp_core_sync (data->core, NULL, (GAsyncReadyCallback) async_quit, data); |
| 130 |
} |
| 131 |
|
| 132 |
static void |
| 133 |
on_core_activated (WpObject * core, GAsyncResult * res, Data * data) |
| 134 |
{ |
| 135 |
g_autoptr (GError) error = NULL; |
| 136 |
|
| 137 |
if (!wp_object_activate_finish (core, res, &error)) { |
| 138 |
fprintf (stderr, "%s\n", error->message); |
| 139 |
g_main_loop_quit (data->loop); |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
data->om = wp_object_manager_new (); |
| 144 |
wp_object_manager_add_interest (data->om, WP_TYPE_NODE, |
| 145 |
WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", "=u", data->arg_id, NULL); |
| 146 |
wp_object_manager_request_object_features (data->om, WP_TYPE_NODE, |
| 147 |
WP_PIPEWIRE_OBJECT_FEATURES_ALL); |
| 148 |
g_signal_connect (data->om, "installed", G_CALLBACK (on_om_installed), data); |
| 149 |
wp_core_install_object_manager (data->core, data->om); |
| 150 |
} |
| 151 |
|
| 152 |
int |
| 153 |
main (int argc, char **argv) |
| 154 |
{ |
| 155 |
Data data = {0}; |
| 156 |
|
| 157 |
wp_init (WP_INIT_ALL); |
| 158 |
|
| 159 |
if (argc < 2) { |
| 160 |
printf ("Usage: %s ID '[param1, value1, param2, value2, ...]'\n", argv[0]); |
| 161 |
return 1; |
| 162 |
} |
| 163 |
|
| 164 |
data.loop = g_main_loop_new (NULL, FALSE); |
| 165 |
data.core = wp_core_new (NULL, NULL, NULL); |
| 166 |
|
| 167 |
data.arg_id = atoi (argv[1]); |
| 168 |
data.arg_params = wp_spa_json_new_wrap_string (argv[2]); |
| 169 |
|
| 170 |
wp_object_activate (WP_OBJECT (data.core), WP_CORE_FEATURE_CONNECTED, NULL, |
| 171 |
(GAsyncReadyCallback) on_core_activated, &data); |
| 172 |
|
| 173 |
g_main_loop_run (data.loop); |
| 174 |
|
| 175 |
wp_spa_json_unref (data.arg_params); |
| 176 |
g_object_unref (data.core); |
| 177 |
g_main_loop_unref (data.loop); |
| 178 |
return 0; |
| 179 |
} |