~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-07-04 16:23:34 UTC
  • mfrom: (1.1.5 upstream) (6.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080704162334-b3esbkcapt8wbrk4
Tags: 0.5DrLecter-2ubuntu1
* Merge from debian unstable (LP: #241098), remaining changes:
  + debian/control:
    + Update Maintainer field
    + add lpia to xmms2-plugin-alsa supported architectures
    + Added liba52-0.7.4-dev to build depends
  + debian/rules: Added patch, patch-stamp and unpatch
  + changed 01_gcc4.3.patch:
    + src/include/xmmsclient/xmmsclient++/helpers.h: Added #include <climits>
* New upstream relase fixes LP: #212566, #222341

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*  XMMS2 - X Music Multiplexer System
2
 
 *  Copyright (C) 2003-2007 XMMS2 Team
 
2
 *  Copyright (C) 2003-2008 XMMS2 Team
3
3
 *
4
4
 *  PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
5
5
 *
30
30
} xmms_pulse_formats[] = {
31
31
        {XMMS_SAMPLE_FORMAT_U8, PA_SAMPLE_U8},
32
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 */
 
33
                                       but they does only work
 
34
                                       if you can be sure that
 
35
                                       WORDS_BIGENDIAN is correctly
 
36
                                       defined */
37
37
        {XMMS_SAMPLE_FORMAT_S16, PA_SAMPLE_S16LE},
38
38
        {XMMS_SAMPLE_FORMAT_FLOAT, PA_SAMPLE_FLOAT32LE},
39
39
#else
48
48
        pa_stream *stream;
49
49
        pa_sample_spec sample_spec;
50
50
        pa_channel_map channel_map;
51
 
        pa_cvolume volume;
52
51
        int operation_success;
 
52
        int volume;
53
53
};
54
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) {
 
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
58
                if ((p->context &&
59
 
                     pa_context_get_state(p->context) == PA_CONTEXT_FAILED) ||
 
59
                     pa_context_get_state (p->context) == PA_CONTEXT_FAILED) ||
60
60
                    (p->stream &&
61
 
                     pa_stream_get_state(p->stream) == PA_STREAM_FAILED)) {
 
61
                     pa_stream_get_state (p->stream) == PA_STREAM_FAILED)) {
62
62
                        if (rerror)
63
 
                                *(rerror) = pa_context_errno(p->context);
 
63
                                *(rerror) = pa_context_errno (p->context);
64
64
                } else if (rerror)
65
65
                        *(rerror) = PA_ERR_BADSTATE;
66
66
                return FALSE;
71
71
/*
72
72
 * Callbacks to handle updates from the Pulse daemon.
73
73
 */
74
 
