~ubuntu-branches/ubuntu/raring/alsa-plugins/raring

« back to all changes in this revision

Viewing changes to .pc/0002-pulse-Add-fallback-option.patch/pulse/pcm_pulse.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2011-08-12 23:36:12 UTC
  • Revision ID: james.westby@ubuntu.com-20110812233612-5652bkypeumed9bp
Tags: 1.0.24-0ubuntu5
* Build with LDFLAGS=-Wl,-z,defs to prevent accidentally building modules
  with undefined symbols.
* debian/patches/arcam-av_uses_pthreads.patch: link arcam-av module
  against libpthread.
* drop upstream cherry picks 5f5cde5 and 440e791: these rely on a new
  symbol, snd_pcm_open_fallback, which is not available in the currently
  available upstream release of alsa-lib, so cherry-picking these patches
  simply made the pulseaudio module stop working altogether.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-*- linux-c -*-*/
2
 
 
3
 
/*
4
 
 * ALSA <-> PulseAudio PCM I/O plugin
5
 
 *
6
 
 * Copyright (c) 2006 by Pierre Ossman <ossman@cendio.se>
7
 
 *
8
 
 * This library is free software; you can redistribute it and/or modify
9
 
 * it under the terms of the GNU Lesser General Public License as
10
 
 * published by the Free Software Foundation; either version 2.1 of
11
 
 * the License, or (at your option) any later version.
12
 
 *
13
 
 * This program is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
 * GNU Lesser General Public License for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU Lesser General Public
19
 
 * License along with this library; if not, write to the Free Software
20
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21
 
 */
22
 
 
23
 
#include <stdio.h>
24
 
#include <sys/poll.h>
25
 
 
26
 
#include <alsa/asoundlib.h>
27
 
#include <alsa/pcm_external.h>
28
 
 
29
 
#include "pulse.h"
30
 
 
31
 
typedef struct snd_pcm_pulse {
32
 
        snd_pcm_ioplug_t io;
33
 
 
34
 
        snd_pulse_t *p;
35
 
 
36
 
        char *device;
37
 
 
38
 
        /* Since ALSA expects a ring buffer we must do some voodoo. */
39
 
        size_t last_size;
40
 
        size_t ptr;
41
 
        int underrun;
42
 
        int handle_underrun;
43
 
 
44
 
        size_t offset;
45
 
 
46
 
        pa_stream *stream;
47
 
 
48
 
        pa_sample_spec ss;
49
 
        size_t frame_size;
50
 
        pa_buffer_attr buffer_attr;
51
 
} snd_pcm_pulse_t;
52
 
 
53
 
static int check_stream(snd_pcm_pulse_t *pcm)
54
 
{
55
 
        int err;
56
 
        pa_stream_state_t state;
57
 
 
58
 
        assert(pcm);
59
 
 
60
 
        if (!pcm->p)
61
 
                return -EBADFD;
62
 
 
63
 
        err = pulse_check_connection(pcm->p);
64
 
        if (err < 0)
65
 
                return err;
66
 
 
67
 
        if (!pcm->stream)
68
 
                return -EBADFD;
69
 
 
70
 
        state = pa_stream_get_state(pcm->stream);
71
 
        if (!PA_STREAM_IS_GOOD(state))
72
 
                return -EIO;
73
 
 
74
 
        err = 0;
75
 
 
76
 
        return err;
77
 
}
78
 
 
79
 
static int update_ptr(snd_pcm_pulse_t *pcm)
80
 
{
81
 
        size_t size;
82
 
 
83
 
        if (pcm->io.stream == SND_PCM_STREAM_PLAYBACK)
84
 
                size = pa_stream_writable_size(pcm->stream);
85
 
        else
86
 
                size = pa_stream_readable_size(pcm->stream);
87
 
 
88
 
        if (size == (size_t) -1)
89
 
                return -EIO;
90
 
 
91
 
        if (pcm->io.stream == SND_PCM_STREAM_CAPTURE)
92
 
                size -= pcm->offset;
93
 
 
94
 
        /* Prevent accidental overrun of the fake ringbuffer */
95
 
        if (size > pcm->buffer_attr.tlength - pcm->frame_size)
96
 
                size = pcm->buffer_attr.tlength - pcm->frame_size;
97
 
 
98
 
        if (size > pcm->last_size) {
99
 
                pcm->ptr += size - pcm->last_size;
100
 
                pcm->ptr %= pcm->buffer_attr.tlength;
101
 
        }
102
 
 
103
 
        pcm->last_size = size;
104
 
        return 0;
105
 
  }
106
 
 
107
 
static int check_active(snd_pcm_pulse_t *pcm) {
108
 
        assert(pcm);
109
 
 
110
 
        /*
111
 
         * ALSA thinks in periods, not bytes, samples or frames.
112
 
         */
113
 
 
114
 
        if (pcm->io.stream == SND_PCM_STREAM_PLAYBACK) {
115
 
                size_t wsize;
116
 
 
117
 
                wsize = pa_stream_writable_size(pcm->stream);
118
 
 
119
 
                if (wsize == (size_t) -1)
120
 
                        return -EIO;
121
 
 
122
 
                return wsize >= pcm->buffer_attr.minreq;
123
 
        } else {
124
 
                size_t rsize;
125
 
 
126
 
                rsize = pa_stream_readable_size(pcm->stream);
127
 
 
128
 
                if (rsize == (size_t) -1)
129
 
                        return -EIO;
130
 
 
131
 
                return rsize >= pcm->buffer_attr.fragsize;
132
 
        }
133
 
}
134
 
 
135
 
