4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
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.
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.
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
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>
43 pa_threaded_mainloop *mainloop;
46 pa_stream_direction_t direction;
48 const void *read_data;
49 size_t read_index, read_length;
51 int operation_success;
54 #define CHECK_VALIDITY_RETURN_ANY(rerror, expression, error, ret) do { \
55 if (!(expression)) { \
62 #define CHECK_SUCCESS_GOTO(p, rerror, expression, label) do { \
63 if (!(expression)) { \
65 *(rerror) = pa_context_errno((p)->context); \
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)) { \
76 *(rerror) = pa_context_errno((p)->context); \
79 *(rerror) = PA_ERR_BADSTATE; \
84 static void context_state_cb(pa_context *c, void *userdata) {
85 pa_simple *p = userdata;
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);
96 case PA_CONTEXT_UNCONNECTED:
97 case PA_CONTEXT_CONNECTING:
98 case PA_CONTEXT_AUTHORIZING:
99 case PA_CONTEXT_SETTING_NAME:
104 static void stream_state_cb(pa_stream *s, void * userdata) {
105 pa_simple *p = userdata;
109 switch (pa_stream_get_state(s)) {
111 case PA_STREAM_READY:
112 case PA_STREAM_FAILED:
113 case PA_STREAM_TERMINATED:
114 pa_threaded_mainloop_signal(p->mainloop, 0);
117 case PA_STREAM_UNCONNECTED:
118 case PA_STREAM_CREATING:
123 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
124 pa_simple *p = userdata;
127 pa_threaded_mainloop_signal(p->mainloop, 0);
130 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
131 pa_simple *p = userdata;
135 pa_threaded_mainloop_signal(p->mainloop, 0);
138 pa_simple* pa_simple_new(
141 pa_stream_direction_t dir,
143 const char *stream_name,
144 const pa_sample_spec *ss,
145 const pa_channel_map *map,
146 const pa_buffer_attr *attr,
150 int error = PA_ERR_INTERNAL, r;
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)
158 p = pa_xnew(pa_simple, 1);
163 p->read_index = p->read_length = 0;
165 if (!(p->mainloop = pa_threaded_mainloop_new()))
168 if (!(p->context = pa_context_new(pa_threaded_mainloop_get_api(p->mainloop), name)))
171 pa_context_set_state_callback(p->context, context_state_cb, p);
173 if (pa_context_connect(p->context, server, 0, NULL) < 0) {
174 error = pa_context_errno(p->context);
178 pa_threaded_mainloop_lock(p->mainloop);
180 if (pa_threaded_mainloop_start(p->mainloop) < 0)
181 goto unlock_and_fail;
183 /* Wait until the context is ready */
184 pa_threaded_mainloop_wait(p->mainloop);
186 if (pa_context_get_state(p->context) != PA_CONTEXT_READY) {
187 error = pa_context_errno(p->context);
188 goto unlock_and_fail;
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;
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);
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);
204 r = pa_stream_connect_record(p->stream, dev, attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE);
207 error = pa_context_errno(p->context);
208 goto unlock_and_fail;
211 /* Wait until the stream is ready */
212 pa_threaded_mainloop_wait(p->mainloop);
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;
220 pa_threaded_mainloop_unlock(p->mainloop);
225 pa_threaded_mainloop_unlock(p->mainloop);
234 void pa_simple_free(pa_simple *s) {
238 pa_threaded_mainloop_stop(s->mainloop);
241 pa_stream_unref(s->stream);
244 pa_context_unref(s->context);
247 pa_threaded_mainloop_free(s->mainloop);
252 int pa_simple_write(pa_simple *p, const void*data, size_t length, int *rerror) {
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);
258 pa_threaded_mainloop_lock(p->mainloop);
260 CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
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);
271 CHECK_SUCCESS_GOTO(p, rerror, l != (size_t) -1, unlock_and_fail);
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);
279 data = (const uint8_t*) data + l;
283 pa_threaded_mainloop_unlock(p->mainloop);
287 pa_threaded_mainloop_unlock(p->mainloop);
291 int pa_simple_read(pa_simple *p, void*data, size_t length, int *rerror) {
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);
297 pa_threaded_mainloop_lock(p->mainloop);
299 CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
304 while (!p->read_data) {
307 r = pa_stream_peek(p->stream, &p->read_data, &p->read_length);
308 CHECK_SUCCESS_GOTO(p, rerror, r == 0, unlock_and_fail);
311 pa_threaded_mainloop_wait(p->mainloop);
312 CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
317 l = p->read_length < length ? p->read_length : length;
318 memcpy(data, (const uint8_t*) p->read_data+p->read_index, l);
320 data = (uint8_t*) data + l;
326 if (!p->read_length) {
329 r = pa_stream_drop(p->stream);
334 CHECK_SUCCESS_GOTO(p, rerror, r == 0, unlock_and_fail);
338 pa_threaded_mainloop_unlock(p->mainloop);
342 pa_threaded_mainloop_unlock(p->mainloop);
346 static void success_cb(pa_stream *s, int success, void *userdata) {
347 pa_simple *p = userdata;
352 p->operation_success = success;
353 pa_threaded_mainloop_signal(p->mainloop, 0);
356 int pa_simple_drain(pa_simple *p, int *rerror) {
357 pa_operation *o = NULL;
361 CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
363 pa_threaded_mainloop_lock(p->mainloop);
364 CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
366 o = pa_stream_drain(p->stream, success_cb, p);
367 CHECK_SUCCESS_GOTO(p, rerror, o, unlock_and_fail);
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);
374 CHECK_SUCCESS_GOTO(p, rerror, p->operation_success, unlock_and_fail);
376 pa_operation_unref(o);
377 pa_threaded_mainloop_unlock(p->mainloop);
384 pa_operation_cancel(o);
385 pa_operation_unref(o);
388 pa_threaded_mainloop_unlock(p->mainloop);
392 int pa_simple_flush(pa_simple *p, int *rerror) {
393 pa_operation *o = NULL;
397 CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
399 pa_threaded_mainloop_lock(p->mainloop);
400 CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
402 o = pa_stream_flush(p->stream, success_cb, p);
403 CHECK_SUCCESS_GOTO(p, rerror, o, unlock_and_fail);
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);
410 CHECK_SUCCESS_GOTO(p, rerror, p->operation_success, unlock_and_fail);
412 pa_operation_unref(o);
413 pa_threaded_mainloop_unlock(p->mainloop);
420 pa_operation_cancel(o);
421 pa_operation_unref(o);
424 pa_threaded_mainloop_unlock(p->mainloop);
428 pa_usec_t pa_simple_get_latency(pa_simple *p, int *rerror) {
434 pa_threaded_mainloop_lock(p->mainloop);
437 CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
439 if (pa_stream_get_latency(p->stream, &t, &negative) >= 0)
442 CHECK_SUCCESS_GOTO(p, rerror, pa_context_errno(p->context) == PA_ERR_NODATA, unlock_and_fail);
444 /* Wait until latency data is available again */
445 pa_threaded_mainloop_wait(p->mainloop);
448 pa_threaded_mainloop_unlock(p->mainloop);
450 return negative ? 0 : t;
454 pa_threaded_mainloop_unlock(p->mainloop);
455 return (pa_usec_t) -1;