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, buffer+buffer_index, l, NULL);
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:
118 case PA_STREAM_READY:
119 fprintf(stderr, "Stream successfully created\n");
122 case PA_STREAM_TERMINATED:
126 case PA_STREAM_FAILED:
128 fprintf(stderr, "Stream errror: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
133 /* This is called whenever the context status changes */
134 static void context_state_callback(struct pa_context *c, void *userdata) {
135 static const struct pa_sample_spec ss = {
136 .format = PA_SAMPLE_S16LE,
143 switch (pa_context_get_state(c)) {
144 case PA_CONTEXT_CONNECTING:
145 case PA_CONTEXT_AUTHORIZING:
146 case PA_CONTEXT_SETTING_NAME:
149 case PA_CONTEXT_READY:
151 assert(c && !stream);
152 fprintf(stderr, "Connection established.\n");
154 stream = pa_stream_new(c, "pacat", &ss);
157 pa_stream_set_state_callback(stream, stream_state_callback, NULL);
158 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
159 pa_stream_set_read_callback(stream, stream_read_callback, NULL);
161 if (mode == PLAYBACK)
162 pa_stream_connect_playback(stream, NULL, NULL);
164 pa_stream_connect_record(stream, NULL, NULL);
168 case PA_CONTEXT_TERMINATED:
172 case PA_CONTEXT_FAILED:
174 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
179 /* Connection draining complete */
180 static void context_drain_complete(struct pa_context*c, void *userdata) {
181 pa_context_disconnect(c);
184 /* Stream draining complete */
185 static void stream_drain_complete(struct pa_stream*s, int success, void *userdata) {
186 struct pa_operation *o;
189 fprintf(stderr, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context)));
193 fprintf(stderr, "Playback stream drained.\n");
195 pa_stream_disconnect(stream);
196 pa_stream_unref(stream);
199 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
200 pa_context_disconnect(context);
202 pa_operation_unref(o);
203 fprintf(stderr, "Draining connection to server.\n");
207 /* New data on STDIN **/
208 static void stdin_callback(struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
211 assert(a == mainloop_api && e && stdio_event == e);
214 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
218 if (!stream || pa_stream_get_state(stream) != PA_STREAM_READY || !(l = w = pa_stream_writable_size(stream)))
223 if ((r = read(fd, buffer, l)) <= 0) {
225 fprintf(stderr, "Got EOF.\n");
226 pa_operation_unref(pa_stream_drain(stream, stream_drain_complete, NULL));
228 fprintf(stderr, "read() failed: %s\n", strerror(errno));
232 mainloop_api->io_free(stdio_event);
244 /* Some data may be written to STDOUT */
245 static void stdout_callback(struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
247 assert(a == mainloop_api && e && stdio_event == e);
250 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
254 assert(buffer_length);
256 if ((r = write(fd, buffer+buffer_index, buffer_length)) <= 0) {
257 fprintf(stderr, "write() failed: %s\n", strerror(errno));
260 mainloop_api->io_free(stdio_event);
268 if (!buffer_length) {
271 buffer_length = buffer_index = 0;
275 /* UNIX signal to quit recieved */
276 static void exit_signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
277 fprintf(stderr, "Got SIGINT, exiting.\n");
282 /* Show the current playback latency */
283 static void stream_get_latency_callback(struct pa_stream *s, uint32_t latency, void *userdata) {
286 if (latency == (uint32_t) -1) {
287 fprintf(stderr, "Failed to get latency: %s\n", strerror(errno));
292 fprintf(stderr, "Current latency is %u usecs.\n", latency);
295 /* Someone requested that the latency is shown */
296 static void sigusr1_signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
297 if (mode != PLAYBACK)
300 fprintf(stderr, "Got SIGUSR1, requesting latency.\n");
301 pa_operation_unref(pa_stream_get_latency(stream, stream_get_latency_callback, NULL));
304 int main(int argc, char *argv[]) {
305 struct pa_mainloop* m = NULL;
309 if (!(bn = strrchr(argv[0], '/')))
314 if (strstr(bn, "rec") || strstr(bn, "mon"))
316 else if (strstr(bn, "cat") || strstr(bn, "play"))
319 fprintf(stderr, "Opening a %s stream.\n", mode == RECORD ? "recording" : "playback");
321 /* Set up a new main loop */
322 if (!(m = pa_mainloop_new())) {
323 fprintf(stderr, "pa_mainloop_new() failed.\n");
327 mainloop_api = pa_mainloop_get_api(m);
329 r = pa_signal_init(mainloop_api);
331 pa_signal_new(SIGINT, exit_signal_callback, NULL);
332 pa_signal_new(SIGUSR1, sigusr1_signal_callback, NULL);
333 signal(SIGPIPE, SIG_IGN);
335 if (!(stdio_event = mainloop_api->io_new(mainloop_api,
336 mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO,
337 mode == PLAYBACK ? PA_IO_EVENT_INPUT : PA_IO_EVENT_OUTPUT,
338 mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) {
339 fprintf(stderr, "source_io() failed.\n");
343 /* Create a new connection context */
344 if (!(context = pa_context_new(mainloop_api, bn))) {
345 fprintf(stderr, "pa_context_new() failed.\n");
349 pa_context_set_state_callback(context, context_state_callback, NULL);
351 /* Connect the context */
352 pa_context_connect(context, NULL);
354 /* Run the main loop */
355 if (pa_mainloop_run(m, &ret) < 0) {
356 fprintf(stderr, "pa_mainloop_run() failed.\n");
362 pa_stream_unref(stream);
365 pa_context_unref(context);
368 assert(mainloop_api);
369 mainloop_api->io_free(stdio_event);