static int update_active(snd_pcm_pulse_t *pcm) {
136
 
        int ret;
137
 
 
138
 
        assert(pcm);
139
 
 
140
 
        if (!pcm->p)
141
 
                return -EBADFD;
142
 
 
143
 
        ret = check_stream(pcm);
144
 
        if (ret < 0)
145
 
                goto finish;
146
 
 
147
 
        ret = check_active(pcm);
148
 
 
149
 
finish:
150
 
 
151
 
        if (ret != 0) /* On error signal the caller, too */
152
 
                pulse_poll_activate(pcm->p);
153
 
        else
154
 
                pulse_poll_deactivate(pcm->p);
155
 
 
156
 
        return ret;
157
 
}
158
 
 
159
 
static int wait_stream_state(snd_pcm_pulse_t *pcm, pa_stream_state_t target)
160
 
{
161
 
        pa_stream_state_t state;
162
 
 
163
 
        assert(pcm);
164
 
 
165
 
        if (!pcm->p)
166
 
                return -EBADFD;
167
 
 
168
 
        for (;;) {
169
 
                int err;
170
 
 
171
 
                err = pulse_check_connection(pcm->p);
172
 
                if (err < 0)
173
 
                        return err;
174
 
 
175
 
                if (!pcm->stream)
176
 
                        return -EBADFD;
177
 
 
178
 
                state = pa_stream_get_state(pcm->stream);
179
 
                if (state == target)
180
 
                        break;
181
 
 
182
 
                if (!PA_STREAM_IS_GOOD(state))
183
 
                        return -EIO;
184
 
 
185
 
                pa_threaded_mainloop_wait(pcm->p->mainloop);
186
 
        }
187
 
 
188
 
        return 0;
189
 
}
190
 
 
191
 
static void stream_success_cb(pa_stream * p, int success, void *userdata)
192
 
{
193
 
        snd_pcm_pulse_t *pcm = userdata;
194
 
 
195
 
        assert(pcm);
196
 
 
197
 
        if (!pcm->p)
198
 
                return;
199
 
 
200
 
        pa_threaded_mainloop_signal(pcm->p->mainloop, 0);
201
 
}
202
 
 
203
 
static int pulse_start(snd_pcm_ioplug_t * io)
204
 
{
205
 
        snd_pcm_pulse_t *pcm = io->private_data;
206
 
        pa_operation *o, *u;
207
 
        int err = 0, err_o = 0, err_u = 0;
208
 
 
209
 
        assert(pcm);
210
 
 
211
 
        if (!pcm->p || !pcm->p->mainloop)
212
 
                return -EBADFD;
213
 
 
214
 
        pa_threaded_mainloop_lock(pcm->p->mainloop);
215
 
 
216
 
        err = check_stream(pcm);
217
 
        if (err < 0)
218
 
                goto finish;
219
 
 
220
 
        o = pa_stream_cork(pcm->stream, 0, stream_success_cb, pcm);
221
 
        if (!o) {
222
 
                err = -EIO;
223
 
                goto finish;
224
 
        }
225
 
 
226
 
        u = pa_stream_trigger(pcm->stream, stream_success_cb, pcm);
227
 
 
228
 
        pcm->underrun = 0;
229
 
        err_o = pulse_wait_operation(pcm->p, o);
230
 
        if (u)
231
 
                err_u = pulse_wait_operation(pcm->p, u);
232
 
 
233
 
        pa_operation_unref(o);
234
 
        if (u)
235
 
                pa_operation_unref(u);
236
 
 
237
 
        if (err_o < 0 || err_u < 0) {
238
 
                err = -EIO;
239
 
                goto finish;
240
 
        }
241
 
 
242
 
finish:
243
 
        pa_threaded_mainloop_unlock(pcm->p->mainloop);
244
 
 
245
 
        return err;
246
 
}
247
 
 
248
 
static int pulse_stop(snd_pcm_ioplug_t * io)
249
 
{
250
 
        snd_pcm_pulse_t *pcm = io->private_data;
251
 
        pa_operation *o, *u;
252
 
        int err = 0, err_o = 0, err_u = 0;
253
 
 
254
 
        assert(pcm);
255
 
 
256
 
        if (!pcm->p || !pcm->p->mainloop)
257
 
                return -EBADFD;
258
 
 
259
 
        pa_threaded_mainloop_lock(pcm->p->mainloop);
260
 
 
261
 
        err = check_stream(pcm);
262
 
        if (err < 0)
263
 
                goto finish;
264
 
 
265
 
        o = pa_stream_cork(pcm->stream, 1, stream_success_cb, pcm);
266
 
        if (!o) {
267
 
                err = -EIO;
268
 
                goto finish;
269
 
        }
270
 
 
271
 
        u = pa_stream_flush(pcm->stream, stream_success_cb, pcm);
272
 
        if (!u) {
273
 
                pa_operation_unref(o);
274
 
                err = -EIO;
275
 
                goto finish;
276
 
        }
277
 
 
278
 
        err_o = pulse_wait_operation(pcm->p, o);
279
 
        err_u = pulse_wait_operation(pcm->p, u);
280
 
 
281
 
        pa_operation_unref(o);
282
 
        pa_operation_unref(u);
283
 
 
284
 
        if (err_o < 0 || err_u < 0) {
285
 
                err = -EIO;
286
 
                goto finish;
287
 
        }
288
 
 
289
 
finish:
290
 
        pa_threaded_mainloop_unlock(pcm->p->mainloop);
291
 
 
292
 
        return err;
293
 
}
294
 
 
295
 
