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