2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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 of the License,
10 or (at your option) any later version.
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.
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
38 #include <pulse/i18n.h>
39 #include <pulse/pulseaudio.h>
41 #define TIME_EVENT_USEC 50000
43 #define CLEAR_LINE "\x1B[K"
45 static enum { RECORD, PLAYBACK } mode = PLAYBACK;
47 static pa_context *context = NULL;
48 static pa_stream *stream = NULL;
49 static pa_mainloop_api *mainloop_api = NULL;
51 static void *buffer = NULL;
52 static size_t buffer_length = 0, buffer_index = 0;
54 static pa_io_event* stdio_event = NULL;
56 static char *stream_name = NULL, *client_name = NULL, *device = NULL;
58 static int verbose = 0;
59 static pa_volume_t volume = PA_VOLUME_NORM;
60 static int volume_is_set = 0;
62 static pa_sample_spec sample_spec = {
63 .format = PA_SAMPLE_S16LE,
68 static pa_channel_map channel_map;
69 static int channel_map_set = 0;
71 static pa_stream_flags_t flags = 0;
73 static size_t latency = 0, process_time=0;
75 /* A shortcut for terminating the application */
76 static void quit(int ret) {
78 mainloop_api->quit(mainloop_api, ret);
81 /* Write some data to the stream */
82 static void do_stream_write(size_t length) {
86 if (!buffer || !buffer_length)
90 if (l > buffer_length)
93 if (pa_stream_write(stream, (uint8_t*) buffer + buffer_index, l, NULL, 0, PA_SEEK_RELATIVE) < 0) {
94 fprintf(stderr, _("pa_stream_write() failed: %s\n"), pa_strerror(pa_context_errno(context)));
102 if (!buffer_length) {
105 buffer_index = buffer_length = 0;
109 /* This is called whenever new data may be written to the stream */
110 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
115 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_INPUT);
120 do_stream_write(length);
123 /* This is called whenever new data may is available */
124 static void stream_read_callback(pa_stream *s, size_t length, void *userdata) {
130 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_OUTPUT);
132 if (pa_stream_peek(s, &data, &length) < 0) {
133 fprintf(stderr, _("pa_stream_peek() failed: %s\n"), pa_strerror(pa_context_errno(context)));
142 fprintf(stderr, _("Buffer overrun, dropping incoming data\n"));
143 if (pa_stream_drop(s) < 0) {
144 fprintf(stderr, _("pa_stream_drop() failed: %s\n"), pa_strerror(pa_context_errno(context)));
150 buffer = pa_xmalloc(buffer_length = length);
151 memcpy(buffer, data, length);
156 /* This routine is called whenever the stream state changes */
157 static void stream_state_callback(pa_stream *s, void *userdata) {
160 switch (pa_stream_get_state(s)) {
161 case PA_STREAM_CREATING:
162 case PA_STREAM_TERMINATED:
165 case PA_STREAM_READY:
167 const pa_buffer_attr *a;
168 char cmt[PA_CHANNEL_MAP_SNPRINT_MAX], sst[PA_SAMPLE_SPEC_SNPRINT_MAX];
170 fprintf(stderr, _("Stream successfully created.\n"));
172 if (!(a = pa_stream_get_buffer_attr(s)))
173 fprintf(stderr, _("pa_stream_get_buffer_attr() failed: %s\n"), pa_strerror(pa_context_errno(pa_stream_get_context(s))));
176 if (mode == PLAYBACK)
177 fprintf(stderr, _("Buffer metrics: maxlength=%u, tlength=%u, prebuf=%u, minreq=%u\n"), a->maxlength, a->tlength, a->prebuf, a->minreq);
179 assert(mode == RECORD);
180 fprintf(stderr, _("Buffer metrics: maxlength=%u, fragsize=%u\n"), a->maxlength, a->fragsize);
184 fprintf(stderr, _("Using sample spec '%s', channel map '%s'.\n"),
185 pa_sample_spec_snprint(sst, sizeof(sst), pa_stream_get_sample_spec(s)),
186 pa_channel_map_snprint(cmt, sizeof(cmt), pa_stream_get_channel_map(s)));
188 fprintf(stderr, _("Connected to device %s (%u, %ssuspended).\n"),
189 pa_stream_get_device_name(s),
190 pa_stream_get_device_index(s),
191 pa_stream_is_suspended(s) ? "" : "not ");
196 case PA_STREAM_FAILED:
198 fprintf(stderr, _("Stream error: %s\n"), pa_strerror(pa_context_errno(pa_stream_get_context(s))));
203 static void stream_suspended_callback(pa_stream *s, void *userdata) {
207 if (pa_stream_is_suspended(s))
208 fprintf(stderr, _("Stream device suspended.%s \n"), CLEAR_LINE);
210 fprintf(stderr, _("Stream device resumed.%s \n"), CLEAR_LINE);
214 static void stream_underflow_callback(pa_stream *s, void *userdata) {
218 fprintf(stderr, _("Stream underrun.%s \n"), CLEAR_LINE);
221 static void stream_overflow_callback(pa_stream *s, void *userdata) {
225 fprintf(stderr, _("Stream overrun.%s \n"), CLEAR_LINE);
228 static void stream_started_callback(pa_stream *s, void *userdata) {
232 fprintf(stderr, _("Stream started.%s \n"), CLEAR_LINE);
235 static void stream_moved_callback(pa_stream *s, void *userdata) {
239 fprintf(stderr, _("Stream moved to device %s (%u, %ssuspended).%s \n"), pa_stream_get_device_name(s), pa_stream_get_device_index(s), pa_stream_is_suspended(s) ? "" : _("not "), CLEAR_LINE);
242 /* This is called whenever the context status changes */
243 static void context_state_callback(pa_context *c, void *userdata) {
246 switch (pa_context_get_state(c)) {
247 case PA_CONTEXT_CONNECTING:
248 case PA_CONTEXT_AUTHORIZING:
249 case PA_CONTEXT_SETTING_NAME:
252 case PA_CONTEXT_READY: {
254 pa_buffer_attr buffer_attr;
260 fprintf(stderr, _("Connection established.%s \n"), CLEAR_LINE);
262 if (!(stream = pa_stream_new(c, stream_name, &sample_spec, channel_map_set ? &channel_map : NULL))) {
263 fprintf(stderr, _("pa_stream_new() failed: %s\n"), pa_strerror(pa_context_errno(c)));
267 pa_stream_set_state_callback(stream, stream_state_callback, NULL);
268 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
269 pa_stream_set_read_callback(stream, stream_read_callback, NULL);
270 pa_stream_set_suspended_callback(stream, stream_suspended_callback, NULL);
271 pa_stream_set_moved_callback(stream, stream_moved_callback, NULL);
272 pa_stream_set_underflow_callback(stream, stream_underflow_callback, NULL);
273 pa_stream_set_overflow_callback(stream, stream_overflow_callback, NULL);
274 pa_stream_set_started_callback(stream, stream_started_callback, NULL);
277 memset(&buffer_attr, 0, sizeof(buffer_attr));
278 buffer_attr.tlength = (uint32_t) latency;
279 buffer_attr.minreq = (uint32_t) process_time;
280 buffer_attr.maxlength = (uint32_t) -1;
281 buffer_attr.prebuf = (uint32_t) -1;
282 flags |= PA_STREAM_ADJUST_LATENCY;
285 if (mode == PLAYBACK) {
287 if ((r = pa_stream_connect_playback(stream, device, latency > 0 ? &buffer_attr : NULL, flags, volume_is_set ? pa_cvolume_set(&cv, sample_spec.channels, volume) : NULL, NULL)) < 0) {
288 fprintf(stderr, _("pa_stream_connect_playback() failed: %s\n"), pa_strerror(pa_context_errno(c)));
293 if ((r = pa_stream_connect_record(stream, device, latency > 0 ? &buffer_attr : NULL, flags)) < 0) {
294 fprintf(stderr, _("pa_stream_connect_record() failed: %s\n"), pa_strerror(pa_context_errno(c)));
302 case PA_CONTEXT_TERMINATED:
306 case PA_CONTEXT_FAILED:
308 fprintf(stderr, _("Connection failure: %s\n"), pa_strerror(pa_context_errno(c)));
319 /* Connection draining complete */
320 static void context_drain_complete(pa_context*c, void *userdata) {
321 pa_context_disconnect(c);
324 /* Stream draining complete */
325 static void stream_drain_complete(pa_stream*s, int success, void *userdata) {
329 fprintf(stderr, _("Failed to drain stream: %s\n"), pa_strerror(pa_context_errno(context)));
334 fprintf(stderr, _("Playback stream drained.\n"));
336 pa_stream_disconnect(stream);
337 pa_stream_unref(stream);
340 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
341 pa_context_disconnect(context);
344 fprintf(stderr, _("Draining connection to server.\n"));
348 /* New data on STDIN **/
349 static void stdin_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
353 assert(a == mainloop_api);
355 assert(stdio_event == e);
358 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
362 if (!stream || pa_stream_get_state(stream) != PA_STREAM_READY || !(l = w = pa_stream_writable_size(stream)))
365 buffer = pa_xmalloc(l);
367 if ((r = read(fd, buffer, l)) <= 0) {
370 fprintf(stderr, _("Got EOF.\n"));
375 if (!(o = pa_stream_drain(stream, stream_drain_complete, NULL))) {
376 fprintf(stderr, _("pa_stream_drain(): %s\n"), pa_strerror(pa_context_errno(context)));
381 pa_operation_unref(o);
386 fprintf(stderr, _("read() failed: %s\n"), strerror(errno));
390 mainloop_api->io_free(stdio_event);
395 buffer_length = (uint32_t) r;
402 /* Some data may be written to STDOUT */
403 static void stdout_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
406 assert(a == mainloop_api);
408 assert(stdio_event == e);
411 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
415 assert(buffer_length);
417 if ((r = write(fd, (uint8_t*) buffer+buffer_index, buffer_length)) <= 0) {
418 fprintf(stderr, _("write() failed: %s\n"), strerror(errno));
421 mainloop_api->io_free(stdio_event);
426 buffer_length -= (uint32_t) r;
427 buffer_index += (uint32_t) r;
429 if (!buffer_length) {
432 buffer_length = buffer_index = 0;
436 /* UNIX signal to quit recieved */
437 static void exit_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
439 fprintf(stderr, _("Got signal, exiting.\n"));
443 /* Show the current latency */
444 static void stream_update_timing_callback(pa_stream *s, int success, void *userdata) {
451 pa_stream_get_time(s, &usec) < 0 ||
452 pa_stream_get_latency(s, &l, &negative) < 0) {
453 fprintf(stderr, _("Failed to get latency: %s\n"), pa_strerror(pa_context_errno(context)));
458 fprintf(stderr, _("Time: %0.3f sec; Latency: %0.0f usec. \r"),
459 (float) usec / 1000000,
460 (float) l * (negative?-1.0f:1.0f));
463 /* Someone requested that the latency is shown */
464 static void sigusr1_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
469 pa_operation_unref(pa_stream_update_timing_info(stream, stream_update_timing_callback, NULL));
472 static void time_event_callback(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
475 if (stream && pa_stream_get_state(stream) == PA_STREAM_READY) {
477 if (!(o = pa_stream_update_timing_info(stream, stream_update_timing_callback, NULL)))
478 fprintf(stderr, _("pa_stream_update_timing_info() failed: %s\n"), pa_strerror(pa_context_errno(context)));
480 pa_operation_unref(o);
483 pa_gettimeofday(&next);
484 pa_timeval_add(&next, TIME_EVENT_USEC);
486 m->time_restart(e, &next);
489 static void help(const char *argv0) {
491 printf(_("%s [options]\n\n"
492 " -h, --help Show this help\n"
493 " --version Show version\n\n"
494 " -r, --record Create a connection for recording\n"
495 " -p, --playback Create a connection for playback\n\n"
496 " -v, --verbose Enable verbose operations\n\n"
497 " -s, --server=SERVER The name of the server to connect to\n"
498 " -d, --device=DEVICE The name of the sink/source to connect to\n"
499 " -n, --client-name=NAME How to call this client on the server\n"
500 " --stream-name=NAME How to call this stream on the server\n"
501 " --volume=VOLUME Specify the initial (linear) volume in range 0...65536\n"
502 " --rate=SAMPLERATE The sample rate in Hz (defaults to 44100)\n"
503 " --format=SAMPLEFORMAT The sample type, one of s16le, s16be, u8, float32le,\n"
504 " float32be, ulaw, alaw, s32le, s32be (defaults to s16ne)\n"
505 " --channels=CHANNELS The number of channels, 1 for mono, 2 for stereo\n"
507 " --channel-map=CHANNELMAP Channel map to use instead of the default\n"
508 " --fix-format Take the sample format from the sink the stream is\n"
509 " being connected to.\n"
510 " --fix-rate Take the sampling rate from the sink the stream is\n"
511 " being connected to.\n"
512 " --fix-channels Take the number of channels and the channel map\n"
513 " from the sink the stream is being connected to.\n"
514 " --no-remix Don't upmix or downmix channels.\n"
515 " --no-remap Map channels by index instead of name.\n"
516 " --latency=BYTES Request the specified latency in bytes.\n"
517 " --process-time=BYTES Request the specified process time per request in bytes.\n")
539 int main(int argc, char *argv[]) {
540 pa_mainloop* m = NULL;
542 char *bn, *server = NULL;
543 pa_time_event *time_event = NULL;
545 static const struct option long_options[] = {
546 {"record", 0, NULL, 'r'},
547 {"playback", 0, NULL, 'p'},
548 {"device", 1, NULL, 'd'},
549 {"server", 1, NULL, 's'},
550 {"client-name", 1, NULL, 'n'},
551 {"stream-name", 1, NULL, ARG_STREAM_NAME},
552 {"version", 0, NULL, ARG_VERSION},
553 {"help", 0, NULL, 'h'},
554 {"verbose", 0, NULL, 'v'},
555 {"volume", 1, NULL, ARG_VOLUME},
556 {"rate", 1, NULL, ARG_SAMPLERATE},
557 {"format", 1, NULL, ARG_SAMPLEFORMAT},
558 {"channels", 1, NULL, ARG_CHANNELS},
559 {"channel-map", 1, NULL, ARG_CHANNELMAP},
560 {"fix-format", 0, NULL, ARG_FIX_FORMAT},
561 {"fix-rate", 0, NULL, ARG_FIX_RATE},
562 {"fix-channels", 0, NULL, ARG_FIX_CHANNELS},
563 {"no-remap", 0, NULL, ARG_NO_REMAP},
564 {"no-remix", 0, NULL, ARG_NO_REMIX},
565 {"latency", 1, NULL, ARG_LATENCY},
566 {"process-time", 1, NULL, ARG_PROCESS_TIME},
570 setlocale(LC_ALL, "");
571 bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
573 if (!(bn = strrchr(argv[0], '/')))
578 if (strstr(bn, "rec") || strstr(bn, "mon"))
580 else if (strstr(bn, "cat") || strstr(bn, "play"))
583 while ((c = getopt_long(argc, argv, "rpd:s:n:hv", long_options, NULL)) != -1) {
592 printf(_("pacat %s\nCompiled with libpulse %s\nLinked with libpulse %s\n"), PACKAGE_VERSION, pa_get_headers_version(), pa_get_library_version());
606 device = pa_xstrdup(optarg);
611 server = pa_xstrdup(optarg);
615 pa_xfree(client_name);
616 client_name = pa_xstrdup(optarg);
619 case ARG_STREAM_NAME:
620 pa_xfree(stream_name);
621 stream_name = pa_xstrdup(optarg);
629 int v = atoi(optarg);
630 volume = v < 0 ? 0U : (pa_volume_t) v;
636 sample_spec.channels = (uint8_t) atoi(optarg);
639 case ARG_SAMPLEFORMAT:
640 sample_spec.format = pa_parse_sample_format(optarg);
644 sample_spec.rate = (uint32_t) atoi(optarg);
648 if (!pa_channel_map_parse(&channel_map, optarg)) {
649 fprintf(stderr, _("Invalid channel map '%s'\n"), optarg);
656 case ARG_FIX_CHANNELS:
657 flags |= PA_STREAM_FIX_CHANNELS;
661 flags |= PA_STREAM_FIX_RATE;
665 flags |= PA_STREAM_FIX_FORMAT;
669 flags |= PA_STREAM_NO_REMIX_CHANNELS;
673 flags |= PA_STREAM_NO_REMAP_CHANNELS;
677 if (((latency = (size_t) atoi(optarg))) <= 0) {
678 fprintf(stderr, _("Invalid latency specification '%s'\n"), optarg);
683 case ARG_PROCESS_TIME:
684 if (((process_time = (size_t) atoi(optarg))) <= 0) {
685 fprintf(stderr, _("Invalid process time specification '%s'\n"), optarg);
695 if (!pa_sample_spec_valid(&sample_spec)) {
696 fprintf(stderr, _("Invalid sample specification\n"));
700 if (channel_map_set && pa_channel_map_compatible(&channel_map, &sample_spec)) {
701 fprintf(stderr, _("Channel map doesn't match sample specification\n"));
706 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
707 pa_sample_spec_snprint(t, sizeof(t), &sample_spec);
708 fprintf(stderr, _("Opening a %s stream with sample specification '%s'.\n"), mode == RECORD ? _("recording") : _("playback"), t);
711 if (!(optind >= argc)) {
712 if (optind+1 == argc) {
715 if ((fd = open(argv[optind], mode == PLAYBACK ? O_RDONLY : O_WRONLY|O_TRUNC|O_CREAT, 0666)) < 0) {
716 fprintf(stderr, _("open(): %s\n"), strerror(errno));
720 if (dup2(fd, mode == PLAYBACK ? 0 : 1) < 0) {
721 fprintf(stderr, _("dup2(): %s\n"), strerror(errno));
728 stream_name = pa_xstrdup(argv[optind]);
731 fprintf(stderr, _("Too many arguments.\n"));
737 client_name = pa_xstrdup(bn);
740 stream_name = pa_xstrdup(client_name);
742 /* Set up a new main loop */
743 if (!(m = pa_mainloop_new())) {
744 fprintf(stderr, _("pa_mainloop_new() failed.\n"));
748 mainloop_api = pa_mainloop_get_api(m);
750 r = pa_signal_init(mainloop_api);
752 pa_signal_new(SIGINT, exit_signal_callback, NULL);
753 pa_signal_new(SIGTERM, exit_signal_callback, NULL);
755 pa_signal_new(SIGUSR1, sigusr1_signal_callback, NULL);
758 signal(SIGPIPE, SIG_IGN);
761 if (!(stdio_event = mainloop_api->io_new(mainloop_api,
762 mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO,
763 mode == PLAYBACK ? PA_IO_EVENT_INPUT : PA_IO_EVENT_OUTPUT,
764 mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) {
765 fprintf(stderr, _("io_new() failed.\n"));
769 /* Create a new connection context */
770 if (!(context = pa_context_new(mainloop_api, client_name))) {
771 fprintf(stderr, _("pa_context_new() failed.\n"));
775 pa_context_set_state_callback(context, context_state_callback, NULL);
777 /* Connect the context */
778 if (pa_context_connect(context, server, 0, NULL) < 0) {
779 fprintf(stderr, _("pa_context_connect() failed: %s"), pa_strerror(pa_context_errno(context)));
786 pa_gettimeofday(&tv);
787 pa_timeval_add(&tv, TIME_EVENT_USEC);
789 if (!(time_event = mainloop_api->time_new(mainloop_api, &tv, time_event_callback, NULL))) {
790 fprintf(stderr, _("time_new() failed.\n"));
795 /* Run the main loop */
796 if (pa_mainloop_run(m, &ret) < 0) {
797 fprintf(stderr, _("pa_mainloop_run() failed.\n"));
803 pa_stream_unref(stream);
806 pa_context_unref(context);
809 assert(mainloop_api);
810 mainloop_api->io_free(stdio_event);
814 assert(mainloop_api);
815 mainloop_api->time_free(time_event);
827 pa_xfree(client_name);
828 pa_xfree(stream_name);