4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
40 #include <pulse/pulseaudio.h>
41 #include <pulsecore/core-util.h>
43 #if PA_API_VERSION < 10
44 #error Invalid PulseAudio API version
49 static pa_context *context = NULL;
50 static pa_mainloop_api *mainloop_api = NULL;
52 static char *device = NULL, *sample_name = NULL, *sink_name = NULL, *source_name = NULL, *module_name = NULL, *module_args = NULL;
53 static uint32_t sink_input_idx = PA_INVALID_INDEX, source_output_idx = PA_INVALID_INDEX;
54 static uint32_t module_index;
57 static SNDFILE *sndfile = NULL;
58 static pa_stream *sample_stream = NULL;
59 static pa_sample_spec sample_spec;
60 static size_t sample_length = 0;
62 static int actions = 1;
82 static void quit(int ret) {
84 mainloop_api->quit(mainloop_api, ret);
88 static void context_drain_complete(pa_context *c, void *userdata) {
89 pa_context_disconnect(c);
92 static void drain(void) {
94 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
95 pa_context_disconnect(context);
97 pa_operation_unref(o);
101 static void complete_action(void) {
108 static void stat_callback(pa_context *c, const pa_stat_info *i, void *userdata) {
111 fprintf(stderr, "Failed to get statistics: %s\n", pa_strerror(pa_context_errno(c)));
116 pa_bytes_snprint(s, sizeof(s), i->memblock_total_size);
117 printf("Currently in use: %u blocks containing %s bytes total.\n", i->memblock_total, s);
119 pa_bytes_snprint(s, sizeof(s), i->memblock_allocated_size);
120 printf("Allocated during whole lifetime: %u blocks containing %s bytes total.\n", i->memblock_allocated, s);
122 pa_bytes_snprint(s, sizeof(s), i->scache_size);
123 printf("Sample cache size: %s\n", s);
128 static void get_server_info_callback(pa_context *c, const pa_server_info *i, void *useerdata) {
129 char s[PA_SAMPLE_SPEC_SNPRINT_MAX];
132 fprintf(stderr, "Failed to get server information: %s\n", pa_strerror(pa_context_errno(c)));
137 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec);
139 printf("User name: %s\n"
142 "Server Version: %s\n"
143 "Default Sample Specification: %s\n"
145 "Default Source: %s\n"
152 i->default_sink_name,
153 i->default_source_name,
159 static void get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
160 char s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
164 fprintf(stderr, "Failed to get sink information: %s\n", pa_strerror(pa_context_errno(c)));
180 printf("*** Sink #%u ***\n"
183 "Sample Specification: %s\n"
187 "Monitor Source: %s\n"
188 "Latency: %0.0f usec, configured %0.0f usec\n"
189 "Flags: %s%s%s%s%s%s\n"
193 pa_strnull(i->driver),
194 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
195 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
197 i->mute ? "muted" : pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
198 pa_strnull(i->monitor_source_name),
199 (double) i->latency, (double) i->configured_latency,
200 i->flags & PA_SINK_HARDWARE ? "HARDWARE " : "",
201 i->flags & PA_SINK_NETWORK ? "NETWORK " : "",
202 i->flags & PA_SINK_HW_MUTE_CTRL ? "HW_MUTE_CTRL " : "",
203 i->flags & PA_SINK_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
204 i->flags & PA_SINK_DECIBEL_VOLUME ? "DECIBEL_VOLUME " : "",
205 i->flags & PA_SINK_LATENCY ? "LATENCY " : "",
206 pl = pa_proplist_to_string(i->proplist));
211 static void get_source_info_callback(pa_context *c, const pa_source_info *i, int is_last, void *userdata) {
212 char s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
216 fprintf(stderr, "Failed to get source information: %s\n", pa_strerror(pa_context_errno(c)));
232 printf("*** Source #%u ***\n"
235 "Sample Specification: %s\n"
239 "Monitor of Sink: %s\n"
240 "Latency: %0.0f usec, configured %0.0f usec\n"
241 "Flags: %s%s%s%s%s%s\n"
245 pa_strnull(i->driver),
246 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
247 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
249 i->mute ? "muted" : pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
250 i->monitor_of_sink_name ? i->monitor_of_sink_name : "n/a",
251 (double) i->latency, (double) i->configured_latency,
252 i->flags & PA_SOURCE_HARDWARE ? "HARDWARE " : "",
253 i->flags & PA_SOURCE_NETWORK ? "NETWORK " : "",
254 i->flags & PA_SOURCE_HW_MUTE_CTRL ? "HW_MUTE_CTRL " : "",
255 i->flags & PA_SOURCE_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
256 i->flags & PA_SOURCE_DECIBEL_VOLUME ? "DECIBEL_VOLUME " : "",
257 i->flags & PA_SOURCE_LATENCY ? "LATENCY " : "",
258 pl = pa_proplist_to_string(i->proplist));
263 static void get_module_info_callback(pa_context *c, const pa_module_info *i, int is_last, void *userdata) {
267 fprintf(stderr, "Failed to get module information: %s\n", pa_strerror(pa_context_errno(c)));
283 snprintf(t, sizeof(t), "%u", i->n_used);
285 printf("*** Module #%u ***\n"
288 "Usage counter: %s\n"
292 i->argument ? i->argument : "",
293 i->n_used != PA_INVALID_INDEX ? t : "n/a",
294 pa_yes_no(i->auto_unload));
297 static void get_client_info_callback(pa_context *c, const pa_client_info *i, int is_last, void *userdata) {
302 fprintf(stderr, "Failed to get client information: %s\n", pa_strerror(pa_context_errno(c)));
318 snprintf(t, sizeof(t), "%u", i->owner_module);
320 printf("*** Client #%u ***\n"
325 pa_strnull(i->driver),
326 i->owner_module != PA_INVALID_INDEX ? t : "n/a",
327 pl = pa_proplist_to_string(i->proplist));
332 static void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
333 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
337 fprintf(stderr, "Failed to get sink input information: %s\n", pa_strerror(pa_context_errno(c)));
353 snprintf(t, sizeof(t), "%u", i->owner_module);
354 snprintf(k, sizeof(k), "%u", i->client);
356 printf("*** Sink Input #%u ***\n"
361 "Sample Specification: %s\n"
364 "Buffer Latency: %0.0f usec\n"
365 "Sink Latency: %0.0f usec\n"
366 "Resample method: %s\n"
369 pa_strnull(i->driver),
370 i->owner_module != PA_INVALID_INDEX ? t : "n/a",
371 i->client != PA_INVALID_INDEX ? k : "n/a",
373 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
374 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
375 i->mute ? "muted" : pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
376 (double) i->buffer_usec,
377 (double) i->sink_usec,
378 i->resample_method ? i->resample_method : "n/a",
379 pl = pa_proplist_to_string(i->proplist));
384 static void get_source_output_info_callback(pa_context *c, const pa_source_output_info *i, int is_last, void *userdata) {
385 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
389 fprintf(stderr, "Failed to get source output information: %s\n", pa_strerror(pa_context_errno(c)));
406 snprintf(t, sizeof(t), "%u", i->owner_module);
407 snprintf(k, sizeof(k), "%u", i->client);
409 printf("*** Source Output #%u ***\n"
414 "Sample Specification: %s\n"
416 "Buffer Latency: %0.0f usec\n"
417 "Source Latency: %0.0f usec\n"
418 "Resample method: %s\n"
421 pa_strnull(i->driver),
422 i->owner_module != PA_INVALID_INDEX ? t : "n/a",
423 i->client != PA_INVALID_INDEX ? k : "n/a",
425 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
426 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
427 (double) i->buffer_usec,
428 (double) i->source_usec,
429 i->resample_method ? i->resample_method : "n/a",
430 pl = pa_proplist_to_string(i->proplist));
435 static void get_sample_info_callback(pa_context *c, const pa_sample_info *i, int is_last, void *userdata) {
436 char t[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
440 fprintf(stderr, "Failed to get sample information: %s\n", pa_strerror(pa_context_errno(c)));
457 pa_bytes_snprint(t, sizeof(t), i->bytes);
459 printf("*** Sample #%u ***\n"
462 "Sample Specification: %s\n"
471 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
472 pa_sample_spec_valid(&i->sample_spec) ? pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec) : "n/a",
473 pa_sample_spec_valid(&i->sample_spec) ? pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map) : "n/a",
474 (double) i->duration/1000000,
477 i->filename ? i->filename : "n/a",
478 pl = pa_proplist_to_string(i->proplist));
483 static void get_autoload_info_callback(pa_context *c, const pa_autoload_info *i, int is_last, void *userdata) {
485 fprintf(stderr, "Failed to get autoload information: %s\n", pa_strerror(pa_context_errno(c)));
501 printf("*** Autoload Entry #%u ***\n"
508 i->type == PA_AUTOLOAD_SINK ? "sink" : "source",
510 i->argument ? i->argument : "");
513 static void simple_callback(pa_context *c, int success, void *userdata) {
515 fprintf(stderr, "Failure: %s\n", pa_strerror(pa_context_errno(c)));
523 static void index_callback(pa_context *c, uint32_t idx, void *userdata) {
524 if (idx == PA_INVALID_INDEX) {
525 fprintf(stderr, "Failure: %s\n", pa_strerror(pa_context_errno(c)));
535 static void stream_state_callback(pa_stream *s, void *userdata) {
538 switch (pa_stream_get_state(s)) {
539 case PA_STREAM_CREATING:
540 case PA_STREAM_READY:
543 case PA_STREAM_TERMINATED:
547 case PA_STREAM_FAILED:
549 fprintf(stderr, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
554 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
557 assert(s && length && sndfile);
559 d = pa_xmalloc(length);
561 assert(sample_length >= length);
562 l = length/pa_frame_size(&sample_spec);
564 if ((sf_readf_float(sndfile, d, l)) != l) {
566 fprintf(stderr, "Premature end of file\n");
570 pa_stream_write(s, d, length, pa_xfree, 0, PA_SEEK_RELATIVE);
572 sample_length -= length;
574 if (sample_length <= 0) {
575 pa_stream_set_write_callback(sample_stream, NULL, NULL);
576 pa_stream_finish_upload(sample_stream);
580 static void context_state_callback(pa_context *c, void *userdata) {
582 switch (pa_context_get_state(c)) {
583 case PA_CONTEXT_CONNECTING:
584 case PA_CONTEXT_AUTHORIZING:
585 case PA_CONTEXT_SETTING_NAME:
588 case PA_CONTEXT_READY:
592 pa_operation_unref(pa_context_stat(c, stat_callback, NULL));
593 pa_operation_unref(pa_context_get_server_info(c, get_server_info_callback, NULL));
597 pa_operation_unref(pa_context_play_sample(c, sample_name, device, PA_VOLUME_NORM, simple_callback, NULL));
601 pa_operation_unref(pa_context_remove_sample(c, sample_name, simple_callback, NULL));
605 sample_stream = pa_stream_new(c, sample_name, &sample_spec, NULL);
606 assert(sample_stream);
608 pa_stream_set_state_callback(sample_stream, stream_state_callback, NULL);
609 pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL);
610 pa_stream_connect_upload(sample_stream, sample_length);
614 pa_operation_unref(pa_context_exit_daemon(c, NULL, NULL));
619 pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL));
620 pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL));
621 pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL));
622 pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL));
623 pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL));
624 pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL));
625 pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL));
626 pa_operation_unref(pa_context_get_autoload_info_list(c, get_autoload_info_callback, NULL));
629 case MOVE_SINK_INPUT:
630 pa_operation_unref(pa_context_move_sink_input_by_name(c, sink_input_idx, sink_name, simple_callback, NULL));
633 case MOVE_SOURCE_OUTPUT:
634 pa_operation_unref(pa_context_move_source_output_by_name(c, source_output_idx, source_name, simple_callback, NULL));
638 pa_operation_unref(pa_context_load_module(c, module_name, module_args, index_callback, NULL));
642 pa_operation_unref(pa_context_unload_module(c, module_index, simple_callback, NULL));
647 pa_operation_unref(pa_context_suspend_sink_by_name(c, sink_name, suspend, simple_callback, NULL));
649 pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL));
654 pa_operation_unref(pa_context_suspend_source_by_name(c, source_name, suspend, simple_callback, NULL));
656 pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL));
664 case PA_CONTEXT_TERMINATED:
668 case PA_CONTEXT_FAILED:
670 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
675 static void exit_signal_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
676 fprintf(stderr, "Got SIGINT, exiting.\n");
680 static void help(const char *argv0) {
682 printf("%s [options] stat\n"
683 "%s [options] list\n"
684 "%s [options] exit\n"
685 "%s [options] upload-sample FILENAME [NAME]\n"
686 "%s [options] play-sample NAME [SINK]\n"
687 "%s [options] remove-sample NAME\n"
688 "%s [options] move-sink-input ID SINK\n"
689 "%s [options] move-source-output ID SOURCE\n"
690 "%s [options] load-module NAME [ARGS ...]\n"
691 "%s [options] unload-module ID\n"
692 "%s [options] suspend-sink [SINK] 1|0\n"
693 "%s [options] suspend-source [SOURCE] 1|0\n\n"
694 " -h, --help Show this help\n"
695 " --version Show version\n\n"
696 " -s, --server=SERVER The name of the server to connect to\n"
697 " -n, --client-name=NAME How to call this client on the server\n",
698 argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0);
701 enum { ARG_VERSION = 256 };
703 int main(int argc, char *argv[]) {
704 pa_mainloop* m = NULL;
707 char *server = NULL, *client_name = NULL, *bn;
709 static const struct option long_options[] = {
710 {"server", 1, NULL, 's'},
711 {"client-name", 1, NULL, 'n'},
712 {"version", 0, NULL, ARG_VERSION},
713 {"help", 0, NULL, 'h'},
717 if (!(bn = strrchr(argv[0], '/')))
722 while ((c = getopt_long(argc, argv, "s:n:h", long_options, NULL)) != -1) {
730 printf("pactl "PACKAGE_VERSION"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
736 server = pa_xstrdup(optarg);
740 pa_xfree(client_name);
741 client_name = pa_xstrdup(optarg);
750 client_name = pa_xstrdup(bn);
753 if (!strcmp(argv[optind], "stat"))
755 else if (!strcmp(argv[optind], "exit"))
757 else if (!strcmp(argv[optind], "list"))
759 else if (!strcmp(argv[optind], "upload-sample")) {
760 struct SF_INFO sfinfo;
761 action = UPLOAD_SAMPLE;
763 if (optind+1 >= argc) {
764 fprintf(stderr, "Please specify a sample file to load\n");
769 sample_name = pa_xstrdup(argv[optind+2]);
771 char *f = strrchr(argv[optind+1], '/');
781 sample_name = pa_xstrdup(tmp);
784 memset(&sfinfo, 0, sizeof(sfinfo));
785 if (!(sndfile = sf_open(argv[optind+1], SFM_READ, &sfinfo))) {
786 fprintf(stderr, "Failed to open sound file.\n");
790 sample_spec.format = PA_SAMPLE_FLOAT32;
791 sample_spec.rate = sfinfo.samplerate;
792 sample_spec.channels = sfinfo.channels;
794 sample_length = sfinfo.frames*pa_frame_size(&sample_spec);
795 } else if (!strcmp(argv[optind], "play-sample")) {
796 action = PLAY_SAMPLE;
797 if (argc != optind+2 && argc != optind+3) {
798 fprintf(stderr, "You have to specify a sample name to play\n");
802 sample_name = pa_xstrdup(argv[optind+1]);
805 device = pa_xstrdup(argv[optind+2]);
807 } else if (!strcmp(argv[optind], "remove-sample")) {
808 action = REMOVE_SAMPLE;
809 if (argc != optind+2) {
810 fprintf(stderr, "You have to specify a sample name to remove\n");
814 sample_name = pa_xstrdup(argv[optind+1]);
815 } else if (!strcmp(argv[optind], "move-sink-input")) {
816 action = MOVE_SINK_INPUT;
817 if (argc != optind+3) {
818 fprintf(stderr, "You have to specify a sink input index and a sink\n");
822 sink_input_idx = atoi(argv[optind+1]);
823 sink_name = pa_xstrdup(argv[optind+2]);
824 } else if (!strcmp(argv[optind], "move-source-output")) {
825 action = MOVE_SOURCE_OUTPUT;
826 if (argc != optind+3) {
827 fprintf(stderr, "You have to specify a source output index and a source\n");
831 source_output_idx = atoi(argv[optind+1]);
832 source_name = pa_xstrdup(argv[optind+2]);
833 } else if (!strcmp(argv[optind], "load-module")) {
838 action = LOAD_MODULE;
840 if (argc <= optind+1) {
841 fprintf(stderr, "You have to specify a module name and arguments.\n");
845 module_name = argv[optind+1];
847 for (i = optind+2; i < argc; i++)
848 n += strlen(argv[i])+1;
851 p = module_args = pa_xnew0(char, n);
853 for (i = optind+2; i < argc; i++)
854 p += sprintf(p, "%s%s", p == module_args ? "" : " ", argv[i]);
857 } else if (!strcmp(argv[optind], "unload-module")) {
858 action = UNLOAD_MODULE;
860 if (argc != optind+2) {
861 fprintf(stderr, "You have to specify a module index\n");
865 module_index = atoi(argv[optind+1]);
867 } else if (!strcmp(argv[optind], "suspend-sink")) {
868 action = SUSPEND_SINK;
870 if (argc > optind+3 || optind+1 >= argc) {
871 fprintf(stderr, "You may not specify more than one sink. You have to specify at least one boolean value.\n");
875 suspend = pa_parse_boolean(argv[argc-1]);
878 sink_name = pa_xstrdup(argv[optind+1]);
880 } else if (!strcmp(argv[optind], "suspend-source")) {
881 action = SUSPEND_SOURCE;
883 if (argc > optind+3 || optind+1 >= argc) {
884 fprintf(stderr, "You may not specify more than one source. You have to specify at least one boolean value.\n");
888 suspend = pa_parse_boolean(argv[argc-1]);
891 source_name = pa_xstrdup(argv[optind+1]);
892 } else if (!strcmp(argv[optind], "help")) {
899 if (action == NONE) {
900 fprintf(stderr, "No valid command specified.\n");
904 if (!(m = pa_mainloop_new())) {
905 fprintf(stderr, "pa_mainloop_new() failed.\n");
909 mainloop_api = pa_mainloop_get_api(m);
911 r = pa_signal_init(mainloop_api);
913 pa_signal_new(SIGINT, exit_signal_callback, NULL);
915 signal(SIGPIPE, SIG_IGN);
918 if (!(context = pa_context_new(mainloop_api, client_name))) {
919 fprintf(stderr, "pa_context_new() failed.\n");
923 pa_context_set_state_callback(context, context_state_callback, NULL);
924 pa_context_connect(context, server, 0, NULL);
926 if (pa_mainloop_run(m, &ret) < 0) {
927 fprintf(stderr, "pa_mainloop_run() failed.\n");
933 pa_stream_unref(sample_stream);
936 pa_context_unref(context);
948 pa_xfree(sample_name);
950 pa_xfree(source_name);
951 pa_xfree(module_args);
952 pa_xfree(client_name);