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