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 /* A shortcut for terminating the application */
51 static void quit(int ret) {
53 mainloop_api->quit(mainloop_api, ret);
56 /* Write some data to the stream */
57 static void do_stream_write(size_t length) {
61 if (!buffer || !buffer_length)
65 if (l > buffer_length)
68 pa_stream_write(stream, (uint8_t*) buffer + buffer_index, l, NULL, 0);
75 buffer_index = buffer_length = 0;
79 /* This is called whenever new data may be written to the stream */
80 static void stream_write_callback(struct pa_stream *s, size_t length, void *userdata) {
84 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_INPUT);
89 do_stream_write(length);
92 /* This is called whenever new data may is available */
93 static void stream_read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) {
94 assert(s && data && length);
97 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_OUTPUT);
100 fprintf(stderr, "Buffer overrrun, dropping incoming data\n");
104 buffer = malloc(buffer_length = length);
106 memcpy(buffer, data, length);
110 /* This routine is called whenever the stream state changes */
111 static void stream_state_callback(struct pa_stream *s, void *userdata) {
114 switch (pa_stream_get_state(s)) {
115 case PA_STREAM_CREATING:
116 case PA_STREAM_TERMINATED:
119 case PA_STREAM_READY:
120 fprintf(stderr, "Stream successfully created\n");
123 case PA_STREAM_FAILED:
125 fprintf(stderr, "Stream errror: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
130 /* This is called whenever the context status changes */
131 static void context_state_callback(struct pa_context *c, void *userdata) {
132 static const struct pa_sample_spec ss = {
133 .format = PA_SAMPLE_S16LE,
140 switch (pa_context_get_state(c)) {
141 case PA_CONTEXT_CONNECTING:
142 case PA_CONTEXT_AUTHORIZING:
143 case PA_CONTEXT_SETTING_NAME:
146 case PA_CONTEXT_READY:
148 assert(c && !stream);
149 fprintf(stderr, "Connection established.\n");
151 stream = pa_stream_new(c, "pacat", &ss);
154 pa_stream_set_state_callback(stream, stream_state_callback, NULL);
155 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
156 pa_stream_set_read_callback(stream, stream_read_callback, NULL);
158 if (mode == PLAYBACK)
159 pa_stream_connect_playback(stream, NULL, NULL);
161 pa_stream_connect_record(stream, NULL, NULL);
165 case PA_CONTEXT_TERMINATED:
169 case PA_CONTEXT_FAILED:
171 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
176 /* Connection draining complete */
177 static void context_drain_complete(struct pa_context*c, void *userdata) {
178 pa_context_disconnect(c);
181 /* Stream draining complete */
182 static void stream_drain_complete(struct pa_stream*s, int success, void *userdata) {
183 struct pa_operation *o;
186 fprintf(stderr, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context)));
190 fprintf(stderr, "Playback stream drained.\n");
192 pa_stream_disconnect(stream);
193 pa_stream_unref(stream);
196 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
197 pa_context_disconnect(context);
199 pa_operation_unref(o);
200 fprintf(stderr, "Draining connection to server.\n");
204 /* New data on STDIN **/
205 static void stdin_callback(struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
208 assert(a == mainloop_api && e && stdio_event == e);
211 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
215 if (!stream || pa_stream_get_state(stream) != PA_STREAM_READY || !(l = w = pa_stream_writable_size(stream)))
220 if ((r = read(fd, buffer, l)) <= 0) {
222 fprintf(stderr, "Got EOF.\n");
223 pa_operation_unref(pa_stream_drain(stream, stream_drain_complete, NULL));
225 fprintf(stderr, "read() failed: %s\n", strerror(errno));
229 mainloop_api->io_free(stdio_event);
241 /* Some data may be written to STDOUT */
242 static void stdout_callback(struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
244 assert(a == mainloop_api && e && stdio_event == e);
247 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
251 assert(buffer_length);
253 if ((r = write(fd, (uint8_t*) buffer+buffer_index, buffer_length)) <= 0) {
254 fprintf(stderr, "write() failed: %s\n", strerror(errno));
257 mainloop_api->io_free(stdio_event);
265 if (!buffer_length) {
268 buffer_length = buffer_index = 0;
272 /* UNIX signal to quit recieved */
273 static void exit_signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
274 fprintf(stderr, "Got SIGINT, exiting.\n");
279 /* Show the current playback latency */
280 static void stream_get_latency_callback(struct pa_stream *s, const struct pa_latency_info *i, void *userdata) {
284 fprintf(stderr, "Failed to get latency: %s\n", strerror(errno));
289 fprintf(stderr, "Current latency is %u usecs.\n", i->buffer_usec+i->sink_usec);
292 /* Someone requested that the latency is shown */
293 static void sigusr1_signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
294 if (mode != PLAYBACK)
297 fprintf(stderr, "Got SIGUSR1, requesting latency.\n");
298 pa_operation_unref(pa_stream_get_latency(stream, stream_get_latency_callback, NULL));
301 int main(int argc, char *argv[]) {
302 struct pa_mainloop* m = NULL;
306 if (!(bn = strrchr(argv[0], '/')))
311 if (strstr(bn, "rec") || strstr(bn, "mon"))
313 else if (strstr(bn, "cat") || strstr(bn, "play"))
316 fprintf(stderr, "Opening a %s stream.\n", mode == RECORD ? "recording" : "playback");
318 /* Set up a new main loop */
319 if (!(m = pa_mainloop_new())) {
320 fprintf(stderr, "pa_mainloop_new() failed.\n");
324 mainloop_api = pa_mainloop_get_api(m);
326 r = pa_signal_init(mainloop_api);
328 pa_signal_new(SIGINT, exit_signal_callback, NULL);
329 pa_signal_new(SIGUSR1, sigusr1_signal_callback, NULL);
330 signal(SIGPIPE, SIG_IGN);
332 if (!(stdio_event = mainloop_api->io_new(mainloop_api,
333 mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO,
334 mode == PLAYBACK ? PA_IO_EVENT_INPUT : PA_IO_EVENT_OUTPUT,
335 mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) {
336 fprintf(stderr, "source_io() failed.\n");
340 /* Create a new connection context */
341 if (!(context = pa_context_new(mainloop_api, bn))) {
342 fprintf(stderr, "pa_context_new() failed.\n");
346 pa_context_set_state_callback(context, context_state_callback, NULL);
348 /* Connect the context */
349 pa_context_connect(context, NULL);
351 /* Run the main loop */
352 if (pa_mainloop_run(m, &ret) < 0) {
353 fprintf(stderr, "pa_mainloop_run() failed.\n");
359 pa_stream_unref(stream);
362 pa_context_unref(context);
365 assert(mainloop_api);
366 mainloop_api->io_free(stdio_event);