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 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 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 Lesser 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 != 7
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"
328 "Resample method: %s\n",
331 i->owner_module != PA_INVALID_INDEX ? t : "n/a",
332 i->client != PA_INVALID_INDEX ? k : "n/a",
335 i->volume, pa_volume_to_dB(i->volume),
336 (double) i->buffer_usec,
337 (double) i->sink_usec,
338 i->resample_method ? i->resample_method : "n/a");
341 static void get_source_output_info_callback(struct pa_context *c, const struct pa_source_output_info *i, int is_last, void *userdata) {
342 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX];
345 fprintf(stderr, "Failed to get source output information: %s\n", pa_strerror(pa_context_errno(c)));
361 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec);
362 snprintf(t, sizeof(t), "%u", i->owner_module);
363 snprintf(k, sizeof(k), "%u", i->client);
365 printf("*** Source Output #%u ***\n"
370 "Sample Specification: %s\n"
371 "Buffer Latency: %0.0f usec\n"
372 "Source Latency: %0.0f usec\n"
373 "Resample method: %s\n",
376 i->owner_module != PA_INVALID_INDEX ? t : "n/a",
377 i->client != PA_INVALID_INDEX ? k : "n/a",
380 (double) i->buffer_usec,
381 (double) i->source_usec,
382 i->resample_method ? i->resample_method : "n/a");
385 static void get_sample_info_callback(struct pa_context *c, const struct pa_sample_info *i, int is_last, void *userdata) {
386 char t[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX];
389 fprintf(stderr, "Failed to get sample information: %s\n", pa_strerror(pa_context_errno(c)));
405 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec);
406 pa_bytes_snprint(t, sizeof(t), i->bytes);
408 printf("*** Sample #%u ***\n"
410 "Volume: 0x%03x (%0.2f dB)\n"
411 "Sample Specification: %s\n"
418 i->volume, pa_volume_to_dB(i->volume),
419 pa_sample_spec_valid(&i->sample_spec) ? s : "n/a",
420 (double) i->duration/1000000,
422 i->lazy ? "yes" : "no",
423 i->filename ? i->filename : "n/a");
426 static void get_autoload_info_callback(struct pa_context *c, const struct pa_autoload_info *i, int is_last, void *userdata) {
428 fprintf(stderr, "Failed to get autoload information: %s\n", pa_strerror(pa_context_errno(c)));
444 printf("*** Autoload Entry #%u ***\n"
451 i->type == PA_AUTOLOAD_SINK ? "sink" : "source",
456 static void simple_callback(struct pa_context *c, int success, void *userdata) {
458 fprintf(stderr, "Failure: %s\n", pa_strerror(pa_context_errno(c)));
466 static void stream_state_callback(struct pa_stream *s, void *userdata) {
469 switch (pa_stream_get_state(s)) {
470 case PA_STREAM_CREATING:
471 case PA_STREAM_READY:
474 case PA_STREAM_TERMINATED:
478 case PA_STREAM_FAILED:
480 fprintf(stderr, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
485 static void stream_write_callback(struct pa_stream *s, size_t length, void *userdata) {
488 assert(s && length && sndfile);
493 assert(sample_length >= length);
494 l = length/pa_frame_size(&sample_spec);
496 if ((sf_readf_float(sndfile, d, l)) != l) {
498 fprintf(stderr, "Premature end of file\n");
502 pa_stream_write(s, d, length, free, 0);
504 sample_length -= length;
506 if (sample_length <= 0) {
507 pa_stream_set_write_callback(sample_stream, NULL, NULL);
508 pa_stream_finish_upload(sample_stream);
512 static void context_state_callback(struct pa_context *c, void *userdata) {
514 switch (pa_context_get_state(c)) {
515 case PA_CONTEXT_CONNECTING:
516 case PA_CONTEXT_AUTHORIZING:
517 case PA_CONTEXT_SETTING_NAME:
520 case PA_CONTEXT_READY:
524 pa_operation_unref(pa_context_stat(c, stat_callback, NULL));
525 pa_operation_unref(pa_context_get_server_info(c, get_server_info_callback, NULL));
529 pa_operation_unref(pa_context_play_sample(c, sample_name, device, PA_VOLUME_NORM, simple_callback, NULL));
533 pa_operation_unref(pa_context_remove_sample(c, sample_name, simple_callback, NULL));
537 sample_stream = pa_stream_new(c, sample_name, &sample_spec);
538 assert(sample_stream);
540 pa_stream_set_state_callback(sample_stream, stream_state_callback, NULL);
541 pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL);
542 pa_stream_connect_upload(sample_stream, sample_length);
546 pa_context_exit_daemon(c);
551 pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL));
552 pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL));
553 pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL));
554 pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL));
555 pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL));
556 pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL));
557 pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL));
558 pa_operation_unref(pa_context_get_autoload_info_list(c, get_autoload_info_callback, NULL));
566 case PA_CONTEXT_TERMINATED:
570 case PA_CONTEXT_FAILED:
572 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
577 static void exit_signal_callback(struct pa_mainloop_api *m, struct pa_signal_event *e, int sig, void *userdata) {
578 fprintf(stderr, "Got SIGINT, exiting.\n");
582 static void help(const char *argv0) {
584 printf("%s [options] stat\n"
585 "%s [options] list\n"
586 "%s [options] exit\n"
587 "%s [options] upload-sample FILENAME [NAME]\n"
588 "%s [options] play-sample NAME [SINK]\n"
589 "%s [options] remove-sample NAME\n\n"
590 " -h, --help Show this help\n"
591 " --version Show version\n\n"
592 " -s, --server=SERVER The name of the server to connect to\n"
593 " -n, --client-name=NAME How to call this client on the server\n",
594 argv0, argv0, argv0, argv0, argv0, argv0);
597 enum { ARG_VERSION = 256 };
599 int main(int argc, char *argv[]) {
600 struct pa_mainloop* m = NULL;
603 char *server = NULL, *client_name = NULL, *bn;
605 static const struct option long_options[] = {
606 {"server", 1, NULL, 's'},
607 {"client-name", 1, NULL, 'n'},
608 {"version", 0, NULL, ARG_VERSION},
609 {"help", 0, NULL, 'h'},
613 if (!(bn = strrchr(argv[0], '/')))
618 while ((c = getopt_long(argc, argv, "s:n:h", long_options, NULL)) != -1) {
626 printf("pactl "PACKAGE_VERSION"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
632 server = strdup(optarg);
637 client_name = strdup(optarg);
646 client_name = strdup(bn);
649 if (!strcmp(argv[optind], "stat"))
651 else if (!strcmp(argv[optind], "exit"))
653 else if (!strcmp(argv[optind], "list"))
655 else if (!strcmp(argv[optind], "upload-sample")) {
656 struct SF_INFO sfinfo;
657 action = UPLOAD_SAMPLE;
659 if (optind+1 >= argc) {
660 fprintf(stderr, "Please specify a sample file to load\n");
665 sample_name = strdup(argv[optind+2]);
667 char *f = strrchr(argv[optind+1], '/');
677 sample_name = strdup(tmp);
680 memset(&sfinfo, 0, sizeof(sfinfo));
681 if (!(sndfile = sf_open(argv[optind+1], SFM_READ, &sfinfo))) {
682 fprintf(stderr, "Failed to open sound file.\n");
686 sample_spec.format = PA_SAMPLE_FLOAT32;
687 sample_spec.rate = sfinfo.samplerate;
688 sample_spec.channels = sfinfo.channels;
690 sample_length = sfinfo.frames*pa_frame_size(&sample_spec);
691 } else if (!strcmp(argv[optind], "play-sample")) {
692 action = PLAY_SAMPLE;
693 if (optind+1 >= argc) {
694 fprintf(stderr, "You have to specify a sample name to play\n");
698 sample_name = strdup(argv[optind+1]);
701 device = strdup(argv[optind+2]);
703 } else if (!strcmp(argv[optind], "remove-sample")) {
704 action = REMOVE_SAMPLE;
705 if (optind+1 >= argc) {
706 fprintf(stderr, "You have to specify a sample name to remove\n");
710 sample_name = strdup(argv[optind+1]);
714 if (action == NONE) {
715 fprintf(stderr, "No valid command specified.\n");
719 if (!(m = pa_mainloop_new())) {
720 fprintf(stderr, "pa_mainloop_new() failed.\n");
724 mainloop_api = pa_mainloop_get_api(m);
726 r = pa_signal_init(mainloop_api);
728 pa_signal_new(SIGINT, exit_signal_callback, NULL);
729 signal(SIGPIPE, SIG_IGN);
731 if (!(context = pa_context_new(mainloop_api, client_name))) {
732 fprintf(stderr, "pa_context_new() failed.\n");
736 pa_context_set_state_callback(context, context_state_callback, NULL);
737 pa_context_connect(context, server, 1, NULL);
739 if (pa_mainloop_run(m, &ret) < 0) {
740 fprintf(stderr, "pa_mainloop_run() failed.\n");
746 pa_stream_unref(sample_stream);
749 pa_context_unref(context);