summaryrefslogtreecommitdiff
path: root/backends/pulse/pulse.c
blob: d3065774fd7e6c734f47a3f594cf22e157e172f8 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * Copyright (C) 2014 Michal Ratajsky <michal.ratajsky@gmail.com>
 *
 * This library 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 of the licence, or (at your option) any later version.
 *
 * This library 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 library; if not, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <glib-object.h>
#include <sys/types.h>
#include <unistd.h>

#include <libmatemixer/matemixer-backend.h>
#include <libmatemixer/matemixer-backend-module.h>

#include <pulse/pulseaudio.h>
#include <pulse/thread-mainloop.h>

#include "pulse.h"
#include "pulse-device.h"

#define BACKEND_NAME      "PulseAudio"
#define BACKEND_PRIORITY   0

struct _MateMixerPulsePrivate
{
    pa_threaded_mainloop  *mainloop;
    pa_context            *context;
    GHashTable            *devices;
};

/* Support function for dynamic loading of the backend module */
void  backend_module_init (GTypeModule *module);

const MateMixerBackendModuleInfo *backend_module_get_info (void);

static void    mate_mixer_backend_interface_init (MateMixerBackendInterface *iface);

static void    pulse_card_info_cb (pa_context *c,
                                   const pa_card_info *info,
                                   int eol,
                                   void *userdata);

static void    pulse_card_update  (MateMixerPulse *pulse, const pa_card_info *i);

static void    pulse_state_cb     (pa_context *c, void *userdata);

static void    pulse_subscribe_cb (pa_context *c,
                                   pa_subscription_event_type_t t,
                                   uint32_t idx,
                                   void *userdata);

static gchar  *pulse_get_app_name (void);

G_DEFINE_DYNAMIC_TYPE_EXTENDED (MateMixerPulse, mate_mixer_pulse,
                                G_TYPE_OBJECT, 0,
                                G_IMPLEMENT_INTERFACE_DYNAMIC (MATE_MIXER_TYPE_BACKEND,
                                                               mate_mixer_backend_interface_init))

static MateMixerBackendModuleInfo info;

void
backend_module_init (GTypeModule *module)
{
    mate_mixer_pulse_register_type (module);

    info.name         = BACKEND_NAME;
    info.priority     = BACKEND_PRIORITY;
    info.g_type       = MATE_MIXER_TYPE_PULSE;
    info.backend_type = MATE_MIXER_BACKEND_TYPE_PULSE;
}

const MateMixerBackendModuleInfo *
backend_module_get_info (void)
{
    return &info;
}

static void
mate_mixer_backend_interface_init (MateMixerBackendInterface *iface)
{
    iface->open = mate_mixer_pulse_open;
    iface->close = mate_mixer_pulse_close;
    iface->list_devices = mate_mixer_pulse_list_devices;
}

static void
mate_mixer_pulse_init (MateMixerPulse *pulse)
{
    pulse->priv = G_TYPE_INSTANCE_GET_PRIVATE (
        pulse,
        MATE_MIXER_TYPE_PULSE,
        MateMixerPulsePrivate);

    pulse->priv->devices = g_hash_table_new_full (
        g_direct_hash,
        g_direct_equal,
        NULL,
        g_object_unref);
}

static void
mate_mixer_pulse_finalize (GObject *object)
{
    MateMixerPulse *pulse;

    pulse = MATE_MIXER_PULSE (object);

    g_hash_table_destroy (pulse->priv->devices);

    G_OBJECT_CLASS (mate_mixer_pulse_parent_class)->finalize (object);
}

static void
mate_mixer_pulse_class_init (MateMixerPulseClass *klass)
{
    GObjectClass *object_class;

    object_class = G_OBJECT_CLASS (klass);
    object_class->finalize = mate_mixer_pulse_finalize;

    g_type_class_add_private (object_class, sizeof (MateMixerPulsePrivate));
}

/* Called in the code generated by G_DEFINE_DYNAMIC_TYPE_EXTENDED() */
static void
mate_mixer_pulse_class_finalize (MateMixerPulseClass *klass)
{
}

gboolean
mate_mixer_pulse_open (MateMixerBackend *backend)
{
    int              ret;
    gchar           *app_name;
    MateMixerPulse  *pulse;

    g_return_val_if_fail (MATE_MIXER_IS_BACKEND (backend), FALSE);

    pulse = MATE_MIXER_PULSE (backend);

    g_return_val_if_fail (pulse->priv->mainloop == NULL, FALSE);

    pulse->priv->mainloop = pa_threaded_mainloop_new ();
    if (G_UNLIKELY (pulse->priv->mainloop == NULL)) {
        g_warning ("Failed to created PulseAudio main loop");
        return FALSE;
    }

    app_name = pulse_get_app_name ();

    pulse->priv->context = pa_context_new (
        pa_threaded_mainloop_get_api (pulse->priv->mainloop),
        app_name);

    g_free (app_name);

    if (G_UNLIKELY (pulse->priv->context == NULL)) {
        g_warning ("Failed to created PulseAudio context");

        pa_threaded_mainloop_free (pulse->priv->mainloop);
        pulse->priv->mainloop = NULL;
        return FALSE;
    }

    // XXX: investigate PA_CONTEXT_NOFAIL
    ret = pa_context_connect (pulse->priv->context, NULL, PA_CONTEXT_NOFLAGS, NULL);
    if (ret < 0) {
        g_warning ("Failed to connect to PulseAudio server: %s", pa_strerror (ret));

        pa_context_unref (pulse->priv->context);
        pa_threaded_mainloop_free (pulse->priv->mainloop);

        pulse->priv->context = NULL;
        pulse->priv->mainloop = NULL;
        return FALSE;
    }

    g_debug ("Connected to PulseAudio server");

    pa_threaded_mainloop_lock (pulse->priv->mainloop);

    pa_context_set_state_callback (pulse->priv->context,
        pulse_state_cb,
        backend);
    pa_context_set_subscribe_callback (pulse->priv->context,
        pulse_subscribe_cb,
        backend);

    ret = pa_threaded_mainloop_start (pulse->priv->mainloop);
    if (ret < 0) {
        g_warning ("Failed to start PulseAudio main loop: %s", pa_strerror (ret));

        pa_threaded_mainloop_unlock (pulse->priv->mainloop);

        pa_context_unref (pulse->priv->context);
        pa_threaded_mainloop_free (pulse->priv->mainloop);

        pulse->priv->context = NULL;
        pulse->priv->mainloop = NULL;
        return FALSE;
    }

    while (pa_context_get_state (pulse->priv->context) != PA_CONTEXT_READY) {
        // XXX this will get stuck if connection fails
        pa_threaded_mainloop_wait (pulse->priv->mainloop);
    }

    pa_threaded_mainloop_unlock (pulse->priv->mainloop);

    return TRUE;
}

