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 struct pa_io_event
* stdio_event
= NULL
;
50 static void quit(int ret
) {
52 mainloop_api
->quit(mainloop_api
, ret
);
55 static void do_stream_write(size_t length
) {
59 if (!buffer
|| !buffer_length
)
63 if (l
> buffer_length
)
66 pa_stream_write(stream
, buffer
+buffer_index
, l
, NULL
);
73 buffer_index
= buffer_length
= 0;
77 static void stream_write_callback(struct pa_stream
*s
, size_t length
, void *userdata
) {
81 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_INPUT
);
86 do_stream_write(length
);
89 static void stream_read_callback(struct pa_stream
*s
, const void*data
, size_t length
, void *userdata
) {
90 assert(s
&& data
&& length
);
93 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_OUTPUT
);
96 fprintf(stderr
, "Buffer overrrun, dropping incoming data\n");
100 buffer
= malloc(buffer_length
= length
);
102 memcpy(buffer
, data
, length
);
106 static void stream_state_callback(struct pa_stream
*s
, void *userdata
) {
109 switch (pa_stream_get_state(s
)) {
110 case PA_STREAM_CREATING
:
113 case PA_STREAM_READY
:
114 fprintf(stderr
, "Stream successfully created\n");
117 case PA_STREAM_TERMINATED
:
121 case PA_STREAM_FAILED
:
123 fprintf(stderr
, "Stream errror: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
128 static void context_state_callback(struct pa_context
*c
, void *userdata
) {
129 static const struct pa_sample_spec ss
= {
130 .format
= PA_SAMPLE_S16LE
,
137 switch (pa_context_get_state(c
)) {
138 case PA_CONTEXT_CONNECTING
:
139 case PA_CONTEXT_AUTHORIZING
:
140 case PA_CONTEXT_SETTING_NAME
:
143 case PA_CONTEXT_READY
:
145 assert(c
&& !stream
);
146 fprintf(stderr
, "Connection established.\n");
148 stream
= pa_stream_new(c
, "pacat", &ss
);
151 pa_stream_set_state_callback(stream
, stream_state_callback
, NULL
);
152 pa_stream_set_write_callback(stream
, stream_write_callback
, NULL
);
153 pa_stream_set_read_callback(stream
, stream_read_callback
, NULL
);
155 if (mode
== PLAYBACK
)
156 pa_stream_connect_playback(stream
, NULL
, NULL
);
158 pa_stream_connect_record(stream
, NULL
, NULL
);
162 case PA_CONTEXT_TERMINATED
:
166 case PA_CONTEXT_FAILED
:
168 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
173 static void context_drain_complete(struct pa_context
*c
, void *userdata
) {
174 pa_context_disconnect(c
);
177 static void stream_drain_complete(struct pa_stream
*s
, int success
, void *userdata
) {
178 struct pa_operation
*o
;
181 fprintf(stderr
, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context
)));
185 fprintf(stderr
, "Playback stream drained.\n");
187 pa_stream_disconnect(stream
);
188 pa_stream_unref(stream
);
191 if (!(o
= pa_context_drain(context
, context_drain_complete
, NULL
)))
192 pa_context_disconnect(context
);
194 pa_operation_unref(o
);
195 fprintf(stderr
, "Draining connection to server.\n");
199 static void stdin_callback(struct pa_mainloop_api
*a
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
202 assert(a
== mainloop_api
&& e
&& stdio_event
== e
);
205 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_NULL
);
209 if (!stream
|| pa_stream_get_state(stream
) != PA_STREAM_READY
|| !(l
= w
= pa_stream_writable_size(stream
)))
214 if ((r
= read(fd
, buffer
, l
)) <= 0) {
216 fprintf(stderr
, "Got EOF.\n");
217 pa_operation_unref(pa_stream_drain(stream
, stream_drain_complete
, NULL
));
219 fprintf(stderr
, "read() failed: %s\n", strerror(errno
));
223 mainloop_api
->io_free(stdio_event
);
235 static void stdout_callback(struct pa_mainloop_api
*a
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
237 assert(a
== mainloop_api
&& e
&& stdio_event
== e
);
240 mainloop_api
->io_enable(stdio_event
, PA_IO_EVENT_NULL
);
244 assert(buffer_length
);
246 if ((r
= write(fd
, buffer
+buffer_index
, buffer_length
)) <= 0) {
247 fprintf(stderr
, "write() failed: %s\n", strerror(errno
));
250 mainloop_api
->io_free(stdio_event
);
258 if (!buffer_length
) {
261 buffer_length
= buffer_index
= 0;
265 static void exit_signal_callback(struct pa_mainloop_api
*m
, struct pa_signal_event
*e
, int sig
, void *userdata
) {
266 fprintf(stderr
, "Got SIGINT, exiting.\n");
271 static void stream_get_latency_callback(struct pa_stream
*s
, uint32_t latency
, void *userdata
) {
274 if (latency
== (uint32_t) -1) {
275 fprintf(stderr
, "Failed to get latency: %s\n", strerror(errno
));
280 fprintf(stderr
, "Current latency is %u usecs.\n", latency
);
283 static void sigusr1_signal_callback(struct pa_mainloop_api
*m
, struct pa_signal_event
*e
, int sig
, void *userdata
) {
284 if (mode
!= PLAYBACK
)
287 fprintf(stderr
, "Got SIGUSR1, requesting latency.\n");
288 pa_operation_unref(pa_stream_get_latency(stream
, stream_get_latency_callback
, NULL
));
291 int main(int argc
, char *argv
[]) {
292 struct pa_mainloop
* m
= NULL
;
296 if (!(bn
= strrchr(argv
[0], '/')))
299 if (strstr(bn
, "rec") || strstr(bn
, "mon"))
301 else if (strstr(bn
, "cat") || strstr(bn
, "play"))
304 fprintf(stderr
, "Opening a %s stream.\n", mode
== RECORD
? "recording" : "playback");
306 if (!(m
= pa_mainloop_new())) {
307 fprintf(stderr
, "pa_mainloop_new() failed.\n");
311 mainloop_api
= pa_mainloop_get_api(m
);
313 r
= pa_signal_init(mainloop_api
);
315 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
316 pa_signal_new(SIGUSR1
, sigusr1_signal_callback
, NULL
);
317 signal(SIGPIPE
, SIG_IGN
);
319 if (!(stdio_event
= mainloop_api
->io_new(mainloop_api
,
320 mode
== PLAYBACK
? STDIN_FILENO
: STDOUT_FILENO
,
321 mode
== PLAYBACK
? PA_IO_EVENT_INPUT
: PA_IO_EVENT_OUTPUT
,
322 mode
== PLAYBACK
? stdin_callback
: stdout_callback
, NULL
))) {
323 fprintf(stderr
, "source_io() failed.\n");
327 if (!(context
= pa_context_new(mainloop_api
, argv
[0]))) {
328 fprintf(stderr
, "pa_context_new() failed.\n");
332 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
334 pa_context_connect(context
, NULL
);
336 if (pa_mainloop_run(m
, &ret
) < 0) {
337 fprintf(stderr
, "pa_mainloop_run() failed.\n");
343 pa_stream_unref(stream
);
346 pa_context_unref(context
);
349 assert(mainloop_api
);
350 mainloop_api
->io_free(stdio_event
);