]> code.delx.au - pulseaudio/blob - src/tests/parec-simple.c
module-switch-on-port-available: Use new find best port function
[pulseaudio] / src / tests / parec-simple.c
1 /***
2 This file is part of PulseAudio.
3
4 PulseAudio is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2.1 of the License,
7 or (at your option) any later version.
8
9 PulseAudio is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with PulseAudio; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include <pulse/simple.h>
30 #include <pulse/error.h>
31
32 #define BUFSIZE 1024
33
34 /* A simple routine calling UNIX write() in a loop */
35 static ssize_t loop_write(int fd, const void*data, size_t size) {
36 ssize_t ret = 0;
37
38 while (size > 0) {
39 ssize_t r;
40
41 if ((r = write(fd, data, size)) < 0)
42 return r;
43
44 if (r == 0)
45 break;
46
47 ret += r;
48 data = (const uint8_t*) data + r;
49 size -= (size_t) r;
50 }
51
52 return ret;
53 }
54
55 int main(int argc, char*argv[]) {
56 /* The sample type to use */
57 static const pa_sample_spec ss = {
58 .format = PA_SAMPLE_S16LE,
59 .rate = 44100,
60 .channels = 2
61 };
62 pa_simple *s = NULL;
63 int ret = 1;
64 int error;
65
66 /* Create the recording stream */
67 if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {
68 fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
69 goto finish;
70 }
71
72 for (;;) {
73 uint8_t buf[BUFSIZE];
74
75 /* Record some data ... */
76 if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) {
77 fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
78 goto finish;
79 }
80
81 /* And write it to STDOUT */
82 if (loop_write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf)) {
83 fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
84 goto finish;
85 }
86 }
87
88 ret = 0;
89
90 finish:
91
92 if (s)
93 pa_simple_free(s);
94
95 return ret;
96 }