static void signal_mainloop(void *userdata) {
 
74
static void signal_mainloop (void *userdata) {
75
75
        xmms_pulse *p = userdata;
76
 
        assert(p);
 
76
        assert (p);
77
77
 
78
 
        pa_threaded_mainloop_signal(p->mainloop, 0);
 
78
        pa_threaded_mainloop_signal (p->mainloop, 0);
79
79
}
80
80
 
81
 
static void context_state_cb(pa_context *c, void *userdata) {
82
 
        assert(c);
 
81
static void context_state_cb (pa_context *c, void *userdata) {
 
82
        assert (c);
83
83
 
84
 
        switch (pa_context_get_state(c)) {
 
84
        switch (pa_context_get_state (c)) {
85
85
        case PA_CONTEXT_READY:
86
86
        case PA_CONTEXT_TERMINATED:
87
87
        case PA_CONTEXT_FAILED:
88
 
                signal_mainloop(userdata);
 
88
                signal_mainloop (userdata);
89
89
 
90
90
        case PA_CONTEXT_UNCONNECTED:
91
91
        case PA_CONTEXT_CONNECTING:
95
95
        }
96
96
}
97
97
 
98
 
static void stream_state_cb(pa_stream *s, void * userdata) {
99
 
        assert(s);
 
98
static void stream_state_cb (pa_stream *s, void * userdata) {
 
99
        assert (s);
100
100
 
101
 
        switch (pa_stream_get_state(s)) {
 
101
        switch (pa_stream_get_state (s)) {
102
102
        case PA_STREAM_READY:
103
103
        case PA_STREAM_FAILED:
104
104
        case PA_STREAM_TERMINATED:
105
 
                signal_mainloop(userdata);
 
105
                signal_mainloop (userdata);
106
106
 
107
107
        case PA_STREAM_UNCONNECTED:
108
108
        case PA_STREAM_CREATING:
110
110
        }
111
111
}
112
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) {
 
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
122
        xmms_pulse *p = userdata;
123
 
        assert(s);
124
 
        assert(p);
 
123
        assert (s);
 
124
        assert (p);
125
125
 
126
126
        p->operation_success = success;
127
 
        signal_mainloop(userdata);
 
127
        signal_mainloop (userdata);
128
128
}
129
129
 
130
130
 
132
132
 * Public API.
133
133
 */
134
134
xmms_pulse *
135
 
xmms_pulse_backend_new(const char *server, const char *name,
136
 
                       int *rerror) {
 
135
xmms_pulse_backend_new (const char *server, const char *name,
 
136
                        int *rerror) {
137
137
        xmms_pulse *p;
138
138
        int error = PA_ERR_INTERNAL;
139
139
 
143
143
                return NULL;
144
144
        }
145
145
 
146
 
        p = g_new0(xmms_pulse, 1);
 
146
        p = g_new0 (xmms_pulse, 1);
147
147
        if (!p)
148
148
                return NULL;
149
 
    
150
 
        p->mainloop = pa_threaded_mainloop_new();
 
149
 
 
150
        p->volume = 100;
 
151
 
 
152
        p->mainloop = pa_threaded_mainloop_new ();
151
153
        if (!p->mainloop)
152
154
                goto fail;
153
155
 
154
 
        p->context = pa_context_new(pa_threaded_mainloop_get_api(p->mainloop), name);
 
156
        p->context = pa_context_new (pa_threaded_mainloop_get_api (p->mainloop), name);
155
157
        if (!p->context)
156
158
                goto fail;
157
159
 
158
 
        pa_context_set_state_callback(p->context, context_state_cb, p);
 
160
        pa_context_set_state_callback (p->context, context_state_cb, p);
159
161
 
160
 
        if (pa_context_connect(p->context, server, 0, NULL) < 0) {
161
 
                error = pa_context_errno(p->context);
 
162
        if (pa_context_connect (p->context, server, 0, NULL) < 0) {
 
163
                error = pa_context_errno (p->context);
162
164
                goto fail;
163
165
        }
164
166
 
165
 
        pa_threaded_mainloop_lock(p->mainloop);
 
167
        pa_threaded_mainloop_lock (p->mainloop);
166
168
 
167
 
        if (pa_threaded_mainloop_start(p->mainloop) < 0)
 
169
        if (pa_threaded_mainloop_start (p->mainloop) < 0)
168
170
                goto unlock_and_fail;
169
171
 
170
172
        /* Wait until the context is ready */
171
 
        pa_threaded_mainloop_wait(p->mainloop);
 
173
        pa_threaded_mainloop_wait (p->mainloop);
172
174
 
173
 
        if (pa_context_get_state(p->context) != PA_CONTEXT_READY) {
174
 
                error = pa_context_errno(p->context);
 
175
        if (pa_context_get_state (p->context) != PA_CONTEXT_READY) {
 
176
                error = pa_context_errno (p->context);
175
177
                goto unlock_and_fail;
176
178
        }
177
179
 
178
 
        pa_threaded_mainloop_unlock(p->mainloop);
 
180
        pa_threaded_mainloop_unlock (p->mainloop);
179
181
        return p;
180
182
 
181
183
 unlock_and_fail:
182
 
        pa_threaded_mainloop_unlock(p->mainloop);
 
184
        pa_threaded_mainloop_unlock (p->mainloop);
183
185
 fail:
184
186
        if (rerror)
185
187
                *rerror = error;
186
 
        xmms_pulse_backend_free(p);
 
188
        xmms_pulse_backend_free (p);
187
189
        return NULL;
188
190
}
189
191
 
190
192
 
191
 
void xmms_pulse_backend_free(xmms_pulse *p) {
192
 
        assert(p);
 
193
void xmms_pulse_backend_free (xmms_pulse *p) {
 
194
        assert (p);
193
195
 
194
196
        if (p->stream)
195
 
                xmms_pulse_backend_close_stream(p);
 
197
                xmms_pulse_backend_close_stream (p);
196
198
        if (p->mainloop)
197
 
                pa_threaded_mainloop_stop(p->mainloop);
 
199
                pa_threaded_mainloop_stop (p->mainloop);
198
200
        if (p->context)
199
 
                pa_context_unref(p->context);
 
201
                pa_context_unref (p->context);
200
202
        if (p->mainloop)
201
 
                pa_threaded_mainloop_free(p->mainloop);
 
203
                pa_threaded_mainloop_free (p->mainloop);
202
204
 
203
 
        g_free(p);
 
205
        g_free (p);
204
206
}
205
207
 
206
208
 
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) {
 
209
gboolean xmms_pulse_backend_set_stream (xmms_pulse *p, const char *stream_name,
 
210
                                        const char *sink,
 
211
                                        xmms_sample_format_t format,
 
212
                                        int samplerate, int channels,
 
213
                                        int *rerror) {
212
214
        pa_sample_format_t pa_format = PA_SAMPLE_INVALID;
 
215
        pa_cvolume cvol;
213
216
        int error = PA_ERR_INTERNAL;
214
217
        int ret;
215
218
        int i;
216
 
        assert(p);
 
219
        assert (p);
217
220
 
218
221
        /* Convert the XMMS2 sample format to the pulse format. */
219
 
        for (i = 0; i < G_N_ELEMENTS(xmms_pulse_formats); i++) {
 
222
        for (i = 0; i < G_N_ELEMENTS (xmms_pulse_formats); i++) {
220
223
                if (xmms_pulse_formats[i].xmms_fmt == format) {
221
224
                        pa_format = xmms_pulse_formats[i].pulse_fmt;
222
225
                        break;
233
236
 
234
237
        /* The existing stream needs to be shut down. */
235
238
        if (p->stream)
236
 
                xmms_pulse_backend_close_stream(p);
 
239
                xmms_pulse_backend_close_stream (p);
237
240
 
238
 
        pa_threaded_mainloop_lock(p->mainloop);
 
241
        pa_threaded_mainloop_lock (p->mainloop);
239
242
 
240
243
 
241
244
        /* Configure the new stream. */
242
245
        p->sample_spec.format = pa_format;
243
246
        p->sample_spec.rate = samplerate;
244
247
        p->sample_spec.channels = channels;
245
 
        pa_channel_map_init_auto(&p->channel_map, channels, PA_CHANNEL_MAP_DEFAULT);
 
248
        pa_channel_map_init_auto (&p->channel_map, channels, PA_CHANNEL_MAP_DEFAULT);
246
249
 
247
250
        /* Create and set up the new stream. */
248
 
        p->stream = pa_stream_new(p->context, stream_name, &p->sample_spec, &p->channel_map);
 
251
        p->stream = pa_stream_new (p->context, stream_name, &p->sample_spec, &p->channel_map);
249
252
        if (!p->stream) {
250
 
                error = pa_context_errno(p->context);
 
253
                error = pa_context_errno (p->context);
251
254
                goto unlock_and_fail;
252
255
        }
253
256
 
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);
 
257
        pa_stream_set_state_callback (p->stream, stream_state_cb, p);
 
258
        pa_stream_set_write_callback (p->stream, stream_request_cb, p);
 
259
        pa_stream_set_latency_update_callback (p->stream, stream_latency_update_cb, p);
 
260
 
 
261
        pa_cvolume_set (&cvol, p->sample_spec.channels,
 
262
                        PA_VOLUME_NORM * p->volume / 100);
 
263
 
 
264
        ret = pa_stream_connect_playback (p->stream, sink, NULL,
 
265
                                          PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE,
 
266
                                          &cvol, NULL);
262
267
 
263
268
        if (ret < 0) {
264
 
                error = pa_context_errno(p->context);
 
269
                error = pa_context_errno (p->context);
265
270
                goto unlock_and_fail;
266
271
        }
267
272
 
268
273
        /* 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
        while (pa_stream_get_state (p->stream) == PA_STREAM_CREATING) {
 
275
                pa_threaded_mainloop_wait (p->mainloop);
 
276
        }
 
277
        if (pa_stream_get_state (p->stream) != PA_STREAM_READY) {
 
278
                error = pa_context_errno (p->context);
274
279
                goto unlock_and_fail;
275
280
        }
276
281
 
277
 
        pa_threaded_mainloop_unlock(p->mainloop);
 
282
        pa_threaded_mainloop_unlock (p->mainloop);
278
283
        return TRUE;
279
284
 
280
285
 unlock_and_fail:
281
 
        pa_threaded_mainloop_unlock(p->mainloop);
 
286
        pa_threaded_mainloop_unlock (p->mainloop);
282
287
        if (rerror)
283
288
                *rerror = error;
284
289
        if (p->stream)
285
 
                pa_stream_unref(p->stream);
 
290
                pa_stream_unref (p->stream);
286
291
        p->stream = NULL;
287
292
        return FALSE;
288
293
}
289
294
 
290
295
 
291
 
void xmms_pulse_backend_close_stream(xmms_pulse *p)
 
296
void xmms_pulse_backend_close_stream (xmms_pulse *p)
292
297
{
293
 
        assert(p);
 
298
        assert (p);
294
299
 
295
 
        pa_threaded_mainloop_lock(p->mainloop);
 
300
        pa_threaded_mainloop_lock (p->mainloop);
296
301
 
297
302
        /* We're killing it anyway, sod errors. */
298
 
        xmms_pulse_backend_drain(p, NULL);
 
303
        xmms_pulse_backend_drain (p, NULL);
299
304
 
300
 
        pa_stream_disconnect(p->stream);
301
 
        pa_stream_unref(p->stream);
 
305
        pa_stream_disconnect (p->stream);
 
306
        pa_stream_unref (p->stream);
302
307
        p->stream = NULL;
303
308
 
304
 
        pa_threaded_mainloop_unlock(p->mainloop);
 
309
        pa_threaded_mainloop_unlock (p->mainloop);
305
310
}
306
311
 
307
 
gboolean xmms_pulse_backend_write(xmms_pulse *p, const char *data,
308
 
                                  size_t length, int *rerror)
 
312
gboolean xmms_pulse_backend_write (xmms_pulse *p, const char *data,
 
313
                                   size_t length, int *rerror)
309
314
{
310
 
        assert(p);
 
315
        assert (p);
311
316
 
312
317
        if (!data || !length) {
313
318
                if (rerror)
315
320
                return FALSE;
316
321
        }
317
322
 
318
 
        pa_threaded_mainloop_lock(p->mainloop);
319
 
        if (!check_pulse_health(p, rerror))
 
323
        pa_threaded_mainloop_lock (p->mainloop);
 
324
        if (!check_pulse_health (p, rerror))
320
325
                goto unlock_and_fail;
321
326
 
322
327
        while (length > 0) {
323
328
                size_t buf_len;
324
329
                int ret;
325
330
 
326
 
                while (!(buf_len = pa_stream_writable_size(p->stream))) {
327
 
                        pa_threaded_mainloop_wait(p->mainloop);
328
 
                        if (!check_pulse_health(p, rerror))
 
331
                while (!(buf_len = pa_stream_writable_size (p->stream))) {
 
332
                        pa_threaded_mainloop_wait (p->mainloop);
 
333
                        if (!check_pulse_health (p, rerror))
329
334
                                goto unlock_and_fail;
330
335
                }
331
336
 
332
337
                if (buf_len == (size_t)-1) {
333
338
                        if (rerror)
334
 
                                *rerror = pa_context_errno((p)->context);
 
339
                                *rerror = pa_context_errno ((p)->context);
335
340
                        goto unlock_and_fail;
336
341
                }
337
342
                if (buf_len > length)
338
343
                        buf_len = length;
339
344
 
340
 
                ret = pa_stream_write(p->stream, data, buf_len, NULL, 0, PA_SEEK_RELATIVE);
 
345
                ret = pa_stream_write (p->stream, data, buf_len, NULL, 0, PA_SEEK_RELATIVE);
341
346
                if (ret < 0) {
342
347
                        if (rerror)
343
 
                                *rerror = pa_context_errno((p)->context);
 
348
                                *rerror = pa_context_errno ((p)->context);
344
349
                        goto unlock_and_fail;
345
350
                }
346
351
 
348
353
                length -= buf_len;
349
354
        }
350
355
 
351
 
        pa_threaded_mainloop_unlock(p->mainloop);
 
356
        pa_threaded_mainloop_unlock (p->mainloop);
352
357
        return TRUE;
353
358
 
354
359
 unlock_and_fail:
355
 
        pa_threaded_mainloop_unlock(p->mainloop);
 
360
        pa_threaded_mainloop_unlock (p->mainloop);
356
361
        return FALSE;
357
362
}
358
363
 
359
364
 
360
 
gboolean xmms_pulse_backend_drain(xmms_pulse *p, int *rerror) {
 
365
gboolean xmms_pulse_backend_drain (xmms_pulse *p, int *rerror) {
361
366
        pa_operation *o = NULL;
362
 
        assert(p);
 
367
        assert (p);
363
368
 
364
 
        if (!check_pulse_health(p, rerror))
 
369
        if (!check_pulse_health (p, rerror))
365
370
                goto unlock_and_fail;
366
371
 
367
 
        o = pa_stream_drain(p->stream, drain_result_cb, p);
 
372
        o = pa_stream_drain (p->stream, drain_result_cb, p);
368
373
        if (!o) {
369
374
                if (rerror)
370
 
                        *rerror = pa_context_errno((p)->context);
 
375
                        *rerror = pa_context_errno ((p)->context);
371
376
                goto unlock_and_fail;
372
377
        }
373
378
 
374
379
        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))
 
380
        while (pa_operation_get_state (o) != PA_OPERATION_DONE) {
 
381
                pa_threaded_mainloop_wait (p->mainloop);
 
382
                if (!check_pulse_health (p, rerror))
378
383
                        goto unlock_and_fail;
379
384
        }
380
 
        pa_operation_unref(o);
 
385
        pa_operation_unref (o);
381
386
        o = NULL;
382
387
        if (!p->operation_success) {
383
388
                if (rerror)
384
 
                        *rerror = pa_context_errno((p)->context);
 
389
                        *rerror = pa_context_errno ((p)->context);
385
390
                goto unlock_and_fail;
386
391
        }
387
392
 
389
394
 
390
395
 unlock_and_fail:
391
396
        if (o) {
392
 
                pa_operation_cancel(o);
393
 
                pa_operation_unref(o);
 
397
                pa_operation_cancel (o);
 
398
                pa_operation_unref (o);
394
399
        }
395
400
 
396
401
        return FALSE;
397
402
}
398
403
 
399
404
 
400
 
gboolean xmms_pulse_backend_flush(xmms_pulse *p, int *rerror) {
 
405
gboolean xmms_pulse_backend_flush (xmms_pulse *p, int *rerror) {
401
406
        pa_operation *o;
402
407
 
403
 
        pa_threaded_mainloop_lock(p->mainloop);
404
 
        if (!check_pulse_health(p, rerror))
 
408
        pa_threaded_mainloop_lock (p->mainloop);
 
409
        if (!check_pulse_health (p, rerror))
405
410
                goto unlock_and_fail;
406
411
 
407
 
        o = pa_stream_flush(p->stream, drain_result_cb, p);
 
412
        o = pa_stream_flush (p->stream, drain_result_cb, p);
408
413
        if (!o) {
409
414
                if (rerror)
410
 
                        *rerror = pa_context_errno((p)->context);
 
415
                        *rerror = pa_context_errno ((p)->context);
411
416
                goto unlock_and_fail;
412
417
        }
413
418
 
414
419
        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))
 
420
        while (pa_operation_get_state (o) != PA_OPERATION_DONE) {
 
421
                pa_threaded_mainloop_wait (p->mainloop);
 
422
                if (!check_pulse_health (p, rerror))
418
423
                        goto unlock_and_fail;
419
424
        }
420
 
        pa_operation_unref(o);
 
425
        pa_operation_unref (o);
421
426
        o = NULL;
422
427
        if (!p->operation_success) {
423
428
                if (rerror)
424
 
                        *rerror = pa_context_errno((p)->context);
 
429
                        *rerror = pa_context_errno ((p)->context);
425
430
                goto unlock_and_fail;
426
431
        }
427
432
 
428
 
        pa_threaded_mainloop_unlock(p->mainloop);
 
433
        pa_threaded_mainloop_unlock (p->mainloop);
429
434
        return 0;
430
 
    
 
435
 
431
436
 unlock_and_fail:
432
437
        if (o) {
433
 
                pa_operation_cancel(o);
434
 
                pa_operation_unref(o);
 
438
                pa_operation_cancel (o);
 
439
                pa_operation_unref (o);
435
440
        }
436
 
    
437
 
        pa_threaded_mainloop_unlock(p->mainloop);
 
441
 
 
442
        pa_threaded_mainloop_unlock (p->mainloop);
438
443
        return -1;
439
444
}
440
445
 
441
446
 
442
 
int xmms_pulse_backend_get_latency(xmms_pulse *p, int *rerror) {
 
447
int xmms_pulse_backend_get_latency (xmms_pulse *p, int *rerror) {
443
448
        pa_usec_t t;
444
449
        int negative, r;
445
 
        assert(p);
 
450
        assert (p);
446
451
 
447
 
        pa_threaded_mainloop_lock(p->mainloop);
 
452
        pa_threaded_mainloop_lock (p->mainloop);
448
453
 
449
454
        while (1) {
450
 
                if (!check_pulse_health(p, rerror))
 
455
                if (!check_pulse_health (p, rerror))
451
456
                        goto unlock_and_fail;
452
457
 
453
 
                if (pa_stream_get_latency(p->stream, &t, &negative) >= 0)
 
458
                if (pa_stream_get_latency (p->stream, &t, &negative) >= 0)
454
459
                        break;
455
460
 
456
 
                r = pa_context_errno(p->context);
 
461
                r = pa_context_errno (p->context);
457
462
                if (r != PA_ERR_NODATA) {
458
463
                        if (rerror)
459
464
                                *rerror = r;
460
465
                        goto unlock_and_fail;
461
466
                }
462
467
                /* Wait until latency data is available again */
463
 
                pa_threaded_mainloop_wait(p->mainloop);
 
468
                pa_threaded_mainloop_wait (p->mainloop);
464
469
        }
465
470
 
466
 
        pa_threaded_mainloop_unlock(p->mainloop);
 
471
        pa_threaded_mainloop_unlock (p->mainloop);
467
472
 
468
473
        return negative ? 0 : t;
469
474
 
470
475
 unlock_and_fail:
471
 
        pa_threaded_mainloop_unlock(p->mainloop);
 
476
        pa_threaded_mainloop_unlock (p->mainloop);
472
477
        return -1;
473
478
}
 
479
 
 
480
 
 
481
void volume_set_cb (pa_context *c, int success, void *udata) {
 
482
        int *res = (int *) udata;
 
483
        *res = success;
 
484
}
 
485
 
 
486
 
 
487
int xmms_pulse_backend_volume_set (xmms_pulse *p, unsigned int vol) {
 
488
        pa_operation *o;
 
489
        pa_cvolume cvol;
 
490
        int idx, res = 0;
 
491
 
 
492
        if (p == NULL) {
 
493
                return FALSE;
 
494
        }
 
495
 
 
496
        pa_threaded_mainloop_lock (p->mainloop);
 
497
 
 
498
        if (p->stream != NULL) {
 
499
                pa_cvolume_set (&cvol, p->sample_spec.channels,
 
500
                                PA_VOLUME_NORM * vol / 100);
 
501
 
 
502
                idx = pa_stream_get_index (p->stream);
 
503
 
 
504
                o = pa_context_set_sink_input_volume (p->context, idx, &cvol,
 
505
                                                      volume_set_cb, &res);
 
506
                if (o) {
 
507
                        /* wait for result to land */
 
508
                        while (pa_operation_get_state (o) != PA_OPERATION_DONE) {
 
509
                                pa_threaded_mainloop_wait (p->mainloop);
 
510
                        }
 
511
 
 
512
                        pa_operation_unref (o);
 
513
 
 
514
                        /* The cb set res to 1 or 0 depending on success */
 
515
                        if (res) {
 
516
                                p->volume = vol;
 
517
                        }
 
518
                }
 
519
 
 
520
        }
 
521
 
 
522
        pa_threaded_mainloop_unlock (p->mainloop);
 
523
 
 
524
        return res;
 
525
}
 
526
 
 
527
 
 
528
void volume_get_cb (pa_context *c, const pa_sink_input_info *i,
 
529
                   int eol, void *udata) {
 
530
        unsigned int *vol = (unsigned int *) udata;
 
531
        double total = 0;
 
532
        int j;
 
533
 
 
534
        if (i != NULL && i->volume.channels > 0 && *vol == -1) {
 
535
                for (j = 0; j < i->volume.channels; j++) {
 
536
                        total += i->volume.values[j] * 100.0 / PA_VOLUME_NORM;
 
537
                }
 
538
 
 
539
                *vol = (unsigned int) ceil (total / i->volume.channels);
 
540
        }
 
541
}
 
542
 
 
543
 
 
544
int xmms_pulse_backend_volume_get (xmms_pulse *p, unsigned int *vol) {
 
545
        pa_operation *o;
 
546
        int idx;
 
547
 
 
548
        if (p == NULL) {
 
549
                return FALSE;
 
550
        }
 
551
 
 
552
        pa_threaded_mainloop_lock (p->mainloop);
 
553
 
 
554
        *vol = -1;
 
555
 
 
556
        if (p->stream != NULL) {
 
557
                idx = pa_stream_get_index (p->stream);
 
558
 
 
559
                o = pa_context_get_sink_input_info (p->context, idx,
 
560
                                                    volume_get_cb, vol);
 
561
 
 
562
                if (o) {
 
563
                        while (pa_operation_get_state (o) != PA_OPERATION_DONE) {
 
564
                                pa_threaded_mainloop_wait (p->mainloop);
 
565
                        }
 
566
 
 
567
                        pa_operation_unref (o);
 
568
                }
 
569
        }
 
570
 
 
571
        pa_threaded_mainloop_unlock (p->mainloop);
 
572
 
 
573
        return *vol != -1;
 
574
}