static int pulse_drain(snd_pcm_ioplug_t * io)
296
 
{
297
 
        snd_pcm_pulse_t *pcm = io->private_data;
298
 
        pa_operation *o;
299
 
        int err = 0;
300
 
 
301
 
        assert(pcm);
302
 
 
303
 
        if (!pcm->p || !pcm->p->mainloop)
304
 
                return -EBADFD;
305
 
 
306
 
        pa_threaded_mainloop_lock(pcm->p->mainloop);
307
 
 
308
 
        err = check_stream(pcm);
309
 
        if (err < 0)
310
 
                goto finish;
311
 
 
312
 
        o = pa_stream_drain(pcm->stream, stream_success_cb, pcm);
313
 
        if (!o) {
314
 
                err = -EIO;
315
 
                goto finish;
316
 
        }
317
 
 
318
 
        err = pulse_wait_operation(pcm->p, o);
319
 
 
320
 
        pa_operation_unref(o);
321
 
 
322
 
        if (err < 0) {
323
 
                err = -EIO;
324
 
                goto finish;
325
 
        }
326
 
 
327
 
finish:
328
 
        pa_threaded_mainloop_unlock(pcm->p->mainloop);
329
 
 
330
 
        return err;
331
 
}
332
 
 
333
 
static snd_pcm_sframes_t pulse_pointer(snd_pcm_ioplug_t * io)
334
 
{
335
 
        snd_pcm_pulse_t *pcm = io->private_data;
336
 
        snd_pcm_sframes_t ret = 0;
337
 
 
338
 
        assert(pcm);
339
 
 
340
 
        if (!pcm->p || !pcm->p->mainloop)
341
 
                return -EBADFD;
342
 
 
343
 
        if (io->state == SND_PCM_STATE_XRUN)
344
 
                return -EPIPE;
345
 
 
346
 
        if (io->state != SND_PCM_STATE_RUNNING)
347
 
                return 0;
348
 
 
349
 
        pa_threaded_mainloop_lock(pcm->p->mainloop);
350
 
 
351
 
        ret = check_stream(pcm);
352
 
        if (ret < 0)
353
 
                goto finish;
354
 
 
355
 
        if (pcm->underrun) {
356
 
                ret = -EPIPE;
357
 
                goto finish;
358
 
        }
359
 
 
360
 
        ret = update_ptr(pcm);
361
 
        if (ret < 0) {
362
 
                ret = -EPIPE;
363
 
                goto finish;
364
 
        }
365
 
 
366
 
        if (pcm->underrun)
367
 
                ret = -EPIPE;
368
 
        else
369
 
                ret = snd_pcm_bytes_to_frames(io->pcm, pcm->ptr);
370
 
 
371
 
finish:
372
 
 
373
 
        pa_threaded_mainloop_unlock(pcm->p->mainloop);
374
 
 
375
 
        return ret;
376
 
}
377
 
 
378
 
static int pulse_delay(snd_pcm_ioplug_t * io, snd_pcm_sframes_t * delayp)
379
 
{
380
 
        snd_pcm_pulse_t *pcm = io->private_data;
381
 
        int err = 0;
382
 
        pa_usec_t lat = 0;
383
 
 
384
 
        assert(pcm);
385
 
 
386
 
        if (!pcm->p || !pcm->p->mainloop)
387
 
                return -EBADFD;
388
 
 
389
 
        pa_threaded_mainloop_lock(pcm->p->mainloop);
390
 
 
391
 
        for (;;) {
392
 
                err = check_stream(pcm);
393
 
                if (err < 0)
394
 
                        goto finish;
395
 
 
396
 
                err = pa_stream_get_latency(pcm->stream, &lat, NULL);
397
 
                if (err) {
398
 
                        if (err != PA_ERR_NODATA) {
399
 
                                err = -EIO;
400
 
                                goto finish;
401
 
                        }
402
 
                } else
403
 
                        break;
404
 
 
405
 
                pa_threaded_mainloop_wait(pcm->p->mainloop);
406
 
        }
407
 
 
408
 
        *delayp =
409
 
            snd_pcm_bytes_to_frames(io->pcm,
410
 
                                    pa_usec_to_bytes(lat, &pcm->ss));
411
 
 
412
 
        err = 0;
413
 
 
414
 
finish:
415
 
 
416
 
        if (pcm->underrun && pcm->io.state == SND_PCM_STATE_RUNNING)
417
 
                snd_pcm_ioplug_set_state(io, SND_PCM_STATE_XRUN);
418
 
 
419
 
        pa_threaded_mainloop_unlock(pcm->p->mainloop);
420
 
 
421
 
        return err;
422
 
}
423
 
 
424
 
