From 66a0bf60931194d1aee54c83636ce264db3994a6 Mon Sep 17 00:00:00 2001 From: George Kiagiadakis Date: Mon, 11 May 2020 15:45:09 +0300 Subject: [PATCH] lib: implement a wp_init() function --- lib/wp/core.c | 14 ---- lib/wp/debug.c | 6 +- lib/wp/debug.h | 4 +- lib/wp/meson.build | 1 + lib/wp/session.c | 11 --- lib/wp/wp.c | 83 +++++++++++++++++++++++ lib/wp/wp.h | 20 ++++++ src/main.c | 2 +- tests/common/base-test-fixture.h | 3 - tests/examples/audiotestsrc-play.c | 3 +- tests/modules/config-endpoint.c | 3 +- tests/modules/config-policy.c | 3 +- tests/modules/config-static-nodes.c | 3 +- tests/modules/dbus-device-reservation.c | 3 +- tests/modules/si-audio-softdsp-endpoint.c | 3 +- tests/modules/si-simple-node-endpoint.c | 3 +- tests/modules/si-standard-link.c | 3 +- tests/wp/endpoint.c | 3 +- tests/wp/proxy.c | 3 +- tests/wp/session-item.c | 2 +- tests/wp/session.c | 3 +- tests/wp/spa-props.c | 1 + tools/wireplumber-cli.c | 14 +--- 23 files changed, 125 insertions(+), 69 deletions(-) create mode 100644 lib/wp/wp.c diff --git a/lib/wp/core.c b/lib/wp/core.c index 6b289797..d0287512 100644 --- a/lib/wp/core.c +++ b/lib/wp/core.c @@ -280,9 +280,6 @@ wp_core_class_init (WpCoreClass * klass) { GObjectClass *object_class = (GObjectClass *) klass; - pw_init (NULL, NULL); - wp_spa_type_init (TRUE); - object_class->constructed = wp_core_constructed; object_class->dispose = wp_core_dispose; object_class->finalize = wp_core_finalize; @@ -326,17 +323,6 @@ wp_core_class_init (WpCoreClass * klass) signals[SIGNAL_DISCONNECTED] = g_signal_new ("disconnected", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0); - - /* ensure WpProxy subclasses are loaded, which is needed to be able - to autodetect the GType of proxies created through wp_proxy_new_global() */ - g_type_ensure (WP_TYPE_CLIENT); - g_type_ensure (WP_TYPE_DEVICE); - g_type_ensure (WP_TYPE_ENDPOINT); - g_type_ensure (WP_TYPE_ENDPOINT_STREAM); - g_type_ensure (WP_TYPE_LINK); - g_type_ensure (WP_TYPE_NODE); - g_type_ensure (WP_TYPE_PORT); - g_type_ensure (WP_TYPE_SESSION); } /** diff --git a/lib/wp/debug.c b/lib/wp/debug.c index 33bfe1e5..3287b4b9 100644 --- a/lib/wp/debug.c +++ b/lib/wp/debug.c @@ -401,8 +401,8 @@ static struct spa_log wp_spa_log = { .level = SPA_LOG_LEVEL_INFO, }; -void -wp_install_glib_pw_log (void) +struct spa_log * +wp_spa_log_get_instance (void) { - pw_log_set (&wp_spa_log); + return &wp_spa_log; } diff --git a/lib/wp/debug.h b/lib/wp/debug.h index cf3b86e5..3d337fcc 100644 --- a/lib/wp/debug.h +++ b/lib/wp/debug.h @@ -79,8 +79,10 @@ void wp_log_structured_standard (const gchar *log_domain, #define wp_trace_boxed(type, object, ...) \ wp_log (WP_LOG_LEVEL_TRACE, type, object, __VA_ARGS__) +struct spa_log; + WP_API -void wp_install_glib_pw_log (void); +struct spa_log * wp_spa_log_get_instance (void); G_END_DECLS diff --git a/lib/wp/meson.build b/lib/wp/meson.build index 4372a4d1..38e9d9cc 100644 --- a/lib/wp/meson.build +++ b/lib/wp/meson.build @@ -29,6 +29,7 @@ wp_lib_sources = files( 'spa-props.c', 'spa-type.c', 'transition.c', + 'wp.c', ) wp_lib_headers = files( diff --git a/lib/wp/session.c b/lib/wp/session.c index 9199e7b8..08f5ab46 100644 --- a/lib/wp/session.c +++ b/lib/wp/session.c @@ -344,17 +344,6 @@ wp_session_class_init (WpSessionClass * klass) GObjectClass *object_class = (GObjectClass *) klass; WpProxyClass *proxy_class = (WpProxyClass *) klass; - /* Register custom wireplumber session types */ - wp_spa_type_register (WP_SPA_TYPE_TABLE_PROPS, - "Wp:Session:Default:Endpoint:Audio:Source", - "wp-session-default-endpoint-audio-source"); - wp_spa_type_register (WP_SPA_TYPE_TABLE_PROPS, - "Wp:Session:Default:Endpoint:Audio:Sink", - "wp-session-default-endpoint-audio-sink"); - wp_spa_type_register (WP_SPA_TYPE_TABLE_PROPS, - "Wp:Session:Default:Endpoint:Video:Source", - "wp-session-default-endpoint-video-source"); - object_class->finalize = wp_session_finalize; proxy_class->pw_iface_type = PW_TYPE_INTERFACE_Session; diff --git a/lib/wp/wp.c b/lib/wp/wp.c new file mode 100644 index 00000000..ecabd559 --- /dev/null +++ b/lib/wp/wp.c @@ -0,0 +1,83 @@ +/* WirePlumber + * + * Copyright © 2020 Collabora Ltd. + * @author George Kiagiadakis + * + * SPDX-License-Identifier: MIT + */ + +#include "wp.h" +#include + +/** + * SECTION: wp + * @title: Library Initialization + */ + + +/** + * WpInitFlags: + * @WP_INIT_PIPEWIRE: Initializes libpipewire by calling `pw_init()` + * @WP_INIT_SPA_TYPES: Initializes WirePlumber's SPA types integration, + * required for using #WpSpaPod among other things + * @WP_INIT_SET_PW_LOG: Enables redirecting debug log messages from + * libpipewire to GLib's logging system, by installing WirePlumber's + * implementation of `struct spa_log` (see wp_spa_log_get_instance()) + * with `pw_log_set()` + * @WP_INIT_SET_GLIB_LOG: Installs WirePlumber's debug log handler, + * wp_log_writer_default(), on GLib with g_log_set_writer_func() + * @WP_INIT_ALL: Enables all of the above + * + * See wp_init() + */ + +/** + * wp_init: + * @flags: initialization flags + * + * Initializes WirePlumber and PipeWire underneath. @flags can modify + * which parts are initialized, in cases where you want to handle part + * of this initialization externally. + */ +void +wp_init (WpInitFlags flags) +{ + if (flags & WP_INIT_SET_PW_LOG) + pw_log_set (wp_spa_log_get_instance ()); + + if (flags & WP_INIT_SET_GLIB_LOG) + g_log_set_writer_func (wp_log_writer_default, NULL, NULL); + + /* a dummy message, to initialize the logging system */ + wp_message ("WirePlumber initializing"); + + if (flags & WP_INIT_PIPEWIRE) + pw_init (NULL, NULL); + + if (flags & WP_INIT_SPA_TYPES) { + wp_spa_type_init (TRUE); + + /* Register custom wireplumber session types */ + wp_spa_type_register (WP_SPA_TYPE_TABLE_PROPS, + "Wp:Session:Default:Endpoint:Audio:Source", + "wp-session-default-endpoint-audio-source"); + wp_spa_type_register (WP_SPA_TYPE_TABLE_PROPS, + "Wp:Session:Default:Endpoint:Audio:Sink", + "wp-session-default-endpoint-audio-sink"); + wp_spa_type_register (WP_SPA_TYPE_TABLE_PROPS, + "Wp:Session:Default:Endpoint:Video:Source", + "wp-session-default-endpoint-video-source"); + } + + /* ensure WpProxy subclasses are loaded, which is needed to be able + to autodetect the GType of proxies created through wp_proxy_new_global() */ + g_type_ensure (WP_TYPE_CLIENT); + g_type_ensure (WP_TYPE_DEVICE); + g_type_ensure (WP_TYPE_ENDPOINT); + g_type_ensure (WP_TYPE_ENDPOINT_LINK); + g_type_ensure (WP_TYPE_ENDPOINT_STREAM); + g_type_ensure (WP_TYPE_LINK); + g_type_ensure (WP_TYPE_NODE); + g_type_ensure (WP_TYPE_PORT); + g_type_ensure (WP_TYPE_SESSION); +} diff --git a/lib/wp/wp.h b/lib/wp/wp.h index cf5b91c5..b05b838d 100644 --- a/lib/wp/wp.h +++ b/lib/wp/wp.h @@ -6,6 +6,9 @@ * SPDX-License-Identifier: MIT */ +#ifndef __WIREPLUMBER_WP_H__ +#define __WIREPLUMBER_WP_H__ + #include "base-endpoint.h" #include "client.h" #include "configuration.h" @@ -36,3 +39,20 @@ #include "spa-type.h" #include "transition.h" #include "wpenums.h" + +G_BEGIN_DECLS + +typedef enum { + WP_INIT_PIPEWIRE = (1<<0), + WP_INIT_SPA_TYPES = (1<<1), + WP_INIT_SET_PW_LOG = (1<<2), + WP_INIT_SET_GLIB_LOG = (1<<3), + WP_INIT_ALL = 0xf, +} WpInitFlags; + +WP_API +void wp_init (WpInitFlags flags); + +G_END_DECLS + +#endif diff --git a/src/main.c b/src/main.c index bb44fe8a..f02cdceb 100644 --- a/src/main.c +++ b/src/main.c @@ -324,7 +324,7 @@ main (gint argc, gchar **argv) g_autoptr (GPtrArray) sessions = NULL; const gchar *configuration_path; - g_log_set_writer_func (wp_log_writer_default, NULL, NULL); + wp_init (WP_INIT_ALL); context = g_option_context_new ("- PipeWire Session/Policy Manager"); g_option_context_add_main_entries (context, entries, NULL); diff --git a/tests/common/base-test-fixture.h b/tests/common/base-test-fixture.h index a0aa4fcb..7ebb3ce9 100644 --- a/tests/common/base-test-fixture.h +++ b/tests/common/base-test-fixture.h @@ -58,9 +58,6 @@ wp_base_test_fixture_setup (WpBaseTestFixture * self, WpBaseTestFlags flags) { g_autoptr (WpProperties) props = NULL; - /* ensure types are loaded */ - g_type_ensure (WP_TYPE_CORE); - /* init test server */ wp_test_server_setup (&self->server); diff --git a/tests/examples/audiotestsrc-play.c b/tests/examples/audiotestsrc-play.c index e0449c93..a7226423 100644 --- a/tests/examples/audiotestsrc-play.c +++ b/tests/examples/audiotestsrc-play.c @@ -418,8 +418,7 @@ main (gint argc, gchar *argv[]) g_auto (AppData) data = {0}; g_autoptr (GError) error = NULL; - pw_init (NULL, NULL); - g_log_set_writer_func (wp_log_writer_default, NULL, NULL); + wp_init (WP_INIT_ALL); if (argc > 1) data.alsa_device = argv[1]; diff --git a/tests/modules/config-endpoint.c b/tests/modules/config-endpoint.c index bd3411ba..94200283 100644 --- a/tests/modules/config-endpoint.c +++ b/tests/modules/config-endpoint.c @@ -197,8 +197,7 @@ int main (int argc, char *argv[]) { g_test_init (&argc, &argv, NULL); - pw_init (NULL, NULL); - g_log_set_writer_func (wp_log_writer_default, NULL, NULL); + wp_init (WP_INIT_ALL); g_test_add ("/modules/config-endpoint/simple", TestConfigEndpointFixture, NULL, config_endpoint_setup, simple, config_endpoint_teardown); diff --git a/tests/modules/config-policy.c b/tests/modules/config-policy.c index 53c29309..96babea5 100644 --- a/tests/modules/config-policy.c +++ b/tests/modules/config-policy.c @@ -379,8 +379,7 @@ int main (int argc, char *argv[]) { g_test_init (&argc, &argv, NULL); - pw_init (NULL, NULL); - g_log_set_writer_func (wp_log_writer_default, NULL, NULL); + wp_init (WP_INIT_ALL); g_test_add ("/modules/config-policy/playback", TestConfigPolicyFixture, NULL, config_policy_setup, playback, config_policy_teardown); diff --git a/tests/modules/config-static-nodes.c b/tests/modules/config-static-nodes.c index 4f173fb1..a48183b4 100644 --- a/tests/modules/config-static-nodes.c +++ b/tests/modules/config-static-nodes.c @@ -74,8 +74,7 @@ int main (int argc, char *argv[]) { g_test_init (&argc, &argv, NULL); - pw_init (NULL, NULL); - g_log_set_writer_func (wp_log_writer_default, NULL, NULL); + wp_init (WP_INIT_ALL); g_test_add ("/modules/config-static-nodes/basic", TestConfigStaticNodesFixture, NULL, diff --git a/tests/modules/dbus-device-reservation.c b/tests/modules/dbus-device-reservation.c index 1719b7be..ed9fd08e 100644 --- a/tests/modules/dbus-device-reservation.c +++ b/tests/modules/dbus-device-reservation.c @@ -193,8 +193,7 @@ gint main (gint argc, gchar *argv[]) { g_test_init (&argc, &argv, NULL); - pw_init (NULL, NULL); - g_log_set_writer_func (wp_log_writer_default, NULL, NULL); + wp_init (WP_INIT_ALL); g_test_add ("/wp/dbus/basic", TestDbusFixture, NULL, test_dbus_setup, test_dbus_basic, test_dbus_teardown); diff --git a/tests/modules/si-audio-softdsp-endpoint.c b/tests/modules/si-audio-softdsp-endpoint.c index 47037fed..33b58f24 100644 --- a/tests/modules/si-audio-softdsp-endpoint.c +++ b/tests/modules/si-audio-softdsp-endpoint.c @@ -352,8 +352,7 @@ gint main (gint argc, gchar *argv[]) { g_test_init (&argc, &argv, NULL); - pw_init (NULL, NULL); - g_log_set_writer_func (wp_log_writer_default, NULL, NULL); + wp_init (WP_INIT_ALL); /* configure-activate */ diff --git a/tests/modules/si-simple-node-endpoint.c b/tests/modules/si-simple-node-endpoint.c index 29bfb2c1..621e2182 100644 --- a/tests/modules/si-simple-node-endpoint.c +++ b/tests/modules/si-simple-node-endpoint.c @@ -370,8 +370,7 @@ gint main (gint argc, gchar *argv[]) { g_test_init (&argc, &argv, NULL); - pw_init (NULL, NULL); - g_log_set_writer_func (wp_log_writer_default, NULL, NULL); + wp_init (WP_INIT_ALL); /* data */ diff --git a/tests/modules/si-standard-link.c b/tests/modules/si-standard-link.c index fe6179de..02f917ee 100644 --- a/tests/modules/si-standard-link.c +++ b/tests/modules/si-standard-link.c @@ -426,8 +426,7 @@ gint main (gint argc, gchar *argv[]) { g_test_init (&argc, &argv, NULL); - pw_init (NULL, NULL); - g_log_set_writer_func (wp_log_writer_default, NULL, NULL); + wp_init (WP_INIT_ALL); g_test_add ("/modules/si-standard-link/main", TestFixture, NULL, diff --git a/tests/wp/endpoint.c b/tests/wp/endpoint.c index 2eb8d85a..b2ebf836 100644 --- a/tests/wp/endpoint.c +++ b/tests/wp/endpoint.c @@ -490,8 +490,7 @@ gint main (gint argc, gchar *argv[]) { g_test_init (&argc, &argv, NULL); - pw_init (NULL, NULL); - g_log_set_writer_func (wp_log_writer_default, NULL, NULL); + wp_init (WP_INIT_ALL); g_test_add ("/wp/endpoint/basic", TestEndpointFixture, NULL, test_endpoint_setup, test_endpoint_basic, test_endpoint_teardown); diff --git a/tests/wp/proxy.c b/tests/wp/proxy.c index 3e816a76..7dbdd567 100644 --- a/tests/wp/proxy.c +++ b/tests/wp/proxy.c @@ -196,8 +196,7 @@ gint main (gint argc, gchar *argv[]) { g_test_init (&argc, &argv, NULL); - pw_init (NULL, NULL); - g_log_set_writer_func (wp_log_writer_default, NULL, NULL); + wp_init (WP_INIT_ALL); g_test_add ("/wp/proxy/basic", TestProxyFixture, NULL, test_proxy_setup, test_proxy_basic, test_proxy_teardown); diff --git a/tests/wp/session-item.c b/tests/wp/session-item.c index ce7181c9..f2e73d3d 100644 --- a/tests/wp/session-item.c +++ b/tests/wp/session-item.c @@ -510,7 +510,7 @@ gint main (gint argc, gchar *argv[]) { g_test_init (&argc, &argv, NULL); - g_log_set_writer_func (wp_log_writer_default, NULL, NULL); + wp_init (WP_INIT_ALL); g_test_add_func ("/wp/session-item/flags", test_flags); g_test_add_func ("/wp/session-item/configuration", test_configuration); diff --git a/tests/wp/session.c b/tests/wp/session.c index 75b0f393..95586f63 100644 --- a/tests/wp/session.c +++ b/tests/wp/session.c @@ -311,8 +311,7 @@ gint main (gint argc, gchar *argv[]) { g_test_init (&argc, &argv, NULL); - pw_init (NULL, NULL); - g_log_set_writer_func (wp_log_writer_default, NULL, NULL); + wp_init (WP_INIT_ALL); g_test_add ("/wp/session/basic", TestSessionFixture, NULL, test_session_setup, test_session_basic, test_session_teardown); diff --git a/tests/wp/spa-props.c b/tests/wp/spa-props.c index 0446eaf5..634a1f5a 100644 --- a/tests/wp/spa-props.c +++ b/tests/wp/spa-props.c @@ -257,6 +257,7 @@ 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/spa_props/set_get", test_spa_props_set_get); g_test_add_func ("/wp/spa_props/build_all", test_spa_props_build_all); diff --git a/tools/wireplumber-cli.c b/tools/wireplumber-cli.c index 66ca2da9..65977acf 100644 --- a/tools/wireplumber-cli.c +++ b/tools/wireplumber-cli.c @@ -275,19 +275,7 @@ main (gint argc, gchar **argv) g_autoptr (WpObjectManager) om = NULL; g_autoptr (GMainLoop) loop = NULL; - g_log_set_writer_func (wp_log_writer_default, NULL, NULL); - - /* Register custom wireplumber session types */ - wp_spa_type_init (TRUE); - wp_spa_type_register (WP_SPA_TYPE_TABLE_BASIC, - "Wp:Session:Default:Endpoint:Audio:Source", - "wp-session-default-endpoint-audio-source"); - wp_spa_type_register (WP_SPA_TYPE_TABLE_BASIC, - "Wp:Session:Default:Endpoint:Audio:Sink", - "wp-session-default-endpoint-audio-sink"); - wp_spa_type_register (WP_SPA_TYPE_TABLE_BASIC, - "Wp:Session:Default:Endpoint:Video:Source", - "wp-session-default-endpoint-video-source"); + wp_init (WP_INIT_ALL); context = g_option_context_new ("- PipeWire Session/Policy Manager Helper CLI"); g_option_context_add_main_entries (context, entries, NULL);