]> code.delx.au - pulseaudio/blob - polyp/polyplib-simple.c
introduce pa_xmalloc() and friends
[pulseaudio] / 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 "xmalloc.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 = pa_xmalloc(sizeof(struct pa_simple));
102 p->context = NULL;
103 p->stream = NULL;
104 p->mainloop = pa_mainloop_new();
105 assert(p->mainloop);
106 p->dead = 0;
107 p->direction = dir;
108 p->read_data = NULL;
109 p->read_index = p->read_length = 0;
110
111 if (!(p->context = pa_context_new(pa_mainloop_get_api(p->mainloop), name)))
112 goto fail;
113
114 if (pa_context_connect(p->context, server, NULL, NULL) < 0) {
115 error = pa_context_errno(p->context);
116 goto fail;
117 }
118
119 /* Wait until the context is ready */
120 while (!pa_context_is_ready(p->context)) {
121 if (iterate(p, 1, &error) < 0)
122 goto fail;
123 }
124
125 if (!(p->stream = pa_stream_new(p->context, dir, dev, stream_name, ss, attr, NULL, NULL)))
126 goto fail;
127
128 /* Wait until the stream is ready */
129 while (!pa_stream_is_ready(p->stream)) {
130 if (iterate(p, 1, &error) < 0)
131 goto fail;
132 }
133
134 pa_stream_set_read_callback(p->stream, read_callback, p);
135
136 return p;
137
138 fail:
139 if (perror)
140 *perror = error;
141 pa_simple_free(p);
142 return NULL;
143 }
144
145 void pa_simple_free(struct pa_simple *s) {
146 assert(s);
147
148 pa_xfree(s->read_data);
149
150 if (s->stream)
151 pa_stream_free(s->stream);
152
153 if (s->context)
154 pa_context_free(s->context);
155
156 if (s->mainloop)
157 pa_mainloop_free(s->mainloop);
158
159 pa_xfree(s);
160 }
161
162 int pa_simple_write(struct pa_simple *p, const void*data, size_t length, int *perror) {
163 assert(p && data && p->direction == PA_STREAM_PLAYBACK);
164
165 while (length > 0) {
166 size_t l;
167
168 while (!(l = pa_stream_writable_size(p->stream)))
169 if (iterate(p, 1, perror) < 0)
170 return -1;
171
172 if (l > length)
173 l = length;
174
175 pa_stream_write(p->stream, data, l);
176 data += l;
177 length -= l;
178 }
179
180 /* Make sure that no data is pending for write */
181 if (iterate(p, 0, perror) < 0)
182 return -1;
183
184 return 0;
185 }
186
187 static void read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) {
188 struct pa_simple *p = userdata;
189 assert(s && data && length && p);
190
191 if (p->read_data) {
192 fprintf(stderr, __FILE__": Buffer overflow, dropping incoming memory blocks.\n");
193 pa_xfree(p->read_data);
194 }
195
196 p->read_data = pa_xmalloc(p->read_length = length);
197 memcpy(p->read_data, data, length);
198 p->read_index = 0;
199 }
200
201 int pa_simple_read(struct pa_simple *p, void*data, size_t length, int *perror) {
202 assert(p && data && p->direction == PA_STREAM_RECORD);
203
204 while (length > 0) {
205 if (p->read_data) {
206 size_t l = length;
207
208 if (p->read_length <= l)
209 l = p->read_length;
210
211 memcpy(data, p->read_data+p->read_index, l);
212
213 data += l;
214 length -= l;
215
216 p->read_index += l;
217 p->read_length -= l;
218
219 if (!p->read_length) {
220 pa_xfree(p->read_data);
221 p->read_data = NULL;
222 p->read_index = 0;
223 }
224
225 if (!length)
226 return 0;
227
228 assert(!p->read_data);
229 }
230
231 if (iterate(p, 1, perror) < 0)
232 return -1;
233 }
234
235 return 0;
236 }
237
238 static void drain_complete(struct pa_stream *s, void *userdata) {
239 struct pa_simple *p = userdata;
240 assert(s && p);
241 p->drained = 1;
242 }
243
244 int pa_simple_drain(struct pa_simple *p, int *perror) {
245 assert(p && p->direction == PA_STREAM_PLAYBACK);
246 p->drained = 0;
247 pa_stream_drain(p->stream, drain_complete, p);
248
249 while (!p->drained) {
250 if (iterate(p, 1, perror) < 0) {
251 pa_stream_drain(p->stream, NULL, NULL);
252 return -1;
253 }
254 }
255
256 return 0;
257 }