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, see <http://www.gnu.org/licenses/>.
16 ***/
17
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include <pulse/simple.h>
28 #include <pulse/error.h>
29
30 #define BUFSIZE 1024
31
32 /* A simple routine calling UNIX write() in a loop */
loop_write(int fd,const void * data,size_t size)33 static ssize_t loop_write(int fd, const void*data, size_t size) {
34 ssize_t ret = 0;
35
36 while (size > 0) {
37 ssize_t r;
38
39 if ((r = write(fd, data, size)) < 0)
40 return r;
41
42 if (r == 0)
43 break;
44
45 ret += r;
46 data = (const uint8_t*) data + r;
47 size -= (size_t) r;
48 }
49
50 return ret;
51 }
52
main(int argc,char * argv[])53 int main(int argc, char*argv[]) {
54 /* The sample type to use */
55 static const pa_sample_spec ss = {
56 .format = PA_SAMPLE_S16LE,
57 .rate = 44100,
58 .channels = 2
59 };
60 pa_simple *s = NULL;
61 int ret = 1;
62 int error;
63
64 /* Create the recording stream */
65 if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {
66 fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
67 goto finish;
68 }
69
70 for (;;) {
71 uint8_t buf[BUFSIZE];
72
73 /* Record some data ... */
74 if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) {
75 fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
76 goto finish;
77 }
78
79 /* And write it to STDOUT */
80 if (loop_write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf)) {
81 fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
82 goto finish;
83 }
84 }
85
86 ret = 0;
87
88 finish:
89
90 if (s)
91 pa_simple_free(s);
92
93 return ret;
94 }
95