4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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 polypaudio 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 General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
38 #include <polyp/polyplib.h>
39 #include <polyp/polyplib-error.h>
40 #include <polyp/mainloop.h>
41 #include <polyp/mainloop-signal.h>
42 #include <polyp/sample.h>
44 #if PA_API_VERSION != PA_API_VERSION_0_6
45 #error Invalid Polypaudio API version
50 static struct pa_context *context = NULL;
51 static struct pa_mainloop_api *mainloop_api = NULL;
53 static char *device = NULL, *sample_name = NULL;
55 static SNDFILE *sndfile = NULL;
56 static struct pa_stream *sample_stream = NULL;
57 static struct pa_sample_spec sample_spec;
58 static size_t sample_length = 0;
60 static int actions = 1;
74 static void quit(int ret) {
76 mainloop_api->quit(mainloop_api, ret);
80 static void context_drain_complete(struct pa_context *c, void *userdata) {
81 pa_context_disconnect(c);
84 static void drain(void) {
85 struct pa_operation *o;
86 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
87 pa_context_disconnect(context);
89 pa_operation_unref(o);
93 static void complete_action(void) {
100 static void stat_callback(struct pa_context *c, const struct pa_stat_info *i, void *userdata) {
103 fprintf(stderr, "Failed to get statistics: %s\n", pa_strerror(pa_context_errno(c)));
108 pa_bytes_snprint(s, sizeof(s), i->memblock_total_size);
109 printf("Currently in use: %u blocks containing %s bytes total.\n", i->memblock_total, s);
111 pa_bytes_snprint(s, sizeof(s), i->memblock_allocated_size);
112 printf("Allocated during whole lifetime: %u blocks containing %s bytes total.\n", i->memblock_allocated, s);
114 pa_bytes_snprint(s, sizeof(s), i->scache_size);
115 printf("Sample cache size: %s\n", s);
120 static void get_server_info_callback(struct pa_context *c, const struct pa_server_info *i, void *useerdata) {
121 char s[PA_SAMPLE_SPEC_SNPRINT_MAX];
124 fprintf(stderr, "Failed to get server information: %s\n", pa_strerror(pa_context_errno(c)));
129 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec);
131 printf("User name: %s\n"
134 "Server Version: %s\n"
135 "Default Sample Specification: %s\n"
137 "Default Source: %s\n",
143 i->default_sink_name,
144 i->default_source_name);
149 static void get_sink_info_callback(struct pa_context *c, const struct pa_sink_info *i, int is_last, void *userdata) {
150 char s[PA_SAMPLE_SPEC_SNPRINT_MAX];
153 fprintf(stderr, "Failed to get sink information: %s\n", pa_strerror(pa_context_errno(c)));
169 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec);
171 printf("*** Sink #%u ***\n"
174 "Sample Specification: %s\n"
176 "Volume: 0x%03x (%0.2f dB)\n"
177 "Monitor Source: %u\n"
178 "Latency: %0.0f usec\n",
184 i->volume, pa_volume_to_dB(i->volume),
186 (double) i->latency);
189 static void get_source_info_callback(struct pa_context *c, const struct pa_source_info *i, int is_last, void *userdata) {
190 char s[PA_SAMPLE_SPEC_SNPRINT_MAX], t[32];
193 fprintf(stderr, "Failed to get source information: %s\n", pa_strerror(pa_context_errno(c)));
209 snprintf(t, sizeof(t), "%u", i->monitor_of_sink);
211 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec);
213 printf("*** Source #%u ***\n"
216 "Sample Specification: %s\n"
218 "Monitor of Sink: %s\n"
219 "Latency: %0.0f usec\n",
225 i->monitor_of_sink != PA_INVALID_INDEX ? t : "no",
226 (double) i->latency);
229 static void get_module_info_callback(struct pa_context *c, const struct pa_module_info *i, int is_last, void *userdata) {
233 fprintf(stderr, "Failed to get module information: %s\n", pa_strerror(pa_context_errno(c)));
249 snprintf(t, sizeof(t), "%u", i->n_used);
251 printf("*** Module #%u ***\n"
254 "Usage counter: %s\n"
259 i->n_used != PA_INVALID_INDEX ? t : "n/a",
260 i->auto_unload ? "yes" : "no");
263 static void get_client_info_callback(struct pa_context *c, const struct pa_client_info *i, int is_last, void *userdata) {
267 fprintf(stderr, "Failed to get client information: %s\n", pa_strerror(pa_context_errno(c)));
283 snprintf(t, sizeof(t), "%u", i->owner_module);
285 printf("*** Client #%u ***\n"
288 "Protocol Name: %s\n",
291 i->owner_module != PA_INVALID_INDEX ? t : "n/a",
295 static void get_sink_input_info_callback(struct pa_context *c, const struct pa_sink_input_info *i, int is_last, void *userdata) {
296 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX];
299 fprintf(stderr, "Failed to get sink input information: %s\n", pa_strerror(pa_context_errno(c)));
315 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec);
316 snprintf(t, sizeof(t), "%u", i->owner_module);
317 snprintf(k, sizeof(k), "%u", i->client);
319 printf("*** Sink Input #%u ***\n"
324 "Sample Specification: %s\n"
325 "Volume: 0x%03x (%0.2f dB)\n"
326 "Buffer Latency: %0.0f usec\n"
327 "Sink Latency: %0.0f usec\n",
330 i->owner_module != PA_INVALID_INDEX ? t : "n/a",
331 i->client != PA_INVALID_INDEX ? k : "n/a",
334 i->volume, pa_volume_to_dB(i->volume),
335 (double) i->buffer_usec,
336 (double) i->sink_usec);
339 static void get_source_output_info_callback(struct pa_context *c, const struct pa_source_output_info *i, int is_last, void *userdata) {
340 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX];
343 fprintf(stderr, "Failed to get source output information: %s\n", pa_strerror(pa_context_errno(c)));
359 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec);
360 snprintf(t, sizeof(t), "%u", i->owner_module);
361 snprintf(k, sizeof(k), "%u", i->client);
363 printf("*** Source Output #%u ***\n"
368 "Sample Specification: %s\n"
369 "Buffer Latency: %0.0f usec\n"
370 "Source Latency: %0.0f usec\n",
373 i->owner_module != PA_INVALID_INDEX ? t : "n/a",
374 i->client != PA_INVALID_INDEX ? k : "n/a",
377 (double) i->buffer_usec,
378 (double) i->source_usec);
381 static void get_sample_info_callback(struct pa_context *c, const struct pa_sample_info *i, int is_last, void *userdata) {
382 char t[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX];
385 fprintf(stderr, "Failed to get sample information: %s\n", pa_strerror(pa_context_errno(c)));
401 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec);
402 pa_bytes_snprint(t, sizeof(t), i->bytes);
404 printf("*** Sample #%u ***\n"
406 "Volume: 0x%03x (%0.2f dB)\n"
407 "Sample Specification: %s\n"
414 i->volume, pa_volume_to_dB(i->volume),
415 pa_sample_spec_valid(&i->sample_spec) ? s : "n/a",
416 (double) i->duration/1000000,
418 i->lazy ? "yes" : "no",
419 i->filename ? i->filename : "n/a");
422 static void get_autoload_info_callback(struct pa_context *c, const struct pa_autoload_info *i, int is_last, void *userdata) {
424 fprintf(stderr, "Failed to get autoload information: %s\n", pa_strerror(pa_context_errno(c)));
440 printf("*** Autoload Entry ***\n"
446 i->type == PA_AUTOLOAD_SINK ? "sink" : "source",
451 static void simple_callback(struct pa_context *c, int success, void *userdata) {
453 fprintf(stderr, "Failure: %s\n", pa_strerror(pa_context_errno(c)));
461 static void stream_state_callback(struct pa_stream *s, void *userdata) {
464 switch (pa_stream_get_state(s)) {
465 case PA_STREAM_CREATING:
466 case PA_STREAM_READY:
469 case PA_STREAM_TERMINATED:
473 case PA_STREAM_FAILED:
475 fprintf(stderr, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
480 static void stream_write_callback(struct pa_stream *s, size_t length, void *userdata) {
483 assert(s && length && sndfile);
488 assert(sample_length >= length);
489 l = length/pa_frame_size(&sample_spec);
491 if ((sf_readf_float(sndfile, d, l)) != l) {
493 fprintf(stderr, "Premature end of file\n");
497 pa_stream_write(s, d, length, free, 0);
499 sample_length -= length;
501 if (sample_length <= 0) {
502 pa_stream_set_write_callback(sample_stream, NULL, NULL);
503 pa_stream_finish_upload(sample_stream);
507 static void context_state_callback(struct pa_context *c, void *userdata) {
509 switch (pa_context_get_state(c)) {
510 case PA_CONTEXT_CONNECTING:
511 case PA_CONTEXT_AUTHORIZING:
512 case PA_CONTEXT_SETTING_NAME:
515 case PA_CONTEXT_READY:
519 pa_operation_unref(pa_context_stat(c, stat_callback, NULL));
520 pa_operation_unref(pa_context_get_server_info(c, get_server_info_callback, NULL));
524 pa_operation_unref(pa_context_play_sample(c, sample_name, device, PA_VOLUME_NORM, simple_callback, NULL));
528 pa_operation_unref(pa_context_remove_sample(c, sample_name, simple_callback, NULL));
532 sample_stream = pa_stream_new(c, sample_name, &sample_spec);
533 assert(sample_stream);
535 pa_stream_set_state_callback(sample_stream, stream_state_callback, NULL);
536 pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL);
537 pa_stream_connect_upload(sample_stream, sample_length);
541 pa_context_exit_daemon(c);
546 pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL));
547 pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL));
548 pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL));
549 pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL));
550 pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL));
551 pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL));
552 pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL));
553 pa_operation_unref(pa_context_get_autoload_info_list(c, get_autoload_info_callback, NULL));
561 case PA_CONTEXT_TERMINATED:
565 case PA_CONTEXT_FAILED:
567 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
572 static void exit_signal_callback(struct pa_mainloop_api *m, struct pa_signal_event *e, int sig, void *userdata) {
573 fprintf(stderr, "Got SIGINT, exiting.\n");
577 static void help(const char *argv0) {
579 printf("%s [options] stat\n"
580 "%s [options] list\n"
581 "%s [options] exit\n"
582 "%s [options] upload-sample FILENAME [NAME]\n"
583 "%s [options] play-sample NAME [SINK]\n"
584 "%s [options] remove-sample NAME\n\n"
585 " -h, --help Show this help\n"
586 " --version Show version\n\n"
587 " -s, --server=SERVER The name of the server to connect to\n"
588 " -n, --client-name=NAME How to call this client on the server\n",
589 argv0, argv0, argv0, argv0, argv0, argv0);
592 enum { ARG_VERSION = 256 };
594 int main(int argc, char *argv[]) {
595 struct pa_mainloop* m = NULL;
598 char *server = NULL, *client_name = NULL, *bn;
600 static const struct option long_options[] = {
601 {"server", 1, NULL, 's'},
602 {"client-name", 1, NULL, 'n'},
603 {"version", 0, NULL, ARG_VERSION},
604 {"help", 0, NULL, 'h'},
608 if (!(bn = strrchr(argv[0], '/')))
613 while ((c = getopt_long(argc, argv, "s:n:h", long_options, NULL)) != -1) {
621 printf("pactl "PACKAGE_VERSION"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
627 server = strdup(optarg);
632 client_name = strdup(optarg);
641 client_name = strdup(bn);
644 if (!strcmp(argv[optind], "stat"))
646 else if (!strcmp(argv[optind], "exit"))
648 else if (!strcmp(argv[optind], "list"))
650 else if (!strcmp(argv[optind], "upload-sample")) {
651 struct SF_INFO sfinfo;
652 action = UPLOAD_SAMPLE;
654 if (optind+1 >= argc) {
655 fprintf(stderr, "Please specify a sample file to load\n");
660 sample_name = strdup(argv[optind+2]);
662 char *f = strrchr(argv[optind+1], '/');
672 sample_name = strdup(tmp);
675 memset(&sfinfo, 0, sizeof(sfinfo));
676 if (!(sndfile = sf_open(argv[optind+1], SFM_READ, &sfinfo))) {
677 fprintf(stderr, "Failed to open sound file.\n");
681 sample_spec.format = PA_SAMPLE_FLOAT32;
682 sample_spec.rate = sfinfo.samplerate;
683 sample_spec.channels = sfinfo.channels;
685 sample_length = sfinfo.frames*pa_frame_size(&sample_spec);
686 } else if (!strcmp(argv[optind], "play-sample")) {
687 action = PLAY_SAMPLE;
688 if (optind+1 >= argc) {
689 fprintf(stderr, "You have to specify a sample name to play\n");
693 sample_name = strdup(argv[optind+1]);
696 device = strdup(argv[optind+2]);
698 } else if (!strcmp(argv[optind], "remove-sample")) {
699 action = REMOVE_SAMPLE;
700 if (optind+1 >= argc) {
701 fprintf(stderr, "You have to specify a sample name to remove\n");
705 sample_name = strdup(argv[optind+1]);
709 if (action == NONE) {
710 fprintf(stderr, "No valid command specified.\n");
714 if (!(m = pa_mainloop_new())) {
715 fprintf(stderr, "pa_mainloop_new() failed.\n");
719 mainloop_api = pa_mainloop_get_api(m);
721 r = pa_signal_init(mainloop_api);
723 pa_signal_new(SIGINT, exit_signal_callback, NULL);
724 signal(SIGPIPE, SIG_IGN);
726 if (!(context = pa_context_new(mainloop_api, client_name))) {
727 fprintf(stderr, "pa_context_new() failed.\n");
731 pa_context_set_state_callback(context, context_state_callback, NULL);
732 pa_context_connect(context, server, 1, NULL);
734 if (pa_mainloop_run(m, &ret) < 0) {
735 fprintf(stderr, "pa_mainloop_run() failed.\n");
741 pa_stream_unref(sample_stream);
744 pa_context_unref(context);