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
35 #include <polyp/polyplib.h>
36 #include <polyp/polyplib-error.h>
37 #include <polyp/mainloop.h>
38 #include <polyp/mainloop-signal.h>
40 static enum { RECORD
, PLAYBACK
} mode
= PLAYBACK
;
42 static struct pa_context
*context
= NULL
;
43 static struct pa_stream
*stream
= NULL
;
44 static struct pa_mainloop_api
*mainloop_api
= NULL
;
46 static void *buffer
= NULL
;
47 static size_t buffer_length
= 0, buffer_index
= 0;
49 static struct pa_io_event
* stdio_event
= NULL
;
51 static char *stream_name
= NULL
, *client_name
= NULL
, *device
= NULL
;
53 static int verbose
= 0;
54 static pa_volume_t volume
= PA_VOLUME_NORM
;
56 /* A shortcut for terminating the application */
57 static void quit(int ret
) {
59 mainloop_api
->quit(mainloop_api
, ret
);
62 /* Write some data to the stream */
63 static void do_stream_write(size_t length
) {
67 if (!buffer
|| !buffer_length
)
71 if (l
> buffer_length
)
74 pa_stream_write(stream
, (uint8_t*) buffer
+ buffer_index
, l
, NULL
, 0);
81 buffer_index
= buffer_length
= 0;
85 /* This is called whenever new data may be written to the stream */
86 static void stream_write_callback(struct pa_stream
*s
, size_t length
, void *userdata
) {
90 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_INPUT
);
95 do_stream_write(length
);
98 /* This is called whenever new data may is available */
99 static void stream_read_callback(struct pa_stream
*s
, const void*data
, size_t length
, void *userdata
) {
100 assert(s
&& data
&& length
);
103 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_OUTPUT
);
106 fprintf(stderr
, "Buffer overrrun, dropping incoming data\n");
110 buffer
= malloc(buffer_length
= length
);
112 memcpy(buffer
, data
, length
);
116 /* This routine is called whenever the stream state changes */
117 static void stream_state_callback(struct pa_stream
*s
, void *userdata
) {
120 switch (pa_stream_get_state(s
)) {
121 case PA_STREAM_CREATING
:
122 case PA_STREAM_TERMINATED
:
125 case PA_STREAM_READY
:
127 fprintf(stderr
, "Stream successfully created\n");
130 case PA_STREAM_FAILED
:
132 fprintf(stderr
, "Stream errror: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
137 /* This is called whenever the context status changes */
138 static void context_state_callback(struct pa_context
*c
, void *userdata
) {
139 static const struct pa_sample_spec ss
= {
140 .format
= PA_SAMPLE_S16LE
,
147 switch (pa_context_get_state(c
)) {
148 case PA_CONTEXT_CONNECTING
:
149 case PA_CONTEXT_AUTHORIZING
:
150 case PA_CONTEXT_SETTING_NAME
:
153 case PA_CONTEXT_READY
:
155 assert(c
&& !stream
);
158 fprintf(stderr
, "Connection established.\n");
160 stream
= pa_stream_new(c
, stream_name
, &ss
);
163 pa_stream_set_state_callback(stream
, stream_state_callback
, NULL
);
164 pa_stream_set_write_callback(stream
, stream_write_callback
, NULL
);
165 pa_stream_set_read_callback(stream
, stream_read_callback
, NULL
);
167 if (mode
== PLAYBACK
)
168 pa_stream_connect_playback(stream
, device
, NULL
, volume
);
170 pa_stream_connect_record(stream
, device
, NULL
);
174 case PA_CONTEXT_TERMINATED
:
178 case PA_CONTEXT_FAILED
:
180 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
185 /* Connection draining complete */
186 static void context_drain_complete(struct pa_context
*c
, void *userdata
) {
187 pa_context_disconnect(c
);
190 /* Stream draining complete */
191 static void stream_drain_complete(struct pa_stream
*s
, int success
, void *userdata
) {
192 struct pa_operation
*o
;
195 fprintf(stderr
, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context
)));
200 fprintf(stderr
, "Playback stream drained.\n");
202 pa_stream_disconnect(stream
);
203 pa_stream_unref(stream
);
206 if (!(o
= pa_context_drain(context
, context_drain_complete
, NULL
)))
207 pa_context_disconnect(context
);
209 pa_operation_unref(o
);
212 fprintf(stderr
, "Draining connection to server.\n");
216 /* New data on STDIN **/
217 static void stdin_callback(struct pa_mainloop_api
*a
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
220 assert(a
== mainloop_api
&& e
&& stdio_event
== e
);
223 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_NULL
);
227 if (!stream
|| pa_stream_get_state(stream
) != PA_STREAM_READY
|| !(l
= w
= pa_stream_writable_size(stream
)))
232 if ((r
= read(fd
, buffer
, l
)) <= 0) {
235 fprintf(stderr
, "Got EOF.\n");
236 pa_operation_unref(pa_stream_drain(stream
, stream_drain_complete
, NULL
));
238 fprintf(stderr
, "read() failed: %s\n", strerror(errno
));
242 mainloop_api
->io_free(stdio_event
);
254 /* Some data may be written to STDOUT */
255 static void stdout_callback(struct pa_mainloop_api
*a
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
257 assert(a
== mainloop_api
&& e
&& stdio_event
== e
);
260 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_NULL
);
264 assert(buffer_length
);
266 if ((r
= write(fd
, (uint8_t*) buffer
+buffer_index
, buffer_length
)) <= 0) {
267 fprintf(stderr
, "write() failed: %s\n", strerror(errno
));
270 mainloop_api
->io_free(stdio_event
);
278 if (!buffer_length
) {
281 buffer_length
= buffer_index
= 0;
285 /* UNIX signal to quit recieved */
286 static void exit_signal_callback(struct pa_mainloop_api
*m
, struct pa_signal_event
*e
, int sig
, void *userdata
) {
288 fprintf(stderr
, "Got SIGINT, exiting.\n");
293 /* Show the current latency */
294 static void stream_get_latency_callback(struct pa_stream
*s
, const struct pa_latency_info
*i
, void *userdata
) {
299 fprintf(stderr
, "Failed to get latency: %s\n", pa_strerror(pa_context_errno(context
)));
304 if (mode
== PLAYBACK
)
305 total
= (double) i
->sink_usec
+ i
->buffer_usec
+ i
->transport_usec
;
307 total
= (double) i
->source_usec
+ i
->buffer_usec
+ i
->transport_usec
- i
->sink_usec
;
309 fprintf(stderr
, "Latency: buffer: %0.0f usec; sink: %0.0f usec; source: %0.0f usec; transport: %0.0f usec; total: %0.0f usec; synchronized clocks: %s.\n",
310 (float) i
->buffer_usec
, (float) i
->sink_usec
, (float) i
->source_usec
, (float) i
->transport_usec
, total
,
311 i
->synchronized_clocks
? "yes" : "no");
314 /* Someone requested that the latency is shown */
315 static void sigusr1_signal_callback(struct pa_mainloop_api
*m
, struct pa_signal_event
*e
, int sig
, void *userdata
) {
316 fprintf(stderr
, "Got SIGUSR1, requesting latency.\n");
317 pa_operation_unref(pa_stream_get_latency(stream
, stream_get_latency_callback
, NULL
));
321 static void help(const char *argv0
) {
323 printf("%s [options]\n"
324 " -h, --help Show this help\n"
325 " --version Show version\n\n"
326 " -r, --record Create a connection for recording\n"
327 " -p, --playback Create a connection for playback\n\n"
328 " -v, --verbose Enable verbose operations\n\n"
329 " -s, --server=SERVER The name of the server to connect to\n"
330 " -d, --device=DEVICE The name of the sink/source to connect to\n"
331 " -n, --client-name=NAME How to call this client on the server\n"
332 " --stream-name=NAME How to call this stream on the server\n"
333 " --volume=VOLUME Specify the initial (linear) volume in range 0...256\n",
343 int main(int argc
, char *argv
[]) {
344 struct pa_mainloop
* m
= NULL
;
346 char *bn
, *server
= NULL
;
348 static const struct option long_options
[] = {
349 {"record", 0, NULL
, 'r'},
350 {"playback", 0, NULL
, 'p'},
351 {"device", 1, NULL
, 'd'},
352 {"server", 1, NULL
, 's'},
353 {"client-name", 1, NULL
, 'n'},
354 {"stream-name", 1, NULL
, ARG_STREAM_NAME
},
355 {"version", 0, NULL
, ARG_VERSION
},
356 {"help", 0, NULL
, 'h'},
357 {"verbose", 0, NULL
, 'v'},
358 {"volume", 1, NULL
, ARG_VOLUME
},
362 if (!(bn
= strrchr(argv
[0], '/')))
367 if (strstr(bn
, "rec") || strstr(bn
, "mon"))
369 else if (strstr(bn
, "cat") || strstr(bn
, "play"))
372 while ((c
= getopt_long(argc
, argv
, "rpd:s:n:hv", long_options
, NULL
)) != -1) {
381 printf("pacat "PACKAGE_VERSION
"\n");
395 device
= strdup(optarg
);
400 server
= strdup(optarg
);
405 client_name
= strdup(optarg
);
408 case ARG_STREAM_NAME
:
410 stream_name
= strdup(optarg
);
418 int v
= atoi(optarg
);
419 volume
= v
< 0 ? 0 : v
;
429 client_name
= strdup(bn
);
432 stream_name
= strdup(client_name
);
435 fprintf(stderr
, "Opening a %s stream.\n", mode
== RECORD
? "recording" : "playback");
437 /* Set up a new main loop */
438 if (!(m
= pa_mainloop_new())) {
439 fprintf(stderr
, "pa_mainloop_new() failed.\n");
443 mainloop_api
= pa_mainloop_get_api(m
);
445 r
= pa_signal_init(mainloop_api
);
447 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
448 pa_signal_new(SIGUSR1
, sigusr1_signal_callback
, NULL
);
449 signal(SIGPIPE
, SIG_IGN
);
451 if (!(stdio_event
= mainloop_api
->io_new(mainloop_api
,
452 mode
== PLAYBACK
? STDIN_FILENO
: STDOUT_FILENO
,
453 mode
== PLAYBACK
? PA_IO_EVENT_INPUT
: PA_IO_EVENT_OUTPUT
,
454 mode
== PLAYBACK
? stdin_callback
: stdout_callback
, NULL
))) {
455 fprintf(stderr
, "source_io() failed.\n");
459 /* Create a new connection context */
460 if (!(context
= pa_context_new(mainloop_api
, client_name
))) {
461 fprintf(stderr
, "pa_context_new() failed.\n");
465 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
467 /* Connect the context */
468 pa_context_connect(context
, server
, 1, NULL
);
470 /* Run the main loop */
471 if (pa_mainloop_run(m
, &ret
) < 0) {
472 fprintf(stderr
, "pa_mainloop_run() failed.\n");
478 pa_stream_unref(stream
);
481 pa_context_unref(context
);
484 assert(mainloop_api
);
485 mainloop_api
->io_free(stdio_event
);