]> code.delx.au - pulseaudio/blob - polyp/pacat.c
bum version number
[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 #include <getopt.h>
34
35 #include <polyp/polyplib.h>
36 #include <polyp/polyplib-error.h>
37 #include <polyp/mainloop.h>
38 #include <polyp/mainloop-signal.h>
39
40 #if PA_API_VERSION != PA_API_VERSION_0_6
41 #error Invalid Polypaudio API version
42 #endif
43
44 static enum { RECORD, PLAYBACK } mode = PLAYBACK;
45
46 static struct pa_context *context = NULL;
47 static struct pa_stream *stream = NULL;
48 static struct pa_mainloop_api *mainloop_api = NULL;
49
50 static void *buffer = NULL;
51 static size_t buffer_length = 0, buffer_index = 0;
52
53 static struct pa_io_event* stdio_event = NULL;
54
55 static char *stream_name = NULL, *client_name = NULL, *device = NULL;
56
57 static int verbose = 0;
58 static pa_volume_t volume = PA_VOLUME_NORM;
59
60 /* A shortcut for terminating the application */
61 static void quit(int ret) {
62 assert(mainloop_api);
63 mainloop_api->quit(mainloop_api, ret);
64 }
65
66 /* Write some data to the stream */
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, (uint8_t*) buffer + buffer_index, l, NULL, 0);
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 /* This is called whenever new data may be written to the stream */
90 static void stream_write_callback(struct pa_stream *s, size_t length, void *userdata) {
91 assert(s && length);
92
93 if (stdio_event)
94 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_INPUT);
95
96 if (!buffer)
97 return;
98
99 do_stream_write(length);
100 }
101
102 /* This is called whenever new data may is available */
103 static void stream_read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) {
104 assert(s && data && length);
105
106 if (stdio_event)
107 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_OUTPUT);
108
109 if (buffer) {
110 fprintf(stderr, "Buffer overrrun, dropping incoming data\n");
111 return;
112 }
113
114 buffer = malloc(buffer_length = length);
115 assert(buffer);
116 memcpy(buffer, data, length);
117 buffer_index = 0;
118 }
119
120 /* This routine is called whenever the stream state changes */
121 static void stream_state_callback(struct pa_stream *s, void *userdata) {
122 assert(s);
123
124 switch (pa_stream_get_state(s)) {
125 case PA_STREAM_CREATING:
126 case PA_STREAM_TERMINATED:
127 break;
128
129 case PA_STREAM_READY:
130 if (verbose)
131 fprintf(stderr, "Stream successfully created\n");
132 break;
133
134 case PA_STREAM_FAILED:
135 default:
136 fprintf(stderr, "Stream errror: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
137 quit(1);
138 }
139 }
140
141 /* This is called whenever the context status changes */
142 static void context_state_callback(struct pa_context *c, void *userdata) {
143 static const struct pa_sample_spec ss = {
144 .format = PA_SAMPLE_S16LE,
145 .rate = 44100,
146 .channels = 2
147 };
148
149 assert(c);
150
151 switch (pa_context_get_state(c)) {
152 case PA_CONTEXT_CONNECTING:
153 case PA_CONTEXT_AUTHORIZING:
154 case PA_CONTEXT_SETTING_NAME:
155 break;
156
157 case PA_CONTEXT_READY:
158
159 assert(c && !stream);
160
161 if (verbose)
162 fprintf(stderr, "Connection established.\n");
163
164 stream = pa_stream_new(c, stream_name, &ss);
165 assert(stream);
166
167 pa_stream_set_state_callback(stream, stream_state_callback, NULL);
168 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
169 pa_stream_set_read_callback(stream, stream_read_callback, NULL);
170
171 if (mode == PLAYBACK)
172 pa_stream_connect_playback(stream, device, NULL, 0, volume);
173 else
174 pa_stream_connect_record(stream, device, NULL, 0);
175
176 break;
177
178 case PA_CONTEXT_TERMINATED:
179 quit(0);
180 break;
181
182 case PA_CONTEXT_FAILED:
183 default:
184 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
185 quit(1);
186 }
187 }
188
189 /* Connection draining complete */
190 static void context_drain_complete(struct pa_context*c, void *userdata) {
191 pa_context_disconnect(c);
192 }
193
194 /* Stream draining complete */
195 static void stream_drain_complete(struct pa_stream*s, int success, void *userdata) {
196 struct pa_operation *o;
197
198 if (!success) {
199 fprintf(stderr, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context)));
200 quit(1);
201 }
202
203 if (verbose)
204 fprintf(stderr, "Playback stream drained.\n");
205
206 pa_stream_disconnect(stream);
207 pa_stream_unref(stream);
208 stream = NULL;
209
210 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
211 pa_context_disconnect(context);
212 else {
213 pa_operation_unref(o);
214
215 if (verbose)
216 fprintf(stderr, "Draining connection to server.\n");
217 }
218 }
219
220 /* New data on STDIN **/
221 static void stdin_callback(struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
222 size_t l, w = 0;
223 ssize_t r;
224 assert(a == mainloop_api && e && stdio_event == e);
225
226 if (buffer) {
227 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
228 return;
229 }
230
231 if (!stream || pa_stream_get_state(stream) != PA_STREAM_READY || !(l = w = pa_stream_writable_size(stream)))
232 l = 4096;
233
234 buffer = malloc(l);
235 assert(buffer);
236 if ((r = read(fd, buffer, l)) <= 0) {
237 if (r == 0) {
238 if (verbose)
239 fprintf(stderr, "Got EOF.\n");
240 pa_operation_unref(pa_stream_drain(stream, stream_drain_complete, NULL));
241 } else {
242 fprintf(stderr, "read() failed: %s\n", strerror(errno));
243 quit(1);
244 }
245
246 mainloop_api->io_free(stdio_event);
247 stdio_event = NULL;
248 return;
249 }
250
251 buffer_length = r;
252 buffer_index = 0;
253
254 if (w)
255 do_stream_write(w);
256 }
257
258 /* Some data may be written to STDOUT */
259 static void stdout_callback(struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
260 ssize_t r;
261 assert(a == mainloop_api && e && stdio_event == e);
262
263 if (!buffer) {
264 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
265 return;
266 }
267
268 assert(buffer_length);
269
270 if ((r = write(fd, (uint8_t*) buffer+buffer_index, buffer_length)) <= 0) {
271 fprintf(stderr, "write() failed: %s\n", strerror(errno));
272 quit(1);
273
274 mainloop_api->io_free(stdio_event);
275 stdio_event = NULL;
276 return;
277 }
278
279 buffer_length -= r;
280 buffer_index += r;
281
282 if (!buffer_length) {
283 free(buffer);
284 buffer = NULL;
285 buffer_length = buffer_index = 0;
286 }
287 }
288
289 /* UNIX signal to quit recieved */
290 static void exit_signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
291 if (verbose)
292 fprintf(stderr, "Got SIGINT, exiting.\n");
293 quit(0);
294
295 }
296
297 /* Show the current latency */
298 static void stream_get_latency_callback(struct pa_stream *s, const struct pa_latency_info *i, void *userdata) {
299 double total;
300 assert(s);
301
302 if (!i) {
303 fprintf(stderr, "Failed to get latency: %s\n", pa_strerror(pa_context_errno(context)));
304 quit(1);
305 return;
306 }
307
308 if (mode == PLAYBACK)
309 total = (double) i->sink_usec + i->buffer_usec + i->transport_usec;
310 else
311 total = (double) i->source_usec + i->buffer_usec + i->transport_usec - i->sink_usec;
312
313 fprintf(stderr, "Latency: buffer: %0.0f usec; sink: %0.0f usec; source: %0.0f usec; transport: %0.0f usec; total: %0.0f usec; synchronized clocks: %s.\n",
314 (float) i->buffer_usec, (float) i->sink_usec, (float) i->source_usec, (float) i->transport_usec, total,
315 i->synchronized_clocks ? "yes" : "no");
316 }
317
318 /* Someone requested that the latency is shown */
319 static void sigusr1_signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
320 fprintf(stderr, "Got SIGUSR1, requesting latency.\n");
321 pa_operation_unref(pa_stream_get_latency(stream, stream_get_latency_callback, NULL));
322 }
323
324
325 static void help(const char *argv0) {
326
327 printf("%s [options]\n\n"
328 " -h, --help Show this help\n"
329 " --version Show version\n\n"
330 " -r, --record Create a connection for recording\n"
331 " -p, --playback Create a connection for playback\n\n"
332 " -v, --verbose Enable verbose operations\n\n"
333 " -s, --server=SERVER The name of the server to connect to\n"
334 " -d, --device=DEVICE The name of the sink/source to connect to\n"
335 " -n, --client-name=NAME How to call this client on the server\n"
336 " --stream-name=NAME How to call this stream on the server\n"
337 " --volume=VOLUME Specify the initial (linear) volume in range 0...256\n",
338 argv0);
339 }
340
341 enum {
342 ARG_VERSION = 256,
343 ARG_STREAM_NAME,
344 ARG_VOLUME
345 };
346
347 int main(int argc, char *argv[]) {
348 struct pa_mainloop* m = NULL;
349 int ret = 1, r, c;
350 char *bn, *server = NULL;
351
352 static const struct option long_options[] = {
353 {"record", 0, NULL, 'r'},
354 {"playback", 0, NULL, 'p'},
355 {"device", 1, NULL, 'd'},
356 {"server", 1, NULL, 's'},
357 {"client-name", 1, NULL, 'n'},
358 {"stream-name", 1, NULL, ARG_STREAM_NAME},
359 {"version", 0, NULL, ARG_VERSION},
360 {"help", 0, NULL, 'h'},
361 {"verbose", 0, NULL, 'v'},
362 {"volume", 1, NULL, ARG_VOLUME},
363 {NULL, 0, NULL, 0}
364 };
365
366 if (!(bn = strrchr(argv[0], '/')))
367 bn = argv[0];
368 else
369 bn++;
370
371 if (strstr(bn, "rec") || strstr(bn, "mon"))
372 mode = RECORD;
373 else if (strstr(bn, "cat") || strstr(bn, "play"))
374 mode = PLAYBACK;
375
376 while ((c = getopt_long(argc, argv, "rpd:s:n:hv", long_options, NULL)) != -1) {
377
378 switch (c) {
379 case 'h' :
380 help(bn);
381 ret = 0;
382 goto quit;
383
384 case ARG_VERSION:
385 printf("pacat "PACKAGE_VERSION"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
386 ret = 0;
387 goto quit;
388
389 case 'r':
390 mode = RECORD;
391 break;
392
393 case 'p':
394 mode = PLAYBACK;
395 break;
396
397 case 'd':
398 free(device);
399 device = strdup(optarg);
400 break;
401
402 case 's':
403 free(server);
404 server = strdup(optarg);
405 break;
406
407 case 'n':
408 free(client_name);
409 client_name = strdup(optarg);
410 break;
411
412 case ARG_STREAM_NAME:
413 free(stream_name);
414 stream_name = strdup(optarg);
415 break;
416
417 case 'v':
418 verbose = 1;
419 break;
420
421 case ARG_VOLUME: {
422 int v = atoi(optarg);
423 volume = v < 0 ? 0 : v;
424 break;
425 }
426
427 default:
428 goto quit;
429 }
430 }
431
432 if (!client_name)
433 client_name = strdup(bn);
434
435 if (!stream_name)
436 stream_name = strdup(client_name);
437
438 if (verbose)
439 fprintf(stderr, "Opening a %s stream.\n", mode == RECORD ? "recording" : "playback");
440
441 /* Set up a new main loop */
442 if (!(m = pa_mainloop_new())) {
443 fprintf(stderr, "pa_mainloop_new() failed.\n");
444 goto quit;
445 }
446
447 mainloop_api = pa_mainloop_get_api(m);
448
449 r = pa_signal_init(mainloop_api);
450 assert(r == 0);
451 pa_signal_new(SIGINT, exit_signal_callback, NULL);
452 pa_signal_new(SIGUSR1, sigusr1_signal_callback, NULL);
453 signal(SIGPIPE, SIG_IGN);
454
455 if (!(stdio_event = mainloop_api->io_new(mainloop_api,
456 mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO,
457 mode == PLAYBACK ? PA_IO_EVENT_INPUT : PA_IO_EVENT_OUTPUT,
458 mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) {
459 fprintf(stderr, "source_io() failed.\n");
460 goto quit;
461 }
462
463 /* Create a new connection context */
464 if (!(context = pa_context_new(mainloop_api, client_name))) {
465 fprintf(stderr, "pa_context_new() failed.\n");
466 goto quit;
467 }
468
469 pa_context_set_state_callback(context, context_state_callback, NULL);
470
471 /* Connect the context */
472 pa_context_connect(context, server, 1, NULL);
473
474 /* Run the main loop */
475 if (pa_mainloop_run(m, &ret) < 0) {
476 fprintf(stderr, "pa_mainloop_run() failed.\n");
477 goto quit;
478 }
479
480 quit:
481 if (stream)
482 pa_stream_unref(stream);
483
484 if (context)
485 pa_context_unref(context);
486
487 if (stdio_event) {
488 assert(mainloop_api);
489 mainloop_api->io_free(stdio_event);
490 }
491
492 if (m) {
493 pa_signal_done();
494 pa_mainloop_free(m);
495 }
496
497 free(buffer);
498
499 free(server);
500 free(device);
501 free(client_name);
502 free(stream_name);
503
504 return ret;
505 }