]> code.delx.au - pulseaudio/blob - src/utils/pacat.c
3be1f6c8a8aebd339c97584e63a759ae4b001e8b
[pulseaudio] / src / utils / pacat.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <signal.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <assert.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <getopt.h>
35 #include <fcntl.h>
36 #include <locale.h>
37
38 #include <sndfile.h>
39
40 #include <pulse/pulseaudio.h>
41 #include <pulse/rtclock.h>
42
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/i18n.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/macro.h>
47 #include <pulsecore/sndfile-util.h>
48
49 #define TIME_EVENT_USEC 50000
50
51 #define CLEAR_LINE "\x1B[K"
52
53 static enum { RECORD, PLAYBACK } mode = PLAYBACK;
54
55 static pa_context *context = NULL;
56 static pa_stream *stream = NULL;
57 static pa_mainloop_api *mainloop_api = NULL;
58
59 static void *buffer = NULL;
60 static size_t buffer_length = 0, buffer_index = 0;
61
62 static pa_io_event* stdio_event = NULL;
63
64 static pa_proplist *proplist = NULL;
65 static char *device = NULL;
66
67 static SNDFILE* sndfile = NULL;
68
69 static pa_bool_t verbose = FALSE;
70 static pa_volume_t volume = PA_VOLUME_NORM;
71 static pa_bool_t volume_is_set = FALSE;
72
73 static pa_sample_spec sample_spec = {
74 .format = PA_SAMPLE_S16LE,
75 .rate = 44100,
76 .channels = 2
77 };
78 static pa_bool_t sample_spec_set = FALSE;
79
80 static pa_channel_map channel_map;
81 static pa_bool_t channel_map_set = FALSE;
82
83 static sf_count_t (*readf_function)(SNDFILE *_sndfile, void *ptr, sf_count_t frames) = NULL;
84 static sf_count_t (*writef_function)(SNDFILE *_sndfile, const void *ptr, sf_count_t frames) = NULL;
85
86 static pa_stream_flags_t flags = 0;
87
88 static size_t latency = 0, process_time = 0;
89 static int32_t latency_msec = 0, process_time_msec = 0;
90
91 static pa_bool_t raw = TRUE;
92 static int file_format = -1;
93
94 static uint32_t cork_requests = 0;
95
96 /* A shortcut for terminating the application */
97 static void quit(int ret) {
98 pa_assert(mainloop_api);
99 mainloop_api->quit(mainloop_api, ret);
100 }
101
102 /* Connection draining complete */
103 static void context_drain_complete(pa_context*c, void *userdata) {
104 pa_context_disconnect(c);
105 }
106
107 /* Stream draining complete */
108 static void stream_drain_complete(pa_stream*s, int success, void *userdata) {
109 pa_operation *o = NULL;
110
111 if (!success) {
112 pa_log(_("Failed to drain stream: %s"), pa_strerror(pa_context_errno(context)));
113 quit(1);
114 }
115
116 if (verbose)
117 pa_log(_("Playback stream drained."));
118
119 pa_stream_disconnect(stream);
120 pa_stream_unref(stream);
121 stream = NULL;
122
123 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
124 pa_context_disconnect(context);
125 else {
126 pa_operation_unref(o);
127 if (verbose)
128 pa_log(_("Draining connection to server."));
129 }
130 }
131
132 /* Start draining */
133 static void start_drain(void) {
134
135 if (stream) {
136 pa_operation *o;
137
138 pa_stream_set_write_callback(stream, NULL, NULL);
139
140 if (!(o = pa_stream_drain(stream, stream_drain_complete, NULL))) {
141 pa_log(_("pa_stream_drain(): %s"), pa_strerror(pa_context_errno(context)));
142 quit(1);
143 return;
144 }
145
146 pa_operation_unref(o);
147 } else
148 quit(0);
149 }
150
151 /* Write some data to the stream */
152 static void do_stream_write(size_t length) {
153 size_t l;
154 pa_assert(length);
155
156 if (!buffer || !buffer_length)
157 return;
158
159 l = length;
160 if (l > buffer_length)
161 l = buffer_length;
162
163 if (pa_stream_write(stream, (uint8_t*) buffer + buffer_index, l, NULL, 0, PA_SEEK_RELATIVE) < 0) {
164 pa_log(_("pa_stream_write() failed: %s"), pa_strerror(pa_context_errno(context)));
165 quit(1);
166 return;
167 }
168
169 buffer_length -= l;
170 buffer_index += l;
171
172 if (!buffer_length) {
173 pa_xfree(buffer);
174 buffer = NULL;
175 buffer_index = buffer_length = 0;
176 }
177 }
178
179 /* This is called whenever new data may be written to the stream */
180 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
181 pa_assert(s);
182 pa_assert(length > 0);
183
184 if (raw) {
185 pa_assert(!sndfile);
186
187 if (stdio_event)
188 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_INPUT);
189
190 if (!buffer)
191 return;
192
193 do_stream_write(length);
194
195 } else {
196 sf_count_t bytes;
197 void *data;
198
199 pa_assert(sndfile);
200
201 for (;;) {
202 size_t data_length = length;
203
204 if (pa_stream_begin_write(s, &data, &data_length) < 0) {
205 pa_log(_("pa_stream_begin_write() failed: %s"), pa_strerror(pa_context_errno(context)));
206 quit(1);
207 return;
208 }
209
210 if (readf_function) {
211 size_t k = pa_frame_size(&sample_spec);
212
213 if ((bytes = readf_function(sndfile, data, (sf_count_t) (data_length/k))) > 0)
214 bytes *= (sf_count_t) k;
215
216 } else
217 bytes = sf_read_raw(sndfile, data, (sf_count_t) data_length);
218
219 if (bytes > 0)
220 pa_stream_write(s, data, (size_t) bytes, NULL, 0, PA_SEEK_RELATIVE);
221 else
222 pa_stream_cancel_write(s);
223
224 /* EOF? */
225 if (bytes < (sf_count_t) data_length) {
226 start_drain();
227 break;
228 }
229
230 /* Request fulfilled */
231 if ((size_t) bytes >= length)
232 break;
233
234 length -= bytes;
235 }
236 }
237 }
238
239 /* This is called whenever new data may is available */
240 static void stream_read_callback(pa_stream *s, size_t length, void *userdata) {
241
242 pa_assert(s);
243 pa_assert(length > 0);
244
245 if (raw) {
246 pa_assert(!sndfile);
247
248 if (stdio_event)
249 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_OUTPUT);
250
251 while (pa_stream_readable_size(s) > 0) {
252 const void *data;
253
254 if (pa_stream_peek(s, &data, &length) < 0) {
255 pa_log(_("pa_stream_peek() failed: %s"), pa_strerror(pa_context_errno(context)));
256 quit(1);
257 return;
258 }
259
260 pa_assert(data);
261 pa_assert(length > 0);
262
263 if (buffer) {
264 buffer = pa_xrealloc(buffer, buffer_length + length);
265 memcpy((uint8_t*) buffer + buffer_length, data, length);
266 buffer_length += length;
267 } else {
268 buffer = pa_xmalloc(length);
269 memcpy(buffer, data, length);
270 buffer_length = length;
271 buffer_index = 0;
272 }
273
274 pa_stream_drop(s);
275 }
276
277 } else {
278 pa_assert(sndfile);
279
280 while (pa_stream_readable_size(s) > 0) {
281 sf_count_t bytes;
282 const void *data;
283
284 if (pa_stream_peek(s, &data, &length) < 0) {
285 pa_log(_("pa_stream_peek() failed: %s"), pa_strerror(pa_context_errno(context)));
286 quit(1);
287 return;
288 }
289
290 pa_assert(data);
291 pa_assert(length > 0);
292
293 if (writef_function) {
294 size_t k = pa_frame_size(&sample_spec);
295
296 if ((bytes = writef_function(sndfile, data, (sf_count_t) (length/k))) > 0)
297 bytes *= (sf_count_t) k;
298
299 } else
300 bytes = sf_write_raw(sndfile, data, (sf_count_t) length);
301
302 if (bytes < (sf_count_t) length)
303 quit(1);
304
305 pa_stream_drop(s);
306 }
307 }
308 }
309
310 /* This routine is called whenever the stream state changes */
311 static void stream_state_callback(pa_stream *s, void *userdata) {
312 pa_assert(s);
313
314 switch (pa_stream_get_state(s)) {
315 case PA_STREAM_CREATING:
316 case PA_STREAM_TERMINATED:
317 break;
318
319 case PA_STREAM_READY:
320
321 if (verbose) {
322 const pa_buffer_attr *a;
323 char cmt[PA_CHANNEL_MAP_SNPRINT_MAX], sst[PA_SAMPLE_SPEC_SNPRINT_MAX];
324
325 pa_log(_("Stream successfully created."));
326
327 if (!(a = pa_stream_get_buffer_attr(s)))
328 pa_log(_("pa_stream_get_buffer_attr() failed: %s"), pa_strerror(pa_context_errno(pa_stream_get_context(s))));
329 else {
330
331 if (mode == PLAYBACK)
332 pa_log(_("Buffer metrics: maxlength=%u, tlength=%u, prebuf=%u, minreq=%u"), a->maxlength, a->tlength, a->prebuf, a->minreq);
333 else {
334 pa_assert(mode == RECORD);
335 pa_log(_("Buffer metrics: maxlength=%u, fragsize=%u"), a->maxlength, a->fragsize);
336 }
337 }
338
339 pa_log(_("Using sample spec '%s', channel map '%s'."),
340 pa_sample_spec_snprint(sst, sizeof(sst), pa_stream_get_sample_spec(s)),
341 pa_channel_map_snprint(cmt, sizeof(cmt), pa_stream_get_channel_map(s)));
342
343 pa_log(_("Connected to device %s (%u, %ssuspended)."),
344 pa_stream_get_device_name(s),
345 pa_stream_get_device_index(s),
346 pa_stream_is_suspended(s) ? "" : "not ");
347 }
348
349 break;
350
351 case PA_STREAM_FAILED:
352 default:
353 pa_log(_("Stream error: %s"), pa_strerror(pa_context_errno(pa_stream_get_context(s))));
354 quit(1);
355 }
356 }
357
358 static void stream_suspended_callback(pa_stream *s, void *userdata) {
359 pa_assert(s);
360
361 if (verbose) {
362 if (pa_stream_is_suspended(s))
363 pa_log(_("Stream device suspended.%s"), CLEAR_LINE);
364 else
365 pa_log(_("Stream device resumed.%s"), CLEAR_LINE);
366 }
367 }
368
369 static void stream_underflow_callback(pa_stream *s, void *userdata) {
370 pa_assert(s);
371
372 if (verbose)
373 pa_log(_("Stream underrun.%s"), CLEAR_LINE);
374 }
375
376 static void stream_overflow_callback(pa_stream *s, void *userdata) {
377 pa_assert(s);
378
379 if (verbose)
380 pa_log(_("Stream overrun.%s"), CLEAR_LINE);
381 }
382
383 static void stream_started_callback(pa_stream *s, void *userdata) {
384 pa_assert(s);
385
386 if (verbose)
387 pa_log(_("Stream started.%s"), CLEAR_LINE);
388 }
389
390 static void stream_moved_callback(pa_stream *s, void *userdata) {
391 pa_assert(s);
392
393 if (verbose)
394 pa_log(_("Stream moved to device %s (%u, %ssuspended).%s"), pa_stream_get_device_name(s), pa_stream_get_device_index(s), pa_stream_is_suspended(s) ? "" : _("not "), CLEAR_LINE);
395 }
396
397 static void stream_buffer_attr_callback(pa_stream *s, void *userdata) {
398 pa_assert(s);
399
400 if (verbose)
401 pa_log(_("Stream buffer attributes changed.%s"), CLEAR_LINE);
402 }
403
404 static void stream_event_callback(pa_stream *s, const char *name, pa_proplist *pl, void *userdata) {
405 char *t;
406
407 pa_assert(s);
408 pa_assert(name);
409 pa_assert(pl);
410
411 t = pa_proplist_to_string_sep(pl, ", ");
412 pa_log("Got event '%s', properties '%s'", name, t);
413
414 if (pa_streq(name, PA_STREAM_EVENT_REQUEST_CORK)) {
415 if (cork_requests == 0) {
416 pa_log(_("Cork request stack is empty: corking stream"));
417 pa_operation_unref(pa_stream_cork(s, 1, NULL, NULL));
418 }
419 cork_requests++;
420 } else if (pa_streq(name, PA_STREAM_EVENT_REQUEST_UNCORK)) {
421 if (cork_requests == 1) {
422 pa_log(_("Cork request stack is empty: uncorking stream"));
423 pa_operation_unref(pa_stream_cork(s, 0, NULL, NULL));
424 }
425 if (cork_requests == 0)
426 pa_log(_("Warning: Received more uncork requests than cork requests!"));
427 else
428 cork_requests--;
429 }
430
431 pa_xfree(t);
432 }
433
434 /* This is called whenever the context status changes */
435 static void context_state_callback(pa_context *c, void *userdata) {
436 pa_assert(c);
437
438 switch (pa_context_get_state(c)) {
439 case PA_CONTEXT_CONNECTING:
440 case PA_CONTEXT_AUTHORIZING:
441 case PA_CONTEXT_SETTING_NAME:
442 break;
443
444 case PA_CONTEXT_READY: {
445 pa_buffer_attr buffer_attr;
446
447 pa_assert(c);
448 pa_assert(!stream);
449
450 if (verbose)
451 pa_log(_("Connection established.%s"), CLEAR_LINE);
452
453 if (!(stream = pa_stream_new_with_proplist(c, NULL, &sample_spec, &channel_map, proplist))) {
454 pa_log(_("pa_stream_new() failed: %s"), pa_strerror(pa_context_errno(c)));
455 goto fail;
456 }
457
458 pa_stream_set_state_callback(stream, stream_state_callback, NULL);
459 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
460 pa_stream_set_read_callback(stream, stream_read_callback, NULL);
461 pa_stream_set_suspended_callback(stream, stream_suspended_callback, NULL);
462 pa_stream_set_moved_callback(stream, stream_moved_callback, NULL);
463 pa_stream_set_underflow_callback(stream, stream_underflow_callback, NULL);
464 pa_stream_set_overflow_callback(stream, stream_overflow_callback, NULL);
465 pa_stream_set_started_callback(stream, stream_started_callback, NULL);
466 pa_stream_set_event_callback(stream, stream_event_callback, NULL);
467 pa_stream_set_buffer_attr_callback(stream, stream_buffer_attr_callback, NULL);
468
469 pa_zero(buffer_attr);
470 buffer_attr.maxlength = (uint32_t) -1;
471 buffer_attr.prebuf = (uint32_t) -1;
472
473 if (latency_msec > 0) {
474 buffer_attr.fragsize = buffer_attr.tlength = pa_usec_to_bytes(latency_msec * PA_USEC_PER_MSEC, &sample_spec);
475 flags |= PA_STREAM_ADJUST_LATENCY;
476 } else if (latency > 0) {
477 buffer_attr.fragsize = buffer_attr.tlength = (uint32_t) latency;
478 flags |= PA_STREAM_ADJUST_LATENCY;
479 } else
480 buffer_attr.fragsize = buffer_attr.tlength = (uint32_t) -1;
481
482 if (process_time_msec > 0) {
483 buffer_attr.minreq = pa_usec_to_bytes(process_time_msec * PA_USEC_PER_MSEC, &sample_spec);
484 } else if (process_time > 0)
485 buffer_attr.minreq = (uint32_t) process_time;
486 else
487 buffer_attr.minreq = (uint32_t) -1;
488
489 if (mode == PLAYBACK) {
490 pa_cvolume cv;
491 if (pa_stream_connect_playback(stream, device, &buffer_attr, flags, volume_is_set ? pa_cvolume_set(&cv, sample_spec.channels, volume) : NULL, NULL) < 0) {
492 pa_log(_("pa_stream_connect_playback() failed: %s"), pa_strerror(pa_context_errno(c)));
493 goto fail;
494 }
495
496 } else {
497 if (pa_stream_connect_record(stream, device, &buffer_attr, flags) < 0) {
498 pa_log(_("pa_stream_connect_record() failed: %s"), pa_strerror(pa_context_errno(c)));
499 goto fail;
500 }
501 }
502
503 break;
504 }
505
506 case PA_CONTEXT_TERMINATED:
507 quit(0);
508 break;
509
510 case PA_CONTEXT_FAILED:
511 default:
512 pa_log(_("Connection failure: %s"), pa_strerror(pa_context_errno(c)));
513 goto fail;
514 }
515
516 return;
517
518 fail:
519 quit(1);
520
521 }
522
523 /* New data on STDIN **/
524 static void stdin_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
525 size_t l, w = 0;
526 ssize_t r;
527
528 pa_assert(a == mainloop_api);
529 pa_assert(e);
530 pa_assert(stdio_event == e);
531
532 if (buffer) {
533 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
534 return;
535 }
536
537 if (!stream || pa_stream_get_state(stream) != PA_STREAM_READY || !(l = w = pa_stream_writable_size(stream)))
538 l = 4096;
539
540 buffer = pa_xmalloc(l);
541
542 if ((r = read(fd, buffer, l)) <= 0) {
543 if (r == 0) {
544 if (verbose)
545 pa_log(_("Got EOF."));
546
547 start_drain();
548
549 } else {
550 pa_log(_("read() failed: %s"), strerror(errno));
551 quit(1);
552 }
553
554 mainloop_api->io_free(stdio_event);
555 stdio_event = NULL;
556 return;
557 }
558
559 buffer_length = (uint32_t) r;
560 buffer_index = 0;
561
562 if (w)
563 do_stream_write(w);
564 }
565
566 /* Some data may be written to STDOUT */
567 static void stdout_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
568 ssize_t r;
569
570 pa_assert(a == mainloop_api);
571 pa_assert(e);
572 pa_assert(stdio_event == e);
573
574 if (!buffer) {
575 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
576 return;
577 }
578
579 pa_assert(buffer_length);
580
581 if ((r = write(fd, (uint8_t*) buffer+buffer_index, buffer_length)) <= 0) {
582 pa_log(_("write() failed: %s"), strerror(errno));
583 quit(1);
584
585 mainloop_api->io_free(stdio_event);
586 stdio_event = NULL;
587 return;
588 }
589
590 buffer_length -= (uint32_t) r;
591 buffer_index += (uint32_t) r;
592
593 if (!buffer_length) {
594 pa_xfree(buffer);
595 buffer = NULL;
596 buffer_length = buffer_index = 0;
597 }
598 }
599
600 /* UNIX signal to quit received */
601 static void exit_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
602 if (verbose)
603 pa_log(_("Got signal, exiting."));
604 quit(0);
605 }
606
607 /* Show the current latency */
608 static void stream_update_timing_callback(pa_stream *s, int success, void *userdata) {
609 pa_usec_t l, usec;
610 int negative = 0;
611
612 pa_assert(s);
613
614 if (!success ||
615 pa_stream_get_time(s, &usec) < 0 ||
616 pa_stream_get_latency(s, &l, &negative) < 0) {
617 pa_log(_("Failed to get latency: %s"), pa_strerror(pa_context_errno(context)));
618 quit(1);
619 return;
620 }
621
622 fprintf(stderr, _("Time: %0.3f sec; Latency: %0.0f usec."),
623 (float) usec / 1000000,
624 (float) l * (negative?-1.0f:1.0f));
625 fprintf(stderr, " \r");
626 }
627
628 #ifdef SIGUSR1
629 /* Someone requested that the latency is shown */
630 static void sigusr1_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
631
632 if (!stream)
633 return;
634
635 pa_operation_unref(pa_stream_update_timing_info(stream, stream_update_timing_callback, NULL));
636 }
637 #endif
638
639 static void time_event_callback(pa_mainloop_api *m, pa_time_event *e, const struct timeval *t, void *userdata) {
640 if (stream && pa_stream_get_state(stream) == PA_STREAM_READY) {
641 pa_operation *o;
642 if (!(o = pa_stream_update_timing_info(stream, stream_update_timing_callback, NULL)))
643 pa_log(_("pa_stream_update_timing_info() failed: %s"), pa_strerror(pa_context_errno(context)));
644 else
645 pa_operation_unref(o);
646 }
647
648 pa_context_rttime_restart(context, e, pa_rtclock_now() + TIME_EVENT_USEC);
649 }
650
651 static void help(const char *argv0) {
652
653 printf(_("%s [options]\n\n"
654 " -h, --help Show this help\n"
655 " --version Show version\n\n"
656 " -r, --record Create a connection for recording\n"
657 " -p, --playback Create a connection for playback\n\n"
658 " -v, --verbose Enable verbose operations\n\n"
659 " -s, --server=SERVER The name of the server to connect to\n"
660 " -d, --device=DEVICE The name of the sink/source to connect to\n"
661 " -n, --client-name=NAME How to call this client on the server\n"
662 " --stream-name=NAME How to call this stream on the server\n"
663 " --volume=VOLUME Specify the initial (linear) volume in range 0...65536\n"
664 " --rate=SAMPLERATE The sample rate in Hz (defaults to 44100)\n"
665 " --format=SAMPLEFORMAT The sample type, one of s16le, s16be, u8, float32le,\n"
666 " float32be, ulaw, alaw, s32le, s32be, s24le, s24be,\n"
667 " s24-32le, s24-32be (defaults to s16ne)\n"
668 " --channels=CHANNELS The number of channels, 1 for mono, 2 for stereo\n"
669 " (defaults to 2)\n"
670 " --channel-map=CHANNELMAP Channel map to use instead of the default\n"
671 " --fix-format Take the sample format from the sink the stream is\n"
672 " being connected to.\n"
673 " --fix-rate Take the sampling rate from the sink the stream is\n"
674 " being connected to.\n"
675 " --fix-channels Take the number of channels and the channel map\n"
676 " from the sink the stream is being connected to.\n"
677 " --no-remix Don't upmix or downmix channels.\n"
678 " --no-remap Map channels by index instead of name.\n"
679 " --latency=BYTES Request the specified latency in bytes.\n"
680 " --process-time=BYTES Request the specified process time per request in bytes.\n"
681 " --latency-msec=MSEC Request the specified latency in msec.\n"
682 " --process-time-msec=MSEC Request the specified process time per request in msec.\n"
683 " --property=PROPERTY=VALUE Set the specified property to the specified value.\n"
684 " --raw Record/play raw PCM data.\n"
685 " --passthrough passthrough data \n"
686 " --file-format[=FFORMAT] Record/play formatted PCM data.\n"
687 " --list-file-formats List available file formats.\n")
688 , argv0);
689 }
690
691 enum {
692 ARG_VERSION = 256,
693 ARG_STREAM_NAME,
694 ARG_VOLUME,
695 ARG_SAMPLERATE,
696 ARG_SAMPLEFORMAT,
697 ARG_CHANNELS,
698 ARG_CHANNELMAP,
699 ARG_FIX_FORMAT,
700 ARG_FIX_RATE,
701 ARG_FIX_CHANNELS,
702 ARG_NO_REMAP,
703 ARG_NO_REMIX,
704 ARG_LATENCY,
705 ARG_PROCESS_TIME,
706 ARG_RAW,
707 ARG_PASSTHROUGH,
708 ARG_PROPERTY,
709 ARG_FILE_FORMAT,
710 ARG_LIST_FILE_FORMATS,
711 ARG_LATENCY_MSEC,
712 ARG_PROCESS_TIME_MSEC
713 };
714
715 int main(int argc, char *argv[]) {
716 pa_mainloop* m = NULL;
717 int ret = 1, c;
718 char *bn, *server = NULL;
719 pa_time_event *time_event = NULL;
720 const char *filename = NULL;
721
722 static const struct option long_options[] = {
723 {"record", 0, NULL, 'r'},
724 {"playback", 0, NULL, 'p'},
725 {"device", 1, NULL, 'd'},
726 {"server", 1, NULL, 's'},
727 {"client-name", 1, NULL, 'n'},
728 {"stream-name", 1, NULL, ARG_STREAM_NAME},
729 {"version", 0, NULL, ARG_VERSION},
730 {"help", 0, NULL, 'h'},
731 {"verbose", 0, NULL, 'v'},
732 {"volume", 1, NULL, ARG_VOLUME},
733 {"rate", 1, NULL, ARG_SAMPLERATE},
734 {"format", 1, NULL, ARG_SAMPLEFORMAT},
735 {"channels", 1, NULL, ARG_CHANNELS},
736 {"channel-map", 1, NULL, ARG_CHANNELMAP},
737 {"fix-format", 0, NULL, ARG_FIX_FORMAT},
738 {"fix-rate", 0, NULL, ARG_FIX_RATE},
739 {"fix-channels", 0, NULL, ARG_FIX_CHANNELS},
740 {"no-remap", 0, NULL, ARG_NO_REMAP},
741 {"no-remix", 0, NULL, ARG_NO_REMIX},
742 {"latency", 1, NULL, ARG_LATENCY},
743 {"process-time", 1, NULL, ARG_PROCESS_TIME},
744 {"property", 1, NULL, ARG_PROPERTY},
745 {"raw", 0, NULL, ARG_RAW},
746 {"passthrough", 0, NULL, ARG_PASSTHROUGH},
747 {"file-format", 2, NULL, ARG_FILE_FORMAT},
748 {"list-file-formats", 0, NULL, ARG_LIST_FILE_FORMATS},
749 {"latency-msec", 1, NULL, ARG_LATENCY_MSEC},
750 {"process-time-msec", 1, NULL, ARG_PROCESS_TIME_MSEC},
751 {NULL, 0, NULL, 0}
752 };
753
754 setlocale(LC_ALL, "");
755 bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
756
757 bn = pa_path_get_filename(argv[0]);
758
759 if (strstr(bn, "play")) {
760 mode = PLAYBACK;
761 raw = FALSE;
762 } else if (strstr(bn, "record")) {
763 mode = RECORD;
764 raw = FALSE;
765 } else if (strstr(bn, "cat")) {
766 mode = PLAYBACK;
767 raw = TRUE;
768 } if (strstr(bn, "rec") || strstr(bn, "mon")) {
769 mode = RECORD;
770 raw = TRUE;
771 }
772
773 proplist = pa_proplist_new();
774
775 while ((c = getopt_long(argc, argv, "rpd:s:n:hv", long_options, NULL)) != -1) {
776
777 switch (c) {
778 case 'h' :
779 help(bn);
780 ret = 0;
781 goto quit;
782
783 case ARG_VERSION:
784 printf(_("pacat %s\n"
785 "Compiled with libpulse %s\n"
786 "Linked with libpulse %s\n"),
787 PACKAGE_VERSION,
788 pa_get_headers_version(),
789 pa_get_library_version());
790 ret = 0;
791 goto quit;
792
793 case 'r':
794 mode = RECORD;
795 break;
796
797 case 'p':
798 mode = PLAYBACK;
799 break;
800
801 case 'd':
802 pa_xfree(device);
803 device = pa_xstrdup(optarg);
804 break;
805
806 case 's':
807 pa_xfree(server);
808 server = pa_xstrdup(optarg);
809 break;
810
811 case 'n': {
812 char *t;
813
814 if (!(t = pa_locale_to_utf8(optarg)) ||
815 pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, t) < 0) {
816
817 pa_log(_("Invalid client name '%s'"), t ? t : optarg);
818 pa_xfree(t);
819 goto quit;
820 }
821
822 pa_xfree(t);
823 break;
824 }
825
826 case ARG_STREAM_NAME: {
827 char *t;
828
829 if (!(t = pa_locale_to_utf8(optarg)) ||
830 pa_proplist_sets(proplist, PA_PROP_MEDIA_NAME, t) < 0) {
831
832 pa_log(_("Invalid stream name '%s'"), t ? t : optarg);
833 pa_xfree(t);
834 goto quit;
835 }
836
837 pa_xfree(t);
838 break;
839 }
840
841 case 'v':
842 verbose = 1;
843 break;
844
845 case ARG_VOLUME: {
846 int v = atoi(optarg);
847 volume = v < 0 ? 0U : (pa_volume_t) v;
848 volume_is_set = TRUE;
849 break;
850 }
851
852 case ARG_CHANNELS:
853 sample_spec.channels = (uint8_t) atoi(optarg);
854 sample_spec_set = TRUE;
855 break;
856
857 case ARG_SAMPLEFORMAT:
858 sample_spec.format = pa_parse_sample_format(optarg);
859 sample_spec_set = TRUE;
860 break;
861
862 case ARG_SAMPLERATE:
863 sample_spec.rate = (uint32_t) atoi(optarg);
864 sample_spec_set = TRUE;
865 break;
866
867 case ARG_CHANNELMAP:
868 if (!pa_channel_map_parse(&channel_map, optarg)) {
869 pa_log(_("Invalid channel map '%s'"), optarg);
870 goto quit;
871 }
872
873 channel_map_set = TRUE;
874 break;
875
876 case ARG_FIX_CHANNELS:
877 flags |= PA_STREAM_FIX_CHANNELS;
878 break;
879
880 case ARG_FIX_RATE:
881 flags |= PA_STREAM_FIX_RATE;
882 break;
883
884 case ARG_FIX_FORMAT:
885 flags |= PA_STREAM_FIX_FORMAT;
886 break;
887
888 case ARG_NO_REMIX:
889 flags |= PA_STREAM_NO_REMIX_CHANNELS;
890 break;
891
892 case ARG_NO_REMAP:
893 flags |= PA_STREAM_NO_REMAP_CHANNELS;
894 break;
895
896 case ARG_LATENCY:
897 if (((latency = (size_t) atoi(optarg))) <= 0) {
898 pa_log(_("Invalid latency specification '%s'"), optarg);
899 goto quit;
900 }
901 break;
902
903 case ARG_PROCESS_TIME:
904 if (((process_time = (size_t) atoi(optarg))) <= 0) {
905 pa_log(_("Invalid process time specification '%s'"), optarg);
906 goto quit;
907 }
908 break;
909
910 case ARG_LATENCY_MSEC:
911 if (((latency_msec = (int32_t) atoi(optarg))) <= 0) {
912 pa_log(_("Invalid latency specification '%s'"), optarg);
913 goto quit;
914 }
915 break;
916
917 case ARG_PROCESS_TIME_MSEC:
918 if (((process_time_msec = (int32_t) atoi(optarg))) <= 0) {
919 pa_log(_("Invalid process time specification '%s'"), optarg);
920 goto quit;
921 }
922 break;
923
924 case ARG_PROPERTY: {
925 char *t;
926
927 if (!(t = pa_locale_to_utf8(optarg)) ||
928 pa_proplist_setp(proplist, t) < 0) {
929
930 pa_xfree(t);
931 pa_log(_("Invalid property '%s'"), optarg);
932 goto quit;
933 }
934
935 pa_xfree(t);
936 break;
937 }
938
939 case ARG_RAW:
940 raw = TRUE;
941 break;
942
943 case ARG_PASSTHROUGH:
944 flags |= PA_STREAM_PASSTHROUGH;
945 break;
946
947 case ARG_FILE_FORMAT:
948 if (optarg) {
949 if ((file_format = pa_sndfile_format_from_string(optarg)) < 0) {
950 pa_log(_("Unknown file format %s."), optarg);
951 goto quit;
952 }
953 }
954
955 raw = FALSE;
956 break;
957
958 case ARG_LIST_FILE_FORMATS:
959 pa_sndfile_dump_formats();
960 ret = 0;
961 goto quit;
962
963 default:
964 goto quit;
965 }
966 }
967
968 if (!pa_sample_spec_valid(&sample_spec)) {
969 pa_log(_("Invalid sample specification"));
970 goto quit;
971 }
972
973 if (optind+1 == argc) {
974 int fd;
975
976 filename = argv[optind];
977
978 if ((fd = pa_open_cloexec(argv[optind], mode == PLAYBACK ? O_RDONLY : O_WRONLY|O_TRUNC|O_CREAT, 0666)) < 0) {
979 pa_log(_("open(): %s"), strerror(errno));
980 goto quit;
981 }
982
983 if (dup2(fd, mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO) < 0) {
984 pa_log(_("dup2(): %s"), strerror(errno));
985 goto quit;
986 }
987
988 pa_close(fd);
989
990 } else if (optind+1 <= argc) {
991 pa_log(_("Too many arguments."));
992 goto quit;
993 }
994
995 if (!raw) {
996 SF_INFO sfi;
997 pa_zero(sfi);
998
999 if (mode == RECORD) {
1000 /* This might patch up the sample spec */
1001 if (pa_sndfile_write_sample_spec(&sfi, &sample_spec) < 0) {
1002 pa_log(_("Failed to generate sample specification for file."));
1003 goto quit;
1004 }
1005
1006 if (file_format <= 0) {
1007 char *extension;
1008 if (filename && (extension = strrchr(filename, '.')))
1009 file_format = pa_sndfile_format_from_string(extension+1);
1010 if (file_format <= 0)
1011 file_format = SF_FORMAT_WAV;
1012 /* Transparently upgrade classic .wav to wavex for multichannel audio */
1013 if (file_format == SF_FORMAT_WAV &&
1014 (sample_spec.channels > 2 ||
1015 (channel_map_set &&
1016 !(sample_spec.channels == 1 && channel_map.map[0] == PA_CHANNEL_POSITION_MONO) &&
1017 !(sample_spec.channels == 2 && channel_map.map[0] == PA_CHANNEL_POSITION_LEFT
1018 && channel_map.map[1] == PA_CHANNEL_POSITION_RIGHT))))
1019 file_format = SF_FORMAT_WAVEX;
1020 }
1021
1022 sfi.format |= file_format;
1023 }
1024
1025 if (!(sndfile = sf_open_fd(mode == RECORD ? STDOUT_FILENO : STDIN_FILENO,
1026 mode == RECORD ? SFM_WRITE : SFM_READ,
1027 &sfi, 0))) {
1028 pa_log(_("Failed to open audio file."));
1029 goto quit;
1030 }
1031
1032 if (mode == PLAYBACK) {
1033 if (sample_spec_set)
1034 pa_log(_("Warning: specified sample specification will be overwritten with specification from file."));
1035
1036 if (pa_sndfile_read_sample_spec(sndfile, &sample_spec) < 0) {
1037 pa_log(_("Failed to determine sample specification from file."));
1038 goto quit;
1039 }
1040 sample_spec_set = TRUE;
1041
1042 if (!channel_map_set) {
1043 /* Allow the user to overwrite the channel map on the command line */
1044 if (pa_sndfile_read_channel_map(sndfile, &channel_map) < 0) {
1045 if (sample_spec.channels > 2)
1046 pa_log(_("Warning: Failed to determine channel map from file."));
1047 } else
1048 channel_map_set = TRUE;
1049 }
1050 }
1051 }
1052
1053 if (!channel_map_set)
1054 pa_channel_map_init_extend(&channel_map, sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
1055
1056 if (!pa_channel_map_compatible(&channel_map, &sample_spec)) {
1057 pa_log(_("Channel map doesn't match sample specification"));
1058 goto quit;
1059 }
1060
1061 if (!raw) {
1062 pa_proplist *sfp;
1063
1064 if (mode == PLAYBACK)
1065 readf_function = pa_sndfile_readf_function(&sample_spec);
1066 else {
1067 if (pa_sndfile_write_channel_map(sndfile, &channel_map) < 0)
1068 pa_log(_("Warning: failed to write channel map to file."));
1069
1070 writef_function = pa_sndfile_writef_function(&sample_spec);
1071 }
1072
1073 /* Fill in libsndfile prop list data */
1074 sfp = pa_proplist_new();
1075 pa_sndfile_init_proplist(sndfile, sfp);
1076 pa_proplist_update(proplist, PA_UPDATE_MERGE, sfp);
1077 pa_proplist_free(sfp);
1078 }
1079
1080 if (verbose) {
1081 char tss[PA_SAMPLE_SPEC_SNPRINT_MAX], tcm[PA_CHANNEL_MAP_SNPRINT_MAX];
1082
1083 pa_log(_("Opening a %s stream with sample specification '%s' and channel map '%s'."),
1084 mode == RECORD ? _("recording") : _("playback"),
1085 pa_sample_spec_snprint(tss, sizeof(tss), &sample_spec),
1086 pa_channel_map_snprint(tcm, sizeof(tcm), &channel_map));
1087 }
1088
1089 /* Fill in client name if none was set */
1090 if (!pa_proplist_contains(proplist, PA_PROP_APPLICATION_NAME)) {
1091 char *t;
1092
1093 if ((t = pa_locale_to_utf8(bn))) {
1094 pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, t);
1095 pa_xfree(t);
1096 }
1097 }
1098
1099 /* Fill in media name if none was set */
1100 if (!pa_proplist_contains(proplist, PA_PROP_MEDIA_NAME)) {
1101 const char *t;
1102
1103 if ((t = filename) ||
1104 (t = pa_proplist_gets(proplist, PA_PROP_APPLICATION_NAME)))
1105 pa_proplist_sets(proplist, PA_PROP_MEDIA_NAME, t);
1106 }
1107
1108 /* Set up a new main loop */
1109 if (!(m = pa_mainloop_new())) {
1110 pa_log(_("pa_mainloop_new() failed."));
1111 goto quit;
1112 }
1113
1114 mainloop_api = pa_mainloop_get_api(m);
1115
1116 pa_assert_se(pa_signal_init(mainloop_api) == 0);
1117 pa_signal_new(SIGINT, exit_signal_callback, NULL);
1118 pa_signal_new(SIGTERM, exit_signal_callback, NULL);
1119 #ifdef SIGUSR1
1120 pa_signal_new(SIGUSR1, sigusr1_signal_callback, NULL);
1121 #endif
1122 pa_disable_sigpipe();
1123
1124 if (raw) {
1125 if (!(stdio_event = mainloop_api->io_new(mainloop_api,
1126 mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO,
1127 mode == PLAYBACK ? PA_IO_EVENT_INPUT : PA_IO_EVENT_OUTPUT,
1128 mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) {
1129 pa_log(_("io_new() failed."));
1130 goto quit;
1131 }
1132 }
1133
1134 /* Create a new connection context */
1135 if (!(context = pa_context_new_with_proplist(mainloop_api, NULL, proplist))) {
1136 pa_log(_("pa_context_new() failed."));
1137 goto quit;
1138 }
1139
1140 pa_context_set_state_callback(context, context_state_callback, NULL);
1141
1142 /* Connect the context */
1143 if (pa_context_connect(context, server, 0, NULL) < 0) {
1144 pa_log(_("pa_context_connect() failed: %s"), pa_strerror(pa_context_errno(context)));
1145 goto quit;
1146 }
1147
1148 if (verbose) {
1149 if (!(time_event = pa_context_rttime_new(context, pa_rtclock_now() + TIME_EVENT_USEC, time_event_callback, NULL))) {
1150 pa_log(_("pa_context_rttime_new() failed."));
1151 goto quit;
1152 }
1153 }
1154
1155 /* Run the main loop */
1156 if (pa_mainloop_run(m, &ret) < 0) {
1157 pa_log(_("pa_mainloop_run() failed."));
1158 goto quit;
1159 }
1160
1161 quit:
1162 if (stream)
1163 pa_stream_unref(stream);
1164
1165 if (context)
1166 pa_context_unref(context);
1167
1168 if (stdio_event) {
1169 pa_assert(mainloop_api);
1170 mainloop_api->io_free(stdio_event);
1171 }
1172
1173 if (time_event) {
1174 pa_assert(mainloop_api);
1175 mainloop_api->time_free(time_event);
1176 }
1177
1178 if (m) {
1179 pa_signal_done();
1180 pa_mainloop_free(m);
1181 }
1182
1183 pa_xfree(buffer);
1184
1185 pa_xfree(server);
1186 pa_xfree(device);
1187
1188 if (sndfile)
1189 sf_close(sndfile);
1190
1191 if (proplist)
1192 pa_proplist_free(proplist);
1193
1194 return ret;
1195 }