4 This file is part of PulseAudio.
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.
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.
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
38 #include <pulse/pulseaudio.h>
40 #if PA_API_VERSION < 10
41 #error Invalid PulseAudio API version
46 static pa_context *context = NULL;
47 static pa_mainloop_api *mainloop_api = NULL;
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;
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;
57 static int actions = 1;
73 static void quit(int ret) {
75 mainloop_api->quit(mainloop_api, ret);
79 static void context_drain_complete(pa_context *c, void *userdata) {
80 pa_context_disconnect(c);
83 static void drain(void) {
85 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
86 pa_context_disconnect(context);
88 pa_operation_unref(o);
92 static void complete_action(void) {
99 static void stat_callback(pa_context *c, const pa_stat_info *i, void *userdata) {
102 fprintf(stderr, "Failed to get statistics: %s\n", pa_strerror(pa_context_errno(c)));
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);
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);
113 pa_bytes_snprint(s, sizeof(s), i->scache_size);
114 printf("Sample cache size: %s\n", s);
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];
123 fprintf(stderr, "Failed to get server information: %s\n", pa_strerror(pa_context_errno(c)));
128 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec);
130 printf("User name: %s\n"
133 "Server Version: %s\n"
134 "Default Sample Specification: %s\n"
136 "Default Source: %s\n"
143 i->default_sink_name,
144 i->default_source_name,
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];
154 fprintf(stderr, "Failed to get sink information: %s\n", pa_strerror(pa_context_errno(c)));
170 printf("*** Sink #%u ***\n"
174 "Sample Specification: %s\n"
178 "Monitor Source: %u\n"
179 "Latency: %0.0f usec\n"
185 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
186 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
188 i->mute ? "muted" : pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
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" : "");
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];
201 fprintf(stderr, "Failed to get source information: %s\n", pa_strerror(pa_context_errno(c)));
217 snprintf(t, sizeof(t), "%u", i->monitor_of_sink);
219 printf("*** Source #%u ***\n"
223 "Sample Specification: %s\n"
227 "Monitor of Sink: %s\n"
228 "Latency: %0.0f usec\n"
234 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
235 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
237 i->mute ? "muted" : pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
238 i->monitor_of_sink != PA_INVALID_INDEX ? t : "no",
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" : "");
246 static void get_module_info_callback(pa_context *c, const pa_module_info *i, int is_last, void *userdata) {
250 fprintf(stderr, "Failed to get module information: %s\n", pa_strerror(pa_context_errno(c)));
266 snprintf(t, sizeof(t), "%u", i->n_used);
268 printf("*** Module #%u ***\n"
271 "Usage counter: %s\n"
276 i->n_used != PA_INVALID_INDEX ? t : "n/a",
277 i->auto_unload ? "yes" : "no");
280 static void get_client_info_callback(pa_context *c, const pa_client_info *i, int is_last, void *userdata) {
284 fprintf(stderr, "Failed to get client information: %s\n", pa_strerror(pa_context_errno(c)));
300 snprintf(t, sizeof(t), "%u", i->owner_module);
302 printf("*** Client #%u ***\n"
305 "Owner Module: %s\n",
309 i->owner_module != PA_INVALID_INDEX ? t : "n/a");
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];
316 fprintf(stderr, "Failed to get sink input information: %s\n", pa_strerror(pa_context_errno(c)));
332 snprintf(t, sizeof(t), "%u", i->owner_module);
333 snprintf(k, sizeof(k), "%u", i->client);
335 printf("*** Sink Input #%u ***\n"
341 "Sample Specification: %s\n"
344 "Buffer Latency: %0.0f usec\n"
345 "Sink Latency: %0.0f usec\n"
346 "Resample method: %s\n",
350 i->owner_module != PA_INVALID_INDEX ? t : "n/a",
351 i->client != PA_INVALID_INDEX ? k : "n/a",
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");
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];
366 fprintf(stderr, "Failed to get source output information: %s\n", pa_strerror(pa_context_errno(c)));
383 snprintf(t, sizeof(t), "%u", i->owner_module);
384 snprintf(k, sizeof(k), "%u", i->client);
386 printf("*** Source Output #%u ***\n"
392 "Sample Specification: %s\n"
394 "Buffer Latency: %0.0f usec\n"
395 "Source Latency: %0.0f usec\n"
396 "Resample method: %s\n",
400 i->owner_module != PA_INVALID_INDEX ? t : "n/a",
401 i->client != PA_INVALID_INDEX ? k : "n/a",
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");
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];
414 fprintf(stderr, "Failed to get sample information: %s\n", pa_strerror(pa_context_errno(c)));
431 pa_bytes_snprint(t, sizeof(t), i->bytes);
433 printf("*** Sample #%u ***\n"
436 "Sample Specification: %s\n"
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,
449 i->lazy ? "yes" : "no",
450 i->filename ? i->filename : "n/a");
453 static void get_autoload_info_callback(pa_context *c, const pa_autoload_info *i, int is_last, void *userdata) {
455 fprintf(stderr, "Failed to get autoload information: %s\n", pa_strerror(pa_context_errno(c)));
471 printf("*** Autoload Entry #%u ***\n"
478 i->type == PA_AUTOLOAD_SINK ? "sink" : "source",
483 static void simple_callback(pa_context *c, int success, void *userdata) {
485 fprintf(stderr, "Failure: %s\n", pa_strerror(pa_context_errno(c)));
493 static void stream_state_callback(pa_stream *s, void *userdata) {
496 switch (pa_stream_get_state(s)) {
497 case PA_STREAM_CREATING:
498 case PA_STREAM_READY:
501 case PA_STREAM_TERMINATED:
505 case PA_STREAM_FAILED:
507 fprintf(stderr, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
512 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
515 assert(s && length && sndfile);
517 d = pa_xmalloc(length);
519 assert(sample_length >= length);
520 l = length/pa_frame_size(&sample_spec);
522 if ((sf_readf_float(sndfile, d, l)) != l) {
524 fprintf(stderr, "Premature end of file\n");
528 pa_stream_write(s, d, length, pa_xfree, 0, PA_SEEK_RELATIVE);
530 sample_length -= length;
532 if (sample_length <= 0) {
533 pa_stream_set_write_callback(sample_stream, NULL, NULL);
534 pa_stream_finish_upload(sample_stream);
538 static void context_state_callback(pa_context *c, void *userdata) {
540 switch (pa_context_get_state(c)) {
541 case PA_CONTEXT_CONNECTING:
542 case PA_CONTEXT_AUTHORIZING:
543 case PA_CONTEXT_SETTING_NAME:
546 case PA_CONTEXT_READY:
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));
555 pa_operation_unref(pa_context_play_sample(c, sample_name, device, PA_VOLUME_NORM, simple_callback, NULL));
559 pa_operation_unref(pa_context_remove_sample(c, sample_name, simple_callback, NULL));
563 sample_stream = pa_stream_new(c, sample_name, &sample_spec, NULL);
564 assert(sample_stream);
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);
572 pa_operation_unref(pa_context_exit_daemon(c, NULL, NULL));
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));
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));
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));
600 case PA_CONTEXT_TERMINATED:
604 case PA_CONTEXT_FAILED:
606 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
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");
616 static void help(const char *argv0) {
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);
633 enum { ARG_VERSION = 256 };
635 int main(int argc, char *argv[]) {
636 pa_mainloop* m = NULL;
639 char *server = NULL, *client_name = NULL, *bn;
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'},
649 if (!(bn = strrchr(argv[0], '/')))
654 while ((c = getopt_long(argc, argv, "s:n:h", long_options, NULL)) != -1) {
662 printf("pactl "PACKAGE_VERSION"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
668 server = pa_xstrdup(optarg);
672 pa_xfree(client_name);
673 client_name = pa_xstrdup(optarg);
682 client_name = pa_xstrdup(bn);
685 if (!strcmp(argv[optind], "stat"))
687 else if (!strcmp(argv[optind], "exit"))
689 else if (!strcmp(argv[optind], "list"))
691 else if (!strcmp(argv[optind], "upload-sample")) {
692 struct SF_INFO sfinfo;
693 action = UPLOAD_SAMPLE;
695 if (optind+1 >= argc) {
696 fprintf(stderr, "Please specify a sample file to load\n");
701 sample_name = pa_xstrdup(argv[optind+2]);
703 char *f = strrchr(argv[optind+1], '/');
713 sample_name = pa_xstrdup(tmp);
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");
722 sample_spec.format = PA_SAMPLE_FLOAT32;
723 sample_spec.rate = sfinfo.samplerate;
724 sample_spec.channels = sfinfo.channels;
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");
734 sample_name = pa_xstrdup(argv[optind+1]);
737 device = pa_xstrdup(argv[optind+2]);
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");
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");
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");
763 source_output_idx = atoi(argv[optind+1]);
764 source_name = pa_xstrdup(argv[optind+2]);
768 if (action == NONE) {
769 fprintf(stderr, "No valid command specified.\n");
773 if (!(m = pa_mainloop_new())) {
774 fprintf(stderr, "pa_mainloop_new() failed.\n");
778 mainloop_api = pa_mainloop_get_api(m);
780 r = pa_signal_init(mainloop_api);
782 pa_signal_new(SIGINT, exit_signal_callback, NULL);
784 signal(SIGPIPE, SIG_IGN);
787 if (!(context = pa_context_new(mainloop_api, client_name))) {
788 fprintf(stderr, "pa_context_new() failed.\n");
792 pa_context_set_state_callback(context, context_state_callback, NULL);
793 pa_context_connect(context, server, 0, NULL);
795 if (pa_mainloop_run(m, &ret) < 0) {
796 fprintf(stderr, "pa_mainloop_run() failed.\n");
802 pa_stream_unref(sample_stream);
805 pa_context_unref(context);
817 pa_xfree(sample_name);
819 pa_xfree(source_name);