]> code.delx.au - pulseaudio/blob - src/tests/pacat-simple.c
d4224e110710187f6c988dd2178c0af1fe389c32
[pulseaudio] / src / tests / pacat-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 #include <fcntl.h>
29
30 #include <pulse/simple.h>
31 #include <pulse/error.h>
32 #include <pulse/gccmacro.h>
33
34 #define BUFSIZE 1024
35
36 int main(int argc, char*argv[]) {
37
38 /* The Sample format to use */
39 static const pa_sample_spec ss = {
40 .format = PA_SAMPLE_S16LE,
41 .rate = 44100,
42 .channels = 2
43 };
44
45 pa_simple *s = NULL;
46 int ret = 1;
47 int error;
48
49 /* replace STDIN with the specified file if needed */
50 if (argc > 1) {
51 int fd;
52
53 if ((fd = open(argv[1], O_RDONLY)) < 0) {
54 fprintf(stderr, __FILE__": open() failed: %s\n", strerror(errno));
55 goto finish;
56 }
57
58 if (dup2(fd, STDIN_FILENO) < 0) {
59 fprintf(stderr, __FILE__": dup2() failed: %s\n", strerror(errno));
60 goto finish;
61 }
62
63 close(fd);
64 }
65
66 /* Create a new playback stream */
67 if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &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 ssize_t r;
75
76 #if 0
77 pa_usec_t latency;
78
79 if ((latency = pa_simple_get_latency(s, &error)) == (pa_usec_t) -1) {
80 fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));
81 goto finish;
82 }
83
84 fprintf(stderr, "%0.0f usec \r", (float)latency);
85 #endif
86
87 /* Read some data ... */
88 if ((r = read(STDIN_FILENO, buf, sizeof(buf))) <= 0) {
89 if (r == 0) /* EOF */
90 break;
91
92 fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
93 goto finish;
94 }
95
96 /* ... and play it */
97 if (pa_simple_write(s, buf, (size_t) r, &error) < 0) {
98 fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
99 goto finish;
100 }
101 }
102
103 /* Make sure that every single sample was played */
104 if (pa_simple_drain(s, &error) < 0) {
105 fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
106 goto finish;
107 }
108
109 ret = 0;
110
111 finish:
112
113 if (s)
114 pa_simple_free(s);
115
116 return ret;
117 }