add output stream draining
[profile/ivi/pulseaudio.git] / src / pacat-simple.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <errno.h>
5
6 #include "simple.h"
7 #include "polyp-error.h"
8
9 #define BUFSIZE 1024
10
11 int main(int argc, char*argv[]) {
12     static const struct pa_sample_spec ss = {
13         .format = PA_SAMPLE_S16LE,
14         .rate = 44100,
15         .channels = 2
16     };
17     struct pa_simple *s = NULL;
18     int ret = 1;
19     int error;
20
21     if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, &error))) {
22         fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
23         goto finish;
24     }
25
26     for (;;) {
27         uint8_t buf[BUFSIZE];
28         ssize_t r;
29         
30         if ((r = read(STDIN_FILENO, buf, sizeof(buf))) <= 0) {
31             if (r == 0) /* eof */
32                 break;
33             
34             fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
35             goto finish;
36         }
37
38         if (pa_simple_write(s, buf, r, &error) < 0) {
39             fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
40             goto finish;
41         }
42     }
43
44     /* Make sure that every single sample way played */
45     if (pa_simple_drain(s, &error) < 0) {
46         fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
47         goto finish;
48     }
49
50     ret = 0;
51
52 finish:
53
54     if (s)
55         pa_simple_free(s);
56     
57     return ret;
58 }