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>
42 #if PA_API_VERSION < 10
43 #error Invalid PulseAudio API version
48 static pa_context *context = NULL;
49 static pa_mainloop_api *mainloop_api = NULL;
51 static char *device = NULL, *sample_name = NULL, *sink_name = NULL, *source_name = NULL;
52 static uint32_t sink_input_idx = PA_INVALID_INDEX, source_output_idx = PA_INVALID_INDEX;
54 static SNDFILE *sndfile = NULL;
55 static pa_stream *sample_stream = NULL;
56 static pa_sample_spec sample_spec;
57 static size_t sample_length = 0;
59 static int actions = 1;
75 static void quit(int ret) {
77 mainloop_api->quit(mainloop_api, ret);
81 static void context_drain_complete(pa_context *c, void *userdata) {
82 pa_context_disconnect(c);
85 static void drain(void) {
87 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
88 pa_context_disconnect(context);
90 pa_operation_unref(o);
94 static void complete_action(void) {
101 static void stat_callback(pa_context *c, const pa_stat_info *i, void *userdata) {
104 fprintf(stderr, "Failed to get statistics: %s\n", pa_strerror(pa_context_errno(c)));
109 pa_bytes_snprint(s, sizeof(s), i->memblock_total_size);
110 printf("Currently in use: %u blocks containing %s bytes total.\n", i->memblock_total, s);
112 pa_bytes_snprint(s, sizeof(s), i->memblock_allocated_size);
113 printf("Allocated during whole lifetime: %u blocks containing %s bytes total.\n", i->memblock_allocated, s);
115 pa_bytes_snprint(s, sizeof(s), i->scache_size);
116 printf("Sample cache size: %s\n", s);
121 static void get_server_info_callback(pa_context *c, const pa_server_info *i, void *useerdata) {
122 char s[PA_SAMPLE_SPEC_SNPRINT_MAX];
125 fprintf(stderr, "Failed to get server information: %s\n", pa_strerror(pa_context_errno(c)));
130 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec);
132 printf("User name: %s\n"
135 "Server Version: %s\n"
136 "Default Sample Specification: %s\n"
138 "Default Source: %s\n"
145 i->default_sink_name,
146 i->default_source_name,
152 static void get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
153 char s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
156 fprintf(stderr, "Failed to get sink information: %s\n", pa_strerror(pa_context_errno(c)));
172 printf("*** Sink #%u ***\n"
176 "Sample Specification: %s\n"
180 "Monitor Source: %u\n"
181 "Latency: %0.0f usec\n"
187 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
188 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
190 i->mute ? "muted" : pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
193 i->flags & PA_SINK_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
194 i->flags & PA_SINK_LATENCY ? "LATENCY " : "",
195 i->flags & PA_SINK_HARDWARE ? "HARDWARE" : "");
199 static void get_source_info_callback(pa_context *c, const pa_source_info *i, int is_last, void *userdata) {
200 char s[PA_SAMPLE_SPEC_SNPRINT_MAX], t[32], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
203 fprintf(stderr, "Failed to get source information: %s\n", pa_strerror(pa_context_errno(c)));
219 snprintf(t, sizeof(t), "%u", i->monitor_of_sink);
221 printf("*** Source #%u ***\n"
225 "Sample Specification: %s\n"
229 "Monitor of Sink: %s\n"
230 "Latency: %0.0f usec\n"
236 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
237 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
239 i->mute ? "muted" : pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
240 i->monitor_of_sink != PA_INVALID_INDEX ? t : "no",
242 i->flags & PA_SOURCE_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
243 i->flags & PA_SOURCE_LATENCY ? "LATENCY " : "",
244 i->flags & PA_SOURCE_HARDWARE ? "HARDWARE" : "");
248 static void get_module_info_callback(pa_context *c, const pa_module_info *i, int is_last, void *userdata) {
252 fprintf(stderr, "Failed to get module information: %s\n", pa_strerror(pa_context_errno(c)));
268 snprintf(t, sizeof(t), "%u", i->n_used);
270 printf("*** Module #%u ***\n"
273 "Usage counter: %s\n"
278 i->n_used != PA_INVALID_INDEX ? t : "n/a",
279 i->auto_unload ? "yes" : "no");
282 static void get_client_info_callback(pa_context *c, const pa_client_info *i, int is_last, void *userdata) {
286 fprintf(stderr, "Failed to get client information: %s\n", pa_strerror(pa_context_errno(c)));
302 snprintf(t, sizeof(t), "%u", i->owner_module);
304 printf("*** Client #%u ***\n"
307 "Owner Module: %s\n",
311 i->owner_module != PA_INVALID_INDEX ? t : "n/a");
314 static void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
315 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
318 fprintf(stderr, "Failed to get sink input information: %s\n", pa_strerror(pa_context_errno(c)));
334 snprintf(t, sizeof(t), "%u", i->owner_module);
335 snprintf(k, sizeof(k), "%u", i->client);
337 printf("*** Sink Input #%u ***\n"
343 "Sample Specification: %s\n"
346 "Buffer Latency: %0.0f usec\n"
347 "Sink Latency: %0.0f usec\n"
348 "Resample method: %s\n",
352 i->owner_module != PA_INVALID_INDEX ? t : "n/a",
353 i->client != PA_INVALID_INDEX ? k : "n/a",
355 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
356 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
357 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
358 (double) i->buffer_usec,
359 (double) i->sink_usec,
360 i->resample_method ? i->resample_method : "n/a");
364 static void get_source_output_info_callback(pa_context *c, const pa_source_output_info *i, int is_last, void *userdata) {
365 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
368 fprintf(stderr, "Failed to get source output information: %s\n", pa_strerror(pa_context_errno(c)));
385 snprintf(t, sizeof(t), "%u", i->owner_module);
386 snprintf(k, sizeof(k), "%u", i->client);
388 printf("*** Source Output #%u ***\n"
394 "Sample Specification: %s\n"
396 "Buffer Latency: %0.0f usec\n"
397 "Source Latency: %0.0f usec\n"
398 "Resample method: %s\n",
402 i->owner_module != PA_INVALID_INDEX ? t : "n/a",
403 i->client != PA_INVALID_INDEX ? k : "n/a",
405 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
406 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
407 (double) i->buffer_usec,
408 (double) i->source_usec,
409 i->resample_method ? i->resample_method : "n/a");
412 static void get_sample_info_callback(pa_context *c, const pa_sample_info *i, int is_last, void *userdata) {
413 char t[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
416 fprintf(stderr, "Failed to get sample information: %s\n", pa_strerror(pa_context_errno(c)));
433 pa_bytes_snprint(t, sizeof(t), i->bytes);
435 printf("*** Sample #%u ***\n"
438 "Sample Specification: %s\n"
446 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
447 pa_sample_spec_valid(&i->sample_spec) ? pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec) : "n/a",
448 pa_sample_spec_valid(&i->sample_spec) ? pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map) : "n/a",
449 (double) i->duration/1000000,
451 i->lazy ? "yes" : "no",
452 i->filename ? i->filename : "n/a");
455 static void get_autoload_info_callback(pa_context *c, const pa_autoload_info *i, int is_last, void *userdata) {
457 fprintf(stderr, "Failed to get autoload information: %s\n", pa_strerror(pa_context_errno(c)));
473 printf("*** Autoload Entry #%u ***\n"
480 i->type == PA_AUTOLOAD_SINK ? "sink" : "source",
485 static void simple_callback(pa_context *c, int success, void *userdata) {
487 fprintf(stderr, "Failure: %s\n", pa_strerror(pa_context_errno(c)));
495 static void stream_state_callback(pa_stream *s, void *userdata) {
498 switch (pa_stream_get_state(s)) {
499 case PA_STREAM_CREATING:
500 case PA_STREAM_READY:
503 case PA_STREAM_TERMINATED:
507 case PA_STREAM_FAILED:
509 fprintf(stderr, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
514 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
517 assert(s && length && sndfile);
519 d = pa_xmalloc(length);
521 assert(sample_length >= length);
522 l = length/pa_frame_size(&sample_spec);
524 if ((sf_readf_float(sndfile, d, l)) != l) {
526 fprintf(stderr, "Premature end of file\n");
530 pa_stream_write(s, d, length, pa_xfree, 0, PA_SEEK_RELATIVE);
532 sample_length -= length;
534 if (sample_length <= 0) {
535 pa_stream_set_write_callback(sample_stream, NULL, NULL);
536 pa_stream_finish_upload(sample_stream);
540 static void context_state_callback(pa_context *c, void *userdata) {
542 switch (pa_context_get_state(c)) {
543 case PA_CONTEXT_CONNECTING:
544 case PA_CONTEXT_AUTHORIZING:
545 case PA_CONTEXT_SETTING_NAME:
548 case PA_CONTEXT_READY:
552 pa_operation_unref(pa_context_stat(c, stat_callback, NULL));
553 pa_operation_unref(pa_context_get_server_info(c, get_server_info_callback, NULL));
557 pa_operation_unref(pa_context_play_sample(c, sample_name, device, PA_VOLUME_NORM, simple_callback, NULL));
561 pa_operation_unref(pa_context_remove_sample(c, sample_name, simple_callback, NULL));
565 sample_stream = pa_stream_new(c, sample_name, &sample_spec, NULL);
566 assert(sample_stream);
568 pa_stream_set_state_callback(sample_stream, stream_state_callback, NULL);
569 pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL);
570 pa_stream_connect_upload(sample_stream, sample_length);
574 pa_operation_unref(pa_context_exit_daemon(c, NULL, NULL));
579 pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL));
580 pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL));
581 pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL));
582 pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL));
583 pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL));
584 pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL));
585 pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL));
586 pa_operation_unref(pa_context_get_autoload_info_list(c, get_autoload_info_callback, NULL));
589 case MOVE_SINK_INPUT:
590 pa_operation_unref(pa_context_move_sink_input_by_name(c, sink_input_idx, sink_name, simple_callback, NULL));
593 case MOVE_SOURCE_OUTPUT:
594 pa_operation_unref(pa_context_move_source_output_by_name(c, source_output_idx, source_name, simple_callback, NULL));
602 case PA_CONTEXT_TERMINATED:
606 case PA_CONTEXT_FAILED:
608 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
613 static void exit_signal_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
614 fprintf(stderr, "Got SIGINT, exiting.\n");
618 static void help(const char *argv0) {
620 printf("%s [options] stat\n"
621 "%s [options] list\n"
622 "%s [options] exit\n"
623 "%s [options] upload-sample FILENAME [NAME]\n"
624 "%s [options] play-sample NAME [SINK]\n"
625 "%s [options] move-sink-input ID SINK\n"
626 "%s [options] move-source-output ID SOURCE\n"
627 "%s [options] remove-sample NAME\n\n"
628 " -h, --help Show this help\n"
629 " --version Show version\n\n"
630 " -s, --server=SERVER The name of the server to connect to\n"
631 " -n, --client-name=NAME How to call this client on the server\n",
632 argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0);
635 enum { ARG_VERSION = 256 };
637 int main(int argc, char *argv[]) {
638 pa_mainloop* m = NULL;
641 char *server = NULL, *client_name = NULL, *bn;
643 static const struct option long_options[] = {
644 {"server", 1, NULL, 's'},
645 {"client-name", 1, NULL, 'n'},
646 {"version", 0, NULL, ARG_VERSION},
647 {"help", 0, NULL, 'h'},
651 if (!(bn = strrchr(argv[0], '/')))
656 while ((c = getopt_long(argc, argv, "s:n:h", long_options, NULL)) != -1) {
664 printf("pactl "PACKAGE_VERSION"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
670 server = pa_xstrdup(optarg);
674 pa_xfree(client_name);
675 client_name = pa_xstrdup(optarg);
684 client_name = pa_xstrdup(bn);
687 if (!strcmp(argv[optind], "stat"))
689 else if (!strcmp(argv[optind], "exit"))
691 else if (!strcmp(argv[optind], "list"))
693 else if (!strcmp(argv[optind], "upload-sample")) {
694 struct SF_INFO sfinfo;
695 action = UPLOAD_SAMPLE;
697 if (optind+1 >= argc) {
698 fprintf(stderr, "Please specify a sample file to load\n");
703 sample_name = pa_xstrdup(argv[optind+2]);
705 char *f = strrchr(argv[optind+1], '/');
715 sample_name = pa_xstrdup(tmp);
718 memset(&sfinfo, 0, sizeof(sfinfo));
719 if (!(sndfile = sf_open(argv[optind+1], SFM_READ, &sfinfo))) {
720 fprintf(stderr, "Failed to open sound file.\n");
724 sample_spec.format = PA_SAMPLE_FLOAT32;
725 sample_spec.rate = sfinfo.samplerate;
726 sample_spec.channels = sfinfo.channels;
728 sample_length = sfinfo.frames*pa_frame_size(&sample_spec);
729 } else if (!strcmp(argv[optind], "play-sample")) {
730 action = PLAY_SAMPLE;
731 if (optind+1 >= argc) {
732 fprintf(stderr, "You have to specify a sample name to play\n");
736 sample_name = pa_xstrdup(argv[optind+1]);
739 device = pa_xstrdup(argv[optind+2]);
741 } else if (!strcmp(argv[optind], "remove-sample")) {
742 action = REMOVE_SAMPLE;
743 if (optind+1 >= argc) {
744 fprintf(stderr, "You have to specify a sample name to remove\n");
748 sample_name = pa_xstrdup(argv[optind+1]);
749 } else if (!strcmp(argv[optind], "move-sink-input")) {
750 action = MOVE_SINK_INPUT;
751 if (optind+2 >= argc) {
752 fprintf(stderr, "You have to specify a sink input index and a sink\n");
756 sink_input_idx = atoi(argv[optind+1]);
757 sink_name = pa_xstrdup(argv[optind+2]);
758 } else if (!strcmp(argv[optind], "move-source-output")) {
759 action = MOVE_SOURCE_OUTPUT;
760 if (optind+2 >= argc) {
761 fprintf(stderr, "You have to specify a source output index and a source\n");
765 source_output_idx = atoi(argv[optind+1]);
766 source_name = pa_xstrdup(argv[optind+2]);
770 if (action == NONE) {
771 fprintf(stderr, "No valid command specified.\n");
775 if (!(m = pa_mainloop_new())) {
776 fprintf(stderr, "pa_mainloop_new() failed.\n");
780 mainloop_api = pa_mainloop_get_api(m);
782 r = pa_signal_init(mainloop_api);
784 pa_signal_new(SIGINT, exit_signal_callback, NULL);
786 signal(SIGPIPE, SIG_IGN);
789 if (!(context = pa_context_new(mainloop_api, client_name))) {
790 fprintf(stderr, "pa_context_new() failed.\n");
794 pa_context_set_state_callback(context, context_state_callback, NULL);
795 pa_context_connect(context, server, 0, NULL);
797 if (pa_mainloop_run(m, &ret) < 0) {
798 fprintf(stderr, "pa_mainloop_run() failed.\n");
804 pa_stream_unref(sample_stream);
807 pa_context_unref(context);
819 pa_xfree(sample_name);
821 pa_xfree(source_name);