Merge remote-tracking branch 'origin/master' into next

This commit is contained in:
George Kiagiadakis
2020-11-26 17:47:49 +02:00
13 changed files with 977 additions and 59 deletions

View File

@@ -30,6 +30,7 @@ wp_lib_sources = files(
'si-interfaces.c',
'spa-pod.c',
'spa-type.c',
'state.c',
'transition.c',
'wp.c',
)
@@ -66,6 +67,7 @@ wp_lib_headers = files(
'si-interfaces.h',
'spa-pod.h',
'spa-type.h',
'state.h',
'transition.h',
'wp.h',
)

320
lib/wp/state.c Normal file
View File

@@ -0,0 +1,320 @@
/* WirePlumber
*
* Copyright © 2020 Collabora Ltd.
* @author Julian Bouzas <julian.bouzas@collabora.com>
*
* SPDX-License-Identifier: MIT
*/
/**
* SECTION: WpState
*
* The #WpState class saves and loads properties from a file
*/
#define G_LOG_DOMAIN "wp-state"
#define WP_STATE_DIR_NAME "wireplumber"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "debug.h"
#include "state.h"
enum {
PROP_0,
PROP_NAME,
};
struct _WpState
{
GObject parent;
/* Props */
gchar *name;
gchar *location;
};
G_DEFINE_TYPE (WpState, wp_state, G_TYPE_OBJECT)
static gboolean
path_exists (const char *path)
{
struct stat info;
return stat (path, &info) == 0;
}
static char *
get_new_location (const char *name)
{
g_autofree gchar *path = NULL;
/* Get the config path */
path = g_build_filename (g_get_user_config_dir (), WP_STATE_DIR_NAME, NULL);
g_return_val_if_fail (path, NULL);
/* Create the directory if it doesn't exist */
if (!path_exists (path))
g_mkdir_with_parents (path, 0700);
return g_build_filename (path, name, NULL);
}
static void
wp_state_ensure_location (WpState *self)
{
if (!self->location)
self->location = get_new_location (self->name);
g_return_if_fail (self->location);
}
static void
wp_state_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec)
{
WpState *self = WP_STATE (object);
switch (property_id) {
case PROP_NAME:
g_clear_pointer (&self->name, g_free);
self->name = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
wp_state_get_property (GObject * object, guint property_id, GValue * value,
GParamSpec * pspec)
{
WpState *self = WP_STATE (object);
switch (property_id) {
case PROP_NAME:
g_value_set_string (value, self->name);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
wp_state_finalize (GObject * object)
{
WpState * self = WP_STATE (object);
g_clear_pointer (&self->name, g_free);
g_clear_pointer (&self->location, g_free);
G_OBJECT_CLASS (wp_state_parent_class)->finalize (object);
}
static void
wp_state_init (WpState * self)
{
}
static void
wp_state_class_init (WpStateClass * klass)
{
GObjectClass *object_class = (GObjectClass *) klass;
object_class->finalize = wp_state_finalize;
object_class->set_property = wp_state_set_property;
object_class->get_property = wp_state_get_property;
/**
* WpState:name:
* The file name where the state will be stored.
*/
g_object_class_install_property (object_class, PROP_NAME,
g_param_spec_string ("name", "name",
"The file name where the state will be stored", NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
}
/**
* wp_state_new:
* @name: the state name
*
* Returns: (transfer full): the new #WpState
*/
WpState *
wp_state_new (const gchar *name)
{
g_return_val_if_fail (name, NULL);
return g_object_new (wp_state_get_type (),
"name", name,
NULL);
}
/**
* wp_state_get_name:
* @self: the state
*
* Returns: the name of this state
*/
const gchar *
wp_state_get_name (WpState *self)
{
g_return_val_if_fail (WP_IS_STATE (self), NULL);
return self->name;
}
/**
* wp_state_get_location:
* @self: the state
*
* Returns: the location of this state
*/
const gchar *
wp_state_get_location (WpState *self)
{
g_return_val_if_fail (WP_IS_STATE (self), NULL);
wp_state_ensure_location (self);
return self->location;
}
/**
* wp_state_clear:
* @self: the state
*
* Clears the state removing its file
*/
void
wp_state_clear (WpState *self)
{
g_return_if_fail (WP_IS_STATE (self));
wp_state_ensure_location (self);
if (path_exists (self->location))
remove (self->location);
}
/**
* wp_state_save:
* @self: the state
* @props: (transfer none): the properties to save
*
* Saves new properties in the state, overwriting all previous data.
*
* Returns: TRUE if the properties could be saved, FALSE otherwise
*/
gboolean
wp_state_save (WpState *self, WpProperties *props)
{
g_autoptr (WpIterator) it = NULL;
g_auto (GValue) item = G_VALUE_INIT;
g_autofree gchar *tmp_name = NULL, *tmp_location = NULL;
gulong tmp_size;
int fd;
FILE *f;
g_return_val_if_fail (WP_IS_STATE (self), FALSE);
wp_state_ensure_location (self);
wp_info_object (self, "saving state into %s", self->location);
/* Get the temporary name and location */
tmp_size = strlen (self->name) + 5;
tmp_name = g_malloc (tmp_size);
g_snprintf (tmp_name, tmp_size, "%s.tmp", self->name);
tmp_location = get_new_location (tmp_name);
g_return_val_if_fail (tmp_location, FALSE);
/* Open */
fd = open (tmp_location, O_CLOEXEC | O_CREAT | O_WRONLY | O_TRUNC, 0700);
if (fd == -1) {
wp_critical_object (self, "can't open %s", tmp_location);
return FALSE;
}
/* Write */
f = fdopen(fd, "w");
for (it = wp_properties_iterate (props);
wp_iterator_next (it, &item);
g_value_unset (&item)) {
const gchar *p = wp_properties_iterator_item_get_key (&item);
while (*p) {
if (*p == ' ' || *p == '\\')
fputc('\\', f);
fprintf(f, "%c", *p++);
}
fprintf(f, " %s\n", wp_properties_iterator_item_get_value (&item));
}
fclose(f);
/* Rename temporary file */
if (rename(tmp_location, self->location) < 0) {
wp_critical_object("can't rename temporary file '%s' to '%s'", tmp_name,
self->name);
return FALSE;
}
return TRUE;
}
/**
* wp_state_load:
* @self: the state
*
* Loads the state data into new properties.
*
* Returns (transfer full): the new properties with the state data
*/
WpProperties *
wp_state_load (WpState *self)
{
g_autoptr (WpProperties) props = wp_properties_new_empty ();
int fd;
FILE *f;
char line[1024];
g_return_val_if_fail (WP_IS_STATE (self), NULL);
wp_state_ensure_location (self);
/* Open */
wp_info_object (self, "loading state from %s", self->location);
fd = open (self->location, O_CLOEXEC | O_RDONLY);
if (fd == -1) {
/* We consider empty state if fill does not exist */
if (errno == ENOENT)
return g_steal_pointer (&props);
wp_critical_object (self, "can't open %s", self->location);
return NULL;
}
/* Read */
f = fdopen(fd, "r");
while (fgets (line, sizeof(line)-1, f)) {
char *val, *key, *k, *p;
val = strrchr(line, '\n');
if (val)
*val = '\0';
key = k = p = line;
while (*p) {
if (*p == ' ')
break;
if (*p == '\\')
p++;
*k++ = *p++;
}
*k = '\0';
val = ++p;
wp_properties_set (props, key, val);
}
fclose(f);
return g_steal_pointer (&props);
}

47
lib/wp/state.h Normal file
View File

@@ -0,0 +1,47 @@
/* WirePlumber
*
* Copyright © 2020 Collabora Ltd.
* @author Julian Bouzas <julian.bouzas@collabora.com>
*
* SPDX-License-Identifier: MIT
*/
#ifndef __WIREPLUMBER_STATE_H__
#define __WIREPLUMBER_STATE_H__
#include "properties.h"
G_BEGIN_DECLS
/* WpState */
/**
* WP_TYPE_STATE:
*
* The #WpState #GType
*/
#define WP_TYPE_STATE (wp_state_get_type ())
WP_API
G_DECLARE_FINAL_TYPE (WpState, wp_state, WP, STATE, GObject)
WP_API
WpState * wp_state_new (const gchar *name);
WP_API
const gchar * wp_state_get_name (WpState *self);
WP_API
const gchar * wp_state_get_location (WpState *self);
WP_API
void wp_state_clear (WpState *self);
WP_API
gboolean wp_state_save (WpState *self, WpProperties *props);
WP_API
WpProperties * wp_state_load (WpState *self);
G_END_DECLS
#endif

View File

@@ -39,6 +39,7 @@
#include "si-interfaces.h"
#include "spa-pod.h"
#include "spa-type.h"
#include "state.h"
#include "transition.h"
#include "wpenums.h"
#include "wpversion.h"

View File

@@ -56,6 +56,17 @@ shared_library(
dependencies : [wp_dep, pipewire_dep, giounix_dep],
)
shared_library(
'wireplumber-module-default-profile',
[
'module-default-profile.c',
],
c_args : [common_c_args, '-DG_LOG_DOMAIN="m-default-profile"'],
install : true,
install_dir : wireplumber_module_dir,
dependencies : [wp_dep, pipewire_dep],
)
shared_library(
'wireplumber-module-device-activation',
[

View File

@@ -0,0 +1,342 @@
/* WirePlumber
*
* Copyright © 2020 Collabora Ltd.
* @author Julian Bouzas <julian.bouzas@collabora.com>
*
* SPDX-License-Identifier: MIT
*/
#include <wp/wp.h>
#include <pipewire/pipewire.h>
#define STATE_NAME "default-profile"
#define SAVE_INTERVAL_MS 1000
G_DEFINE_QUARK (wp-module-default-profile-profiles, profiles);
/* Signals */
enum
{
SIGNAL_GET_PROFILE,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
G_DECLARE_DERIVABLE_TYPE (WpDefaultProfile, wp_default_profile, WP,
DEFAULT_PROFILE, WpPlugin)
struct _WpDefaultProfileClass
{
WpPluginClass parent_class;
void (*get_profile) (WpDefaultProfile *self, WpPipewireObject *device,
const char **curr_profile);
};
typedef struct _WpDefaultProfilePrivate WpDefaultProfilePrivate;
struct _WpDefaultProfilePrivate
{
WpState *state;
WpProperties *profiles;
GSource *timeout_source;
WpObjectManager *devices_om;
};
G_DEFINE_TYPE_WITH_PRIVATE (WpDefaultProfile, wp_default_profile,
WP_TYPE_PLUGIN)
static gint
find_device_profile (WpPipewireObject *device, const gchar *lookup_name)
{
WpIterator *profiles = NULL;
g_auto (GValue) item = G_VALUE_INIT;
profiles = g_object_get_qdata (G_OBJECT (device), profiles_quark ());
g_return_val_if_fail (profiles, -1);
wp_iterator_reset (profiles);
for (; wp_iterator_next (profiles, &item); g_value_unset (&item)) {
WpSpaPod *pod = g_value_get_boxed (&item);
gint index = 0;
const gchar *name = NULL;
/* Parse */
if (!wp_spa_pod_get_object (pod,
"Profile", NULL,
"index", "i", &index,
"name", "s", &name,
NULL)) {
continue;
}
if (g_strcmp0 (name, lookup_name) == 0)
return index;
}
return -1;
}
static gboolean
timeout_save_callback (WpDefaultProfile *self)
{
WpDefaultProfilePrivate *priv =
wp_default_profile_get_instance_private (self);
if (!wp_state_save (priv->state, priv->profiles))
wp_warning_object (self, "could not save profiles");
return G_SOURCE_REMOVE;
}
static void
timeout_save_profiles (WpDefaultProfile *self, guint ms)
{
WpDefaultProfilePrivate *priv =
wp_default_profile_get_instance_private (self);
g_autoptr (WpCore) core = wp_plugin_get_core (WP_PLUGIN (self));
g_return_if_fail (core);
g_return_if_fail (priv->profiles);
/* Clear the current timeout callback */
if (priv->timeout_source)
g_source_destroy (priv->timeout_source);
g_clear_pointer (&priv->timeout_source, g_source_unref);
/* Add the timeout callback */
wp_core_timeout_add_closure (core, &priv->timeout_source, ms,
g_cclosure_new_object (G_CALLBACK (timeout_save_callback),
G_OBJECT (self)));
}
static void
wp_default_profile_get_profile (WpDefaultProfile *self,
WpPipewireObject *device, const gchar **curr_profile)
{
WpDefaultProfilePrivate *priv =
wp_default_profile_get_instance_private (self);
const gchar *dev_name = NULL;
g_return_if_fail (device);
g_return_if_fail (curr_profile);
g_return_if_fail (priv->profiles);
/* Get the device name */
dev_name = wp_pipewire_object_get_property (device, PW_KEY_DEVICE_NAME);
g_return_if_fail (dev_name);
/* Get the profile */
*curr_profile = wp_properties_get (priv->profiles, dev_name);
}
static void
update_profile (WpDefaultProfile *self, WpPipewireObject *device,
const gchar *new_profile)
{
WpDefaultProfilePrivate *priv =
wp_default_profile_get_instance_private (self);
const gchar *dev_name, *curr_profile = NULL;
gint index;
g_return_if_fail (new_profile);
g_return_if_fail (priv->profiles);
/* Get the device name */
dev_name = wp_pipewire_object_get_property (device, PW_KEY_DEVICE_NAME);
g_return_if_fail (dev_name);
/* Check if the new profile is the same as the current one */
curr_profile = wp_properties_get (priv->profiles, dev_name);
if (curr_profile && g_strcmp0 (curr_profile, new_profile) == 0)
return;
/* Make sure the profile is valid */
index = find_device_profile (device, new_profile);
if (index < 0) {
wp_info_object (self, "profile '%s' (%d) is not valid on device '%s'",
new_profile, index, dev_name);
return;
}
/* Otherwise update the profile and add timeout save callback */
wp_properties_set (priv->profiles, dev_name, new_profile);
timeout_save_profiles (self, SAVE_INTERVAL_MS);
wp_info_object (self, "updated profile '%s' (%d) on device '%s'", new_profile,
index, dev_name);
}
static void
on_device_profile_notified (WpPipewireObject *device, GAsyncResult *res,
WpDefaultProfile *self)
{
g_autoptr (WpIterator) profiles = NULL;
g_autoptr (GError) error = NULL;
g_auto (GValue) item = G_VALUE_INIT;
const gchar *name = NULL;
gint index = 0;
/* Finish */
profiles = wp_pipewire_object_enum_params_finish (device, res, &error);
if (error) {
wp_warning_object (self, "failed to get current profile on device");
return;
}
/* Ignore empty profile notifications */
if (!wp_iterator_next (profiles, &item))
return;
/* Parse the profile */
WpSpaPod *pod = g_value_get_boxed (&item);
if (!wp_spa_pod_get_object (pod,
"Profile", NULL,
"index", "i", &index,
"name", "s", &name,
NULL)) {
wp_warning_object (self, "failed to parse current profile");
return;
}
g_value_unset (&item);
/* Update the profile */
update_profile (self, device, name);
}
static void
on_device_param_info_notified (WpPipewireObject * device, GParamSpec * param,
WpDefaultProfile *self)
{
/* Check the profile every time the params have changed */
wp_pipewire_object_enum_params (device, "Profile", NULL, NULL,
(GAsyncReadyCallback) on_device_profile_notified, self);
}
static void
on_device_enum_profile_done (WpPipewireObject *device, GAsyncResult *res,
WpDefaultProfile *self)
{
g_autoptr (WpIterator) profiles = NULL;
g_autoptr (GError) error = NULL;
/* Finish */
profiles = wp_pipewire_object_enum_params_finish (device, res, &error);
if (error) {
wp_warning_object (self, "failed to enum profiles in device "
WP_OBJECT_FORMAT, WP_OBJECT_ARGS (device));
return;
}
/* Keep a reference of the profiles in the device object */
g_object_set_qdata_full (G_OBJECT (device), profiles_quark (),
g_steal_pointer (&profiles), (GDestroyNotify) wp_iterator_unref);
/* Watch for param info changes */
g_signal_connect_object (device, "notify::param-info",
G_CALLBACK (on_device_param_info_notified), self, 0);
}
static void
on_device_added (WpObjectManager *om, WpPipewireObject *proxy, gpointer d)
{
WpDefaultProfile *self = WP_DEFAULT_PROFILE (d);
wp_debug_object (self, "device " WP_OBJECT_FORMAT " added",
WP_OBJECT_ARGS (proxy));
/* Enum available profiles */
wp_pipewire_object_enum_params (proxy, "EnumProfile", NULL, NULL,
(GAsyncReadyCallback) on_device_enum_profile_done, self);
}
static void
wp_default_profile_activate (WpPlugin * plugin)
{
g_autoptr (WpCore) core = wp_plugin_get_core (plugin);
WpDefaultProfile *self = WP_DEFAULT_PROFILE (plugin);
WpDefaultProfilePrivate *priv =
wp_default_profile_get_instance_private (self);
/* Create the devices object manager */
priv->devices_om = wp_object_manager_new ();
wp_object_manager_add_interest (priv->devices_om, WP_TYPE_DEVICE, NULL);
wp_object_manager_request_object_features (priv->devices_om,
WP_TYPE_DEVICE, WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL);
g_signal_connect_object (priv->devices_om, "object-added",
G_CALLBACK (on_device_added), self, 0);
wp_core_install_object_manager (core, priv->devices_om);
}
static void
wp_default_profile_deactivate (WpPlugin * plugin)
{
WpDefaultProfile *self = WP_DEFAULT_PROFILE (plugin);
WpDefaultProfilePrivate *priv =
wp_default_profile_get_instance_private (self);
g_clear_object (&priv->devices_om);
}
static void
wp_default_profile_finalize (GObject * object)
{
WpDefaultProfile *self = WP_DEFAULT_PROFILE (object);
WpDefaultProfilePrivate *priv =
wp_default_profile_get_instance_private (self);
/* Clear the current timeout callback */
if (priv->timeout_source)
g_source_destroy (priv->timeout_source);
g_clear_pointer (&priv->timeout_source, g_source_unref);
g_clear_pointer (&priv->profiles, wp_properties_unref);
g_clear_object (&priv->state);
}
static void
wp_default_profile_init (WpDefaultProfile * self)
{
WpDefaultProfilePrivate *priv =
wp_default_profile_get_instance_private (self);
priv->state = wp_state_new (STATE_NAME);
/* Load the saved profiles */
priv->profiles = wp_state_load (priv->state);
if (!priv->profiles) {
wp_warning_object (self, "could not load profiles");
return;
}
}
static void
wp_default_profile_class_init (WpDefaultProfileClass * klass)
{
GObjectClass *object_class = (GObjectClass *) klass;
WpPluginClass *plugin_class = (WpPluginClass *) klass;
object_class->finalize = wp_default_profile_finalize;
plugin_class->activate = wp_default_profile_activate;
plugin_class->deactivate = wp_default_profile_deactivate;
klass->get_profile = wp_default_profile_get_profile;
/* Signals */
signals[SIGNAL_GET_PROFILE] = g_signal_new ("get-profile",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (WpDefaultProfileClass, get_profile), NULL, NULL,
NULL, G_TYPE_NONE, 2, WP_TYPE_DEVICE, G_TYPE_POINTER);
}
WP_PLUGIN_EXPORT void
wireplumber__module_init (WpModule * module, WpCore * core, GVariant * args)
{
wp_plugin_register (g_object_new (wp_default_profile_get_type (),
"name", STATE_NAME,
"module", module,
NULL));
}

View File

@@ -16,6 +16,8 @@ struct _WpDeviceActivation
{
WpPlugin parent;
GWeakRef default_profile;
WpObjectManager *plugins_om;
WpObjectManager *devices_om;
};
@@ -24,86 +26,110 @@ G_DECLARE_FINAL_TYPE (WpDeviceActivation, wp_device_activation, WP,
G_DEFINE_TYPE (WpDeviceActivation, wp_device_activation, WP_TYPE_PLUGIN)
static void
set_device_profile (WpPipewireObject *device, gint index)
set_device_profile (WpDeviceActivation *self,
WpPipewireObject *device, gint index)
{
g_return_if_fail (device);
g_autoptr (WpSpaPod) profile = wp_spa_pod_new_object (
/* Set profile */
wp_pipewire_object_set_param (device, "Profile", 0,
wp_spa_pod_new_object (
"Profile", "Profile",
"index", "i", index,
NULL);
wp_debug_object (device, "set profile %d", index);
wp_pipewire_object_set_param (device, "Profile", 0, profile);
NULL));
wp_info_object (self, "profile %d set on device " WP_OBJECT_FORMAT, index,
WP_OBJECT_ARGS (device));
}
static void
on_device_enum_profile_done (WpPipewireObject *proxy, GAsyncResult *res,
WpDeviceActivation *self)
{
g_autoptr (WpPlugin) dp = g_weak_ref_get (&self->default_profile);
g_autoptr (WpIterator) profiles = NULL;
g_auto (GValue) item = G_VALUE_INIT;
g_autoptr (GError) error = NULL;
guint profile_index = 1;
const gchar *name = NULL;
gint index = -1;
/* Finish */
profiles = wp_pipewire_object_enum_params_finish (proxy, res, &error);
if (error) {
wp_warning_object (self, "failed to enum profiles in bluetooth device");
wp_warning_object (self, "failed to enum profiles on device");
return;
}
/* Get the first available profile */
/* Get the default profile name if default-profile module is loaded */
if (dp)
g_signal_emit_by_name (dp, "get-profile", WP_DEVICE (proxy), &name);
/* Find the profile index */
if (name) {
g_auto (GValue) item = G_VALUE_INIT;
for (; wp_iterator_next (profiles, &item); g_value_unset (&item)) {
WpSpaPod *pod = g_value_get_boxed (&item);
gint index = 0;
const gchar *name = NULL;
g_return_if_fail (pod);
g_return_if_fail (wp_spa_pod_is_object (pod));
gint i = 0;
const gchar *n = NULL;
/* Parse */
if (!wp_spa_pod_get_object (pod,
"Profile", NULL,
"index", "i", &index,
"name", "s", &name,
"index", "i", &i,
"name", "s", &n,
NULL)) {
wp_warning_object (self, "bluetooth profile does not have index / name");
continue;
}
wp_info_object (self, "bluez profile found: %s (%d)", name, index);
/* TODO: we assume the last profile is the one with highest priority */
profile_index = index;
if (g_strcmp0 (name, n) == 0) {
index = i;
break;
}
}
}
set_device_profile (proxy, profile_index);
/* If not profile was found, use index 1 for ALSA (no ACP) and Bluez5 */
if (index < 0) {
/* Alsa */
const gchar *api =
wp_pipewire_object_get_property (proxy, PW_KEY_DEVICE_API);
if (api && g_str_has_prefix (api, "alsa")) {
const gchar *acp =
wp_pipewire_object_get_property (proxy, "device.api.alsa.acp");
if (!acp || !atoi (acp))
index = 1;
}
/* Bluez5 */
else if (api && g_str_has_prefix (api, "bluez5")) {
index = 1;
}
}
/* Set the profile */
if (index >= 0)
set_device_profile (self, proxy, index);
}
static void
on_device_added (WpObjectManager *om, WpPipewireObject *proxy, gpointer d)
{
WpDeviceActivation *self = WP_DEVICE_ACTIVATION (d);
const gchar *device_api =
wp_pipewire_object_get_property (proxy, PW_KEY_DEVICE_API);
g_return_if_fail (device_api);
wp_debug_object (self, "device " WP_OBJECT_FORMAT " added, api '%s'",
WP_OBJECT_ARGS (proxy), device_api);
/* ALSA */
if (g_str_has_prefix (device_api, "alsa")) {
set_device_profile (proxy, 1);
}
/* Bluez5 */
else if (g_str_has_prefix (device_api, "bluez5")) {
/* Enum available bluetooth profiles */
/* Enum available profiles */
wp_pipewire_object_enum_params (proxy, "EnumProfile", NULL, NULL,
(GAsyncReadyCallback) on_device_enum_profile_done, self);
}
/* Video */
else if (g_str_has_prefix (device_api, "v4l2")) {
/* No need to activate video devices */
}
static void
on_plugin_added (WpObjectManager *om, WpPlugin *plugin, gpointer d)
{
WpDeviceActivation *self = WP_DEVICE_ACTIVATION (d);
g_autoptr (WpPlugin) dp = g_weak_ref_get (&self->default_profile);
if (dp)
wp_warning_object (self, "skipping additional default profile plugin");
else
g_weak_ref_set (&self->default_profile, plugin);
}
static void
@@ -112,6 +138,15 @@ wp_device_activation_activate (WpPlugin * plugin)
WpDeviceActivation *self = WP_DEVICE_ACTIVATION (plugin);
g_autoptr (WpCore) core = wp_plugin_get_core (WP_PLUGIN (self));
/* Create the plugin object manager */
self->plugins_om = wp_object_manager_new ();
wp_object_manager_add_interest (self->plugins_om, WP_TYPE_PLUGIN,
WP_CONSTRAINT_TYPE_G_PROPERTY, "name", "=s", "default-profile",
NULL);
g_signal_connect_object (self->plugins_om, "object-added",
G_CALLBACK (on_plugin_added), self, 0);
wp_core_install_object_manager (core, self->plugins_om);
/* Create the devices object manager */
self->devices_om = wp_object_manager_new ();
wp_object_manager_add_interest (self->devices_om, WP_TYPE_DEVICE, NULL);
@@ -128,6 +163,8 @@ wp_device_activation_deactivate (WpPlugin * plugin)
WpDeviceActivation *self = WP_DEVICE_ACTIVATION (plugin);
g_clear_object (&self->devices_om);
g_clear_object (&self->plugins_om);
g_weak_ref_clear (&self->default_profile);
}
static void

View File

@@ -197,7 +197,6 @@ on_device_enum_profile_done (WpPipewireObject *proxy, GAsyncResult *res,
/* Iterate all profiles */
for (; wp_iterator_next (profiles, &item); g_value_unset (&item)) {
WpSpaPod *pod = g_value_get_boxed (&item);
g_autoptr (WpSpaPodParser) pp = NULL;
gint index = 0;
const gchar *name = NULL;
const gchar *desc = NULL;

View File

@@ -38,10 +38,6 @@ WpSession * wp_limited_creation_lookup_session (WpLimitedCreation *self, ...);
WpSession * wp_limited_creation_lookup_session_full (WpLimitedCreation *self,
WpObjectInterest * interest);
void wp_limited_creation_add_node (WpLimitedCreation * self, WpNode *node);
void wp_limited_creation_remove_node (WpLimitedCreation * self, WpNode *node);
/* for subclasses only */
void wp_endpoint_creation_notify_endpoint_created(WpLimitedCreation * self,
WpSessionItem *ep);

View File

@@ -21,6 +21,7 @@ G_DEFINE_QUARK (wp-module-monitor-children, children);
typedef enum {
FLAG_LOCAL_NODES = (1 << 0),
FLAG_USE_ADAPTER = (1 << 1),
FLAG_USE_ACP = (1 << 2),
} MonitorFlags;
static const struct {
@@ -29,6 +30,7 @@ static const struct {
} flag_names[] = {
{ FLAG_LOCAL_NODES, "local-nodes" },
{ FLAG_USE_ADAPTER, "use-adapter" },
{ FLAG_USE_ACP, "use-acp" },
};
enum {
@@ -80,12 +82,17 @@ device_data_free (gpointer data, GClosure *closure)
}
static void
setup_device_props (WpProperties *p)
setup_device_props (WpMonitor * self, WpProperties *p)
{
const gchar *s, *d, *api;
api = wp_properties_get (p, SPA_KEY_DEVICE_API);
/* if alsa and ACP, set acp property to true */
if (!g_strcmp0 (api, "alsa") && (self->flags & FLAG_USE_ACP))
wp_properties_setf (p, "device.api.alsa.acp", "%d",
self->flags & FLAG_USE_ACP ? TRUE : FALSE);
/* set the device name if it's not already set */
if (!wp_properties_get (p, SPA_KEY_DEVICE_NAME)) {
if ((s = wp_properties_get (p, SPA_KEY_DEVICE_BUS_ID)) == NULL) {
@@ -339,14 +346,18 @@ create_device (WpMonitor * self, WpSpaDevice * parent, guint id,
GList *children = NULL;
GList *link = NULL;
GObject *child = NULL;
const char *factory_name = NULL;
g_return_val_if_fail (parent, NULL);
g_return_val_if_fail (spa_factory, NULL);
find_child (G_OBJECT (parent), id, &children, &link, &child);
factory_name = self->flags & FLAG_USE_ACP ?
SPA_NAME_API_ALSA_ACP_DEVICE : spa_factory;
/* Create the device */
device = wp_spa_device_new_from_spa_factory (self->local_core, spa_factory,
device = wp_spa_device_new_from_spa_factory (self->local_core, factory_name,
props);
if (!device)
return NULL;
@@ -414,7 +425,7 @@ maybe_create_device (WpMonitor * self, WpSpaDevice * parent, guint id,
/* Create the properties */
props = wp_properties_copy (props);
setup_device_props (props);
setup_device_props (self, props);
/* If dbus reservation API exists, let dbus manage the device, otherwise just
* create it and never destroy it */

View File

@@ -40,7 +40,7 @@ load-module C libwireplumber-module-si-bluez5-endpoint
#
load-module C libwireplumber-module-monitor {
"alsa": <{"factory": <"api.alsa.enum.udev">, "flags": <["use-adapter"]>}>,
"alsa": <{"factory": <"api.alsa.enum.udev">, "flags": <["use-acp", "use-adapter"]>}>,
"bluez5": <{"factory": <"api.bluez5.enum.dbus">, "flags": <["local-nodes", "use-adapter"]>}>,
"v4l2": <{"factory": <"api.v4l2.enum.udev">}>
}
@@ -52,6 +52,9 @@ load-module C libwireplumber-module-monitor {
# Grants dbus reservation functionality
load-module C libwireplumber-module-dbus-reservation
# Grants functionality to store and restaure default device profiles
load-module C libwireplumber-module-default-profile
# Grants access to security confined clients
load-module C libwireplumber-module-client-permissions

View File

@@ -2,6 +2,7 @@ common_deps = [gobject_dep, gio_dep, wp_dep, pipewire_dep]
common_env = [
'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()),
'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir()),
'XDG_CONFIG_HOME=@0@'.format(meson.current_source_dir() / '.config'),
'WIREPLUMBER_MODULE_DIR=@0@'.format(meson.current_build_dir() / '..' / '..' / 'modules'),
'WIREPLUMBER_DEBUG=7',
]
@@ -78,6 +79,13 @@ test(
env: common_env,
)
test(
'test-state',
executable('test-state', 'state.c',
dependencies: common_deps, c_args: common_args),
env: common_env,
)
test(
'test-transition',
executable('test-transition', 'transition.c',

141
tests/wp/state.c Normal file
View File

@@ -0,0 +1,141 @@
/* WirePlumber
*
* Copyright © 2020 Collabora Ltd.
* @author Julian Bouzas <julian.bouzas@collabora.com>
*
* SPDX-License-Identifier: MIT
*/
#include <wp/wp.h>
static void
test_state_basic (void)
{
g_autoptr (WpState) state = wp_state_new ("basic");
g_assert_nonnull (state);
g_assert_cmpstr (wp_state_get_name (state), ==, "basic");
g_assert_true (g_str_has_suffix (wp_state_get_location (state), "basic"));
/* Save */
{
g_autoptr (WpProperties) props = wp_properties_new_empty ();
wp_properties_set (props, "key1", "value1");
wp_properties_set (props, "key2", "value2");
wp_properties_set (props, "key3", "value3");
g_assert_true (wp_state_save (state, props));
}
/* Load */
{
g_autoptr (WpProperties) props = wp_state_load (state);
g_assert_nonnull (props);
g_assert_cmpstr (wp_properties_get (props, "key1"), ==, "value1");
g_assert_cmpstr (wp_properties_get (props, "key2"), ==, "value2");
g_assert_cmpstr (wp_properties_get (props, "key2"), ==, "value2");
g_assert_null (wp_properties_get (props, "invalid"));
}
/* Re-Save */
{
g_autoptr (WpProperties) props = wp_properties_new_empty ();
wp_properties_set (props, "new-key", "new-value");
g_assert_true (wp_state_save (state, props));
}
/* Re-Load */
{
g_autoptr (WpProperties) props = wp_state_load (state);
g_assert_nonnull (props);
g_assert_cmpstr (wp_properties_get (props, "new-key"), ==, "new-value");
g_assert_null (wp_properties_get (props, "key1"));
g_assert_null (wp_properties_get (props, "key2"));
g_assert_null (wp_properties_get (props, "key3"));
}
wp_state_clear (state);
/* Load empty */
{
g_autoptr (WpProperties) props = wp_state_load (state);
g_assert_nonnull (props);
g_assert_null (wp_properties_get (props, "new-key"));
g_assert_null (wp_properties_get (props, "key1"));
g_assert_null (wp_properties_get (props, "key2"));
g_assert_null (wp_properties_get (props, "key3"));
}
wp_state_clear (state);
}
static void
test_state_empty (void)
{
g_autoptr (WpState) state = wp_state_new ("empty");
g_assert_nonnull (state);
/* Save */
{
g_autoptr (WpProperties) props = wp_properties_new_empty ();
wp_properties_set (props, "key", "value");
g_assert_true (wp_state_save (state, props));
}
/* Load */
{
g_autoptr (WpProperties) props = wp_state_load (state);
g_assert_nonnull (props);
g_assert_cmpstr (wp_properties_get (props, "key"), ==, "value");
}
/* Save empty */
{
g_autoptr (WpProperties) props = wp_properties_new_empty ();
g_assert_true (wp_state_save (state, props));
}
/* Load empty */
{
g_autoptr (WpProperties) props = wp_state_load (state);
g_assert_nonnull (props);
g_assert_null (wp_properties_get (props, "key"));
}
wp_state_clear (state);
}
static void
test_state_spaces (void)
{
g_autoptr (WpState) state = wp_state_new ("spaces");
g_assert_nonnull (state);
/* Save */
{
g_autoptr (WpProperties) props = wp_properties_new_empty ();
wp_properties_set (props, "key", "value with spaces");
g_assert_true (wp_state_save (state, props));
}
/* Load */
{
g_autoptr (WpProperties) props = wp_state_load (state);
g_assert_nonnull (props);
g_assert_cmpstr (wp_properties_get (props, "key"), ==, "value with spaces");
}
wp_state_clear (state);
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_log_set_writer_func (wp_log_writer_default, NULL, NULL);
g_test_add_func ("/wp/state/basic", test_state_basic);
g_test_add_func ("/wp/state/empty", test_state_empty);
g_test_add_func ("/wp/state/spaces", test_state_spaces);
return g_test_run ();
}