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 != 8
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"
144 i
->default_sink_name
,
145 i
->default_source_name
,
151 static void get_sink_info_callback(struct pa_context
*c
, const struct pa_sink_info
*i
, int is_last
, void *userdata
) {
152 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], tid
[5];
155 fprintf(stderr
, "Failed to get sink information: %s\n", pa_strerror(pa_context_errno(c
)));
171 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
);
173 printf("*** Sink #%u ***\n"
177 "Sample Specification: %s\n"
179 "Volume: 0x%03x (%0.2f dB)\n"
180 "Monitor Source: %u\n"
181 "Latency: %0.0f usec\n",
184 pa_typeid_to_string(i
->_typeid
, tid
, sizeof(tid
)),
188 i
->volume
, pa_volume_to_dB(i
->volume
),
190 (double) i
->latency
);
194 static void get_source_info_callback(struct pa_context
*c
, const struct pa_source_info
*i
, int is_last
, void *userdata
) {
195 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], t
[32], tid
[5];
198 fprintf(stderr
, "Failed to get source information: %s\n", pa_strerror(pa_context_errno(c
)));
214 snprintf(t
, sizeof(t
), "%u", i
->monitor_of_sink
);
216 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
);
218 printf("*** Source #%u ***\n"
222 "Sample Specification: %s\n"
224 "Monitor of Sink: %s\n"
225 "Latency: %0.0f usec\n",
227 pa_typeid_to_string(i
->_typeid
, tid
, sizeof(tid
)),
232 i
->monitor_of_sink
!= PA_INVALID_INDEX
? t
: "no",
233 (double) i
->latency
);
237 static void get_module_info_callback(struct pa_context
*c
, const struct pa_module_info
*i
, int is_last
, void *userdata
) {
241 fprintf(stderr
, "Failed to get module information: %s\n", pa_strerror(pa_context_errno(c
)));
257 snprintf(t
, sizeof(t
), "%u", i
->n_used
);
259 printf("*** Module #%u ***\n"
262 "Usage counter: %s\n"
267 i
->n_used
!= PA_INVALID_INDEX
? t
: "n/a",
268 i
->auto_unload
? "yes" : "no");
271 static void get_client_info_callback(struct pa_context
*c
, const struct pa_client_info
*i
, int is_last
, void *userdata
) {
275 fprintf(stderr
, "Failed to get client information: %s\n", pa_strerror(pa_context_errno(c
)));
291 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
293 printf("*** Client #%u ***\n"
296 "Owner Module: %s\n",
299 pa_typeid_to_string(i
->_typeid
, tid
, sizeof(tid
)),
300 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a");
303 static void get_sink_input_info_callback(struct pa_context
*c
, const struct pa_sink_input_info
*i
, int is_last
, void *userdata
) {
304 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], tid
[5];
307 fprintf(stderr
, "Failed to get sink input information: %s\n", pa_strerror(pa_context_errno(c
)));
323 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
);
324 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
325 snprintf(k
, sizeof(k
), "%u", i
->client
);
327 printf("*** Sink Input #%u ***\n"
333 "Sample Specification: %s\n"
334 "Volume: 0x%03x (%0.2f dB)\n"
335 "Buffer Latency: %0.0f usec\n"
336 "Sink Latency: %0.0f usec\n"
337 "Resample method: %s\n",
340 pa_typeid_to_string(i
->_typeid
, tid
, sizeof(tid
)),
341 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
342 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
345 i
->volume
, pa_volume_to_dB(i
->volume
),
346 (double) i
->buffer_usec
,
347 (double) i
->sink_usec
,
348 i
->resample_method
? i
->resample_method
: "n/a");
352 static void get_source_output_info_callback(struct pa_context
*c
, const struct pa_source_output_info
*i
, int is_last
, void *userdata
) {
353 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], tid
[5];
356 fprintf(stderr
, "Failed to get source output information: %s\n", pa_strerror(pa_context_errno(c
)));
372 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
);
373 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
374 snprintf(k
, sizeof(k
), "%u", i
->client
);
376 printf("*** Source Output #%u ***\n"
382 "Sample Specification: %s\n"
383 "Buffer Latency: %0.0f usec\n"
384 "Source Latency: %0.0f usec\n"
385 "Resample method: %s\n",
388 pa_typeid_to_string(i
->_typeid
, tid
, sizeof(tid
)),
389 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
390 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
393 (double) i
->buffer_usec
,
394 (double) i
->source_usec
,
395 i
->resample_method
? i
->resample_method
: "n/a");
398 static void get_sample_info_callback(struct pa_context
*c
, const struct pa_sample_info
*i
, int is_last
, void *userdata
) {
399 char t
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
402 fprintf(stderr
, "Failed to get sample information: %s\n", pa_strerror(pa_context_errno(c
)));
418 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
);
419 pa_bytes_snprint(t
, sizeof(t
), i
->bytes
);
421 printf("*** Sample #%u ***\n"
423 "Volume: 0x%03x (%0.2f dB)\n"
424 "Sample Specification: %s\n"
431 i
->volume
, pa_volume_to_dB(i
->volume
),
432 pa_sample_spec_valid(&i
->sample_spec
) ? s
: "n/a",
433 (double) i
->duration
/1000000,
435 i
->lazy
? "yes" : "no",
436 i
->filename
? i
->filename
: "n/a");
439 static void get_autoload_info_callback(struct pa_context
*c
, const struct pa_autoload_info
*i
, int is_last
, void *userdata
) {
441 fprintf(stderr
, "Failed to get autoload information: %s\n", pa_strerror(pa_context_errno(c
)));
457 printf("*** Autoload Entry #%u ***\n"
464 i
->type
== PA_AUTOLOAD_SINK
? "sink" : "source",
469 static void simple_callback(struct pa_context
*c
, int success
, void *userdata
) {
471 fprintf(stderr
, "Failure: %s\n", pa_strerror(pa_context_errno(c
)));
479 static void stream_state_callback(struct pa_stream
*s
, void *userdata
) {
482 switch (pa_stream_get_state(s
)) {
483 case PA_STREAM_CREATING
:
484 case PA_STREAM_READY
:
487 case PA_STREAM_TERMINATED
:
491 case PA_STREAM_FAILED
:
493 fprintf(stderr
, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
498 static void stream_write_callback(struct pa_stream
*s
, size_t length
, void *userdata
) {
501 assert(s
&& length
&& sndfile
);
506 assert(sample_length
>= length
);
507 l
= length
/pa_frame_size(&sample_spec
);
509 if ((sf_readf_float(sndfile
, d
, l
)) != l
) {
511 fprintf(stderr
, "Premature end of file\n");
515 pa_stream_write(s
, d
, length
, free
, 0);
517 sample_length
-= length
;
519 if (sample_length
<= 0) {
520 pa_stream_set_write_callback(sample_stream
, NULL
, NULL
);
521 pa_stream_finish_upload(sample_stream
);
525 static void context_state_callback(struct pa_context
*c
, void *userdata
) {
527 switch (pa_context_get_state(c
)) {
528 case PA_CONTEXT_CONNECTING
:
529 case PA_CONTEXT_AUTHORIZING
:
530 case PA_CONTEXT_SETTING_NAME
:
533 case PA_CONTEXT_READY
:
537 pa_operation_unref(pa_context_stat(c
, stat_callback
, NULL
));
538 pa_operation_unref(pa_context_get_server_info(c
, get_server_info_callback
, NULL
));
542 pa_operation_unref(pa_context_play_sample(c
, sample_name
, device
, PA_VOLUME_NORM
, simple_callback
, NULL
));
546 pa_operation_unref(pa_context_remove_sample(c
, sample_name
, simple_callback
, NULL
));
550 sample_stream
= pa_stream_new(c
, sample_name
, &sample_spec
);
551 assert(sample_stream
);
553 pa_stream_set_state_callback(sample_stream
, stream_state_callback
, NULL
);
554 pa_stream_set_write_callback(sample_stream
, stream_write_callback
, NULL
);
555 pa_stream_connect_upload(sample_stream
, sample_length
);
559 pa_context_exit_daemon(c
);
564 pa_operation_unref(pa_context_get_module_info_list(c
, get_module_info_callback
, NULL
));
565 pa_operation_unref(pa_context_get_sink_info_list(c
, get_sink_info_callback
, NULL
));
566 pa_operation_unref(pa_context_get_source_info_list(c
, get_source_info_callback
, NULL
));
567 pa_operation_unref(pa_context_get_sink_input_info_list(c
, get_sink_input_info_callback
, NULL
));
568 pa_operation_unref(pa_context_get_source_output_info_list(c
, get_source_output_info_callback
, NULL
));
569 pa_operation_unref(pa_context_get_client_info_list(c
, get_client_info_callback
, NULL
));
570 pa_operation_unref(pa_context_get_sample_info_list(c
, get_sample_info_callback
, NULL
));
571 pa_operation_unref(pa_context_get_autoload_info_list(c
, get_autoload_info_callback
, NULL
));
579 case PA_CONTEXT_TERMINATED
:
583 case PA_CONTEXT_FAILED
:
585 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
590 static void exit_signal_callback(struct pa_mainloop_api
*m
, struct pa_signal_event
*e
, int sig
, void *userdata
) {
591 fprintf(stderr
, "Got SIGINT, exiting.\n");
595 static void help(const char *argv0
) {
597 printf("%s [options] stat\n"
598 "%s [options] list\n"
599 "%s [options] exit\n"
600 "%s [options] upload-sample FILENAME [NAME]\n"
601 "%s [options] play-sample NAME [SINK]\n"
602 "%s [options] remove-sample NAME\n\n"
603 " -h, --help Show this help\n"
604 " --version Show version\n\n"
605 " -s, --server=SERVER The name of the server to connect to\n"
606 " -n, --client-name=NAME How to call this client on the server\n",
607 argv0
, argv0
, argv0
, argv0
, argv0
, argv0
);
610 enum { ARG_VERSION
= 256 };
612 int main(int argc
, char *argv
[]) {
613 struct pa_mainloop
* m
= NULL
;
616 char *server
= NULL
, *client_name
= NULL
, *bn
;
618 static const struct option long_options
[] = {
619 {"server", 1, NULL
, 's'},
620 {"client-name", 1, NULL
, 'n'},
621 {"version", 0, NULL
, ARG_VERSION
},
622 {"help", 0, NULL
, 'h'},
626 if (!(bn
= strrchr(argv
[0], '/')))
631 while ((c
= getopt_long(argc
, argv
, "s:n:h", long_options
, NULL
)) != -1) {
639 printf("pactl "PACKAGE_VERSION
"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
645 server
= strdup(optarg
);
650 client_name
= strdup(optarg
);
659 client_name
= strdup(bn
);
662 if (!strcmp(argv
[optind
], "stat"))
664 else if (!strcmp(argv
[optind
], "exit"))
666 else if (!strcmp(argv
[optind
], "list"))
668 else if (!strcmp(argv
[optind
], "upload-sample")) {
669 struct SF_INFO sfinfo
;
670 action
= UPLOAD_SAMPLE
;
672 if (optind
+1 >= argc
) {
673 fprintf(stderr
, "Please specify a sample file to load\n");
678 sample_name
= strdup(argv
[optind
+2]);
680 char *f
= strrchr(argv
[optind
+1], '/');
690 sample_name
= strdup(tmp
);
693 memset(&sfinfo
, 0, sizeof(sfinfo
));
694 if (!(sndfile
= sf_open(argv
[optind
+1], SFM_READ
, &sfinfo
))) {
695 fprintf(stderr
, "Failed to open sound file.\n");
699 sample_spec
.format
= PA_SAMPLE_FLOAT32
;
700 sample_spec
.rate
= sfinfo
.samplerate
;
701 sample_spec
.channels
= sfinfo
.channels
;
703 sample_length
= sfinfo
.frames
*pa_frame_size(&sample_spec
);
704 } else if (!strcmp(argv
[optind
], "play-sample")) {
705 action
= PLAY_SAMPLE
;
706 if (optind
+1 >= argc
) {
707 fprintf(stderr
, "You have to specify a sample name to play\n");
711 sample_name
= strdup(argv
[optind
+1]);
714 device
= strdup(argv
[optind
+2]);
716 } else if (!strcmp(argv
[optind
], "remove-sample")) {
717 action
= REMOVE_SAMPLE
;
718 if (optind
+1 >= argc
) {
719 fprintf(stderr
, "You have to specify a sample name to remove\n");
723 sample_name
= strdup(argv
[optind
+1]);
727 if (action
== NONE
) {
728 fprintf(stderr
, "No valid command specified.\n");
732 if (!(m
= pa_mainloop_new())) {
733 fprintf(stderr
, "pa_mainloop_new() failed.\n");
737 mainloop_api
= pa_mainloop_get_api(m
);
739 r
= pa_signal_init(mainloop_api
);
741 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
742 signal(SIGPIPE
, SIG_IGN
);
744 if (!(context
= pa_context_new(mainloop_api
, client_name
))) {
745 fprintf(stderr
, "pa_context_new() failed.\n");
749 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
750 pa_context_connect(context
, server
, 1, NULL
);
752 if (pa_mainloop_run(m
, &ret
) < 0) {
753 fprintf(stderr
, "pa_mainloop_run() failed.\n");
759 pa_stream_unref(sample_stream
);
762 pa_context_unref(context
);