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 #if PA_API_VERSION != PA_API_VERSION_0_6
41 #error Invalid Polypaudio API version
44 static enum { RECORD
, PLAYBACK
} mode
= PLAYBACK
;
46 static struct pa_context
*context
= NULL
;
47 static struct pa_stream
*stream
= NULL
;
48 static struct pa_mainloop_api
*mainloop_api
= NULL
;
50 static void *buffer
= NULL
;
51 static size_t buffer_length
= 0, buffer_index
= 0;
53 static struct pa_io_event
* stdio_event
= NULL
;
55 static char *stream_name
= NULL
, *client_name
= NULL
, *device
= NULL
;
57 static int verbose
= 0;
58 static pa_volume_t volume
= PA_VOLUME_NORM
;
60 /* A shortcut for terminating the application */
61 static void quit(int ret
) {
63 mainloop_api
->quit(mainloop_api
, ret
);
66 /* Write some data to the stream */
67 static void do_stream_write(size_t length
) {
71 if (!buffer
|| !buffer_length
)
75 if (l
> buffer_length
)
78 pa_stream_write(stream
, (uint8_t*) buffer
+ buffer_index
, l
, NULL
, 0);
85 buffer_index
= buffer_length
= 0;
89 /* This is called whenever new data may be written to the stream */
90 static void stream_write_callback(struct pa_stream
*s
, size_t length
, void *userdata
) {
94 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_INPUT
);
99 do_stream_write(length
);
102 /* This is called whenever new data may is available */
103 static void stream_read_callback(struct pa_stream
*s
, const void*data
, size_t length
, void *userdata
) {
104 assert(s
&& data
&& length
);
107 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_OUTPUT
);
110 fprintf(stderr
, "Buffer overrrun, dropping incoming data\n");
114 buffer
= malloc(buffer_length
= length
);
116 memcpy(buffer
, data
, length
);
120 /* This routine is called whenever the stream state changes */
121 static void stream_state_callback(struct pa_stream
*s
, void *userdata
) {
124 switch (pa_stream_get_state(s
)) {
125 case PA_STREAM_CREATING
:
126 case PA_STREAM_TERMINATED
:
129 case PA_STREAM_READY
:
131 fprintf(stderr
, "Stream successfully created\n");
134 case PA_STREAM_FAILED
:
136 fprintf(stderr
, "Stream errror: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
141 /* This is called whenever the context status changes */
142 static void context_state_callback(struct pa_context
*c
, void *userdata
) {
143 static const struct pa_sample_spec ss
= {
144 .format
= PA_SAMPLE_S16LE
,
151 switch (pa_context_get_state(c
)) {
152 case PA_CONTEXT_CONNECTING
:
153 case PA_CONTEXT_AUTHORIZING
:
154 case PA_CONTEXT_SETTING_NAME
:
157 case PA_CONTEXT_READY
:
159 assert(c
&& !stream
);
162 fprintf(stderr
, "Connection established.\n");
164 stream
= pa_stream_new(c
, stream_name
, &ss
);
167 pa_stream_set_state_callback(stream
, stream_state_callback
, NULL
);
168 pa_stream_set_write_callback(stream
, stream_write_callback
, NULL
);
169 pa_stream_set_read_callback(stream
, stream_read_callback
, NULL
);
171 if (mode
== PLAYBACK
)
172 pa_stream_connect_playback(stream
, device
, NULL
, 0, volume
);
174 pa_stream_connect_record(stream
, device
, NULL
, 0);
178 case PA_CONTEXT_TERMINATED
:
182 case PA_CONTEXT_FAILED
:
184 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
189 /* Connection draining complete */
190 static void context_drain_complete(struct pa_context
*c
, void *userdata
) {
191 pa_context_disconnect(c
);
194 /* Stream draining complete */
195 static void stream_drain_complete(struct pa_stream
*s
, int success
, void *userdata
) {
196 struct pa_operation
*o
;
199 fprintf(stderr
, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context
)));
204 fprintf(stderr
, "Playback stream drained.\n");
206 pa_stream_disconnect(stream
);
207 pa_stream_unref(stream
);
210 if (!(o
= pa_context_drain(context
, context_drain_complete
, NULL
)))
211 pa_context_disconnect(context
);
213 pa_operation_unref(o
);
216 fprintf(stderr
, "Draining connection to server.\n");
220 /* New data on STDIN **/
221 static void stdin_callback(struct pa_mainloop_api
*a
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
224 assert(a
== mainloop_api
&& e
&& stdio_event
== e
);
227 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_NULL
);
231 if (!stream
|| pa_stream_get_state(stream
) != PA_STREAM_READY
|| !(l
= w
= pa_stream_writable_size(stream
)))
236 if ((r
= read(fd
, buffer
, l
)) <= 0) {
239 fprintf(stderr
, "Got EOF.\n");
240 pa_operation_unref(pa_stream_drain(stream
, stream_drain_complete
, NULL
));
242 fprintf(stderr
, "read() failed: %s\n", strerror(errno
));
246 mainloop_api
->io_free(stdio_event
);
258 /* Some data may be written to STDOUT */
259 static void stdout_callback(struct pa_mainloop_api
*a
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
261 assert(a
== mainloop_api
&& e
&& stdio_event
== e
);
264 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_NULL
);
268 assert(buffer_length
);
270 if ((r
= write(fd
, (uint8_t*) buffer
+buffer_index
, buffer_length
)) <= 0) {
271 fprintf(stderr
, "write() failed: %s\n", strerror(errno
));
274 mainloop_api
->io_free(stdio_event
);
282 if (!buffer_length
) {
285 buffer_length
= buffer_index
= 0;
289 /* UNIX signal to quit recieved */
290 static void exit_signal_callback(struct pa_mainloop_api
*m
, struct pa_signal_event
*e
, int sig
, void *userdata
) {
292 fprintf(stderr
, "Got SIGINT, exiting.\n");
297 /* Show the current latency */
298 static void stream_get_latency_callback(struct pa_stream
*s
, const struct pa_latency_info
*i
, void *userdata
) {
303 fprintf(stderr
, "Failed to get latency: %s\n", pa_strerror(pa_context_errno(context
)));
308 if (mode
== PLAYBACK
)
309 total
= (double) i
->sink_usec
+ i
->buffer_usec
+ i
->transport_usec
;
311 total
= (double) i
->source_usec
+ i
->buffer_usec
+ i
->transport_usec
- i
->sink_usec
;
313 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",
314 (float) i
->buffer_usec
, (float) i
->sink_usec
, (float) i
->source_usec
, (float) i
->transport_usec
, total
,
315 i
->synchronized_clocks
? "yes" : "no");
318 /* Someone requested that the latency is shown */
319 static void sigusr1_signal_callback(struct pa_mainloop_api
*m
, struct pa_signal_event
*e
, int sig
, void *userdata
) {
320 fprintf(stderr
, "Got SIGUSR1, requesting latency.\n");
321 pa_operation_unref(pa_stream_get_latency(stream
, stream_get_latency_callback
, NULL
));
325 static void help(const char *argv0
) {
327 printf("%s [options]\n\n"
328 " -h, --help Show this help\n"
329 " --version Show version\n\n"
330 " -r, --record Create a connection for recording\n"
331 " -p, --playback Create a connection for playback\n\n"
332 " -v, --verbose Enable verbose operations\n\n"
333 " -s, --server=SERVER The name of the server to connect to\n"
334 " -d, --device=DEVICE The name of the sink/source to connect to\n"
335 " -n, --client-name=NAME How to call this client on the server\n"
336 " --stream-name=NAME How to call this stream on the server\n"
337 " --volume=VOLUME Specify the initial (linear) volume in range 0...256\n",
347 int main(int argc
, char *argv
[]) {
348 struct pa_mainloop
* m
= NULL
;
350 char *bn
, *server
= NULL
;
352 static const struct option long_options
[] = {
353 {"record", 0, NULL
, 'r'},
354 {"playback", 0, NULL
, 'p'},
355 {"device", 1, NULL
, 'd'},
356 {"server", 1, NULL
, 's'},
357 {"client-name", 1, NULL
, 'n'},
358 {"stream-name", 1, NULL
, ARG_STREAM_NAME
},
359 {"version", 0, NULL
, ARG_VERSION
},
360 {"help", 0, NULL
, 'h'},
361 {"verbose", 0, NULL
, 'v'},
362 {"volume", 1, NULL
, ARG_VOLUME
},
366 if (!(bn
= strrchr(argv
[0], '/')))
371 if (strstr(bn
, "rec") || strstr(bn
, "mon"))
373 else if (strstr(bn
, "cat") || strstr(bn
, "play"))
376 while ((c
= getopt_long(argc
, argv
, "rpd:s:n:hv", long_options
, NULL
)) != -1) {
385 printf("pacat "PACKAGE_VERSION
"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
399 device
= strdup(optarg
);
404 server
= strdup(optarg
);
409 client_name
= strdup(optarg
);
412 case ARG_STREAM_NAME
:
414 stream_name
= strdup(optarg
);
422 int v
= atoi(optarg
);
423 volume
= v
< 0 ? 0 : v
;
433 client_name
= strdup(bn
);
436 stream_name
= strdup(client_name
);
439 fprintf(stderr
, "Opening a %s stream.\n", mode
== RECORD
? "recording" : "playback");
441 /* Set up a new main loop */
442 if (!(m
= pa_mainloop_new())) {
443 fprintf(stderr
, "pa_mainloop_new() failed.\n");
447 mainloop_api
= pa_mainloop_get_api(m
);
449 r
= pa_signal_init(mainloop_api
);
451 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
452 pa_signal_new(SIGUSR1
, sigusr1_signal_callback
, NULL
);
453 signal(SIGPIPE
, SIG_IGN
);
455 if (!(stdio_event
= mainloop_api
->io_new(mainloop_api
,
456 mode
== PLAYBACK
? STDIN_FILENO
: STDOUT_FILENO
,
457 mode
== PLAYBACK
? PA_IO_EVENT_INPUT
: PA_IO_EVENT_OUTPUT
,
458 mode
== PLAYBACK
? stdin_callback
: stdout_callback
, NULL
))) {
459 fprintf(stderr
, "source_io() failed.\n");
463 /* Create a new connection context */
464 if (!(context
= pa_context_new(mainloop_api
, client_name
))) {
465 fprintf(stderr
, "pa_context_new() failed.\n");
469 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
471 /* Connect the context */
472 pa_context_connect(context
, server
, 1, NULL
);
474 /* Run the main loop */
475 if (pa_mainloop_run(m
, &ret
) < 0) {
476 fprintf(stderr
, "pa_mainloop_run() failed.\n");
482 pa_stream_unref(stream
);
485 pa_context_unref(context
);
488 assert(mainloop_api
);
489 mainloop_api
->io_free(stdio_event
);