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