]> code.delx.au - pulseaudio/blob - src/utils/pacat.c
implement --process-time
[pulseaudio] / src / utils / pacat.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <signal.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <assert.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <getopt.h>
37 #include <fcntl.h>
38
39 #include <pulse/pulseaudio.h>
40
41 #define TIME_EVENT_USEC 50000
42
43 #if PA_API_VERSION < 10
44 #error Invalid PulseAudio API version
45 #endif
46
47 static enum { RECORD, PLAYBACK } mode = PLAYBACK;
48
49 static pa_context *context = NULL;
50 static pa_stream *stream = NULL;
51 static pa_mainloop_api *mainloop_api = NULL;
52
53 static void *buffer = NULL;
54 static size_t buffer_length = 0, buffer_index = 0;
55
56 static pa_io_event* stdio_event = NULL;
57
58 static char *stream_name = NULL, *client_name = NULL, *device = NULL;
59
60 static int verbose = 0;
61 static pa_volume_t volume = PA_VOLUME_NORM;
62
63 static pa_sample_spec sample_spec = {
64 .format = PA_SAMPLE_S16LE,
65 .rate = 44100,
66 .channels = 2
67 };
68
69 static pa_channel_map channel_map;
70 static int channel_map_set = 0;
71
72 static pa_stream_flags_t flags = 0;
73
74 static size_t latency = 0, process_time=0;
75
76 /* A shortcut for terminating the application */
77 static void quit(int ret) {
78 assert(mainloop_api);
79 mainloop_api->quit(mainloop_api, ret);
80 }
81
82 /* Write some data to the stream */
83 static void do_stream_write(size_t length) {
84 size_t l;
85 assert(length);
86
87 if (!buffer || !buffer_length)
88 return;
89
90 l = length;
91 if (l > buffer_length)
92 l = buffer_length;
93
94 if (pa_stream_write(stream, (uint8_t*) buffer + buffer_index, l, NULL, 0, PA_SEEK_RELATIVE) < 0) {
95 fprintf(stderr, "pa_stream_write() failed: %s\n", pa_strerror(pa_context_errno(context)));
96 quit(1);
97 return;
98 }
99
100 buffer_length -= l;
101 buffer_index += l;
102
103 if (!buffer_length) {
104 pa_xfree(buffer);
105 buffer = NULL;
106 buffer_index = buffer_length = 0;
107 }
108 }
109
110 /* This is called whenever new data may be written to the stream */
111 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
112 assert(s);
113 assert(length > 0);
114
115 if (stdio_event)
116 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_INPUT);
117
118 if (!buffer)
119 return;
120
121 do_stream_write(length);
122 }
123
124 /* This is called whenever new data may is available */
125 static void stream_read_callback(pa_stream *s, size_t length, void *userdata) {
126 const void *data;
127 assert(s);
128 assert(length > 0);
129
130 if (stdio_event)
131 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_OUTPUT);
132
133 if (pa_stream_peek(s, &data, &length) < 0) {
134 fprintf(stderr, "pa_stream_peek() failed: %s\n", pa_strerror(pa_context_errno(context)));
135 quit(1);
136 return;
137 }
138
139 assert(data);
140 assert(length > 0);
141
142 if (buffer) {
143 fprintf(stderr, "Buffer overrun, dropping incoming data\n");
144 if (pa_stream_drop(s) < 0) {
145 fprintf(stderr, "pa_stream_drop() failed: %s\n", pa_strerror(pa_context_errno(context)));
146 quit(1);
147 }
148 return;
149 }
150
151 buffer = pa_xmalloc(buffer_length = length);
152 memcpy(buffer, data, length);
153 buffer_index = 0;
154 pa_stream_drop(s);
155 }
156
157 /* This routine is called whenever the stream state changes */
158 static void stream_state_callback(pa_stream *s, void *userdata) {
159 assert(s);
160
161 switch (pa_stream_get_state(s)) {
162 case PA_STREAM_CREATING:
163 case PA_STREAM_TERMINATED:
164 break;
165
166 case PA_STREAM_READY:
167 if (verbose) {
168 const pa_buffer_attr *a;
169 char cmt[PA_CHANNEL_MAP_SNPRINT_MAX], sst[PA_SAMPLE_SPEC_SNPRINT_MAX];
170
171 fprintf(stderr, "Stream successfully created.\n");
172
173 if (!(a = pa_stream_get_buffer_attr(s)))
174 fprintf(stderr, "pa_stream_get_buffer_attr() failed: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
175 else {
176
177 if (mode == PLAYBACK)
178 fprintf(stderr, "Buffer metrics: maxlength=%u, tlength=%u, prebuf=%u, minreq=%u\n", a->maxlength, a->tlength, a->prebuf, a->minreq);
179 else {
180 assert(mode == RECORD);
181 fprintf(stderr, "Buffer metrics: maxlength=%u, fragsize=%u\n", a->maxlength, a->fragsize);
182 }
183 }
184
185 fprintf(stderr, "Using sample spec '%s', channel map '%s'.\n",
186 pa_sample_spec_snprint(sst, sizeof(sst), pa_stream_get_sample_spec(s)),
187 pa_channel_map_snprint(cmt, sizeof(cmt), pa_stream_get_channel_map(s)));
188
189 fprintf(stderr, "Connected to device %s (%u, %ssuspended).\n",
190 pa_stream_get_device_name(s),
191 pa_stream_get_device_index(s),
192 pa_stream_is_suspended(s) ? "" : "not ");
193 }
194
195 break;
196
197 case PA_STREAM_FAILED:
198 default:
199 fprintf(stderr, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
200 quit(1);
201 }
202 }
203
204 static void stream_suspended_callback(pa_stream *s, void *userdata) {
205 assert(s);
206
207 if (verbose) {
208 if (pa_stream_is_suspended(s))
209 fprintf(stderr, "Stream device suspended.\n");
210 else
211 fprintf(stderr, "Stream device resumed.\n");
212 }
213 }
214
215 static void stream_underflow_callback(pa_stream *s, void *userdata) {
216 assert(s);
217
218 fprintf(stderr, "Underrun.\n");
219 }
220
221 static void stream_overflow_callback(pa_stream *s, void *userdata) {
222 assert(s);
223
224 fprintf(stderr, "Overrun.\n");
225 }
226
227 static void stream_moved_callback(pa_stream *s, void *userdata) {
228 assert(s);
229
230 if (verbose)
231 fprintf(stderr, "Stream moved to device %s (%u, %ssuspended).\n", pa_stream_get_device_name(s), pa_stream_get_device_index(s), pa_stream_is_suspended(s) ? "" : "not ");
232 }
233
234 /* This is called whenever the context status changes */
235 static void context_state_callback(pa_context *c, void *userdata) {
236 assert(c);
237
238 switch (pa_context_get_state(c)) {
239 case PA_CONTEXT_CONNECTING:
240 case PA_CONTEXT_AUTHORIZING:
241 case PA_CONTEXT_SETTING_NAME:
242 break;
243
244 case PA_CONTEXT_READY: {
245 int r;
246 pa_buffer_attr buffer_attr;
247
248 assert(c);
249 assert(!stream);
250
251 if (verbose)
252 fprintf(stderr, "Connection established.\n");
253
254 if (!(stream = pa_stream_new(c, stream_name, &sample_spec, channel_map_set ? &channel_map : NULL))) {
255 fprintf(stderr, "pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(c)));
256 goto fail;
257 }
258
259 pa_stream_set_state_callback(stream, stream_state_callback, NULL);
260 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
261 pa_stream_set_read_callback(stream, stream_read_callback, NULL);
262 pa_stream_set_suspended_callback(stream, stream_suspended_callback, NULL);
263 pa_stream_set_moved_callback(stream, stream_moved_callback, NULL);
264 pa_stream_set_underflow_callback(stream, stream_underflow_callback, NULL);
265 pa_stream_set_overflow_callback(stream, stream_overflow_callback, NULL);
266
267 if (latency > 0) {
268 memset(&buffer_attr, 0, sizeof(buffer_attr));
269 buffer_attr.tlength = latency;
270 buffer_attr.minreq = process_time;
271 flags |= PA_STREAM_ADJUST_LATENCY;
272 }
273
274 if (mode == PLAYBACK) {
275 pa_cvolume cv;
276 if ((r = pa_stream_connect_playback(stream, device, latency > 0 ? &buffer_attr : NULL, flags, pa_cvolume_set(&cv, sample_spec.channels, volume), NULL)) < 0) {
277 fprintf(stderr, "pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(c)));
278 goto fail;
279 }
280
281 } else {
282 if ((r = pa_stream_connect_record(stream, device, latency > 0 ? &buffer_attr : NULL, flags)) < 0) {
283 fprintf(stderr, "pa_stream_connect_record() failed: %s\n", pa_strerror(pa_context_errno(c)));
284 goto fail;
285 }
286 }
287
288 break;
289 }
290
291 case PA_CONTEXT_TERMINATED:
292 quit(0);
293 break;
294
295 case PA_CONTEXT_FAILED:
296 default:
297 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
298 goto fail;
299 }
300
301 return;
302
303 fail:
304 quit(1);
305
306 }
307
308 /* Connection draining complete */
309 static void context_drain_complete(pa_context*c, void *userdata) {
310 pa_context_disconnect(c);
311 }
312
313 /* Stream draining complete */
314 static void stream_drain_complete(pa_stream*s, int success, void *userdata) {
315 pa_operation *o;
316
317 if (!success) {
318 fprintf(stderr, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context)));
319 quit(1);
320 }
321
322 if (verbose)
323 fprintf(stderr, "Playback stream drained.\n");
324
325 pa_stream_disconnect(stream);
326 pa_stream_unref(stream);
327 stream = NULL;
328
329 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
330 pa_context_disconnect(context);
331 else {
332 if (verbose)
333 fprintf(stderr, "Draining connection to server.\n");
334 }
335 }
336
337 /* New data on STDIN **/
338 static void stdin_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
339 size_t l, w = 0;
340 ssize_t r;
341
342 assert(a == mainloop_api);
343 assert(e);
344 assert(stdio_event == e);
345
346 if (buffer) {
347 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
348 return;
349 }
350
351 if (!stream || pa_stream_get_state(stream) != PA_STREAM_READY || !(l = w = pa_stream_writable_size(stream)))
352 l = 4096;
353
354 buffer = pa_xmalloc(l);
355
356 if ((r = read(fd, buffer, l)) <= 0) {
357 if (r == 0) {
358 if (verbose)
359 fprintf(stderr, "Got EOF.\n");
360
361 if (stream) {
362 pa_operation *o;
363
364 if (!(o = pa_stream_drain(stream, stream_drain_complete, NULL))) {
365 fprintf(stderr, "pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(context)));
366 quit(1);
367 return;
368 }
369
370 pa_operation_unref(o);
371 } else
372 quit(0);
373
374 } else {
375 fprintf(stderr, "read() failed: %s\n", strerror(errno));
376 quit(1);
377 }
378
379 mainloop_api->io_free(stdio_event);
380 stdio_event = NULL;
381 return;
382 }
383
384 buffer_length = r;
385 buffer_index = 0;
386
387 if (w)
388 do_stream_write(w);
389 }
390
391 /* Some data may be written to STDOUT */
392 static void stdout_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
393 ssize_t r;
394
395 assert(a == mainloop_api);
396 assert(e);
397 assert(stdio_event == e);
398
399 if (!buffer) {
400 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
401 return;
402 }
403
404 assert(buffer_length);
405
406 if ((r = write(fd, (uint8_t*) buffer+buffer_index, buffer_length)) <= 0) {
407 fprintf(stderr, "write() failed: %s\n", strerror(errno));
408 quit(1);
409
410 mainloop_api->io_free(stdio_event);
411 stdio_event = NULL;
412 return;
413 }
414
415 buffer_length -= r;
416 buffer_index += r;
417
418 if (!buffer_length) {
419 pa_xfree(buffer);
420 buffer = NULL;
421 buffer_length = buffer_index = 0;
422 }
423 }
424
425 /* UNIX signal to quit recieved */
426 static void exit_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
427 if (verbose)
428 fprintf(stderr, "Got signal, exiting.\n");
429 quit(0);
430 }
431
432 /* Show the current latency */
433 static void stream_update_timing_callback(pa_stream *s, int success, void *userdata) {
434 pa_usec_t l, usec;
435 int negative = 0;
436
437 assert(s);
438
439 if (!success ||
440 pa_stream_get_time(s, &usec) < 0 ||
441 pa_stream_get_latency(s, &l, &negative) < 0) {
442 fprintf(stderr, "Failed to get latency: %s\n", pa_strerror(pa_context_errno(context)));
443 quit(1);
444 return;
445 }
446
447 fprintf(stderr, "Time: %0.3f sec; Latency: %0.0f usec. \r",
448 (float) usec / 1000000,
449 (float) l * (negative?-1:1));
450 }
451
452 /* Someone requested that the latency is shown */
453 static void sigusr1_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
454
455 if (!stream)
456 return;
457
458 pa_operation_unref(pa_stream_update_timing_info(stream, stream_update_timing_callback, NULL));
459 }
460
461 static void time_event_callback(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
462 struct timeval next;
463
464 if (stream && pa_stream_get_state(stream) == PA_STREAM_READY) {
465 pa_operation *o;
466 if (!(o = pa_stream_update_timing_info(stream, stream_update_timing_callback, NULL)))
467 fprintf(stderr, "pa_stream_update_timing_info() failed: %s\n", pa_strerror(pa_context_errno(context)));
468 else
469 pa_operation_unref(o);
470 }
471
472 pa_gettimeofday(&next);
473 pa_timeval_add(&next, TIME_EVENT_USEC);
474
475 m->time_restart(e, &next);
476 }
477
478 static void help(const char *argv0) {
479
480 printf("%s [options]\n\n"
481 " -h, --help Show this help\n"
482 " --version Show version\n\n"
483 " -r, --record Create a connection for recording\n"
484 " -p, --playback Create a connection for playback\n\n"
485 " -v, --verbose Enable verbose operations\n\n"
486 " -s, --server=SERVER The name of the server to connect to\n"
487 " -d, --device=DEVICE The name of the sink/source to connect to\n"
488 " -n, --client-name=NAME How to call this client on the server\n"
489 " --stream-name=NAME How to call this stream on the server\n"
490 " --volume=VOLUME Specify the initial (linear) volume in range 0...65536\n"
491 " --rate=SAMPLERATE The sample rate in Hz (defaults to 44100)\n"
492 " --format=SAMPLEFORMAT The sample type, one of s16le, s16be, u8, float32le,\n"
493 " float32be, ulaw, alaw (defaults to s16ne)\n"
494 " --channels=CHANNELS The number of channels, 1 for mono, 2 for stereo\n"
495 " (defaults to 2)\n"
496 " --channel-map=CHANNELMAP Channel map to use instead of the default\n"
497 " --fix-format Take the sample format from the sink the stream is\n"
498 " being connected to.\n"
499 " --fix-rate Take the sampling rate from the sink the stream is\n"
500 " being connected to.\n"
501 " --fix-channels Take the number of channels and the channel map\n"
502 " from the sink the stream is being connected to.\n"
503 " --no-remix Don't upmix or downmix channels.\n"
504 " --no-remap Map channels by index instead of name.\n"
505 " --latency=BYTES Request the specified latency in bytes.\n"
506 " --process-time=BYTES Request the specified process time per request in bytes.\n"
507 ,
508 argv0);
509 }
510
511 enum {
512 ARG_VERSION = 256,
513 ARG_STREAM_NAME,
514 ARG_VOLUME,
515 ARG_SAMPLERATE,
516 ARG_SAMPLEFORMAT,
517 ARG_CHANNELS,
518 ARG_CHANNELMAP,
519 ARG_FIX_FORMAT,
520 ARG_FIX_RATE,
521 ARG_FIX_CHANNELS,
522 ARG_NO_REMAP,
523 ARG_NO_REMIX,
524 ARG_LATENCY,
525 ARG_PROCESS_TIME
526 };
527
528 int main(int argc, char *argv[]) {
529 pa_mainloop* m = NULL;
530 int ret = 1, r, c;
531 char *bn, *server = NULL;
532 pa_time_event *time_event = NULL;
533
534 static const struct option long_options[] = {
535 {"record", 0, NULL, 'r'},
536 {"playback", 0, NULL, 'p'},
537 {"device", 1, NULL, 'd'},
538 {"server", 1, NULL, 's'},
539 {"client-name", 1, NULL, 'n'},
540 {"stream-name", 1, NULL, ARG_STREAM_NAME},
541 {"version", 0, NULL, ARG_VERSION},
542 {"help", 0, NULL, 'h'},
543 {"verbose", 0, NULL, 'v'},
544 {"volume", 1, NULL, ARG_VOLUME},
545 {"rate", 1, NULL, ARG_SAMPLERATE},
546 {"format", 1, NULL, ARG_SAMPLEFORMAT},
547 {"channels", 1, NULL, ARG_CHANNELS},
548 {"channel-map", 1, NULL, ARG_CHANNELMAP},
549 {"fix-format", 0, NULL, ARG_FIX_FORMAT},
550 {"fix-rate", 0, NULL, ARG_FIX_RATE},
551 {"fix-channels", 0, NULL, ARG_FIX_CHANNELS},
552 {"no-remap", 0, NULL, ARG_NO_REMAP},
553 {"no-remix", 0, NULL, ARG_NO_REMIX},
554 {"latency", 1, NULL, ARG_LATENCY},
555 {"process-time", 1, NULL, ARG_PROCESS_TIME},
556 {NULL, 0, NULL, 0}
557 };
558
559 if (!(bn = strrchr(argv[0], '/')))
560 bn = argv[0];
561 else
562 bn++;
563
564 if (strstr(bn, "rec") || strstr(bn, "mon"))
565 mode = RECORD;
566 else if (strstr(bn, "cat") || strstr(bn, "play"))
567 mode = PLAYBACK;
568
569 while ((c = getopt_long(argc, argv, "rpd:s:n:hv", long_options, NULL)) != -1) {
570
571 switch (c) {
572 case 'h' :
573 help(bn);
574 ret = 0;
575 goto quit;
576
577 case ARG_VERSION:
578 printf("pacat "PACKAGE_VERSION"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
579 ret = 0;
580 goto quit;
581
582 case 'r':
583 mode = RECORD;
584 break;
585
586 case 'p':
587 mode = PLAYBACK;
588 break;
589
590 case 'd':
591 pa_xfree(device);
592 device = pa_xstrdup(optarg);
593 break;
594
595 case 's':
596 pa_xfree(server);
597 server = pa_xstrdup(optarg);
598 break;
599
600 case 'n':
601 pa_xfree(client_name);
602 client_name = pa_xstrdup(optarg);
603 break;
604
605 case ARG_STREAM_NAME:
606 pa_xfree(stream_name);
607 stream_name = pa_xstrdup(optarg);
608 break;
609
610 case 'v':
611 verbose = 1;
612 break;
613
614 case ARG_VOLUME: {
615 int v = atoi(optarg);
616 volume = v < 0 ? 0 : v;
617 break;
618 }
619
620 case ARG_CHANNELS:
621 sample_spec.channels = atoi(optarg);
622 break;
623
624 case ARG_SAMPLEFORMAT:
625 sample_spec.format = pa_parse_sample_format(optarg);
626 break;
627
628 case ARG_SAMPLERATE:
629 sample_spec.rate = atoi(optarg);
630 break;
631
632 case ARG_CHANNELMAP:
633 if (!pa_channel_map_parse(&channel_map, optarg)) {
634 fprintf(stderr, "Invalid channel map '%s'\n", optarg);
635 goto quit;
636 }
637
638 channel_map_set = 1;
639 break;
640
641 case ARG_FIX_CHANNELS:
642 flags |= PA_STREAM_FIX_CHANNELS;
643 break;
644
645 case ARG_FIX_RATE:
646 flags |= PA_STREAM_FIX_RATE;
647 break;
648
649 case ARG_FIX_FORMAT:
650 flags |= PA_STREAM_FIX_FORMAT;
651 break;
652
653 case ARG_NO_REMIX:
654 flags |= PA_STREAM_NO_REMIX_CHANNELS;
655 break;
656
657 case ARG_NO_REMAP:
658 flags |= PA_STREAM_NO_REMAP_CHANNELS;
659 break;
660
661 case ARG_LATENCY:
662 if (((latency = atoi(optarg))) <= 0) {
663 fprintf(stderr, "Invalid latency specification '%s'\n", optarg);
664 goto quit;
665 }
666 break;
667
668 case ARG_PROCESS_TIME:
669 if (((process_time = atoi(optarg))) <= 0) {
670 fprintf(stderr, "Invalid process time specification '%s'\n", optarg);
671 goto quit;
672 }
673 break;
674
675 default:
676 goto quit;
677 }
678 }
679
680 if (!pa_sample_spec_valid(&sample_spec)) {
681 fprintf(stderr, "Invalid sample specification\n");
682 goto quit;
683 }
684
685 if (channel_map_set && channel_map.channels != sample_spec.channels) {
686 fprintf(stderr, "Channel map doesn't match sample specification\n");
687 goto quit;
688 }
689
690 if (verbose) {
691 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
692 pa_sample_spec_snprint(t, sizeof(t), &sample_spec);
693 fprintf(stderr, "Opening a %s stream with sample specification '%s'.\n", mode == RECORD ? "recording" : "playback", t);
694 }
695
696 if (!(optind >= argc)) {
697 if (optind+1 == argc) {
698 int fd;
699
700 if ((fd = open(argv[optind], mode == PLAYBACK ? O_RDONLY : O_WRONLY|O_TRUNC|O_CREAT, 0666)) < 0) {
701 fprintf(stderr, "open(): %s\n", strerror(errno));
702 goto quit;
703 }
704
705 if (dup2(fd, mode == PLAYBACK ? 0 : 1) < 0) {
706 fprintf(stderr, "dup2(): %s\n", strerror(errno));
707 goto quit;
708 }
709
710 close(fd);
711
712 if (!stream_name)
713 stream_name = pa_xstrdup(argv[optind]);
714
715 } else {
716 fprintf(stderr, "Too many arguments.\n");
717 goto quit;
718 }
719 }
720
721 if (!client_name)
722 client_name = pa_xstrdup(bn);
723
724 if (!stream_name)
725 stream_name = pa_xstrdup(client_name);
726
727 /* Set up a new main loop */
728 if (!(m = pa_mainloop_new())) {
729 fprintf(stderr, "pa_mainloop_new() failed.\n");
730 goto quit;
731 }
732
733 mainloop_api = pa_mainloop_get_api(m);
734
735 r = pa_signal_init(mainloop_api);
736 assert(r == 0);
737 pa_signal_new(SIGINT, exit_signal_callback, NULL);
738 pa_signal_new(SIGTERM, exit_signal_callback, NULL);
739 #ifdef SIGUSR1
740 pa_signal_new(SIGUSR1, sigusr1_signal_callback, NULL);
741 #endif
742 #ifdef SIGPIPE
743 signal(SIGPIPE, SIG_IGN);
744 #endif
745
746 if (!(stdio_event = mainloop_api->io_new(mainloop_api,
747 mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO,
748 mode == PLAYBACK ? PA_IO_EVENT_INPUT : PA_IO_EVENT_OUTPUT,
749 mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) {
750 fprintf(stderr, "io_new() failed.\n");
751 goto quit;
752 }
753
754 /* Create a new connection context */
755 if (!(context = pa_context_new(mainloop_api, client_name))) {
756 fprintf(stderr, "pa_context_new() failed.\n");
757 goto quit;
758 }
759
760 pa_context_set_state_callback(context, context_state_callback, NULL);
761
762 /* Connect the context */
763 pa_context_connect(context, server, 0, NULL);
764
765 if (verbose) {
766 struct timeval tv;
767
768 pa_gettimeofday(&tv);
769 pa_timeval_add(&tv, TIME_EVENT_USEC);
770
771 if (!(time_event = mainloop_api->time_new(mainloop_api, &tv, time_event_callback, NULL))) {
772 fprintf(stderr, "time_new() failed.\n");
773 goto quit;
774 }
775 }
776
777 /* Run the main loop */
778 if (pa_mainloop_run(m, &ret) < 0) {
779 fprintf(stderr, "pa_mainloop_run() failed.\n");
780 goto quit;
781 }
782
783 quit:
784 if (stream)
785 pa_stream_unref(stream);
786
787 if (context)
788 pa_context_unref(context);
789
790 if (stdio_event) {
791 assert(mainloop_api);
792 mainloop_api->io_free(stdio_event);
793 }
794
795 if (time_event) {
796 assert(mainloop_api);
797 mainloop_api->time_free(time_event);
798 }
799
800 if (m) {
801 pa_signal_done();
802 pa_mainloop_free(m);
803 }
804
805 pa_xfree(buffer);
806
807 pa_xfree(server);
808 pa_xfree(device);
809 pa_xfree(client_name);
810 pa_xfree(stream_name);
811
812 return ret;
813 }