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 != 9
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
;
50 static uint32_t sink_input_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;
72 static void quit(int ret
) {
74 mainloop_api
->quit(mainloop_api
, ret
);
78 static void context_drain_complete(pa_context
*c
, void *userdata
) {
79 pa_context_disconnect(c
);
82 static void drain(void) {
84 if (!(o
= pa_context_drain(context
, context_drain_complete
, NULL
)))
85 pa_context_disconnect(context
);
87 pa_operation_unref(o
);
91 static void complete_action(void) {
98 static void stat_callback(pa_context
*c
, const pa_stat_info
*i
, void *userdata
) {
101 fprintf(stderr
, "Failed to get statistics: %s\n", pa_strerror(pa_context_errno(c
)));
106 pa_bytes_snprint(s
, sizeof(s
), i
->memblock_total_size
);
107 printf("Currently in use: %u blocks containing %s bytes total.\n", i
->memblock_total
, s
);
109 pa_bytes_snprint(s
, sizeof(s
), i
->memblock_allocated_size
);
110 printf("Allocated during whole lifetime: %u blocks containing %s bytes total.\n", i
->memblock_allocated
, s
);
112 pa_bytes_snprint(s
, sizeof(s
), i
->scache_size
);
113 printf("Sample cache size: %s\n", s
);
118 static void get_server_info_callback(pa_context
*c
, const pa_server_info
*i
, void *useerdata
) {
119 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
122 fprintf(stderr
, "Failed to get server information: %s\n", pa_strerror(pa_context_errno(c
)));
127 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
);
129 printf("User name: %s\n"
132 "Server Version: %s\n"
133 "Default Sample Specification: %s\n"
135 "Default Source: %s\n"
142 i
->default_sink_name
,
143 i
->default_source_name
,
149 static void get_sink_info_callback(pa_context
*c
, const pa_sink_info
*i
, int is_last
, void *userdata
) {
150 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
153 fprintf(stderr
, "Failed to get sink information: %s\n", pa_strerror(pa_context_errno(c
)));
169 printf("*** Sink #%u ***\n"
173 "Sample Specification: %s\n"
177 "Monitor Source: %u\n"
178 "Latency: %0.0f usec\n"
184 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
185 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
187 i
->mute
? "muted" : pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
190 i
->flags
& PA_SINK_HW_VOLUME_CTRL
? "HW_VOLUME_CTRL " : "",
191 i
->flags
& PA_SINK_LATENCY
? "LATENCY " : "",
192 i
->flags
& PA_SINK_HARDWARE
? "HARDWARE" : "");
196 static void get_source_info_callback(pa_context
*c
, const pa_source_info
*i
, int is_last
, void *userdata
) {
197 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], t
[32], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
200 fprintf(stderr
, "Failed to get source information: %s\n", pa_strerror(pa_context_errno(c
)));
216 snprintf(t
, sizeof(t
), "%u", i
->monitor_of_sink
);
218 printf("*** Source #%u ***\n"
222 "Sample Specification: %s\n"
226 "Monitor of Sink: %s\n"
227 "Latency: %0.0f usec\n"
233 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
234 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
236 i
->mute
? "muted" : pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
237 i
->monitor_of_sink
!= PA_INVALID_INDEX
? t
: "no",
239 i
->flags
& PA_SOURCE_HW_VOLUME_CTRL
? "HW_VOLUME_CTRL " : "",
240 i
->flags
& PA_SOURCE_LATENCY
? "LATENCY " : "",
241 i
->flags
& PA_SOURCE_HARDWARE
? "HARDWARE" : "");
245 static void get_module_info_callback(pa_context
*c
, const pa_module_info
*i
, int is_last
, void *userdata
) {
249 fprintf(stderr
, "Failed to get module information: %s\n", pa_strerror(pa_context_errno(c
)));
265 snprintf(t
, sizeof(t
), "%u", i
->n_used
);
267 printf("*** Module #%u ***\n"
270 "Usage counter: %s\n"
275 i
->n_used
!= PA_INVALID_INDEX
? t
: "n/a",
276 i
->auto_unload
? "yes" : "no");
279 static void get_client_info_callback(pa_context
*c
, const pa_client_info
*i
, int is_last
, void *userdata
) {
283 fprintf(stderr
, "Failed to get client information: %s\n", pa_strerror(pa_context_errno(c
)));
299 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
301 printf("*** Client #%u ***\n"
304 "Owner Module: %s\n",
308 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a");
311 static void get_sink_input_info_callback(pa_context
*c
, const pa_sink_input_info
*i
, int is_last
, void *userdata
) {
312 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
315 fprintf(stderr
, "Failed to get sink input information: %s\n", pa_strerror(pa_context_errno(c
)));
331 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
332 snprintf(k
, sizeof(k
), "%u", i
->client
);
334 printf("*** Sink Input #%u ***\n"
340 "Sample Specification: %s\n"
343 "Buffer Latency: %0.0f usec\n"
344 "Sink Latency: %0.0f usec\n"
345 "Resample method: %s\n",
349 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
350 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
352 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
353 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
354 pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
355 (double) i
->buffer_usec
,
356 (double) i
->sink_usec
,
357 i
->resample_method
? i
->resample_method
: "n/a");
361 static void get_source_output_info_callback(pa_context
*c
, const pa_source_output_info
*i
, int is_last
, void *userdata
) {
362 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
365 fprintf(stderr
, "Failed to get source output information: %s\n", pa_strerror(pa_context_errno(c
)));
382 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
383 snprintf(k
, sizeof(k
), "%u", i
->client
);
385 printf("*** Source Output #%u ***\n"
391 "Sample Specification: %s\n"
393 "Buffer Latency: %0.0f usec\n"
394 "Source Latency: %0.0f usec\n"
395 "Resample method: %s\n",
399 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
400 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
402 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
403 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
404 (double) i
->buffer_usec
,
405 (double) i
->source_usec
,
406 i
->resample_method
? i
->resample_method
: "n/a");
409 static void get_sample_info_callback(pa_context
*c
, const pa_sample_info
*i
, int is_last
, void *userdata
) {
410 char t
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
413 fprintf(stderr
, "Failed to get sample information: %s\n", pa_strerror(pa_context_errno(c
)));
430 pa_bytes_snprint(t
, sizeof(t
), i
->bytes
);
432 printf("*** Sample #%u ***\n"
435 "Sample Specification: %s\n"
443 pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
444 pa_sample_spec_valid(&i
->sample_spec
) ? pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
) : "n/a",
445 pa_sample_spec_valid(&i
->sample_spec
) ? pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
) : "n/a",
446 (double) i
->duration
/1000000,
448 i
->lazy
? "yes" : "no",
449 i
->filename
? i
->filename
: "n/a");
452 static void get_autoload_info_callback(pa_context
*c
, const pa_autoload_info
*i
, int is_last
, void *userdata
) {
454 fprintf(stderr
, "Failed to get autoload information: %s\n", pa_strerror(pa_context_errno(c
)));
470 printf("*** Autoload Entry #%u ***\n"
477 i
->type
== PA_AUTOLOAD_SINK
? "sink" : "source",
482 static void simple_callback(pa_context
*c
, int success
, void *userdata
) {
484 fprintf(stderr
, "Failure: %s\n", pa_strerror(pa_context_errno(c
)));
492 static void stream_state_callback(pa_stream
*s
, void *userdata
) {
495 switch (pa_stream_get_state(s
)) {
496 case PA_STREAM_CREATING
:
497 case PA_STREAM_READY
:
500 case PA_STREAM_TERMINATED
:
504 case PA_STREAM_FAILED
:
506 fprintf(stderr
, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
511 static void stream_write_callback(pa_stream
*s
, size_t length
, void *userdata
) {
514 assert(s
&& length
&& sndfile
);
516 d
= pa_xmalloc(length
);
518 assert(sample_length
>= length
);
519 l
= length
/pa_frame_size(&sample_spec
);
521 if ((sf_readf_float(sndfile
, d
, l
)) != l
) {
523 fprintf(stderr
, "Premature end of file\n");
527 pa_stream_write(s
, d
, length
, pa_xfree
, 0, PA_SEEK_RELATIVE
);
529 sample_length
-= length
;
531 if (sample_length
<= 0) {
532 pa_stream_set_write_callback(sample_stream
, NULL
, NULL
);
533 pa_stream_finish_upload(sample_stream
);
537 static void context_state_callback(pa_context
*c
, void *userdata
) {
539 switch (pa_context_get_state(c
)) {
540 case PA_CONTEXT_CONNECTING
:
541 case PA_CONTEXT_AUTHORIZING
:
542 case PA_CONTEXT_SETTING_NAME
:
545 case PA_CONTEXT_READY
:
549 pa_operation_unref(pa_context_stat(c
, stat_callback
, NULL
));
550 pa_operation_unref(pa_context_get_server_info(c
, get_server_info_callback
, NULL
));
554 pa_operation_unref(pa_context_play_sample(c
, sample_name
, device
, PA_VOLUME_NORM
, simple_callback
, NULL
));
558 pa_operation_unref(pa_context_remove_sample(c
, sample_name
, simple_callback
, NULL
));
562 sample_stream
= pa_stream_new(c
, sample_name
, &sample_spec
, NULL
);
563 assert(sample_stream
);
565 pa_stream_set_state_callback(sample_stream
, stream_state_callback
, NULL
);
566 pa_stream_set_write_callback(sample_stream
, stream_write_callback
, NULL
);
567 pa_stream_connect_upload(sample_stream
, sample_length
);
571 pa_operation_unref(pa_context_exit_daemon(c
, NULL
, NULL
));
576 pa_operation_unref(pa_context_get_module_info_list(c
, get_module_info_callback
, NULL
));
577 pa_operation_unref(pa_context_get_sink_info_list(c
, get_sink_info_callback
, NULL
));
578 pa_operation_unref(pa_context_get_source_info_list(c
, get_source_info_callback
, NULL
));
579 pa_operation_unref(pa_context_get_sink_input_info_list(c
, get_sink_input_info_callback
, NULL
));
580 pa_operation_unref(pa_context_get_source_output_info_list(c
, get_source_output_info_callback
, NULL
));
581 pa_operation_unref(pa_context_get_client_info_list(c
, get_client_info_callback
, NULL
));
582 pa_operation_unref(pa_context_get_sample_info_list(c
, get_sample_info_callback
, NULL
));
583 pa_operation_unref(pa_context_get_autoload_info_list(c
, get_autoload_info_callback
, NULL
));
586 case MOVE_SINK_INPUT
:
587 pa_operation_unref(pa_context_move_sink_input_by_name(c
, sink_input_idx
, sink_name
, simple_callback
, NULL
));
595 case PA_CONTEXT_TERMINATED
:
599 case PA_CONTEXT_FAILED
:
601 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
606 static void exit_signal_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
607 fprintf(stderr
, "Got SIGINT, exiting.\n");
611 static void help(const char *argv0
) {
613 printf("%s [options] stat\n"
614 "%s [options] list\n"
615 "%s [options] exit\n"
616 "%s [options] upload-sample FILENAME [NAME]\n"
617 "%s [options] play-sample NAME [SINK]\n"
618 "%s [options] move-sink-input NAME [SINK]\n"
619 "%s [options] remove-sample NAME\n\n"
620 " -h, --help Show this help\n"
621 " --version Show version\n\n"
622 " -s, --server=SERVER The name of the server to connect to\n"
623 " -n, --client-name=NAME How to call this client on the server\n",
624 argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
);
627 enum { ARG_VERSION
= 256 };
629 int main(int argc
, char *argv
[]) {
630 pa_mainloop
* m
= NULL
;
633 char *server
= NULL
, *client_name
= NULL
, *bn
;
635 static const struct option long_options
[] = {
636 {"server", 1, NULL
, 's'},
637 {"client-name", 1, NULL
, 'n'},
638 {"version", 0, NULL
, ARG_VERSION
},
639 {"help", 0, NULL
, 'h'},
643 if (!(bn
= strrchr(argv
[0], '/')))
648 while ((c
= getopt_long(argc
, argv
, "s:n:h", long_options
, NULL
)) != -1) {
656 printf("pactl "PACKAGE_VERSION
"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
662 server
= pa_xstrdup(optarg
);
666 pa_xfree(client_name
);
667 client_name
= pa_xstrdup(optarg
);
676 client_name
= pa_xstrdup(bn
);
679 if (!strcmp(argv
[optind
], "stat"))
681 else if (!strcmp(argv
[optind
], "exit"))
683 else if (!strcmp(argv
[optind
], "list"))
685 else if (!strcmp(argv
[optind
], "upload-sample")) {
686 struct SF_INFO sfinfo
;
687 action
= UPLOAD_SAMPLE
;
689 if (optind
+1 >= argc
) {
690 fprintf(stderr
, "Please specify a sample file to load\n");
695 sample_name
= pa_xstrdup(argv
[optind
+2]);
697 char *f
= strrchr(argv
[optind
+1], '/');
707 sample_name
= pa_xstrdup(tmp
);
710 memset(&sfinfo
, 0, sizeof(sfinfo
));
711 if (!(sndfile
= sf_open(argv
[optind
+1], SFM_READ
, &sfinfo
))) {
712 fprintf(stderr
, "Failed to open sound file.\n");
716 sample_spec
.format
= PA_SAMPLE_FLOAT32
;
717 sample_spec
.rate
= sfinfo
.samplerate
;
718 sample_spec
.channels
= sfinfo
.channels
;
720 sample_length
= sfinfo
.frames
*pa_frame_size(&sample_spec
);
721 } else if (!strcmp(argv
[optind
], "play-sample")) {
722 action
= PLAY_SAMPLE
;
723 if (optind
+1 >= argc
) {
724 fprintf(stderr
, "You have to specify a sample name to play\n");
728 sample_name
= pa_xstrdup(argv
[optind
+1]);
731 device
= pa_xstrdup(argv
[optind
+2]);
733 } else if (!strcmp(argv
[optind
], "remove-sample")) {
734 action
= REMOVE_SAMPLE
;
735 if (optind
+1 >= argc
) {
736 fprintf(stderr
, "You have to specify a sample name to remove\n");
740 sample_name
= pa_xstrdup(argv
[optind
+1]);
741 } else if (!strcmp(argv
[optind
], "move-sink-input")) {
742 action
= MOVE_SINK_INPUT
;
743 if (optind
+2 >= argc
) {
744 fprintf(stderr
, "You have to specify a sink input index and a sink\n");
748 sink_input_idx
= atoi(argv
[optind
+1]);
749 sink_name
= pa_xstrdup(argv
[optind
+2]);
753 if (action
== NONE
) {
754 fprintf(stderr
, "No valid command specified.\n");
758 if (!(m
= pa_mainloop_new())) {
759 fprintf(stderr
, "pa_mainloop_new() failed.\n");
763 mainloop_api
= pa_mainloop_get_api(m
);
765 r
= pa_signal_init(mainloop_api
);
767 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
769 signal(SIGPIPE
, SIG_IGN
);
772 if (!(context
= pa_context_new(mainloop_api
, client_name
))) {
773 fprintf(stderr
, "pa_context_new() failed.\n");
777 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
778 pa_context_connect(context
, server
, 0, NULL
);
780 if (pa_mainloop_run(m
, &ret
) < 0) {
781 fprintf(stderr
, "pa_mainloop_run() failed.\n");
787 pa_stream_unref(sample_stream
);
790 pa_context_unref(context
);
802 pa_xfree(sample_name
);