static snd_pcm_sframes_t pulse_write(snd_pcm_ioplug_t * io,
425
 
                                     const snd_pcm_channel_area_t * areas,
426
 
                                     snd_pcm_uframes_t offset,
427
 
                                     snd_pcm_uframes_t size)
428
 
{
429
 
        snd_pcm_pulse_t *pcm = io->private_data;
430
 
        const char *buf;
431
 
        snd_pcm_sframes_t ret = 0;
432
 
        size_t writebytes;
433
 
 
434
 
        assert(pcm);
435
 
 
436
 
        if (!pcm->p || !pcm->p->mainloop)
437
 
                return -EBADFD;
438
 
 
439
 
        pa_threaded_mainloop_lock(pcm->p->mainloop);
440
 
 
441
 
        ret = check_stream(pcm);
442
 
        if (ret < 0)
443
 
                goto finish;
444
 
 
445
 
        /* Make sure the buffer pointer is in sync */
446
 
        ret = update_ptr(pcm);
447
 
        if (ret < 0)
448
 
                goto finish;
449
 
 
450
 
        buf =
451
 
            (char *) areas->addr + (areas->first +
452
 
                                    areas->step * offset) / 8;
453
 
 
454
 
        writebytes = size * pcm->frame_size;
455
 
        ret = pa_stream_write(pcm->stream, buf, writebytes, NULL, 0, 0);
456
 
        if (ret < 0) {
457
 
                ret = -EIO;
458
 
                goto finish;
459
 
        }
460
 
 
461
 
        /* Make sure the buffer pointer is in sync */
462
 
        pcm->last_size -= writebytes;
463
 
        ret = update_ptr(pcm);
464
 
        if (ret < 0)
465
 
                goto finish;
466
 
 
467
 
 
468
 
        ret = update_active(pcm);
469
 
        if (ret < 0)
470
 
                goto finish;
471
 
 
472
 
        ret = size;
473
 
        pcm->underrun = 0;
474
 
 
475
 
finish:
476
 
        pa_threaded_mainloop_unlock(pcm->p->mainloop);
477
 
 
478
 
        return ret;
479
 
}
480
 
 
481
 
static snd_pcm_sframes_t pulse_read(snd_pcm_ioplug_t * io,
482
 
                                    const snd_pcm_channel_area_t * areas,
483
 
                                    snd_pcm_uframes_t offset,
484
 
                                    snd_pcm_uframes_t size)
485
 
{
486
 
        snd_pcm_pulse_t *pcm = io->private_data;
487
 
        void *dst_buf;
488
 
        size_t remain_size, frag_length;
489
 
        snd_pcm_sframes_t ret = 0;
490
 
 
491
 
        assert(pcm);
492
 
 
493
 
        if (!pcm->p || !pcm->p->mainloop)
494
 
                return -EBADFD;
495
 
 
496
 
        pa_threaded_mainloop_lock(pcm->p->mainloop);
497
 
 
498
 
        ret = check_stream(pcm);
499
 
        if (ret < 0)
500
 
                goto finish;
501
 
 
502
 
        /* Make sure the buffer pointer is in sync */
503
 
        ret = update_ptr(pcm);
504
 
        if (ret < 0)
505
 
                goto finish;
506
 
 
507
 
        remain_size = size * pcm->frame_size;
508
 
 
509
 
        dst_buf =
510
 
            (char *) areas->addr + (areas->first +
511
 
                                    areas->step * offset) / 8;
512
 
        while (remain_size > 0) {
513
 
                const void *src_buf;
514
 
 
515
 
                if (pa_stream_peek(pcm->stream, &src_buf, &frag_length) < 0) {
516
 
                        ret = -EIO;
517
 
                        goto finish;
518
 
                }
519
 
 
520
 
                if (frag_length == 0)
521
 
                        break;
522
 
 
523
 
                src_buf = (char *) src_buf + pcm->offset;
524
 
                frag_length -= pcm->offset;
525
 
 
526
 
                if (frag_length > remain_size) {
527
 
                        pcm->offset += remain_size;
528
 
                        frag_length = remain_size;
529
 
                } else
530
 
                        pcm->offset = 0;
531
 
 
532
 
                memcpy(dst_buf, src_buf, frag_length);
533
 
 
534
 
                if (pcm->offset == 0)
535
 
                        pa_stream_drop(pcm->stream);
536
 
 
537
 
                dst_buf = (char *) dst_buf + frag_length;
538
 
                remain_size -= frag_length;
539
 
                pcm->last_size -= frag_length;
540
 
        }
541
 
 
542
 
        /* Make sure the buffer pointer is in sync */
543
 
        ret = update_ptr(pcm);
544
 
        if (ret < 0)
545
 
                goto finish;
546
 
 
547
 
        ret = update_active(pcm);
548
 
        if (ret < 0)
549
 
                goto finish;
550
 
 
551
 
        ret = size - (remain_size / pcm->frame_size);
552
 
 
553
 
finish:
554
 
        pa_threaded_mainloop_unlock(pcm->p->mainloop);
555
 
 
556
 
        return ret;
557
 
}
558
 
 
559
 
static void stream_state_cb(pa_stream * p, void *userdata)
560
 
