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