really create glitch-free branch
[profile/ivi/pulseaudio.git] / src / pulse / simple.c
1
2 /* $Id$ */
3
4 /***
5   This file is part of PulseAudio.
6
7   Copyright 2004-2006 Lennart Poettering
8
9   PulseAudio is free software; you can redistribute it and/or modify
10   it under the terms of the GNU Lesser General Public License as published
11   by the Free Software Foundation; either version 2 of the License,
12   or (at your option) any later version.
13
14   PulseAudio is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with PulseAudio; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22   USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 #include <pulse/pulseaudio.h>
34 #include <pulse/thread-mainloop.h>
35 #include <pulse/xmalloc.h>
36
37 #include <pulsecore/native-common.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/macro.h>
40
41 #include "simple.h"
42
43 struct pa_simple {
44     pa_threaded_mainloop *mainloop;
45     pa_context *context;
46     pa_stream *stream;
47     pa_stream_direction_t direction;
48
49     const void *read_data;
50     size_t read_index, read_length;
51
52     int operation_success;
53 };
54
55 #define CHECK_VALIDITY_RETURN_ANY(rerror, expression, error, ret) do { \
56 if (!(expression)) { \
57     if (rerror) \
58         *(rerror) = error; \
59     return (ret); \
60     }  \
61 } while(0);
62
63 #define CHECK_SUCCESS_GOTO(p, rerror, expression, label) do { \
64 if (!(expression)) { \
65     if (rerror) \
66         *(rerror) = pa_context_errno((p)->context); \
67     goto label; \
68     }  \
69 } while(0);
70
71 #define CHECK_DEAD_GOTO(p, rerror, label) do { \
72 if (!(p)->context || pa_context_get_state((p)->context) != PA_CONTEXT_READY || \
73     !(p)->stream || pa_stream_get_state((p)->stream) != PA_STREAM_READY) { \
74         if (((p)->context && pa_context_get_state((p)->context) == PA_CONTEXT_FAILED) || \
75             ((p)->stream && pa_stream_get_state((p)->stream) == PA_STREAM_FAILED)) { \
76             if (rerror) \
77                 *(rerror) = pa_context_errno((p)->context); \
78         } else \
79             if (rerror) \
80                 *(rerror) = PA_ERR_BADSTATE; \
81         goto label; \
82     } \
83 } while(0);
84
85 static void context_state_cb(pa_context *c, void *userdata) {
86     pa_simple *p = userdata;
87     pa_assert(c);
88     pa_assert(p);
89
90     switch (pa_context_get_state(c)) {
91         case PA_CONTEXT_READY:
92         case PA_CONTEXT_TERMINATED:
93         case PA_CONTEXT_FAILED:
94             pa_threaded_mainloop_signal(p->mainloop, 0);
95             break;
96
97         case PA_CONTEXT_UNCONNECTED:
98         case PA_CONTEXT_CONNECTING:
99         case PA_CONTEXT_AUTHORIZING:
100         case PA_CONTEXT_SETTING_NAME:
101             break;
102     }
103 }
104
105 static void stream_state_cb(pa_stream *s, void * userdata) {
106     pa_simple *p = userdata;
107     pa_assert(s);
108     pa_assert(p);
109
110     switch (pa_stream_get_state(s)) {
111
112         case PA_STREAM_READY:
113         case PA_STREAM_FAILED:
114         case PA_STREAM_TERMINATED:
115             pa_threaded_mainloop_signal(p->mainloop, 0);
116             break;
117
118         case PA_STREAM_UNCONNECTED:
119         case PA_STREAM_CREATING:
120             break;
121     }
122 }
123
124 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
125     pa_simple *p = userdata;
126     pa_assert(p);
127
128     pa_threaded_mainloop_signal(p->mainloop, 0);
129 }
130
131 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
132     pa_simple *p = userdata;
133
134     pa_assert(p);
135
136     pa_threaded_mainloop_signal(p->mainloop, 0);
137 }
138
139 pa_simple* pa_simple_new(
140         const char *server,
141         const char *name,
142         pa_stream_direction_t dir,
143         const char *dev,
144         const char *stream_name,
145         const pa_sample_spec *ss,
146         const pa_channel_map *map,
147         const pa_buffer_attr *attr,
148         int *rerror) {
149
150     pa_simple *p;
151     int error = PA_ERR_INTERNAL, r;
152
153     CHECK_VALIDITY_RETURN_ANY(rerror, !server || *server, PA_ERR_INVALID, NULL);
154     CHECK_VALIDITY_RETURN_ANY(rerror, dir == PA_STREAM_PLAYBACK || dir == PA_STREAM_RECORD, PA_ERR_INVALID, NULL);
155     CHECK_VALIDITY_RETURN_ANY(rerror, !dev || *dev, PA_ERR_INVALID, NULL);
156     CHECK_VALIDITY_RETURN_ANY(rerror, ss && pa_sample_spec_valid(ss), PA_ERR_INVALID, NULL);
157     CHECK_VALIDITY_RETURN_ANY(rerror, !map || (pa_channel_map_valid(map) && map->channels == ss->channels), PA_ERR_INVALID, NULL)
158
159     p = pa_xnew(pa_simple, 1);
160     p->context = NULL;
161     p->stream = NULL;
162     p->direction = dir;
163     p->read_data = NULL;
164     p->read_index = p->read_length = 0;
165
166     if (!(p->mainloop = pa_threaded_mainloop_new()))
167         goto fail;
168
169     if (!(p->context = pa_context_new(pa_threaded_mainloop_get_api(p->mainloop), name)))
170         goto fail;
171
172     pa_context_set_state_callback(p->context, context_state_cb, p);
173
174     if (pa_context_connect(p->context, server, 0, NULL) < 0) {
175         error = pa_context_errno(p->context);
176         goto fail;
177     }
178
179     pa_threaded_mainloop_lock(p->mainloop);
180
181     if (pa_threaded_mainloop_start(p->mainloop) < 0)
182         goto unlock_and_fail;
183
184     /* Wait until the context is ready */
185     pa_threaded_mainloop_wait(p->mainloop);
186
187     if (pa_context_get_state(p->context) != PA_CONTEXT_READY) {
188         error = pa_context_errno(p->context);
189         goto unlock_and_fail;
190     }
191
192     if (!(p->stream = pa_stream_new(p->context, stream_name, ss, map))) {
193         error = pa_context_errno(p->context);
194         goto unlock_and_fail;
195     }
196
197     pa_stream_set_state_callback(p->stream, stream_state_cb, p);
198     pa_stream_set_read_callback(p->stream, stream_request_cb, p);
199     pa_stream_set_write_callback(p->stream, stream_request_cb, p);
200     pa_stream_set_latency_update_callback(p->stream, stream_latency_update_cb, p);
201
202     if (dir == PA_STREAM_PLAYBACK)
203         r = pa_stream_connect_playback(p->stream, dev, attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL);
204     else
205         r = pa_stream_connect_record(p->stream, dev, attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE);
206
207     if (r < 0) {
208         error = pa_context_errno(p->context);
209         goto unlock_and_fail;
210     }
211
212     /* Wait until the stream is ready */
213     pa_threaded_mainloop_wait(p->mainloop);
214
215     /* Wait until the stream is ready */
216     if (pa_stream_get_state(p->stream) != PA_STREAM_READY) {
217         error = pa_context_errno(p->context);
218         goto unlock_and_fail;
219     }
220
221     pa_threaded_mainloop_unlock(p->mainloop);
222
223     return p;
224
225 unlock_and_fail:
226     pa_threaded_mainloop_unlock(p->mainloop);
227
228 fail:
229     if (rerror)
230         *rerror = error;
231     pa_simple_free(p);
232     return NULL;
233 }
234
235 void pa_simple_free(pa_simple *s) {
236     pa_assert(s);
237
238     if (s->mainloop)
239         pa_threaded_mainloop_stop(s->mainloop);
240
241     if (s->stream)
242         pa_stream_unref(s->stream);
243
244     if (s->context)
245         pa_context_unref(s->context);
246
247     if (s->mainloop)
248         pa_threaded_mainloop_free(s->mainloop);
249
250     pa_xfree(s);
251 }
252
253 int pa_simple_write(pa_simple *p, const void*data, size_t length, int *rerror) {
254     pa_assert(p);
255
256     CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
257     CHECK_VALIDITY_RETURN_ANY(rerror, data && length, PA_ERR_INVALID, -1);
258
259     pa_threaded_mainloop_lock(p->mainloop);
260
261     CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
262
263     while (length > 0) {
264         size_t l;
265         int r;
266
267         while (!(l = pa_stream_writable_size(p->stream))) {
268             pa_threaded_mainloop_wait(p->mainloop);
269             CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
270         }
271
272         CHECK_SUCCESS_GOTO(p, rerror, l != (size_t) -1, unlock_and_fail);
273
274         if (l > length)
275             l = length;
276
277         r = pa_stream_write(p->stream, data, l, NULL, 0, PA_SEEK_RELATIVE);
278         CHECK_SUCCESS_GOTO(p, rerror, r >= 0, unlock_and_fail);
279
280         data = (const uint8_t*) data + l;
281         length -= l;
282     }
283
284     pa_threaded_mainloop_unlock(p->mainloop);
285     return 0;
286
287 unlock_and_fail:
288     pa_threaded_mainloop_unlock(p->mainloop);
289     return -1;
290 }
291
292 int pa_simple_read(pa_simple *p, void*data, size_t length, int *rerror) {
293     pa_assert(p);
294
295     CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE, -1);
296     CHECK_VALIDITY_RETURN_ANY(rerror, data && length, PA_ERR_INVALID, -1);
297
298     pa_threaded_mainloop_lock(p->mainloop);
299
300     CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
301
302     while (length > 0) {
303         size_t l;
304
305         while (!p->read_data) {
306             int r;
307
308             r = pa_stream_peek(p->stream, &p->read_data, &p->read_length);
309             CHECK_SUCCESS_GOTO(p, rerror, r == 0, unlock_and_fail);
310
311             if (!p->read_data) {
312                 pa_threaded_mainloop_wait(p->mainloop);
313                 CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
314             } else
315                 p->read_index = 0;
316         }
317
318         l = p->read_length < length ? p->read_length : length;
319         memcpy(data, (const uint8_t*) p->read_data+p->read_index, l);
320
321         data = (uint8_t*) data + l;
322         length -= l;
323
324         p->read_index += l;
325         p->read_length -= l;
326
327         if (!p->read_length) {
328             int r;
329
330             r = pa_stream_drop(p->stream);
331             p->read_data = NULL;
332             p->read_length = 0;
333             p->read_index = 0;
334
335             CHECK_SUCCESS_GOTO(p, rerror, r == 0, unlock_and_fail);
336         }
337     }
338
339     pa_threaded_mainloop_unlock(p->mainloop);
340     return 0;
341
342 unlock_and_fail:
343     pa_threaded_mainloop_unlock(p->mainloop);
344     return -1;
345 }
346
347 static void success_cb(pa_stream *s, int success, void *userdata) {
348     pa_simple *p = userdata;
349
350     pa_assert(s);
351     pa_assert(p);
352
353     p->operation_success = success;
354     pa_threaded_mainloop_signal(p->mainloop, 0);
355 }
356
357 int pa_simple_drain(pa_simple *p, int *rerror) {
358     pa_operation *o = NULL;
359
360     pa_assert(p);
361
362     CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
363
364     pa_threaded_mainloop_lock(p->mainloop);
365     CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
366
367     o = pa_stream_drain(p->stream, success_cb, p);
368     CHECK_SUCCESS_GOTO(p, rerror, o, unlock_and_fail);
369
370     p->operation_success = 0;
371     while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
372         pa_threaded_mainloop_wait(p->mainloop);
373         CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
374     }
375     CHECK_SUCCESS_GOTO(p, rerror, p->operation_success, unlock_and_fail);
376
377     pa_operation_unref(o);
378     pa_threaded_mainloop_unlock(p->mainloop);
379
380     return 0;
381
382 unlock_and_fail:
383
384     if (o) {
385         pa_operation_cancel(o);
386         pa_operation_unref(o);
387     }
388
389     pa_threaded_mainloop_unlock(p->mainloop);
390     return -1;
391 }
392
393 int pa_simple_flush(pa_simple *p, int *rerror) {
394     pa_operation *o = NULL;
395
396     pa_assert(p);
397
398     CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
399
400     pa_threaded_mainloop_lock(p->mainloop);
401     CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
402
403     o = pa_stream_flush(p->stream, success_cb, p);
404     CHECK_SUCCESS_GOTO(p, rerror, o, unlock_and_fail);
405
406     p->operation_success = 0;
407     while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
408         pa_threaded_mainloop_wait(p->mainloop);
409         CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
410     }
411     CHECK_SUCCESS_GOTO(p, rerror, p->operation_success, unlock_and_fail);
412
413     pa_operation_unref(o);
414     pa_threaded_mainloop_unlock(p->mainloop);
415
416     return 0;
417
418 unlock_and_fail:
419
420     if (o) {
421         pa_operation_cancel(o);
422         pa_operation_unref(o);
423     }
424
425     pa_threaded_mainloop_unlock(p->mainloop);
426     return -1;
427 }
428
429 pa_usec_t pa_simple_get_latency(pa_simple *p, int *rerror) {
430     pa_usec_t t;
431     int negative;
432
433     pa_assert(p);
434
435     pa_threaded_mainloop_lock(p->mainloop);
436
437     for (;;) {
438         CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
439
440         if (pa_stream_get_latency(p->stream, &t, &negative) >= 0)
441             break;
442
443         CHECK_SUCCESS_GOTO(p, rerror, pa_context_errno(p->context) == PA_ERR_NODATA, unlock_and_fail);
444
445         /* Wait until latency data is available again */
446         pa_threaded_mainloop_wait(p->mainloop);
447     }
448
449     pa_threaded_mainloop_unlock(p->mainloop);
450
451     return negative ? 0 : t;
452
453 unlock_and_fail:
454
455     pa_threaded_mainloop_unlock(p->mainloop);
456     return (pa_usec_t) -1;
457 }
458