{
561
 
        snd_pcm_pulse_t *pcm = userdata;
562
 
        pa_stream_state_t state;
563
 
 
564
 
        assert(pcm);
565
 
 
566
 
        if (!pcm->p)
567
 
                return;
568
 
 
569
 
        state = pa_stream_get_state(p);
570
 
        if (!PA_STREAM_IS_GOOD(state))
571
 
                pulse_poll_activate(pcm->p);
572
 
 
573
 
        pa_threaded_mainloop_signal(pcm->p->mainloop, 0);
574
 
}
575
 
 
576
 
static void stream_request_cb(pa_stream * p, size_t length, void *userdata)
577
 
{
578
 
        snd_pcm_pulse_t *pcm = userdata;
579
 
 
580
 
        assert(pcm);
581
 
 
582
 
        if (!pcm->p)
583
 
                return;
584
 
 
585
 
        update_active(pcm);
586
 
}
587
 
 
588
 
static void stream_underrun_cb(pa_stream * p, void *userdata)
589
 
{
590
 
        snd_pcm_pulse_t *pcm = userdata;
591
 
 
592
 
        assert(pcm);
593
 
 
594
 
        if (!pcm->p)
595
 
                return;
596
 
 
597
 
        pcm->underrun = 1;
598
 
}
599
 
 
600
 
static void stream_latency_cb(pa_stream *p, void *userdata) {
601
 
        snd_pcm_pulse_t *pcm = userdata;
602
 
 
603
 
        assert(pcm);
604
 
 
605
 
        if (!pcm->p)
606
 
                return;
607
 
 
608
 
        pa_threaded_mainloop_signal(pcm->p->mainloop, 0);
609
 
}
610
 
 
611
 
static int pulse_pcm_poll_revents(snd_pcm_ioplug_t * io,
612
 
                                  struct pollfd *pfd, unsigned int nfds,
613
 
                                  unsigned short *revents)
614
 
{
615
 
        int err = 0;
616
 
        snd_pcm_pulse_t *pcm = io->private_data;
617
 
 
618
 
        assert(pcm);
619
 
 
620
 
        if (!pcm->p || !pcm->p->mainloop)
621
 
                return -EBADFD;
622
 
 
623
 
        pa_threaded_mainloop_lock(pcm->p->mainloop);
624
 
 
625
 
        err = check_stream(pcm);
626
 
        if (err < 0)
627
 
                goto finish;
628
 
 
629
 
        err = check_active(pcm);
630
 
        if (err < 0)
631
 
                goto finish;
632
 
 
633
 
        if (err > 0)
634
 
                *revents = io->stream == SND_PCM_STREAM_PLAYBACK ? POLLOUT : POLLIN;
635
 
        else
636
 
                *revents = 0;
637
 
 
638
 
finish:
639
 
 
640
 
        pa_threaded_mainloop_unlock(pcm->p->mainloop);
641
 
 
642
 
        return err;
643
 
}
644
 
 
645
 
static int pulse_prepare(snd_pcm_ioplug_t * io)
646
 
{
647
 
        pa_channel_map map;
648
 
        snd_pcm_pulse_t *pcm = io->private_data;
649
 
        int err = 0, r;
650
 
        unsigned c, d;
651
 
 
652
 
        assert(pcm);
653
 
 
654
 
        if (!pcm->p || !pcm->p->mainloop)
655
 
                return -EBADFD;
656
 
 
657
 
        pa_threaded_mainloop_lock(pcm->p->mainloop);
658
 
 
659
 
        if (pcm->stream) {
660
 
                pa_stream_disconnect(pcm->stream);
661
 
                wait_stream_state(pcm, PA_STREAM_TERMINATED);
662
 
                pa_stream_unref(pcm->stream);
663
 
                pcm->stream = NULL;
664
 
        }
665
 
 
666
 
        err = pulse_check_connection(pcm->p);
667
 
        if (err < 0)
668
 
                goto finish;
669
 
 
670
 
        assert(pcm->stream == NULL);
671
 
 
672
 
        for (c = pcm->ss.channels; c > 0; c--)
673
 
                if (pa_channel_map_init_auto(&map, c, PA_CHANNEL_MAP_ALSA))
674
 
                        break;
675
 
 
676
 
        /* Extend if nessary */
677
 
        for (d = c; d < pcm->ss.channels; d++)
678
 
                map.map[d] = PA_CHANNEL_POSITION_AUX0+(d-c);
679
 
 
680
 
        map.channels = pcm->ss.channels;
681
 
 
682
 
        if (io->stream == SND_PCM_STREAM_PLAYBACK)
683
 
                pcm->stream =
684
 
                    pa_stream_new(pcm->p->context, "ALSA Playback", &pcm->ss, &map);
685
 
        else
686
 
                pcm->stream =
687
 
                    pa_stream_new(pcm->p->context, "ALSA Capture", &pcm->ss, &map);
688
 
 
689
 
        if (!pcm->stream) {
690
 
                err = -ENOMEM;
691
 
                goto finish;
692
 
        }
693
 
 
694
 
        pa_stream_set_state_callback(pcm->stream, stream_state_cb, pcm);
695
 
        pa_stream_set_latency_update_callback(pcm->stream, stream_latency_cb, pcm);
696
 
 
697
 
        if (io->stream == SND_PCM_STREAM_PLAYBACK) {
698
 
                pa_stream_set_write_callback(pcm->stream,
699
 
                                             stream_request_cb, pcm);
700
 
                if (pcm->handle_underrun)
701
 
                        pa_stream_set_underflow_callback(pcm->stream,
702
 
                                                         stream_underrun_cb, pcm);
703
 
                r = pa_stream_connect_playback(pcm->stream, pcm->device,
704
 
                                               &pcm->buffer_attr,
705
 
                                               PA_STREAM_AUTO_TIMING_UPDATE |
706
 
                                               PA_STREAM_INTERPOLATE_TIMING
707
 
#ifdef PA_STREAM_EARLY_REQUESTS
708
 
                                             | PA_STREAM_EARLY_REQUESTS
709
 
#endif
710
 
                                               , NULL, NULL);
711
 
        } else {
712
 
                pa_stream_set_read_callback(pcm->stream, stream_request_cb,
713
 
                                            pcm);
714
 
                r = pa_stream_connect_record(pcm->stream, pcm->device,
715
 
                                             &pcm->buffer_attr,
716
 
                                             PA_STREAM_AUTO_TIMING_UPDATE |
717
 
                                             PA_STREAM_INTERPOLATE_TIMING
718
 
#ifdef PA_STREAM_EARLY_REQUESTS
719
 
                                             | PA_STREAM_EARLY_REQUESTS
720
 
#endif
721
 
                        );
722
 
        }
723
 
 
724
 
        if (r < 0) {
725
 
                SNDERR("PulseAudio: Unable to create stream: %s\n", pa_strerror(pa_context_errno(pcm->p->context)));
726
 
                pa_stream_unref(pcm->stream);
727
 
                pcm->stream = NULL;
728
 
                r = -EIO;
729
 
                goto finish;
730
 
        }
731
 
 
732
 
        err = wait_stream_state(pcm, PA_STREAM_READY);
733
 
        if (err < 0) {
734
 
                SNDERR("PulseAudio: Unable to create stream: %s\n", pa_strerror(pa_context_errno(pcm->p->context)));
735
 
                pa_stream_unref(pcm->stream);
736
 
                pcm->stream = NULL;
737
 
                goto finish;
738
 
        }
739
 
 
740
 
        pcm->offset = 0;
741
 
        pcm->underrun = 0;
742
 
 
743
 
        /* Reset fake ringbuffer */
744
 
        pcm->last_size = 0;
745
 
        pcm->ptr = 0;
746
 
        update_ptr(pcm);
747
 
 
748
 
      finish:
749
 
        pa_threaded_mainloop_unlock(pcm->p->mainloop);
750
 
 
751
 
        return err;
752
 
}
753
 
 
754
 
