summaryrefslogtreecommitdiffstats
path: root/polyp/pcm_polyp.c
blob: e3ea5067d8e4138d9dfcc1fd189297c38e8a328c (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
 * ALSA <-> Polypaudio PCM I/O plugin
 *
 * Copyright (c) 2006 by Pierre Ossman <ossman@cendio.se>
 *
 * 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.1 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <stdio.h>
#include <sys/poll.h>

#include <alsa/asoundlib.h>
#include <alsa/pcm_external.h>

#include "polyp.h"

typedef struct snd_pcm_polyp {
	snd_pcm_ioplug_t io;

    snd_polyp_t *p;

    char *device;

    /* Since ALSA expects a ring buffer we must do some voodoo. */
    size_t last_size;
    size_t ptr;

    size_t offset;

    pa_stream *stream;

    pa_sample_spec ss;
    unsigned int frame_size;
    pa_buffer_attr buffer_attr;
} snd_pcm_polyp_t;

static void update_ptr(snd_pcm_polyp_t *pcm)
{
    size_t size;

    if (pcm->io.stream == SND_PCM_STREAM_PLAYBACK)
        size = pa_stream_writable_size(pcm->stream);
    else
        size = pa_stream_readable_size(pcm->stream) - pcm->offset;

    if (size > pcm->last_size) {
        pcm->ptr += size - pcm->last_size;
        pcm->ptr %= pcm->buffer_attr.maxlength;
    }

    pcm->last_size = size;
}

static int polyp_start(snd_pcm_ioplug_t *io)
{
	snd_pcm_polyp_t *pcm = io->private_data;
	pa_operation *o;
	int err;

    assert(pcm && pcm->p && pcm->stream);

    err = polyp_finish_poll(pcm->p);
    if (err < 0)
        return err;

    err = polyp_check_connection(pcm->p);
    if (err < 0)
        return err;

    o = pa_stream_cork(pcm->stream, 0, NULL, NULL);
    assert(o);

    err = polyp_wait_operation(pcm->p, o);

    pa_operation_unref(o);

    if (err < 0)
        return -EIO;

	return 0;
}

static int polyp_stop(snd_pcm_ioplug_t *io)
{
	snd_pcm_polyp_t *pcm = io->private_data;
	pa_operation *o;
	int err;

    assert(pcm && pcm->p && pcm->stream);

    err = polyp_finish_poll(pcm->p);
    if (err < 0)
        return err;

    err = polyp_check_connection(pcm->p);
    if (err < 0)
        return err;

    o = pa_stream_flush(pcm->stream, NULL, NULL);
    assert(o);

    err = polyp_wait_operation(pcm->p, o);

    pa_operation_unref(o);

    if (err < 0)
        return -EIO;

    o = pa_stream_cork(pcm->stream, 1, NULL, NULL);
    assert(o);

    err = polyp_wait_operation(pcm->p, o);

    pa_operation_unref(o);

    if (err < 0)
        return -EIO;

	return 0;
}

int polyp_drain(snd_pcm_ioplug_t *io)
{
    snd_pcm_polyp_t *pcm = io->private_data;
	pa_operation *o;
	int err;

    assert(pcm && pcm->p && pcm->stream);

    err = polyp_finish_poll(pcm->p);
    if (err < 0)
        return err;

    err = polyp_check_connection(pcm->p);
    if (err < 0)
        return err;

    o = pa_stream_drain(pcm->stream, NULL, NULL);
    assert(o);

    err = polyp_wait_operation(pcm->p, o);

    pa_operation_unref(o);

    if (err < 0)
        return -EIO;

    return 0;
}

static snd_pcm_sframes_t polyp_pointer(snd_pcm_ioplug_t *io)
{
	snd_pcm_polyp_t *pcm = io->private_data;
	int err;

    assert(pcm && pcm->p && pcm->stream);

    err = polyp_finish_poll(pcm->p);
    if (err < 0)
        return err;

    err = polyp_check_connection(pcm->p);
    if (err < 0)
        return err;

    update_ptr(pcm);

    err = polyp_start_poll(pcm->p);
    if (err < 0)
        return err;

	return snd_pcm_bytes_to_frames(io->pcm, pcm->ptr);
}

static snd_pcm_sframes_t polyp_write(snd_pcm_ioplug_t *io,
                                     const snd_pcm_channel_area_t *areas,
                                     snd_pcm_uframes_t offset,
                                     snd_pcm_uframes_t size)
{
	snd_pcm_polyp_t *pcm = io->private_data;
	const char *buf;
	int err;

    assert(pcm && pcm->p && pcm->stream);

    err = polyp_finish_poll(pcm->p);
    if (err < 0)
        return err;

    err = polyp_check_connection(pcm->p);
    if (err < 0)
        return err;

    /* Make sure the buffer pointer is in sync */
    update_ptr(pcm);

    assert(pcm->last_size >= (size * pcm->frame_size));

	buf = (char *)areas->addr + (areas->first + areas->step * offset) / 8;

    pa_stream_write(pcm->stream, buf, size * pcm->frame_size, NULL, 0);

    /* Make sure the buffer pointer is in sync */
    update_ptr(pcm);

    return size;
}

static snd_pcm_sframes_t polyp_read(snd_pcm_ioplug_t *io,
                                    const snd_pcm_channel_area_t *areas,
                                    snd_pcm_uframes_t offset,
                                    snd_pcm_uframes_t size)
{
	snd_pcm_polyp_t *pcm = io->private_data;
	void *dst_buf, *src_buf;
	size_t remain_size, frag_length;
	int err;

    assert(pcm && pcm->p && pcm->stream);

    err = polyp_finish_poll(pcm->p);
    if (err < 0)
        return err;

    err = polyp_check_connection(pcm->p);
    if (err < 0)
        return err;

    /* Make sure the buffer pointer is in sync */
    update_ptr(pcm);

    remain_size = size * pcm->frame_size;

	dst_buf = (char *)areas->addr + (areas->first + areas->step * offset) / 8;
    while (remain_size > 0) {
        pa_stream_peek(pcm->stream, (void**)&src_buf, &frag_length);
        if (frag_length == 0)
            break;

        src_buf = (char*)src_buf + pcm->offset;
        frag_length -= pcm->offset;

        if (frag_length > remain_size) {
            pcm->offset += remain_size;
            frag_length = remain_size;
        } else
            pcm->offset = 0;

        memcpy(dst_buf, src_buf, frag_length);

        if (pcm->offset == 0)
            pa_stream_drop(pcm->stream);

        dst_buf = (char*)dst_buf + frag_length;
        remain_size -= frag_length;
    }

    /* Make sure the buffer pointer is in sync */
    update_ptr(pcm);

    return size - (remain_size / pcm->frame_size);
}

static int polyp_pcm_poll_descriptors_count(snd_pcm_ioplug_t *io)
{
	snd_pcm_polyp_t *pcm = io->private_data;

    assert(pcm && pcm->p);

    return polyp_poll_descriptors_count(pcm->p);
}

static int polyp_pcm_poll_descriptors(snd_pcm_ioplug_t *io, struct pollfd *pfd, unsigned int space)
{
	snd_pcm_polyp_t *pcm = io->private_data;

    assert(pcm && pcm->p);

    return polyp_poll_descriptors(pcm->p, pfd, space);
}

static int polyp_pcm_poll_revents(snd_pcm_ioplug_t *io, struct pollfd *pfd, unsigned int nfds, unsigned short *revents)
{
	snd_pcm_polyp_t *pcm = io->private_data;
	int err;

    assert(pcm && pcm->p);

    err = polyp_poll_revents(pcm->p, pfd, nfds, revents);
    if (err < 0)
        return err;

    *revents = 0;

    /*
     * Make sure we have an up-to-date value.
     */
    update_ptr(pcm);

    /*
     * ALSA thinks in periods, not bytes, samples or frames.
     */
    if (pcm->last_size >= pcm->buffer_attr.minreq) {
        if (io->stream == SND_PCM_STREAM_PLAYBACK)
            *revents |= POLLIN;
        else
            *revents |= POLLOUT;
    }

    return 0;
}

static int polyp_prepare(snd_pcm_ioplug_t *io)
{
    snd_pcm_polyp_t *pcm = io->private_data;
    int err;
    pa_stream_state_t state;

    assert(pcm && pcm->p);

    err = polyp_finish_poll(pcm->p);
    if (err < 0)
        return err;

    if (pcm->stream) {
        pa_stream_unref(pcm->stream);
        pcm->stream = NULL;
    }

    err = polyp_check_connection(pcm->p);
    if (err < 0)
        return err;

    assert(pcm->stream == NULL);

    if (io->stream == SND_PCM_STREAM_PLAYBACK)
        pcm->stream = pa_stream_new(pcm->p->context, "ALSA Playback", &pcm->ss, NULL);
    else
        pcm->stream = pa_stream_new(pcm->p->context, "ALSA Capture", &pcm->ss, NULL);
    assert(pcm->stream);

    if (io->stream == SND_PCM_STREAM_PLAYBACK)
        pa_stream_connect_playback(pcm->stream, pcm->device, &pcm->buffer_attr, 0, NULL);
    else
        pa_stream_connect_record(pcm->stream, pcm->device, &pcm->buffer_attr, 0);

    while (1) {
        state = pa_stream_get_state(pcm->stream);

        if (state == PA_STREAM_FAILED)
            goto error;

        if (state == PA_STREAM_READY)
            break;

        err = pa_mainloop_iterate(pcm->p->mainloop, 1, NULL);
        if (err < 0)
            return -EIO;
    }

    pcm->last_size = 0;
    pcm->ptr = 0;
    pcm->offset = 0;

    return 0;

error:
    fprintf(stderr, "*** POLYPAUDIO: Unable to create stream.\n");
    pa_stream_unref(pcm->stream);
    pcm->stream = NULL;
    return -EIO;
}

static int polyp_hw_params(snd_pcm_ioplug_t *io, snd_pcm_hw_params_t *params)
{
    snd_pcm_polyp_t *pcm = io->private_data;

    assert(pcm && pcm->p && !pcm->stream);

    pcm->frame_size = (snd_pcm_format_physical_width(io->format) * io->channels) / 8;

    switch (io->format) {
    case SND_PCM_FORMAT_U8:
        pcm->ss.format = PA_SAMPLE_U8;
        break;
    case SND_PCM_FORMAT_A_LAW:
        pcm->ss.format = PA_SAMPLE_ALAW;
        break;
    case SND_PCM_FORMAT_MU_LAW:
        pcm->ss.format = PA_SAMPLE_ULAW;
        break;
    case SND_PCM_FORMAT_S16_LE:
        pcm->ss.format = PA_SAMPLE_S16LE;
        break;
    case SND_PCM_FORMAT_S16_BE:
        pcm->ss.format = PA_SAMPLE_S16BE;
        break;
    case SND_PCM_FORMAT_FLOAT_LE:
        pcm->ss.format = PA_SAMPLE_FLOAT32LE;
        break;
    case SND_PCM_FORMAT_FLOAT_BE:
        pcm->ss.format = PA_SAMPLE_FLOAT32BE;
        break;
    default:
        fprintf(stderr, "*** POLYPAUDIO: unsupported format %s\n",
            snd_pcm_format_name(io->format));
        return -EINVAL;
    }

    pcm->ss.rate = io->rate;
    pcm->ss.channels = io->channels;

    pcm->buffer_attr.maxlength = io->buffer_size * pcm->frame_size;
    pcm->buffer_attr.tlength = io->buffer_size * pcm->frame_size;
    pcm->buffer_attr.prebuf = io->period_size * pcm->frame_size;
    pcm->buffer_attr.minreq = io->period_size * pcm->frame_size;
    pcm->buffer_attr.fragsize = io->period_size * pcm->frame_size;

    return 0;
}

static int polyp_close(snd_pcm_ioplug_t *io)
{
	snd_pcm_polyp_t *pcm = io->private_data;

    assert(pcm);

    if (pcm->stream)
        pa_stream_unref(pcm->stream);

    if (pcm->p)
        polyp_free(pcm->p);

    if (pcm->device)
        free(pcm->device);

	free(pcm);

	return 0;
}

static snd_pcm_ioplug_callback_t polyp_playback_callback = {
	.start = polyp_start,
	.stop = polyp_stop,
    .drain = polyp_drain,
	.pointer = polyp_pointer,
	.transfer = polyp_write,
    .poll_descriptors_count = polyp_pcm_poll_descriptors_count,
    .poll_descriptors = polyp_pcm_poll_descriptors,
    .poll_revents = polyp_pcm_poll_revents,
	.prepare = polyp_prepare,
	.hw_params = polyp_hw_params,
	.close = polyp_close,
};


static snd_pcm_ioplug_callback_t polyp_capture_callback = {
	.start = polyp_start,
	.stop = polyp_stop,
	.pointer = polyp_pointer,
	.transfer = polyp_read,
    .poll_descriptors_count = polyp_pcm_poll_descriptors_count,
    .poll_descriptors = polyp_pcm_poll_descriptors,
    .poll_revents = polyp_pcm_poll_revents,
	.prepare = polyp_prepare,
	.hw_params = polyp_hw_params,
	.close = polyp_close,
};


static int polyp_hw_constraint(snd_pcm_polyp_t *pcm)
{
    snd_pcm_ioplug_t *io = &pcm->io;

    static const snd_pcm_access_t access_list[] = {
        SND_PCM_ACCESS_RW_INTERLEAVED
    };
    static const unsigned int formats[] = {
        	SND_PCM_FORMAT_U8,
        SND_PCM_FORMAT_A_LAW,
        SND_PCM_FORMAT_MU_LAW,
        SND_PCM_FORMAT_S16_LE,
        SND_PCM_FORMAT_S16_BE,
        SND_PCM_FORMAT_FLOAT_LE,
        SND_PCM_FORMAT_FLOAT_BE
    };

    int err;

    err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_ACCESS,
        ARRAY_SIZE(access_list), access_list);
    if (err < 0)
        return err;

    err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_FORMAT,
        ARRAY_SIZE(formats), formats);
    if (err < 0)
        return err;

    err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_CHANNELS,
        1, PA_CHANNELS_MAX);
    if (err < 0)
        return err;

    /* FIXME: Investigate actual min and max */
    err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_RATE,
        8000, 48000);
    if (err < 0)
        return err;

    err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_RATE,
        8000, 48000);
    if (err < 0)
        return err;

    err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_PERIOD_BYTES,
        1, 4294967295U);
    if (err < 0)
        return err;

    err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_PERIODS,
        2, 4294967295U);
    if (err < 0)
        return err;

    err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_BUFFER_BYTES,
        1, 4294967295U);
    if (err < 0)
        return err;

    return 0;
}

