]> code.delx.au - pulseaudio/blob - polyp/mcalign-test.c
e4a6a2393feceb18600282a9cfc62d687088684b
[pulseaudio] / polyp / mcalign-test.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <time.h>
33
34 #include "util.h"
35 #include "mcalign.h"
36
37 /* A simple program for testing pa_mcalign */
38
39 int main(int argc, char *argv[]) {
40 struct pa_mcalign *a = pa_mcalign_new(11, NULL);
41 struct pa_memchunk c;
42
43 pa_memchunk_reset(&c);
44
45 srand(time(NULL));
46
47 for (;;) {
48 ssize_t r;
49 size_t l;
50
51 if (!c.memblock) {
52 c.memblock = pa_memblock_new(2048, NULL);
53 c.index = c.length = 0;
54 }
55
56 assert(c.index < c.memblock->length);
57
58 l = c.memblock->length - c.index;
59
60 l = l <= 1 ? l : rand() % (l-1) +1 ;
61
62 if ((r = read(STDIN_FILENO, (uint8_t*) c.memblock->data + c.index, l)) <= 0) {
63 fprintf(stderr, "read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
64 break;
65 }
66
67 c.length = r;
68 pa_mcalign_push(a, &c);
69 fprintf(stderr, "Read %u bytes\n", r);
70
71 c.index += r;
72
73 if (c.index >= c.memblock->length) {
74 pa_memblock_unref(c.memblock);
75 pa_memchunk_reset(&c);
76 }
77
78 for (;;) {
79 struct pa_memchunk t;
80
81 if (pa_mcalign_pop(a, &t) < 0)
82 break;
83
84 pa_loop_write(STDOUT_FILENO, (uint8_t*) t.memblock->data + t.index, t.length);
85 fprintf(stderr, "Wrote %lu bytes.\n", (unsigned long) t.length);
86
87 pa_memblock_unref(t.memblock);
88 }
89 }
90
91 pa_mcalign_free(a);
92
93 if (c.memblock)
94 pa_memblock_unref(c.memblock);
95 }