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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
|
/**
* \file pcm/pcm_ioplug.c
* \ingroup Plugin_SDK
* \brief I/O Plugin SDK
* \author Takashi Iwai <tiwai@suse.de>
* \date 2005
*/
/*
* PCM - External I/O Plugin SDK
* Copyright (c) 2005 by Takashi Iwai <tiwai@suse.de>
*
*
* 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 "pcm_local.h"
#include "pcm_ioplug.h"
#include "pcm_ext_parm.h"
/* hw_params */
typedef struct snd_pcm_ioplug_priv {
snd_pcm_ioplug_t *data;
struct snd_ext_parm params[SND_PCM_IOPLUG_HW_PARAMS];
unsigned int last_hw;
snd_pcm_uframes_t avail_max;
snd_htimestamp_t trigger_tstamp;
} ioplug_priv_t;
/* update the hw pointer */
static void snd_pcm_ioplug_hw_ptr_update(snd_pcm_t *pcm)
{
ioplug_priv_t *io = pcm->private_data;
snd_pcm_sframes_t hw;
hw = io->data->callback->pointer(io->data);
if (hw >= 0) {
unsigned int delta;
if ((unsigned int)hw >= io->last_hw)
delta = hw - io->last_hw;
else
delta = pcm->buffer_size + hw - io->last_hw;
io->data->hw_ptr += delta;
io->last_hw = hw;
} else
io->data->state = SNDRV_PCM_STATE_XRUN;
}
static int snd_pcm_ioplug_info(snd_pcm_t *pcm, snd_pcm_info_t *info)
{
memset(info, 0, sizeof(*info));
info->stream = pcm->stream;
info->card = -1;
if (pcm->name) {
strncpy(info->id, pcm->name, sizeof(info->id));
strncpy(info->name, pcm->name, sizeof(info->name));
strncpy(info->subname, pcm->name, sizeof(info->subname));
}
info->subdevices_count = 1;
return 0;
}
static int snd_pcm_ioplug_channel_info(snd_pcm_t *pcm, snd_pcm_channel_info_t *info)
{
return snd_pcm_channel_info_shm(pcm, info, -1);
}
static int snd_pcm_ioplug_status(snd_pcm_t *pcm, snd_pcm_status_t * status)
{
ioplug_priv_t *io = pcm->private_data;
memset(status, 0, sizeof(*status));
snd_pcm_ioplug_hw_ptr_update(pcm);
status->state = io->data->state;
status->trigger_tstamp = io->trigger_tstamp;
status->avail = snd_pcm_mmap_avail(pcm);
status->avail_max = io->avail_max;
return 0;
}
static snd_pcm_state_t snd_pcm_ioplug_state(snd_pcm_t *pcm)
{
ioplug_priv_t *io = pcm->private_data;
return io->data->state;
}
static int snd_pcm_ioplug_hwsync(snd_pcm_t *pcm)
{
snd_pcm_ioplug_hw_ptr_update(pcm);
return 0;
}
static int snd_pcm_ioplug_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp)
{
snd_pcm_ioplug_hw_ptr_update(pcm);
*delayp = snd_pcm_mmap_hw_avail(pcm);
return 0;
}
static int snd_pcm_ioplug_reset(snd_pcm_t *pcm)
{
ioplug_priv_t *io = pcm->private_data;
io->data->appl_ptr = 0;
io->data->hw_ptr = 0;
io->last_hw = 0;
io->avail_max = 0;
return 0;
}
static int snd_pcm_ioplug_prepare(snd_pcm_t *pcm)
{
ioplug_priv_t *io = pcm->private_data;
io->data->state = SND_PCM_STATE_PREPARED;
snd_pcm_ioplug_reset(pcm);
if (io->data->callback->prepare)
return io->data->callback->prepare(io->data);
return 0;
}
static int hw_params_type[SND_PCM_IOPLUG_HW_PARAMS] = {
[SND_PCM_IOPLUG_HW_ACCESS] = SND_PCM_HW_PARAM_ACCESS,
[SND_PCM_IOPLUG_HW_FORMAT] = SND_PCM_HW_PARAM_FORMAT,
[SND_PCM_IOPLUG_HW_CHANNELS] = SND_PCM_HW_PARAM_CHANNELS,
[SND_PCM_IOPLUG_HW_RATE] = SND_PCM_HW_PARAM_RATE,
[SND_PCM_IOPLUG_HW_PERIOD_BYTES] = SND_PCM_HW_PARAM_PERIOD_BYTES,
[SND_PCM_IOPLUG_HW_BUFFER_BYTES] = SND_PCM_HW_PARAM_BUFFER_BYTES,
[SND_PCM_IOPLUG_HW_PERIODS] = SND_PCM_HW_PARAM_PERIODS,
};
/* x = a * b */
static int rule_mul(snd_pcm_hw_params_t *params, int x, int a, int b)
{
snd_interval_t t;
snd_interval_mul(hw_param_interval(params, a),
hw_param_interval(params, b), &t);
return snd_interval_refine(hw_param_interval(params, x), &t);
}
/* x = a / b */
static int rule_div(snd_pcm_hw_params_t *params, int x, int a, int b)
{
snd_interval_t t;
snd_interval_div(hw_param_interval(params, a),
hw_param_interval(params, b), &t);
return snd_interval_refine(hw_param_interval(params, x), &t);
}
/* x = a * b / k */
static int rule_muldivk(snd_pcm_hw_params_t *params, int x, int a, int b, int k)
{
snd_interval_t t;
snd_interval_muldivk(hw_param_interval(params, a),
hw_param_interval(params, b), k, &t);
return snd_interval_refine(hw_param_interval(params, x), &t);
}
/* x = a * k / b */
static int rule_mulkdiv(snd_pcm_hw_params_t *params, int x, int a, int k, int b)
{
snd_interval_t t;
snd_interval_mulkdiv(hw_param_interval(params, a), k,
hw_param_interval(params, b), &t);
return snd_interval_refine(hw_param_interval(params, x), &t);
}
#if 0
static void dump_parm(snd_pcm_hw_params_t *params)
{
snd_output_t *log;
snd_output_stdio_attach(&log, stderr, 0);
snd_pcm_hw_params_dump(params, log);
snd_output_close(log);
}
#endif
/* refine *_TIME and *_SIZE, then update *_BYTES */
static int refine_time_and_size(snd_pcm_hw_params_t *params,
int time, int size, int bytes)
{
int err, change1 = 0;
/* size = time * rate / 1000000 */
err = rule_muldivk(params, size, time,
SND_PCM_HW_PARAM_RATE, 1000000);
if (err < 0)
return err;
change1 |= err;
/* bytes = size * framebits / 8 */
err = rule_muldivk(params, bytes, size,
SND_PCM_HW_PARAM_FRAME_BITS, 8);
if (err < 0)
return err;
change1 |= err;
return change1;
}
/* refine *_TIME and *_SIZE from *_BYTES */
static int refine_back_time_and_size(snd_pcm_hw_params_t *params,
int time, int size, int bytes)
{
int err;
/* size = bytes * 8 / framebits */
err = rule_mulkdiv(params, size, bytes, 8, SND_PCM_HW_PARAM_FRAME_BITS);
if (err < 0)
return err;
/* time = size * 1000000 / rate */
err = rule_mulkdiv(params, time, size, 1000000, SND_PCM_HW_PARAM_RATE);
if (err < 0)
return err;
return 0;
}
static int snd_pcm_ioplug_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
{
int change = 0, change1, change2, err;
ioplug_priv_t *io = pcm->private_data;
int i;
/* access, format */
for (i = SND_PCM_IOPLUG_HW_ACCESS; i <= SND_PCM_IOPLUG_HW_FORMAT; i++) {
err = snd_ext_parm_mask_refine(hw_param_mask(params, hw_params_type[i]),
io->params, i);
if (err < 0)
return err;
change |= err;
}
/* channels, rate */
for (; i <= SND_PCM_IOPLUG_HW_RATE; i++) {
err = snd_ext_parm_interval_refine(hw_param_interval(params, hw_params_type[i]),
io->params, i);
if (err < 0)
return err;
change |= err;
}
if (params->rmask & ((1 << SND_PCM_HW_PARAM_ACCESS) |
(1 << SND_PCM_HW_PARAM_FORMAT) |
(1 << SND_PCM_HW_PARAM_SUBFORMAT) |
(1 << SND_PCM_HW_PARAM_CHANNELS) |
(1 << SND_PCM_HW_PARAM_RATE))) {
err = snd_pcm_hw_refine_soft(pcm, params);
if (err < 0)
return err;
change |= err;
}
change1 = refine_time_and_size(params, SND_PCM_HW_PARAM_PERIOD_TIME,
SND_PCM_HW_PARAM_PERIOD_SIZE,
SND_PCM_HW_PARAM_PERIOD_BYTES);
if (change1 < 0)
return change1;
err = snd_ext_parm_interval_refine(hw_param_interval(params, SND_PCM_HW_PARAM_PERIOD_BYTES),
io->params, SND_PCM_IOPLUG_HW_PERIOD_BYTES);
if (err < 0)
return err;
change1 |= err;
if (change1) {
change |= change1;
err = refine_back_time_and_size(params, SND_PCM_HW_PARAM_PERIOD_TIME,
SND_PCM_HW_PARAM_PERIOD_SIZE,
SND_PCM_HW_PARAM_PERIOD_BYTES);
if (err < 0)
return err;
}
change1 = refine_time_and_size(params, SND_PCM_HW_PARAM_BUFFER_TIME,
SND_PCM_HW_PARAM_BUFFER_SIZE,
SND_PCM_HW_PARAM_BUFFER_BYTES);
if (change1 < 0)
return change1;
change |= change1;
do {
change2 = 0;
err = snd_ext_parm_interval_refine(hw_param_interval(params, SND_PCM_HW_PARAM_BUFFER_BYTES),
io->params, SND_PCM_IOPLUG_HW_BUFFER_BYTES);
if (err < 0)
return err;
change2 |= err;
/* periods = buffer_bytes / periods */
err = rule_div(params, SND_PCM_HW_PARAM_PERIODS,
SND_PCM_HW_PARAM_BUFFER_BYTES,
SND_PCM_HW_PARAM_PERIOD_BYTES);
if (err < 0)
return err;
change2 |= err;
err = snd_ext_parm_interval_refine(hw_param_interval(params, SND_PCM_HW_PARAM_PERIODS),
io->params, SND_PCM_IOPLUG_HW_PERIODS);
if (err < 0)
return err;
change2 |= err;
/* buffer_bytes = periods * period_bytes */
err = rule_mul(params, SND_PCM_HW_PARAM_BUFFER_BYTES,
SND_PCM_HW_PARAM_PERIOD_BYTES,
SND_PCM_HW_PARAM_PERIODS);
if (err < 0)
return err;
change2 |= err;
change1 |= change2;
} while (change2);
change |= change1;
if (change1) {
err = refine_back_time_and_size(params, SND_PCM_HW_PARAM_BUFFER_TIME,
SND_PCM_HW_PARAM_BUFFER_SIZE,
SND_PCM_HW_PARAM_BUFFER_BYTES);
if (err < 0)
return err;
}
#if 0
fprintf(stderr, "XXX\n");
dump_parm(params);
#endif
return change;
}
static int snd_pcm_ioplug_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
{
ioplug_priv_t *io = pcm->private_data;
int err;
INTERNAL(snd_pcm_hw_params_get_access)(params, &io->data->access);
INTERNAL(snd_pcm_hw_params_get_format)(params, &io->data->format);
INTERNAL(snd_pcm_hw_params_get_channels)(params, &io->data->channels);
INTERNAL(snd_pcm_hw_params_get_rate)(params, &io->data->rate, 0);
INTERNAL(snd_pcm_hw_params_get_period_size)(params, &io->data->period_size, 0);
INTERNAL(snd_pcm_hw_params_get_buffer_size)(params, &io->data->buffer_size);
if (io->data->callback->hw_params) {
err = io->data->callback->hw_params(io->data, params);
if (err < 0)
return err;
INTERNAL(snd_pcm_hw_params_get_access)(params, &io->data->access);
INTERNAL(snd_pcm_hw_params_get_format)(params, &io->data->format);
INTERNAL(snd_pcm_hw_params_get_channels)(params, &io->data->channels);
INTERNAL(snd_pcm_hw_params_get_rate)(params, &io->data->rate, 0);
INTERNAL(snd_pcm_hw_params_get_period_size)(params, &io->data->period_size, 0);
INTERNAL(snd_pcm_hw_params_get_buffer_size)(params, &io->data->buffer_size);
}
return 0;
}
static int snd_pcm_ioplug_hw_free(snd_pcm_t *pcm)
{
ioplug_priv_t *io = pcm->private_data;
if (io->data->callback->hw_free)
return io->data->callback->hw_free(io->data);
return 0;
}
static int snd_pcm_ioplug_sw_params(snd_pcm_t *pcm, snd_pcm_sw_params_t *params)
{
ioplug_priv_t *io = pcm->private_data;
if (io->data->callback->sw_params)
return io->data->callback->sw_params(io->data, params);
return 0;
}
static int snd_pcm_ioplug_start(snd_pcm_t *pcm)
{
ioplug_priv_t *io = pcm->private_data;
struct timeval tv;
int err;
if (io->data->state != SND_PCM_STATE_PREPARED)
return -EBUSY;
err = io->data->callback->start(io->data);
if (err < 0)
return err;
gettimeofday(&tv, 0);
io->trigger_tstamp.tv_sec = tv.tv_sec;
io->trigger_tstamp.tv_nsec = tv.tv_usec * 1000L;
io->data->state = SND_PCM_STATE_RUNNING;
return 0;
}
static int snd_pcm_ioplug_drop(snd_pcm_t *pcm)
{
ioplug_priv_t *io = pcm->private_data;
struct timeval tv;
if (io->data->state == SND_PCM_STATE_OPEN)
return -EBADFD;
io->data->callback->stop(io->data);
gettimeofday(&tv, 0);
io->trigger_tstamp.tv_sec = tv.tv_sec;
io->trigger_tstamp.tv_nsec = tv.tv_usec * 1000L;
io->data->state = SND_PCM_STATE_SETUP;
return 0;
}
static int snd_pcm_ioplug_drain(snd_pcm_t *pcm)
{
ioplug_priv_t *io = pcm->private_data;
if (io->data->state == SND_PCM_STATE_OPEN)
return -EBADFD;
if (io->data->callback->drain)
io->data->callback->drain(io->data);
return snd_pcm_ioplug_drop(pcm);
}
static int snd_pcm_ioplug_pause(snd_pcm_t *pcm, int enable)
{
ioplug_priv_t *io = pcm->private_data;
static snd_pcm_state_t states[2] = {
SND_PCM_STATE_PAUSED, SND_PCM_STATE_RUNNING
};
int prev, err;
prev = !enable;
enable = !prev;
if (io->data->state != states[prev])
return -EBADFD;
if (io->data->callback->pause) {
err = io->data->callback->pause(io->data, enable);
if (err < 0)
return err;
}
io->data->state = states[enable];
return 0;
}
static snd_pcm_sframes_t snd_pcm_ioplug_rewind(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
{
snd_pcm_mmap_appl_backward(pcm, frames);
return frames;
}
static snd_pcm_sframes_t snd_pcm_ioplug_forward(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
{
snd_pcm_mmap_appl_forward(pcm, frames);
return frames;
}
static int snd_pcm_ioplug_resume(snd_pcm_t *pcm)
{
ioplug_priv_t *io = pcm->private_data;
if (io->data->callback->resume)
io->data->callback->resume(io->data);
return 0;
}
static snd_pcm_sframes_t ioplug_priv_transfer_areas(snd_pcm_t *pcm,
const snd_pcm_channel_area_t *areas,
snd_pcm_uframes_t offset,
snd_pcm_uframes_t size)
{
ioplug_priv_t *io = pcm->private_data;
snd_pcm_sframes_t result;
if (! size)
return 0;
if (io->data->callback->transfer)
result = io->data->callback->transfer(io->data, areas, offset, size);
else
result = size;
if (result > 0)
snd_pcm_mmap_appl_forward(pcm, result);
return result;
}
static snd_pcm_sframes_t snd_pcm_ioplug_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size)
{
if (pcm->mmap_rw)
return snd_pcm_mmap_writei(pcm, buffer, size);
else {
snd_pcm_channel_area_t areas[pcm->channels];
snd_pcm_areas_from_buf(pcm, areas, (void*)buffer);
return snd_pcm_write_areas(pcm, areas, 0, size,
ioplug_priv_transfer_areas);
}
}
static snd_pcm_sframes_t snd_pcm_ioplug_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
{
if (pcm->mmap_rw)
return snd_pcm_mmap_writen(pcm, bufs, size);
else {
snd_pcm_channel_area_t areas[pcm->channels];
snd_pcm_areas_from_bufs(pcm, areas, bufs);
return snd_pcm_write_areas(pcm, areas, 0, size,
ioplug_priv_transfer_areas);
}
}
static snd_pcm_sframes_t snd_pcm_ioplug_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size)
{
if (pcm->mmap_rw)
return snd_pcm_mmap_readi(pcm, buffer, size);
else {
snd_pcm_channel_area_t areas[pcm->channels];
snd_pcm_areas_from_buf(pcm, areas, buffer);
return snd_pcm_read_areas(pcm, areas, 0, size,
ioplug_priv_transfer_areas);
}
}
static snd_pcm_sframes_t snd_pcm_ioplug_readn(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
{
if (pcm->mmap_rw)
return snd_pcm_mmap_readn(pcm, bufs, size);
else {
snd_pcm_channel_area_t areas[pcm->channels];
snd_pcm_areas_from_bufs(pcm, areas, bufs);
return snd_pcm_read_areas(pcm, areas, 0, size,
ioplug_priv_transfer_areas);
}
}
static snd_pcm_sframes_t snd_pcm_ioplug_mmap_commit(snd_pcm_t *pcm,
snd_pcm_uframes_t offset,
snd_pcm_uframes_t size)
{
if (pcm->stream == SND_PCM_STREAM_PLAYBACK &&
pcm->access != SND_PCM_ACCESS_RW_INTERLEAVED &&
pcm->access != SND_PCM_ACCESS_RW_NONINTERLEAVED) {
const snd_pcm_channel_area_t *areas;
snd_pcm_uframes_t ofs, frames = size;
snd_pcm_mmap_begin(pcm, &areas, &ofs, &frames);
if (ofs != offset)
return -EIO;
return ioplug_priv_transfer_areas(pcm, areas, offset, frames);
}
snd_pcm_mmap_appl_forward(pcm, size);
return size;
}
static snd_pcm_sframes_t snd_pcm_ioplug_avail_update(snd_pcm_t *pcm)
{
ioplug_priv_t *io = pcm->private_data;
snd_pcm_uframes_t avail;
snd_pcm_ioplug_hw_ptr_update(pcm);
if (pcm->stream == SND_PCM_STREAM_CAPTURE &&
pcm->access != SND_PCM_ACCESS_RW_INTERLEAVED &&
pcm->access != SND_PCM_ACCESS_RW_NONINTERLEAVED) {
if (io->data->callback->transfer) {
const snd_pcm_channel_area_t *areas;
snd_pcm_uframes_t offset, size = UINT_MAX;
snd_pcm_sframes_t result;
snd_pcm_mmap_begin(pcm, &areas, &offset, &size);
result = io->data->callback->transfer(io->data, areas, offset, size);
if (result < 0)
return result;
}
}
avail = snd_pcm_mmap_avail(pcm);
if (avail > io->avail_max)
io->avail_max = avail;
return (snd_pcm_sframes_t)avail;
}
static int snd_pcm_ioplug_nonblock(snd_pcm_t *pcm, int nonblock)
{
ioplug_priv_t *io = pcm->private_data;
io->data->nonblock = nonblock;
return 0;
}
static int snd_pcm_ioplug_poll_revents(snd_pcm_t *pcm, struct pollfd *pfds, unsigned int nfds, unsigned short *revents)
{
ioplug_priv_t *io = pcm->private_data;
if (io->data->callback->poll_revents)
return io->data->callback->poll_revents(io->data, pfds, nfds, revents);
else
*revents = pfds->revents;
return 0;
}
static int snd_pcm_ioplug_mmap(snd_pcm_t *pcm ATTRIBUTE_UNUSED)
{
return 0;
}
static int snd_pcm_ioplug_async(snd_pcm_t *pcm ATTRIBUTE_UNUSED,
int sig ATTRIBUTE_UNUSED,
pid_t pid ATTRIBUTE_UNUSED)
{
return -ENOSYS;
}
static int snd_pcm_ioplug_munmap(snd_pcm_t *pcm ATTRIBUTE_UNUSED)
{
return 0;
}
static void snd_pcm_ioplug_dump(snd_pcm_t *pcm, snd_output_t *out)
{
ioplug_priv_t *io = pcm->private_data;
if (io->data->callback->dump)
io->data->callback->dump(io->data, out);
else {
if (io->data->name)
snd_output_printf(out, "%s\n", io->data->name);
else
snd_output_printf(out, "IO-PCM Plugin\n");
if (pcm->setup) {
snd_output_printf(out, "Its setup is:\n");
snd_pcm_dump_setup(pcm, out);
}
}
}
static void clear_io_params(ioplug_priv_t *io)
{
int i;
for (i = 0; i < SND_PCM_IOPLUG_HW_PARAMS; i++)
snd_ext_parm_clear(&io->params[i]);
}
static int snd_pcm_ioplug_close(snd_pcm_t *pcm)
{
ioplug_priv_t *io = pcm->private_data;
clear_io_params(io);
if (io->data->callback->close)
io->data->callback->close(io->data);
free(io);
return 0;
}
static snd_pcm_ops_t snd_pcm_ioplug_ops = {
.close = snd_pcm_ioplug_close,
.nonblock = snd_pcm_ioplug_nonblock,
.async = snd_pcm_ioplug_async,
.poll_revents = snd_pcm_ioplug_poll_revents,
.info = snd_pcm_ioplug_info,
.hw_refine = snd_pcm_ioplug_hw_refine,
.hw_params = snd_pcm_ioplug_hw_params,
.hw_free = snd_pcm_ioplug_hw_free,
.sw_params = snd_pcm_ioplug_sw_params,
.channel_info = snd_pcm_ioplug_channel_info,
.dump = snd_pcm_ioplug_dump,
.mmap = snd_pcm_ioplug_mmap,
.munmap = snd_pcm_ioplug_munmap,
};
static snd_pcm_fast_ops_t snd_pcm_ioplug_fast_ops = {
.status = snd_pcm_ioplug_status,
.prepare = snd_pcm_ioplug_prepare,
.reset = snd_pcm_ioplug_reset,
.start = snd_pcm_ioplug_start,
.drop = snd_pcm_ioplug_drop,
.drain = snd_pcm_ioplug_drain,
.pause = snd_pcm_ioplug_pause,
.state = snd_pcm_ioplug_state,
.hwsync = snd_pcm_ioplug_hwsync,
.delay = snd_pcm_ioplug_delay,
.resume = snd_pcm_ioplug_resume,
.poll_ask = NULL,
.link_fd = NULL,
.link = NULL,
.unlink = NULL,
.rewind = snd_pcm_ioplug_rewind,
.forward = snd_pcm_ioplug_forward,
.writei = snd_pcm_ioplug_writei,
.writen = snd_pcm_ioplug_writen,
.readi = snd_pcm_ioplug_readi,
.readn = snd_pcm_ioplug_readn,
.avail_update = snd_pcm_ioplug_avail_update,
.mmap_commit = snd_pcm_ioplug_mmap_commit,
};
void snd_pcm_ioplug_params_reset(snd_pcm_ioplug_t *ioplug)
{
ioplug_priv_t *io = ioplug->pcm->private_data;
clear_io_params(io);
}
int snd_pcm_ioplug_set_param_list(snd_pcm_ioplug_t *ioplug, int type, unsigned int num_list, const unsigned int *list)
{
ioplug_priv_t *io = ioplug->pcm->private_data;
if (type < 0 && type >= SND_PCM_IOPLUG_HW_PARAMS) {
SNDERR("IOPLUG: invalid parameter type %d", type);
return -EINVAL;
}
if (type == SND_PCM_IOPLUG_HW_PERIODS)
io->params[type].integer = 1;
return snd_ext_parm_set_list(&io->params[type], num_list, list);
}
int snd_pcm_ioplug_set_param_minmax(snd_pcm_ioplug_t *ioplug, int type, unsigned int min, unsigned int max)
{
ioplug_priv_t *io = ioplug->pcm->private_data;
if (type < 0 && type >= SND_PCM_IOPLUG_HW_PARAMS) {
SNDERR("IOPLUG: invalid parameter type %d", type);
return -EINVAL;
}
if (type == SND_PCM_IOPLUG_HW_ACCESS || type == SND_PCM_IOPLUG_HW_FORMAT) {
SNDERR("IOPLUG: invalid parameter type %d", type);
return -EINVAL;
}
if (type == SND_PCM_IOPLUG_HW_PERIODS)
io->params[type].integer = 1;
return snd_ext_parm_set_minmax(&io->params[type], min, max);
}
int snd_pcm_ioplug_reinit_status(snd_pcm_ioplug_t *ioplug)
{
ioplug->pcm->poll_fd = ioplug->poll_fd;
ioplug->pcm->poll_events = ioplug->poll_events;
ioplug->pcm->mmap_rw = ioplug->mmap_rw;
return 0;
}
const snd_pcm_channel_area_t *snd_pcm_ioplug_mmap_areas(snd_pcm_ioplug_t *ioplug)
{
if (ioplug->mmap_rw)
return snd_pcm_mmap_areas(ioplug->pcm);
return NULL;
}
int snd_pcm_ioplug_create(snd_pcm_ioplug_t *ioplug, const char *name,
snd_pcm_stream_t stream, int mode)
{
ioplug_priv_t *io;
int err;
snd_pcm_t *pcm;
assert(ioplug && ioplug->callback);
assert(ioplug->callback->start &&
ioplug->callback->stop &&
ioplug->callback->pointer);
io = calloc(1, sizeof(*io));
if (! io)
return -ENOMEM;
io->data = ioplug;
ioplug->state = SND_PCM_STATE_OPEN;
ioplug->stream = stream;
err = snd_pcm_new(&pcm, SND_PCM_TYPE_IOPLUG, name, stream, mode);
if (err < 0) {
free(io);
return err;
}
ioplug->pcm = pcm;
pcm->ops = &snd_pcm_ioplug_ops;
pcm->fast_ops = &snd_pcm_ioplug_fast_ops;
pcm->private_data = io;
snd_pcm_set_hw_ptr(pcm, &ioplug->hw_ptr, -1, 0);
snd_pcm_set_appl_ptr(pcm, &ioplug->appl_ptr, -1, 0);
snd_pcm_ioplug_reinit_status(ioplug);
return 0;
}
int snd_pcm_ioplug_delete(snd_pcm_ioplug_t *ioplug)
{
return snd_pcm_close(ioplug->pcm);
}
|