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 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 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>
46 static struct pa_context
*context
= NULL
;
47 static struct pa_mainloop_api
*mainloop_api
= NULL
;
49 static char *device
= NULL
, *sample_name
= NULL
;
51 static SNDFILE
*sndfile
= NULL
;
52 static struct pa_stream
*sample_stream
= NULL
;
53 static struct 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(struct pa_context
*c
, void *userdata
) {
77 pa_context_disconnect(c
);
80 static void drain(void) {
81 struct pa_operation
*o
;
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(struct pa_context
*c
, const struct 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(struct pa_context
*c
, const struct 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",
139 i
->default_sink_name
,
140 i
->default_source_name
);
145 static void get_sink_info_callback(struct pa_context
*c
, const struct pa_sink_info
*i
, int is_last
, void *userdata
) {
146 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
149 fprintf(stderr
, "Failed to get sink information: %s\n", pa_strerror(pa_context_errno(c
)));
165 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
);
167 printf("*** Sink #%u ***\n"
170 "Sample Specification: %s\n"
172 "Volume: 0x%03x (%0.2f dB)\n"
173 "Monitor Source: %u\n"
174 "Latency: %0.0f usec\n",
180 i
->volume
, pa_volume_to_dB(i
->volume
),
182 (double) i
->latency
);
185 static void get_source_info_callback(struct pa_context
*c
, const struct pa_source_info
*i
, int is_last
, void *userdata
) {
186 char s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], t
[32];
189 fprintf(stderr
, "Failed to get source information: %s\n", pa_strerror(pa_context_errno(c
)));
205 snprintf(t
, sizeof(t
), "%u", i
->monitor_of_sink
);
207 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
);
209 printf("*** Source #%u ***\n"
212 "Sample Specification: %s\n"
214 "Monitor of Sink: %s\n"
215 "Latency: %0.0f usec\n",
221 i
->monitor_of_sink
!= PA_INVALID_INDEX
? t
: "no",
222 (double) i
->latency
);
225 static void get_module_info_callback(struct pa_context
*c
, const struct pa_module_info
*i
, int is_last
, void *userdata
) {
229 fprintf(stderr
, "Failed to get module information: %s\n", pa_strerror(pa_context_errno(c
)));
245 snprintf(t
, sizeof(t
), "%u", i
->n_used
);
247 printf("*** Module #%u ***\n"
250 "Usage counter: %s\n"
255 i
->n_used
!= PA_INVALID_INDEX
? t
: "n/a",
256 i
->auto_unload
? "yes" : "no");
259 static void get_client_info_callback(struct pa_context
*c
, const struct pa_client_info
*i
, int is_last
, void *userdata
) {
263 fprintf(stderr
, "Failed to get client information: %s\n", pa_strerror(pa_context_errno(c
)));
279 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
281 printf("*** Client #%u ***\n"
284 "Protocol Name: %s\n",
287 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
291 static void get_sink_input_info_callback(struct pa_context
*c
, const struct pa_sink_input_info
*i
, int is_last
, void *userdata
) {
292 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
295 fprintf(stderr
, "Failed to get sink input information: %s\n", pa_strerror(pa_context_errno(c
)));
311 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
);
312 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
313 snprintf(k
, sizeof(k
), "%u", i
->client
);
315 printf("*** Sink Input #%u ***\n"
320 "Sample Specification: %s\n"
321 "Volume: 0x%03x (%0.2f dB)\n"
322 "Buffer Latency: %0.0f usec\n"
323 "Sink Latency: %0.0f usec\n",
326 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
327 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
330 i
->volume
, pa_volume_to_dB(i
->volume
),
331 (double) i
->buffer_usec
,
332 (double) i
->sink_usec
);
335 static void get_source_output_info_callback(struct pa_context
*c
, const struct pa_source_output_info
*i
, int is_last
, void *userdata
) {
336 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
339 fprintf(stderr
, "Failed to get source output information: %s\n", pa_strerror(pa_context_errno(c
)));
355 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
);
356 snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
357 snprintf(k
, sizeof(k
), "%u", i
->client
);
359 printf("*** Source Output #%u ***\n"
364 "Sample Specification: %s\n"
365 "Buffer Latency: %0.0f usec\n"
366 "Source Latency: %0.0f usec\n",
369 i
->owner_module
!= PA_INVALID_INDEX
? t
: "n/a",
370 i
->client
!= PA_INVALID_INDEX
? k
: "n/a",
373 (double) i
->buffer_usec
,
374 (double) i
->source_usec
);
377 static void get_sample_info_callback(struct pa_context
*c
, const struct pa_sample_info
*i
, int is_last
, void *userdata
) {
378 char t
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
381 fprintf(stderr
, "Failed to get sample information: %s\n", pa_strerror(pa_context_errno(c
)));
397 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
);
398 pa_bytes_snprint(t
, sizeof(t
), i
->bytes
);
400 printf("*** Sample #%u ***\n"
402 "Volume: 0x%03x (%0.2f dB)\n"
403 "Sample Specification: %s\n"
410 i
->volume
, pa_volume_to_dB(i
->volume
),
411 pa_sample_spec_valid(&i
->sample_spec
) ? s
: "n/a",
412 (double) i
->duration
/1000000,
414 i
->lazy
? "yes" : "no",
415 i
->filename
? i
->filename
: "n/a");
418 static void get_autoload_info_callback(struct pa_context
*c
, const struct pa_autoload_info
*i
, int is_last
, void *userdata
) {
420 fprintf(stderr
, "Failed to get autoload information: %s\n", pa_strerror(pa_context_errno(c
)));
436 printf("*** Autoload Entry ***\n"
442 i
->type
== PA_AUTOLOAD_SINK
? "sink" : "source",
447 static void simple_callback(struct pa_context
*c
, int success
, void *userdata
) {
449 fprintf(stderr
, "Failure: %s\n", pa_strerror(pa_context_errno(c
)));
457 static void stream_state_callback(struct pa_stream
*s
, void *userdata
) {
460 switch (pa_stream_get_state(s
)) {
461 case PA_STREAM_CREATING
:
462 case PA_STREAM_READY
:
465 case PA_STREAM_TERMINATED
:
469 case PA_STREAM_FAILED
:
471 fprintf(stderr
, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
476 static void stream_write_callback(struct pa_stream
*s
, size_t length
, void *userdata
) {
479 assert(s
&& length
&& sndfile
);
484 assert(sample_length
>= length
);
485 l
= length
/pa_frame_size(&sample_spec
);
487 if ((sf_readf_float(sndfile
, d
, l
)) != l
) {
489 fprintf(stderr
, "Premature end of file\n");
493 pa_stream_write(s
, d
, length
, free
, 0);
495 sample_length
-= length
;
497 if (sample_length
<= 0) {
498 pa_stream_set_write_callback(sample_stream
, NULL
, NULL
);
499 pa_stream_finish_upload(sample_stream
);
503 static void context_state_callback(struct pa_context
*c
, void *userdata
) {
505 switch (pa_context_get_state(c
)) {
506 case PA_CONTEXT_CONNECTING
:
507 case PA_CONTEXT_AUTHORIZING
:
508 case PA_CONTEXT_SETTING_NAME
:
511 case PA_CONTEXT_READY
:
515 pa_operation_unref(pa_context_stat(c
, stat_callback
, NULL
));
516 pa_operation_unref(pa_context_get_server_info(c
, get_server_info_callback
, NULL
));
520 pa_operation_unref(pa_context_play_sample(c
, sample_name
, device
, PA_VOLUME_NORM
, simple_callback
, NULL
));
524 pa_operation_unref(pa_context_remove_sample(c
, sample_name
, simple_callback
, NULL
));
528 sample_stream
= pa_stream_new(c
, sample_name
, &sample_spec
);
529 assert(sample_stream
);
531 pa_stream_set_state_callback(sample_stream
, stream_state_callback
, NULL
);
532 pa_stream_set_write_callback(sample_stream
, stream_write_callback
, NULL
);
533 pa_stream_connect_upload(sample_stream
, sample_length
);
537 pa_context_exit_daemon(c
);
542 pa_operation_unref(pa_context_get_module_info_list(c
, get_module_info_callback
, NULL
));
543 pa_operation_unref(pa_context_get_sink_info_list(c
, get_sink_info_callback
, NULL
));
544 pa_operation_unref(pa_context_get_source_info_list(c
, get_source_info_callback
, NULL
));
545 pa_operation_unref(pa_context_get_sink_input_info_list(c
, get_sink_input_info_callback
, NULL
));
546 pa_operation_unref(pa_context_get_source_output_info_list(c
, get_source_output_info_callback
, NULL
));
547 pa_operation_unref(pa_context_get_client_info_list(c
, get_client_info_callback
, NULL
));
548 pa_operation_unref(pa_context_get_sample_info_list(c
, get_sample_info_callback
, NULL
));
549 pa_operation_unref(pa_context_get_autoload_info_list(c
, get_autoload_info_callback
, NULL
));
557 case PA_CONTEXT_TERMINATED
:
561 case PA_CONTEXT_FAILED
:
563 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
568 static void exit_signal_callback(struct pa_mainloop_api
*m
, struct pa_signal_event
*e
, int sig
, void *userdata
) {
569 fprintf(stderr
, "Got SIGINT, exiting.\n");
573 static void help(const char *argv0
) {
575 printf("%s [options] stat\n"
576 "%s [options] list\n"
577 "%s [options] exit\n"
578 "%s [options] upload-sample FILENAME [NAME]\n"
579 "%s [options] play-sample NAME [SINK]\n"
580 "%s [options] remove-sample NAME\n\n"
581 " -h, --help Show this help\n"
582 " --version Show version\n\n"
583 " -s, --server=SERVER The name of the server to connect to\n"
584 " -n, --client-name=NAME How to call this client on the server\n",
585 argv0
, argv0
, argv0
, argv0
, argv0
, argv0
);
588 enum { ARG_VERSION
= 256 };
590 int main(int argc
, char *argv
[]) {
591 struct pa_mainloop
* m
= NULL
;
594 char *server
= NULL
, *client_name
= NULL
, *bn
;
596 static const struct option long_options
[] = {
597 {"server", 1, NULL
, 's'},
598 {"client-name", 1, NULL
, 'n'},
599 {"version", 0, NULL
, ARG_VERSION
},
600 {"help", 0, NULL
, 'h'},
604 if (!(bn
= strrchr(argv
[0], '/')))
609 while ((c
= getopt_long(argc
, argv
, "s:n:h", long_options
, NULL
)) != -1) {
617 printf("pactl "PACKAGE_VERSION
"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
623 server
= strdup(optarg
);
628 client_name
= strdup(optarg
);
637 client_name
= strdup(bn
);
640 if (!strcmp(argv
[optind
], "stat"))
642 else if (!strcmp(argv
[optind
], "exit"))
644 else if (!strcmp(argv
[optind
], "list"))
646 else if (!strcmp(argv
[optind
], "upload-sample")) {
647 struct SF_INFO sfinfo
;
648 action
= UPLOAD_SAMPLE
;
650 if (optind
+1 >= argc
) {
651 fprintf(stderr
, "Please specify a sample file to load\n");
656 sample_name
= strdup(argv
[optind
+2]);
658 char *f
= strrchr(argv
[optind
+1], '/');
668 sample_name
= strdup(tmp
);
671 memset(&sfinfo
, 0, sizeof(sfinfo
));
672 if (!(sndfile
= sf_open(argv
[optind
+1], SFM_READ
, &sfinfo
))) {
673 fprintf(stderr
, "Failed to open sound file.\n");
677 sample_spec
.format
= PA_SAMPLE_FLOAT32
;
678 sample_spec
.rate
= sfinfo
.samplerate
;
679 sample_spec
.channels
= sfinfo
.channels
;
681 sample_length
= sfinfo
.frames
*pa_frame_size(&sample_spec
);
682 } else if (!strcmp(argv
[optind
], "play-sample")) {
683 action
= PLAY_SAMPLE
;
684 if (optind
+1 >= argc
) {
685 fprintf(stderr
, "You have to specify a sample name to play\n");
689 sample_name
= strdup(argv
[optind
+1]);
692 device
= strdup(argv
[optind
+2]);
694 } else if (!strcmp(argv
[optind
], "remove-sample")) {
695 action
= REMOVE_SAMPLE
;
696 if (optind
+1 >= argc
) {
697 fprintf(stderr
, "You have to specify a sample name to remove\n");
701 sample_name
= strdup(argv
[optind
+1]);
705 if (action
== NONE
) {
706 fprintf(stderr
, "No valid command specified.\n");
710 if (!(m
= pa_mainloop_new())) {
711 fprintf(stderr
, "pa_mainloop_new() failed.\n");
715 mainloop_api
= pa_mainloop_get_api(m
);
717 r
= pa_signal_init(mainloop_api
);
719 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
720 signal(SIGPIPE
, SIG_IGN
);
722 if (!(context
= pa_context_new(mainloop_api
, client_name
))) {
723 fprintf(stderr
, "pa_context_new() failed.\n");
727 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
728 pa_context_connect(context
, server
, 1, NULL
);
730 if (pa_mainloop_run(m
, &ret
) < 0) {
731 fprintf(stderr
, "pa_mainloop_run() failed.\n");
737 pa_stream_unref(sample_stream
);
740 pa_context_unref(context
);