Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* WirePlumber
*
* Copyright © 2019 Collabora Ltd.
* @author George Kiagiadakis <george.kiagiadakis@collabora.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define G_LOG_DOMAIN "wireplumber-core"
#include "core.h"
#include "loop-source.h"
#include "utils.h"
#include <pipewire/pipewire.h>
#include <glib-unix.h>
struct _WpCore
{
GObject parent;
GMainLoop *loop;
GSource *source;
struct pw_core *core;
struct pw_remote *remote;
struct spa_hook remote_listener;
struct pw_core_proxy *core_proxy;
struct spa_hook core_proxy_listener;
struct pw_registry_proxy *registry_proxy;
struct spa_hook registry_proxy_listener;
GError *exit_error;
};
G_DEFINE_TYPE (WpCore, wp_core, G_TYPE_OBJECT);
static const struct pw_registry_proxy_events registry_events = {
PW_VERSION_REGISTRY_PROXY_EVENTS,
//.global = registry_global,
//.global_remove = registry_global_remove,
};
static const struct pw_core_proxy_events core_events = {
PW_VERSION_CORE_EVENTS,
//.done = core_done
};
static void on_state_changed (void * data,
enum pw_remote_state old_state,
enum pw_remote_state new_state,
const char * error)
{
WpCore *self = WP_CORE (data);
g_debug ("remote state changed, old:%s new:%s",
pw_remote_state_as_string (old_state),
pw_remote_state_as_string (new_state));
switch (new_state) {
case PW_REMOTE_STATE_CONNECTED:
self->core_proxy = pw_remote_get_core_proxy (self->remote);
pw_core_proxy_add_listener (self->core_proxy, &self->core_proxy_listener,
&core_events, self);
self->registry_proxy = pw_core_proxy_get_registry (self->core_proxy,
PW_TYPE_INTERFACE_Registry, PW_VERSION_REGISTRY, 0);
pw_registry_proxy_add_listener (self->registry_proxy,
&self->registry_proxy_listener, ®istry_events, self);
break;
case PW_REMOTE_STATE_UNCONNECTED:
self->core_proxy = NULL;
self->registry_proxy = NULL;
wp_core_exit (self, WP_DOMAIN_CORE, WP_CODE_DISCONNECTED, "disconnected");
break;
case PW_REMOTE_STATE_ERROR:
wp_core_exit (self, WP_DOMAIN_CORE, WP_CODE_REMOTE_ERROR,
"pipewire remote error: %s", error);
break;
default:
break;
}
}
static const struct pw_remote_events remote_events = {
PW_VERSION_REMOTE_EVENTS,
.state_changed = on_state_changed,
};
static void
wp_core_init (WpCore * self)
{
self->loop = g_main_loop_new (NULL, FALSE);
self->source = wp_loop_source_new ();
g_source_attach (self->source, NULL);
self->core = pw_core_new (wp_loop_source_get_loop (self->source), NULL, 0);
self->remote = pw_remote_new (self->core, NULL, 0);
pw_remote_add_listener (self->remote, &self->remote_listener, &remote_events,
self);
}
static void
wp_core_finalize (GObject * obj)
{
WpCore *self = WP_CORE (obj);
spa_hook_remove (&self->remote_listener);
pw_remote_destroy (self->remote);
pw_core_destroy (self->core);
g_source_destroy (self->source);
g_source_unref (self->source);
g_main_loop_unref (self->loop);
g_warn_if_fail (self->exit_error == NULL);
g_clear_error (&self->exit_error);
}
static void
wp_core_class_init (WpCoreClass * klass)
{
GObjectClass * object_class = (GObjectClass *) klass;
object_class->finalize = wp_core_finalize;
}
WpCore *
wp_core_get_instance (void)
{
static WpCore *instance = NULL;
if (G_UNLIKELY (!instance)) {
instance = g_object_new (wp_core_get_type (), NULL);
}
return instance;
}
static gboolean
signal_handler (gpointer data)
{
WpCore *self = WP_CORE (data);
wp_core_exit (self, WP_DOMAIN_CORE, WP_CODE_INTERRUPTED,
"interrupted by signal");
return G_SOURCE_CONTINUE;
}
void
wp_core_run (WpCore * self, GError ** error)
{
g_unix_signal_add (SIGINT, signal_handler, self);
g_unix_signal_add (SIGTERM, signal_handler, self);
g_unix_signal_add (SIGHUP, signal_handler, self);
g_idle_add ((GSourceFunc) pw_remote_connect, self->remote);
g_main_loop_run (self->loop);
if (self->exit_error) {
g_propagate_error (error, self->exit_error);
self->exit_error = NULL;
}
}
void
wp_core_exit (WpCore * self, GQuark domain, gint code,
const gchar *format, ...)
{
va_list args;
va_start (args, format);
self->exit_error = g_error_new_valist (domain, code, format, args);
va_end (args);
g_main_loop_quit (self->loop);
}