b251cc35ff92adc4d17089fccbaabe713ac67640
[profile/ivi/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, (uint8_t*) buffer + buffer_index, l, NULL, 0);
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         case PA_STREAM_TERMINATED:
117             break;
118
119         case PA_STREAM_READY:
120             fprintf(stderr, "Stream successfully created\n");
121             break;
122             
123         case PA_STREAM_FAILED:
124         default:
125             fprintf(stderr, "Stream errror: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
126             quit(1);
127     }
128 }
129
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,
134         .rate = 44100,
135         .channels = 2
136     };
137
138     assert(c);
139
140     switch (pa_context_get_state(c)) {
141         case PA_CONTEXT_CONNECTING:
142         case PA_CONTEXT_AUTHORIZING:
143         case PA_CONTEXT_SETTING_NAME:
144             break;
145         
146         case PA_CONTEXT_READY:
147             
148             assert(c && !stream);
149             fprintf(stderr, "Connection established.\n");
150
151             stream = pa_stream_new(c, "pacat", &ss);
152             assert(stream);
153
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);
157
158             if (mode == PLAYBACK)
159                 pa_stream_connect_playback(stream, NULL, NULL);
160             else
161                 pa_stream_connect_record(stream, NULL, NULL);
162                 
163             break;
164             
165         case PA_CONTEXT_TERMINATED:
166             quit(0);
167             break;
168
169         case PA_CONTEXT_FAILED:
170         default:
171             fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
172             quit(1);
173     }
174 }
175
176 /* Connection draining complete */
177 static void context_drain_complete(struct pa_context*c, void *userdata) {
178     pa_context_disconnect(c);
179 }
180
181 /* Stream draining complete */
182 static void stream_drain_complete(struct pa_stream*s, int success, void *userdata) {
183     struct pa_operation *o;
184
185     if (!success) {
186         fprintf(stderr, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context)));
187         quit(1);
188     }
189         
190     fprintf(stderr, "Playback stream drained.\n");
191
192     pa_stream_disconnect(stream);
193     pa_stream_unref(stream);
194     stream = NULL;
195     
196     if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
197         pa_context_disconnect(context);
198     else {
199         pa_operation_unref(o);
200         fprintf(stderr, "Draining connection to server.\n");
201     }
202 }
203
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) {
206     size_t l, w = 0;
207     ssize_t r;
208     assert(a == mainloop_api && e && stdio_event == e);
209
210     if (buffer) {
211         mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
212         return;
213     }
214
215     if (!stream || pa_stream_get_state(stream) != PA_STREAM_READY || !(l = w = pa_stream_writable_size(stream)))
216         l = 4096;
217     
218     buffer = malloc(l);
219     assert(buffer);
220     if ((r = read(fd, buffer, l)) <= 0) {
221         if (r == 0) {
222             fprintf(stderr, "Got EOF.\n");
223             pa_operation_unref(pa_stream_drain(stream, stream_drain_complete, NULL));
224         } else {
225             fprintf(stderr, "read() failed: %s\n", strerror(errno));
226             quit(1);
227         }
228
229         mainloop_api->io_free(stdio_event);
230         stdio_event = NULL;
231         return;
232     }
233
234     buffer_length = r;
235     buffer_index = 0;
236
237     if (w)
238         do_stream_write(w);
239 }
240
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) {
243     ssize_t r;
244     assert(a == mainloop_api && e && stdio_event == e);
245
246     if (!buffer) {
247         mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
248         return;
249     }
250
251     assert(buffer_length);
252     
253     if ((r = write(fd, (uint8_t*) buffer+buffer_index, buffer_length)) <= 0) {
254         fprintf(stderr, "write() failed: %s\n", strerror(errno));
255         quit(1);
256
257         mainloop_api->io_free(stdio_event);
258         stdio_event = NULL;
259         return;
260     }
261
262     buffer_length -= r;
263     buffer_index += r;
264
265     if (!buffer_length) {
266         free(buffer);
267         buffer = NULL;
268         buffer_length = buffer_index = 0;
269     }
270 }
271
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");
275     quit(0);
276     
277 }
278
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) {
281     assert(s);
282
283     if (!i) {
284         fprintf(stderr, "Failed to get latency: %s\n", strerror(errno));
285         quit(1);
286         return;
287     }
288
289     fprintf(stderr, "Current latency is %u usecs.\n", i->buffer_usec+i->sink_usec);
290 }
291
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)
295         return;
296     
297     fprintf(stderr, "Got SIGUSR1, requesting latency.\n");
298     pa_operation_unref(pa_stream_get_latency(stream, stream_get_latency_callback, NULL));
299 }
300
301 int main(int argc, char *argv[]) {
302     struct pa_mainloop* m = NULL;
303     int ret = 1, r;
304     char *bn;
305
306     if (!(bn = strrchr(argv[0], '/')))
307         bn = argv[0];
308     else
309         bn++;
310
311     if (strstr(bn, "rec") || strstr(bn, "mon"))
312         mode = RECORD;
313     else if (strstr(bn, "cat") || strstr(bn, "play"))
314         mode = PLAYBACK;
315
316     fprintf(stderr, "Opening a %s stream.\n", mode == RECORD ? "recording" : "playback");
317
318     /* Set up a new main loop */
319     if (!(m = pa_mainloop_new())) {
320         fprintf(stderr, "pa_mainloop_new() failed.\n");
321         goto quit;
322     }
323
324     mainloop_api = pa_mainloop_get_api(m);
325
326     r = pa_signal_init(mainloop_api);
327     assert(r == 0);
328     pa_signal_new(SIGINT, exit_signal_callback, NULL);
329     pa_signal_new(SIGUSR1, sigusr1_signal_callback, NULL);
330     signal(SIGPIPE, SIG_IGN);
331     
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");
337         goto quit;
338     }
339
340     /* Create a new connection context */
341     if (!(context = pa_context_new(mainloop_api, bn))) {
342         fprintf(stderr, "pa_context_new() failed.\n");
343         goto quit;
344     }
345
346     pa_context_set_state_callback(context, context_state_callback, NULL);
347
348     /* Connect the context */
349     pa_context_connect(context, NULL);
350
351     /* Run the main loop */
352     if (pa_mainloop_run(m, &ret) < 0) {
353         fprintf(stderr, "pa_mainloop_run() failed.\n");
354         goto quit;
355     }
356     
357 quit:
358     if (stream)
359         pa_stream_unref(stream);
360
361     if (context)
362         pa_context_unref(context);
363
364     if (stdio_event) {
365         assert(mainloop_api);
366         mainloop_api->io_free(stdio_event);
367     }
368     
369     if (m) {
370         pa_signal_done();
371         pa_mainloop_free(m);
372     }
373
374     if (buffer)
375         free(buffer);
376     
377     return ret;
378 }