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/polypaudio.h>
39 #include <polyp/mainloop.h>
40 #include <polyp/mainloop-signal.h>
42 #if PA_API_VERSION != 8
43 #error Invalid Polypaudio API version
48 static pa_context
*context
= NULL
;
49 static pa_mainloop_api
*mainloop_api
= NULL
;
51 static char *device
= NULL
, *sample_name
= NULL
;
53 static SNDFILE
*sndfile
= NULL
;
54 static pa_stream
*sample_stream
= NULL
;
55 static pa_sample_spec sample_spec
;
56 static size_t sample_length
= 0;
58 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" : "");
195 static void get_source_info_callback(pa_context
*c
, const pa_source_info
*i
, int is_last
, void *userdata
) {
196 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], t
[32], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
199 fprintf(stderr
, "Failed to get source information: %s\n", pa_strerror(pa_context_errno(c
)));
215 snprintf(t
, sizeof(t
), "%u", i
->monitor_of_sink
);
217 printf("*** Source #%u ***\n"
221 "Sample Specification: %s\n"
225 "Monitor of Sink: %s\n"
226 "Latency: %0.0f usec\n"
232 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
233 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
235 i
->mute
? "muted" : pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
236 i
->monitor_of_sink
!= PA_INVALID_INDEX
? t
: "no",
238 i
->flags
& PA_SOURCE_HW_VOLUME_CTRL
? "HW_VOLUME_CTRL " : "",
239 i
->flags
& PA_SOURCE_LATENCY
? "LATENCY" : "");
243 static void get_module_info_callback(pa_context
*c
, const pa_module_info
*i
, int is_last
, void *userdata
) {
247 fprintf(stderr
, "Failed to get module information: %s\n", pa_strerror(pa_context_errno(c
)));
263 snprintf(t
, sizeof(t
), "%u", i
->n_used
);
265 printf("*** Module #%u ***\n"
268 "Usage counter: %s\n"
273 i
->n_used
!= PA_INVALID_INDEX
? t
: "n/a",
274 i
->auto_unload
? "yes" : "no");
277 static void get_client_info_callback(pa_context
*c
, const pa_client_info
*i
, int is_last
, void *userdata
) {
281 fprintf(stderr
, "Failed to get client information: %s\n", pa_strerror(pa_context_errno(c
)));
297 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
299 printf("*** Client #%u ***\n"
302 "Owner Module: %s\n",
306 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a");
309 static void get_sink_input_info_callback(pa_context
*c
, const pa_sink_input_info
*i
, int is_last
, void *userdata
) {
310 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
313 fprintf(stderr
, "Failed to get sink input information: %s\n", pa_strerror(pa_context_errno(c
)));
329 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
330 snprintf(k
, sizeof(k
), "%u", i
->client
);
332 printf("*** Sink Input #%u ***\n"
338 "Sample Specification: %s\n"
341 "Buffer Latency: %0.0f usec\n"
342 "Sink Latency: %0.0f usec\n"
343 "Resample method: %s\n",
347 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
348 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
350 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
351 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
352 pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
353 (double) i
->buffer_usec
,
354 (double) i
->sink_usec
,
355 i
->resample_method
? i
->resample_method
: "n/a");
359 static void get_source_output_info_callback(pa_context
*c
, const pa_source_output_info
*i
, int is_last
, void *userdata
) {
360 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
363 fprintf(stderr
, "Failed to get source output information: %s\n", pa_strerror(pa_context_errno(c
)));
380 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
381 snprintf(k
, sizeof(k
), "%u", i
->client
);
383 printf("*** Source Output #%u ***\n"
389 "Sample Specification: %s\n"
391 "Buffer Latency: %0.0f usec\n"
392 "Source Latency: %0.0f usec\n"
393 "Resample method: %s\n",
397 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
398 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
400 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
401 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
402 (double) i
->buffer_usec
,
403 (double) i
->source_usec
,
404 i
->resample_method
? i
->resample_method
: "n/a");
407 static void get_sample_info_callback(pa_context
*c
, const pa_sample_info
*i
, int is_last
, void *userdata
) {
408 char t
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
411 fprintf(stderr
, "Failed to get sample information: %s\n", pa_strerror(pa_context_errno(c
)));
428 pa_bytes_snprint(t
, sizeof(t
), i
->bytes
);
430 printf("*** Sample #%u ***\n"
433 "Sample Specification: %s\n"
441 pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
442 pa_sample_spec_valid(&i
->sample_spec
) ? pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
) : "n/a",
443 pa_sample_spec_valid(&i
->sample_spec
) ? pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
) : "n/a",
444 (double) i
->duration
/1000000,
446 i
->lazy
? "yes" : "no",
447 i
->filename
? i
->filename
: "n/a");
450 static void get_autoload_info_callback(pa_context
*c
, const pa_autoload_info
*i
, int is_last
, void *userdata
) {
452 fprintf(stderr
, "Failed to get autoload information: %s\n", pa_strerror(pa_context_errno(c
)));
468 printf("*** Autoload Entry #%u ***\n"
475 i
->type
== PA_AUTOLOAD_SINK
? "sink" : "source",
480 static void simple_callback(pa_context
*c
, int success
, void *userdata
) {
482 fprintf(stderr
, "Failure: %s\n", pa_strerror(pa_context_errno(c
)));
490 static void stream_state_callback(pa_stream
*s
, void *userdata
) {
493 switch (pa_stream_get_state(s
)) {
494 case PA_STREAM_CREATING
:
495 case PA_STREAM_READY
:
498 case PA_STREAM_TERMINATED
:
502 case PA_STREAM_FAILED
:
504 fprintf(stderr
, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
509 static void stream_write_callback(pa_stream
*s
, size_t length
, void *userdata
) {
512 assert(s
&& length
&& sndfile
);
517 assert(sample_length
>= length
);
518 l
= length
/pa_frame_size(&sample_spec
);
520 if ((sf_readf_float(sndfile
, d
, l
)) != l
) {
522 fprintf(stderr
, "Premature end of file\n");
526 pa_stream_write(s
, d
, length
, free
, 0, PA_SEEK_RELATIVE
);
528 sample_length
-= length
;
530 if (sample_length
<= 0) {
531 pa_stream_set_write_callback(sample_stream
, NULL
, NULL
);
532 pa_stream_finish_upload(sample_stream
);
536 static void context_state_callback(pa_context
*c
, void *userdata
) {
538 switch (pa_context_get_state(c
)) {
539 case PA_CONTEXT_CONNECTING
:
540 case PA_CONTEXT_AUTHORIZING
:
541 case PA_CONTEXT_SETTING_NAME
:
544 case PA_CONTEXT_READY
:
548 pa_operation_unref(pa_context_stat(c
, stat_callback
, NULL
));
549 pa_operation_unref(pa_context_get_server_info(c
, get_server_info_callback
, NULL
));
553 pa_operation_unref(pa_context_play_sample(c
, sample_name
, device
, PA_VOLUME_NORM
, simple_callback
, NULL
));
557 pa_operation_unref(pa_context_remove_sample(c
, sample_name
, simple_callback
, NULL
));
561 sample_stream
= pa_stream_new(c
, sample_name
, &sample_spec
, NULL
);
562 assert(sample_stream
);
564 pa_stream_set_state_callback(sample_stream
, stream_state_callback
, NULL
);
565 pa_stream_set_write_callback(sample_stream
, stream_write_callback
, NULL
);
566 pa_stream_connect_upload(sample_stream
, sample_length
);
570 pa_operation_unref(pa_context_exit_daemon(c
, NULL
, NULL
));
575 pa_operation_unref(pa_context_get_module_info_list(c
, get_module_info_callback
, NULL
));
576 pa_operation_unref(pa_context_get_sink_info_list(c
, get_sink_info_callback
, NULL
));
577 pa_operation_unref(pa_context_get_source_info_list(c
, get_source_info_callback
, NULL
));
578 pa_operation_unref(pa_context_get_sink_input_info_list(c
, get_sink_input_info_callback
, NULL
));
579 pa_operation_unref(pa_context_get_source_output_info_list(c
, get_source_output_info_callback
, NULL
));
580 pa_operation_unref(pa_context_get_client_info_list(c
, get_client_info_callback
, NULL
));
581 pa_operation_unref(pa_context_get_sample_info_list(c
, get_sample_info_callback
, NULL
));
582 pa_operation_unref(pa_context_get_autoload_info_list(c
, get_autoload_info_callback
, NULL
));
590 case PA_CONTEXT_TERMINATED
:
594 case PA_CONTEXT_FAILED
:
596 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
601 static void exit_signal_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
602 fprintf(stderr
, "Got SIGINT, exiting.\n");
606 static void help(const char *argv0
) {
608 printf("%s [options] stat\n"
609 "%s [options] list\n"
610 "%s [options] exit\n"
611 "%s [options] upload-sample FILENAME [NAME]\n"
612 "%s [options] play-sample NAME [SINK]\n"
613 "%s [options] remove-sample NAME\n\n"
614 " -h, --help Show this help\n"
615 " --version Show version\n\n"
616 " -s, --server=SERVER The name of the server to connect to\n"
617 " -n, --client-name=NAME How to call this client on the server\n",
618 argv0
, argv0
, argv0
, argv0
, argv0
, argv0
);
621 enum { ARG_VERSION
= 256 };
623 int main(int argc
, char *argv
[]) {
624 pa_mainloop
* m
= NULL
;
627 char *server
= NULL
, *client_name
= NULL
, *bn
;
629 static const struct option long_options
[] = {
630 {"server", 1, NULL
, 's'},
631 {"client-name", 1, NULL
, 'n'},
632 {"version", 0, NULL
, ARG_VERSION
},
633 {"help", 0, NULL
, 'h'},
637 if (!(bn
= strrchr(argv
[0], '/')))
642 while ((c
= getopt_long(argc
, argv
, "s:n:h", long_options
, NULL
)) != -1) {
650 printf("pactl "PACKAGE_VERSION
"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
656 server
= strdup(optarg
);
661 client_name
= strdup(optarg
);
670 client_name
= strdup(bn
);
673 if (!strcmp(argv
[optind
], "stat"))
675 else if (!strcmp(argv
[optind
], "exit"))
677 else if (!strcmp(argv
[optind
], "list"))
679 else if (!strcmp(argv
[optind
], "upload-sample")) {
680 struct SF_INFO sfinfo
;
681 action
= UPLOAD_SAMPLE
;
683 if (optind
+1 >= argc
) {
684 fprintf(stderr
, "Please specify a sample file to load\n");
689 sample_name
= strdup(argv
[optind
+2]);
691 char *f
= strrchr(argv
[optind
+1], '/');
701 sample_name
= strdup(tmp
);
704 memset(&sfinfo
, 0, sizeof(sfinfo
));
705 if (!(sndfile
= sf_open(argv
[optind
+1], SFM_READ
, &sfinfo
))) {
706 fprintf(stderr
, "Failed to open sound file.\n");
710 sample_spec
.format
= PA_SAMPLE_FLOAT32
;
711 sample_spec
.rate
= sfinfo
.samplerate
;
712 sample_spec
.channels
= sfinfo
.channels
;
714 sample_length
= sfinfo
.frames
*pa_frame_size(&sample_spec
);
715 } else if (!strcmp(argv
[optind
], "play-sample")) {
716 action
= PLAY_SAMPLE
;
717 if (optind
+1 >= argc
) {
718 fprintf(stderr
, "You have to specify a sample name to play\n");
722 sample_name
= strdup(argv
[optind
+1]);
725 device
= strdup(argv
[optind
+2]);
727 } else if (!strcmp(argv
[optind
], "remove-sample")) {
728 action
= REMOVE_SAMPLE
;
729 if (optind
+1 >= argc
) {
730 fprintf(stderr
, "You have to specify a sample name to remove\n");
734 sample_name
= strdup(argv
[optind
+1]);
738 if (action
== NONE
) {
739 fprintf(stderr
, "No valid command specified.\n");
743 if (!(m
= pa_mainloop_new())) {
744 fprintf(stderr
, "pa_mainloop_new() failed.\n");
748 mainloop_api
= pa_mainloop_get_api(m
);
750 r
= pa_signal_init(mainloop_api
);
752 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
754 signal(SIGPIPE
, SIG_IGN
);
757 if (!(context
= pa_context_new(mainloop_api
, client_name
))) {
758 fprintf(stderr
, "pa_context_new() failed.\n");
762 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
763 pa_context_connect(context
, server
, 0, NULL
);
765 if (pa_mainloop_run(m
, &ret
) < 0) {
766 fprintf(stderr
, "pa_mainloop_run() failed.\n");
772 pa_stream_unref(sample_stream
);
775 pa_context_unref(context
);