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