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