static int pulse_hw_params(snd_pcm_ioplug_t * io,
755
 
                           snd_pcm_hw_params_t * params)
756
 
{
757
 
        snd_pcm_pulse_t *pcm = io->private_data;
758
 
        int err = 0;
759
 
 
760
 
        assert(pcm);
761
 
 
762
 
        if (!pcm->p || !pcm->p->mainloop)
763
 
                return -EBADFD;
764
 
 
765
 
        pa_threaded_mainloop_lock(pcm->p->mainloop);
766
 
 
767
 
        pcm->frame_size =
768
 
            (snd_pcm_format_physical_width(io->format) * io->channels) / 8;
769
 
 
770
 
        switch (io->format) {
771
 
        case SND_PCM_FORMAT_U8:
772
 
                pcm->ss.format = PA_SAMPLE_U8;
773
 
                break;
774
 
        case SND_PCM_FORMAT_A_LAW:
775
 
                pcm->ss.format = PA_SAMPLE_ALAW;
776
 
                break;
777
 
        case SND_PCM_FORMAT_MU_LAW:
778
 
                pcm->ss.format = PA_SAMPLE_ULAW;
779
 
                break;
780
 
        case SND_PCM_FORMAT_S16_LE:
781
 
                pcm->ss.format = PA_SAMPLE_S16LE;
782
 
                break;
783
 
        case SND_PCM_FORMAT_S16_BE:
784
 
                pcm->ss.format = PA_SAMPLE_S16BE;
785
 
                break;
786
 
#ifdef PA_SAMPLE_FLOAT32LE
787
 
        case SND_PCM_FORMAT_FLOAT_LE:
788
 
                pcm->ss.format = PA_SAMPLE_FLOAT32LE;
789
 
                break;
790
 
#endif
791
 
#ifdef PA_SAMPLE_FLOAT32BE
792
 
        case SND_PCM_FORMAT_FLOAT_BE:
793
 
                pcm->ss.format = PA_SAMPLE_FLOAT32BE;
794
 
                break;
795
 
#endif
796
 
#ifdef PA_SAMPLE_S32LE
797
 
        case SND_PCM_FORMAT_S32_LE:
798
 
                pcm->ss.format = PA_SAMPLE_S32LE;
799
 
                break;
800
 
#endif
801
 
#ifdef PA_SAMPLE_S32BE
802
 
        case SND_PCM_FORMAT_S32_BE:
803
 
                pcm->ss.format = PA_SAMPLE_S32BE;
804
 
                break;
805
 
#endif
806
 
        default:
807
 
                SNDERR("PulseAudio: Unsupported format %s\n",
808
 
                        snd_pcm_format_name(io->format));
809
 
                err = -EINVAL;
810
 
                goto finish;
811
 
        }
812
 
 
813
 
        pcm->ss.rate = io->rate;
814
 
        pcm->ss.channels = io->channels;
815
 
 
816
 
        pcm->buffer_attr.maxlength =
817
 
                4 * 1024 * 1024;
818
 
        pcm->buffer_attr.tlength =
819
 
                io->buffer_size * pcm->frame_size;
820
 
        pcm->buffer_attr.prebuf =
821
 
            (io->buffer_size - io->period_size) * pcm->frame_size;
822
 
        pcm->buffer_attr.minreq = io->period_size * pcm->frame_size;
823
 
        pcm->buffer_attr.fragsize = io->period_size * pcm->frame_size;
824
 
 
825
 
      finish:
826
 
        pa_threaded_mainloop_unlock(pcm->p->mainloop);
827
 
 
828
 
        return err;
829
 
}
830
 
 
831
 
