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",
183 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
184 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
186 pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
188 (double) i
->latency
);
192 static void get_source_info_callback(pa_context
*c
, const pa_source_info
*i
, int is_last
, void *userdata
) {
193 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], t
[32], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
196 fprintf(stderr
, "Failed to get source information: %s\n", pa_strerror(pa_context_errno(c
)));
212 snprintf(t
, sizeof(t
), "%u", i
->monitor_of_sink
);
214 printf("*** Source #%u ***\n"
218 "Sample Specification: %s\n"
221 "Monitor of Sink: %s\n"
222 "Latency: %0.0f usec\n",
227 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
228 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
230 i
->monitor_of_sink
!= PA_INVALID_INDEX
? t
: "no",
231 (double) i
->latency
);
235 static void get_module_info_callback(pa_context
*c
, const pa_module_info
*i
, int is_last
, void *userdata
) {
239 fprintf(stderr
, "Failed to get module information: %s\n", pa_strerror(pa_context_errno(c
)));
255 snprintf(t
, sizeof(t
), "%u", i
->n_used
);
257 printf("*** Module #%u ***\n"
260 "Usage counter: %s\n"
265 i
->n_used
!= PA_INVALID_INDEX
? t
: "n/a",
266 i
->auto_unload
? "yes" : "no");
269 static void get_client_info_callback(pa_context
*c
, const pa_client_info
*i
, int is_last
, void *userdata
) {
273 fprintf(stderr
, "Failed to get client information: %s\n", pa_strerror(pa_context_errno(c
)));
289 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
291 printf("*** Client #%u ***\n"
294 "Owner Module: %s\n",
298 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a");
301 static void get_sink_input_info_callback(pa_context
*c
, const pa_sink_input_info
*i
, int is_last
, void *userdata
) {
302 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
305 fprintf(stderr
, "Failed to get sink input information: %s\n", pa_strerror(pa_context_errno(c
)));
321 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
322 snprintf(k
, sizeof(k
), "%u", i
->client
);
324 printf("*** Sink Input #%u ***\n"
330 "Sample Specification: %s\n"
333 "Buffer Latency: %0.0f usec\n"
334 "Sink Latency: %0.0f usec\n"
335 "Resample method: %s\n",
339 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
340 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
342 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
343 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
344 pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
345 (double) i
->buffer_usec
,
346 (double) i
->sink_usec
,
347 i
->resample_method
? i
->resample_method
: "n/a");
351 static void get_source_output_info_callback(pa_context
*c
, const pa_source_output_info
*i
, int is_last
, void *userdata
) {
352 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
355 fprintf(stderr
, "Failed to get source output information: %s\n", pa_strerror(pa_context_errno(c
)));
372 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
373 snprintf(k
, sizeof(k
), "%u", i
->client
);
375 printf("*** Source Output #%u ***\n"
381 "Sample Specification: %s\n"
383 "Buffer Latency: %0.0f usec\n"
384 "Source Latency: %0.0f usec\n"
385 "Resample method: %s\n",
389 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
390 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
392 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
393 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
394 (double) i
->buffer_usec
,
395 (double) i
->source_usec
,
396 i
->resample_method
? i
->resample_method
: "n/a");
399 static void get_sample_info_callback(pa_context
*c
, const pa_sample_info
*i
, int is_last
, void *userdata
) {
400 char t
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
403 fprintf(stderr
, "Failed to get sample information: %s\n", pa_strerror(pa_context_errno(c
)));
420 pa_bytes_snprint(t
, sizeof(t
), i
->bytes
);
422 printf("*** Sample #%u ***\n"
425 "Sample Specification: %s\n"
433 pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
434 pa_sample_spec_valid(&i
->sample_spec
) ? pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
) : "n/a",
435 pa_sample_spec_valid(&i
->sample_spec
) ? pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
) : "n/a",
436 (double) i
->duration
/1000000,
438 i
->lazy
? "yes" : "no",
439 i
->filename
? i
->filename
: "n/a");
442 static void get_autoload_info_callback(pa_context
*c
, const pa_autoload_info
*i
, int is_last
, void *userdata
) {
444 fprintf(stderr
, "Failed to get autoload information: %s\n", pa_strerror(pa_context_errno(c
)));
460 printf("*** Autoload Entry #%u ***\n"
467 i
->type
== PA_AUTOLOAD_SINK
? "sink" : "source",
472 static void simple_callback(pa_context
*c
, int success
, void *userdata
) {
474 fprintf(stderr
, "Failure: %s\n", pa_strerror(pa_context_errno(c
)));
482 static void stream_state_callback(pa_stream
*s
, void *userdata
) {
485 switch (pa_stream_get_state(s
)) {
486 case PA_STREAM_CREATING
:
487 case PA_STREAM_READY
:
490 case PA_STREAM_TERMINATED
:
494 case PA_STREAM_FAILED
:
496 fprintf(stderr
, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
501 static void stream_write_callback(pa_stream
*s
, size_t length
, void *userdata
) {
504 assert(s
&& length
&& sndfile
);
509 assert(sample_length
>= length
);
510 l
= length
/pa_frame_size(&sample_spec
);
512 if ((sf_readf_float(sndfile
, d
, l
)) != l
) {
514 fprintf(stderr
, "Premature end of file\n");
518 pa_stream_write(s
, d
, length
, free
, 0, PA_SEEK_RELATIVE
);
520 sample_length
-= length
;
522 if (sample_length
<= 0) {
523 pa_stream_set_write_callback(sample_stream
, NULL
, NULL
);
524 pa_stream_finish_upload(sample_stream
);
528 static void context_state_callback(pa_context
*c
, void *userdata
) {
530 switch (pa_context_get_state(c
)) {
531 case PA_CONTEXT_CONNECTING
:
532 case PA_CONTEXT_AUTHORIZING
:
533 case PA_CONTEXT_SETTING_NAME
:
536 case PA_CONTEXT_READY
:
540 pa_operation_unref(pa_context_stat(c
, stat_callback
, NULL
));
541 pa_operation_unref(pa_context_get_server_info(c
, get_server_info_callback
, NULL
));
545 pa_operation_unref(pa_context_play_sample(c
, sample_name
, device
, PA_VOLUME_NORM
, simple_callback
, NULL
));
549 pa_operation_unref(pa_context_remove_sample(c
, sample_name
, simple_callback
, NULL
));
553 sample_stream
= pa_stream_new(c
, sample_name
, &sample_spec
, NULL
);
554 assert(sample_stream
);
556 pa_stream_set_state_callback(sample_stream
, stream_state_callback
, NULL
);
557 pa_stream_set_write_callback(sample_stream
, stream_write_callback
, NULL
);
558 pa_stream_connect_upload(sample_stream
, sample_length
);
562 pa_context_exit_daemon(c
);
567 pa_operation_unref(pa_context_get_module_info_list(c
, get_module_info_callback
, NULL
));
568 pa_operation_unref(pa_context_get_sink_info_list(c
, get_sink_info_callback
, NULL
));
569 pa_operation_unref(pa_context_get_source_info_list(c
, get_source_info_callback
, NULL
));
570 pa_operation_unref(pa_context_get_sink_input_info_list(c
, get_sink_input_info_callback
, NULL
));
571 pa_operation_unref(pa_context_get_source_output_info_list(c
, get_source_output_info_callback
, NULL
));
572 pa_operation_unref(pa_context_get_client_info_list(c
, get_client_info_callback
, NULL
));
573 pa_operation_unref(pa_context_get_sample_info_list(c
, get_sample_info_callback
, NULL
));
574 pa_operation_unref(pa_context_get_autoload_info_list(c
, get_autoload_info_callback
, NULL
));
582 case PA_CONTEXT_TERMINATED
:
586 case PA_CONTEXT_FAILED
:
588 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
593 static void exit_signal_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
594 fprintf(stderr
, "Got SIGINT, exiting.\n");
598 static void help(const char *argv0
) {
600 printf("%s [options] stat\n"
601 "%s [options] list\n"
602 "%s [options] exit\n"
603 "%s [options] upload-sample FILENAME [NAME]\n"
604 "%s [options] play-sample NAME [SINK]\n"
605 "%s [options] remove-sample NAME\n\n"
606 " -h, --help Show this help\n"
607 " --version Show version\n\n"
608 " -s, --server=SERVER The name of the server to connect to\n"
609 " -n, --client-name=NAME How to call this client on the server\n",
610 argv0
, argv0
, argv0
, argv0
, argv0
, argv0
);
613 enum { ARG_VERSION
= 256 };
615 int main(int argc
, char *argv
[]) {
616 pa_mainloop
* m
= NULL
;
619 char *server
= NULL
, *client_name
= NULL
, *bn
;
621 static const struct option long_options
[] = {
622 {"server", 1, NULL
, 's'},
623 {"client-name", 1, NULL
, 'n'},
624 {"version", 0, NULL
, ARG_VERSION
},
625 {"help", 0, NULL
, 'h'},
629 if (!(bn
= strrchr(argv
[0], '/')))
634 while ((c
= getopt_long(argc
, argv
, "s:n:h", long_options
, NULL
)) != -1) {
642 printf("pactl "PACKAGE_VERSION
"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
648 server
= strdup(optarg
);
653 client_name
= strdup(optarg
);
662 client_name
= strdup(bn
);
665 if (!strcmp(argv
[optind
], "stat"))
667 else if (!strcmp(argv
[optind
], "exit"))
669 else if (!strcmp(argv
[optind
], "list"))
671 else if (!strcmp(argv
[optind
], "upload-sample")) {
672 struct SF_INFO sfinfo
;
673 action
= UPLOAD_SAMPLE
;
675 if (optind
+1 >= argc
) {
676 fprintf(stderr
, "Please specify a sample file to load\n");
681 sample_name
= strdup(argv
[optind
+2]);
683 char *f
= strrchr(argv
[optind
+1], '/');
693 sample_name
= strdup(tmp
);
696 memset(&sfinfo
, 0, sizeof(sfinfo
));
697 if (!(sndfile
= sf_open(argv
[optind
+1], SFM_READ
, &sfinfo
))) {
698 fprintf(stderr
, "Failed to open sound file.\n");
702 sample_spec
.format
= PA_SAMPLE_FLOAT32
;
703 sample_spec
.rate
= sfinfo
.samplerate
;
704 sample_spec
.channels
= sfinfo
.channels
;
706 sample_length
= sfinfo
.frames
*pa_frame_size(&sample_spec
);
707 } else if (!strcmp(argv
[optind
], "play-sample")) {
708 action
= PLAY_SAMPLE
;
709 if (optind
+1 >= argc
) {
710 fprintf(stderr
, "You have to specify a sample name to play\n");
714 sample_name
= strdup(argv
[optind
+1]);
717 device
= strdup(argv
[optind
+2]);
719 } else if (!strcmp(argv
[optind
], "remove-sample")) {
720 action
= REMOVE_SAMPLE
;
721 if (optind
+1 >= argc
) {
722 fprintf(stderr
, "You have to specify a sample name to remove\n");
726 sample_name
= strdup(argv
[optind
+1]);
730 if (action
== NONE
) {
731 fprintf(stderr
, "No valid command specified.\n");
735 if (!(m
= pa_mainloop_new())) {
736 fprintf(stderr
, "pa_mainloop_new() failed.\n");
740 mainloop_api
= pa_mainloop_get_api(m
);
742 r
= pa_signal_init(mainloop_api
);
744 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
746 signal(SIGPIPE
, SIG_IGN
);
749 if (!(context
= pa_context_new(mainloop_api
, client_name
))) {
750 fprintf(stderr
, "pa_context_new() failed.\n");
754 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
755 pa_context_connect(context
, server
, 1, NULL
);
757 if (pa_mainloop_run(m
, &ret
) < 0) {
758 fprintf(stderr
, "pa_mainloop_run() failed.\n");
764 pa_stream_unref(sample_stream
);
767 pa_context_unref(context
);