Huge trailing whitespace cleanup. Let's keep the tree pure from here on,
[profile/ivi/pulseaudio.git] / src / pulse / simple.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   PulseAudio 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   PulseAudio 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 PulseAudio; 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 <pulse/pulseaudio.h>
32 #include <pulse/thread-mainloop.h>
33 #include <pulse/xmalloc.h>
34
35 #include <pulsecore/native-common.h>
36 #include <pulsecore/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 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
129     pa_simple *p = userdata;
130
131     assert(p);
132
133     pa_threaded_mainloop_signal(p->mainloop, 0);
134 }
135
136 pa_simple* pa_simple_new(
137     const char *server,
138     const char *name,
139     pa_stream_direction_t dir,
140     const char *dev,
141     const char *stream_name,
142     const pa_sample_spec *ss,
143     const pa_channel_map *map,
144     const pa_buffer_attr *attr,
145     int *rerror) {
146
147     pa_simple *p;
148     int error = PA_ERR_INTERNAL, r;
149
150     CHECK_VALIDITY_RETURN_ANY(rerror, !server || *server, PA_ERR_INVALID, NULL);
151     CHECK_VALIDITY_RETURN_ANY(rerror, dir == PA_STREAM_PLAYBACK || dir == PA_STREAM_RECORD, PA_ERR_INVALID, NULL);
152     CHECK_VALIDITY_RETURN_ANY(rerror, !dev || *dev, PA_ERR_INVALID, NULL);
153     CHECK_VALIDITY_RETURN_ANY(rerror, ss && pa_sample_spec_valid(ss), PA_ERR_INVALID, NULL);
154     CHECK_VALIDITY_RETURN_ANY(rerror, !map || (pa_channel_map_valid(map) && map->channels == ss->channels), PA_ERR_INVALID, NULL)
155
156     p = pa_xnew(pa_simple, 1);
157     p->context = NULL;
158     p->stream = NULL;
159     p->direction = dir;
160     p->read_data = NULL;
161     p->read_index = p->read_length = 0;
162
163     if (!(p->mainloop = pa_threaded_mainloop_new()))
164         goto fail;
165
166     if (!(p->context = pa_context_new(pa_threaded_mainloop_get_api(p->mainloop), name)))
167         goto fail;
168
169     pa_context_set_state_callback(p->context, context_state_cb, p);
170
171     if (pa_context_connect(p->context, server, 0, NULL) < 0) {
172         error = pa_context_errno(p->context);
173         goto fail;
174     }
175
176     pa_threaded_mainloop_lock(p->mainloop);
177
178     if (pa_threaded_mainloop_start(p->mainloop) < 0)
179         goto unlock_and_fail;
180
181     /* Wait until the context is ready */
182     pa_threaded_mainloop_wait(p->mainloop);
183
184     if (pa_context_get_state(p->context) != PA_CONTEXT_READY) {
185         error = pa_context_errno(p->context);
186         goto unlock_and_fail;
187     }
188
189     if (!(p->stream = pa_stream_new(p->context, stream_name, ss, map))) {
190         error = pa_context_errno(p->context);
191         goto unlock_and_fail;
192     }
193
194     pa_stream_set_state_callback(p->stream, stream_state_cb, p);
195     pa_stream_set_read_callback(p->stream, stream_request_cb, p);
196     pa_stream_set_write_callback(p->stream, stream_request_cb, p);
197     pa_stream_set_latency_update_callback(p->stream, stream_latency_update_cb, p);
198
199     if (dir == PA_STREAM_PLAYBACK)
200         r = pa_stream_connect_playback(p->stream, dev, attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL);
201     else
202         r = pa_stream_connect_record(p->stream, dev, attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE);
203
204     if (r < 0) {
205         error = pa_context_errno(p->context);
206         goto unlock_and_fail;
207     }
208
209     /* Wait until the stream is ready */
210     pa_threaded_mainloop_wait(p->mainloop);
211
212     /* Wait until the stream is ready */
213     if (pa_stream_get_state(p->stream) != PA_STREAM_READY) {
214         error = pa_context_errno(p->context);
215         goto unlock_and_fail;
216     }
217
218     pa_threaded_mainloop_unlock(p->mainloop);
219
220     return p;
221
222 unlock_and_fail:
223     pa_threaded_mainloop_unlock(p->mainloop);
224
225 fail:
226     if (rerror)
227         *rerror = error;
228     pa_simple_free(p);
229     return NULL;
230 }
231
232 void pa_simple_free(pa_simple *s) {
233     assert(s);
234
235     if (s->mainloop)
236         pa_threaded_mainloop_stop(s->mainloop);
237
238     if (s->stream)
239         pa_stream_unref(s->stream);
240
241     if (s->context)
242         pa_context_unref(s->context);
243
244     if (s->mainloop)
245         pa_threaded_mainloop_free(s->mainloop);
246
247     pa_xfree(s);
248 }
249
250 int pa_simple_write(pa_simple *p, const void*data, size_t length, int *rerror) {
251     assert(p);
252
253     CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
254     CHECK_VALIDITY_RETURN_ANY(rerror, data && length, PA_ERR_INVALID, -1);
255
256     pa_threaded_mainloop_lock(p->mainloop);
257
258     CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
259
260     while (length > 0) {
261         size_t l;
262         int r;
263
264         while (!(l = pa_stream_writable_size(p->stream))) {
265             pa_threaded_mainloop_wait(p->mainloop);
266             CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
267         }
268
269         CHECK_SUCCESS_GOTO(p, rerror, l != (size_t) -1, unlock_and_fail);
270
271         if (l > length)
272             l = length;
273
274         r = pa_stream_write(p->stream, data, l, NULL, 0, PA_SEEK_RELATIVE);
275         CHECK_SUCCESS_GOTO(p, rerror, r >= 0, unlock_and_fail);
276
277         data = (const uint8_t*) data + l;
278         length -= l;
279     }
280
281     pa_threaded_mainloop_unlock(p->mainloop);
282     return 0;
283
284 unlock_and_fail:
285     pa_threaded_mainloop_unlock(p->mainloop);
286     return -1;
287 }
288
289 int pa_simple_read(pa_simple *p, void*data, size_t length, int *rerror) {
290     assert(p);
291
292     CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE, -1);
293     CHECK_VALIDITY_RETURN_ANY(rerror, data && length, PA_ERR_INVALID, -1);
294
295     pa_threaded_mainloop_lock(p->mainloop);
296
297     CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
298
299     while (length > 0) {
300         size_t l;
301
302         while (!p->read_data) {
303             int r;
304
305             r = pa_stream_peek(p->stream, &p->read_data, &p->read_length);
306             CHECK_SUCCESS_GOTO(p, rerror, r == 0, unlock_and_fail);
307
308             if (!p->read_data) {
309                 pa_threaded_mainloop_wait(p->mainloop);
310                 CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
311             } else
312                 p->read_index = 0;
313         }
314
315         l = p->read_length < length ? p->read_length : length;
316         memcpy(data, (const uint8_t*) p->read_data+p->read_index, l);
317
318         data = (uint8_t*) data + l;
319         length -= l;
320
321         p->read_index += l;
322         p->read_length -= l;
323
324         if (!p->read_length) {
325             int r;
326
327             r = pa_stream_drop(p->stream);
328             p->read_data = NULL;
329             p->read_length = 0;
330             p->read_index = 0;
331
332             CHECK_SUCCESS_GOTO(p, rerror, r == 0, unlock_and_fail);
333         }
334     }
335
336     pa_threaded_mainloop_unlock(p->mainloop);
337     return 0;
338
339 unlock_and_fail:
340     pa_threaded_mainloop_unlock(p->mainloop);
341     return -1;
342 }
343
344 static void success_cb(pa_stream *s, int success, void *userdata) {
345     pa_simple *p = userdata;
346
347     assert(s);
348     assert(p);
349
350     p->operation_success = success;
351     pa_threaded_mainloop_signal(p->mainloop, 0);
352 }
353
354 int pa_simple_drain(pa_simple *p, int *rerror) {
355     pa_operation *o = NULL;
356
357     assert(p);
358
359     CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
360
361     pa_threaded_mainloop_lock(p->mainloop);
362     CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
363
364     o = pa_stream_drain(p->stream, success_cb, p);
365     CHECK_SUCCESS_GOTO(p, rerror, o, unlock_and_fail);
366
367     p->operation_success = 0;
368     while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
369         pa_threaded_mainloop_wait(p->mainloop);
370         CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
371     }
372     CHECK_SUCCESS_GOTO(p, rerror, p->operation_success, unlock_and_fail);
373
374     pa_operation_unref(o);
375     pa_threaded_mainloop_unlock(p->mainloop);
376
377     return 0;
378
379 unlock_and_fail:
380
381     if (o) {
382         pa_operation_cancel(o);
383         pa_operation_unref(o);
384     }
385
386     pa_threaded_mainloop_unlock(p->mainloop);
387     return -1;
388 }
389
390 int pa_simple_flush(pa_simple *p, int *rerror) {
391     pa_operation *o = NULL;
392
393     assert(p);
394
395     CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
396
397     pa_threaded_mainloop_lock(p->mainloop);
398     CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
399
400     o = pa_stream_flush(p->stream, success_cb, p);
401     CHECK_SUCCESS_GOTO(p, rerror, o, unlock_and_fail);
402
403     p->operation_success = 0;
404     while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
405         pa_threaded_mainloop_wait(p->mainloop);
406         CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
407     }
408     CHECK_SUCCESS_GOTO(p, rerror, p->operation_success, unlock_and_fail);
409
410     pa_operation_unref(o);
411     pa_threaded_mainloop_unlock(p->mainloop);
412
413     return 0;
414
415 unlock_and_fail:
416
417     if (o) {
418         pa_operation_cancel(o);
419         pa_operation_unref(o);
420     }
421
422     pa_threaded_mainloop_unlock(p->mainloop);
423     return -1;
424 }
425
426 pa_usec_t pa_simple_get_latency(pa_simple *p, int *rerror) {
427     pa_usec_t t;
428     int negative;
429
430     assert(p);
431
432     pa_threaded_mainloop_lock(p->mainloop);
433
434     for (;;) {
435         CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
436
437         if (pa_stream_get_latency(p->stream, &t, &negative) >= 0)
438             break;
439
440         CHECK_SUCCESS_GOTO(p, rerror, pa_context_errno(p->context) == PA_ERR_NODATA, unlock_and_fail);
441
442         /* Wait until latency data is available again */
443         pa_threaded_mainloop_wait(p->mainloop);
444     }
445
446     pa_threaded_mainloop_unlock(p->mainloop);
447
448     return negative ? 0 : t;
449
450 unlock_and_fail:
451
452     pa_threaded_mainloop_unlock(p->mainloop);
453     return (pa_usec_t) -1;
454 }
455