implement WpSession & WpSessionRegistry
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "core-interfaces.h"
|
||||
#include "plugin.h"
|
||||
#include "session.h"
|
||||
|
||||
/* WpPluginRegistry */
|
||||
|
||||
@@ -147,3 +148,90 @@ wp_proxy_registry_get_pw_registry_proxy (WpProxyRegistry * self)
|
||||
|
||||
return iface->get_pw_registry_proxy (self);
|
||||
}
|
||||
|
||||
/* WpSessionRegistry */
|
||||
|
||||
G_DEFINE_INTERFACE (WpSessionRegistry, wp_session_registry, G_TYPE_OBJECT)
|
||||
|
||||
enum {
|
||||
SIG_SESSION_REGISTERED,
|
||||
SIG_SESSION_UNREGISTERED,
|
||||
N_SESSION_REGISTRY_SIGNALS
|
||||
};
|
||||
|
||||
static guint session_registry_signals[N_SESSION_REGISTRY_SIGNALS];
|
||||
|
||||
static void
|
||||
wp_session_registry_default_init (WpSessionRegistryInterface * iface)
|
||||
{
|
||||
session_registry_signals[SIG_SESSION_REGISTERED] = g_signal_new (
|
||||
"session-registered", G_TYPE_FROM_INTERFACE (iface),
|
||||
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT,
|
||||
WP_TYPE_SESSION);
|
||||
|
||||
session_registry_signals[SIG_SESSION_UNREGISTERED] = g_signal_new (
|
||||
"session-unregistered", G_TYPE_FROM_INTERFACE (iface),
|
||||
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_UINT);
|
||||
}
|
||||
|
||||
guint32
|
||||
wp_session_registry_register_session (WpSessionRegistry * self,
|
||||
WpSession * session,
|
||||
GError ** error)
|
||||
{
|
||||
WpSessionRegistryInterface *iface = WP_SESSION_REGISTRY_GET_IFACE (self);
|
||||
guint32 id;
|
||||
|
||||
g_return_val_if_fail (WP_IS_SESSION_REGISTRY (self), -1);
|
||||
g_return_val_if_fail (session != NULL, -1);
|
||||
g_return_val_if_fail (iface->register_session, -1);
|
||||
|
||||
id = iface->register_session (self, session, error);
|
||||
if (id != -1) {
|
||||
g_signal_emit (self, session_registry_signals[SIG_SESSION_REGISTERED], 0,
|
||||
id, session);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
gboolean
|
||||
wp_session_registry_unregister_session (WpSessionRegistry * self,
|
||||
guint32 session_id)
|
||||
{
|
||||
WpSessionRegistryInterface *iface = WP_SESSION_REGISTRY_GET_IFACE (self);
|
||||
gboolean ret;
|
||||
|
||||
g_return_val_if_fail (WP_IS_SESSION_REGISTRY (self), FALSE);
|
||||
g_return_val_if_fail (iface->unregister_session, FALSE);
|
||||
|
||||
ret = iface->unregister_session (self, session_id);
|
||||
if (ret) {
|
||||
g_signal_emit (self, session_registry_signals[SIG_SESSION_UNREGISTERED], 0,
|
||||
session_id);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
GArray *
|
||||
wp_session_registry_list_sessions (WpSessionRegistry * self,
|
||||
const gchar * media_class)
|
||||
{
|
||||
WpSessionRegistryInterface *iface = WP_SESSION_REGISTRY_GET_IFACE (self);
|
||||
|
||||
g_return_val_if_fail (WP_IS_SESSION_REGISTRY (self), NULL);
|
||||
g_return_val_if_fail (iface->list_sessions, NULL);
|
||||
|
||||
return iface->list_sessions (self, media_class);
|
||||
}
|
||||
|
||||
WpSession *
|
||||
wp_session_registry_get_session (WpSessionRegistry * self,
|
||||
guint32 session_id)
|
||||
{
|
||||
WpSessionRegistryInterface *iface = WP_SESSION_REGISTRY_GET_IFACE (self);
|
||||
|
||||
g_return_val_if_fail (WP_IS_SESSION_REGISTRY (self), NULL);
|
||||
g_return_val_if_fail (iface->get_session, NULL);
|
||||
|
||||
return iface->get_session (self, session_id);
|
||||
}
|
||||
|
@@ -68,6 +68,43 @@ WpProxy * wp_proxy_registry_get_proxy (WpProxyRegistry * self,
|
||||
struct pw_registry_proxy * wp_proxy_registry_get_pw_registry_proxy (
|
||||
WpProxyRegistry * self);
|
||||
|
||||
/* WpSessionRegistry */
|
||||
|
||||
#define WP_TYPE_SESSION_REGISTRY (wp_session_registry_get_type ())
|
||||
G_DECLARE_INTERFACE (WpSessionRegistry, wp_session_registry, WP, SESSION_REGISTRY, GObject)
|
||||
|
||||
typedef struct _WpSession WpSession;
|
||||
|
||||
struct _WpSessionRegistryInterface
|
||||
{
|
||||
GTypeInterface parent;
|
||||
|
||||
guint32 (*register_session) (WpSessionRegistry * self,
|
||||
WpSession * session,
|
||||
GError ** error);
|
||||
|
||||
gboolean (*unregister_session) (WpSessionRegistry * self, guint32 session_id);
|
||||
|
||||
GArray * (*list_sessions) (WpSessionRegistry * self,
|
||||
const gchar * media_class);
|
||||
|
||||
WpSession * (*get_session) (WpSessionRegistry * self,
|
||||
guint32 session_id);
|
||||
};
|
||||
|
||||
guint32 wp_session_registry_register_session (WpSessionRegistry * self,
|
||||
WpSession * session_object,
|
||||
GError ** error);
|
||||
|
||||
gboolean wp_session_registry_unregister_session (WpSessionRegistry * self,
|
||||
guint32 session_id);
|
||||
|
||||
GArray * wp_session_registry_list_sessions (WpSessionRegistry * self,
|
||||
const gchar * media_class);
|
||||
|
||||
WpSession * wp_session_registry_get_session (WpSessionRegistry * self,
|
||||
guint32 session_id);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif
|
||||
|
@@ -5,6 +5,7 @@ wp_lib_sources = [
|
||||
'object.c',
|
||||
'plugin.c',
|
||||
'proxy.c',
|
||||
'session.c',
|
||||
]
|
||||
|
||||
wp_lib_headers = [
|
||||
@@ -14,6 +15,7 @@ wp_lib_headers = [
|
||||
'object.h',
|
||||
'plugin.h',
|
||||
'proxy.h',
|
||||
'session.h',
|
||||
]
|
||||
|
||||
enums = gnome.mkenums_simple('wpenums', sources: wp_lib_headers)
|
||||
|
82
lib/wp/session.c
Normal file
82
lib/wp/session.c
Normal file
@@ -0,0 +1,82 @@
|
||||
/* WirePlumber
|
||||
*
|
||||
* Copyright © 2019 Collabora Ltd.
|
||||
* @author George Kiagiadakis <george.kiagiadakis@collabora.com>
|
||||
*
|
||||
* SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
*/
|
||||
|
||||
#include "session.h"
|
||||
#include "wpenums.h"
|
||||
|
||||
enum {
|
||||
PROP_0,
|
||||
PROP_DIRECTION
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
WpSessionDirection direction;
|
||||
} WpSessionPrivate;
|
||||
|
||||
G_DEFINE_TYPE_WITH_PRIVATE (WpSession, wp_session, WP_TYPE_OBJECT)
|
||||
|
||||
static void
|
||||
wp_session_init (WpSession * self)
|
||||
{
|
||||
}
|
||||
|
||||
static void
|
||||
wp_session_set_property (GObject * object, guint property_id,
|
||||
const GValue * value, GParamSpec * pspec)
|
||||
{
|
||||
WpSessionPrivate *priv = wp_session_get_instance_private (WP_SESSION (object));
|
||||
|
||||
switch (property_id) {
|
||||
case PROP_DIRECTION:
|
||||
priv->direction = g_value_get_enum (value);
|
||||
break;
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
wp_session_get_property (GObject * object, guint property_id, GValue * value,
|
||||
GParamSpec * pspec)
|
||||
{
|
||||
WpSessionPrivate *priv = wp_session_get_instance_private (WP_SESSION (object));
|
||||
|
||||
switch (property_id) {
|
||||
case PROP_DIRECTION:
|
||||
g_value_set_enum (value, priv->direction);
|
||||
break;
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
wp_session_class_init (WpSessionClass * klass)
|
||||
{
|
||||
GObjectClass *object_class = (GObjectClass *) klass;
|
||||
|
||||
object_class->set_property = wp_session_set_property;
|
||||
object_class->get_property = wp_session_get_property;
|
||||
|
||||
g_object_class_install_property (object_class, PROP_DIRECTION,
|
||||
g_param_spec_enum ("direction", "direction",
|
||||
"The media flow direction of the session",
|
||||
WP_TYPE_SESSION_DIRECTION, 0,
|
||||
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
|
||||
}
|
||||
|
||||
WpSessionDirection
|
||||
wp_session_get_direction (WpSession * self)
|
||||
{
|
||||
WpSessionDirection dir;
|
||||
g_object_get (self, "direction", &dir, NULL);
|
||||
return dir;
|
||||
}
|
35
lib/wp/session.h
Normal file
35
lib/wp/session.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/* WirePlumber
|
||||
*
|
||||
* Copyright © 2019 Collabora Ltd.
|
||||
* @author George Kiagiadakis <george.kiagiadakis@collabora.com>
|
||||
*
|
||||
* SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
*/
|
||||
|
||||
#ifndef __WP_SESSION_H__
|
||||
#define __WP_SESSION_H__
|
||||
|
||||
#include "object.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define WP_TYPE_SESSION (wp_session_get_type ())
|
||||
G_DECLARE_DERIVABLE_TYPE (WpSession, wp_session, WP, SESSION, WpObject)
|
||||
|
||||
typedef enum {
|
||||
WP_SESSION_DIRECTION_INPUT,
|
||||
WP_SESSION_DIRECTION_OUTPUT
|
||||
} WpSessionDirection;
|
||||
|
||||
struct _WpSessionClass
|
||||
{
|
||||
WpObjectClass parent_class;
|
||||
};
|
||||
|
||||
WpSessionDirection wp_session_get_direction (WpSession * session);
|
||||
|
||||
#define WP_SESSION_PW_PROP_MEDIA_CLASS "media.class"
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif
|
@@ -11,6 +11,7 @@
|
||||
#include "module-loader.h"
|
||||
#include "plugin-registry-impl.h"
|
||||
#include "proxy-registry-impl.h"
|
||||
#include "session-registry-impl.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include <pipewire/pipewire.h>
|
||||
@@ -198,6 +199,7 @@ wp_core_init (WpCore * self)
|
||||
{
|
||||
WpPluginRegistryImpl *plugin_registry;
|
||||
WpProxyRegistryImpl *proxy_registry;
|
||||
WpSessionRegistryImpl *session_registry;
|
||||
|
||||
self->loop = g_main_loop_new (NULL, FALSE);
|
||||
self->source = wp_loop_source_new ();
|
||||
@@ -216,6 +218,9 @@ wp_core_init (WpCore * self)
|
||||
|
||||
plugin_registry = wp_plugin_registry_impl_new ();
|
||||
wp_object_attach_interface_impl (WP_OBJECT (self), plugin_registry, NULL);
|
||||
|
||||
session_registry = wp_session_registry_impl_new ();
|
||||
wp_object_attach_interface_impl (WP_OBJECT (self), session_registry, NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
|
@@ -5,6 +5,7 @@ wp_sources = [
|
||||
'module-loader.c',
|
||||
'plugin-registry-impl.c',
|
||||
'proxy-registry-impl.c',
|
||||
'session-registry-impl.c',
|
||||
'utils.c',
|
||||
]
|
||||
|
||||
|
206
src/session-registry-impl.c
Normal file
206
src/session-registry-impl.c
Normal file
@@ -0,0 +1,206 @@
|
||||
/* WirePlumber
|
||||
*
|
||||
* Copyright © 2019 Collabora Ltd.
|
||||
* @author George Kiagiadakis <george.kiagiadakis@collabora.com>
|
||||
*
|
||||
* SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
*/
|
||||
|
||||
#include "session-registry-impl.h"
|
||||
#include "plugin-registry-impl.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include <wp/session.h>
|
||||
#include <wp/plugin.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
guint32 id;
|
||||
gchar *media_class;
|
||||
WpSession *session;
|
||||
} SessionData;
|
||||
|
||||
struct _WpSessionRegistryImpl
|
||||
{
|
||||
WpInterfaceImpl parent;
|
||||
|
||||
guint32 next_id;
|
||||
GArray *sessions;
|
||||
};
|
||||
|
||||
static void wp_session_registry_impl_iface_init (WpSessionRegistryInterface * iface);
|
||||
|
||||
G_DEFINE_TYPE_WITH_CODE (WpSessionRegistryImpl, wp_session_registry_impl, WP_TYPE_INTERFACE_IMPL,
|
||||
G_IMPLEMENT_INTERFACE (WP_TYPE_SESSION_REGISTRY, wp_session_registry_impl_iface_init);)
|
||||
|
||||
static void
|
||||
wp_session_registry_impl_init (WpSessionRegistryImpl * self)
|
||||
{
|
||||
self->sessions = g_array_new (FALSE, FALSE, sizeof (SessionData));
|
||||
}
|
||||
|
||||
static void
|
||||
wp_session_registry_impl_finalize (GObject * obj)
|
||||
{
|
||||
WpSessionRegistryImpl * self = WP_SESSION_REGISTRY_IMPL (obj);
|
||||
|
||||
g_array_unref (self->sessions);
|
||||
|
||||
G_OBJECT_CLASS (wp_session_registry_impl_parent_class)->finalize (obj);
|
||||
}
|
||||
|
||||
static void
|
||||
wp_session_registry_impl_class_init (WpSessionRegistryImplClass * klass)
|
||||
{
|
||||
GObjectClass *object_class = (GObjectClass *) klass;
|
||||
object_class->finalize = wp_session_registry_impl_finalize;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
media_class_matches (const gchar * media_class, const gchar * lookup)
|
||||
{
|
||||
const gchar *c1 = media_class, *c2 = lookup;
|
||||
|
||||
/* empty lookup matches all classes */
|
||||
if (!lookup)
|
||||
return TRUE;
|
||||
|
||||
/* compare until we reach the end of the lookup string */
|
||||
for (; *c2 != '\0'; c1++, c2++) {
|
||||
if (*c1 != *c2)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* the lookup may not end in a slash, however it must match up
|
||||
* to the end of a submedia_class. i.e.:
|
||||
* OK: media_class: Audio/Source/Virtual/
|
||||
* lookup: Audio/Source
|
||||
*
|
||||
* Not OK: media_class: Audio/Source/Virtual/
|
||||
* lookup: Audio/Sou
|
||||
*
|
||||
* if *c1 is not /, also check the previous char, because the lookup
|
||||
* may actually end in a slash.
|
||||
*/
|
||||
if (*c1 != '/' && *(c1 - 1) != '/')
|
||||
return FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gchar *
|
||||
sanitize_media_class (const gchar *media_class)
|
||||
{
|
||||
gsize len = strlen (media_class);
|
||||
if (media_class[len-1] != '/')
|
||||
return g_strdup_printf ("%s/", media_class);
|
||||
else
|
||||
return g_strdup (media_class);
|
||||
}
|
||||
|
||||
static guint32
|
||||
register_session (WpSessionRegistry * sr,
|
||||
WpSession * session,
|
||||
GError ** error)
|
||||
{
|
||||
WpSessionRegistryImpl * self = WP_SESSION_REGISTRY_IMPL (sr);
|
||||
g_autoptr (WpPluginRegistry) plugin_registry = NULL;
|
||||
g_autoptr (WpPipewireProperties) pw_props = NULL;
|
||||
const gchar *media_class = NULL;
|
||||
SessionData data;
|
||||
|
||||
plugin_registry = wp_interface_impl_get_sibling (WP_INTERFACE_IMPL (self),
|
||||
WP_TYPE_PLUGIN_REGISTRY);
|
||||
wp_plugin_registry_impl_invoke (plugin_registry,
|
||||
wp_plugin_provide_interfaces, WP_OBJECT (session));
|
||||
|
||||
pw_props = wp_object_get_interface (WP_OBJECT (session),
|
||||
WP_TYPE_PIPEWIRE_PROPERTIES);
|
||||
if (!pw_props) {
|
||||
g_set_error (error, WP_DOMAIN_CORE, WP_CODE_INVALID_ARGUMENT,
|
||||
"session object does not implement WpPipewirePropertiesInterface");
|
||||
return -1;
|
||||
}
|
||||
|
||||
media_class = wp_pipewire_properties_get (pw_props,
|
||||
WP_SESSION_PW_PROP_MEDIA_CLASS);
|
||||
if (!media_class) {
|
||||
g_set_error (error, WP_DOMAIN_CORE, WP_CODE_INVALID_ARGUMENT,
|
||||
"session media_class is NULL");
|
||||
return -1;
|
||||
}
|
||||
|
||||
data.id = self->next_id++;
|
||||
data.media_class = sanitize_media_class (media_class);
|
||||
data.session = g_object_ref (session);
|
||||
g_array_append_val (self->sessions, data);
|
||||
|
||||
return data.id;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
unregister_session (WpSessionRegistry * sr, guint32 session_id)
|
||||
{
|
||||
WpSessionRegistryImpl * self = WP_SESSION_REGISTRY_IMPL (sr);
|
||||
guint i;
|
||||
|
||||
for (i = 0; i < self->sessions->len; i++) {
|
||||
SessionData *d = &g_array_index (self->sessions, SessionData, i);
|
||||
if (session_id == d->id) {
|
||||
g_free (d->media_class);
|
||||
g_object_unref (d->session);
|
||||
g_array_remove_index_fast (self->sessions, i);
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static WpSession *
|
||||
get_session (WpSessionRegistry * sr, guint32 session_id)
|
||||
{
|
||||
WpSessionRegistryImpl * self = WP_SESSION_REGISTRY_IMPL (sr);
|
||||
guint i;
|
||||
|
||||
for (i = 0; i < self->sessions->len; i++) {
|
||||
SessionData *d = &g_array_index (self->sessions, SessionData, i);
|
||||
if (session_id == d->id)
|
||||
return g_object_ref (d->session);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static GArray *
|
||||
list_sessions (WpSessionRegistry * sr, const gchar * media_class)
|
||||
{
|
||||
WpSessionRegistryImpl * self = WP_SESSION_REGISTRY_IMPL (sr);
|
||||
guint i;
|
||||
GArray *ret;
|
||||
|
||||
ret = g_array_new (FALSE, FALSE, sizeof (guint32));
|
||||
|
||||
for (i = 0; i < self->sessions->len; i++) {
|
||||
SessionData *d = &g_array_index (self->sessions, SessionData, i);
|
||||
if (media_class_matches (d->media_class, media_class))
|
||||
g_array_append_val (ret, d->id);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void
|
||||
wp_session_registry_impl_iface_init (WpSessionRegistryInterface * iface)
|
||||
{
|
||||
iface->register_session = register_session;
|
||||
iface->unregister_session = unregister_session;
|
||||
iface->get_session = get_session;
|
||||
iface->list_sessions = list_sessions;
|
||||
}
|
||||
|
||||
WpSessionRegistryImpl *
|
||||
wp_session_registry_impl_new (void)
|
||||
{
|
||||
return g_object_new (wp_session_registry_impl_get_type (), NULL);
|
||||
}
|
23
src/session-registry-impl.h
Normal file
23
src/session-registry-impl.h
Normal file
@@ -0,0 +1,23 @@
|
||||
/* WirePlumber
|
||||
*
|
||||
* Copyright © 2019 Collabora Ltd.
|
||||
* @author George Kiagiadakis <george.kiagiadakis@collabora.com>
|
||||
*
|
||||
* SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
*/
|
||||
|
||||
#ifndef __WP_SESSION_REGISTRY_IMPL_H__
|
||||
#define __WP_SESSION_REGISTRY_IMPL_H__
|
||||
|
||||
#include <wp/core-interfaces.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
G_DECLARE_FINAL_TYPE (WpSessionRegistryImpl, wp_session_registry_impl,
|
||||
WP, SESSION_REGISTRY_IMPL, WpInterfaceImpl)
|
||||
|
||||
WpSessionRegistryImpl * wp_session_registry_impl_new (void);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user