Add a missing pa_xfree.
[pulseaudio.git] / src / tests / parec-simple.c
blobd7d883607d815032f086f9f7476bbe998d6adb07
1 /* $Id$ */
3 /***
4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio 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 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <errno.h>
31 #include <pulse/simple.h>
32 #include <pulse/error.h>
33 #include <pulsecore/gccmacro.h>
35 #define BUFSIZE 1024
37 /* A simple routine calling UNIX write() in a loop */
38 static ssize_t loop_write(int fd, const void*data, size_t size) {
39 ssize_t ret = 0;
41 while (size > 0) {
42 ssize_t r;
44 if ((r = write(fd, data, size)) < 0)
45 return r;
47 if (r == 0)
48 break;
50 ret += r;
51 data = (const uint8_t*) data + r;
52 size -= r;
55 return ret;
58 int main(PA_GCC_UNUSED int argc, char*argv[]) {
59 /* The sample type to use */
60 static const pa_sample_spec ss = {
61 .format = PA_SAMPLE_S16LE,
62 .rate = 44100,
63 .channels = 2
65 pa_simple *s = NULL;
66 int ret = 1;
67 int error;
69 /* Create the recording stream */
70 if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {
71 fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
72 goto finish;
75 for (;;) {
76 uint8_t buf[BUFSIZE];
77 ssize_t r;
79 /* Record some data ... */
80 if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) {
81 fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
82 goto finish;
85 /* And write it to STDOUT */
86 if ((r = loop_write(STDOUT_FILENO, buf, sizeof(buf))) <= 0) {
87 fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
88 goto finish;
92 ret = 0;
94 finish:
96 if (s)
97 pa_simple_free(s);
99 return ret;