00001 /* $Id: pacat.c 1179 2006-08-01 21:04:43Z lennart $ */ 00002 00003 /*** 00004 This file is part of PulseAudio. 00005 00006 PulseAudio is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU Lesser General Public License as published 00008 by the Free Software Foundation; either version 2 of the License, 00009 or (at your option) any later version. 00010 00011 PulseAudio is distributed in the hope that it will be useful, but 00012 WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public License 00017 along with PulseAudio; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00019 USA. 00020 ***/ 00021 00022 #ifdef HAVE_CONFIG_H 00023 #include <config.h> 00024 #endif 00025 00026 #include <signal.h> 00027 #include <string.h> 00028 #include <errno.h> 00029 #include <unistd.h> 00030 #include <assert.h> 00031 #include <stdio.h> 00032 #include <stdlib.h> 00033 #include <getopt.h> 00034 #include <fcntl.h> 00035 00036 #include <pulse/pulseaudio.h> 00037 00038 #define TIME_EVENT_USEC 50000 00039 00040 #if PA_API_VERSION < 9 00041 #error Invalid PulseAudio API version 00042 #endif 00043 00044 static enum { RECORD, PLAYBACK } mode = PLAYBACK; 00045 00046 static pa_context *context = NULL; 00047 static pa_stream *stream = NULL; 00048 static pa_mainloop_api *mainloop_api = NULL; 00049 00050 static void *buffer = NULL; 00051 static size_t buffer_length = 0, buffer_index = 0; 00052 00053 static pa_io_event* stdio_event = NULL; 00054 00055 static char *stream_name = NULL, *client_name = NULL, *device = NULL; 00056 00057 static int verbose = 0; 00058 static pa_volume_t volume = PA_VOLUME_NORM; 00059 00060 static pa_sample_spec sample_spec = { 00061 .format = PA_SAMPLE_S16LE, 00062 .rate = 44100, 00063 .channels = 2 00064 }; 00065 00066 static pa_channel_map channel_map; 00067 static int channel_map_set = 0; 00068 00069 /* A shortcut for terminating the application */ 00070 static void quit(int ret) { 00071 assert(mainloop_api); 00072 mainloop_api->quit(mainloop_api, ret); 00073 } 00074 00075 /* Write some data to the stream */ 00076 static void do_stream_write(size_t length) { 00077 size_t l; 00078 assert(length); 00079 00080 if (!buffer || !buffer_length) 00081 return; 00082 00083 l = length; 00084 if (l > buffer_length) 00085 l = buffer_length; 00086 00087 if (pa_stream_write(stream, (uint8_t*) buffer + buffer_index, l, NULL, 0, PA_SEEK_RELATIVE) < 0) { 00088 fprintf(stderr, "pa_stream_write() failed: %s\n", pa_strerror(pa_context_errno(context))); 00089 quit(1); 00090 return; 00091 } 00092 00093 buffer_length -= l; 00094 buffer_index += l; 00095 00096 if (!buffer_length) { 00097 pa_xfree(buffer); 00098 buffer = NULL; 00099 buffer_index = buffer_length = 0; 00100 } 00101 } 00102 00103 /* This is called whenever new data may be written to the stream */ 00104 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) { 00105 assert(s && length); 00106 00107 if (stdio_event) 00108 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_INPUT); 00109 00110 if (!buffer) 00111 return; 00112 00113 do_stream_write(length); 00114 } 00115 00116 /* This is called whenever new data may is available */ 00117 static void stream_read_callback(pa_stream *s, size_t length, void *userdata) { 00118 const void *data; 00119 assert(s && length); 00120 00121 if (stdio_event) 00122 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_OUTPUT); 00123 00124 if (pa_stream_peek(s, &data, &length) < 0) { 00125 fprintf(stderr, "pa_stream_peek() failed: %s\n", pa_strerror(pa_context_errno(context))); 00126 quit(1); 00127 return; 00128 } 00129 00130 assert(data && length); 00131 00132 if (buffer) { 00133 fprintf(stderr, "Buffer overrun, dropping incoming data\n"); 00134 if (pa_stream_drop(s) < 0) { 00135 fprintf(stderr, "pa_stream_drop() failed: %s\n", pa_strerror(pa_context_errno(context))); 00136 quit(1); 00137 } 00138 return; 00139 } 00140 00141 buffer = pa_xmalloc(buffer_length = length); 00142 memcpy(buffer, data, length); 00143 buffer_index = 0; 00144 pa_stream_drop(s); 00145 } 00146 00147 /* This routine is called whenever the stream state changes */ 00148 static void stream_state_callback(pa_stream *s, void *userdata) { 00149 assert(s); 00150 00151 switch (pa_stream_get_state(s)) { 00152 case PA_STREAM_CREATING: 00153 case PA_STREAM_TERMINATED: 00154 break; 00155 00156 case PA_STREAM_READY: 00157 if (verbose) { 00158 const pa_buffer_attr *a; 00159 00160 fprintf(stderr, "Stream successfully created.\n"); 00161 00162 if (!(a = pa_stream_get_buffer_attr(s))) 00163 fprintf(stderr, "pa_stream_get_buffer_attr() failed: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s)))); 00164 else { 00165 00166 if (mode == PLAYBACK) 00167 fprintf(stderr, "Buffer metrics: maxlength=%u, tlength=%u, prebuf=%u, minreq=%u\n", a->maxlength, a->tlength, a->prebuf, a->minreq); 00168 else { 00169 assert(mode == RECORD); 00170 fprintf(stderr, "Buffer metrics: maxlength=%u, fragsize=%u\n", a->maxlength, a->fragsize); 00171 } 00172 00173 } 00174 00175 } 00176 00177 break; 00178 00179 case PA_STREAM_FAILED: 00180 default: 00181 fprintf(stderr, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s)))); 00182 quit(1); 00183 } 00184 } 00185 00186 /* This is called whenever the context status changes */ 00187 static void context_state_callback(pa_context *c, void *userdata) { 00188 assert(c); 00189 00190 switch (pa_context_get_state(c)) { 00191 case PA_CONTEXT_CONNECTING: 00192 case PA_CONTEXT_AUTHORIZING: 00193 case PA_CONTEXT_SETTING_NAME: 00194 break; 00195 00196 case PA_CONTEXT_READY: { 00197 int r; 00198 00199 assert(c && !stream); 00200 00201 if (verbose) 00202 fprintf(stderr, "Connection established.\n"); 00203 00204 if (!(stream = pa_stream_new(c, stream_name, &sample_spec, channel_map_set ? &channel_map : NULL))) { 00205 fprintf(stderr, "pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(c))); 00206 goto fail; 00207 } 00208 00209 pa_stream_set_state_callback(stream, stream_state_callback, NULL); 00210 pa_stream_set_write_callback(stream, stream_write_callback, NULL); 00211 pa_stream_set_read_callback(stream, stream_read_callback, NULL); 00212 00213 if (mode == PLAYBACK) { 00214 pa_cvolume cv; 00215 if ((r = pa_stream_connect_playback(stream, device, NULL, 0, pa_cvolume_set(&cv, sample_spec.channels, volume), NULL)) < 0) { 00216 fprintf(stderr, "pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(c))); 00217 goto fail; 00218 } 00219 00220 } else { 00221 if ((r = pa_stream_connect_record(stream, device, NULL, 0)) < 0) { 00222 fprintf(stderr, "pa_stream_connect_record() failed: %s\n", pa_strerror(pa_context_errno(c))); 00223 goto fail; 00224 } 00225 } 00226 00227 break; 00228 } 00229 00230 case PA_CONTEXT_TERMINATED: 00231 quit(0); 00232 break; 00233 00234 case PA_CONTEXT_FAILED: 00235 default: 00236 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c))); 00237 goto fail; 00238 } 00239 00240 return; 00241 00242 fail: 00243 quit(1); 00244 00245 } 00246 00247 /* Connection draining complete */ 00248 static void context_drain_complete(pa_context*c, void *userdata) { 00249 pa_context_disconnect(c); 00250 } 00251 00252 /* Stream draining complete */ 00253 static void stream_drain_complete(pa_stream*s, int success, void *userdata) { 00254 pa_operation *o; 00255 00256 if (!success) { 00257 fprintf(stderr, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context))); 00258 quit(1); 00259 } 00260 00261 if (verbose) 00262 fprintf(stderr, "Playback stream drained.\n"); 00263 00264 pa_stream_disconnect(stream); 00265 pa_stream_unref(stream); 00266 stream = NULL; 00267 00268 if (!(o = pa_context_drain(context, context_drain_complete, NULL))) 00269 pa_context_disconnect(context); 00270 else { 00271 if (verbose) 00272 fprintf(stderr, "Draining connection to server.\n"); 00273 } 00274 } 00275 00276 /* New data on STDIN **/ 00277 static void stdin_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) { 00278 size_t l, w = 0; 00279 ssize_t r; 00280 assert(a == mainloop_api && e && stdio_event == e); 00281 00282 if (buffer) { 00283 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL); 00284 return; 00285 } 00286 00287 if (!stream || pa_stream_get_state(stream) != PA_STREAM_READY || !(l = w = pa_stream_writable_size(stream))) 00288 l = 4096; 00289 00290 buffer = pa_xmalloc(l); 00291 00292 if ((r = read(fd, buffer, l)) <= 0) { 00293 if (r == 0) { 00294 if (verbose) 00295 fprintf(stderr, "Got EOF.\n"); 00296 00297 if (stream) { 00298 pa_operation *o; 00299 00300 if (!(o = pa_stream_drain(stream, stream_drain_complete, NULL))) { 00301 fprintf(stderr, "pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(context))); 00302 quit(1); 00303 return; 00304 } 00305 00306 pa_operation_unref(o); 00307 } else 00308 quit(0); 00309 00310 } else { 00311 fprintf(stderr, "read() failed: %s\n", strerror(errno)); 00312 quit(1); 00313 } 00314 00315 mainloop_api->io_free(stdio_event); 00316 stdio_event = NULL; 00317 return; 00318 } 00319 00320 buffer_length = r; 00321 buffer_index = 0; 00322 00323 if (w) 00324 do_stream_write(w); 00325 } 00326 00327 /* Some data may be written to STDOUT */ 00328 static void stdout_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) { 00329 ssize_t r; 00330 assert(a == mainloop_api && e && stdio_event == e); 00331 00332 if (!buffer) { 00333 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL); 00334 return; 00335 } 00336 00337 assert(buffer_length); 00338 00339 if ((r = write(fd, (uint8_t*) buffer+buffer_index, buffer_length)) <= 0) { 00340 fprintf(stderr, "write() failed: %s\n", strerror(errno)); 00341 quit(1); 00342 00343 mainloop_api->io_free(stdio_event); 00344 stdio_event = NULL; 00345 return; 00346 } 00347 00348 buffer_length -= r; 00349 buffer_index += r; 00350 00351 if (!buffer_length) { 00352 pa_xfree(buffer); 00353 buffer = NULL; 00354 buffer_length = buffer_index = 0; 00355 } 00356 } 00357 00358 /* UNIX signal to quit recieved */ 00359 static void exit_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) { 00360 if (verbose) 00361 fprintf(stderr, "Got signal, exiting.\n"); 00362 quit(0); 00363 } 00364 00365 /* Show the current latency */ 00366 static void stream_update_timing_callback(pa_stream *s, int success, void *userdata) { 00367 pa_usec_t latency, usec; 00368 int negative = 0; 00369 00370 assert(s); 00371 00372 if (!success || 00373 pa_stream_get_time(s, &usec) < 0 || 00374 pa_stream_get_latency(s, &latency, &negative) < 0) { 00375 fprintf(stderr, "Failed to get latency: %s\n", pa_strerror(pa_context_errno(context))); 00376 quit(1); 00377 return; 00378 } 00379 00380 fprintf(stderr, "Time: %0.3f sec; Latency: %0.0f usec. \r", 00381 (float) usec / 1000000, 00382 (float) latency * (negative?-1:1)); 00383 } 00384 00385 /* Someone requested that the latency is shown */ 00386 static void sigusr1_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) { 00387 00388 if (!stream) 00389 return; 00390 00391 pa_operation_unref(pa_stream_update_timing_info(stream, stream_update_timing_callback, NULL)); 00392 } 00393 00394 static void time_event_callback(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) { 00395 struct timeval next; 00396 00397 if (stream && pa_stream_get_state(stream) == PA_STREAM_READY) { 00398 pa_operation *o; 00399 if (!(o = pa_stream_update_timing_info(stream, stream_update_timing_callback, NULL))) 00400 fprintf(stderr, "pa_stream_update_timing_info() failed: %s\n", pa_strerror(pa_context_errno(context))); 00401 else 00402 pa_operation_unref(o); 00403 } 00404 00405 pa_gettimeofday(&next); 00406 pa_timeval_add(&next, TIME_EVENT_USEC); 00407 00408 m->time_restart(e, &next); 00409 } 00410 00411 static void help(const char *argv0) { 00412 00413 printf("%s [options]\n\n" 00414 " -h, --help Show this help\n" 00415 " --version Show version\n\n" 00416 " -r, --record Create a connection for recording\n" 00417 " -p, --playback Create a connection for playback\n\n" 00418 " -v, --verbose Enable verbose operations\n\n" 00419 " -s, --server=SERVER The name of the server to connect to\n" 00420 " -d, --device=DEVICE The name of the sink/source to connect to\n" 00421 " -n, --client-name=NAME How to call this client on the server\n" 00422 " --stream-name=NAME How to call this stream on the server\n" 00423 " --volume=VOLUME Specify the initial (linear) volume in range 0...65536\n" 00424 " --rate=SAMPLERATE The sample rate in Hz (defaults to 44100)\n" 00425 " --format=SAMPLEFORMAT The sample type, one of s16le, s16be, u8, float32le,\n" 00426 " float32be, ulaw, alaw (defaults to s16ne)\n" 00427 " --channels=CHANNELS The number of channels, 1 for mono, 2 for stereo\n" 00428 " (defaults to 2)\n" 00429 " --channel-map=CHANNELMAP Channel map to use instead of the default\n", 00430 argv0); 00431 } 00432 00433 enum { 00434 ARG_VERSION = 256, 00435 ARG_STREAM_NAME, 00436 ARG_VOLUME, 00437 ARG_SAMPLERATE, 00438 ARG_SAMPLEFORMAT, 00439 ARG_CHANNELS, 00440 ARG_CHANNELMAP, 00441 }; 00442 00443 int main(int argc, char *argv[]) { 00444 pa_mainloop* m = NULL; 00445 int ret = 1, r, c; 00446 char *bn, *server = NULL; 00447 pa_time_event *time_event = NULL; 00448 00449 static const struct option long_options[] = { 00450 {"record", 0, NULL, 'r'}, 00451 {"playback", 0, NULL, 'p'}, 00452 {"device", 1, NULL, 'd'}, 00453 {"server", 1, NULL, 's'}, 00454 {"client-name", 1, NULL, 'n'}, 00455 {"stream-name", 1, NULL, ARG_STREAM_NAME}, 00456 {"version", 0, NULL, ARG_VERSION}, 00457 {"help", 0, NULL, 'h'}, 00458 {"verbose", 0, NULL, 'v'}, 00459 {"volume", 1, NULL, ARG_VOLUME}, 00460 {"rate", 1, NULL, ARG_SAMPLERATE}, 00461 {"format", 1, NULL, ARG_SAMPLEFORMAT}, 00462 {"channels", 1, NULL, ARG_CHANNELS}, 00463 {"channel-map", 1, NULL, ARG_CHANNELMAP}, 00464 {NULL, 0, NULL, 0} 00465 }; 00466 00467 if (!(bn = strrchr(argv[0], '/'))) 00468 bn = argv[0]; 00469 else 00470 bn++; 00471 00472 if (strstr(bn, "rec") || strstr(bn, "mon")) 00473 mode = RECORD; 00474 else if (strstr(bn, "cat") || strstr(bn, "play")) 00475 mode = PLAYBACK; 00476 00477 while ((c = getopt_long(argc, argv, "rpd:s:n:hv", long_options, NULL)) != -1) { 00478 00479 switch (c) { 00480 case 'h' : 00481 help(bn); 00482 ret = 0; 00483 goto quit; 00484 00485 case ARG_VERSION: 00486 printf("pacat "PACKAGE_VERSION"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version()); 00487 ret = 0; 00488 goto quit; 00489 00490 case 'r': 00491 mode = RECORD; 00492 break; 00493 00494 case 'p': 00495 mode = PLAYBACK; 00496 break; 00497 00498 case 'd': 00499 pa_xfree(device); 00500 device = pa_xstrdup(optarg); 00501 break; 00502 00503 case 's': 00504 pa_xfree(server); 00505 server = pa_xstrdup(optarg); 00506 break; 00507 00508 case 'n': 00509 pa_xfree(client_name); 00510 client_name = pa_xstrdup(optarg); 00511 break; 00512 00513 case ARG_STREAM_NAME: 00514 pa_xfree(stream_name); 00515 stream_name = pa_xstrdup(optarg); 00516 break; 00517 00518 case 'v': 00519 verbose = 1; 00520 break; 00521 00522 case ARG_VOLUME: { 00523 int v = atoi(optarg); 00524 volume = v < 0 ? 0 : v; 00525 break; 00526 } 00527 00528 case ARG_CHANNELS: 00529 sample_spec.channels = atoi(optarg); 00530 break; 00531 00532 case ARG_SAMPLEFORMAT: 00533 sample_spec.format = pa_parse_sample_format(optarg); 00534 break; 00535 00536 case ARG_SAMPLERATE: 00537 sample_spec.rate = atoi(optarg); 00538 break; 00539 00540 case ARG_CHANNELMAP: 00541 if (!pa_channel_map_parse(&channel_map, optarg)) { 00542 fprintf(stderr, "Invalid channel map\n"); 00543 goto quit; 00544 } 00545 00546 channel_map_set = 1; 00547 break; 00548 00549 default: 00550 goto quit; 00551 } 00552 } 00553 00554 if (!pa_sample_spec_valid(&sample_spec)) { 00555 fprintf(stderr, "Invalid sample specification\n"); 00556 goto quit; 00557 } 00558 00559 if (channel_map_set && channel_map.channels != sample_spec.channels) { 00560 fprintf(stderr, "Channel map doesn't match sample specification\n"); 00561 goto quit; 00562 } 00563 00564 if (verbose) { 00565 char t[PA_SAMPLE_SPEC_SNPRINT_MAX]; 00566 pa_sample_spec_snprint(t, sizeof(t), &sample_spec); 00567 fprintf(stderr, "Opening a %s stream with sample specification '%s'.\n", mode == RECORD ? "recording" : "playback", t); 00568 } 00569 00570 if (!(optind >= argc)) { 00571 if (optind+1 == argc) { 00572 int fd; 00573 00574 if ((fd = open(argv[optind], mode == PLAYBACK ? O_RDONLY : O_WRONLY|O_TRUNC|O_CREAT, 0666)) < 0) { 00575 fprintf(stderr, "open(): %s\n", strerror(errno)); 00576 goto quit; 00577 } 00578 00579 if (dup2(fd, mode == PLAYBACK ? 0 : 1) < 0) { 00580 fprintf(stderr, "dup2(): %s\n", strerror(errno)); 00581 goto quit; 00582 } 00583 00584 close(fd); 00585 00586 if (!stream_name) 00587 stream_name = pa_xstrdup(argv[optind]); 00588 00589 } else { 00590 fprintf(stderr, "Too many arguments.\n"); 00591 goto quit; 00592 } 00593 } 00594 00595 if (!client_name) 00596 client_name = pa_xstrdup(bn); 00597 00598 if (!stream_name) 00599 stream_name = pa_xstrdup(client_name); 00600 00601 /* Set up a new main loop */ 00602 if (!(m = pa_mainloop_new())) { 00603 fprintf(stderr, "pa_mainloop_new() failed.\n"); 00604 goto quit; 00605 } 00606 00607 mainloop_api = pa_mainloop_get_api(m); 00608 00609 r = pa_signal_init(mainloop_api); 00610 assert(r == 0); 00611 pa_signal_new(SIGINT, exit_signal_callback, NULL); 00612 pa_signal_new(SIGTERM, exit_signal_callback, NULL); 00613 #ifdef SIGUSR1 00614 pa_signal_new(SIGUSR1, sigusr1_signal_callback, NULL); 00615 #endif 00616 #ifdef SIGPIPE 00617 signal(SIGPIPE, SIG_IGN); 00618 #endif 00619 00620 if (!(stdio_event = mainloop_api->io_new(mainloop_api, 00621 mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO, 00622 mode == PLAYBACK ? PA_IO_EVENT_INPUT : PA_IO_EVENT_OUTPUT, 00623 mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) { 00624 fprintf(stderr, "io_new() failed.\n"); 00625 goto quit; 00626 } 00627 00628 /* Create a new connection context */ 00629 if (!(context = pa_context_new(mainloop_api, client_name))) { 00630 fprintf(stderr, "pa_context_new() failed.\n"); 00631 goto quit; 00632 } 00633 00634 pa_context_set_state_callback(context, context_state_callback, NULL); 00635 00636 /* Connect the context */ 00637 pa_context_connect(context, server, 0, NULL); 00638 00639 if (verbose) { 00640 struct timeval tv; 00641 00642 pa_gettimeofday(&tv); 00643 pa_timeval_add(&tv, TIME_EVENT_USEC); 00644 00645 if (!(time_event = mainloop_api->time_new(mainloop_api, &tv, time_event_callback, NULL))) { 00646 fprintf(stderr, "time_new() failed.\n"); 00647 goto quit; 00648 } 00649 } 00650 00651 /* Run the main loop */ 00652 if (pa_mainloop_run(m, &ret) < 0) { 00653 fprintf(stderr, "pa_mainloop_run() failed.\n"); 00654 goto quit; 00655 } 00656 00657 quit: 00658 if (stream) 00659 pa_stream_unref(stream); 00660 00661 if (context) 00662 pa_context_unref(context); 00663 00664 if (stdio_event) { 00665 assert(mainloop_api); 00666 mainloop_api->io_free(stdio_event); 00667 } 00668 00669 if (time_event) { 00670 assert(mainloop_api); 00671 mainloop_api->time_free(time_event); 00672 } 00673 00674 if (m) { 00675 pa_signal_done(); 00676 pa_mainloop_free(m); 00677 } 00678 00679 pa_xfree(buffer); 00680 00681 pa_xfree(server); 00682 pa_xfree(device); 00683 pa_xfree(client_name); 00684 pa_xfree(stream_name); 00685 00686 return ret; 00687 }