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
35 #include <polyp/polyplib.h>
36 #include <polyp/polyplib-error.h>
37 #include <polyp/mainloop.h>
38 #include <polyp/mainloop-signal.h>
39 #include <polyp/polyplib-version.h>
41 #if PA_API_VERSION != 7
42 #error Invalid Polypaudio API version
45 static enum { RECORD, PLAYBACK } mode = PLAYBACK;
47 static struct pa_context *context = NULL;
48 static struct pa_stream *stream = NULL;
49 static struct pa_mainloop_api *mainloop_api = NULL;
51 static void *buffer = NULL;
52 static size_t buffer_length = 0, buffer_index = 0;
54 static struct 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;
61 static struct pa_sample_spec sample_spec = {
62 .format = PA_SAMPLE_S16LE,
67 /* A shortcut for terminating the application */
68 static void quit(int ret) {
70 mainloop_api->quit(mainloop_api, ret);
73 /* Write some data to the stream */
74 static void do_stream_write(size_t length) {
78 if (!buffer || !buffer_length)
82 if (l > buffer_length)
85 pa_stream_write(stream, (uint8_t*) buffer + buffer_index, l, NULL, 0);
92 buffer_index = buffer_length = 0;
96 /* This is called whenever new data may be written to the stream */
97 static void stream_write_callback(struct pa_stream *s, size_t length, void *userdata) {
101 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_INPUT);
106 do_stream_write(length);
109 /* This is called whenever new data may is available */
110 static void stream_read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) {
111 assert(s && data && length);
114 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_OUTPUT);
117 fprintf(stderr, "Buffer overrun, dropping incoming data\n");
121 buffer = malloc(buffer_length = length);
123 memcpy(buffer, data, length);
127 /* This routine is called whenever the stream state changes */
128 static void stream_state_callback(struct pa_stream *s, void *userdata) {
131 switch (pa_stream_get_state(s)) {
132 case PA_STREAM_CREATING:
133 case PA_STREAM_TERMINATED:
136 case PA_STREAM_READY:
138 fprintf(stderr, "Stream successfully created\n");
141 case PA_STREAM_FAILED:
143 fprintf(stderr, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
148 /* This is called whenever the context status changes */
149 static void context_state_callback(struct pa_context *c, void *userdata) {
152 switch (pa_context_get_state(c)) {
153 case PA_CONTEXT_CONNECTING:
154 case PA_CONTEXT_AUTHORIZING:
155 case PA_CONTEXT_SETTING_NAME:
158 case PA_CONTEXT_READY:
160 assert(c && !stream);
163 fprintf(stderr, "Connection established.\n");
165 stream = pa_stream_new(c, stream_name, &sample_spec);
168 pa_stream_set_state_callback(stream, stream_state_callback, NULL);
169 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
170 pa_stream_set_read_callback(stream, stream_read_callback, NULL);
172 if (mode == PLAYBACK)
173 pa_stream_connect_playback(stream, device, NULL, 0, volume);
175 pa_stream_connect_record(stream, device, NULL, 0);
179 case PA_CONTEXT_TERMINATED:
183 case PA_CONTEXT_FAILED:
185 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
190 /* Connection draining complete */
191 static void context_drain_complete(struct pa_context*c, void *userdata) {
192 pa_context_disconnect(c);
195 /* Stream draining complete */
196 static void stream_drain_complete(struct pa_stream*s, int success, void *userdata) {
197 struct pa_operation *o;
200 fprintf(stderr, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context)));
205 fprintf(stderr, "Playback stream drained.\n");
207 pa_stream_disconnect(stream);
208 pa_stream_unref(stream);
211 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
212 pa_context_disconnect(context);
214 pa_operation_unref(o);
217 fprintf(stderr, "Draining connection to server.\n");
221 /* New data on STDIN **/
222 static void stdin_callback(struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
225 assert(a == mainloop_api && e && stdio_event == e);
228 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
232 if (!stream || pa_stream_get_state(stream) != PA_STREAM_READY || !(l = w = pa_stream_writable_size(stream)))
237 if ((r = read(fd, buffer, l)) <= 0) {
240 fprintf(stderr, "Got EOF.\n");
241 pa_operation_unref(pa_stream_drain(stream, stream_drain_complete, NULL));
243 fprintf(stderr, "read() failed: %s\n", strerror(errno));
247 mainloop_api->io_free(stdio_event);
259 /* Some data may be written to STDOUT */
260 static void stdout_callback(struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
262 assert(a == mainloop_api && e && stdio_event == e);
265 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
269 assert(buffer_length);
271 if ((r = write(fd, (uint8_t*) buffer+buffer_index, buffer_length)) <= 0) {
272 fprintf(stderr, "write() failed: %s\n", strerror(errno));
275 mainloop_api->io_free(stdio_event);
283 if (!buffer_length) {
286 buffer_length = buffer_index = 0;
290 /* UNIX signal to quit recieved */
291 static void exit_signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
293 fprintf(stderr, "Got SIGINT, exiting.\n");
298 /* Show the current latency */
299 static void stream_get_latency_callback(struct pa_stream *s, const struct pa_latency_info *i, void *userdata) {
305 fprintf(stderr, "Failed to get latency: %s\n", pa_strerror(pa_context_errno(context)));
310 total = pa_stream_get_latency(s, i, &negative);
312 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",
313 (float) i->buffer_usec, (float) i->sink_usec, (float) i->source_usec, (float) i->transport_usec, (float) total * (negative?-1:1),
314 i->synchronized_clocks ? "yes" : "no");
317 /* Someone requested that the latency is shown */
318 static void sigusr1_signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
319 fprintf(stderr, "Got SIGUSR1, requesting latency.\n");
320 pa_operation_unref(pa_stream_get_latency_info(stream, stream_get_latency_callback, NULL));
324 static void help(const char *argv0) {
326 printf("%s [options]\n\n"
327 " -h, --help Show this help\n"
328 " --version Show version\n\n"
329 " -r, --record Create a connection for recording\n"
330 " -p, --playback Create a connection for playback\n\n"
331 " -v, --verbose Enable verbose operations\n\n"
332 " -s, --server=SERVER The name of the server to connect to\n"
333 " -d, --device=DEVICE The name of the sink/source to connect to\n"
334 " -n, --client-name=NAME How to call this client on the server\n"
335 " --stream-name=NAME How to call this stream on the server\n"
336 " --volume=VOLUME Specify the initial (linear) volume in range 0...256\n"
337 " --rate=SAMPLERATE The sample rate in Hz (defaults to 44100)\n"
338 " --format=SAMPLEFORMAT The sample type, one of s16le, s16be, u8, float32le,\n"
339 " float32be, ulaw, alaw (defaults to s16ne)\n"
340 " --channels=CHANNELS The number of channels, 1 for mono, 2 for stereo\n"
341 " (defaults to 2)\n",
354 int main(int argc, char *argv[]) {
355 struct pa_mainloop* m = NULL;
357 char *bn, *server = NULL;
359 static const struct option long_options[] = {
360 {"record", 0, NULL, 'r'},
361 {"playback", 0, NULL, 'p'},
362 {"device", 1, NULL, 'd'},
363 {"server", 1, NULL, 's'},
364 {"client-name", 1, NULL, 'n'},
365 {"stream-name", 1, NULL, ARG_STREAM_NAME},
366 {"version", 0, NULL, ARG_VERSION},
367 {"help", 0, NULL, 'h'},
368 {"verbose", 0, NULL, 'v'},
369 {"volume", 1, NULL, ARG_VOLUME},
370 {"rate", 1, NULL, ARG_SAMPLERATE},
371 {"format", 1, NULL, ARG_SAMPLEFORMAT},
372 {"channels", 1, NULL, ARG_CHANNELS},
376 if (!(bn = strrchr(argv[0], '/')))
381 if (strstr(bn, "rec") || strstr(bn, "mon"))
383 else if (strstr(bn, "cat") || strstr(bn, "play"))
386 while ((c = getopt_long(argc, argv, "rpd:s:n:hv", long_options, NULL)) != -1) {
395 printf("pacat "PACKAGE_VERSION"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
409 device = strdup(optarg);
414 server = strdup(optarg);
419 client_name = strdup(optarg);
422 case ARG_STREAM_NAME:
424 stream_name = strdup(optarg);
432 int v = atoi(optarg);
433 volume = v < 0 ? 0 : v;
438 sample_spec.channels = atoi(optarg);
441 case ARG_SAMPLEFORMAT:
442 sample_spec.format = pa_parse_sample_format(optarg);
446 sample_spec.rate = atoi(optarg);
455 client_name = strdup(bn);
458 stream_name = strdup(client_name);
460 if (!pa_sample_spec_valid(&sample_spec)) {
461 fprintf(stderr, "Invalid sample specification\n");
466 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
467 pa_sample_spec_snprint(t, sizeof(t), &sample_spec);
468 fprintf(stderr, "Opening a %s stream with sample specification '%s'.\n", mode == RECORD ? "recording" : "playback", t);
471 /* Set up a new main loop */
472 if (!(m = pa_mainloop_new())) {
473 fprintf(stderr, "pa_mainloop_new() failed.\n");
477 mainloop_api = pa_mainloop_get_api(m);
479 r = pa_signal_init(mainloop_api);
481 pa_signal_new(SIGINT, exit_signal_callback, NULL);
482 pa_signal_new(SIGUSR1, sigusr1_signal_callback, NULL);
483 signal(SIGPIPE, SIG_IGN);
485 if (!(stdio_event = mainloop_api->io_new(mainloop_api,
486 mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO,
487 mode == PLAYBACK ? PA_IO_EVENT_INPUT : PA_IO_EVENT_OUTPUT,
488 mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) {
489 fprintf(stderr, "source_io() failed.\n");
493 /* Create a new connection context */
494 if (!(context = pa_context_new(mainloop_api, client_name))) {
495 fprintf(stderr, "pa_context_new() failed.\n");
499 pa_context_set_state_callback(context, context_state_callback, NULL);
501 /* Connect the context */
502 pa_context_connect(context, server, 1, NULL);
504 /* Run the main loop */
505 if (pa_mainloop_run(m, &ret) < 0) {
506 fprintf(stderr, "pa_mainloop_run() failed.\n");
512 pa_stream_unref(stream);
515 pa_context_unref(context);
518 assert(mainloop_api);
519 mainloop_api->io_free(stdio_event);