~ubuntu-branches/ubuntu/wily/xmms2/wily

« back to all changes in this revision

Viewing changes to src/plugins/pulse/backend.c

  • Committer: Bazaar Package Importer
  • Author(s): Benjamin Drung
  • Date: 2008-05-29 10:14:25 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080529101425-ycw1nbd980uhvzfp
Tags: 0.4DrKosmos-4ubuntu1
* Merge from debian unstable (LP: #178477), remaining changes:
  - debian/control: Update Maintainer field
  - debian/control: add lpia to xmms2-plugin-alsa supported architectures
* This version reads AAC files (LP: #156359)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  XMMS2 - X Music Multiplexer System
 
2
 *  Copyright (C) 2003-2007 XMMS2 Team
 
3
 *
 
4
 *  PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
 
5
 *
 
6
 *  This library is free software; you can redistribute it and/or
 
7
 *  modify it under the terms of the GNU Lesser General Public
 
8
 *  License as published by the Free Software Foundation; either
 
9
 *  version 2.1 of the License, or (at your option) any later version.
 
10
 *
 
11
 *  This library is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 *  Lesser General Public License for more details.
 
15
 */
 
16
 
 
17
#include "xmms/xmms_outputplugin.h"
 
18
#include "xmms/xmms_log.h"
 
19
 
 
20
#include "backend.h"
 
21
 
 
22
#include <glib.h>
 
23
 
 
24
#include <pulse/pulseaudio.h>
 
25
 
 
26
 
 
27
static struct {
 
28
        xmms_sample_format_t xmms_fmt;
 
29
        pa_sample_format_t pulse_fmt;
 
30
} xmms_pulse_formats[] = {
 
31
        {XMMS_SAMPLE_FORMAT_U8, PA_SAMPLE_U8},
 
32
#if G_BYTE_ORDER == G_LITTLE_ENDIAN /* Yes, there is PA_SAMPLE_xxNE,
 
33
                                       but they does only work
 
34
                                       if you can be sure that
 
35
                                       WORDS_BIGENDIAN is correctly
 
36
                                       defined */
 
37
        {XMMS_SAMPLE_FORMAT_S16, PA_SAMPLE_S16LE},
 
38
        {XMMS_SAMPLE_FORMAT_FLOAT, PA_SAMPLE_FLOAT32LE},
 
39
#else
 
40
        {XMMS_SAMPLE_FORMAT_S16, PA_SAMPLE_S16BE},
 
41
        {XMMS_SAMPLE_FORMAT_FLOAT, PA_SAMPLE_FLOAT32BE},
 
42
#endif
 
43
};
 
44
 
 
45
struct xmms_pulse {
 
46
        pa_threaded_mainloop *mainloop;
 
47
        pa_context *context;
 
48
        pa_stream *stream;
 
49
        pa_sample_spec sample_spec;
 
50
        pa_channel_map channel_map;
 
51
        pa_cvolume volume;
 
52
        int operation_success;
 
53
};
 
54
 
 
55
static gboolean check_pulse_health(xmms_pulse *p, int *rerror) {
 
56
        if (!p->context || pa_context_get_state(p->context) != PA_CONTEXT_READY ||
 
57
            !p->stream || pa_stream_get_state(p->stream) != PA_STREAM_READY) {
 
58
                if ((p->context &&
 
59
                     pa_context_get_state(p->context) == PA_CONTEXT_FAILED) ||
 
60
                    (p->stream &&
 
61
                     pa_stream_get_state(p->stream) == PA_STREAM_FAILED)) {
 
62
                        if (rerror)
 
63
                                *(rerror) = pa_context_errno(p->context);
 
64
                } else if (rerror)
 
65
                        *(rerror) = PA_ERR_BADSTATE;
 
66
                return FALSE;
 
67
        }
 
68
        return TRUE;
 
69
}
 
70
 
 
71
/*
 
72
 * Callbacks to handle updates from the Pulse daemon.
 
73
 */
 
74
static void signal_mainloop(void *userdata) {
 
75
        xmms_pulse *p = userdata;
 
76
        assert(p);
 
77
 
 
78
        pa_threaded_mainloop_signal(p->mainloop, 0);
 
79
}
 
80
 
 
81
static void context_state_cb(pa_context *c, void *userdata) {
 
82
        assert(c);
 
83
 
 
84
        switch (pa_context_get_state(c)) {
 
85
        case PA_CONTEXT_READY:
 
86
        case PA_CONTEXT_TERMINATED:
 
87
        case PA_CONTEXT_FAILED:
 
88
                signal_mainloop(userdata);
 
89
 
 
90
        case PA_CONTEXT_UNCONNECTED:
 
91
        case PA_CONTEXT_CONNECTING:
 
92
        case PA_CONTEXT_AUTHORIZING:
 
93
        case PA_CONTEXT_SETTING_NAME:
 
94
                break;
 
95
        }
 
96
}
 
97
 
 
98
static void stream_state_cb(pa_stream *s, void * userdata) {
 
99
        assert(s);
 
100
 
 
101
        switch (pa_stream_get_state(s)) {
 
102
        case PA_STREAM_READY:
 
103
        case PA_STREAM_FAILED:
 
104
        case PA_STREAM_TERMINATED:
 
105
                signal_mainloop(userdata);
 
106
 
 
107
        case PA_STREAM_UNCONNECTED:
 
108
        case PA_STREAM_CREATING:
 
109
                break;
 
110
        }
 
111
}
 
112
 
 
113
static void stream_latency_update_cb(pa_stream *s, void *userdata) {
 
114
        signal_mainloop(userdata);
 
115
}
 
116
 
 
117
static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
 
118
        signal_mainloop(userdata);
 
119
}
 
120
 
 
121
static void drain_result_cb(pa_stream *s, int success, void *userdata) {
 
122
        xmms_pulse *p = userdata;
 
123
        assert(s);
 
124
        assert(p);
 
125
 
 
126
        p->operation_success = success;
 
127
        signal_mainloop(userdata);
 
128
}
 
129
 
 
130
 
 
131
/*
 
132
 * Public API.
 
133
 */
 
134
xmms_pulse *
 
135
xmms_pulse_backend_new(const char *server, const char *name,
 
136
                       int *rerror) {
 
137
        xmms_pulse *p;
 
138
        int error = PA_ERR_INTERNAL;
 
139
 
 
140
        if (server && !*server) {
 
141
                if (rerror)
 
142
                        *rerror = PA_ERR_INVALID;
 
143
                return NULL;
 
144
        }
 
145
 
 
146
        p = g_new0(xmms_pulse, 1);
 
147
        if (!p)
 
148
                return NULL;
 
149
    
 
150
        p->mainloop = pa_threaded_mainloop_new();
 
151
        if (!p->mainloop)
 
152
                goto fail;
 
153
 
 
154
        p->context = pa_context_new(pa_threaded_mainloop_get_api(p->mainloop), name);
 
155
        if (!p->context)
 
156
                goto fail;
 
157
 
 
158
        pa_context_set_state_callback(p->context, context_state_cb, p);
 
159
 
 
160
        if (pa_context_connect(p->context, server, 0, NULL) < 0) {
 
161
                error = pa_context_errno(p->context);
 
162
                goto fail;
 
163
        }
 
164
 
 
165
        pa_threaded_mainloop_lock(p->mainloop);
 
166
 
 
167
        if (pa_threaded_mainloop_start(p->mainloop) < 0)
 
168
                goto unlock_and_fail;
 
169
 
 
170
        /* Wait until the context is ready */
 
171
        pa_threaded_mainloop_wait(p->mainloop);
 
172
 
 
173
        if (pa_context_get_state(p->context) != PA_CONTEXT_READY) {
 
174
                error = pa_context_errno(p->context);
 
175
                goto unlock_and_fail;
 
176
        }
 
177
 
 
178
        pa_threaded_mainloop_unlock(p->mainloop);
 
179
        return p;
 
180
 
 
181
 unlock_and_fail:
 
182
        pa_threaded_mainloop_unlock(p->mainloop);
 
183
 fail:
 
184
        if (rerror)
 
185
                *rerror = error;
 
186
        xmms_pulse_backend_free(p);
 
187
        return NULL;
 
188
}
 
189
 
 
190
 
 
191
void xmms_pulse_backend_free(xmms_pulse *p) {
 
192
        assert(p);
 
193
 
 
194
        if (p->stream)
 
195
                xmms_pulse_backend_close_stream(p);
 
196
        if (p->mainloop)
 
197
                pa_threaded_mainloop_stop(p->mainloop);
 
198
        if (p->context)
 
199
                pa_context_unref(p->context);
 
200
        if (p->mainloop)
 
201
                pa_threaded_mainloop_free(p->mainloop);
 
202
 
 
203
        g_free(p);
 
204
}
 
205
 
 
206
 
 
207
gboolean xmms_pulse_backend_set_stream(xmms_pulse *p, const char *stream_name,
 
208
                                       const char *sink,
 
209
                                       xmms_sample_format_t format,
 
210
                                       int samplerate, int channels,
 
211
                                       int *rerror) {
 
212
        pa_sample_format_t pa_format = PA_SAMPLE_INVALID;
 
213
        int error = PA_ERR_INTERNAL;
 
214
        int ret;
 
215
        int i;
 
216
        assert(p);
 
217
 
 
218
        /* Convert the XMMS2 sample format to the pulse format. */
 
219
        for (i = 0; i < G_N_ELEMENTS(xmms_pulse_formats); i++) {
 
220
                if (xmms_pulse_formats[i].xmms_fmt == format) {
 
221
                        pa_format = xmms_pulse_formats[i].pulse_fmt;
 
222
                        break;
 
223
                }
 
224
        }
 
225
        g_return_val_if_fail (pa_format != PA_SAMPLE_INVALID, FALSE);
 
226
 
 
227
        /* If there is an existing stream, check to see if it can do the
 
228
         * job. */
 
229
        if (p->stream && p->sample_spec.format == pa_format &&
 
230
            p->sample_spec.rate == samplerate &&
 
231
            p->sample_spec.channels == channels)
 
232
                return TRUE;
 
233
 
 
234
        /* The existing stream needs to be shut down. */
 
235
        if (p->stream)
 
236
                xmms_pulse_backend_close_stream(p);
 
237
 
 
238
        pa_threaded_mainloop_lock(p->mainloop);
 
239
 
 
240
 
 
241
        /* Configure the new stream. */
 
242
        p->sample_spec.format = pa_format;
 
243
        p->sample_spec.rate = samplerate;
 
244
        p->sample_spec.channels = channels;
 
245
        pa_channel_map_init_auto(&p->channel_map, channels, PA_CHANNEL_MAP_DEFAULT);
 
246
 
 
247
        /* Create and set up the new stream. */
 
248
        p->stream = pa_stream_new(p->context, stream_name, &p->sample_spec, &p->channel_map);
 
249
        if (!p->stream) {
 
250
                error = pa_context_errno(p->context);
 
251
                goto unlock_and_fail;
 
252
        }
 
253
 
 
254
        pa_stream_set_state_callback(p->stream, stream_state_cb, p);
 
255
        pa_stream_set_write_callback(p->stream, stream_request_cb, p);
 
256
        pa_stream_set_latency_update_callback(p->stream, stream_latency_update_cb, p);
 
257
 
 
258
        ret = pa_stream_connect_playback(
 
259
                p->stream, sink, NULL,
 
260
                PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE,
 
261
                NULL, NULL);
 
262
 
 
263
        if (ret < 0) {
 
264
                error = pa_context_errno(p->context);
 
265
                goto unlock_and_fail;
 
266
        }
 
267
 
 
268
        /* Wait until the stream is ready */
 
269
        while (pa_stream_get_state(p->stream) == PA_STREAM_CREATING) {
 
270
                pa_threaded_mainloop_wait(p->mainloop);
 
271
        }               
 
272
        if (pa_stream_get_state(p->stream) != PA_STREAM_READY) {
 
273
                error = pa_context_errno(p->context);
 
274
                goto unlock_and_fail;
 
275
        }
 
276
 
 
277
        pa_threaded_mainloop_unlock(p->mainloop);
 
278
        return TRUE;
 
279
 
 
280
 unlock_and_fail:
 
281
        pa_threaded_mainloop_unlock(p->mainloop);
 
282
        if (rerror)
 
283
                *rerror = error;
 
284
        if (p->stream)
 
285
                pa_stream_unref(p->stream);
 
286
        p->stream = NULL;
 
287
        return FALSE;
 
288
}
 
289
 
 
290
 
 
291
void xmms_pulse_backend_close_stream(xmms_pulse *p)
 
292
{
 
293
        assert(p);
 
294
 
 
295
        pa_threaded_mainloop_lock(p->mainloop);
 
296
 
 
297
        /* We're killing it anyway, sod errors. */
 
298
        xmms_pulse_backend_drain(p, NULL);
 
299
 
 
300
        pa_stream_disconnect(p->stream);
 
301
        pa_stream_unref(p->stream);
 
302
        p->stream = NULL;
 
303
 
 
304
        pa_threaded_mainloop_unlock(p->mainloop);
 
305
}
 
306
 
 
307
gboolean xmms_pulse_backend_write(xmms_pulse *p, const char *data,
 
308
                                  size_t length, int *rerror)
 
309
{
 
310
        assert(p);
 
311
 
 
312
        if (!data || !length) {
 
313
                if (rerror)
 
314
                        *rerror = PA_ERR_INVALID;
 
315
                return FALSE;
 
316
        }
 
317
 
 
318
        pa_threaded_mainloop_lock(p->mainloop);
 
319
        if (!check_pulse_health(p, rerror))
 
320
                goto unlock_and_fail;
 
321
 
 
322
        while (length > 0) {
 
323
                size_t buf_len;
 
324
                int ret;
 
325
 
 
326
                while (!(buf_len = pa_stream_writable_size(p->stream))) {
 
327
                        pa_threaded_mainloop_wait(p->mainloop);
 
328
                        if (!check_pulse_health(p, rerror))
 
329
                                goto unlock_and_fail;
 
330
                }
 
331
 
 
332
                if (buf_len == (size_t)-1) {
 
333
                        if (rerror)
 
334
                                *rerror = pa_context_errno((p)->context);
 
335
                        goto unlock_and_fail;
 
336
                }
 
337
                if (buf_len > length)
 
338
                        buf_len = length;
 
339
 
 
340
                ret = pa_stream_write(p->stream, data, buf_len, NULL, 0, PA_SEEK_RELATIVE);
 
341
                if (ret < 0) {
 
342
                        if (rerror)
 
343
                                *rerror = pa_context_errno((p)->context);
 
344
                        goto unlock_and_fail;
 
345
                }
 
346
 
 
347
                data += buf_len;
 
348
                length -= buf_len;
 
349
        }
 
350
 
 
351
        pa_threaded_mainloop_unlock(p->mainloop);
 
352
        return TRUE;
 
353
 
 
354
 unlock_and_fail:
 
355
        pa_threaded_mainloop_unlock(p->mainloop);
 
356
        return FALSE;
 
357
}
 
358
 
 
359
 
 
360
gboolean xmms_pulse_backend_drain(xmms_pulse *p, int *rerror) {
 
361
        pa_operation *o = NULL;
 
362
        assert(p);
 
363
 
 
364
        if (!check_pulse_health(p, rerror))
 
365
                goto unlock_and_fail;
 
366
 
 
367
        o = pa_stream_drain(p->stream, drain_result_cb, p);
 
368
        if (!o) {
 
369
                if (rerror)
 
370
                        *rerror = pa_context_errno((p)->context);
 
371
                goto unlock_and_fail;
 
372
        }
 
373
 
 
374
        p->operation_success = 0;
 
375
        while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
 
376
                pa_threaded_mainloop_wait(p->mainloop);
 
377
                if (!check_pulse_health(p, rerror))
 
378
                        goto unlock_and_fail;
 
379
        }
 
380
        pa_operation_unref(o);
 
381
        o = NULL;
 
382
        if (!p->operation_success) {
 
383
                if (rerror)
 
384
                        *rerror = pa_context_errno((p)->context);
 
385
                goto unlock_and_fail;
 
386
        }
 
387
 
 
388
        return TRUE;
 
389
 
 
390
 unlock_and_fail:
 
391
        if (o) {
 
392
                pa_operation_cancel(o);
 
393
                pa_operation_unref(o);
 
394
        }
 
395
 
 
396
        return FALSE;
 
397
}
 
