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