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
];
163 fprintf(stderr
, "Failed to get sink information: %s\n", pa_strerror(pa_context_errno(c
)));
179 printf("*** Sink #%u ***\n"
183 "Sample Specification: %s\n"
187 "Monitor Source: %u\n"
188 "Latency: %0.0f usec\n"
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
),
200 i
->flags
& PA_SINK_HW_VOLUME_CTRL
? "HW_VOLUME_CTRL " : "",
201 i
->flags
& PA_SINK_LATENCY
? "LATENCY " : "",
202 i
->flags
& PA_SINK_HARDWARE
? "HARDWARE" : "");
206 static void get_source_info_callback(pa_context
*c
, const pa_source_info
*i
, int is_last
, void *userdata
) {
207 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], t
[32], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
210 fprintf(stderr
, "Failed to get source information: %s\n", pa_strerror(pa_context_errno(c
)));
226 snprintf(t
, sizeof(t
), "%u", i
->monitor_of_sink
);
228 printf("*** Source #%u ***\n"
232 "Sample Specification: %s\n"
236 "Monitor of Sink: %s\n"
237 "Latency: %0.0f usec\n"
243 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
244 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
246 i
->mute
? "muted" : pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
247 i
->monitor_of_sink
!= PA_INVALID_INDEX
? t
: "no",
249 i
->flags
& PA_SOURCE_HW_VOLUME_CTRL
? "HW_VOLUME_CTRL " : "",
250 i
->flags
& PA_SOURCE_LATENCY
? "LATENCY " : "",
251 i
->flags
& PA_SOURCE_HARDWARE
? "HARDWARE" : "");
255 static void get_module_info_callback(pa_context
*c
, const pa_module_info
*i
, int is_last
, void *userdata
) {
259 fprintf(stderr
, "Failed to get module information: %s\n", pa_strerror(pa_context_errno(c
)));
275 snprintf(t
, sizeof(t
), "%u", i
->n_used
);
277 printf("*** Module #%u ***\n"
280 "Usage counter: %s\n"
284 i
->argument
? i
->argument
: "",
285 i
->n_used
!= PA_INVALID_INDEX
? t
: "n/a",
286 i
->auto_unload
? "yes" : "no");
289 static void get_client_info_callback(pa_context
*c
, const pa_client_info
*i
, int is_last
, void *userdata
) {
293 fprintf(stderr
, "Failed to get client information: %s\n", pa_strerror(pa_context_errno(c
)));
309 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
311 printf("*** Client #%u ***\n"
314 "Owner Module: %s\n",
318 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a");
321 static void get_sink_input_info_callback(pa_context
*c
, const pa_sink_input_info
*i
, int is_last
, void *userdata
) {
322 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
325 fprintf(stderr
, "Failed to get sink input information: %s\n", pa_strerror(pa_context_errno(c
)));
341 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
342 snprintf(k
, sizeof(k
), "%u", i
->client
);
344 printf("*** Sink Input #%u ***\n"
350 "Sample Specification: %s\n"
353 "Buffer Latency: %0.0f usec\n"
354 "Sink Latency: %0.0f usec\n"
355 "Resample method: %s\n",
359 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
360 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
362 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
363 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
364 i
->mute
? "muted" : pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
365 (double) i
->buffer_usec
,
366 (double) i
->sink_usec
,
367 i
->resample_method
? i
->resample_method
: "n/a");
371 static void get_source_output_info_callback(pa_context
*c
, const pa_source_output_info
*i
, int is_last
, void *userdata
) {
372 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
375 fprintf(stderr
, "Failed to get source output information: %s\n", pa_strerror(pa_context_errno(c
)));
392 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
393 snprintf(k
, sizeof(k
), "%u", i
->client
);
395 printf("*** Source Output #%u ***\n"
401 "Sample Specification: %s\n"
403 "Buffer Latency: %0.0f usec\n"
404 "Source Latency: %0.0f usec\n"
405 "Resample method: %s\n",
409 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
410 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
412 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
413 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
414 (double) i
->buffer_usec
,
415 (double) i
->source_usec
,
416 i
->resample_method
? i
->resample_method
: "n/a");
419 static void get_sample_info_callback(pa_context
*c
, const pa_sample_info
*i
, int is_last
, void *userdata
) {
420 char t
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
423 fprintf(stderr
, "Failed to get sample information: %s\n", pa_strerror(pa_context_errno(c
)));
440 pa_bytes_snprint(t
, sizeof(t
), i
->bytes
);
442 printf("*** Sample #%u ***\n"
445 "Sample Specification: %s\n"
453 pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
454 pa_sample_spec_valid(&i
->sample_spec
) ? pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
) : "n/a",
455 pa_sample_spec_valid(&i
->sample_spec
) ? pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
) : "n/a",
456 (double) i
->duration
/1000000,
458 i
->lazy
? "yes" : "no",
459 i
->filename
? i
->filename
: "n/a");
462 static void get_autoload_info_callback(pa_context
*c
, const pa_autoload_info
*i
, int is_last
, void *userdata
) {
464 fprintf(stderr
, "Failed to get autoload information: %s\n", pa_strerror(pa_context_errno(c
)));
480 printf("*** Autoload Entry #%u ***\n"
487 i
->type
== PA_AUTOLOAD_SINK
? "sink" : "source",
492 static void simple_callback(pa_context
*c
, int success
, void *userdata
) {
494 fprintf(stderr
, "Failure: %s\n", pa_strerror(pa_context_errno(c
)));
502 static void index_callback(pa_context
*c
, uint32_t idx
, void *userdata
) {
503 if (idx
== PA_INVALID_INDEX
) {
504 fprintf(stderr
, "Failure: %s\n", pa_strerror(pa_context_errno(c
)));
514 static void stream_state_callback(pa_stream
*s
, void *userdata
) {
517 switch (pa_stream_get_state(s
)) {
518 case PA_STREAM_CREATING
:
519 case PA_STREAM_READY
:
522 case PA_STREAM_TERMINATED
:
526 case PA_STREAM_FAILED
:
528 fprintf(stderr
, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
533 static void stream_write_callback(pa_stream
*s
, size_t length
, void *userdata
) {
536 assert(s
&& length
&& sndfile
);
538 d
= pa_xmalloc(length
);
540 assert(sample_length
>= length
);
541 l
= length
/pa_frame_size(&sample_spec
);
543 if ((sf_readf_float(sndfile
, d
, l
)) != l
) {
545 fprintf(stderr
, "Premature end of file\n");
549 pa_stream_write(s
, d
, length
, pa_xfree
, 0, PA_SEEK_RELATIVE
);
551 sample_length
-= length
;
553 if (sample_length
<= 0) {
554 pa_stream_set_write_callback(sample_stream
, NULL
, NULL
);
555 pa_stream_finish_upload(sample_stream
);
559 static void context_state_callback(pa_context
*c
, void *userdata
) {
561 switch (pa_context_get_state(c
)) {
562 case PA_CONTEXT_CONNECTING
:
563 case PA_CONTEXT_AUTHORIZING
:
564 case PA_CONTEXT_SETTING_NAME
:
567 case PA_CONTEXT_READY
:
571 pa_operation_unref(pa_context_stat(c
, stat_callback
, NULL
));
572 pa_operation_unref(pa_context_get_server_info(c
, get_server_info_callback
, NULL
));
576 pa_operation_unref(pa_context_play_sample(c
, sample_name
, device
, PA_VOLUME_NORM
, simple_callback
, NULL
));
580 pa_operation_unref(pa_context_remove_sample(c
, sample_name
, simple_callback
, NULL
));
584 sample_stream
= pa_stream_new(c
, sample_name
, &sample_spec
, NULL
);
585 assert(sample_stream
);
587 pa_stream_set_state_callback(sample_stream
, stream_state_callback
, NULL
);
588 pa_stream_set_write_callback(sample_stream
, stream_write_callback
, NULL
);
589 pa_stream_connect_upload(sample_stream
, sample_length
);
593 pa_operation_unref(pa_context_exit_daemon(c
, NULL
, NULL
));
598 pa_operation_unref(pa_context_get_module_info_list(c
, get_module_info_callback
, NULL
));
599 pa_operation_unref(pa_context_get_sink_info_list(c
, get_sink_info_callback
, NULL
));
600 pa_operation_unref(pa_context_get_source_info_list(c
, get_source_info_callback
, NULL
));
601 pa_operation_unref(pa_context_get_sink_input_info_list(c
, get_sink_input_info_callback
, NULL
));
602 pa_operation_unref(pa_context_get_source_output_info_list(c
, get_source_output_info_callback
, NULL
));
603 pa_operation_unref(pa_context_get_client_info_list(c
, get_client_info_callback
, NULL
));
604 pa_operation_unref(pa_context_get_sample_info_list(c
, get_sample_info_callback
, NULL
));
605 pa_operation_unref(pa_context_get_autoload_info_list(c
, get_autoload_info_callback
, NULL
));
608 case MOVE_SINK_INPUT
:
609 pa_operation_unref(pa_context_move_sink_input_by_name(c
, sink_input_idx
, sink_name
, simple_callback
, NULL
));
612 case MOVE_SOURCE_OUTPUT
:
613 pa_operation_unref(pa_context_move_source_output_by_name(c
, source_output_idx
, source_name
, simple_callback
, NULL
));
617 pa_operation_unref(pa_context_load_module(c
, module_name
, module_args
, index_callback
, NULL
));
621 pa_operation_unref(pa_context_unload_module(c
, module_index
, simple_callback
, NULL
));
626 pa_operation_unref(pa_context_suspend_sink_by_name(c
, sink_name
, suspend
, simple_callback
, NULL
));
628 pa_operation_unref(pa_context_suspend_sink_by_index(c
, PA_INVALID_INDEX
, suspend
, simple_callback
, NULL
));
633 pa_operation_unref(pa_context_suspend_source_by_name(c
, source_name
, suspend
, simple_callback
, NULL
));
635 pa_operation_unref(pa_context_suspend_source_by_index(c
, PA_INVALID_INDEX
, suspend
, simple_callback
, NULL
));
643 case PA_CONTEXT_TERMINATED
:
647 case PA_CONTEXT_FAILED
:
649 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
654 static void exit_signal_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
655 fprintf(stderr
, "Got SIGINT, exiting.\n");
659 static void help(const char *argv0
) {
661 printf("%s [options] stat\n"
662 "%s [options] list\n"
663 "%s [options] exit\n"
664 "%s [options] upload-sample FILENAME [NAME]\n"
665 "%s [options] play-sample NAME [SINK]\n"
666 "%s [options] remove-sample NAME\n"
667 "%s [options] move-sink-input ID SINK\n"
668 "%s [options] move-source-output ID SOURCE\n"
669 "%s [options] load-module NAME [ARGS ...]\n"
670 "%s [options] unload-module ID\n"
671 "%s [options] suspend-sink [SINK] 1|0\n"
672 "%s [options] suspend-source [SOURCE] 1|0\n\n"
673 " -h, --help Show this help\n"
674 " --version Show version\n\n"
675 " -s, --server=SERVER The name of the server to connect to\n"
676 " -n, --client-name=NAME How to call this client on the server\n",
677 argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
);
680 enum { ARG_VERSION
= 256 };
682 int main(int argc
, char *argv
[]) {
683 pa_mainloop
* m
= NULL
;
686 char *server
= NULL
, *client_name
= NULL
, *bn
;
688 static const struct option long_options
[] = {
689 {"server", 1, NULL
, 's'},
690 {"client-name", 1, NULL
, 'n'},
691 {"version", 0, NULL
, ARG_VERSION
},
692 {"help", 0, NULL
, 'h'},
696 if (!(bn
= strrchr(argv
[0], '/')))
701 while ((c
= getopt_long(argc
, argv
, "s:n:h", long_options
, NULL
)) != -1) {
709 printf("pactl "PACKAGE_VERSION
"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
715 server
= pa_xstrdup(optarg
);
719 pa_xfree(client_name
);
720 client_name
= pa_xstrdup(optarg
);
729 client_name
= pa_xstrdup(bn
);
732 if (!strcmp(argv
[optind
], "stat"))
734 else if (!strcmp(argv
[optind
], "exit"))
736 else if (!strcmp(argv
[optind
], "list"))
738 else if (!strcmp(argv
[optind
], "upload-sample")) {
739 struct SF_INFO sfinfo
;
740 action
= UPLOAD_SAMPLE
;
742 if (optind
+1 >= argc
) {
743 fprintf(stderr
, "Please specify a sample file to load\n");
748 sample_name
= pa_xstrdup(argv
[optind
+2]);
750 char *f
= strrchr(argv
[optind
+1], '/');
760 sample_name
= pa_xstrdup(tmp
);
763 memset(&sfinfo
, 0, sizeof(sfinfo
));
764 if (!(sndfile
= sf_open(argv
[optind
+1], SFM_READ
, &sfinfo
))) {
765 fprintf(stderr
, "Failed to open sound file.\n");
769 sample_spec
.format
= PA_SAMPLE_FLOAT32
;
770 sample_spec
.rate
= sfinfo
.samplerate
;
771 sample_spec
.channels
= sfinfo
.channels
;
773 sample_length
= sfinfo
.frames
*pa_frame_size(&sample_spec
);
774 } else if (!strcmp(argv
[optind
], "play-sample")) {
775 action
= PLAY_SAMPLE
;
776 if (argc
!= optind
+2 && argc
!= optind
+3) {
777 fprintf(stderr
, "You have to specify a sample name to play\n");
781 sample_name
= pa_xstrdup(argv
[optind
+1]);
784 device
= pa_xstrdup(argv
[optind
+2]);
786 } else if (!strcmp(argv
[optind
], "remove-sample")) {
787 action
= REMOVE_SAMPLE
;
788 if (argc
!= optind
+2) {
789 fprintf(stderr
, "You have to specify a sample name to remove\n");
793 sample_name
= pa_xstrdup(argv
[optind
+1]);
794 } else if (!strcmp(argv
[optind
], "move-sink-input")) {
795 action
= MOVE_SINK_INPUT
;
796 if (argc
!= optind
+3) {
797 fprintf(stderr
, "You have to specify a sink input index and a sink\n");
801 sink_input_idx
= atoi(argv
[optind
+1]);
802 sink_name
= pa_xstrdup(argv
[optind
+2]);
803 } else if (!strcmp(argv
[optind
], "move-source-output")) {
804 action
= MOVE_SOURCE_OUTPUT
;
805 if (argc
!= optind
+3) {
806 fprintf(stderr
, "You have to specify a source output index and a source\n");
810 source_output_idx
= atoi(argv
[optind
+1]);
811 source_name
= pa_xstrdup(argv
[optind
+2]);
812 } else if (!strcmp(argv
[optind
], "load-module")) {
817 action
= LOAD_MODULE
;
819 if (argc
<= optind
+1) {
820 fprintf(stderr
, "You have to specify a module name and arguments.\n");
824 module_name
= argv
[optind
+1];
826 for (i
= optind
+2; i
< argc
; i
++)
827 n
+= strlen(argv
[i
])+1;
830 p
= module_args
= pa_xnew0(char, n
);
832 for (i
= optind
+2; i
< argc
; i
++)
833 p
+= sprintf(p
, "%s%s", p
== module_args
? "" : " ", argv
[i
]);
836 } else if (!strcmp(argv
[optind
], "unload-module")) {
837 action
= UNLOAD_MODULE
;
839 if (argc
!= optind
+2) {
840 fprintf(stderr
, "You have to specify a module index\n");
844 module_index
= atoi(argv
[optind
+1]);
846 } else if (!strcmp(argv
[optind
], "suspend-sink")) {
847 action
= SUSPEND_SINK
;
849 if (argc
> optind
+3 || optind
+1 >= argc
) {
850 fprintf(stderr
, "You may not specify more than one sink. You have to specify at least one boolean value.\n");
854 suspend
= pa_parse_boolean(argv
[argc
-1]);
857 sink_name
= pa_xstrdup(argv
[optind
+1]);
859 } else if (!strcmp(argv
[optind
], "suspend-source")) {
860 action
= SUSPEND_SOURCE
;
862 if (argc
> optind
+3 || optind
+1 >= argc
) {
863 fprintf(stderr
, "You may not specify more than one source. You have to specify at least one boolean value.\n");
867 suspend
= pa_parse_boolean(argv
[argc
-1]);
870 source_name
= pa_xstrdup(argv
[optind
+1]);
874 if (action
== NONE
) {
875 fprintf(stderr
, "No valid command specified.\n");
879 if (!(m
= pa_mainloop_new())) {
880 fprintf(stderr
, "pa_mainloop_new() failed.\n");
884 mainloop_api
= pa_mainloop_get_api(m
);
886 r
= pa_signal_init(mainloop_api
);
888 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
890 signal(SIGPIPE
, SIG_IGN
);
893 if (!(context
= pa_context_new(mainloop_api
, client_name
))) {
894 fprintf(stderr
, "pa_context_new() failed.\n");
898 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
899 pa_context_connect(context
, server
, 0, NULL
);
901 if (pa_mainloop_run(m
, &ret
) < 0) {
902 fprintf(stderr
, "pa_mainloop_run() failed.\n");
908 pa_stream_unref(sample_stream
);
911 pa_context_unref(context
);
923 pa_xfree(sample_name
);
925 pa_xfree(source_name
);
926 pa_xfree(module_args
);
927 pa_xfree(client_name
);