5 This file is part of PulseAudio.
7 Copyright 2004-2006 Lennart Poettering
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.
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.
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
33 #include <pulse/pulseaudio.h>
34 #include <pulse/thread-mainloop.h>
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/native-common.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/macro.h>
44 pa_threaded_mainloop *mainloop;
47 pa_stream_direction_t direction;
49 const void *read_data;
50 size_t read_index, read_length;
52 int operation_success;
55 #define CHECK_VALIDITY_RETURN_ANY(rerror, expression, error, ret) do { \
56 if (!(expression)) { \
63 #define CHECK_SUCCESS_GOTO(p, rerror, expression, label) do { \
64 if (!(expression)) { \
66 *(rerror) = pa_context_errno((p)->context); \
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)) { \
77 *(rerror) = pa_context_errno((p)->context); \
80 *(rerror) = PA_ERR_BADSTATE; \
85 static void context_state_cb(pa_context *c, void *userdata) {
86 pa_simple *p = userdata;
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);
97 case PA_CONTEXT_UNCONNECTED:
98 case PA_CONTEXT_CONNECTING:
99 case PA_CONTEXT_AUTHORIZING:
100 case PA_CONTEXT_SETTING_NAME:
105 static void stream_state_cb(pa_stream *s, void * userdata) {
106 pa_simple *p = userdata;
110 switch (pa_stream_get_state(s)) {
112 case PA_STREAM_READY:
113 case PA_STREAM_FAILED:
114 case PA_STREAM_TERMINATED:
115 pa_threaded_mainloop_signal(p->mainloop, 0);
118 case PA_STREAM_UNCONNECTED:
119 case PA_STREAM_CREATING:
124 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
125 pa_simple *p = userdata;
128 pa_threaded_mainloop_signal(p->mainloop, 0);
131 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
132 pa_simple *p = userdata;
136 pa_threaded_mainloop_signal(p->mainloop, 0);
139 pa_simple* pa_simple_new(
142 pa_stream_direction_t dir,
144 const char *stream_name,
145 const pa_sample_spec *ss,
146 const pa_channel_map *map,
147 const pa_buffer_attr *attr,
151 int error = PA_ERR_INTERNAL, r;
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)
159 p = pa_xnew(pa_simple, 1);
164 p->read_index = p->read_length = 0;
166 if (!(p->mainloop = pa_threaded_mainloop_new()))
169 if (!(p->context = pa_context_new(pa_threaded_mainloop_get_api(p->mainloop), name)))
172 pa_context_set_state_callback(p->context, context_state_cb, p);
174 if (pa_context_connect(p->context, server, 0, NULL) < 0) {
175 error = pa_context_errno(p->context);
179 pa_threaded_mainloop_lock(p->mainloop);
181 if (pa_threaded_mainloop_start(p->mainloop) < 0)
182 goto unlock_and_fail;
184 /* Wait until the context is ready */
185 pa_threaded_mainloop_wait(p->mainloop);
187 if (pa_context_get_state(p->context) != PA_CONTEXT_READY) {
188 error = pa_context_errno(p->context);
189 goto unlock_and_fail;
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;
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);
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);
205 r = pa_stream_connect_record(p->stream, dev, attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE);
208 error = pa_context_errno(p->context);
209 goto unlock_and_fail;
212 /* Wait until the stream is ready */
213 pa_threaded_mainloop_wait(p->mainloop);
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;
221 pa_threaded_mainloop_unlock(p->mainloop);
226 pa_threaded_mainloop_unlock(p->mainloop);
235 void pa_simple_free(pa_simple *s) {
239 pa_threaded_mainloop_stop(s->mainloop);
242 pa_stream_unref(s->stream);
245 pa_context_unref(s->context);
248 pa_threaded_mainloop_free(s->mainloop);
253 int pa_simple_write(pa_simple *p, const void*data, size_t length, int *rerror) {
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);
259 pa_threaded_mainloop_lock(p->mainloop);
261 CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
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);
272 CHECK_SUCCESS_GOTO(p, rerror, l != (size_t) -1, unlock_and_fail);
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);
280 data = (const uint8_t*) data + l;
284 pa_threaded_mainloop_unlock(p->mainloop);
288 pa_threaded_mainloop_unlock(p->mainloop);
292 int pa_simple_read(pa_simple *p, void*data, size_t length, int *rerror) {
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);
298 pa_threaded_mainloop_lock(p->mainloop);
300 CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
305 while (!p->read_data) {
308 r = pa_stream_peek(p->stream, &p->read_data, &p->read_length);
309 CHECK_SUCCESS_GOTO(p, rerror, r == 0, unlock_and_fail);
312 pa_threaded_mainloop_wait(p->mainloop);
313 CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
318 l = p->read_length < length ? p->read_length : length;
319 memcpy(data, (const uint8_t*) p->read_data+p->read_index, l);
321 data = (uint8_t*) data + l;
327 if (!p->read_length) {
330 r = pa_stream_drop(p->stream);
335 CHECK_SUCCESS_GOTO(p, rerror, r == 0, unlock_and_fail);
339 pa_threaded_mainloop_unlock(p->mainloop);
343 pa_threaded_mainloop_unlock(p->mainloop);
347 static void success_cb(pa_stream *s, int success, void *userdata) {
348 pa_simple *p = userdata;
353 p->operation_success = success;
354 pa_threaded_mainloop_signal(p->mainloop, 0);
357 int pa_simple_drain(pa_simple *p, int *rerror) {
358 pa_operation *o = NULL;
362 CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
364 pa_threaded_mainloop_lock(p->mainloop);
365 CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
367 o = pa_stream_drain(p->stream, success_cb, p);
368 CHECK_SUCCESS_GOTO(p, rerror, o, unlock_and_fail);
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);
375 CHECK_SUCCESS_GOTO(p, rerror, p->operation_success, unlock_and_fail);
377 pa_operation_unref(o);
378 pa_threaded_mainloop_unlock(p->mainloop);
385 pa_operation_cancel(o);
386 pa_operation_unref(o);
389 pa_threaded_mainloop_unlock(p->mainloop);
393 int pa_simple_flush(pa_simple *p, int *rerror) {
394 pa_operation *o = NULL;
398 CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
400 pa_threaded_mainloop_lock(p->mainloop);
401 CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
403 o = pa_stream_flush(p->stream, success_cb, p);
404 CHECK_SUCCESS_GOTO(p, rerror, o, unlock_and_fail);
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);
411 CHECK_SUCCESS_GOTO(p, rerror, p->operation_success, unlock_and_fail);
413 pa_operation_unref(o);
414 pa_threaded_mainloop_unlock(p->mainloop);
421 pa_operation_cancel(o);
422 pa_operation_unref(o);
425 pa_threaded_mainloop_unlock(p->mainloop);
429 pa_usec_t pa_simple_get_latency(pa_simple *p, int *rerror) {
435 pa_threaded_mainloop_lock(p->mainloop);
438 CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
440 if (pa_stream_get_latency(p->stream, &t, &negative) >= 0)
443 CHECK_SUCCESS_GOTO(p, rerror, pa_context_errno(p->context) == PA_ERR_NODATA, unlock_and_fail);
445 /* Wait until latency data is available again */
446 pa_threaded_mainloop_wait(p->mainloop);
449 pa_threaded_mainloop_unlock(p->mainloop);
451 return negative ? 0 : t;
455 pa_threaded_mainloop_unlock(p->mainloop);
456 return (pa_usec_t) -1;