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