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
36 #include <polyp/polypaudio.h>
37 #include <polyp/mainloop.h>
38 #include <polyp/mainloop-signal.h>
39 #include <polypcore/util.h>
41 #define TIME_EVENT_USEC 50000
43 #if PA_API_VERSION != 8
44 #error Invalid Polypaudio API version
47 static enum { RECORD
, PLAYBACK
} mode
= PLAYBACK
;
49 static pa_context
*context
= NULL
;
50 static pa_stream
*stream
= NULL
;
51 static pa_mainloop_api
*mainloop_api
= NULL
;
53 static void *buffer
= NULL
;
54 static size_t buffer_length
= 0, buffer_index
= 0;
56 static pa_io_event
* stdio_event
= NULL
;
58 static char *stream_name
= NULL
, *client_name
= NULL
, *device
= NULL
;
60 static int verbose
= 0;
61 static pa_volume_t volume
= PA_VOLUME_NORM
;
63 static pa_sample_spec sample_spec
= {
64 .format
= PA_SAMPLE_S16LE
,
69 /* A shortcut for terminating the application */
70 static void quit(int ret
) {
72 mainloop_api
->quit(mainloop_api
, ret
);
75 /* Write some data to the stream */
76 static void do_stream_write(size_t length
) {
80 if (!buffer
|| !buffer_length
)
84 if (l
> buffer_length
)
87 if (pa_stream_write(stream
, (uint8_t*) buffer
+ buffer_index
, l
, NULL
, 0, PA_SEEK_RELATIVE
) < 0) {
88 fprintf(stderr
, "pa_stream_write() failed: %s\n", pa_strerror(pa_context_errno(context
)));
99 buffer_index
= buffer_length
= 0;
103 /* This is called whenever new data may be written to the stream */
104 static void stream_write_callback(pa_stream
*s
, size_t length
, void *userdata
) {
108 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_INPUT
);
113 do_stream_write(length
);
116 /* This is called whenever new data may is available */
117 static void stream_read_callback(pa_stream
*s
, size_t length
, void *userdata
) {
122 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_OUTPUT
);
124 if (pa_stream_peek(s
, &data
, &length
) < 0) {
125 fprintf(stderr
, "pa_stream_peek() failed: %s\n", pa_strerror(pa_context_errno(context
)));
130 assert(data
&& length
);
133 fprintf(stderr
, "Buffer overrun, dropping incoming data\n");
134 if (pa_stream_drop(s
) < 0) {
135 fprintf(stderr
, "pa_stream_drop() failed: %s\n", pa_strerror(pa_context_errno(context
)));
141 buffer
= malloc(buffer_length
= length
);
143 memcpy(buffer
, data
, length
);
148 /* This routine is called whenever the stream state changes */
149 static void stream_state_callback(pa_stream
*s
, void *userdata
) {
152 switch (pa_stream_get_state(s
)) {
153 case PA_STREAM_CREATING
:
154 case PA_STREAM_TERMINATED
:
157 case PA_STREAM_READY
:
159 fprintf(stderr
, "Stream successfully created.\n");
162 case PA_STREAM_FAILED
:
164 fprintf(stderr
, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
169 /* This is called whenever the context status changes */
170 static void context_state_callback(pa_context
*c
, void *userdata
) {
173 switch (pa_context_get_state(c
)) {
174 case PA_CONTEXT_CONNECTING
:
175 case PA_CONTEXT_AUTHORIZING
:
176 case PA_CONTEXT_SETTING_NAME
:
179 case PA_CONTEXT_READY
: {
182 assert(c
&& !stream
);
185 fprintf(stderr
, "Connection established.\n");
187 if (!(stream
= pa_stream_new(c
, stream_name
, &sample_spec
, NULL
))) {
188 fprintf(stderr
, "pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(c
)));
192 pa_stream_set_state_callback(stream
, stream_state_callback
, NULL
);
193 pa_stream_set_write_callback(stream
, stream_write_callback
, NULL
);
194 pa_stream_set_read_callback(stream
, stream_read_callback
, NULL
);
196 if (mode
== PLAYBACK
) {
198 if ((r
= pa_stream_connect_playback(stream
, device
, NULL
, 0, pa_cvolume_set(&cv
, sample_spec
.channels
, volume
), NULL
)) < 0) {
199 fprintf(stderr
, "pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(c
)));
204 if ((r
= pa_stream_connect_record(stream
, device
, NULL
, 0)) < 0) {
205 fprintf(stderr
, "pa_stream_connect_record() failed: %s\n", pa_strerror(pa_context_errno(c
)));
213 case PA_CONTEXT_TERMINATED
:
217 case PA_CONTEXT_FAILED
:
219 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
230 /* Connection draining complete */
231 static void context_drain_complete(pa_context
*c
, void *userdata
) {
232 pa_context_disconnect(c
);
235 /* Stream draining complete */
236 static void stream_drain_complete(pa_stream
*s
, int success
, void *userdata
) {
240 fprintf(stderr
, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context
)));
245 fprintf(stderr
, "Playback stream drained.\n");
247 pa_stream_disconnect(stream
);
248 pa_stream_unref(stream
);
251 if (!(o
= pa_context_drain(context
, context_drain_complete
, NULL
)))
252 pa_context_disconnect(context
);
255 fprintf(stderr
, "Draining connection to server.\n");
259 /* New data on STDIN **/
260 static void stdin_callback(pa_mainloop_api
*a
, pa_io_event
*e
, int fd
, pa_io_event_flags_t f
, void *userdata
) {
263 assert(a
== mainloop_api
&& e
&& stdio_event
== e
);
266 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_NULL
);
270 if (!stream
|| pa_stream_get_state(stream
) != PA_STREAM_READY
|| !(l
= w
= pa_stream_writable_size(stream
)))
275 if ((r
= read(fd
, buffer
, l
)) <= 0) {
280 fprintf(stderr
, "Got EOF.\n");
282 if (!(o
= pa_stream_drain(stream
, stream_drain_complete
, NULL
))) {
283 fprintf(stderr
, "pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(context
)));
288 pa_operation_unref(o
);
290 fprintf(stderr
, "read() failed: %s\n", strerror(errno
));
294 mainloop_api
->io_free(stdio_event
);
306 /* Some data may be written to STDOUT */
307 static void stdout_callback(pa_mainloop_api
*a
, pa_io_event
*e
, int fd
, pa_io_event_flags_t f
, void *userdata
) {
309 assert(a
== mainloop_api
&& e
&& stdio_event
== e
);
312 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_NULL
);
316 assert(buffer_length
);
318 if ((r
= write(fd
, (uint8_t*) buffer
+buffer_index
, buffer_length
)) <= 0) {
319 fprintf(stderr
, "write() failed: %s\n", strerror(errno
));
322 mainloop_api
->io_free(stdio_event
);
330 if (!buffer_length
) {
333 buffer_length
= buffer_index
= 0;
337 /* UNIX signal to quit recieved */
338 static void exit_signal_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
340 fprintf(stderr
, "Got signal, exiting.\n");
345 /* Show the current latency */
346 static void stream_update_timing_callback(pa_stream
*s
, int success
, void *userdata
) {
347 pa_usec_t latency
, usec
;
353 pa_stream_get_time(s
, &usec
) < 0 ||
354 pa_stream_get_latency(s
, &latency
, &negative
) < 0) {
355 fprintf(stderr
, "Failed to get latency: %s\n", pa_strerror(pa_context_errno(context
)));
360 fprintf(stderr
, "Time: %0.3f sec; Latency: %0.0f usec. \r",
361 (float) usec
/ 1000000,
362 (float) latency
* (negative
?-1:1));
365 /* Someone requested that the latency is shown */
366 static void sigusr1_signal_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
371 pa_operation_unref(pa_stream_update_timing_info(stream
, stream_update_timing_callback
, NULL
));
374 static void time_event_callback(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
380 pa_operation_unref(pa_stream_update_timing_info(stream
, stream_update_timing_callback
, NULL
));
382 pa_gettimeofday(&next
);
383 pa_timeval_add(&next
, TIME_EVENT_USEC
);
385 m
->time_restart(e
, &next
);
388 static void help(const char *argv0
) {
390 printf("%s [options]\n\n"
391 " -h, --help Show this help\n"
392 " --version Show version\n\n"
393 " -r, --record Create a connection for recording\n"
394 " -p, --playback Create a connection for playback\n\n"
395 " -v, --verbose Enable verbose operations\n\n"
396 " -s, --server=SERVER The name of the server to connect to\n"
397 " -d, --device=DEVICE The name of the sink/source to connect to\n"
398 " -n, --client-name=NAME How to call this client on the server\n"
399 " --stream-name=NAME How to call this stream on the server\n"
400 " --volume=VOLUME Specify the initial (linear) volume in range 0...256\n"
401 " --rate=SAMPLERATE The sample rate in Hz (defaults to 44100)\n"
402 " --format=SAMPLEFORMAT The sample type, one of s16le, s16be, u8, float32le,\n"
403 " float32be, ulaw, alaw (defaults to s16ne)\n"
404 " --channels=CHANNELS The number of channels, 1 for mono, 2 for stereo\n"
405 " (defaults to 2)\n",
418 int main(int argc
, char *argv
[]) {
419 pa_mainloop
* m
= NULL
;
421 char *bn
, *server
= NULL
;
422 pa_time_event
*time_event
= NULL
;
424 static const struct option long_options
[] = {
425 {"record", 0, NULL
, 'r'},
426 {"playback", 0, NULL
, 'p'},
427 {"device", 1, NULL
, 'd'},
428 {"server", 1, NULL
, 's'},
429 {"client-name", 1, NULL
, 'n'},
430 {"stream-name", 1, NULL
, ARG_STREAM_NAME
},
431 {"version", 0, NULL
, ARG_VERSION
},
432 {"help", 0, NULL
, 'h'},
433 {"verbose", 0, NULL
, 'v'},
434 {"volume", 1, NULL
, ARG_VOLUME
},
435 {"rate", 1, NULL
, ARG_SAMPLERATE
},
436 {"format", 1, NULL
, ARG_SAMPLEFORMAT
},
437 {"channels", 1, NULL
, ARG_CHANNELS
},
441 if (!(bn
= strrchr(argv
[0], '/')))
446 if (strstr(bn
, "rec") || strstr(bn
, "mon"))
448 else if (strstr(bn
, "cat") || strstr(bn
, "play"))
451 while ((c
= getopt_long(argc
, argv
, "rpd:s:n:hv", long_options
, NULL
)) != -1) {
460 printf("pacat "PACKAGE_VERSION
"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
474 device
= strdup(optarg
);
479 server
= strdup(optarg
);
484 client_name
= strdup(optarg
);
487 case ARG_STREAM_NAME
:
489 stream_name
= strdup(optarg
);
497 int v
= atoi(optarg
);
498 volume
= v
< 0 ? 0 : v
;
503 sample_spec
.channels
= atoi(optarg
);
506 case ARG_SAMPLEFORMAT
:
507 sample_spec
.format
= pa_parse_sample_format(optarg
);
511 sample_spec
.rate
= atoi(optarg
);
520 client_name
= strdup(bn
);
523 stream_name
= strdup(client_name
);
525 if (!pa_sample_spec_valid(&sample_spec
)) {
526 fprintf(stderr
, "Invalid sample specification\n");
531 char t
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
532 pa_sample_spec_snprint(t
, sizeof(t
), &sample_spec
);
533 fprintf(stderr
, "Opening a %s stream with sample specification '%s'.\n", mode
== RECORD
? "recording" : "playback", t
);
536 if (!(optind
>= argc
)) {
537 if (optind
+1 == argc
) {
540 if ((fd
= open(argv
[optind
], mode
== PLAYBACK
? O_RDONLY
: O_WRONLY
|O_TRUNC
|O_CREAT
)) < 0) {
541 fprintf(stderr
, "open(): %s\n", strerror(errno
));
545 if (dup2(fd
, mode
== PLAYBACK
? 0 : 1) < 0) {
546 fprintf(stderr
, "dup2(): %s\n", strerror(errno
));
552 fprintf(stderr
, "Too many arguments.\n");
557 /* Set up a new main loop */
558 if (!(m
= pa_mainloop_new())) {
559 fprintf(stderr
, "pa_mainloop_new() failed.\n");
563 mainloop_api
= pa_mainloop_get_api(m
);
565 r
= pa_signal_init(mainloop_api
);
567 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
568 pa_signal_new(SIGTERM
, exit_signal_callback
, NULL
);
570 pa_signal_new(SIGUSR1
, sigusr1_signal_callback
, NULL
);
573 signal(SIGPIPE
, SIG_IGN
);
576 if (!(stdio_event
= mainloop_api
->io_new(mainloop_api
,
577 mode
== PLAYBACK
? STDIN_FILENO
: STDOUT_FILENO
,
578 mode
== PLAYBACK
? PA_IO_EVENT_INPUT
: PA_IO_EVENT_OUTPUT
,
579 mode
== PLAYBACK
? stdin_callback
: stdout_callback
, NULL
))) {
580 fprintf(stderr
, "io_new() failed.\n");
584 /* Create a new connection context */
585 if (!(context
= pa_context_new(mainloop_api
, client_name
))) {
586 fprintf(stderr
, "pa_context_new() failed.\n");
590 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
592 /* Connect the context */
593 pa_context_connect(context
, server
, 0, NULL
);
598 pa_gettimeofday(&tv
);
599 pa_timeval_add(&tv
, TIME_EVENT_USEC
);
601 if (!(time_event
= mainloop_api
->time_new(mainloop_api
, &tv
, time_event_callback
, NULL
))) {
602 fprintf(stderr
, "time_new() failed.\n");
607 /* Run the main loop */
608 if (pa_mainloop_run(m
, &ret
) < 0) {
609 fprintf(stderr
, "pa_mainloop_run() failed.\n");
615 pa_stream_unref(stream
);
618 pa_context_unref(context
);
621 assert(mainloop_api
);
622 mainloop_api
->io_free(stdio_event
);
626 assert(mainloop_api
);
627 mainloop_api
->time_free(time_event
);