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
34 #include <polyp/polyplib.h>
35 #include <polyp/polyplib-error.h>
36 #include <polyp/mainloop.h>
37 #include <polyp/mainloop-signal.h>
39 static enum { RECORD
, PLAYBACK
} mode
= PLAYBACK
;
41 static struct pa_context
*context
= NULL
;
42 static struct pa_stream
*stream
= NULL
;
43 static struct pa_mainloop_api
*mainloop_api
= NULL
;
45 static void *buffer
= NULL
;
46 static size_t buffer_length
= 0, buffer_index
= 0;
48 static void* stdio_source
= NULL
;
50 static void quit(int ret
) {
52 mainloop_api
->quit(mainloop_api
, ret
);
55 static void context_die_callback(struct pa_context
*c
, void *userdata
) {
57 fprintf(stderr
, "Connection to server shut down, exiting.\n");
61 static void stream_die_callback(struct pa_stream
*s
, void *userdata
) {
63 fprintf(stderr
, "Stream deleted, exiting.\n");
67 static void do_stream_write(size_t length
) {
71 if (!buffer
|| !buffer_length
)
75 if (l
> buffer_length
)
78 pa_stream_write(stream
, buffer
+buffer_index
, l
);
85 buffer_index
= buffer_length
= 0;
89 static void stream_write_callback(struct pa_stream
*s
, size_t length
, void *userdata
) {
93 mainloop_api
->enable_io(mainloop_api
, stdio_source
, PA_MAINLOOP_API_IO_EVENT_INPUT
);
98 do_stream_write(length
);
101 static void stream_read_callback(struct pa_stream
*s
, const void*data
, size_t length
, void *userdata
) {
102 assert(s
&& data
&& length
);
105 mainloop_api
->enable_io(mainloop_api
, stdio_source
, PA_MAINLOOP_API_IO_EVENT_OUTPUT
);
108 fprintf(stderr
, "Buffer overrrun, dropping incoming data\n");
112 buffer
= malloc(buffer_length
= length
);
114 memcpy(buffer
, data
, length
);
118 static void stream_complete_callback(struct pa_stream
*s
, int success
, void *userdata
) {
122 fprintf(stderr
, "Stream creation failed: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
127 fprintf(stderr
, "Stream created.\n");
130 static void context_complete_callback(struct pa_context
*c
, int success
, void *userdata
) {
131 static const struct pa_sample_spec ss
= {
132 .format
= PA_SAMPLE_S16LE
,
137 assert(c
&& !stream
);
140 fprintf(stderr
, "Connection failed: %s\n", pa_strerror(pa_context_errno(c
)));
144 fprintf(stderr
, "Connection established.\n");
146 if (!(stream
= pa_stream_new(c
, mode
== PLAYBACK
? PA_STREAM_PLAYBACK
: PA_STREAM_RECORD
, NULL
, "pacat", &ss
, NULL
, stream_complete_callback
, NULL
))) {
147 fprintf(stderr
, "pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(c
)));
151 pa_stream_set_die_callback(stream
, stream_die_callback
, NULL
);
152 pa_stream_set_write_callback(stream
, stream_write_callback
, NULL
);
153 pa_stream_set_read_callback(stream
, stream_read_callback
, NULL
);
161 static void context_drain_complete(struct pa_context
*c
, void *userdata
) {
165 static void stream_drain_complete(struct pa_stream
*s
, void *userdata
) {
166 fprintf(stderr
, "Playback stream drained.\n");
168 pa_stream_free(stream
);
171 if (pa_context_drain(context
, context_drain_complete
, NULL
) < 0)
174 fprintf(stderr
, "Draining connection to server.\n");
177 static void stdin_callback(struct pa_mainloop_api
*a
, void *id
, int fd
, enum pa_mainloop_api_io_events events
, void *userdata
) {
180 assert(a
== mainloop_api
&& id
&& stdio_source
== id
);
183 mainloop_api
->enable_io(mainloop_api
, stdio_source
, PA_MAINLOOP_API_IO_EVENT_NULL
);
187 if (!stream
|| !pa_stream_is_ready(stream
) || !(l
= w
= pa_stream_writable_size(stream
)))
192 if ((r
= read(fd
, buffer
, l
)) <= 0) {
194 fprintf(stderr
, "Got EOF.\n");
195 pa_stream_drain(stream
, stream_drain_complete
, NULL
);
197 fprintf(stderr
, "read() failed: %s\n", strerror(errno
));
201 mainloop_api
->cancel_io(mainloop_api
, stdio_source
);
213 static void stdout_callback(struct pa_mainloop_api
*a
, void *id
, int fd
, enum pa_mainloop_api_io_events events
, void *userdata
) {
215 assert(a
== mainloop_api
&& id
&& stdio_source
== id
);
218 mainloop_api
->enable_io(mainloop_api
, stdio_source
, PA_MAINLOOP_API_IO_EVENT_NULL
);
222 assert(buffer_length
);
224 if ((r
= write(fd
, buffer
+buffer_index
, buffer_length
)) <= 0) {
225 fprintf(stderr
, "write() failed: %s\n", strerror(errno
));
228 mainloop_api
->cancel_io(mainloop_api
, stdio_source
);
236 if (!buffer_length
) {
239 buffer_length
= buffer_index
= 0;
243 static void exit_signal_callback(void *id
, int sig
, void *userdata
) {
244 fprintf(stderr
, "Got SIGINT, exiting.\n");
249 static void stream_get_latency_callback(struct pa_stream
*s
, uint32_t latency
, void *userdata
) {
252 if (latency
== (uint32_t) -1) {
253 fprintf(stderr
, "Failed to get latency: %s\n", strerror(errno
));
258 fprintf(stderr
, "Current latency is %u usecs.\n", latency
);
261 static void sigusr1_signal_callback(void *id
, int sig
, void *userdata
) {
262 if (mode
== PLAYBACK
) {
263 fprintf(stderr
, "Got SIGUSR1, requesting latency.\n");
264 pa_stream_get_latency(stream
, stream_get_latency_callback
, NULL
);
268 int main(int argc
, char *argv
[]) {
269 struct pa_mainloop
* m
= NULL
;
273 if (!(bn
= strrchr(argv
[0], '/')))
276 if (strstr(bn
, "rec") || strstr(bn
, "mon"))
278 else if (strstr(bn
, "cat") || strstr(bn
, "play"))
281 fprintf(stderr
, "Opening a %s stream.\n", mode
== RECORD
? "recording" : "playback");
283 if (!(m
= pa_mainloop_new())) {
284 fprintf(stderr
, "pa_mainloop_new() failed.\n");
288 mainloop_api
= pa_mainloop_get_api(m
);
290 r
= pa_signal_init(mainloop_api
);
292 pa_signal_register(SIGINT
, exit_signal_callback
, NULL
);
293 pa_signal_register(SIGUSR1
, sigusr1_signal_callback
, NULL
);
294 signal(SIGPIPE
, SIG_IGN
);
296 if (!(stdio_source
= mainloop_api
->source_io(mainloop_api
,
297 mode
== PLAYBACK
? STDIN_FILENO
: STDOUT_FILENO
,
298 mode
== PLAYBACK
? PA_MAINLOOP_API_IO_EVENT_INPUT
: PA_MAINLOOP_API_IO_EVENT_OUTPUT
,
299 mode
== PLAYBACK
? stdin_callback
: stdout_callback
, NULL
))) {
300 fprintf(stderr
, "source_io() failed.\n");
304 if (!(context
= pa_context_new(mainloop_api
, argv
[0]))) {
305 fprintf(stderr
, "pa_context_new() failed.\n");
309 if (pa_context_connect(context
, NULL
, context_complete_callback
, NULL
) < 0) {
310 fprintf(stderr
, "pa_context_connext() failed.\n");
314 pa_context_set_die_callback(context
, context_die_callback
, NULL
);
316 if (pa_mainloop_run(m
, &ret
) < 0) {
317 fprintf(stderr
, "pa_mainloop_run() failed.\n");
323 pa_stream_free(stream
);
325 pa_context_free(context
);