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
|
/* $OpenBSD: abuf.c,v 1.23 2011/10/12 07:20:03 ratchov Exp $ */
/*
* Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Simple byte fifo. It has one reader and one writer. The abuf
* structure is used to interconnect audio processing units (aproc
* structures).
*
* The abuf data is split in two parts: (1) valid data available to the reader
* (2) space available to the writer, which is not necessarily unused. It works
* as follows: the write starts filling at offset (start + used), once the data
* is ready, the writer adds to used the count of bytes available.
*/
/*
* TODO
*
* use blocks instead of frames for WOK and ROK macros. If necessary
* (unlikely) define reader block size and writer blocks size to
* ease pipe/socket implementation
*/
#include <err.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "abuf.h"
#include "aparams.h"
#include "aproc.h"
#include "conf.h"
#ifdef DEBUG
#include "dbg.h"
#endif
#ifdef DEBUG
void
abuf_dbg(struct abuf *buf)
{
if (buf->wproc) {
aproc_dbg(buf->wproc);
} else {
dbg_puts("none");
}
dbg_puts(buf->inuse ? "=>" : "->");
if (buf->rproc) {
aproc_dbg(buf->rproc);
} else {
dbg_puts("none");
}
}
void
abuf_dump(struct abuf *buf)
{
abuf_dbg(buf);
dbg_puts(": used = ");
dbg_putu(buf->used);
dbg_puts("/");
dbg_putu(buf->len);
dbg_puts(" start = ");
dbg_putu(buf->start);
dbg_puts("\n");
}
#endif
struct abuf *
abuf_new(unsigned nfr, struct aparams *par)
{
struct abuf *buf;
unsigned len, bpf;
bpf = aparams_bpf(par);
len = nfr * bpf;
buf = malloc(sizeof(struct abuf) + len);
if (buf == NULL) {
#ifdef DEBUG
dbg_puts("couldn't allocate abuf of ");
dbg_putu(nfr);
dbg_puts("fr * ");
dbg_putu(bpf);
dbg_puts("bpf\n");
dbg_panic();
#else
err(1, "malloc");
#endif
}
buf->bpf = bpf;
buf->cmin = par->cmin;
buf->cmax = par->cmax;
buf->inuse = 0;
/*
* fill fifo pointers
*/
buf->len = nfr;
buf->used = 0;
buf->start = 0;
buf->rproc = NULL;
buf->wproc = NULL;
buf->duplex = NULL;
return buf;
}
void
abuf_del(struct abuf *buf)
{
if (buf->duplex)
buf->duplex->duplex = NULL;
#ifdef DEBUG
if (buf->rproc || buf->wproc) {
abuf_dbg(buf);
dbg_puts(": can't delete referenced buffer\n");
dbg_panic();
}
if (ABUF_ROK(buf)) {
/*
* XXX: we should call abort(), here.
* However, poll() doesn't seem to return POLLHUP,
* so the reader is never destroyed; instead it appears
* as blocked. Fix file_poll(), if fixable, and add
* a call to abord() here.
*/
if (debug_level >= 3) {
abuf_dbg(buf);
dbg_puts(": deleting non-empty buffer, used = ");
dbg_putu(buf->used);
dbg_puts("\n");
}
}
#endif
free(buf);
}
/*
* Clear buffer contents.
*/
void
abuf_clear(struct abuf *buf)
{
#ifdef DEBUG
if (debug_level >= 3) {
abuf_dbg(buf);
dbg_puts(": cleared\n");
}
#endif
buf->used = 0;
buf->start = 0;
}
/*
* Get a pointer to the readable block at the given offset.
*/
unsigned char *
abuf_rgetblk(struct abuf *buf, unsigned *rsize, unsigned ofs)
{
unsigned count, start, used;
start = buf->start + ofs;
used = buf->used - ofs;
if (start >= buf->len)
start -= buf->len;
#ifdef DEBUG
if (start >= buf->len || used > buf->used) {
abuf_dump(buf);
dbg_puts(": rgetblk: bad ofs = ");
dbg_putu(ofs);
dbg_puts("\n");
dbg_panic();
}
#endif
count = buf->len - start;
if (count > used)
count = used;
*rsize = count;
return (unsigned char *)buf + sizeof(struct abuf) + start * buf->bpf;
}
/*
* Discard the block at the start postion.
*/
void
abuf_rdiscard(struct abuf *buf, unsigned count)
{
#ifdef DEBUG
if (count > buf->used) {
abuf_dump(buf);
dbg_puts(": rdiscard: bad count = ");
dbg_putu(count);
dbg_puts("\n");
dbg_panic();
}
if (debug_level >= 4) {
abuf_dbg(buf);
dbg_puts(": discard(");
dbg_putu(count);
dbg_puts(")\n");
}
#endif
buf->used -= count;
buf->start += count;
if (buf->start >= buf->len)
buf->start -= buf->len;
}
/*
* Commit the data written at the end postion.
*/
void
abuf_wcommit(struct abuf *buf, unsigned count)
{
#ifdef DEBUG
if (count > (buf->len - buf->used)) {
abuf_dump(buf);
dbg_puts(": rdiscard: bad count = ");
dbg_putu(count);
dbg_puts("\n");
dbg_panic();
}
if (debug_level >= 4) {
abuf_dbg(buf);
dbg_puts(": commit(");
dbg_putu(count);
dbg_puts(")\n");
}
#endif
buf->used += count;
}
/*
* Get a pointer to the writable block at offset ofs.
*/
unsigned char *
abuf_wgetblk(struct abuf *buf, unsigned *rsize, unsigned ofs)
{
unsigned end, avail, count;
end = buf->start + buf->used + ofs;
if (end >= buf->len)
end -= buf->len;
#ifdef DEBUG
if (end >= buf->len) {
abuf_dump(buf);
dbg_puts(": wgetblk: bad ofs = ");
dbg_putu(ofs);
dbg_puts("\n");
dbg_panic();
}
#endif
avail = buf->len - (buf->used + ofs);
count = buf->len - end;
if (count > avail)
count = avail;
*rsize = count;
return (unsigned char *)buf + sizeof(struct abuf) + end * buf->bpf;
}
/*
* Flush buffer either by dropping samples or by calling the aproc
* call-back to consume data. Return 0 if blocked, 1 otherwise.
*/
int
abuf_flush_do(struct abuf *buf)
{
struct aproc *p;
p = buf->rproc;
if (!p)
return 0;
#ifdef DEBUG
if (debug_level >= 4) {
aproc_dbg(p);
dbg_puts(": in\n");
}
#endif
return p->ops->in(p, buf);
}
/*
* Fill the buffer either by generating silence or by calling the aproc
* call-back to provide data. Return 0 if blocked, 1 otherwise.
*/
int
abuf_fill_do(struct abuf *buf)
{
struct aproc *p;
p = buf->wproc;
if (!p)
return 0;
#ifdef DEBUG
if (debug_level >= 4) {
aproc_dbg(p);
dbg_puts(": out\n");
}
#endif
return p->ops->out(p, buf);
}
/*
* Notify the reader that there will be no more input (producer
* disappeared) and destroy the buffer.
*/
void
abuf_eof_do(struct abuf *buf)
{
struct aproc *p;
p = buf->rproc;
if (p) {
buf->rproc = NULL;
LIST_REMOVE(buf, ient);
buf->inuse++;
#ifdef DEBUG
if (debug_level >= 4) {
aproc_dbg(p);
dbg_puts(": eof\n");
}
#endif
p->ops->eof(p, buf);
buf->inuse--;
}
abuf_del(buf);
}
/*
* Notify the writer that the buffer has no more consumer,
* and destroy the buffer.
*/
void
abuf_hup_do(struct abuf *buf)
{
struct aproc *p;
if (ABUF_ROK(buf)) {
#ifdef DEBUG
if (debug_level >= 3) {
abuf_dbg(buf);
dbg_puts(": hup: lost ");
dbg_putu(buf->used);
dbg_puts(" bytes\n");
}
#endif
buf->used = 0;
}
p = buf->wproc;
if (p != NULL) {
buf->wproc = NULL;
LIST_REMOVE(buf, oent);
buf->inuse++;
#ifdef DEBUG
if (debug_level >= 3) {
aproc_dbg(p);
dbg_puts(": hup\n");
}
#endif
p->ops->hup(p, buf);
buf->inuse--;
}
abuf_del(buf);
}
/*
* Notify the read end of the buffer that there is input available
* and that data can be processed again.
*/
int
abuf_flush(struct abuf *buf)
{
if (buf->inuse) {
#ifdef DEBUG
if (debug_level >= 4) {
abuf_dbg(buf);
dbg_puts(": flush blocked (inuse)\n");
}
#endif
} else {
buf->inuse++;
for (;;) {
if (!abuf_flush_do(buf))
break;
}
buf->inuse--;
if (ABUF_HUP(buf)) {
abuf_hup_do(buf);
return 0;
}
}
return 1;
}
/*
* Notify the write end of the buffer that there is room and data can be
* written again. This routine can only be called from the out()
* call-back of the reader.
*
* Return 1 if the buffer was filled, and 0 if eof condition occured. The
* reader must detach the buffer on EOF condition, since its aproc->eof()
* call-back will never be called.
*/
int
abuf_fill(struct abuf *buf)
{
if (buf->inuse) {
#ifdef DEBUG
if (debug_level >= 4) {
abuf_dbg(buf);
dbg_puts(": fill blocked (inuse)\n");
}
#endif
} else {
buf->inuse++;
for (;;) {
if (!abuf_fill_do(buf))
break;
}
buf->inuse--;
if (ABUF_EOF(buf)) {
abuf_eof_do(buf);
return 0;
}
}
return 1;
}
/*
* Run a read/write loop on the buffer until either the reader or the
* writer blocks, or until the buffer reaches eofs. We can not get hup here,
* since hup() is only called from terminal nodes, from the main loop.
*
* NOTE: The buffer may disappear (ie. be free()ed) if eof is reached, so
* do not keep references to the buffer or to its writer or reader.
*/
void
abuf_run(struct abuf *buf)
{
int canfill = 1, canflush = 1;
if (buf->inuse) {
#ifdef DEBUG
if (debug_level >= 4) {
abuf_dbg(buf);
dbg_puts(": run blocked (inuse)\n");
}
#endif
return;
}
buf->inuse++;
for (;;) {
if (canfill) {
if (!abuf_fill_do(buf))
canfill = 0;
else
canflush = 1;
} else if (canflush) {
if (!abuf_flush_do(buf))
canflush = 0;
else
canfill = 1;
} else
break;
}
buf->inuse--;
if (ABUF_EOF(buf)) {
abuf_eof_do(buf);
return;
}
if (ABUF_HUP(buf)) {
abuf_hup_do(buf);
return;
}
}
/*
* Notify the reader that there will be no more input (producer
* disappeared). The buffer is flushed and eof() is called only if all
* data is flushed.
*/
void
abuf_eof(struct abuf *buf)
{
#ifdef DEBUG
if (debug_level >= 3) {
abuf_dbg(buf);
dbg_puts(": eof requested\n");
}
if (buf->wproc == NULL) {
abuf_dbg(buf);
dbg_puts(": eof, no writer\n");
dbg_panic();
}
#endif
LIST_REMOVE(buf, oent);
buf->wproc = NULL;
if (buf->rproc != NULL) {
if (!abuf_flush(buf))
return;
if (ABUF_ROK(buf)) {
/*
* Could not flush everything, the reader will
* have a chance to delete the abuf later.
*/
#ifdef DEBUG
if (debug_level >= 3) {
abuf_dbg(buf);
dbg_puts(": eof, blocked (drain)\n");
}
#endif
return;
}
}
if (buf->inuse) {
#ifdef DEBUG
if (debug_level >= 3) {
abuf_dbg(buf);
dbg_puts(": eof, blocked (inuse)\n");
}
#endif
return;
}
abuf_eof_do(buf);
}
/*
* Notify the writer that the buffer has no more consumer,
* and that no more data will accepted.
*/
void
abuf_hup(struct abuf *buf)
{
#ifdef DEBUG
if (debug_level >= 3) {
abuf_dbg(buf);
dbg_puts(": hup requested\n");
}
if (buf->rproc == NULL) {
abuf_dbg(buf);
dbg_puts(": hup, no reader\n");
dbg_panic();
}
#endif
buf->rproc = NULL;
LIST_REMOVE(buf, ient);
if (buf->wproc != NULL) {
if (buf->inuse) {
#ifdef DEBUG
if (debug_level >= 3) {
abuf_dbg(buf);
dbg_puts(": eof, blocked (inuse)\n");
}
#endif
return;
}
}
abuf_hup_do(buf);
}
/*
* Notify the reader of the change of its real-time position
*/
void
abuf_ipos(struct abuf *buf, int delta)
{
struct aproc *p = buf->rproc;
if (p && p->ops->ipos) {
buf->inuse++;
#ifdef DEBUG
if (debug_level >= 4) {
aproc_dbg(p);
dbg_puts(": ipos delta = ");
dbg_puti(delta);
dbg_puts("\n");
}
#endif
p->ops->ipos(p, buf, delta);
buf->inuse--;
}
if (ABUF_HUP(buf))
abuf_hup_do(buf);
}
/*
* Notify the writer of the change of its real-time position
*/
void
abuf_opos(struct abuf *buf, int delta)
{
struct aproc *p = buf->wproc;
if (p && p->ops->opos) {
buf->inuse++;
#ifdef DEBUG
if (debug_level >= 4) {
aproc_dbg(p);
dbg_puts(": opos delta = ");
dbg_puti(delta);
dbg_puts("\n");
}
#endif
p->ops->opos(p, buf, delta);
buf->inuse--;
}
if (ABUF_HUP(buf))
abuf_hup_do(buf);
}
|