]> code.delx.au - pulseaudio/blob - src/polyp/simple.c
* rename "latency correction" to "write index correction"
[pulseaudio] / src / polyp / 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 Lesser 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 Lesser 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 <polyp/polypaudio.h>
32 #include <polyp/mainloop.h>
33
34 #include <polypcore/native-common.h>
35 #include <polypcore/xmalloc.h>
36 #include <polypcore/log.h>
37
38 #include "simple.h"
39
40 #define CHECK_VALIDITY_RETURN_ANY(rerror, expression, error, ret) do { \
41 if (!(expression)) { \
42 if (rerror) \
43 *(rerror) = error; \
44 return ret; \
45 } \
46 } while(0);
47
48 struct pa_simple {
49 pa_mainloop *mainloop;
50 pa_context *context;
51 pa_stream *stream;
52 pa_stream_direction_t direction;
53
54 int dead;
55
56 const void *read_data;
57 size_t read_index, read_length;
58 pa_usec_t latency;
59 };
60
61 static int check_error(pa_simple *p, int *rerror) {
62 pa_context_state_t cst;
63 pa_stream_state_t sst;
64 assert(p);
65
66 if ((cst = pa_context_get_state(p->context)) == PA_CONTEXT_FAILED)
67 goto fail;
68
69 assert(cst != PA_CONTEXT_TERMINATED);
70
71 if (p->stream) {
72 if ((sst = pa_stream_get_state(p->stream)) == PA_STREAM_FAILED)
73 goto fail;
74
75 assert(sst != PA_STREAM_TERMINATED);
76 }
77
78 return 0;
79
80 fail:
81 if (rerror)
82 *rerror = pa_context_errno(p->context);
83
84 p->dead = 1;
85
86 return -1;
87 }
88
89 static int iterate(pa_simple *p, int block, int *rerror) {
90 assert(p && p->context && p->mainloop);
91
92 if (check_error(p, rerror) < 0)
93 return -1;
94
95 if (!block && !pa_context_is_pending(p->context))
96 return 0;
97
98 do {
99 if (pa_mainloop_iterate(p->mainloop, 1, NULL) < 0) {
100 if (rerror)
101 *rerror = PA_ERR_INTERNAL;
102 return -1;
103 }
104
105 if (check_error(p, rerror) < 0)
106 return -1;
107
108 } while (pa_context_is_pending(p->context));
109
110
111 while (pa_mainloop_deferred_pending(p->mainloop)) {
112
113 if (pa_mainloop_iterate(p->mainloop, 0, NULL) < 0) {
114 if (rerror)
115 *rerror = PA_ERR_INTERNAL;
116 return -1;
117 }
118
119 if (check_error(p, rerror) < 0)
120 return -1;
121 }
122
123 return 0;
124 }
125
126 pa_simple* pa_simple_new(
127 const char *server,
128 const char *name,
129 pa_stream_direction_t dir,
130 const char *dev,
131 const char *stream_name,
132 const pa_sample_spec *ss,
133 const pa_buffer_attr *attr,
134 int *rerror) {
135
136 pa_simple *p;
137 int error = PA_ERR_INTERNAL, r;
138
139 CHECK_VALIDITY_RETURN_ANY(rerror, !server || *server, PA_ERR_INVALID, NULL);
140 CHECK_VALIDITY_RETURN_ANY(rerror, dir == PA_STREAM_PLAYBACK || dir == PA_STREAM_RECORD, PA_ERR_INVALID, NULL);
141 CHECK_VALIDITY_RETURN_ANY(rerror, !dev || *dev, PA_ERR_INVALID, NULL);
142 CHECK_VALIDITY_RETURN_ANY(rerror, ss && pa_sample_spec_valid(ss), PA_ERR_INVALID, NULL);
143
144 p = pa_xnew(pa_simple, 1);
145 p->context = NULL;
146 p->stream = NULL;
147 p->mainloop = pa_mainloop_new();
148 assert(p->mainloop);
149 p->dead = 0;
150 p->direction = dir;
151 p->read_data = NULL;
152 p->read_index = p->read_length = 0;
153 p->latency = 0;
154
155 if (!(p->context = pa_context_new(pa_mainloop_get_api(p->mainloop), name)))
156 goto fail;
157
158 if (pa_context_connect(p->context, server, 0, NULL) < 0) {
159 error = pa_context_errno(p->context);
160 goto fail;
161 }
162
163 /* Wait until the context is ready */
164 while (pa_context_get_state(p->context) != PA_CONTEXT_READY) {
165 if (iterate(p, 1, &error) < 0)
166 goto fail;
167 }
168
169 if (!(p->stream = pa_stream_new(p->context, stream_name, ss, NULL))) {
170 error = pa_context_errno(p->context);
171 goto fail;
172 }
173
174 if (dir == PA_STREAM_PLAYBACK)
175 r = pa_stream_connect_playback(p->stream, dev, attr, 0, NULL, NULL);
176 else
177 r = pa_stream_connect_record(p->stream, dev, attr, 0);
178
179 if (r < 0) {
180 error = pa_context_errno(p->context);
181 goto fail;
182 }
183
184 /* Wait until the stream is ready */
185 while (pa_stream_get_state(p->stream) != PA_STREAM_READY) {
186 if (iterate(p, 1, &error) < 0)
187 goto fail;
188 }
189
190 return p;
191
192 fail:
193 if (rerror)
194 *rerror = error;
195 pa_simple_free(p);
196 return NULL;
197 }
198
199 void pa_simple_free(pa_simple *s) {
200 assert(s);
201
202 if (s->stream)
203 pa_stream_unref(s->stream);
204
205 if (s->context)
206 pa_context_unref(s->context);
207
208 if (s->mainloop)
209 pa_mainloop_free(s->mainloop);
210
211 pa_xfree(s);
212 }
213
214 int pa_simple_write(pa_simple *p, const void*data, size_t length, int *rerror) {
215 assert(p);
216 assert(data);
217
218 CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
219
220 if (p->dead) {
221 if (rerror)
222 *rerror = pa_context_errno(p->context);
223
224 return -1;
225 }
226
227 while (length > 0) {
228 size_t l;
229
230 while (!(l = pa_stream_writable_size(p->stream)))
231 if (iterate(p, 1, rerror) < 0)
232 return -1;
233
234 if (l > length)
235 l = length;
236
237 pa_stream_write(p->stream, data, l, NULL, 0, PA_SEEK_RELATIVE);
238 data = (const uint8_t*) data + l;
239 length -= l;
240 }
241
242 /* Make sure that no data is pending for write */
243 if (iterate(p, 0, rerror) < 0)
244 return -1;
245
246 return 0;
247 }
248
249 int pa_simple_read(pa_simple *p, void*data, size_t length, int *rerror) {
250 assert(p);
251 assert(data);
252
253 CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE, -1);
254
255 if (p->dead) {
256 if (rerror)
257 *rerror = pa_context_errno(p->context);
258
259 return -1;
260 }
261
262 while (length > 0) {
263
264 if (!p->read_data)
265 if (pa_stream_peek(p->stream, &p->read_data, &p->read_length) >= 0)
266 p->read_index = 0;
267
268 if (p->read_data) {
269 size_t l = length;
270
271 if (p->read_length <= l)
272 l = p->read_length;
273
274 memcpy(data, (const uint8_t*) p->read_data+p->read_index, l);
275
276 data = (uint8_t*) data + l;
277 length -= l;
278
279 p->read_index += l;
280 p->read_length -= l;
281
282 if (!p->read_length) {
283 pa_stream_drop(p->stream);
284 p->read_data = NULL;
285 p->read_length = 0;
286 p->read_index = 0;
287 }
288
289 if (!length)
290 return 0;
291
292 assert(!p->read_data);
293 }
294
295 if (iterate(p, 1, rerror) < 0)
296 return -1;
297 }
298
299 return 0;
300 }
301
302 static void drain_or_flush_complete(pa_stream *s, int success, void *userdata) {
303 pa_simple *p = userdata;
304
305 assert(s);
306 assert(p);
307
308 if (!success)
309 p->dead = 1;
310 }
311
312 int pa_simple_drain(pa_simple *p, int *rerror) {
313 pa_operation *o;
314
315 assert(p);
316
317 CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
318
319 if (p->dead) {
320 if (rerror)
321 *rerror = pa_context_errno(p->context);
322
323 return -1;
324 }
325
326 if (!(o = pa_stream_drain(p->stream, drain_or_flush_complete, p))) {
327 if (rerror)
328 *rerror = pa_context_errno(p->context);
329 return -1;
330 }
331
332 while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) {
333 if (iterate(p, 1, rerror) < 0) {
334 pa_operation_cancel(o);
335 pa_operation_unref(o);
336 return -1;
337 }
338 }
339
340 pa_operation_unref(o);
341
342 if (p->dead && rerror)
343 *rerror = pa_context_errno(p->context);
344
345 return p->dead ? -1 : 0;
346 }
347
348 static void timing_complete(pa_stream *s, int success, void *userdata) {
349 pa_simple *p = userdata;
350
351 assert(s);
352 assert(p);
353
354 if (!success)
355 p->dead = 1;
356 else {
357 int negative = 0;
358 if (pa_stream_get_latency(s, &p->latency, &negative) < 0)
359 p->dead = 1;
360 else if (negative)
361 p->latency = 0;
362 }
363 }
364
365 pa_usec_t pa_simple_get_playback_latency(pa_simple *p, int *rerror) {
366 pa_operation *o;
367
368 assert(p);
369
370 CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
371
372 if (p->dead) {
373 if (rerror)
374 *rerror = pa_context_errno(p->context);
375
376 return (pa_usec_t) -1;
377 }
378
379 p->latency = 0;
380 if (!(o = pa_stream_update_timing_info(p->stream, timing_complete, p))) {
381 if (rerror)
382 *rerror = pa_context_errno(p->context);
383 return (pa_usec_t) -1;
384 }
385
386 while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) {
387
388 if (iterate(p, 1, rerror) < 0) {
389 pa_operation_cancel(o);
390 pa_operation_unref(o);
391 return -1;
392 }
393 }
394
395 pa_operation_unref(o);
396
397 if (p->dead && rerror)
398 *rerror = pa_context_errno(p->context);
399
400 return p->dead ? (pa_usec_t) -1 : p->latency;
401 }
402
403 int pa_simple_flush(pa_simple *p, int *rerror) {
404 pa_operation *o;
405
406 assert(p);
407
408 CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
409
410 if (p->dead) {
411 if (rerror)
412 *rerror = pa_context_errno(p->context);
413
414 return -1;
415 }
416
417 if (!(o = pa_stream_flush(p->stream, drain_or_flush_complete, p))) {
418 if (rerror)
419 *rerror = pa_context_errno(p->context);
420 return -1;
421 }
422
423 while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) {
424 if (iterate(p, 1, rerror) < 0) {
425 pa_operation_cancel(o);
426 pa_operation_unref(o);
427 return -1;
428 }
429 }
430
431 pa_operation_unref(o);
432
433 if (p->dead && rerror)
434 *rerror = pa_context_errno(p->context);
435
436 return p->dead ? -1 : 0;
437 }