4 This file is part of polypaudio.
6 polypaudio 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.
11 polypaudio 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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <polyp/polypaudio.h>
32 #include <polyp/thread-mainloop.h>
33 #include <polyp/xmalloc.h>
35 #include <polypcore/native-common.h>
36 #include <polypcore/log.h>
41 pa_threaded_mainloop
*mainloop
;
44 pa_stream_direction_t direction
;
46 const void *read_data
;
47 size_t read_index
, read_length
;
49 int operation_success
;
52 #define CHECK_VALIDITY_RETURN_ANY(rerror, expression, error, ret) do { \
53 if (!(expression)) { \
60 #define CHECK_SUCCESS_GOTO(p, rerror, expression, label) do { \
61 if (!(expression)) { \
63 *(rerror) = pa_context_errno((p)->context); \
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)) { \
74 *(rerror) = pa_context_errno((p)->context); \
77 *(rerror) = PA_ERR_BADSTATE; \
82 static void context_state_cb(pa_context
*c
, void *userdata
) {
83 pa_simple
*p
= userdata
;
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);
94 case PA_CONTEXT_UNCONNECTED
:
95 case PA_CONTEXT_CONNECTING
:
96 case PA_CONTEXT_AUTHORIZING
:
97 case PA_CONTEXT_SETTING_NAME
:
102 static void stream_state_cb(pa_stream
*s
, void * userdata
) {
103 pa_simple
*p
= userdata
;
107 switch (pa_stream_get_state(s
)) {
109 case PA_STREAM_READY
:
110 case PA_STREAM_FAILED
:
111 case PA_STREAM_TERMINATED
:
112 pa_threaded_mainloop_signal(p
->mainloop
, 0);
115 case PA_STREAM_UNCONNECTED
:
116 case PA_STREAM_CREATING
:
121 static void stream_request_cb(pa_stream
*s
, size_t length
, void *userdata
) {
122 pa_simple
*p
= userdata
;
125 pa_threaded_mainloop_signal(p
->mainloop
, 0);
128 static void stream_latency_update_cb(pa_stream
*s
, void *userdata
) {
129 pa_simple
*p
= userdata
;
133 pa_threaded_mainloop_signal(p
->mainloop
, 0);
136 pa_simple
* pa_simple_new(
139 pa_stream_direction_t dir
,
141 const char *stream_name
,
142 const pa_sample_spec
*ss
,
143 const pa_channel_map
*map
,
144 const pa_buffer_attr
*attr
,
148 int error
= PA_ERR_INTERNAL
, r
;
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
)
156 p
= pa_xnew(pa_simple
, 1);
161 p
->read_index
= p
->read_length
= 0;
163 if (!(p
->mainloop
= pa_threaded_mainloop_new()))
166 if (!(p
->context
= pa_context_new(pa_threaded_mainloop_get_api(p
->mainloop
), name
)))
169 pa_context_set_state_callback(p
->context
, context_state_cb
, p
);
171 if (pa_context_connect(p
->context
, server
, 0, NULL
) < 0) {
172 error
= pa_context_errno(p
->context
);
176 pa_threaded_mainloop_lock(p
->mainloop
);
178 if (pa_threaded_mainloop_start(p
->mainloop
) < 0)
179 goto unlock_and_fail
;
181 /* Wait until the context is ready */
182 pa_threaded_mainloop_wait(p
->mainloop
);
184 if (pa_context_get_state(p
->context
) != PA_CONTEXT_READY
) {
185 error
= pa_context_errno(p
->context
);
186 goto unlock_and_fail
;
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
;
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
);
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
);
202 r
= pa_stream_connect_record(p
->stream
, dev
, attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
);
205 error
= pa_context_errno(p
->context
);
206 goto unlock_and_fail
;
209 /* Wait until the stream is ready */
210 pa_threaded_mainloop_wait(p
->mainloop
);
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
;
218 pa_threaded_mainloop_unlock(p
->mainloop
);
223 pa_threaded_mainloop_unlock(p
->mainloop
);
232 void pa_simple_free(pa_simple
*s
) {
236 pa_threaded_mainloop_stop(s
->mainloop
);
239 pa_stream_unref(s
->stream
);
242 pa_context_unref(s
->context
);
245 pa_threaded_mainloop_free(s
->mainloop
);
250 int pa_simple_write(pa_simple
*p
, const void*data
, size_t length
, int *rerror
) {
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);
256 pa_threaded_mainloop_lock(p
->mainloop
);
258 CHECK_DEAD_GOTO(p
, rerror
, unlock_and_fail
);
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
);
269 CHECK_SUCCESS_GOTO(p
, rerror
, l
!= (size_t) -1, unlock_and_fail
);
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
);
277 data
= (const uint8_t*) data
+ l
;
281 pa_threaded_mainloop_unlock(p
->mainloop
);
285 pa_threaded_mainloop_unlock(p
->mainloop
);
289 int pa_simple_read(pa_simple
*p
, void*data
, size_t length
, int *rerror
) {
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);
295 pa_threaded_mainloop_lock(p
->mainloop
);
297 CHECK_DEAD_GOTO(p
, rerror
, unlock_and_fail
);
302 while (!p
->read_data
) {
305 r
= pa_stream_peek(p
->stream
, &p
->read_data
, &p
->read_length
);
306 CHECK_SUCCESS_GOTO(p
, rerror
, r
== 0, unlock_and_fail
);
309 pa_threaded_mainloop_wait(p
->mainloop
);
310 CHECK_DEAD_GOTO(p
, rerror
, unlock_and_fail
);
315 l
= p
->read_length
< length
? p
->read_length
: length
;
316 memcpy(data
, (const uint8_t*) p
->read_data
+p
->read_index
, l
);
318 data
= (uint8_t*) data
+ l
;
324 if (!p
->read_length
) {
327 r
= pa_stream_drop(p
->stream
);
332 CHECK_SUCCESS_GOTO(p
, rerror
, r
== 0, unlock_and_fail
);
336 pa_threaded_mainloop_unlock(p
->mainloop
);
340 pa_threaded_mainloop_unlock(p
->mainloop
);
344 static void success_cb(pa_stream
*s
, int success
, void *userdata
) {
345 pa_simple
*p
= userdata
;
350 p
->operation_success
= success
;
351 pa_threaded_mainloop_signal(p
->mainloop
, 0);
354 int pa_simple_drain(pa_simple
*p
, int *rerror
) {
355 pa_operation
*o
= NULL
;
359 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
, -1);
361 pa_threaded_mainloop_lock(p
->mainloop
);
362 CHECK_DEAD_GOTO(p
, rerror
, unlock_and_fail
);
364 o
= pa_stream_drain(p
->stream
, success_cb
, p
);
365 CHECK_SUCCESS_GOTO(p
, rerror
, o
, unlock_and_fail
);
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
);
372 CHECK_SUCCESS_GOTO(p
, rerror
, p
->operation_success
, unlock_and_fail
);
374 pa_operation_unref(o
);
375 pa_threaded_mainloop_unlock(p
->mainloop
);
382 pa_operation_cancel(o
);
383 pa_operation_unref(o
);
386 pa_threaded_mainloop_unlock(p
->mainloop
);
390 int pa_simple_flush(pa_simple
*p
, int *rerror
) {
391 pa_operation
*o
= NULL
;
395 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
, -1);
397 pa_threaded_mainloop_lock(p
->mainloop
);
398 CHECK_DEAD_GOTO(p
, rerror
, unlock_and_fail
);
400 o
= pa_stream_flush(p
->stream
, success_cb
, p
);
401 CHECK_SUCCESS_GOTO(p
, rerror
, o
, unlock_and_fail
);
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
);
408 CHECK_SUCCESS_GOTO(p
, rerror
, p
->operation_success
, unlock_and_fail
);
410 pa_operation_unref(o
);
411 pa_threaded_mainloop_unlock(p
->mainloop
);
418 pa_operation_cancel(o
);
419 pa_operation_unref(o
);
422 pa_threaded_mainloop_unlock(p
->mainloop
);
426 pa_usec_t
pa_simple_get_latency(pa_simple
*p
, int *rerror
) {
432 pa_threaded_mainloop_lock(p
->mainloop
);
435 CHECK_DEAD_GOTO(p
, rerror
, unlock_and_fail
);
437 if (pa_stream_get_latency(p
->stream
, &t
, &negative
) >= 0)
440 CHECK_SUCCESS_GOTO(p
, rerror
, pa_context_errno(p
->context
) == PA_ERR_NODATA
, unlock_and_fail
);
442 /* Wait until latency data is available again */
443 pa_threaded_mainloop_wait(p
->mainloop
);
446 pa_threaded_mainloop_unlock(p
->mainloop
);
448 return negative
? 0 : t
;
452 pa_threaded_mainloop_unlock(p
->mainloop
);
453 return (pa_usec_t
) -1;