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>
40 #if PA_API_VERSION != 9
41 #error Invalid Polypaudio API version
46 static pa_context
*context
= NULL
;
47 static pa_mainloop_api
*mainloop_api
= NULL
;
49 static char *device
= NULL
, *sample_name
= NULL
;
51 static SNDFILE
*sndfile
= NULL
;
52 static pa_stream
*sample_stream
= NULL
;
53 static pa_sample_spec sample_spec
;
54 static size_t sample_length
= 0;
56 static int actions
= 1;
70 static void quit(int ret
) {
72 mainloop_api
->quit(mainloop_api
, ret
);
76 static void context_drain_complete(pa_context
*c
, void *userdata
) {
77 pa_context_disconnect(c
);
80 static void drain(void) {
82 if (!(o
= pa_context_drain(context
, context_drain_complete
, NULL
)))
83 pa_context_disconnect(context
);
85 pa_operation_unref(o
);
89 static void complete_action(void) {
96 static void stat_callback(pa_context
*c
, const pa_stat_info
*i
, void *userdata
) {
99 fprintf(stderr
, "Failed to get statistics: %s\n", pa_strerror(pa_context_errno(c
)));
104 pa_bytes_snprint(s
, sizeof(s
), i
->memblock_total_size
);
105 printf("Currently in use: %u blocks containing %s bytes total.\n", i
->memblock_total
, s
);
107 pa_bytes_snprint(s
, sizeof(s
), i
->memblock_allocated_size
);
108 printf("Allocated during whole lifetime: %u blocks containing %s bytes total.\n", i
->memblock_allocated
, s
);
110 pa_bytes_snprint(s
, sizeof(s
), i
->scache_size
);
111 printf("Sample cache size: %s\n", s
);
116 static void get_server_info_callback(pa_context
*c
, const pa_server_info
*i
, void *useerdata
) {
117 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
120 fprintf(stderr
, "Failed to get server information: %s\n", pa_strerror(pa_context_errno(c
)));
125 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
);
127 printf("User name: %s\n"
130 "Server Version: %s\n"
131 "Default Sample Specification: %s\n"
133 "Default Source: %s\n"
140 i
->default_sink_name
,
141 i
->default_source_name
,
147 static void get_sink_info_callback(pa_context
*c
, const pa_sink_info
*i
, int is_last
, void *userdata
) {
148 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
151 fprintf(stderr
, "Failed to get sink information: %s\n", pa_strerror(pa_context_errno(c
)));
167 printf("*** Sink #%u ***\n"
171 "Sample Specification: %s\n"
175 "Monitor Source: %u\n"
176 "Latency: %0.0f usec\n"
182 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
183 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
185 i
->mute
? "muted" : pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
188 i
->flags
& PA_SINK_HW_VOLUME_CTRL
? "HW_VOLUME_CTRL " : "",
189 i
->flags
& PA_SINK_LATENCY
? "LATENCY" : "");
193 static void get_source_info_callback(pa_context
*c
, const pa_source_info
*i
, int is_last
, void *userdata
) {
194 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], t
[32], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
197 fprintf(stderr
, "Failed to get source information: %s\n", pa_strerror(pa_context_errno(c
)));
213 snprintf(t
, sizeof(t
), "%u", i
->monitor_of_sink
);
215 printf("*** Source #%u ***\n"
219 "Sample Specification: %s\n"
223 "Monitor of Sink: %s\n"
224 "Latency: %0.0f usec\n"
230 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
231 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
233 i
->mute
? "muted" : pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
234 i
->monitor_of_sink
!= PA_INVALID_INDEX
? t
: "no",
236 i
->flags
& PA_SOURCE_HW_VOLUME_CTRL
? "HW_VOLUME_CTRL " : "",
237 i
->flags
& PA_SOURCE_LATENCY
? "LATENCY" : "");
241 static void get_module_info_callback(pa_context
*c
, const pa_module_info
*i
, int is_last
, void *userdata
) {
245 fprintf(stderr
, "Failed to get module information: %s\n", pa_strerror(pa_context_errno(c
)));
261 snprintf(t
, sizeof(t
), "%u", i
->n_used
);
263 printf("*** Module #%u ***\n"
266 "Usage counter: %s\n"
271 i
->n_used
!= PA_INVALID_INDEX
? t
: "n/a",
272 i
->auto_unload
? "yes" : "no");
275 static void get_client_info_callback(pa_context
*c
, const pa_client_info
*i
, int is_last
, void *userdata
) {
279 fprintf(stderr
, "Failed to get client information: %s\n", pa_strerror(pa_context_errno(c
)));
295 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
297 printf("*** Client #%u ***\n"
300 "Owner Module: %s\n",
304 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a");
307 static void get_sink_input_info_callback(pa_context
*c
, const pa_sink_input_info
*i
, int is_last
, void *userdata
) {
308 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
311 fprintf(stderr
, "Failed to get sink input information: %s\n", pa_strerror(pa_context_errno(c
)));
327 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
328 snprintf(k
, sizeof(k
), "%u", i
->client
);
330 printf("*** Sink Input #%u ***\n"
336 "Sample Specification: %s\n"
339 "Buffer Latency: %0.0f usec\n"
340 "Sink Latency: %0.0f usec\n"
341 "Resample method: %s\n",
345 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
346 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
348 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
349 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
350 pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
351 (double) i
->buffer_usec
,
352 (double) i
->sink_usec
,
353 i
->resample_method
? i
->resample_method
: "n/a");
357 static void get_source_output_info_callback(pa_context
*c
, const pa_source_output_info
*i
, int is_last
, void *userdata
) {
358 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
361 fprintf(stderr
, "Failed to get source output information: %s\n", pa_strerror(pa_context_errno(c
)));
378 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
379 snprintf(k
, sizeof(k
), "%u", i
->client
);
381 printf("*** Source Output #%u ***\n"
387 "Sample Specification: %s\n"
389 "Buffer Latency: %0.0f usec\n"
390 "Source Latency: %0.0f usec\n"
391 "Resample method: %s\n",
395 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
396 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
398 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
399 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
400 (double) i
->buffer_usec
,
401 (double) i
->source_usec
,
402 i
->resample_method
? i
->resample_method
: "n/a");
405 static void get_sample_info_callback(pa_context
*c
, const pa_sample_info
*i
, int is_last
, void *userdata
) {
406 char t
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
409 fprintf(stderr
, "Failed to get sample information: %s\n", pa_strerror(pa_context_errno(c
)));
426 pa_bytes_snprint(t
, sizeof(t
), i
->bytes
);
428 printf("*** Sample #%u ***\n"
431 "Sample Specification: %s\n"
439 pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
440 pa_sample_spec_valid(&i
->sample_spec
) ? pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
) : "n/a",
441 pa_sample_spec_valid(&i
->sample_spec
) ? pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
) : "n/a",
442 (double) i
->duration
/1000000,
444 i
->lazy
? "yes" : "no",
445 i
->filename
? i
->filename
: "n/a");
448 static void get_autoload_info_callback(pa_context
*c
, const pa_autoload_info
*i
, int is_last
, void *userdata
) {
450 fprintf(stderr
, "Failed to get autoload information: %s\n", pa_strerror(pa_context_errno(c
)));
466 printf("*** Autoload Entry #%u ***\n"
473 i
->type
== PA_AUTOLOAD_SINK
? "sink" : "source",
478 static void simple_callback(pa_context
*c
, int success
, void *userdata
) {
480 fprintf(stderr
, "Failure: %s\n", pa_strerror(pa_context_errno(c
)));
488 static void stream_state_callback(pa_stream
*s
, void *userdata
) {
491 switch (pa_stream_get_state(s
)) {
492 case PA_STREAM_CREATING
:
493 case PA_STREAM_READY
:
496 case PA_STREAM_TERMINATED
:
500 case PA_STREAM_FAILED
:
502 fprintf(stderr
, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
507 static void stream_write_callback(pa_stream
*s
, size_t length
, void *userdata
) {
510 assert(s
&& length
&& sndfile
);
512 d
= pa_xmalloc(length
);
514 assert(sample_length
>= length
);
515 l
= length
/pa_frame_size(&sample_spec
);
517 if ((sf_readf_float(sndfile
, d
, l
)) != l
) {
519 fprintf(stderr
, "Premature end of file\n");
523 pa_stream_write(s
, d
, length
, pa_xfree
, 0, PA_SEEK_RELATIVE
);
525 sample_length
-= length
;
527 if (sample_length
<= 0) {
528 pa_stream_set_write_callback(sample_stream
, NULL
, NULL
);
529 pa_stream_finish_upload(sample_stream
);
533 static void context_state_callback(pa_context
*c
, void *userdata
) {
535 switch (pa_context_get_state(c
)) {
536 case PA_CONTEXT_CONNECTING
:
537 case PA_CONTEXT_AUTHORIZING
:
538 case PA_CONTEXT_SETTING_NAME
:
541 case PA_CONTEXT_READY
:
545 pa_operation_unref(pa_context_stat(c
, stat_callback
, NULL
));
546 pa_operation_unref(pa_context_get_server_info(c
, get_server_info_callback
, NULL
));
550 pa_operation_unref(pa_context_play_sample(c
, sample_name
, device
, PA_VOLUME_NORM
, simple_callback
, NULL
));
554 pa_operation_unref(pa_context_remove_sample(c
, sample_name
, simple_callback
, NULL
));
558 sample_stream
= pa_stream_new(c
, sample_name
, &sample_spec
, NULL
);
559 assert(sample_stream
);
561 pa_stream_set_state_callback(sample_stream
, stream_state_callback
, NULL
);
562 pa_stream_set_write_callback(sample_stream
, stream_write_callback
, NULL
);
563 pa_stream_connect_upload(sample_stream
, sample_length
);
567 pa_operation_unref(pa_context_exit_daemon(c
, NULL
, NULL
));
572 pa_operation_unref(pa_context_get_module_info_list(c
, get_module_info_callback
, NULL
));
573 pa_operation_unref(pa_context_get_sink_info_list(c
, get_sink_info_callback
, NULL
));
574 pa_operation_unref(pa_context_get_source_info_list(c
, get_source_info_callback
, NULL
));
575 pa_operation_unref(pa_context_get_sink_input_info_list(c
, get_sink_input_info_callback
, NULL
));
576 pa_operation_unref(pa_context_get_source_output_info_list(c
, get_source_output_info_callback
, NULL
));
577 pa_operation_unref(pa_context_get_client_info_list(c
, get_client_info_callback
, NULL
));
578 pa_operation_unref(pa_context_get_sample_info_list(c
, get_sample_info_callback
, NULL
));
579 pa_operation_unref(pa_context_get_autoload_info_list(c
, get_autoload_info_callback
, NULL
));
587 case PA_CONTEXT_TERMINATED
:
591 case PA_CONTEXT_FAILED
:
593 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
598 static void exit_signal_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
599 fprintf(stderr
, "Got SIGINT, exiting.\n");
603 static void help(const char *argv0
) {
605 printf("%s [options] stat\n"
606 "%s [options] list\n"
607 "%s [options] exit\n"
608 "%s [options] upload-sample FILENAME [NAME]\n"
609 "%s [options] play-sample NAME [SINK]\n"
610 "%s [options] remove-sample NAME\n\n"
611 " -h, --help Show this help\n"
612 " --version Show version\n\n"
613 " -s, --server=SERVER The name of the server to connect to\n"
614 " -n, --client-name=NAME How to call this client on the server\n",
615 argv0
, argv0
, argv0
, argv0
, argv0
, argv0
);
618 enum { ARG_VERSION
= 256 };
620 int main(int argc
, char *argv
[]) {
621 pa_mainloop
* m
= NULL
;
624 char *server
= NULL
, *client_name
= NULL
, *bn
;
626 static const struct option long_options
[] = {
627 {"server", 1, NULL
, 's'},
628 {"client-name", 1, NULL
, 'n'},
629 {"version", 0, NULL
, ARG_VERSION
},
630 {"help", 0, NULL
, 'h'},
634 if (!(bn
= strrchr(argv
[0], '/')))
639 while ((c
= getopt_long(argc
, argv
, "s:n:h", long_options
, NULL
)) != -1) {
647 printf("pactl "PACKAGE_VERSION
"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
653 server
= pa_xstrdup(optarg
);
657 pa_xfree(client_name
);
658 client_name
= pa_xstrdup(optarg
);
667 client_name
= pa_xstrdup(bn
);
670 if (!strcmp(argv
[optind
], "stat"))
672 else if (!strcmp(argv
[optind
], "exit"))
674 else if (!strcmp(argv
[optind
], "list"))
676 else if (!strcmp(argv
[optind
], "upload-sample")) {
677 struct SF_INFO sfinfo
;
678 action
= UPLOAD_SAMPLE
;
680 if (optind
+1 >= argc
) {
681 fprintf(stderr
, "Please specify a sample file to load\n");
686 sample_name
= pa_xstrdup(argv
[optind
+2]);
688 char *f
= strrchr(argv
[optind
+1], '/');
698 sample_name
= pa_xstrdup(tmp
);
701 memset(&sfinfo
, 0, sizeof(sfinfo
));
702 if (!(sndfile
= sf_open(argv
[optind
+1], SFM_READ
, &sfinfo
))) {
703 fprintf(stderr
, "Failed to open sound file.\n");
707 sample_spec
.format
= PA_SAMPLE_FLOAT32
;
708 sample_spec
.rate
= sfinfo
.samplerate
;
709 sample_spec
.channels
= sfinfo
.channels
;
711 sample_length
= sfinfo
.frames
*pa_frame_size(&sample_spec
);
712 } else if (!strcmp(argv
[optind
], "play-sample")) {
713 action
= PLAY_SAMPLE
;
714 if (optind
+1 >= argc
) {
715 fprintf(stderr
, "You have to specify a sample name to play\n");
719 sample_name
= pa_xstrdup(argv
[optind
+1]);
722 device
= pa_xstrdup(argv
[optind
+2]);
724 } else if (!strcmp(argv
[optind
], "remove-sample")) {
725 action
= REMOVE_SAMPLE
;
726 if (optind
+1 >= argc
) {
727 fprintf(stderr
, "You have to specify a sample name to remove\n");
731 sample_name
= pa_xstrdup(argv
[optind
+1]);
735 if (action
== NONE
) {
736 fprintf(stderr
, "No valid command specified.\n");
740 if (!(m
= pa_mainloop_new())) {
741 fprintf(stderr
, "pa_mainloop_new() failed.\n");
745 mainloop_api
= pa_mainloop_get_api(m
);
747 r
= pa_signal_init(mainloop_api
);
749 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
751 signal(SIGPIPE
, SIG_IGN
);
754 if (!(context
= pa_context_new(mainloop_api
, client_name
))) {
755 fprintf(stderr
, "pa_context_new() failed.\n");
759 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
760 pa_context_connect(context
, server
, 0, NULL
);
762 if (pa_mainloop_run(m
, &ret
) < 0) {
763 fprintf(stderr
, "pa_mainloop_run() failed.\n");
769 pa_stream_unref(sample_stream
);
772 pa_context_unref(context
);
784 pa_xfree(sample_name
);