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