Documentation work
[platform/upstream/pulseaudio.git] / polyp / pacat.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <signal.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include <polyp/polyplib.h>
35 #include <polyp/polyplib-error.h>
36 #include <polyp/mainloop.h>
37 #include <polyp/mainloop-signal.h>
38
39 static enum { RECORD, PLAYBACK } mode = PLAYBACK;
40
41 static struct pa_context *context = NULL;
42 static struct pa_stream *stream = NULL;
43 static struct pa_mainloop_api *mainloop_api = NULL;
44
45 static void *buffer = NULL;
46 static size_t buffer_length = 0, buffer_index = 0;
47
48 static struct pa_io_event* stdio_event = NULL;
49
50 /* A shortcut for terminating the application */
51 static void quit(int ret) {
52     assert(mainloop_api);
53     mainloop_api->quit(mainloop_api, ret);
54 }
55
56 /* Write some data to the stream */
57 static void do_stream_write(size_t length) {
58     size_t l;
59     assert(length);
60
61     if (!buffer || !buffer_length)
62         return;
63     
64     l = length;
65     if (l > buffer_length)
66         l = buffer_length;
67     
68     pa_stream_write(stream, buffer+buffer_index, l, NULL);
69     buffer_length -= l;
70     buffer_index += l;
71     
72     if (!buffer_length) {
73         free(buffer);
74         buffer = NULL;
75         buffer_index = buffer_length = 0;
76     }
77 }
78
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) {
81     assert(s && length);
82
83     if (stdio_event)
84         mainloop_api->io_enable(stdio_event, PA_IO_EVENT_INPUT);
85
86     if (!buffer)
87         return;
88
89     do_stream_write(length);
90 }
91
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);
95
96     if (stdio_event)
97         mainloop_api->io_enable(stdio_event, PA_IO_EVENT_OUTPUT);
98
99     if (buffer) {
100         fprintf(stderr, "Buffer overrrun, dropping incoming data\n");
101         return;
102     }
103
104     buffer = malloc(buffer_length = length);
105     assert(buffer);
106     memcpy(buffer, data, length);
107     buffer_index = 0;
108 }
109
110 /* This routine is called whenever the stream state changes */
111 static void stream_state_callback(struct pa_stream *s, void *userdata) {
112     assert(s);
113
114     switch (pa_stream_get_state(s)) {
115         case PA_STREAM_CREATING:
116             break;
117
118         case PA_STREAM_READY:
119             fprintf(stderr, "Stream successfully created\n");
120             break;
121             
122         case PA_STREAM_TERMINATED:
123             quit(0);
124             break;
125             
126         case PA_STREAM_FAILED:
127         default:
128             fprintf(stderr, "Stream errror: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
129             quit(1);
130     }
131 }
132
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,
137         .rate = 44100,
138         .channels = 2
139     };
140
141     assert(c);
142
143     switch (pa_context_get_state(c)) {
144         case PA_CONTEXT_CONNECTING:
145         case PA_CONTEXT_AUTHORIZING:
146         case PA_CONTEXT_SETTING_NAME:
147             break;
148         
149         case PA_CONTEXT_READY:
150             
151             assert(c && !stream);
152             fprintf(stderr, "Connection established.\n");
153
154             stream = pa_stream_new(c, "pacat", &ss);
155             assert(stream);
156
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);
160
161             if (mode == PLAYBACK)
162                 pa_stream_connect_playback(stream, NULL, NULL);
163             else
164                 pa_stream_connect_record(stream, NULL, NULL);
165                 
166             break;
167             
168         case PA_CONTEXT_TERMINATED:
169             quit(0);
170             break;
171
172         case PA_CONTEXT_FAILED:
173         default:
174             fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
175             quit(1);
176     }
177 }
178
179 /* Connection draining complete */
180 static void context_drain_complete(struct pa_context*c, void *userdata) {
181     pa_context_disconnect(c);
182 }
183
184 /* Stream draining complete */
185 static void stream_drain_complete(struct pa_stream*s, int success, void *userdata) {
186     struct pa_operation *o;
187
188     if (!success) {
189         fprintf(stderr, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context)));
190         quit(1);
191     }
192         
193     fprintf(stderr, "Playback stream drained.\n");
194
195     pa_stream_disconnect(stream);
196     pa_stream_unref(stream);
197     stream = NULL;
198     
199     if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
200         pa_context_disconnect(context);
201     else {
202         pa_operation_unref(o);
203         fprintf(stderr, "Draining connection to server.\n");
204     }
205 }
206
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) {
209     size_t l, w = 0;
210     ssize_t r;
211     assert(a == mainloop_api && e && stdio_event == e);
212
213     if (buffer) {
214         mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
215         return;
216     }
217
218     if (!stream || pa_stream_get_state(stream) != PA_STREAM_READY || !(l = w = pa_stream_writable_size(stream)))
219         l = 4096;
220     
221     buffer = malloc(l);
222     assert(buffer);
223     if ((r = read(fd, buffer, l)) <= 0) {
224         if (r == 0) {
225             fprintf(stderr, "Got EOF.\n");
226             pa_operation_unref(pa_stream_drain(stream, stream_drain_complete, NULL));
227         } else {
228             fprintf(stderr, "read() failed: %s\n", strerror(errno));
229             quit(1);
230         }
231
232         mainloop_api->io_free(stdio_event);
233         stdio_event = NULL;
234         return;
235     }
236
237     buffer_length = r;
238     buffer_index = 0;
239
240     if (w)
241         do_stream_write(w);
242 }
243
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) {
246     ssize_t r;
247     assert(a == mainloop_api && e && stdio_event == e);
248
249     if (!buffer) {
250         mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
251         return;
252     }
253
254     assert(buffer_length);
255     
256     if ((r = write(fd, buffer+buffer_index, buffer_length)) <= 0) {
257         fprintf(stderr, "write() failed: %s\n", strerror(errno));
258         quit(1);
259
260         mainloop_api->io_free(stdio_event);
261         stdio_event = NULL;
262         return;
263     }
264
265     buffer_length -= r;
266     buffer_index += r;
267
268     if (!buffer_length) {
269         free(buffer);
270         buffer = NULL;
271         buffer_length = buffer_index = 0;
272     }
273 }
274
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");
278     quit(0);
279     
280 }
281
282 /* Show the current playback latency */
283 static void stream_get_latency_callback(struct pa_stream *s, uint32_t latency, void *userdata) {
284     assert(s);
285
286     if (latency == (uint32_t) -1) {
287         fprintf(stderr, "Failed to get latency: %s\n", strerror(errno));
288         quit(1);
289         return;
290     }
291
292     fprintf(stderr, "Current latency is %u usecs.\n", latency);
293 }
294
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)
298         return;
299     
300     fprintf(stderr, "Got SIGUSR1, requesting latency.\n");
301     pa_operation_unref(pa_stream_get_latency(stream, stream_get_latency_callback, NULL));
302 }
303
304 int main(int argc, char *argv[]) {
305     struct pa_mainloop* m = NULL;
306     int ret = 1, r;
307     char *bn;
308
309     if (!(bn = strrchr(argv[0], '/')))
310         bn = argv[0];
311     else
312         bn++;
313
314     if (strstr(bn, "rec") || strstr(bn, "mon"))
315         mode = RECORD;
316     else if (strstr(bn, "cat") || strstr(bn, "play"))
317         mode = PLAYBACK;
318
319     fprintf(stderr, "Opening a %s stream.\n", mode == RECORD ? "recording" : "playback");
320
321     /* Set up a new main loop */
322     if (!(m = pa_mainloop_new())) {
323         fprintf(stderr, "pa_mainloop_new() failed.\n");
324         goto quit;
325     }
326
327     mainloop_api = pa_mainloop_get_api(m);
328
329     r = pa_signal_init(mainloop_api);
330     assert(r == 0);
331     pa_signal_new(SIGINT, exit_signal_callback, NULL);
332     pa_signal_new(SIGUSR1, sigusr1_signal_callback, NULL);
333     signal(SIGPIPE, SIG_IGN);
334     
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");
340         goto quit;
341     }
342
343     /* Create a new connection context */
344     if (!(context = pa_context_new(mainloop_api, bn))) {
345         fprintf(stderr, "pa_context_new() failed.\n");
346         goto quit;
347     }
348
349     pa_context_set_state_callback(context, context_state_callback, NULL);
350
351     /* Connect the context */
352     pa_context_connect(context, NULL);
353
354     /* Run the main loop */
355     if (pa_mainloop_run(m, &ret) < 0) {
356         fprintf(stderr, "pa_mainloop_run() failed.\n");
357         goto quit;
358     }
359     
360 quit:
361     if (stream)
362         pa_stream_unref(stream);
363
364     if (context)
365         pa_context_unref(context);
366
367     if (stdio_event) {
368         assert(mainloop_api);
369         mainloop_api->io_free(stdio_event);
370     }
371     
372     if (m) {
373         pa_signal_done();
374         pa_mainloop_free(m);
375     }
376
377     if (buffer)
378         free(buffer);
379     
380     return ret;
381 }