]> code.delx.au - pulseaudio/blob - src/utils/pactl.c
* show flags value when dumping sink/source info in pactl.
[pulseaudio] / src / utils / pactl.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <signal.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <limits.h>
34 #include <getopt.h>
35
36 #include <sndfile.h>
37
38 #include <polyp/polypaudio.h>
39 #include <polyp/mainloop.h>
40 #include <polyp/mainloop-signal.h>
41
42 #if PA_API_VERSION != 8
43 #error Invalid Polypaudio API version
44 #endif
45
46 #define BUFSIZE 1024
47
48 static pa_context *context = NULL;
49 static pa_mainloop_api *mainloop_api = NULL;
50
51 static char *device = NULL, *sample_name = NULL;
52
53 static SNDFILE *sndfile = NULL;
54 static pa_stream *sample_stream = NULL;
55 static pa_sample_spec sample_spec;
56 static size_t sample_length = 0;
57
58 static int actions = 1;
59
60 static int nl = 0;
61
62 static enum {
63 NONE,
64 EXIT,
65 STAT,
66 UPLOAD_SAMPLE,
67 PLAY_SAMPLE,
68 REMOVE_SAMPLE,
69 LIST
70 } action = NONE;
71
72 static void quit(int ret) {
73 assert(mainloop_api);
74 mainloop_api->quit(mainloop_api, ret);
75 }
76
77
78 static void context_drain_complete(pa_context *c, void *userdata) {
79 pa_context_disconnect(c);
80 }
81
82 static void drain(void) {
83 pa_operation *o;
84 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
85 pa_context_disconnect(context);
86 else
87 pa_operation_unref(o);
88 }
89
90
91 static void complete_action(void) {
92 assert(actions > 0);
93
94 if (!(--actions))
95 drain();
96 }
97
98 static void stat_callback(pa_context *c, const pa_stat_info *i, void *userdata) {
99 char s[128];
100 if (!i) {
101 fprintf(stderr, "Failed to get statistics: %s\n", pa_strerror(pa_context_errno(c)));
102 quit(1);
103 return;
104 }
105
106 pa_bytes_snprint(s, sizeof(s), i->memblock_total_size);
107 printf("Currently in use: %u blocks containing %s bytes total.\n", i->memblock_total, s);
108
109 pa_bytes_snprint(s, sizeof(s), i->memblock_allocated_size);
110 printf("Allocated during whole lifetime: %u blocks containing %s bytes total.\n", i->memblock_allocated, s);
111
112 pa_bytes_snprint(s, sizeof(s), i->scache_size);
113 printf("Sample cache size: %s\n", s);
114
115 complete_action();
116 }
117
118 static void get_server_info_callback(pa_context *c, const pa_server_info *i, void *useerdata) {
119 char s[PA_SAMPLE_SPEC_SNPRINT_MAX];
120
121 if (!i) {
122 fprintf(stderr, "Failed to get server information: %s\n", pa_strerror(pa_context_errno(c)));
123 quit(1);
124 return;
125 }
126
127 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec);
128
129 printf("User name: %s\n"
130 "Host Name: %s\n"
131 "Server Name: %s\n"
132 "Server Version: %s\n"
133 "Default Sample Specification: %s\n"
134 "Default Sink: %s\n"
135 "Default Source: %s\n"
136 "Cookie: %08x\n",
137 i->user_name,
138 i->host_name,
139 i->server_name,
140 i->server_version,
141 s,
142 i->default_sink_name,
143 i->default_source_name,
144 i->cookie);
145
146 complete_action();
147 }
148
149 static void get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
150 char s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
151
152 if (is_last < 0) {
153 fprintf(stderr, "Failed to get sink information: %s\n", pa_strerror(pa_context_errno(c)));
154 quit(1);
155 return;
156 }
157
158 if (is_last) {
159 complete_action();
160 return;
161 }
162
163 assert(i);
164
165 if (nl)
166 printf("\n");
167 nl = 1;
168
169 printf("*** Sink #%u ***\n"
170 "Name: %s\n"
171 "Driver: %s\n"
172 "Description: %s\n"
173 "Sample Specification: %s\n"
174 "Channel Map: %s\n"
175 "Owner Module: %u\n"
176 "Volume: %s\n"
177 "Monitor Source: %u\n"
178 "Latency: %0.0f usec\n"
179 "Flags: %s%s\n",
180 i->index,
181 i->name,
182 i->driver,
183 i->description,
184 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
185 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
186 i->owner_module,
187 i->mute ? "muted" : pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
188 i->monitor_source,
189 (double) i->latency,
190 i->flags & PA_SINK_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
191 i->flags & PA_SINK_LATENCY ? "LATENCY" : "");
192
193 }
194
195 static void get_source_info_callback(pa_context *c, const pa_source_info *i, int is_last, void *userdata) {
196 char s[PA_SAMPLE_SPEC_SNPRINT_MAX], t[32], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
197
198 if (is_last < 0) {
199 fprintf(stderr, "Failed to get source information: %s\n", pa_strerror(pa_context_errno(c)));
200 quit(1);
201 return;
202 }
203
204 if (is_last) {
205 complete_action();
206 return;
207 }
208
209 assert(i);
210
211 if (nl)
212 printf("\n");
213 nl = 1;
214
215 snprintf(t, sizeof(t), "%u", i->monitor_of_sink);
216
217 printf("*** Source #%u ***\n"
218 "Name: %s\n"
219 "Driver: %s\n"
220 "Description: %s\n"
221 "Sample Specification: %s\n"
222 "Channel Map: %s\n"
223 "Owner Module: %u\n"
224 "Volume: %s\n"
225 "Monitor of Sink: %s\n"
226 "Latency: %0.0f usec\n"
227 "Flags: %s%s\n",
228 i->index,
229 i->driver,
230 i->name,
231 i->description,
232 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
233 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
234 i->owner_module,
235 i->mute ? "muted" : pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
236 i->monitor_of_sink != PA_INVALID_INDEX ? t : "no",
237 (double) i->latency,
238 i->flags & PA_SOURCE_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
239 i->flags & PA_SOURCE_LATENCY ? "LATENCY" : "");
240
241 }
242
243 static void get_module_info_callback(pa_context *c, const pa_module_info *i, int is_last, void *userdata) {
244 char t[32];
245
246 if (is_last < 0) {
247 fprintf(stderr, "Failed to get module information: %s\n", pa_strerror(pa_context_errno(c)));
248 quit(1);
249 return;
250 }
251
252 if (is_last) {
253 complete_action();
254 return;
255 }
256
257 assert(i);
258
259 if (nl)
260 printf("\n");
261 nl = 1;
262
263 snprintf(t, sizeof(t), "%u", i->n_used);
264
265 printf("*** Module #%u ***\n"
266 "Name: %s\n"
267 "Argument: %s\n"
268 "Usage counter: %s\n"
269 "Auto unload: %s\n",
270 i->index,
271 i->name,
272 i->argument,
273 i->n_used != PA_INVALID_INDEX ? t : "n/a",
274 i->auto_unload ? "yes" : "no");
275 }
276
277 static void get_client_info_callback(pa_context *c, const pa_client_info *i, int is_last, void *userdata) {
278 char t[32];
279
280 if (is_last < 0) {
281 fprintf(stderr, "Failed to get client information: %s\n", pa_strerror(pa_context_errno(c)));
282 quit(1);
283 return;
284 }
285
286 if (is_last) {
287 complete_action();
288 return;
289 }
290
291 assert(i);
292
293 if (nl)
294 printf("\n");
295 nl = 1;
296
297 snprintf(t, sizeof(t), "%u", i->owner_module);
298
299 printf("*** Client #%u ***\n"
300 "Name: %s\n"
301 "Driver: %s\n"
302 "Owner Module: %s\n",
303 i->index,
304 i->name,
305 i->driver,
306 i->owner_module != PA_INVALID_INDEX ? t : "n/a");
307 }
308
309 static void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
310 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
311
312 if (is_last < 0) {
313 fprintf(stderr, "Failed to get sink input information: %s\n", pa_strerror(pa_context_errno(c)));
314 quit(1);
315 return;
316 }
317
318 if (is_last) {
319 complete_action();
320 return;
321 }
322
323 assert(i);
324
325 if (nl)
326 printf("\n");
327 nl = 1;
328
329 snprintf(t, sizeof(t), "%u", i->owner_module);
330 snprintf(k, sizeof(k), "%u", i->client);
331
332 printf("*** Sink Input #%u ***\n"
333 "Name: %s\n"
334 "Driver: %s\n"
335 "Owner Module: %s\n"
336 "Client: %s\n"
337 "Sink: %u\n"
338 "Sample Specification: %s\n"
339 "Channel Map: %s\n"
340 "Volume: %s\n"
341 "Buffer Latency: %0.0f usec\n"
342 "Sink Latency: %0.0f usec\n"
343 "Resample method: %s\n",
344 i->index,
345 i->name,
346 i->driver,
347 i->owner_module != PA_INVALID_INDEX ? t : "n/a",
348 i->client != PA_INVALID_INDEX ? k : "n/a",
349 i->sink,
350 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
351 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
352 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
353 (double) i->buffer_usec,
354 (double) i->sink_usec,
355 i->resample_method ? i->resample_method : "n/a");
356 }
357
358
359 static void get_source_output_info_callback(pa_context *c, const pa_source_output_info *i, int is_last, void *userdata) {
360 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
361
362 if (is_last < 0) {
363 fprintf(stderr, "Failed to get source output information: %s\n", pa_strerror(pa_context_errno(c)));
364 quit(1);
365 return;
366 }
367
368 if (is_last) {
369 complete_action();
370 return;
371 }
372
373 assert(i);
374
375 if (nl)
376 printf("\n");
377 nl = 1;
378
379
380 snprintf(t, sizeof(t), "%u", i->owner_module);
381 snprintf(k, sizeof(k), "%u", i->client);
382
383 printf("*** Source Output #%u ***\n"
384 "Name: %s\n"
385 "Driver: %s\n"
386 "Owner Module: %s\n"
387 "Client: %s\n"
388 "Source: %u\n"
389 "Sample Specification: %s\n"
390 "Channel Map: %s\n"
391 "Buffer Latency: %0.0f usec\n"
392 "Source Latency: %0.0f usec\n"
393 "Resample method: %s\n",
394 i->index,
395 i->name,
396 i->driver,
397 i->owner_module != PA_INVALID_INDEX ? t : "n/a",
398 i->client != PA_INVALID_INDEX ? k : "n/a",
399 i->source,
400 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
401 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
402 (double) i->buffer_usec,
403 (double) i->source_usec,
404 i->resample_method ? i->resample_method : "n/a");
405 }
406
407 static void get_sample_info_callback(pa_context *c, const pa_sample_info *i, int is_last, void *userdata) {
408 char t[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
409
410 if (is_last < 0) {
411 fprintf(stderr, "Failed to get sample information: %s\n", pa_strerror(pa_context_errno(c)));
412 quit(1);
413 return;
414 }
415
416 if (is_last) {
417 complete_action();
418 return;
419 }
420
421 assert(i);
422
423 if (nl)
424 printf("\n");
425 nl = 1;
426
427
428 pa_bytes_snprint(t, sizeof(t), i->bytes);
429
430 printf("*** Sample #%u ***\n"
431 "Name: %s\n"
432 "Volume: %s\n"
433 "Sample Specification: %s\n"
434 "Channel Map: %s\n"
435 "Duration: %0.1fs\n"
436 "Size: %s\n"
437 "Lazy: %s\n"
438 "Filename: %s\n",
439 i->index,
440 i->name,
441 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
442 pa_sample_spec_valid(&i->sample_spec) ? pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec) : "n/a",
443 pa_sample_spec_valid(&i->sample_spec) ? pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map) : "n/a",
444 (double) i->duration/1000000,
445 t,
446 i->lazy ? "yes" : "no",
447 i->filename ? i->filename : "n/a");
448 }
449
450 static void get_autoload_info_callback(pa_context *c, const pa_autoload_info *i, int is_last, void *userdata) {
451 if (is_last < 0) {
452 fprintf(stderr, "Failed to get autoload information: %s\n", pa_strerror(pa_context_errno(c)));
453 quit(1);
454 return;
455 }
456
457 if (is_last) {
458 complete_action();
459 return;
460 }
461
462 assert(i);
463
464 if (nl)
465 printf("\n");
466 nl = 1;
467
468 printf("*** Autoload Entry #%u ***\n"
469 "Name: %s\n"
470 "Type: %s\n"
471 "Module: %s\n"
472 "Argument: %s\n",
473 i->index,
474 i->name,
475 i->type == PA_AUTOLOAD_SINK ? "sink" : "source",
476 i->module,
477 i->argument);
478 }
479
480 static void simple_callback(pa_context *c, int success, void *userdata) {
481 if (!success) {
482 fprintf(stderr, "Failure: %s\n", pa_strerror(pa_context_errno(c)));
483 quit(1);
484 return;
485 }
486
487 complete_action();
488 }
489
490 static void stream_state_callback(pa_stream *s, void *userdata) {
491 assert(s);
492
493 switch (pa_stream_get_state(s)) {
494 case PA_STREAM_CREATING:
495 case PA_STREAM_READY:
496 break;
497
498 case PA_STREAM_TERMINATED:
499 drain();
500 break;
501
502 case PA_STREAM_FAILED:
503 default:
504 fprintf(stderr, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
505 quit(1);
506 }
507 }
508
509 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
510 sf_count_t l;
511 float *d;
512 assert(s && length && sndfile);
513
514 d = malloc(length);
515 assert(d);
516
517 assert(sample_length >= length);
518 l = length/pa_frame_size(&sample_spec);
519
520 if ((sf_readf_float(sndfile, d, l)) != l) {
521 free(d);
522 fprintf(stderr, "Premature end of file\n");
523 quit(1);
524 }
525
526 pa_stream_write(s, d, length, free, 0, PA_SEEK_RELATIVE);
527
528 sample_length -= length;
529
530 if (sample_length <= 0) {
531 pa_stream_set_write_callback(sample_stream, NULL, NULL);
532 pa_stream_finish_upload(sample_stream);
533 }
534 }
535
536 static void context_state_callback(pa_context *c, void *userdata) {
537 assert(c);
538 switch (pa_context_get_state(c)) {
539 case PA_CONTEXT_CONNECTING:
540 case PA_CONTEXT_AUTHORIZING:
541 case PA_CONTEXT_SETTING_NAME:
542 break;
543
544 case PA_CONTEXT_READY:
545 switch (action) {
546 case STAT:
547 actions = 2;
548 pa_operation_unref(pa_context_stat(c, stat_callback, NULL));
549 pa_operation_unref(pa_context_get_server_info(c, get_server_info_callback, NULL));
550 break;
551
552 case PLAY_SAMPLE:
553 pa_operation_unref(pa_context_play_sample(c, sample_name, device, PA_VOLUME_NORM, simple_callback, NULL));
554 break;
555
556 case REMOVE_SAMPLE:
557 pa_operation_unref(pa_context_remove_sample(c, sample_name, simple_callback, NULL));
558 break;
559
560 case UPLOAD_SAMPLE:
561 sample_stream = pa_stream_new(c, sample_name, &sample_spec, NULL);
562 assert(sample_stream);
563
564 pa_stream_set_state_callback(sample_stream, stream_state_callback, NULL);
565 pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL);
566 pa_stream_connect_upload(sample_stream, sample_length);
567 break;
568
569 case EXIT:
570 pa_operation_unref(pa_context_exit_daemon(c, NULL, NULL));
571 drain();
572
573 case LIST:
574 actions = 8;
575 pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL));
576 pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL));
577 pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL));
578 pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL));
579 pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL));
580 pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL));
581 pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL));
582 pa_operation_unref(pa_context_get_autoload_info_list(c, get_autoload_info_callback, NULL));
583 break;
584
585 default:
586 assert(0);
587 }
588 break;
589
590 case PA_CONTEXT_TERMINATED:
591 quit(0);
592 break;
593
594 case PA_CONTEXT_FAILED:
595 default:
596 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
597 quit(1);
598 }
599 }
600
601 static void exit_signal_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
602 fprintf(stderr, "Got SIGINT, exiting.\n");
603 quit(0);
604 }
605
606 static void help(const char *argv0) {
607
608 printf("%s [options] stat\n"
609 "%s [options] list\n"
610 "%s [options] exit\n"
611 "%s [options] upload-sample FILENAME [NAME]\n"
612 "%s [options] play-sample NAME [SINK]\n"
613 "%s [options] remove-sample NAME\n\n"
614 " -h, --help Show this help\n"
615 " --version Show version\n\n"
616 " -s, --server=SERVER The name of the server to connect to\n"
617 " -n, --client-name=NAME How to call this client on the server\n",
618 argv0, argv0, argv0, argv0, argv0, argv0);
619 }
620
621 enum { ARG_VERSION = 256 };
622
623 int main(int argc, char *argv[]) {
624 pa_mainloop* m = NULL;
625 char tmp[PATH_MAX];
626 int ret = 1, r, c;
627 char *server = NULL, *client_name = NULL, *bn;
628
629 static const struct option long_options[] = {
630 {"server", 1, NULL, 's'},
631 {"client-name", 1, NULL, 'n'},
632 {"version", 0, NULL, ARG_VERSION},
633 {"help", 0, NULL, 'h'},
634 {NULL, 0, NULL, 0}
635 };
636
637 if (!(bn = strrchr(argv[0], '/')))
638 bn = argv[0];
639 else
640 bn++;
641
642 while ((c = getopt_long(argc, argv, "s:n:h", long_options, NULL)) != -1) {
643 switch (c) {
644 case 'h' :
645 help(bn);
646 ret = 0;
647 goto quit;
648
649 case ARG_VERSION:
650 printf("pactl "PACKAGE_VERSION"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
651 ret = 0;
652 goto quit;
653
654 case 's':
655 free(server);
656 server = strdup(optarg);
657 break;
658
659 case 'n':
660 free(client_name);
661 client_name = strdup(optarg);
662 break;
663
664 default:
665 goto quit;
666 }
667 }
668
669 if (!client_name)
670 client_name = strdup(bn);
671
672 if (optind < argc) {
673 if (!strcmp(argv[optind], "stat"))
674 action = STAT;
675 else if (!strcmp(argv[optind], "exit"))
676 action = EXIT;
677 else if (!strcmp(argv[optind], "list"))
678 action = LIST;
679 else if (!strcmp(argv[optind], "upload-sample")) {
680 struct SF_INFO sfinfo;
681 action = UPLOAD_SAMPLE;
682
683 if (optind+1 >= argc) {
684 fprintf(stderr, "Please specify a sample file to load\n");
685 goto quit;
686 }
687
688 if (optind+2 < argc)
689 sample_name = strdup(argv[optind+2]);
690 else {
691 char *f = strrchr(argv[optind+1], '/');
692 size_t n;
693 if (f)
694 f++;
695 else
696 f = argv[optind];
697
698 n = strcspn(f, ".");
699 strncpy(tmp, f, n);
700 tmp[n] = 0;
701 sample_name = strdup(tmp);
702 }
703
704 memset(&sfinfo, 0, sizeof(sfinfo));
705 if (!(sndfile = sf_open(argv[optind+1], SFM_READ, &sfinfo))) {
706 fprintf(stderr, "Failed to open sound file.\n");
707 goto quit;
708 }
709
710 sample_spec.format = PA_SAMPLE_FLOAT32;
711 sample_spec.rate = sfinfo.samplerate;
712 sample_spec.channels = sfinfo.channels;
713
714 sample_length = sfinfo.frames*pa_frame_size(&sample_spec);
715 } else if (!strcmp(argv[optind], "play-sample")) {
716 action = PLAY_SAMPLE;
717 if (optind+1 >= argc) {
718 fprintf(stderr, "You have to specify a sample name to play\n");
719 goto quit;
720 }
721
722 sample_name = strdup(argv[optind+1]);
723
724 if (optind+2 < argc)
725 device = strdup(argv[optind+2]);
726
727 } else if (!strcmp(argv[optind], "remove-sample")) {
728 action = REMOVE_SAMPLE;
729 if (optind+1 >= argc) {
730 fprintf(stderr, "You have to specify a sample name to remove\n");
731 goto quit;
732 }
733
734 sample_name = strdup(argv[optind+1]);
735 }
736 }
737
738 if (action == NONE) {
739 fprintf(stderr, "No valid command specified.\n");
740 goto quit;
741 }
742
743 if (!(m = pa_mainloop_new())) {
744 fprintf(stderr, "pa_mainloop_new() failed.\n");
745 goto quit;
746 }
747
748 mainloop_api = pa_mainloop_get_api(m);
749
750 r = pa_signal_init(mainloop_api);
751 assert(r == 0);
752 pa_signal_new(SIGINT, exit_signal_callback, NULL);
753 #ifdef SIGPIPE
754 signal(SIGPIPE, SIG_IGN);
755 #endif
756
757 if (!(context = pa_context_new(mainloop_api, client_name))) {
758 fprintf(stderr, "pa_context_new() failed.\n");
759 goto quit;
760 }
761
762 pa_context_set_state_callback(context, context_state_callback, NULL);
763 pa_context_connect(context, server, 0, NULL);
764
765 if (pa_mainloop_run(m, &ret) < 0) {
766 fprintf(stderr, "pa_mainloop_run() failed.\n");
767 goto quit;
768 }
769
770 quit:
771 if (sample_stream)
772 pa_stream_unref(sample_stream);
773
774 if (context)
775 pa_context_unref(context);
776
777 if (m) {
778 pa_signal_done();
779 pa_mainloop_free(m);
780 }
781
782 if (sndfile)
783 sf_close(sndfile);
784
785 free(server);
786 free(device);
787 free(sample_name);
788
789 return ret;
790 }