024cb18ab4363610175213196704765be9036f14
[profile/ivi/pulseaudio-panda.git] / polyp / polyplib-simple.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 <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <stdlib.h>
30
31 #include "polyplib-simple.h"
32 #include "polyplib.h"
33 #include "mainloop.h"
34 #include "native-common.h"
35 /*#include "polyp-error.h"*/
36
37 struct pa_simple {
38     struct pa_mainloop *mainloop;
39     struct pa_context *context;
40     struct pa_stream *stream;
41     enum pa_stream_direction direction;
42
43     int dead, drained;
44
45     void *read_data;
46     size_t read_index, read_length;
47 };
48
49 static void read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata);
50
51 static int check_error(struct pa_simple *p, int *perror) {
52     assert(p);
53     
54     if (pa_context_is_dead(p->context) || (p->stream && pa_stream_is_dead(p->stream))) {
55         if (perror)
56             *perror = pa_context_errno(p->context);
57         return -1;
58     }
59
60     return 0;
61 }
62
63 static int iterate(struct pa_simple *p, int block, int *perror) {
64     assert(p && p->context && p->mainloop);
65
66     if (check_error(p, perror) < 0)
67         return -1;
68     
69     if (!block && !pa_context_is_pending(p->context))
70         return 0;
71
72     do {
73         if (pa_mainloop_iterate(p->mainloop, 1, NULL) < 0) {
74             if (perror)
75                 *perror = PA_ERROR_INTERNAL;
76             return -1;
77         }
78
79         if (check_error(p, perror) < 0)
80             return -1;
81         
82     } while (pa_context_is_pending(p->context));
83
84     return 0;
85 }
86
87 struct pa_simple* pa_simple_new(
88     const char *server,
89     const char *name,
90     enum pa_stream_direction dir,
91     const char *dev,
92     const char *stream_name,
93     const struct pa_sample_spec *ss,
94     const struct pa_buffer_attr *attr,
95     int *perror) {
96     
97     struct pa_simple *p;
98     int error = PA_ERROR_INTERNAL;
99     assert(ss);
100
101     p = malloc(sizeof(struct pa_simple));
102     assert(p);
103     p->context = NULL;
104     p->stream = NULL;
105     p->mainloop = pa_mainloop_new();
106     assert(p->mainloop);
107     p->dead = 0;
108     p->direction = dir;
109     p->read_data = NULL;
110     p->read_index = p->read_length = 0;
111
112     if (!(p->context = pa_context_new(pa_mainloop_get_api(p->mainloop), name)))
113         goto fail;
114
115     if (pa_context_connect(p->context, server, NULL, NULL) < 0) {
116         error = pa_context_errno(p->context);
117         goto fail;
118     }
119
120     /* Wait until the context is ready */
121     while (!pa_context_is_ready(p->context)) {
122         if (iterate(p, 1, &error) < 0)
123             goto fail;
124     }
125
126     if (!(p->stream = pa_stream_new(p->context, dir, dev, stream_name, ss, attr, NULL, NULL)))
127         goto fail;
128
129     /* Wait until the stream is ready */
130     while (!pa_stream_is_ready(p->stream)) {
131         if (iterate(p, 1, &error) < 0)
132             goto fail;
133     }
134
135     pa_stream_set_read_callback(p->stream, read_callback, p);
136     
137     return p;
138     
139 fail:
140     if (perror)
141         *perror = error;
142     pa_simple_free(p);
143     return NULL;
144 }
145
146 void pa_simple_free(struct pa_simple *s) {
147     assert(s);
148
149     free(s->read_data);
150
151     if (s->stream)
152         pa_stream_free(s->stream);
153     
154     if (s->context)
155         pa_context_free(s->context);
156
157     if (s->mainloop)
158         pa_mainloop_free(s->mainloop);
159
160     free(s);
161 }
162
163 int pa_simple_write(struct pa_simple *p, const void*data, size_t length, int *perror) {
164     assert(p && data && p->direction == PA_STREAM_PLAYBACK);
165
166     while (length > 0) {
167         size_t l;
168         
169         while (!(l = pa_stream_writable_size(p->stream)))
170             if (iterate(p, 1, perror) < 0)
171                 return -1;
172
173         if (l > length)
174             l = length;
175
176         pa_stream_write(p->stream, data, l);
177         data += l;
178         length -= l;
179     }
180
181     /* Make sure that no data is pending for write */
182     if (iterate(p, 0, perror) < 0)
183         return -1;
184
185     return 0;
186 }
187
188 static void read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) {
189     struct pa_simple *p = userdata;
190     assert(s && data && length && p);
191
192     if (p->read_data) {
193         fprintf(stderr, __FILE__": Buffer overflow, dropping incoming memory blocks.\n");
194         free(p->read_data);
195     }
196
197     p->read_data = malloc(p->read_length = length);
198     assert(p->read_data);
199     memcpy(p->read_data, data, length);
200     p->read_index = 0;
201 }
202
203 int pa_simple_read(struct pa_simple *p, void*data, size_t length, int *perror) {
204     assert(p && data && p->direction == PA_STREAM_RECORD);
205
206     while (length > 0) {
207         if (p->read_data) {
208             size_t l = length;
209
210             if (p->read_length <= l)
211                 l = p->read_length;
212
213             memcpy(data, p->read_data+p->read_index, l);
214
215             data += l;
216             length -= l;
217             
218             p->read_index += l;
219             p->read_length -= l;
220
221             if (!p->read_length) {
222                 free(p->read_data);
223                 p->read_data = NULL;
224                 p->read_index = 0;
225             }
226             
227             if (!length)
228                 return 0;
229
230             assert(!p->read_data);
231         }
232
233         if (iterate(p, 1, perror) < 0)
234             return -1;
235     }
236
237     return 0;
238 }
239
240 static void drain_complete(struct pa_stream *s, void *userdata) {
241     struct pa_simple *p = userdata;
242     assert(s && p);
243     p->drained = 1;
244 }
245
246 int pa_simple_drain(struct pa_simple *p, int *perror) {
247     assert(p && p->direction == PA_STREAM_PLAYBACK);
248     p->drained = 0;
249     pa_stream_drain(p->stream, drain_complete, p);
250
251     while (!p->drained) {
252         if (iterate(p, 1, perror) < 0) {
253             pa_stream_drain(p->stream, NULL, NULL);
254             return -1;
255         }
256     }
257
258     return 0;
259 }