restore-stream: use the new Json API and remove route-settings-api module

This commit is contained in:
Julian Bouzas 2022-01-30 16:03:41 -05:00 committed by George Kiagiadakis
parent e495d4920d
commit b1b603443f
4 changed files with 16 additions and 165 deletions

View file

@ -58,17 +58,6 @@ shared_library(
dependencies : [wp_dep, pipewire_dep],
)
shared_library(
'wireplumber-module-route-settings-api',
[
'module-route-settings-api.c',
],
c_args : [common_c_args, '-DG_LOG_DOMAIN="m-route-settings-api"'],
install : true,
install_dir : wireplumber_module_dir,
dependencies : [wp_dep, pipewire_dep],
)
subdir('module-reserve-device')
shared_library(
'wireplumber-module-reserve-device',

View file

@ -1,137 +0,0 @@
/* WirePlumber
*
* Copyright © 2021 Collabora Ltd.
* @author George Kiagiadakis <george.kiagiadakis@collabora.com>
*
* SPDX-License-Identifier: MIT
*/
#include <wp/wp.h>
#include <pipewire/keys.h>
#include <spa/utils/json.h>
struct _WpRouteSettingsApi
{
WpPlugin parent;
WpImplMetadata *metadata;
};
enum {
ACTION_CONVERT,
N_SIGNALS
};
static guint signals[N_SIGNALS] = {0};
G_DECLARE_FINAL_TYPE (WpRouteSettingsApi, wp_route_settings_api,
WP, ROUTE_SETTINGS_API, WpPlugin)
G_DEFINE_TYPE (WpRouteSettingsApi, wp_route_settings_api, WP_TYPE_PLUGIN)
static void
wp_route_settings_api_init (WpRouteSettingsApi * self)
{
}
static void
on_metadata_activated (GObject * obj, GAsyncResult * res, gpointer user_data)
{
WpTransition * transition = WP_TRANSITION (user_data);
WpRouteSettingsApi * self = wp_transition_get_source_object (transition);
g_autoptr (GError) error = NULL;
if (!wp_object_activate_finish (WP_OBJECT (obj), res, &error)) {
g_clear_object (&self->metadata);
g_prefix_error (&error, "Failed to activate WpImplMetadata: ");
wp_transition_return_error (transition, g_steal_pointer (&error));
return;
}
wp_object_update_features (WP_OBJECT (self), WP_PLUGIN_FEATURE_ENABLED, 0);
}
static void
wp_route_settings_api_enable (WpPlugin * plugin, WpTransition * transition)
{
WpRouteSettingsApi * self = WP_ROUTE_SETTINGS_API (plugin);
g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (plugin));
g_return_if_fail (core);
self->metadata = wp_impl_metadata_new_full (core, "route-settings", NULL);
wp_object_activate (WP_OBJECT (self->metadata),
WP_OBJECT_FEATURES_ALL, NULL, on_metadata_activated, transition);
}
static void
wp_route_settings_api_disable (WpPlugin * plugin)
{
WpRouteSettingsApi * self = WP_ROUTE_SETTINGS_API (plugin);
g_clear_object (&self->metadata);
}
static gchar *
wp_route_settings_api_convert (WpRouteSettingsApi * self,
const gchar * json, const gchar *field)
{
struct spa_json it[3];
char k[128];
spa_json_init(&it[0], json, strlen(json));
if (spa_json_enter_object(&it[0], &it[1]) <= 0)
return NULL;
while (spa_json_get_string(&it[1], k, sizeof(k)) > 0) {
int len;
const char *value;
if (strcmp(k, field) != 0)
continue;
if ((len = spa_json_next(&it[1], &value)) <= 0)
break;
if (spa_json_is_null(value, len))
return NULL;
else if (spa_json_is_array(value, len)) {
GString *str;
spa_json_enter(&it[1], &it[2]);
str = g_string_new("");
while ((len = spa_json_next(&it[2], &value)) > 0) {
char v[1024];
if (spa_json_parse_stringn(value, len, v, sizeof(v)) < 0)
continue;
g_string_append_printf(str, "%s;", v);
}
return g_string_free(str, false);
}
else
return g_strndup(value, len);
}
return NULL;
}
static void
wp_route_settings_api_class_init (WpRouteSettingsApiClass * klass)
{
WpPluginClass *plugin_class = (WpPluginClass *) klass;
plugin_class->enable = wp_route_settings_api_enable;
plugin_class->disable = wp_route_settings_api_disable;
signals[ACTION_CONVERT] = g_signal_new_class_handler (
"convert", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
(GCallback) wp_route_settings_api_convert,
NULL, NULL, NULL,
G_TYPE_STRING, 2, G_TYPE_STRING, G_TYPE_STRING);
}
WP_PLUGIN_EXPORT gboolean
wireplumber__module_init (WpCore * core, GVariant * args, GError ** error)
{
wp_plugin_register (g_object_new (wp_route_settings_api_get_type (),
"name", "route-settings-api",
"core", core,
NULL));
return TRUE;
}

View file

@ -42,9 +42,6 @@ function default_policy.enable()
-- API to access default nodes from scripts
load_module("default-nodes-api")
-- API to access volume of streams from scripts
load_module("route-settings-api")
-- API to access mixer controls, needed for volume ducking
load_module("mixer-api")

View file

@ -12,8 +12,6 @@
state = State("restore-stream")
state_table = state:load()
route_settings = Plugin.find("route-settings-api")
-- simple serializer {"foo", "bar"} -> "foo;bar;"
function serializeArray(a)
local str = ""
@ -325,27 +323,31 @@ function handleRouteSettings(subject, key, type, value)
if string.find(key, "^restore.stream.") == nil then
return
end
if value == nil then
return
end
local json = Json.Raw (value);
if json == nil or not json:is_object () then
return
end
local vparsed = json:parse()
local key_base = string.sub(key, string.len("restore.stream.") + 1)
local str;
key_base = string.gsub(key_base, "%.", ":", 1);
str = route_settings:call("convert", value, "volume");
if str then
state_table[key_base .. ":volume"] = str
if vparsed.volume ~= nil then
state_table[key_base .. ":volume"] = tostring (vparsed.volume)
end
str = route_settings:call("convert", value, "mute");
if str then
state_table[key_base .. ":mute"] = str
if vparsed.mute ~= nil then
state_table[key_base .. ":mute"] = tostring (vparsed.mute)
end
str = route_settings:call("convert", value, "channels");
if str then
state_table[key_base .. ":channelMap"] = str
if vparsed.channels ~= nil then
state_table[key_base .. ":channelMap"] = serializeArray (vparsed.channels)
end
str = route_settings:call("convert", value, "volumes");
if str then
state_table[key_base .. ":channelVolumes"] = str
if vparsed.volumes ~= nil then
state_table[key_base .. ":channelVolumes"] = serializeArray (vparsed.volumes)
end
storeAfterTimeout()