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
, *module_name
= NULL
, *module_args
= NULL
;
52 static uint32_t sink_input_idx
= PA_INVALID_INDEX
, source_output_idx
= PA_INVALID_INDEX
;
53 static uint32_t module_index
;
56 static SNDFILE
*sndfile
= NULL
;
57 static pa_stream
*sample_stream
= NULL
;
58 static pa_sample_spec sample_spec
;
59 static size_t sample_length
= 0;
61 static int actions
= 1;
81 static void quit(int ret
) {
83 mainloop_api
->quit(mainloop_api
, ret
);
87 static void context_drain_complete(pa_context
*c
, void *userdata
) {
88 pa_context_disconnect(c
);
91 static void drain(void) {
93 if (!(o
= pa_context_drain(context
, context_drain_complete
, NULL
)))
94 pa_context_disconnect(context
);
96 pa_operation_unref(o
);
100 static void complete_action(void) {
107 static void stat_callback(pa_context
*c
, const pa_stat_info
*i
, void *userdata
) {
110 fprintf(stderr
, "Failed to get statistics: %s\n", pa_strerror(pa_context_errno(c
)));
115 pa_bytes_snprint(s
, sizeof(s
), i
->memblock_total_size
);
116 printf("Currently in use: %u blocks containing %s bytes total.\n", i
->memblock_total
, s
);
118 pa_bytes_snprint(s
, sizeof(s
), i
->memblock_allocated_size
);
119 printf("Allocated during whole lifetime: %u blocks containing %s bytes total.\n", i
->memblock_allocated
, s
);
121 pa_bytes_snprint(s
, sizeof(s
), i
->scache_size
);
122 printf("Sample cache size: %s\n", s
);
127 static void get_server_info_callback(pa_context
*c
, const pa_server_info
*i
, void *useerdata
) {
128 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
131 fprintf(stderr
, "Failed to get server information: %s\n", pa_strerror(pa_context_errno(c
)));
136 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
);
138 printf("User name: %s\n"
141 "Server Version: %s\n"
142 "Default Sample Specification: %s\n"
144 "Default Source: %s\n"
151 i
->default_sink_name
,
152 i
->default_source_name
,
158 static void get_sink_info_callback(pa_context
*c
, const pa_sink_info
*i
, int is_last
, void *userdata
) {
159 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
162 fprintf(stderr
, "Failed to get sink information: %s\n", pa_strerror(pa_context_errno(c
)));
178 printf("*** Sink #%u ***\n"
182 "Sample Specification: %s\n"
186 "Monitor Source: %u\n"
187 "Latency: %0.0f usec\n"
193 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
194 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
196 i
->mute
? "muted" : pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
199 i
->flags
& PA_SINK_HW_VOLUME_CTRL
? "HW_VOLUME_CTRL " : "",
200 i
->flags
& PA_SINK_LATENCY
? "LATENCY " : "",
201 i
->flags
& PA_SINK_HARDWARE
? "HARDWARE" : "");
205 static void get_source_info_callback(pa_context
*c
, const pa_source_info
*i
, int is_last
, void *userdata
) {
206 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], t
[32], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
209 fprintf(stderr
, "Failed to get source information: %s\n", pa_strerror(pa_context_errno(c
)));
225 snprintf(t
, sizeof(t
), "%u", i
->monitor_of_sink
);
227 printf("*** Source #%u ***\n"
231 "Sample Specification: %s\n"
235 "Monitor of Sink: %s\n"
236 "Latency: %0.0f usec\n"
242 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
243 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
245 i
->mute
? "muted" : pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
246 i
->monitor_of_sink
!= PA_INVALID_INDEX
? t
: "no",
248 i
->flags
& PA_SOURCE_HW_VOLUME_CTRL
? "HW_VOLUME_CTRL " : "",
249 i
->flags
& PA_SOURCE_LATENCY
? "LATENCY " : "",
250 i
->flags
& PA_SOURCE_HARDWARE
? "HARDWARE" : "");
254 static void get_module_info_callback(pa_context
*c
, const pa_module_info
*i
, int is_last
, void *userdata
) {
258 fprintf(stderr
, "Failed to get module information: %s\n", pa_strerror(pa_context_errno(c
)));
274 snprintf(t
, sizeof(t
), "%u", i
->n_used
);
276 printf("*** Module #%u ***\n"
279 "Usage counter: %s\n"
284 i
->n_used
!= PA_INVALID_INDEX
? t
: "n/a",
285 i
->auto_unload
? "yes" : "no");
288 static void get_client_info_callback(pa_context
*c
, const pa_client_info
*i
, int is_last
, void *userdata
) {
292 fprintf(stderr
, "Failed to get client information: %s\n", pa_strerror(pa_context_errno(c
)));
308 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
310 printf("*** Client #%u ***\n"
313 "Owner Module: %s\n",
317 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a");
320 static void get_sink_input_info_callback(pa_context
*c
, const pa_sink_input_info
*i
, int is_last
, void *userdata
) {
321 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
324 fprintf(stderr
, "Failed to get sink input information: %s\n", pa_strerror(pa_context_errno(c
)));
340 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
341 snprintf(k
, sizeof(k
), "%u", i
->client
);
343 printf("*** Sink Input #%u ***\n"
349 "Sample Specification: %s\n"
352 "Buffer Latency: %0.0f usec\n"
353 "Sink Latency: %0.0f usec\n"
354 "Resample method: %s\n",
358 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
359 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
361 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
362 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
363 i
->mute
? "muted" : pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
364 (double) i
->buffer_usec
,
365 (double) i
->sink_usec
,
366 i
->resample_method
? i
->resample_method
: "n/a");
370 static void get_source_output_info_callback(pa_context
*c
, const pa_source_output_info
*i
, int is_last
, void *userdata
) {
371 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
374 fprintf(stderr
, "Failed to get source output information: %s\n", pa_strerror(pa_context_errno(c
)));
391 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
392 snprintf(k
, sizeof(k
), "%u", i
->client
);
394 printf("*** Source Output #%u ***\n"
400 "Sample Specification: %s\n"
402 "Buffer Latency: %0.0f usec\n"
403 "Source Latency: %0.0f usec\n"
404 "Resample method: %s\n",
408 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
409 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
411 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
412 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
413 (double) i
->buffer_usec
,
414 (double) i
->source_usec
,
415 i
->resample_method
? i
->resample_method
: "n/a");
418 static void get_sample_info_callback(pa_context
*c
, const pa_sample_info
*i
, int is_last
, void *userdata
) {
419 char t
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
422 fprintf(stderr
, "Failed to get sample information: %s\n", pa_strerror(pa_context_errno(c
)));
439 pa_bytes_snprint(t
, sizeof(t
), i
->bytes
);
441 printf("*** Sample #%u ***\n"
444 "Sample Specification: %s\n"
452 pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
453 pa_sample_spec_valid(&i
->sample_spec
) ? pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
) : "n/a",
454 pa_sample_spec_valid(&i
->sample_spec
) ? pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
) : "n/a",
455 (double) i
->duration
/1000000,
457 i
->lazy
? "yes" : "no",
458 i
->filename
? i
->filename
: "n/a");
461 static void get_autoload_info_callback(pa_context
*c
, const pa_autoload_info
*i
, int is_last
, void *userdata
) {
463 fprintf(stderr
, "Failed to get autoload information: %s\n", pa_strerror(pa_context_errno(c
)));
479 printf("*** Autoload Entry #%u ***\n"
486 i
->type
== PA_AUTOLOAD_SINK
? "sink" : "source",
491 static void simple_callback(pa_context
*c
, int success
, void *userdata
) {
493 fprintf(stderr
, "Failure: %s\n", pa_strerror(pa_context_errno(c
)));
501 static void index_callback(pa_context
*c
, uint32_t idx
, void *userdata
) {
502 if (idx
== PA_INVALID_INDEX
) {
503 fprintf(stderr
, "Failure: %s\n", pa_strerror(pa_context_errno(c
)));
513 static void stream_state_callback(pa_stream
*s
, void *userdata
) {
516 switch (pa_stream_get_state(s
)) {
517 case PA_STREAM_CREATING
:
518 case PA_STREAM_READY
:
521 case PA_STREAM_TERMINATED
:
525 case PA_STREAM_FAILED
:
527 fprintf(stderr
, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
532 static void stream_write_callback(pa_stream
*s
, size_t length
, void *userdata
) {
535 assert(s
&& length
&& sndfile
);
537 d
= pa_xmalloc(length
);
539 assert(sample_length
>= length
);
540 l
= length
/pa_frame_size(&sample_spec
);
542 if ((sf_readf_float(sndfile
, d
, l
)) != l
) {
544 fprintf(stderr
, "Premature end of file\n");
548 pa_stream_write(s
, d
, length
, pa_xfree
, 0, PA_SEEK_RELATIVE
);
550 sample_length
-= length
;
552 if (sample_length
<= 0) {
553 pa_stream_set_write_callback(sample_stream
, NULL
, NULL
);
554 pa_stream_finish_upload(sample_stream
);
558 static void context_state_callback(pa_context
*c
, void *userdata
) {
560 switch (pa_context_get_state(c
)) {
561 case PA_CONTEXT_CONNECTING
:
562 case PA_CONTEXT_AUTHORIZING
:
563 case PA_CONTEXT_SETTING_NAME
:
566 case PA_CONTEXT_READY
:
570 pa_operation_unref(pa_context_stat(c
, stat_callback
, NULL
));
571 pa_operation_unref(pa_context_get_server_info(c
, get_server_info_callback
, NULL
));
575 pa_operation_unref(pa_context_play_sample(c
, sample_name
, device
, PA_VOLUME_NORM
, simple_callback
, NULL
));
579 pa_operation_unref(pa_context_remove_sample(c
, sample_name
, simple_callback
, NULL
));
583 sample_stream
= pa_stream_new(c
, sample_name
, &sample_spec
, NULL
);
584 assert(sample_stream
);
586 pa_stream_set_state_callback(sample_stream
, stream_state_callback
, NULL
);
587 pa_stream_set_write_callback(sample_stream
, stream_write_callback
, NULL
);
588 pa_stream_connect_upload(sample_stream
, sample_length
);
592 pa_operation_unref(pa_context_exit_daemon(c
, NULL
, NULL
));
597 pa_operation_unref(pa_context_get_module_info_list(c
, get_module_info_callback
, NULL
));
598 pa_operation_unref(pa_context_get_sink_info_list(c
, get_sink_info_callback
, NULL
));
599 pa_operation_unref(pa_context_get_source_info_list(c
, get_source_info_callback
, NULL
));
600 pa_operation_unref(pa_context_get_sink_input_info_list(c
, get_sink_input_info_callback
, NULL
));
601 pa_operation_unref(pa_context_get_source_output_info_list(c
, get_source_output_info_callback
, NULL
));
602 pa_operation_unref(pa_context_get_client_info_list(c
, get_client_info_callback
, NULL
));
603 pa_operation_unref(pa_context_get_sample_info_list(c
, get_sample_info_callback
, NULL
));
604 pa_operation_unref(pa_context_get_autoload_info_list(c
, get_autoload_info_callback
, NULL
));
607 case MOVE_SINK_INPUT
:
608 pa_operation_unref(pa_context_move_sink_input_by_name(c
, sink_input_idx
, sink_name
, simple_callback
, NULL
));
611 case MOVE_SOURCE_OUTPUT
:
612 pa_operation_unref(pa_context_move_source_output_by_name(c
, source_output_idx
, source_name
, simple_callback
, NULL
));
616 pa_operation_unref(pa_context_load_module(c
, module_name
, module_args
, index_callback
, NULL
));
620 pa_operation_unref(pa_context_unload_module(c
, module_index
, simple_callback
, NULL
));
625 pa_operation_unref(pa_context_suspend_sink_by_name(c
, sink_name
, suspend
, simple_callback
, NULL
));
627 pa_operation_unref(pa_context_suspend_sink_by_index(c
, PA_INVALID_INDEX
, suspend
, simple_callback
, NULL
));
632 pa_operation_unref(pa_context_suspend_source_by_name(c
, source_name
, suspend
, simple_callback
, NULL
));
634 pa_operation_unref(pa_context_suspend_source_by_index(c
, PA_INVALID_INDEX
, suspend
, simple_callback
, NULL
));
642 case PA_CONTEXT_TERMINATED
:
646 case PA_CONTEXT_FAILED
:
648 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
653 static void exit_signal_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
654 fprintf(stderr
, "Got SIGINT, exiting.\n");
658 static void help(const char *argv0
) {
660 printf("%s [options] stat\n"
661 "%s [options] list\n"
662 "%s [options] exit\n"
663 "%s [options] upload-sample FILENAME [NAME]\n"
664 "%s [options] play-sample NAME [SINK]\n"
665 "%s [options] remove-sample NAME\n"
666 "%s [options] move-sink-input ID SINK\n"
667 "%s [options] move-source-output ID SOURCE\n"
668 "%s [options] load-module NAME [ARGS ...]\n"
669 "%s [options] unload-module ID\n"
670 "%s [options] suspend-sink [SINK] 1|0\n"
671 "%s [options] suspend-source [SOURCE] 1|0\n\n"
672 " -h, --help Show this help\n"
673 " --version Show version\n\n"
674 " -s, --server=SERVER The name of the server to connect to\n"
675 " -n, --client-name=NAME How to call this client on the server\n",
676 argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
);
679 enum { ARG_VERSION
= 256 };
681 int main(int argc
, char *argv
[]) {
682 pa_mainloop
* m
= NULL
;
685 char *server
= NULL
, *client_name
= NULL
, *bn
;
687 static const struct option long_options
[] = {
688 {"server", 1, NULL
, 's'},
689 {"client-name", 1, NULL
, 'n'},
690 {"version", 0, NULL
, ARG_VERSION
},
691 {"help", 0, NULL
, 'h'},
695 if (!(bn
= strrchr(argv
[0], '/')))
700 while ((c
= getopt_long(argc
, argv
, "s:n:h", long_options
, NULL
)) != -1) {
708 printf("pactl "PACKAGE_VERSION
"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
714 server
= pa_xstrdup(optarg
);
718 pa_xfree(client_name
);
719 client_name
= pa_xstrdup(optarg
);
728 client_name
= pa_xstrdup(bn
);
731 if (!strcmp(argv
[optind
], "stat"))
733 else if (!strcmp(argv
[optind
], "exit"))
735 else if (!strcmp(argv
[optind
], "list"))
737 else if (!strcmp(argv
[optind
], "upload-sample")) {
738 struct SF_INFO sfinfo
;
739 action
= UPLOAD_SAMPLE
;
741 if (optind
+1 >= argc
) {
742 fprintf(stderr
, "Please specify a sample file to load\n");
747 sample_name
= pa_xstrdup(argv
[optind
+2]);
749 char *f
= strrchr(argv
[optind
+1], '/');
759 sample_name
= pa_xstrdup(tmp
);
762 memset(&sfinfo
, 0, sizeof(sfinfo
));
763 if (!(sndfile
= sf_open(argv
[optind
+1], SFM_READ
, &sfinfo
))) {
764 fprintf(stderr
, "Failed to open sound file.\n");
768 sample_spec
.format
= PA_SAMPLE_FLOAT32
;
769 sample_spec
.rate
= sfinfo
.samplerate
;
770 sample_spec
.channels
= sfinfo
.channels
;
772 sample_length
= sfinfo
.frames
*pa_frame_size(&sample_spec
);
773 } else if (!strcmp(argv
[optind
], "play-sample")) {
774 action
= PLAY_SAMPLE
;
775 if (argc
!= optind
+2 && argc
!= optind
+3) {
776 fprintf(stderr
, "You have to specify a sample name to play\n");
780 sample_name
= pa_xstrdup(argv
[optind
+1]);
783 device
= pa_xstrdup(argv
[optind
+2]);
785 } else if (!strcmp(argv
[optind
], "remove-sample")) {
786 action
= REMOVE_SAMPLE
;
787 if (argc
!= optind
+2) {
788 fprintf(stderr
, "You have to specify a sample name to remove\n");
792 sample_name
= pa_xstrdup(argv
[optind
+1]);
793 } else if (!strcmp(argv
[optind
], "move-sink-input")) {
794 action
= MOVE_SINK_INPUT
;
795 if (argc
!= optind
+3) {
796 fprintf(stderr
, "You have to specify a sink input index and a sink\n");
800 sink_input_idx
= atoi(argv
[optind
+1]);
801 sink_name
= pa_xstrdup(argv
[optind
+2]);
802 } else if (!strcmp(argv
[optind
], "move-source-output")) {
803 action
= MOVE_SOURCE_OUTPUT
;
804 if (argc
!= optind
+3) {
805 fprintf(stderr
, "You have to specify a source output index and a source\n");
809 source_output_idx
= atoi(argv
[optind
+1]);
810 source_name
= pa_xstrdup(argv
[optind
+2]);
811 } else if (!strcmp(argv
[optind
], "load-module")) {
816 action
= LOAD_MODULE
;
818 if (argc
<= optind
+1) {
819 fprintf(stderr
, "You have to specify a module name and arguments.\n");
823 module_name
= argv
[optind
+1];
825 for (i
= optind
+2; i
< argc
; i
++)
826 n
+= strlen(argv
[i
])+1;
829 p
= module_args
= pa_xnew0(char, n
);
831 for (i
= optind
+2; i
< argc
; i
++)
832 p
+= sprintf(p
, "%s%s", p
== module_args
? "" : " ", argv
[i
]);
835 } else if (!strcmp(argv
[optind
], "unload-module")) {
836 action
= UNLOAD_MODULE
;
838 if (argc
!= optind
+2) {
839 fprintf(stderr
, "You have to specify a module index\n");
843 module_index
= atoi(argv
[optind
+1]);
845 } else if (!strcmp(argv
[optind
], "suspend-sink")) {
846 action
= SUSPEND_SINK
;
848 if (argc
> optind
+3 || optind
+1 >= argc
) {
849 fprintf(stderr
, "You may not specify more than one sink. You have to specify at least one boolean value.\n");
853 suspend
= !!atoi(argv
[argc
-1]);
856 sink_name
= pa_xstrdup(argv
[optind
+1]);
858 } else if (!strcmp(argv
[optind
], "suspend-source")) {
859 action
= SUSPEND_SOURCE
;
861 if (argc
> optind
+3 || optind
+1 >= argc
) {
862 fprintf(stderr
, "You may not specify more than one source. You have to specify at least one boolean value.\n");
866 suspend
= !!atoi(argv
[argc
-1]);
869 source_name
= pa_xstrdup(argv
[optind
+1]);
873 if (action
== NONE
) {
874 fprintf(stderr
, "No valid command specified.\n");
878 if (!(m
= pa_mainloop_new())) {
879 fprintf(stderr
, "pa_mainloop_new() failed.\n");
883 mainloop_api
= pa_mainloop_get_api(m
);
885 r
= pa_signal_init(mainloop_api
);
887 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
889 signal(SIGPIPE
, SIG_IGN
);
892 if (!(context
= pa_context_new(mainloop_api
, client_name
))) {
893 fprintf(stderr
, "pa_context_new() failed.\n");
897 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
898 pa_context_connect(context
, server
, 0, NULL
);
900 if (pa_mainloop_run(m
, &ret
) < 0) {
901 fprintf(stderr
, "pa_mainloop_run() failed.\n");
907 pa_stream_unref(sample_stream
);
910 pa_context_unref(context
);
922 pa_xfree(sample_name
);
924 pa_xfree(source_name
);
925 pa_xfree(module_args
);
926 pa_xfree(client_name
);