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>
34 #include <polypcore/native-common.h>
35 #include <polypcore/xmalloc.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 pa_simple
* pa_simple_new(
131 pa_stream_direction_t dir
,
133 const char *stream_name
,
134 const pa_sample_spec
*ss
,
135 const pa_buffer_attr
*attr
,
139 int error
= PA_ERR_INTERNAL
, r
;
141 CHECK_VALIDITY_RETURN_ANY(rerror
, !server
|| *server
, PA_ERR_INVALID
, NULL
);
142 CHECK_VALIDITY_RETURN_ANY(rerror
, dir
== PA_STREAM_PLAYBACK
|| dir
== PA_STREAM_RECORD
, PA_ERR_INVALID
, NULL
);
143 CHECK_VALIDITY_RETURN_ANY(rerror
, !dev
|| *dev
, PA_ERR_INVALID
, NULL
);
144 CHECK_VALIDITY_RETURN_ANY(rerror
, ss
&& pa_sample_spec_valid(ss
), PA_ERR_INVALID
, NULL
);
146 p
= pa_xnew(pa_simple
, 1);
151 p
->read_index
= p
->read_length
= 0;
153 if (!(p
->mainloop
= pa_threaded_mainloop_new()))
156 if (!(p
->context
= pa_context_new(pa_threaded_mainloop_get_api(p
->mainloop
), name
)))
159 pa_context_set_state_callback(p
->context
, context_state_cb
, p
);
161 if (pa_context_connect(p
->context
, server
, 0, NULL
) < 0) {
162 error
= pa_context_errno(p
->context
);
166 pa_threaded_mainloop_lock(p
->mainloop
);
168 if (pa_threaded_mainloop_start(p
->mainloop
) < 0)
169 goto unlock_and_fail
;
171 /* Wait until the context is ready */
172 pa_threaded_mainloop_wait(p
->mainloop
);
174 if (pa_context_get_state(p
->context
) != PA_CONTEXT_READY
) {
175 error
= pa_context_errno(p
->context
);
176 goto unlock_and_fail
;
179 if (!(p
->stream
= pa_stream_new(p
->context
, stream_name
, ss
, NULL
))) {
180 error
= pa_context_errno(p
->context
);
181 goto unlock_and_fail
;
184 pa_stream_set_state_callback(p
->stream
, stream_state_cb
, p
);
185 pa_stream_set_read_callback(p
->stream
, stream_request_cb
, p
);
186 pa_stream_set_write_callback(p
->stream
, stream_request_cb
, p
);
188 if (dir
== PA_STREAM_PLAYBACK
)
189 r
= pa_stream_connect_playback(p
->stream
, dev
, attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
, NULL
, NULL
);
191 r
= pa_stream_connect_record(p
->stream
, dev
, attr
, PA_STREAM_INTERPOLATE_TIMING
|PA_STREAM_AUTO_TIMING_UPDATE
);
194 error
= pa_context_errno(p
->context
);
195 goto unlock_and_fail
;
198 /* Wait until the stream is ready */
199 pa_threaded_mainloop_wait(p
->mainloop
);
201 /* Wait until the stream is ready */
202 if (pa_stream_get_state(p
->stream
) != PA_STREAM_READY
) {
203 error
= pa_context_errno(p
->context
);
204 goto unlock_and_fail
;
207 pa_threaded_mainloop_unlock(p
->mainloop
);
212 pa_threaded_mainloop_unlock(p
->mainloop
);
221 void pa_simple_free(pa_simple
*s
) {
225 pa_threaded_mainloop_stop(s
->mainloop
);
228 pa_stream_unref(s
->stream
);
231 pa_context_unref(s
->context
);
234 pa_threaded_mainloop_free(s
->mainloop
);
239 int pa_simple_write(pa_simple
*p
, const void*data
, size_t length
, int *rerror
) {
242 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
, -1);
243 CHECK_VALIDITY_RETURN_ANY(rerror
, data
&& length
, PA_ERR_INVALID
, -1);
245 pa_threaded_mainloop_lock(p
->mainloop
);
247 CHECK_DEAD_GOTO(p
, rerror
, unlock_and_fail
);
253 while (!(l
= pa_stream_writable_size(p
->stream
))) {
254 pa_threaded_mainloop_wait(p
->mainloop
);
255 CHECK_DEAD_GOTO(p
, rerror
, unlock_and_fail
);
258 CHECK_SUCCESS_GOTO(p
, rerror
, l
!= (size_t) -1, unlock_and_fail
);
263 r
= pa_stream_write(p
->stream
, data
, l
, NULL
, 0, PA_SEEK_RELATIVE
);
264 CHECK_SUCCESS_GOTO(p
, rerror
, r
>= 0, unlock_and_fail
);
266 data
= (const uint8_t*) data
+ l
;
270 pa_threaded_mainloop_unlock(p
->mainloop
);
274 pa_threaded_mainloop_unlock(p
->mainloop
);
278 int pa_simple_read(pa_simple
*p
, void*data
, size_t length
, int *rerror
) {
281 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
, -1);
282 CHECK_VALIDITY_RETURN_ANY(rerror
, data
&& length
, PA_ERR_INVALID
, -1);
284 pa_threaded_mainloop_lock(p
->mainloop
);
286 CHECK_DEAD_GOTO(p
, rerror
, unlock_and_fail
);
291 while (!p
->read_data
) {
294 r
= pa_stream_peek(p
->stream
, &p
->read_data
, &p
->read_length
);
295 CHECK_SUCCESS_GOTO(p
, rerror
, r
== 0, unlock_and_fail
);
298 pa_threaded_mainloop_wait(p
->mainloop
);
299 CHECK_DEAD_GOTO(p
, rerror
, unlock_and_fail
);
304 l
= p
->read_length
< length
? p
->read_length
: length
;
305 memcpy(data
, (const uint8_t*) p
->read_data
+p
->read_index
, l
);
307 data
= (uint8_t*) data
+ l
;
313 if (!p
->read_length
) {
316 r
= pa_stream_drop(p
->stream
);
321 CHECK_SUCCESS_GOTO(p
, rerror
, r
== 0, unlock_and_fail
);
325 pa_threaded_mainloop_unlock(p
->mainloop
);
329 pa_threaded_mainloop_unlock(p
->mainloop
);
333 static void success_cb(pa_stream
*s
, int success
, void *userdata
) {
334 pa_simple
*p
= userdata
;
339 p
->operation_success
= success
;
340 pa_threaded_mainloop_signal(p
->mainloop
, 0);
343 int pa_simple_drain(pa_simple
*p
, int *rerror
) {
344 pa_operation
*o
= NULL
;
348 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
, -1);
350 pa_threaded_mainloop_lock(p
->mainloop
);
351 CHECK_DEAD_GOTO(p
, rerror
, unlock_and_fail
);
353 o
= pa_stream_drain(p
->stream
, success_cb
, p
);
354 CHECK_SUCCESS_GOTO(p
, rerror
, o
, unlock_and_fail
);
356 p
->operation_success
= 0;
357 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
358 pa_threaded_mainloop_wait(p
->mainloop
);
359 CHECK_DEAD_GOTO(p
, rerror
, unlock_and_fail
);
361 CHECK_SUCCESS_GOTO(p
, rerror
, p
->operation_success
, unlock_and_fail
);
363 pa_operation_unref(o
);
364 pa_threaded_mainloop_unlock(p
->mainloop
);
371 pa_operation_cancel(o
);
372 pa_operation_unref(o
);
375 pa_threaded_mainloop_unlock(p
->mainloop
);
379 int pa_simple_flush(pa_simple
*p
, int *rerror
) {
380 pa_operation
*o
= NULL
;
384 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
, -1);
386 pa_threaded_mainloop_lock(p
->mainloop
);
387 CHECK_DEAD_GOTO(p
, rerror
, unlock_and_fail
);
389 o
= pa_stream_flush(p
->stream
, success_cb
, p
);
390 CHECK_SUCCESS_GOTO(p
, rerror
, o
, unlock_and_fail
);
392 p
->operation_success
= 0;
393 while (pa_operation_get_state(o
) != PA_OPERATION_DONE
) {
394 pa_threaded_mainloop_wait(p
->mainloop
);
395 CHECK_DEAD_GOTO(p
, rerror
, unlock_and_fail
);
397 CHECK_SUCCESS_GOTO(p
, rerror
, p
->operation_success
, unlock_and_fail
);
399 pa_operation_unref(o
);
400 pa_threaded_mainloop_unlock(p
->mainloop
);
407 pa_operation_cancel(o
);
408 pa_operation_unref(o
);
411 pa_threaded_mainloop_unlock(p
->mainloop
);
415 pa_usec_t
pa_simple_get_playback_latency(pa_simple
*p
, int *rerror
) {
421 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
, (pa_usec_t
) -1);
423 pa_threaded_mainloop_lock(p
->mainloop
);
424 CHECK_DEAD_GOTO(p
, rerror
, unlock_and_fail
);
426 r
= pa_stream_get_latency(p
->stream
, &t
, &negative
);
427 CHECK_SUCCESS_GOTO(p
, rerror
, r
>= 0, unlock_and_fail
);
429 pa_threaded_mainloop_unlock(p
->mainloop
);
431 return negative
? 0 : t
;
435 pa_threaded_mainloop_unlock(p
->mainloop
);
436 return (pa_usec_t
) -1;