void
mate_mixer_pulse_close (MateMixerBackend *backend)
{
    MateMixerPulse *pulse;

    g_return_if_fail (MATE_MIXER_IS_BACKEND (backend));

    pulse = MATE_MIXER_PULSE (backend);

    g_return_if_fail (pulse->priv->mainloop != NULL);

    pa_threaded_mainloop_stop (pulse->priv->mainloop);

    pa_context_unref (pulse->priv->context);
    pa_threaded_mainloop_free (pulse->priv->mainloop);

    pulse->priv->context = NULL;
    pulse->priv->mainloop = NULL;
}

GList *
mate_mixer_pulse_list_devices (MateMixerBackend *backend)
{
    MateMixerPulse  *pulse;
    pa_operation    *o;

    g_return_val_if_fail (MATE_MIXER_IS_BACKEND (backend), NULL);

    pulse = MATE_MIXER_PULSE (backend);

    pa_threaded_mainloop_lock (pulse->priv->mainloop);

    o = pa_context_get_card_info_list (pulse->priv->context, pulse_card_info_cb, pulse);
    if (o == NULL) {
        g_warning ("Failed to read card list: %s",
            pa_strerror (pa_context_errno (pulse->priv->context)));

        pa_threaded_mainloop_unlock (pulse->priv->mainloop);
        return NULL;
    }

    while (pa_operation_get_state (o) == PA_OPERATION_RUNNING)
        pa_threaded_mainloop_wait (pulse->priv->mainloop);

    pa_operation_unref (o);
    pa_threaded_mainloop_unlock (pulse->priv->mainloop);

    return g_hash_table_get_values (pulse->priv->devices);
}

static void
pulse_card_info_cb (pa_context *c, const pa_card_info *info, int eol, void *userdata)
{
    MateMixerPulse *pulse;

    pulse = MATE_MIXER_PULSE (userdata);

    if (!eol)
        pulse_card_update (pulse, info);

    pa_threaded_mainloop_signal (pulse->priv->mainloop, 0);
}

static void
pulse_card_update (MateMixerPulse *pulse, const pa_card_info *info)
{
    gpointer item;

    item = g_hash_table_lookup (pulse->priv->devices, GINT_TO_POINTER (info->index));
    if (item) {
        /* The card is already known, just update the fields that may
         * have changed */
        mate_mixer_pulse_device_update (MATE_MIXER_PULSE_DEVICE (item), info);
    } else {
        MateMixerPulseDevice *device = mate_mixer_pulse_device_new (info);

        if (G_UNLIKELY (device == NULL))
            g_warning ("Failed to process PulseAudio sound device");
        else
            g_hash_table_insert (
                pulse->priv->devices,
                GINT_TO_POINTER (info->index),
                device);
    }
}

static void
pulse_state_cb (pa_context *c, void *userdata)
{
    MateMixerPulse *pulse;

    pulse = MATE_MIXER_PULSE (userdata);

    // TODO: handle errors

    switch (pa_context_get_state (c)) {
    case PA_CONTEXT_UNCONNECTED:
        break;
    case PA_CONTEXT_CONNECTING:
        break;
    case PA_CONTEXT_AUTHORIZING:
        break;
    case PA_CONTEXT_SETTING_NAME:
        break;
    case PA_CONTEXT_READY:
        break;
    case PA_CONTEXT_FAILED:
        break;
    case PA_CONTEXT_TERMINATED:
        break;
    default:
        break;
    }

    pa_threaded_mainloop_signal (pulse->priv->mainloop, FALSE);
}

static void
pulse_subscribe_cb (pa_context *c,
                    pa_subscription_event_type_t t,
                    uint32_t idx,
                    void *userdata)
{
    // TODO
}

static gchar *
pulse_get_app_name (void)
{
    const char *name_app;
    char        name_buf[256];

    /* Inspired by GStreamer's pulse plugin */
    name_app = g_get_application_name ();
    if (name_app != NULL)
        return g_strdup (name_app);

    if (pa_get_binary_name (name_buf, sizeof (name_buf)) != NULL)
        return g_strdup (name_buf);

    return g_strdup_printf ("libmatemixer-%lu", (gulong) getpid ());
}