398
 
 
399
 
 
400
gboolean xmms_pulse_backend_flush(xmms_pulse *p, int *rerror) {
 
401
        pa_operation *o;
 
402
 
 
403
        pa_threaded_mainloop_lock(p->mainloop);
 
404
        if (!check_pulse_health(p, rerror))
 
405
                goto unlock_and_fail;
 
406
 
 
407
        o = pa_stream_flush(p->stream, drain_result_cb, p);
 
408
        if (!o) {
 
409
                if (rerror)
 
410
                        *rerror = pa_context_errno((p)->context);
 
411
                goto unlock_and_fail;
 
412
        }
 
413
 
 
414
        p->operation_success = 0;
 
415
        while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
 
416
                pa_threaded_mainloop_wait(p->mainloop);
 
417
                if (!check_pulse_health(p, rerror))
 
418
                        goto unlock_and_fail;
 
419
        }
 
420
        pa_operation_unref(o);
 
421
        o = NULL;
 
422
        if (!p->operation_success) {
 
423
                if (rerror)
 
424
                        *rerror = pa_context_errno((p)->context);
 
425
                goto unlock_and_fail;
 
426
        }
 
427
 
 
428
        pa_threaded_mainloop_unlock(p->mainloop);
 
429
        return 0;
 
430
    
 
431
 unlock_and_fail:
 
