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 != 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 #%u ***\n"
447 i
->type
== PA_AUTOLOAD_SINK
? "sink" : "source",
452 static void simple_callback(struct pa_context
*c
, int success
, void *userdata
) {
454 fprintf(stderr
, "Failure: %s\n", pa_strerror(pa_context_errno(c
)));
462 static void stream_state_callback(struct pa_stream
*s
, void *userdata
) {
465 switch (pa_stream_get_state(s
)) {
466 case PA_STREAM_CREATING
:
467 case PA_STREAM_READY
:
470 case PA_STREAM_TERMINATED
:
474 case PA_STREAM_FAILED
:
476 fprintf(stderr
, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
481 static void stream_write_callback(struct pa_stream
*s
, size_t length
, void *userdata
) {
484 assert(s
&& length
&& sndfile
);
489 assert(sample_length
>= length
);
490 l
= length
/pa_frame_size(&sample_spec
);
492 if ((sf_readf_float(sndfile
, d
, l
)) != l
) {
494 fprintf(stderr
, "Premature end of file\n");
498 pa_stream_write(s
, d
, length
, free
, 0);
500 sample_length
-= length
;
502 if (sample_length
<= 0) {
503 pa_stream_set_write_callback(sample_stream
, NULL
, NULL
);
504 pa_stream_finish_upload(sample_stream
);
508 static void context_state_callback(struct pa_context
*c
, void *userdata
) {
510 switch (pa_context_get_state(c
)) {
511 case PA_CONTEXT_CONNECTING
:
512 case PA_CONTEXT_AUTHORIZING
:
513 case PA_CONTEXT_SETTING_NAME
:
516 case PA_CONTEXT_READY
:
520 pa_operation_unref(pa_context_stat(c
, stat_callback
, NULL
));
521 pa_operation_unref(pa_context_get_server_info(c
, get_server_info_callback
, NULL
));
525 pa_operation_unref(pa_context_play_sample(c
, sample_name
, device
, PA_VOLUME_NORM
, simple_callback
, NULL
));
529 pa_operation_unref(pa_context_remove_sample(c
, sample_name
, simple_callback
, NULL
));
533 sample_stream
= pa_stream_new(c
, sample_name
, &sample_spec
);
534 assert(sample_stream
);
536 pa_stream_set_state_callback(sample_stream
, stream_state_callback
, NULL
);
537 pa_stream_set_write_callback(sample_stream
, stream_write_callback
, NULL
);
538 pa_stream_connect_upload(sample_stream
, sample_length
);
542 pa_context_exit_daemon(c
);
547 pa_operation_unref(pa_context_get_module_info_list(c
, get_module_info_callback
, NULL
));
548 pa_operation_unref(pa_context_get_sink_info_list(c
, get_sink_info_callback
, NULL
));
549 pa_operation_unref(pa_context_get_source_info_list(c
, get_source_info_callback
, NULL
));
550 pa_operation_unref(pa_context_get_sink_input_info_list(c
, get_sink_input_info_callback
, NULL
));
551 pa_operation_unref(pa_context_get_source_output_info_list(c
, get_source_output_info_callback
, NULL
));
552 pa_operation_unref(pa_context_get_client_info_list(c
, get_client_info_callback
, NULL
));
553 pa_operation_unref(pa_context_get_sample_info_list(c
, get_sample_info_callback
, NULL
));
554 pa_operation_unref(pa_context_get_autoload_info_list(c
, get_autoload_info_callback
, NULL
));
562 case PA_CONTEXT_TERMINATED
:
566 case PA_CONTEXT_FAILED
:
568 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
573 static void exit_signal_callback(struct pa_mainloop_api
*m
, struct pa_signal_event
*e
, int sig
, void *userdata
) {
574 fprintf(stderr
, "Got SIGINT, exiting.\n");
578 static void help(const char *argv0
) {
580 printf("%s [options] stat\n"
581 "%s [options] list\n"
582 "%s [options] exit\n"
583 "%s [options] upload-sample FILENAME [NAME]\n"
584 "%s [options] play-sample NAME [SINK]\n"
585 "%s [options] remove-sample NAME\n\n"
586 " -h, --help Show this help\n"
587 " --version Show version\n\n"
588 " -s, --server=SERVER The name of the server to connect to\n"
589 " -n, --client-name=NAME How to call this client on the server\n",
590 argv0
, argv0
, argv0
, argv0
, argv0
, argv0
);
593 enum { ARG_VERSION
= 256 };
595 int main(int argc
, char *argv
[]) {
596 struct pa_mainloop
* m
= NULL
;
599 char *server
= NULL
, *client_name
= NULL
, *bn
;
601 static const struct option long_options
[] = {
602 {"server", 1, NULL
, 's'},
603 {"client-name", 1, NULL
, 'n'},
604 {"version", 0, NULL
, ARG_VERSION
},
605 {"help", 0, NULL
, 'h'},
609 if (!(bn
= strrchr(argv
[0], '/')))
614 while ((c
= getopt_long(argc
, argv
, "s:n:h", long_options
, NULL
)) != -1) {
622 printf("pactl "PACKAGE_VERSION
"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
628 server
= strdup(optarg
);
633 client_name
= strdup(optarg
);
642 client_name
= strdup(bn
);
645 if (!strcmp(argv
[optind
], "stat"))
647 else if (!strcmp(argv
[optind
], "exit"))
649 else if (!strcmp(argv
[optind
], "list"))
651 else if (!strcmp(argv
[optind
], "upload-sample")) {
652 struct SF_INFO sfinfo
;
653 action
= UPLOAD_SAMPLE
;
655 if (optind
+1 >= argc
) {
656 fprintf(stderr
, "Please specify a sample file to load\n");
661 sample_name
= strdup(argv
[optind
+2]);
663 char *f
= strrchr(argv
[optind
+1], '/');
673 sample_name
= strdup(tmp
);
676 memset(&sfinfo
, 0, sizeof(sfinfo
));
677 if (!(sndfile
= sf_open(argv
[optind
+1], SFM_READ
, &sfinfo
))) {
678 fprintf(stderr
, "Failed to open sound file.\n");
682 sample_spec
.format
= PA_SAMPLE_FLOAT32
;
683 sample_spec
.rate
= sfinfo
.samplerate
;
684 sample_spec
.channels
= sfinfo
.channels
;
686 sample_length
= sfinfo
.frames
*pa_frame_size(&sample_spec
);
687 } else if (!strcmp(argv
[optind
], "play-sample")) {
688 action
= PLAY_SAMPLE
;
689 if (optind
+1 >= argc
) {
690 fprintf(stderr
, "You have to specify a sample name to play\n");
694 sample_name
= strdup(argv
[optind
+1]);
697 device
= strdup(argv
[optind
+2]);
699 } else if (!strcmp(argv
[optind
], "remove-sample")) {
700 action
= REMOVE_SAMPLE
;
701 if (optind
+1 >= argc
) {
702 fprintf(stderr
, "You have to specify a sample name to remove\n");
706 sample_name
= strdup(argv
[optind
+1]);
710 if (action
== NONE
) {
711 fprintf(stderr
, "No valid command specified.\n");
715 if (!(m
= pa_mainloop_new())) {
716 fprintf(stderr
, "pa_mainloop_new() failed.\n");
720 mainloop_api
= pa_mainloop_get_api(m
);
722 r
= pa_signal_init(mainloop_api
);
724 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
725 signal(SIGPIPE
, SIG_IGN
);
727 if (!(context
= pa_context_new(mainloop_api
, client_name
))) {
728 fprintf(stderr
, "pa_context_new() failed.\n");
732 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
733 pa_context_connect(context
, server
, 1, NULL
);
735 if (pa_mainloop_run(m
, &ret
) < 0) {
736 fprintf(stderr
, "pa_mainloop_run() failed.\n");
742 pa_stream_unref(sample_stream
);
745 pa_context_unref(context
);