4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 #include <pulse/pulseaudio.h>
38 #define TIME_EVENT_USEC 50000
40 #if PA_API_VERSION < 9
41 #error Invalid PulseAudio API version
44 static enum { RECORD, PLAYBACK } mode = PLAYBACK;
46 static pa_context *context = NULL;
47 static pa_stream *stream = NULL;
48 static pa_mainloop_api *mainloop_api = NULL;
50 static void *buffer = NULL;
51 static size_t buffer_length = 0, buffer_index = 0;
53 static pa_io_event* stdio_event = NULL;
55 static char *stream_name = NULL, *client_name = NULL, *device = NULL;
57 static int verbose = 0;
58 static pa_volume_t volume = PA_VOLUME_NORM;
60 static pa_sample_spec sample_spec = {
61 .format = PA_SAMPLE_S16LE,
66 static pa_channel_map channel_map;
67 static int channel_map_set = 0;
69 /* A shortcut for terminating the application */
70 static void quit(int ret) {
72 mainloop_api->quit(mainloop_api, ret);
75 /* Write some data to the stream */
76 static void do_stream_write(size_t length) {
80 if (!buffer || !buffer_length)
84 if (l > buffer_length)
87 if (pa_stream_write(stream, (uint8_t*) buffer + buffer_index, l, NULL, 0, PA_SEEK_RELATIVE) < 0) {
88 fprintf(stderr, "pa_stream_write() failed: %s\n", pa_strerror(pa_context_errno(context)));
99 buffer_index = buffer_length = 0;
103 /* This is called whenever new data may be written to the stream */
104 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
108 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_INPUT);
113 do_stream_write(length);
116 /* This is called whenever new data may is available */
117 static void stream_read_callback(pa_stream *s, size_t length, void *userdata) {
122 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_OUTPUT);
124 if (pa_stream_peek(s, &data, &length) < 0) {
125 fprintf(stderr, "pa_stream_peek() failed: %s\n", pa_strerror(pa_context_errno(context)));
130 assert(data && length);
133 fprintf(stderr, "Buffer overrun, dropping incoming data\n");
134 if (pa_stream_drop(s) < 0) {
135 fprintf(stderr, "pa_stream_drop() failed: %s\n", pa_strerror(pa_context_errno(context)));
141 buffer = pa_xmalloc(buffer_length = length);
142 memcpy(buffer, data, length);
147 /* This routine is called whenever the stream state changes */
148 static void stream_state_callback(pa_stream *s, void *userdata) {
151 switch (pa_stream_get_state(s)) {
152 case PA_STREAM_CREATING:
153 case PA_STREAM_TERMINATED:
156 case PA_STREAM_READY:
158 const pa_buffer_attr *a;
160 fprintf(stderr, "Stream successfully created.\n");
162 if (!(a = pa_stream_get_buffer_attr(s)))
163 fprintf(stderr, "pa_stream_get_buffer_attr() failed: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
166 if (mode == PLAYBACK)
167 fprintf(stderr, "Buffer metrics: maxlength=%u, tlength=%u, prebuf=%u, minreq=%u\n", a->maxlength, a->tlength, a->prebuf, a->minreq);
169 assert(mode == RECORD);
170 fprintf(stderr, "Buffer metrics: maxlength=%u, fragsize=%u\n", a->maxlength, a->fragsize);
179 case PA_STREAM_FAILED:
181 fprintf(stderr, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
186 /* This is called whenever the context status changes */
187 static void context_state_callback(pa_context *c, void *userdata) {
190 switch (pa_context_get_state(c)) {
191 case PA_CONTEXT_CONNECTING:
192 case PA_CONTEXT_AUTHORIZING:
193 case PA_CONTEXT_SETTING_NAME:
196 case PA_CONTEXT_READY: {
199 assert(c && !stream);
202 fprintf(stderr, "Connection established.\n");
204 if (!(stream = pa_stream_new(c, stream_name, &sample_spec, channel_map_set ? &channel_map : NULL))) {
205 fprintf(stderr, "pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(c)));
209 pa_stream_set_state_callback(stream, stream_state_callback, NULL);
210 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
211 pa_stream_set_read_callback(stream, stream_read_callback, NULL);
213 if (mode == PLAYBACK) {
215 if ((r = pa_stream_connect_playback(stream, device, NULL, 0, pa_cvolume_set(&cv, sample_spec.channels, volume), NULL)) < 0) {
216 fprintf(stderr, "pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(c)));
221 if ((r = pa_stream_connect_record(stream, device, NULL, 0)) < 0) {
222 fprintf(stderr, "pa_stream_connect_record() failed: %s\n", pa_strerror(pa_context_errno(c)));
230 case PA_CONTEXT_TERMINATED:
234 case PA_CONTEXT_FAILED:
236 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
247 /* Connection draining complete */
248 static void context_drain_complete(pa_context*c, void *userdata) {
249 pa_context_disconnect(c);
252 /* Stream draining complete */
253 static void stream_drain_complete(pa_stream*s, int success, void *userdata) {
257 fprintf(stderr, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context)));
262 fprintf(stderr, "Playback stream drained.\n");
264 pa_stream_disconnect(stream);
265 pa_stream_unref(stream);
268 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
269 pa_context_disconnect(context);
272 fprintf(stderr, "Draining connection to server.\n");
276 /* New data on STDIN **/
277 static void stdin_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
280 assert(a == mainloop_api && e && stdio_event == e);
283 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
287 if (!stream || pa_stream_get_state(stream) != PA_STREAM_READY || !(l = w = pa_stream_writable_size(stream)))
290 buffer = pa_xmalloc(l);
292 if ((r = read(fd, buffer, l)) <= 0) {
295 fprintf(stderr, "Got EOF.\n");
300 if (!(o = pa_stream_drain(stream, stream_drain_complete, NULL))) {
301 fprintf(stderr, "pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(context)));
306 pa_operation_unref(o);
311 fprintf(stderr, "read() failed: %s\n", strerror(errno));
315 mainloop_api->io_free(stdio_event);
327 /* Some data may be written to STDOUT */
328 static void stdout_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
330 assert(a == mainloop_api && e && stdio_event == e);
333 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
337 assert(buffer_length);
339 if ((r = write(fd, (uint8_t*) buffer+buffer_index, buffer_length)) <= 0) {
340 fprintf(stderr, "write() failed: %s\n", strerror(errno));
343 mainloop_api->io_free(stdio_event);
351 if (!buffer_length) {
354 buffer_length = buffer_index = 0;
358 /* UNIX signal to quit recieved */
359 static void exit_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
361 fprintf(stderr, "Got signal, exiting.\n");
365 /* Show the current latency */
366 static void stream_update_timing_callback(pa_stream *s, int success, void *userdata) {
367 pa_usec_t latency, usec;
373 pa_stream_get_time(s, &usec) < 0 ||
374 pa_stream_get_latency(s, &latency, &negative) < 0) {
375 fprintf(stderr, "Failed to get latency: %s\n", pa_strerror(pa_context_errno(context)));
380 fprintf(stderr, "Time: %0.3f sec; Latency: %0.0f usec. \r",
381 (float) usec / 1000000,
382 (float) latency * (negative?-1:1));
385 /* Someone requested that the latency is shown */
386 static void sigusr1_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
391 pa_operation_unref(pa_stream_update_timing_info(stream, stream_update_timing_callback, NULL));
394 static void time_event_callback(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
397 if (stream && pa_stream_get_state(stream) == PA_STREAM_READY) {
399 if (!(o = pa_stream_update_timing_info(stream, stream_update_timing_callback, NULL)))
400 fprintf(stderr, "pa_stream_update_timing_info() failed: %s\n", pa_strerror(pa_context_errno(context)));
402 pa_operation_unref(o);
405 pa_gettimeofday(&next);
406 pa_timeval_add(&next, TIME_EVENT_USEC);
408 m->time_restart(e, &next);
411 static void help(const char *argv0) {
413 printf("%s [options]\n\n"
414 " -h, --help Show this help\n"
415 " --version Show version\n\n"
416 " -r, --record Create a connection for recording\n"
417 " -p, --playback Create a connection for playback\n\n"
418 " -v, --verbose Enable verbose operations\n\n"
419 " -s, --server=SERVER The name of the server to connect to\n"
420 " -d, --device=DEVICE The name of the sink/source to connect to\n"
421 " -n, --client-name=NAME How to call this client on the server\n"
422 " --stream-name=NAME How to call this stream on the server\n"
423 " --volume=VOLUME Specify the initial (linear) volume in range 0...65536\n"
424 " --rate=SAMPLERATE The sample rate in Hz (defaults to 44100)\n"
425 " --format=SAMPLEFORMAT The sample type, one of s16le, s16be, u8, float32le,\n"
426 " float32be, ulaw, alaw (defaults to s16ne)\n"
427 " --channels=CHANNELS The number of channels, 1 for mono, 2 for stereo\n"
429 " --channel-map=CHANNELMAP Channel map to use instead of the default\n",
443 int main(int argc, char *argv[]) {
444 pa_mainloop* m = NULL;
446 char *bn, *server = NULL;
447 pa_time_event *time_event = NULL;
449 static const struct option long_options[] = {
450 {"record", 0, NULL, 'r'},
451 {"playback", 0, NULL, 'p'},
452 {"device", 1, NULL, 'd'},
453 {"server", 1, NULL, 's'},
454 {"client-name", 1, NULL, 'n'},
455 {"stream-name", 1, NULL, ARG_STREAM_NAME},
456 {"version", 0, NULL, ARG_VERSION},
457 {"help", 0, NULL, 'h'},
458 {"verbose", 0, NULL, 'v'},
459 {"volume", 1, NULL, ARG_VOLUME},
460 {"rate", 1, NULL, ARG_SAMPLERATE},
461 {"format", 1, NULL, ARG_SAMPLEFORMAT},
462 {"channels", 1, NULL, ARG_CHANNELS},
463 {"channel-map", 1, NULL, ARG_CHANNELMAP},
467 if (!(bn = strrchr(argv[0], '/')))
472 if (strstr(bn, "rec") || strstr(bn, "mon"))
474 else if (strstr(bn, "cat") || strstr(bn, "play"))
477 while ((c = getopt_long(argc, argv, "rpd:s:n:hv", long_options, NULL)) != -1) {
486 printf("pacat "PACKAGE_VERSION"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
500 device = pa_xstrdup(optarg);
505 server = pa_xstrdup(optarg);
509 pa_xfree(client_name);
510 client_name = pa_xstrdup(optarg);
513 case ARG_STREAM_NAME:
514 pa_xfree(stream_name);
515 stream_name = pa_xstrdup(optarg);
523 int v = atoi(optarg);
524 volume = v < 0 ? 0 : v;
529 sample_spec.channels = atoi(optarg);
532 case ARG_SAMPLEFORMAT:
533 sample_spec.format = pa_parse_sample_format(optarg);
537 sample_spec.rate = atoi(optarg);
541 if (!pa_channel_map_parse(&channel_map, optarg)) {
542 fprintf(stderr, "Invalid channel map\n");
554 if (!pa_sample_spec_valid(&sample_spec)) {
555 fprintf(stderr, "Invalid sample specification\n");
559 if (channel_map_set && channel_map.channels != sample_spec.channels) {
560 fprintf(stderr, "Channel map doesn't match sample specification\n");
565 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
566 pa_sample_spec_snprint(t, sizeof(t), &sample_spec);
567 fprintf(stderr, "Opening a %s stream with sample specification '%s'.\n", mode == RECORD ? "recording" : "playback", t);
570 if (!(optind >= argc)) {
571 if (optind+1 == argc) {
574 if ((fd = open(argv[optind], mode == PLAYBACK ? O_RDONLY : O_WRONLY|O_TRUNC|O_CREAT, 0666)) < 0) {
575 fprintf(stderr, "open(): %s\n", strerror(errno));
579 if (dup2(fd, mode == PLAYBACK ? 0 : 1) < 0) {
580 fprintf(stderr, "dup2(): %s\n", strerror(errno));
587 stream_name = pa_xstrdup(argv[optind]);
590 fprintf(stderr, "Too many arguments.\n");
596 client_name = pa_xstrdup(bn);
599 stream_name = pa_xstrdup(client_name);
601 /* Set up a new main loop */
602 if (!(m = pa_mainloop_new())) {
603 fprintf(stderr, "pa_mainloop_new() failed.\n");
607 mainloop_api = pa_mainloop_get_api(m);
609 r = pa_signal_init(mainloop_api);
611 pa_signal_new(SIGINT, exit_signal_callback, NULL);
612 pa_signal_new(SIGTERM, exit_signal_callback, NULL);
614 pa_signal_new(SIGUSR1, sigusr1_signal_callback, NULL);
617 signal(SIGPIPE, SIG_IGN);
620 if (!(stdio_event = mainloop_api->io_new(mainloop_api,
621 mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO,
622 mode == PLAYBACK ? PA_IO_EVENT_INPUT : PA_IO_EVENT_OUTPUT,
623 mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) {
624 fprintf(stderr, "io_new() failed.\n");
628 /* Create a new connection context */
629 if (!(context = pa_context_new(mainloop_api, client_name))) {
630 fprintf(stderr, "pa_context_new() failed.\n");
634 pa_context_set_state_callback(context, context_state_callback, NULL);
636 /* Connect the context */
637 pa_context_connect(context, server, 0, NULL);
642 pa_gettimeofday(&tv);
643 pa_timeval_add(&tv, TIME_EVENT_USEC);
645 if (!(time_event = mainloop_api->time_new(mainloop_api, &tv, time_event_callback, NULL))) {
646 fprintf(stderr, "time_new() failed.\n");
651 /* Run the main loop */
652 if (pa_mainloop_run(m, &ret) < 0) {
653 fprintf(stderr, "pa_mainloop_run() failed.\n");
659 pa_stream_unref(stream);
662 pa_context_unref(context);
665 assert(mainloop_api);
666 mainloop_api->io_free(stdio_event);
670 assert(mainloop_api);
671 mainloop_api->time_free(time_event);
683 pa_xfree(client_name);
684 pa_xfree(stream_name);