432
        if (o) {
 
433
                pa_operation_cancel(o);
 
434
                pa_operation_unref(o);
 
435
        }
 
436
    
 
437
        pa_threaded_mainloop_unlock(p->mainloop);
 
438
        return -1;
 
439
}
 
440
 
 
441
 
 
442
int xmms_pulse_backend_get_latency(xmms_pulse *p, int *rerror) {
 
443
        pa_usec_t t;
 
444
        int negative, r;
 
445
        assert(p);
 
446
 
 
447
        pa_threaded_mainloop_lock(p->mainloop);
 
448
 
 
449
        while (1) {
 
450
                if (!check_pulse_health(p, rerror))
 
451
                        goto unlock_and_fail;
 
452
 
 
453
                if (pa_stream_get_latency(p->stream, &t, &negative) >= 0)
 
454
                        break;
 
455
 
 
456
                r = pa_context_errno(p->context);
 
457
                if (r != PA_ERR_NODATA) {
 
458
                        if (rerror)
 
459
                                *rerror = r;
 
460
                        goto unlock_and_fail;
 
461
                }
 
462
                /* Wait until latency data is available again */
 
463
                pa_threaded_mainloop_wait(p->mainloop);
 
464
        }
 
465
 
 
466
        pa_threaded_mainloop_unlock(p->mainloop);
 
467
 
 
468
        return negative ? 0 : t;
 
469
 
 
470
 unlock_and_fail:
 
471
        pa_threaded_mainloop_unlock(p->mainloop);
 
472
        return -1;
 
473
}