static int pulse_close(snd_pcm_ioplug_t * io)
832
 
{
833
 
        snd_pcm_pulse_t *pcm = io->private_data;
834
 
 
835
 
        assert(pcm);
836
 
 
837
 
        if (pcm->p && pcm->p->mainloop) {
838
 
 
839
 
                pa_threaded_mainloop_lock(pcm->p->mainloop);
840
 
 
841
 
                if (pcm->stream) {
842
 
                        pa_stream_disconnect(pcm->stream);
843
 
                        pa_stream_unref(pcm->stream);
844
 
                }
845
 
 
846
 
                pa_threaded_mainloop_unlock(pcm->p->mainloop);
847
 
        }
848
 
 
849
 
        if (pcm->p)
850
 
                pulse_free(pcm->p);
851
 
 
852
 
        free(pcm->device);
853
 
        free(pcm);
854
 
 
855
 
        return 0;
856
 
}
857
 
 
858
 
static int pulse_pause(snd_pcm_ioplug_t * io, int enable)
859
 
{
860
 
        snd_pcm_pulse_t *pcm = io->private_data;
861
 
        int err = 0;
862
 
        pa_operation *o;
863
 
 
864
 
        assert (pcm);
865
 
 
866
 
        if (!pcm->p || !pcm->p->mainloop)
867
 
                return -EBADFD;
868
 
 
869
 
        pa_threaded_mainloop_lock(pcm->p->mainloop);
870
 
 
871
 
        err = check_stream(pcm);
872
 
        if (err < 0)
873
 
                goto finish;
874
 
 
875
 
        o = pa_stream_cork(pcm->stream, enable, NULL, NULL);
876
 
        if (o)
877
 
                pa_operation_unref(o);
878
 
        else
879
 
                err = -EIO;
880
 
 
881
 
finish:
882
 
        pa_threaded_mainloop_unlock(pcm->p->mainloop);
883
 
 
884
 
        return err;
885
 
}
886
 
 
887
 
static const snd_pcm_ioplug_callback_t pulse_playback_callback = {
888
 
        .start = pulse_start,
889
 
        .stop = pulse_stop,
890
 
        .drain = pulse_drain,
891
 
        .pointer = pulse_pointer,
892
 
        .transfer = pulse_write,
893
 
        .delay = pulse_delay,
894
 
        .poll_revents = pulse_pcm_poll_revents,
895
 
        .prepare = pulse_prepare,
896
 
        .hw_params = pulse_hw_params,
897
 
        .close = pulse_close,
898
 
        .pause = pulse_pause
899
 
};
900
 
 
901
 
 
902
 
static const snd_pcm_ioplug_callback_t pulse_capture_callback = {
903
 
        .start = pulse_start,
904
 
        .stop = pulse_stop,
905
 
        .pointer = pulse_pointer,
906
 
        .transfer = pulse_read,
907
 
        .delay = pulse_delay,
908
 
        .poll_revents = pulse_pcm_poll_revents,
909
 
        .prepare = pulse_prepare,
910
 
        .hw_params = pulse_hw_params,
911
 
        .close = pulse_close,
912
 
};
913
 
 
914
 
 
915
 
static int pulse_hw_constraint(snd_pcm_pulse_t * pcm)
916
 
{
917
 
        snd_pcm_ioplug_t *io = &pcm->io;
918
 
 
919
 
        static const snd_pcm_access_t access_list[] = {
920
 
                SND_PCM_ACCESS_RW_INTERLEAVED
921
 
        };
922
 
        static const unsigned int formats[] = {
923
 
                SND_PCM_FORMAT_U8,
924
 
                SND_PCM_FORMAT_A_LAW,
925
 
                SND_PCM_FORMAT_MU_LAW,
926
 
                SND_PCM_FORMAT_S16_LE,
927
 
                SND_PCM_FORMAT_S16_BE,
928
 
                SND_PCM_FORMAT_FLOAT_LE,
929
 
                SND_PCM_FORMAT_FLOAT_BE,
930
 
                SND_PCM_FORMAT_S32_LE,
931
 
                SND_PCM_FORMAT_S32_BE
932
 
        };
933
 
 
934
 
        int err;
935
 
 
936
 
        err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_ACCESS,
937
 
                                            ARRAY_SIZE(access_list),
938
 
                                            access_list);
939
 
        if (err < 0)
940
 
                return err;
941
 
 
942
 
        err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_FORMAT,
943
 
                                            ARRAY_SIZE(formats), formats);
944
 
        if (err < 0)
945
 
                return err;
946
 
 
947
 
        err =
