]> code.delx.au - pulseaudio/blob - src/utils/pacat.c
fix include line for "core-util.h"
[pulseaudio] / src / utils / 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 Lesser 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 Lesser 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 #include <fcntl.h>
35
36 #include <polyp/polypaudio.h>
37
38 #define TIME_EVENT_USEC 50000
39
40 #if PA_API_VERSION != 9
41 #error Invalid Polypaudio API version
42 #endif
43
44 static enum { RECORD, PLAYBACK } mode = PLAYBACK;
45
46 static pa_context *context = NULL;
47 static pa_stream *stream = NULL;
48 static 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 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 static pa_sample_spec sample_spec = {
61 .format = PA_SAMPLE_S16LE,
62 .rate = 44100,
63 .channels = 2
64 };
65
66 static pa_channel_map channel_map;
67 static int channel_map_set = 0;
68
69 /* A shortcut for terminating the application */
70 static void quit(int ret) {
71 assert(mainloop_api);
72 mainloop_api->quit(mainloop_api, ret);
73 }
74
75 /* Write some data to the stream */
76 static void do_stream_write(size_t length) {
77 size_t l;
78 assert(length);
79
80 if (!buffer || !buffer_length)
81 return;
82
83 l = length;
84 if (l > buffer_length)
85 l = buffer_length;
86
87 if (pa_stream_write(stream, (uint8_t*) buffer + buffer_index, l, NULL, 0, PA_SEEK_RELATIVE) < 0) {
88 fprintf(stderr, "pa_stream_write() failed: %s\n", pa_strerror(pa_context_errno(context)));
89 quit(1);
90 return;
91 }
92
93 buffer_length -= l;
94 buffer_index += l;
95
96 if (!buffer_length) {
97 pa_xfree(buffer);
98 buffer = NULL;
99 buffer_index = buffer_length = 0;
100 }
101 }
102
103 /* This is called whenever new data may be written to the stream */
104 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
105 assert(s && length);
106
107 if (stdio_event)
108 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_INPUT);
109
110 if (!buffer)
111 return;
112
113 do_stream_write(length);
114 }
115
116 /* This is called whenever new data may is available */
117 static void stream_read_callback(pa_stream *s, size_t length, void *userdata) {
118 const void *data;
119 assert(s && length);
120
121 if (stdio_event)
122 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_OUTPUT);
123
124 if (pa_stream_peek(s, &data, &length) < 0) {
125 fprintf(stderr, "pa_stream_peek() failed: %s\n", pa_strerror(pa_context_errno(context)));
126 quit(1);
127 return;
128 }
129
130 assert(data && length);
131
132 if (buffer) {
133 fprintf(stderr, "Buffer overrun, dropping incoming data\n");
134 if (pa_stream_drop(s) < 0) {
135 fprintf(stderr, "pa_stream_drop() failed: %s\n", pa_strerror(pa_context_errno(context)));
136 quit(1);
137 }
138 return;
139 }
140
141 buffer = pa_xmalloc(buffer_length = length);
142 memcpy(buffer, data, length);
143 buffer_index = 0;
144 pa_stream_drop(s);
145 }
146
147 /* This routine is called whenever the stream state changes */
148 static void stream_state_callback(pa_stream *s, void *userdata) {
149 assert(s);
150
151 switch (pa_stream_get_state(s)) {
152 case PA_STREAM_CREATING:
153 case PA_STREAM_TERMINATED:
154 break;
155
156 case PA_STREAM_READY:
157 if (verbose)
158 fprintf(stderr, "Stream successfully created.\n");
159 break;
160
161 case PA_STREAM_FAILED:
162 default:
163 fprintf(stderr, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
164 quit(1);
165 }
166 }
167
168 /* This is called whenever the context status changes */
169 static void context_state_callback(pa_context *c, void *userdata) {
170 assert(c);
171
172 switch (pa_context_get_state(c)) {
173 case PA_CONTEXT_CONNECTING:
174 case PA_CONTEXT_AUTHORIZING:
175 case PA_CONTEXT_SETTING_NAME:
176 break;
177
178 case PA_CONTEXT_READY: {
179 int r;
180
181 assert(c && !stream);
182
183 if (verbose)
184 fprintf(stderr, "Connection established.\n");
185
186 if (!(stream = pa_stream_new(c, stream_name, &sample_spec, channel_map_set ? &channel_map : NULL))) {
187 fprintf(stderr, "pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(c)));
188 goto fail;
189 }
190
191 pa_stream_set_state_callback(stream, stream_state_callback, NULL);
192 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
193 pa_stream_set_read_callback(stream, stream_read_callback, NULL);
194
195 if (mode == PLAYBACK) {
196 pa_cvolume cv;
197 if ((r = pa_stream_connect_playback(stream, device, NULL, 0, pa_cvolume_set(&cv, sample_spec.channels, volume), NULL)) < 0) {
198 fprintf(stderr, "pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(c)));
199 goto fail;
200 }
201
202 } else {
203 if ((r = pa_stream_connect_record(stream, device, NULL, 0)) < 0) {
204 fprintf(stderr, "pa_stream_connect_record() failed: %s\n", pa_strerror(pa_context_errno(c)));
205 goto fail;
206 }
207 }
208
209 break;
210 }
211
212 case PA_CONTEXT_TERMINATED:
213 quit(0);
214 break;
215
216 case PA_CONTEXT_FAILED:
217 default:
218 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
219 goto fail;
220 }
221
222 return;
223
224 fail:
225 quit(1);
226
227 }
228
229 /* Connection draining complete */
230 static void context_drain_complete(pa_context*c, void *userdata) {
231 pa_context_disconnect(c);
232 }
233
234 /* Stream draining complete */
235 static void stream_drain_complete(pa_stream*s, int success, void *userdata) {
236 pa_operation *o;
237
238 if (!success) {
239 fprintf(stderr, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context)));
240 quit(1);
241 }
242
243 if (verbose)
244 fprintf(stderr, "Playback stream drained.\n");
245
246 pa_stream_disconnect(stream);
247 pa_stream_unref(stream);
248 stream = NULL;
249
250 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
251 pa_context_disconnect(context);
252 else {
253 if (verbose)
254 fprintf(stderr, "Draining connection to server.\n");
255 }
256 }
257
258 /* New data on STDIN **/
259 static void stdin_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
260 size_t l, w = 0;
261 ssize_t r;
262 assert(a == mainloop_api && e && stdio_event == e);
263
264 if (buffer) {
265 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
266 return;
267 }
268
269 if (!stream || pa_stream_get_state(stream) != PA_STREAM_READY || !(l = w = pa_stream_writable_size(stream)))
270 l = 4096;
271
272 buffer = pa_xmalloc(l);
273
274 if ((r = read(fd, buffer, l)) <= 0) {
275 if (r == 0) {
276 pa_operation *o;
277
278 if (verbose)
279 fprintf(stderr, "Got EOF.\n");
280
281 if (!(o = pa_stream_drain(stream, stream_drain_complete, NULL))) {
282 fprintf(stderr, "pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(context)));
283 quit(1);
284 return;
285 }
286
287 pa_operation_unref(o);
288 } else {
289 fprintf(stderr, "read() failed: %s\n", strerror(errno));
290 quit(1);
291 }
292
293 mainloop_api->io_free(stdio_event);
294 stdio_event = NULL;
295 return;
296 }
297
298 buffer_length = r;
299 buffer_index = 0;
300
301 if (w)
302 do_stream_write(w);
303 }
304
305 /* Some data may be written to STDOUT */
306 static void stdout_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
307 ssize_t r;
308 assert(a == mainloop_api && e && stdio_event == e);
309
310 if (!buffer) {
311 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
312 return;
313 }
314
315 assert(buffer_length);
316
317 if ((r = write(fd, (uint8_t*) buffer+buffer_index, buffer_length)) <= 0) {
318 fprintf(stderr, "write() failed: %s\n", strerror(errno));
319 quit(1);
320
321 mainloop_api->io_free(stdio_event);
322 stdio_event = NULL;
323 return;
324 }
325
326 buffer_length -= r;
327 buffer_index += r;
328
329 if (!buffer_length) {
330 pa_xfree(buffer);
331 buffer = NULL;
332 buffer_length = buffer_index = 0;
333 }
334 }
335
336 /* UNIX signal to quit recieved */
337 static void exit_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
338 if (verbose)
339 fprintf(stderr, "Got signal, exiting.\n");
340 quit(0);
341 }
342
343 /* Show the current latency */
344 static void stream_update_timing_callback(pa_stream *s, int success, void *userdata) {
345 pa_usec_t latency, usec;
346 int negative = 0;
347
348 assert(s);
349
350 if (!success ||
351 pa_stream_get_time(s, &usec) < 0 ||
352 pa_stream_get_latency(s, &latency, &negative) < 0) {
353 fprintf(stderr, "Failed to get latency: %s\n", pa_strerror(pa_context_errno(context)));
354 quit(1);
355 return;
356 }
357
358 fprintf(stderr, "Time: %0.3f sec; Latency: %0.0f usec. \r",
359 (float) usec / 1000000,
360 (float) latency * (negative?-1:1));
361 }
362
363 /* Someone requested that the latency is shown */
364 static void sigusr1_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
365
366 if (!stream)
367 return;
368
369 pa_operation_unref(pa_stream_update_timing_info(stream, stream_update_timing_callback, NULL));
370 }
371
372 static void time_event_callback(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
373 struct timeval next;
374
375 if (stream && pa_stream_get_state(stream) == PA_STREAM_READY) {
376 pa_operation *o;
377 if (!(o = pa_stream_update_timing_info(stream, stream_update_timing_callback, NULL)))
378 fprintf(stderr, "pa_stream_update_timing_info() failed: %s\n", pa_strerror(pa_context_errno(context)));
379 else
380 pa_operation_unref(o);
381 }
382
383 pa_gettimeofday(&next);
384 pa_timeval_add(&next, TIME_EVENT_USEC);
385
386 m->time_restart(e, &next);
387 }
388
389 static void help(const char *argv0) {
390
391 printf("%s [options]\n\n"
392 " -h, --help Show this help\n"
393 " --version Show version\n\n"
394 " -r, --record Create a connection for recording\n"
395 " -p, --playback Create a connection for playback\n\n"
396 " -v, --verbose Enable verbose operations\n\n"
397 " -s, --server=SERVER The name of the server to connect to\n"
398 " -d, --device=DEVICE The name of the sink/source to connect to\n"
399 " -n, --client-name=NAME How to call this client on the server\n"
400 " --stream-name=NAME How to call this stream on the server\n"
401 " --volume=VOLUME Specify the initial (linear) volume in range 0...65536\n"
402 " --rate=SAMPLERATE The sample rate in Hz (defaults to 44100)\n"
403 " --format=SAMPLEFORMAT The sample type, one of s16le, s16be, u8, float32le,\n"
404 " float32be, ulaw, alaw (defaults to s16ne)\n"
405 " --channels=CHANNELS The number of channels, 1 for mono, 2 for stereo\n"
406 " (defaults to 2)\n"
407 " --channel-map=CHANNELMAP Channel map to use instead of the default\n",
408 argv0);
409 }
410
411 enum {
412 ARG_VERSION = 256,
413 ARG_STREAM_NAME,
414 ARG_VOLUME,
415 ARG_SAMPLERATE,
416 ARG_SAMPLEFORMAT,
417 ARG_CHANNELS,
418 ARG_CHANNELMAP,
419 };
420
421 int main(int argc, char *argv[]) {
422 pa_mainloop* m = NULL;
423 int ret = 1, r, c;
424 char *bn, *server = NULL;
425 pa_time_event *time_event = NULL;
426
427 static const struct option long_options[] = {
428 {"record", 0, NULL, 'r'},
429 {"playback", 0, NULL, 'p'},
430 {"device", 1, NULL, 'd'},
431 {"server", 1, NULL, 's'},
432 {"client-name", 1, NULL, 'n'},
433 {"stream-name", 1, NULL, ARG_STREAM_NAME},
434 {"version", 0, NULL, ARG_VERSION},
435 {"help", 0, NULL, 'h'},
436 {"verbose", 0, NULL, 'v'},
437 {"volume", 1, NULL, ARG_VOLUME},
438 {"rate", 1, NULL, ARG_SAMPLERATE},
439 {"format", 1, NULL, ARG_SAMPLEFORMAT},
440 {"channels", 1, NULL, ARG_CHANNELS},
441 {"channel-map", 1, NULL, ARG_CHANNELMAP},
442 {NULL, 0, NULL, 0}
443 };
444
445 if (!(bn = strrchr(argv[0], '/')))
446 bn = argv[0];
447 else
448 bn++;
449
450 if (strstr(bn, "rec") || strstr(bn, "mon"))
451 mode = RECORD;
452 else if (strstr(bn, "cat") || strstr(bn, "play"))
453 mode = PLAYBACK;
454
455 while ((c = getopt_long(argc, argv, "rpd:s:n:hv", long_options, NULL)) != -1) {
456
457 switch (c) {
458 case 'h' :
459 help(bn);
460 ret = 0;
461 goto quit;
462
463 case ARG_VERSION:
464 printf("pacat "PACKAGE_VERSION"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
465 ret = 0;
466 goto quit;
467
468 case 'r':
469 mode = RECORD;
470 break;
471
472 case 'p':
473 mode = PLAYBACK;
474 break;
475
476 case 'd':
477 pa_xfree(device);
478 device = pa_xstrdup(optarg);
479 break;
480
481 case 's':
482 pa_xfree(server);
483 server = pa_xstrdup(optarg);
484 break;
485
486 case 'n':
487 pa_xfree(client_name);
488 client_name = pa_xstrdup(optarg);
489 break;
490
491 case ARG_STREAM_NAME:
492 pa_xfree(stream_name);
493 stream_name = pa_xstrdup(optarg);
494 break;
495
496 case 'v':
497 verbose = 1;
498 break;
499
500 case ARG_VOLUME: {
501 int v = atoi(optarg);
502 volume = v < 0 ? 0 : v;
503 break;
504 }
505
506 case ARG_CHANNELS:
507 sample_spec.channels = atoi(optarg);
508 break;
509
510 case ARG_SAMPLEFORMAT:
511 sample_spec.format = pa_parse_sample_format(optarg);
512 break;
513
514 case ARG_SAMPLERATE:
515 sample_spec.rate = atoi(optarg);
516 break;
517
518 case ARG_CHANNELMAP:
519 if (!pa_channel_map_parse(&channel_map, optarg)) {
520 fprintf(stderr, "Invalid channel map\n");
521 goto quit;
522 }
523
524 channel_map_set = 1;
525 break;
526
527 default:
528 goto quit;
529 }
530 }
531
532 if (!pa_sample_spec_valid(&sample_spec)) {
533 fprintf(stderr, "Invalid sample specification\n");
534 goto quit;
535 }
536
537 if (channel_map_set && channel_map.channels != sample_spec.channels) {
538 fprintf(stderr, "Channel map doesn't match sample specification\n");
539 goto quit;
540 }
541
542 if (verbose) {
543 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
544 pa_sample_spec_snprint(t, sizeof(t), &sample_spec);
545 fprintf(stderr, "Opening a %s stream with sample specification '%s'.\n", mode == RECORD ? "recording" : "playback", t);
546 }
547
548 if (!(optind >= argc)) {
549 if (optind+1 == argc) {
550 int fd;
551
552 if ((fd = open(argv[optind], mode == PLAYBACK ? O_RDONLY : O_WRONLY|O_TRUNC|O_CREAT, 0666)) < 0) {
553 fprintf(stderr, "open(): %s\n", strerror(errno));
554 goto quit;
555 }
556
557 if (dup2(fd, mode == PLAYBACK ? 0 : 1) < 0) {
558 fprintf(stderr, "dup2(): %s\n", strerror(errno));
559 goto quit;
560 }
561
562 close(fd);
563
564 if (!stream_name)
565 stream_name = pa_xstrdup(argv[optind]);
566
567 } else {
568 fprintf(stderr, "Too many arguments.\n");
569 goto quit;
570 }
571 }
572
573 if (!client_name)
574 client_name = pa_xstrdup(bn);
575
576 if (!stream_name)
577 stream_name = pa_xstrdup(client_name);
578
579 /* Set up a new main loop */
580 if (!(m = pa_mainloop_new())) {
581 fprintf(stderr, "pa_mainloop_new() failed.\n");
582 goto quit;
583 }
584
585 mainloop_api = pa_mainloop_get_api(m);
586
587 r = pa_signal_init(mainloop_api);
588 assert(r == 0);
589 pa_signal_new(SIGINT, exit_signal_callback, NULL);
590 pa_signal_new(SIGTERM, exit_signal_callback, NULL);
591 #ifdef SIGUSR1
592 pa_signal_new(SIGUSR1, sigusr1_signal_callback, NULL);
593 #endif
594 #ifdef SIGPIPE
595 signal(SIGPIPE, SIG_IGN);
596 #endif
597
598 if (!(stdio_event = mainloop_api->io_new(mainloop_api,
599 mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO,
600 mode == PLAYBACK ? PA_IO_EVENT_INPUT : PA_IO_EVENT_OUTPUT,
601 mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) {
602 fprintf(stderr, "io_new() failed.\n");
603 goto quit;
604 }
605
606 /* Create a new connection context */
607 if (!(context = pa_context_new(mainloop_api, client_name))) {
608 fprintf(stderr, "pa_context_new() failed.\n");
609 goto quit;
610 }
611
612 pa_context_set_state_callback(context, context_state_callback, NULL);
613
614 /* Connect the context */
615 pa_context_connect(context, server, 0, NULL);
616
617 if (verbose) {
618 struct timeval tv;
619
620 pa_gettimeofday(&tv);
621 pa_timeval_add(&tv, TIME_EVENT_USEC);
622
623 if (!(time_event = mainloop_api->time_new(mainloop_api, &tv, time_event_callback, NULL))) {
624 fprintf(stderr, "time_new() failed.\n");
625 goto quit;
626 }
627 }
628
629 /* Run the main loop */
630 if (pa_mainloop_run(m, &ret) < 0) {
631 fprintf(stderr, "pa_mainloop_run() failed.\n");
632 goto quit;
633 }
634
635 quit:
636 if (stream)
637 pa_stream_unref(stream);
638
639 if (context)
640 pa_context_unref(context);
641
642 if (stdio_event) {
643 assert(mainloop_api);
644 mainloop_api->io_free(stdio_event);
645 }
646
647 if (time_event) {
648 assert(mainloop_api);
649 mainloop_api->time_free(time_event);
650 }
651
652 if (m) {
653 pa_signal_done();
654 pa_mainloop_free(m);
655 }
656
657 pa_xfree(buffer);
658
659 pa_xfree(server);
660 pa_xfree(device);
661 pa_xfree(client_name);
662 pa_xfree(stream_name);
663
664 return ret;
665 }