]> code.delx.au - pulseaudio/blob - polyp/pacat.c
make distcheck clean
[pulseaudio] / polyp / pacat.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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.
10
11 polypaudio 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.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <signal.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include <polyp/polyplib.h>
35 #include <polyp/polyplib-error.h>
36 #include <polyp/mainloop.h>
37 #include <polyp/mainloop-signal.h>
38
39 static enum { RECORD, PLAYBACK } mode = PLAYBACK;
40
41 static struct pa_context *context = NULL;
42 static struct pa_stream *stream = NULL;
43 static struct pa_mainloop_api *mainloop_api = NULL;
44
45 static void *buffer = NULL;
46 static size_t buffer_length = 0, buffer_index = 0;
47
48 static void* stdio_source = NULL;
49
50 static void quit(int ret) {
51 assert(mainloop_api);
52 mainloop_api->quit(mainloop_api, ret);
53 }
54
55 static void context_die_callback(struct pa_context *c, void *userdata) {
56 assert(c);
57 fprintf(stderr, "Connection to server shut down, exiting.\n");
58 quit(1);
59 }
60
61 static void stream_die_callback(struct pa_stream *s, void *userdata) {
62 assert(s);
63 fprintf(stderr, "Stream deleted, exiting.\n");
64 quit(1);
65 }
66
67 static void do_stream_write(size_t length) {
68 size_t l;
69 assert(length);
70
71 if (!buffer || !buffer_length)
72 return;
73
74 l = length;
75 if (l > buffer_length)
76 l = buffer_length;
77
78 pa_stream_write(stream, buffer+buffer_index, l);
79 buffer_length -= l;
80 buffer_index += l;
81
82 if (!buffer_length) {
83 free(buffer);
84 buffer = NULL;
85 buffer_index = buffer_length = 0;
86 }
87 }
88
89 static void stream_write_callback(struct pa_stream *s, size_t length, void *userdata) {
90 assert(s && length);
91
92 if (stdio_source)
93 mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_INPUT);
94
95 if (!buffer)
96 return;
97
98 do_stream_write(length);
99 }
100
101 static void stream_read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) {
102 assert(s && data && length);
103
104 if (stdio_source)
105 mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_OUTPUT);
106
107 if (buffer) {
108 fprintf(stderr, "Buffer overrrun, dropping incoming data\n");
109 return;
110 }
111
112 buffer = malloc(buffer_length = length);
113 assert(buffer);
114 memcpy(buffer, data, length);
115 buffer_index = 0;
116 }
117
118 static void stream_complete_callback(struct pa_stream*s, int success, void *userdata) {
119 assert(s);
120
121 if (!success) {
122 fprintf(stderr, "Stream creation failed: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
123 quit(1);
124 return;
125 }
126
127 fprintf(stderr, "Stream created.\n");
128 }
129
130 static void context_complete_callback(struct pa_context *c, int success, void *userdata) {
131 static const struct pa_sample_spec ss = {
132 .format = PA_SAMPLE_S16LE,
133 .rate = 44100,
134 .channels = 2
135 };
136
137 assert(c && !stream);
138
139 if (!success) {
140 fprintf(stderr, "Connection failed: %s\n", pa_strerror(pa_context_errno(c)));
141 goto fail;
142 }
143
144 fprintf(stderr, "Connection established.\n");
145
146 if (!(stream = pa_stream_new(c, mode == PLAYBACK ? PA_STREAM_PLAYBACK : PA_STREAM_RECORD, NULL, "pacat", &ss, NULL, stream_complete_callback, NULL))) {
147 fprintf(stderr, "pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(c)));
148 goto fail;
149 }
150
151 pa_stream_set_die_callback(stream, stream_die_callback, NULL);
152 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
153 pa_stream_set_read_callback(stream, stream_read_callback, NULL);
154
155 return;
156
157 fail:
158 quit(1);
159 }
160
161 static void context_drain_complete(struct pa_context*c, void *userdata) {
162 quit(0);
163 }
164
165 static void stream_drain_complete(struct pa_stream*s, void *userdata) {
166 fprintf(stderr, "Playback stream drained.\n");
167
168 pa_stream_free(stream);
169 stream = NULL;
170
171 if (pa_context_drain(context, context_drain_complete, NULL) < 0)
172 quit(0);
173 else
174 fprintf(stderr, "Draining connection to server.\n");
175 }
176
177 static void stdin_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
178 size_t l, w = 0;
179 ssize_t r;
180 assert(a == mainloop_api && id && stdio_source == id);
181
182 if (buffer) {
183 mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_NULL);
184 return;
185 }
186
187 if (!stream || !pa_stream_is_ready(stream) || !(l = w = pa_stream_writable_size(stream)))
188 l = 4096;
189
190 buffer = malloc(l);
191 assert(buffer);
192 if ((r = read(fd, buffer, l)) <= 0) {
193 if (r == 0) {
194 fprintf(stderr, "Got EOF.\n");
195 pa_stream_drain(stream, stream_drain_complete, NULL);
196 } else {
197 fprintf(stderr, "read() failed: %s\n", strerror(errno));
198 quit(1);
199 }
200
201 mainloop_api->cancel_io(mainloop_api, stdio_source);
202 stdio_source = NULL;
203 return;
204 }
205
206 buffer_length = r;
207 buffer_index = 0;
208
209 if (w)
210 do_stream_write(w);
211 }
212
213 static void stdout_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
214 ssize_t r;
215 assert(a == mainloop_api && id && stdio_source == id);
216
217 if (!buffer) {
218 mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_NULL);
219 return;
220 }
221
222 assert(buffer_length);
223
224 if ((r = write(fd, buffer+buffer_index, buffer_length)) <= 0) {
225 fprintf(stderr, "write() failed: %s\n", strerror(errno));
226 quit(1);
227
228 mainloop_api->cancel_io(mainloop_api, stdio_source);
229 stdio_source = NULL;
230 return;
231 }
232
233 buffer_length -= r;
234 buffer_index += r;
235
236 if (!buffer_length) {
237 free(buffer);
238 buffer = NULL;
239 buffer_length = buffer_index = 0;
240 }
241 }
242
243 static void exit_signal_callback(void *id, int sig, void *userdata) {
244 fprintf(stderr, "Got SIGINT, exiting.\n");
245 quit(0);
246
247 }
248
249 static void stream_get_latency_callback(struct pa_stream *s, uint32_t latency, void *userdata) {
250 assert(s);
251
252 if (latency == (uint32_t) -1) {
253 fprintf(stderr, "Failed to get latency: %s\n", strerror(errno));
254 quit(1);
255 return;
256 }
257
258 fprintf(stderr, "Current latency is %u usecs.\n", latency);
259 }
260
261 static void sigusr1_signal_callback(void *id, int sig, void *userdata) {
262 if (mode == PLAYBACK) {
263 fprintf(stderr, "Got SIGUSR1, requesting latency.\n");
264 pa_stream_get_latency(stream, stream_get_latency_callback, NULL);
265 }
266 }
267
268 int main(int argc, char *argv[]) {
269 struct pa_mainloop* m = NULL;
270 int ret = 1, r;
271 char *bn;
272
273 if (!(bn = strrchr(argv[0], '/')))
274 bn = argv[0];
275
276 if (strstr(bn, "rec") || strstr(bn, "mon"))
277 mode = RECORD;
278 else if (strstr(bn, "cat") || strstr(bn, "play"))
279 mode = PLAYBACK;
280
281 fprintf(stderr, "Opening a %s stream.\n", mode == RECORD ? "recording" : "playback");
282
283 if (!(m = pa_mainloop_new())) {
284 fprintf(stderr, "pa_mainloop_new() failed.\n");
285 goto quit;
286 }
287
288 mainloop_api = pa_mainloop_get_api(m);
289
290 r = pa_signal_init(mainloop_api);
291 assert(r == 0);
292 pa_signal_register(SIGINT, exit_signal_callback, NULL);
293 pa_signal_register(SIGUSR1, sigusr1_signal_callback, NULL);
294 signal(SIGPIPE, SIG_IGN);
295
296 if (!(stdio_source = mainloop_api->source_io(mainloop_api,
297 mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO,
298 mode == PLAYBACK ? PA_MAINLOOP_API_IO_EVENT_INPUT : PA_MAINLOOP_API_IO_EVENT_OUTPUT,
299 mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) {
300 fprintf(stderr, "source_io() failed.\n");
301 goto quit;
302 }
303
304 if (!(context = pa_context_new(mainloop_api, argv[0]))) {
305 fprintf(stderr, "pa_context_new() failed.\n");
306 goto quit;
307 }
308
309 if (pa_context_connect(context, NULL, context_complete_callback, NULL) < 0) {
310 fprintf(stderr, "pa_context_connext() failed.\n");
311 goto quit;
312 }
313
314 pa_context_set_die_callback(context, context_die_callback, NULL);
315
316 if (pa_mainloop_run(m, &ret) < 0) {
317 fprintf(stderr, "pa_mainloop_run() failed.\n");
318 goto quit;
319 }
320
321 quit:
322 if (stream)
323 pa_stream_free(stream);
324 if (context)
325 pa_context_free(context);
326
327 if (m) {
328 pa_signal_done();
329 pa_mainloop_free(m);
330 }
331
332 if (buffer)
333 free(buffer);
334
335 return ret;
336 }