4 This file is part of polypaudio.
6 polypaudio 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 polypaudio 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 polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 #include <polyp/polypaudio.h>
37 #include <polyp/mainloop.h>
38 #include <polyp/mainloop-signal.h>
40 #if PA_API_VERSION != 8
41 #error Invalid Polypaudio 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 /* A shortcut for terminating the application */
67 static void quit(int ret) {
69 mainloop_api->quit(mainloop_api, ret);
72 /* Write some data to the stream */
73 static void do_stream_write(size_t length) {
77 if (!buffer || !buffer_length)
81 if (l > buffer_length)
84 if (pa_stream_write(stream, (uint8_t*) buffer + buffer_index, l, NULL, 0, PA_SEEK_RELATIVE) < 0) {
85 fprintf(stderr, "pa_stream_write() failed: %s\n", pa_strerror(pa_context_errno(context)));
96 buffer_index = buffer_length = 0;
100 /* This is called whenever new data may be written to the stream */
101 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
105 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_INPUT);
110 do_stream_write(length);
113 /* This is called whenever new data may is available */
114 static void stream_read_callback(pa_stream *s, size_t length, void *userdata) {
119 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_OUTPUT);
121 if (pa_stream_peek(s, &data, &length) < 0) {
122 fprintf(stderr, "pa_stream_peek() failed: %s\n", pa_strerror(pa_context_errno(context)));
127 assert(data && length);
130 fprintf(stderr, "Buffer overrun, dropping incoming data\n");
131 if (pa_stream_drop(s) < 0) {
132 fprintf(stderr, "pa_stream_drop() failed: %s\n", pa_strerror(pa_context_errno(context)));
138 buffer = malloc(buffer_length = length);
140 memcpy(buffer, data, length);
145 /* This routine is called whenever the stream state changes */
146 static void stream_state_callback(pa_stream *s, void *userdata) {
149 switch (pa_stream_get_state(s)) {
150 case PA_STREAM_CREATING:
151 case PA_STREAM_TERMINATED:
154 case PA_STREAM_READY:
156 fprintf(stderr, "Stream successfully created\n");
159 case PA_STREAM_FAILED:
161 fprintf(stderr, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
166 /* This is called whenever the context status changes */
167 static void context_state_callback(pa_context *c, void *userdata) {
170 switch (pa_context_get_state(c)) {
171 case PA_CONTEXT_CONNECTING:
172 case PA_CONTEXT_AUTHORIZING:
173 case PA_CONTEXT_SETTING_NAME:
176 case PA_CONTEXT_READY: {
179 assert(c && !stream);
182 fprintf(stderr, "Connection established.\n");
184 if (!(stream = pa_stream_new(c, stream_name, &sample_spec, NULL))) {
185 fprintf(stderr, "pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(c)));
189 pa_stream_set_state_callback(stream, stream_state_callback, NULL);
190 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
191 pa_stream_set_read_callback(stream, stream_read_callback, NULL);
193 if (mode == PLAYBACK) {
195 if ((r = pa_stream_connect_playback(stream, device, NULL, 0, pa_cvolume_set(&cv, sample_spec.channels, volume), NULL)) < 0) {
196 fprintf(stderr, "pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(c)));
201 if ((r = pa_stream_connect_record(stream, device, NULL, 0)) < 0) {
202 fprintf(stderr, "pa_stream_connect_record() failed: %s\n", pa_strerror(pa_context_errno(c)));
210 case PA_CONTEXT_TERMINATED:
214 case PA_CONTEXT_FAILED:
216 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
227 /* Connection draining complete */
228 static void context_drain_complete(pa_context*c, void *userdata) {
229 pa_context_disconnect(c);
232 /* Stream draining complete */
233 static void stream_drain_complete(pa_stream*s, int success, void *userdata) {
237 fprintf(stderr, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context)));
242 fprintf(stderr, "Playback stream drained.\n");
244 pa_stream_disconnect(stream);
245 pa_stream_unref(stream);
248 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
249 pa_context_disconnect(context);
252 fprintf(stderr, "Draining connection to server.\n");
256 /* New data on STDIN **/
257 static void stdin_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
260 assert(a == mainloop_api && e && stdio_event == e);
263 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
267 if (!stream || pa_stream_get_state(stream) != PA_STREAM_READY || !(l = w = pa_stream_writable_size(stream)))
272 if ((r = read(fd, buffer, l)) <= 0) {
277 fprintf(stderr, "Got EOF.\n");
279 if (!(o = pa_stream_drain(stream, stream_drain_complete, NULL))) {
280 fprintf(stderr, "pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(context)));
285 pa_operation_unref(o);
287 fprintf(stderr, "read() failed: %s\n", strerror(errno));
291 mainloop_api->io_free(stdio_event);
303 /* Some data may be written to STDOUT */
304 static void stdout_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
306 assert(a == mainloop_api && e && stdio_event == e);
309 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
313 assert(buffer_length);
315 if ((r = write(fd, (uint8_t*) buffer+buffer_index, buffer_length)) <= 0) {
316 fprintf(stderr, "write() failed: %s\n", strerror(errno));
319 mainloop_api->io_free(stdio_event);
327 if (!buffer_length) {
330 buffer_length = buffer_index = 0;
334 /* UNIX signal to quit recieved */
335 static void exit_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
337 fprintf(stderr, "Got signal, exiting.\n");
342 /* Show the current latency */
343 static void stream_update_latency_callback(pa_stream *s, int success, void *userdata) {
346 const pa_latency_info *i;
351 !(i = pa_stream_get_latency_info(s)) ||
352 pa_stream_get_latency(s, &total, &negative) < 0) {
353 fprintf(stderr, "Failed to get latency: %s\n", pa_strerror(pa_context_errno(context)));
358 fprintf(stderr, "Latency: buffer: %0.0f usec; sink: %0.0f usec; source: %0.0f usec; transport: %0.0f usec; total: %0.0f usec; synchronized clocks: %s.\n",
359 (float) i->buffer_usec,
360 (float) i->sink_usec,
361 (float) i->source_usec,
362 (float) i->transport_usec,
363 (float) total * (negative?-1:1),
364 i->synchronized_clocks ? "yes" : "no");
367 /* Someone requested that the latency is shown */
368 static void sigusr1_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
369 fprintf(stderr, "Got SIGUSR1, requesting latency.\n");
370 pa_operation_unref(pa_stream_update_latency_info(stream, stream_update_latency_callback, NULL));
374 static void help(const char *argv0) {
376 printf("%s [options]\n\n"
377 " -h, --help Show this help\n"
378 " --version Show version\n\n"
379 " -r, --record Create a connection for recording\n"
380 " -p, --playback Create a connection for playback\n\n"
381 " -v, --verbose Enable verbose operations\n\n"
382 " -s, --server=SERVER The name of the server to connect to\n"
383 " -d, --device=DEVICE The name of the sink/source to connect to\n"
384 " -n, --client-name=NAME How to call this client on the server\n"
385 " --stream-name=NAME How to call this stream on the server\n"
386 " --volume=VOLUME Specify the initial (linear) volume in range 0...256\n"
387 " --rate=SAMPLERATE The sample rate in Hz (defaults to 44100)\n"
388 " --format=SAMPLEFORMAT The sample type, one of s16le, s16be, u8, float32le,\n"
389 " float32be, ulaw, alaw (defaults to s16ne)\n"
390 " --channels=CHANNELS The number of channels, 1 for mono, 2 for stereo\n"
391 " (defaults to 2)\n",
404 int main(int argc, char *argv[]) {
405 pa_mainloop* m = NULL;
407 char *bn, *server = NULL;
409 static const struct option long_options[] = {
410 {"record", 0, NULL, 'r'},
411 {"playback", 0, NULL, 'p'},
412 {"device", 1, NULL, 'd'},
413 {"server", 1, NULL, 's'},
414 {"client-name", 1, NULL, 'n'},
415 {"stream-name", 1, NULL, ARG_STREAM_NAME},
416 {"version", 0, NULL, ARG_VERSION},
417 {"help", 0, NULL, 'h'},
418 {"verbose", 0, NULL, 'v'},
419 {"volume", 1, NULL, ARG_VOLUME},
420 {"rate", 1, NULL, ARG_SAMPLERATE},
421 {"format", 1, NULL, ARG_SAMPLEFORMAT},
422 {"channels", 1, NULL, ARG_CHANNELS},
426 if (!(bn = strrchr(argv[0], '/')))
431 if (strstr(bn, "rec") || strstr(bn, "mon"))
433 else if (strstr(bn, "cat") || strstr(bn, "play"))
436 while ((c = getopt_long(argc, argv, "rpd:s:n:hv", long_options, NULL)) != -1) {
445 printf("pacat "PACKAGE_VERSION"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
459 device = strdup(optarg);
464 server = strdup(optarg);
469 client_name = strdup(optarg);
472 case ARG_STREAM_NAME:
474 stream_name = strdup(optarg);
482 int v = atoi(optarg);
483 volume = v < 0 ? 0 : v;
488 sample_spec.channels = atoi(optarg);
491 case ARG_SAMPLEFORMAT:
492 sample_spec.format = pa_parse_sample_format(optarg);
496 sample_spec.rate = atoi(optarg);
505 client_name = strdup(bn);
508 stream_name = strdup(client_name);
510 if (!pa_sample_spec_valid(&sample_spec)) {
511 fprintf(stderr, "Invalid sample specification\n");
516 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
517 pa_sample_spec_snprint(t, sizeof(t), &sample_spec);
518 fprintf(stderr, "Opening a %s stream with sample specification '%s'.\n", mode == RECORD ? "recording" : "playback", t);
521 if (optind+1 < argc) {
522 fprintf(stderr, "Too many arguments.\n");
526 if (optind+1 == argc) {
529 if ((fd = open(argv[optind], O_RDONLY)) < 0) {
530 fprintf(stderr, "open(): %s\n", strerror(errno));
534 if (dup2(fd, 0) < 0) {
535 fprintf(stderr, "dup2(): %s\n", strerror(errno));
542 /* Set up a new main loop */
543 if (!(m = pa_mainloop_new())) {
544 fprintf(stderr, "pa_mainloop_new() failed.\n");
548 mainloop_api = pa_mainloop_get_api(m);
550 r = pa_signal_init(mainloop_api);
552 pa_signal_new(SIGINT, exit_signal_callback, NULL);
553 pa_signal_new(SIGTERM, exit_signal_callback, NULL);
555 pa_signal_new(SIGUSR1, sigusr1_signal_callback, NULL);
558 signal(SIGPIPE, SIG_IGN);
561 if (!(stdio_event = mainloop_api->io_new(mainloop_api,
562 mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO,
563 mode == PLAYBACK ? PA_IO_EVENT_INPUT : PA_IO_EVENT_OUTPUT,
564 mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) {
565 fprintf(stderr, "source_io() failed.\n");
569 /* Create a new connection context */
570 if (!(context = pa_context_new(mainloop_api, client_name))) {
571 fprintf(stderr, "pa_context_new() failed.\n");
575 pa_context_set_state_callback(context, context_state_callback, NULL);
577 /* Connect the context */
578 pa_context_connect(context, server, 0, NULL);
580 /* Run the main loop */
581 if (pa_mainloop_run(m, &ret) < 0) {
582 fprintf(stderr, "pa_mainloop_run() failed.\n");
588 pa_stream_unref(stream);
591 pa_context_unref(context);
594 assert(mainloop_api);
595 mainloop_api->io_free(stdio_event);