948
 
            snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_CHANNELS,
949
 
                                            1, PA_CHANNELS_MAX);
950
 
        if (err < 0)
951
 
                return err;
952
 
 
953
 
        err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_RATE,
954
 
                                              1, PA_RATE_MAX);
955
 
        if (err < 0)
956
 
                return err;
957
 
 
958
 
        err =
959
 
            snd_pcm_ioplug_set_param_minmax(io,
960
 
                                            SND_PCM_IOPLUG_HW_BUFFER_BYTES,
961
 
                                            1, 4 * 1024 * 1024);
962
 
        if (err < 0)
963
 
                return err;
964
 
 
965
 
        err =
966
 
            snd_pcm_ioplug_set_param_minmax(io,
967
 
                                            SND_PCM_IOPLUG_HW_PERIOD_BYTES,
968
 
                                            128, 2 * 1024 * 1024);
969
 
        if (err < 0)
970
 
                return err;
971
 
 
972
 
        err =
973
 
            snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_PERIODS,
974
 
                                            3, 1024);
975
 
        if (err < 0)
976
 
                return err;
977
 
        return 0;
978
 
}
979
 
 
980
 
SND_PCM_PLUGIN_DEFINE_FUNC(pulse)
981
 
{
982
 
        snd_config_iterator_t i, next;
983
 
        const char *server = NULL;
984
 
        const char *device = NULL;
985
 
        int handle_underrun = 0;
986
 
        int err;
987
 
        snd_pcm_pulse_t *pcm;
988
 
 
989
 
        snd_config_for_each(i, next, conf) {
990
 
                snd_config_t *n = snd_config_iterator_entry(i);
991
 
                const char *id;
992
 
                if (snd_config_get_id(n, &id) < 0)
993
 
                        continue;
994
 
                if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0
995
 
                    || strcmp(id, "hint") == 0)
996
 
                        continue;
997
 
                if (strcmp(id, "server") == 0) {
998
 
                        if (snd_config_get_string(n, &server) < 0) {
999
 
                                SNDERR("Invalid type for %s", id);
1000
 
                                return -EINVAL;
1001
 
                        }
1002
 
                        continue;
1003
 
                }
1004
 
                if (strcmp(id, "device") == 0) {
1005
 
                        if (snd_config_get_string(n, &device) < 0) {
1006
 
                                SNDERR("Invalid type for %s", id);
1007
 
                                return -EINVAL;
1008
 
                        }
1009
 
                        continue;
1010
 
                }
1011
 
                if (strcmp(id, "handle_underrun") == 0) {
1012
 
                        if ((err = snd_config_get_bool(n)) < 0) {
1013
 
                                SNDERR("Invalid value for %s", id);
1014
 
                                return -EINVAL;
1015
 
                        }
1016
 
                        handle_underrun = err;
1017
 
                        continue;
1018
 
                }
1019
 
                SNDERR("Unknown field %s", id);
1020
 
                return -EINVAL;
1021
 
        }
1022
 
 
1023
 
        pcm = calloc(1, sizeof(*pcm));
1024
 
        if (!pcm)
1025
 
                return -ENOMEM;
1026
 
 
1027
 
        if (device) {
1028
 
                pcm->device = strdup(device);
1029
 
 
1030
 
                if (!pcm->device) {
1031
 
                        err = -ENOMEM;
1032
 
                        goto error;
1033
 
                }
1034
 
        }
1035
 
 
1036
 
        pcm->p = pulse_new();
1037
 
        if (!pcm->p) {
1038
 
                err = -EIO;
1039
 
                goto error;
1040
 
        }
1041
 
 
1042
 
        pcm->handle_underrun = handle_underrun;
1043
 
 
1044
 
        err = pulse_connect(pcm->p, server);
1045
 
        if (err < 0)
1046
 
                goto error;
1047
 
 
1048
 
        pcm->io.version = SND_PCM_IOPLUG_VERSION;
1049
 
        pcm->io.name = "ALSA <-> PulseAudio PCM I/O Plugin";
1050
 
        pcm->io.poll_fd = pcm->p->main_fd;
1051
 
        pcm->io.poll_events = POLLIN;
1052
 
        pcm->io.mmap_rw = 0;
1053
 
        pcm->io.callback = stream == SND_PCM_STREAM_PLAYBACK ?
1054
 
            &pulse_playback_callback : &pulse_capture_callback;
1055
 
        pcm->io.private_data = pcm;
1056
 
 
1057
 
        err = snd_pcm_ioplug_create(&pcm->io, name, stream, mode);
1058
 
        if (err < 0)
1059
 
                goto error;
1060
 
 
1061
 
        err = pulse_hw_constraint(pcm);
1062
 
        if (err < 0) {
1063
 
                snd_pcm_ioplug_delete(&pcm->io);
1064
 
                goto error;
1065
 
        }
1066
 
 
1067
 
        *pcmp = pcm->io.pcm;
1068
 
        return 0;
1069
 
 
1070
 
error:
1071
 
        if (pcm->p)
1072
 
                pulse_free(pcm->p);
1073
 
 
1074
 
        free(pcm->device);
1075
 
        free(pcm);
1076
 
 
1077
 
        return err;
1078
 
}
1079
 
 
1080
 
SND_PCM_PLUGIN_SYMBOL(pulse);