SND_PCM_PLUGIN_DEFINE_FUNC(polyp)
{
	snd_config_iterator_t i, next;
	const char *server = NULL;
	const char *device = NULL;
	int err;
	snd_pcm_polyp_t *pcm;

	snd_config_for_each(i, next, conf) {
		snd_config_t *n = snd_config_iterator_entry(i);
		const char *id;
		if (snd_config_get_id(n, &id) < 0)
			continue;
		if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0)
			continue;
        if (strcmp(id, "server") == 0) {
            if (snd_config_get_string(n, &server) < 0) {
                SNDERR("Invalid type for %s", id);
                return -EINVAL;
            }
            continue;
        }
        if (strcmp(id, "device") == 0) {
            if (snd_config_get_string(n, &device) < 0) {
                SNDERR("Invalid type for %s", id);
                return -EINVAL;
            }
            continue;
        }
		SNDERR("Unknown field %s", id);
		return -EINVAL;
	}

	pcm = calloc(1, sizeof(*pcm));

    if (device)
        pcm->device = strdup(device);

    pcm->p = polyp_new();
    assert(pcm->p);

    err = polyp_connect(pcm->p, server);
    if (err < 0)
        goto error;

    err = polyp_start_thread(pcm->p);
    if (err < 0)
        goto error;

	pcm->io.version = SND_PCM_IOPLUG_VERSION;
	pcm->io.name = "ALSA <-> Polypaudio PCM I/O Plugin";
	pcm->io.poll_fd = -1;
	pcm->io.poll_events = 0;
	pcm->io.mmap_rw = 0;
	pcm->io.callback = stream == SND_PCM_STREAM_PLAYBACK ?
		&polyp_playback_callback : &polyp_capture_callback;
	pcm->io.private_data = pcm;
 
	err = snd_pcm_ioplug_create(&pcm->io, name, stream, mode);
	if (err < 0)
		goto error;

    err = polyp_hw_constraint(pcm);
    if (err < 0) {
		snd_pcm_ioplug_delete(&pcm->io);
        goto error;
    }

	*pcmp = pcm->io.pcm;
	return 0;

error:
    if (pcm->p)
        polyp_free(pcm->p);

	free(pcm);

	return err;
}

SND_PCM_PLUGIN_SYMBOL(polyp);