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/mainloop.h>
34 #include <polypcore/native-common.h>
35 #include <polypcore/xmalloc.h>
36 #include <polypcore/log.h>
40 #define CHECK_VALIDITY_RETURN_ANY(rerror, expression, error, ret) do { \
41 if (!(expression)) { \
49 pa_mainloop
*mainloop
;
52 pa_stream_direction_t direction
;
56 const void *read_data
;
57 size_t read_index
, read_length
;
61 static int check_error(pa_simple
*p
, int *rerror
) {
62 pa_context_state_t cst
;
63 pa_stream_state_t sst
;
66 if ((cst
= pa_context_get_state(p
->context
)) == PA_CONTEXT_FAILED
)
69 assert(cst
!= PA_CONTEXT_TERMINATED
);
72 if ((sst
= pa_stream_get_state(p
->stream
)) == PA_STREAM_FAILED
)
75 assert(sst
!= PA_STREAM_TERMINATED
);
82 *rerror
= pa_context_errno(p
->context
);
89 static int iterate(pa_simple
*p
, int block
, int *rerror
) {
90 assert(p
&& p
->context
&& p
->mainloop
);
92 if (check_error(p
, rerror
) < 0)
95 if (block
|| pa_context_is_pending(p
->context
)) {
97 if (pa_mainloop_iterate(p
->mainloop
, 1, NULL
) < 0) {
99 *rerror
= PA_ERR_INTERNAL
;
103 if (check_error(p
, rerror
) < 0)
105 } while (pa_context_is_pending(p
->context
));
111 if ((r
= pa_mainloop_iterate(p
->mainloop
, 0, NULL
)) < 0) {
113 *rerror
= PA_ERR_INTERNAL
;
120 if (check_error(p
, rerror
) < 0)
127 pa_simple
* pa_simple_new(
130 pa_stream_direction_t dir
,
132 const char *stream_name
,
133 const pa_sample_spec
*ss
,
134 const pa_buffer_attr
*attr
,
138 int error
= PA_ERR_INTERNAL
, r
;
140 CHECK_VALIDITY_RETURN_ANY(rerror
, !server
|| *server
, PA_ERR_INVALID
, NULL
);
141 CHECK_VALIDITY_RETURN_ANY(rerror
, dir
== PA_STREAM_PLAYBACK
|| dir
== PA_STREAM_RECORD
, PA_ERR_INVALID
, NULL
);
142 CHECK_VALIDITY_RETURN_ANY(rerror
, !dev
|| *dev
, PA_ERR_INVALID
, NULL
);
143 CHECK_VALIDITY_RETURN_ANY(rerror
, ss
&& pa_sample_spec_valid(ss
), PA_ERR_INVALID
, NULL
);
145 p
= pa_xnew(pa_simple
, 1);
148 p
->mainloop
= pa_mainloop_new();
153 p
->read_index
= p
->read_length
= 0;
156 if (!(p
->context
= pa_context_new(pa_mainloop_get_api(p
->mainloop
), name
)))
159 if (pa_context_connect(p
->context
, server
, 0, NULL
) < 0) {
160 error
= pa_context_errno(p
->context
);
164 /* Wait until the context is ready */
165 while (pa_context_get_state(p
->context
) != PA_CONTEXT_READY
) {
166 if (iterate(p
, 1, &error
) < 0)
170 if (!(p
->stream
= pa_stream_new(p
->context
, stream_name
, ss
, NULL
))) {
171 error
= pa_context_errno(p
->context
);
175 if (dir
== PA_STREAM_PLAYBACK
)
176 r
= pa_stream_connect_playback(p
->stream
, dev
, attr
, 0, NULL
, NULL
);
178 r
= pa_stream_connect_record(p
->stream
, dev
, attr
, 0);
181 error
= pa_context_errno(p
->context
);
185 /* Wait until the stream is ready */
186 while (pa_stream_get_state(p
->stream
) != PA_STREAM_READY
) {
187 if (iterate(p
, 1, &error
) < 0)
200 void pa_simple_free(pa_simple
*s
) {
204 pa_stream_unref(s
->stream
);
207 pa_context_unref(s
->context
);
210 pa_mainloop_free(s
->mainloop
);
215 int pa_simple_write(pa_simple
*p
, const void*data
, size_t length
, int *rerror
) {
219 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
, -1);
223 *rerror
= pa_context_errno(p
->context
);
231 while (!(l
= pa_stream_writable_size(p
->stream
)))
232 if (iterate(p
, 1, rerror
) < 0)
238 pa_stream_write(p
->stream
, data
, l
, NULL
, 0, PA_SEEK_RELATIVE
);
239 data
= (const uint8_t*) data
+ l
;
243 /* Make sure that no data is pending for write */
244 if (iterate(p
, 0, rerror
) < 0)
250 int pa_simple_read(pa_simple
*p
, void*data
, size_t length
, int *rerror
) {
254 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
, -1);
258 *rerror
= pa_context_errno(p
->context
);
266 if (pa_stream_peek(p
->stream
, &p
->read_data
, &p
->read_length
) >= 0)
272 if (p
->read_length
<= l
)
275 memcpy(data
, (const uint8_t*) p
->read_data
+p
->read_index
, l
);
277 data
= (uint8_t*) data
+ l
;
283 if (!p
->read_length
) {
284 pa_stream_drop(p
->stream
);
293 assert(!p
->read_data
);
296 if (iterate(p
, 1, rerror
) < 0)
303 static void drain_or_flush_complete(pa_stream
*s
, int success
, void *userdata
) {
304 pa_simple
*p
= userdata
;
313 int pa_simple_drain(pa_simple
*p
, int *rerror
) {
318 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
, -1);
322 *rerror
= pa_context_errno(p
->context
);
327 if (!(o
= pa_stream_drain(p
->stream
, drain_or_flush_complete
, p
))) {
329 *rerror
= pa_context_errno(p
->context
);
333 while (pa_operation_get_state(o
) == PA_OPERATION_RUNNING
) {
334 if (iterate(p
, 1, rerror
) < 0) {
335 pa_operation_cancel(o
);
336 pa_operation_unref(o
);
341 pa_operation_unref(o
);
343 if (p
->dead
&& rerror
)
344 *rerror
= pa_context_errno(p
->context
);
346 return p
->dead
? -1 : 0;
349 static void timing_complete(pa_stream
*s
, int success
, void *userdata
) {
350 pa_simple
*p
= userdata
;
359 if (pa_stream_get_latency(s
, &p
->latency
, &negative
) < 0)
366 pa_usec_t
pa_simple_get_playback_latency(pa_simple
*p
, int *rerror
) {
371 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
, -1);
375 *rerror
= pa_context_errno(p
->context
);
377 return (pa_usec_t
) -1;
381 if (!(o
= pa_stream_update_timing_info(p
->stream
, timing_complete
, p
))) {
383 *rerror
= pa_context_errno(p
->context
);
384 return (pa_usec_t
) -1;
387 while (pa_operation_get_state(o
) == PA_OPERATION_RUNNING
) {
389 if (iterate(p
, 1, rerror
) < 0) {
390 pa_operation_cancel(o
);
391 pa_operation_unref(o
);
396 pa_operation_unref(o
);
398 if (p
->dead
&& rerror
)
399 *rerror
= pa_context_errno(p
->context
);
401 return p
->dead
? (pa_usec_t
) -1 : p
->latency
;
404 int pa_simple_flush(pa_simple
*p
, int *rerror
) {
409 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
, -1);
413 *rerror
= pa_context_errno(p
->context
);
418 if (!(o
= pa_stream_flush(p
->stream
, drain_or_flush_complete
, p
))) {
420 *rerror
= pa_context_errno(p
->context
);
424 while (pa_operation_get_state(o
) == PA_OPERATION_RUNNING
) {
425 if (iterate(p
, 1, rerror
) < 0) {
426 pa_operation_cancel(o
);
427 pa_operation_unref(o
);
432 pa_operation_unref(o
);
434 if (p
->dead
&& rerror
)
435 *rerror
= pa_context_errno(p
->context
);
437 return p
->dead
? -1 : 0;