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
))
99 if (pa_mainloop_iterate(p
->mainloop
, 1, NULL
) < 0) {
101 *rerror
= PA_ERR_INTERNAL
;
105 if (check_error(p
, rerror
) < 0)
108 } while (pa_context_is_pending(p
->context
));
111 while (pa_mainloop_deferred_pending(p
->mainloop
)) {
113 if (pa_mainloop_iterate(p
->mainloop
, 0, NULL
) < 0) {
115 *rerror
= PA_ERR_INTERNAL
;
119 if (check_error(p
, rerror
) < 0)
126 pa_simple
* pa_simple_new(
129 pa_stream_direction_t dir
,
131 const char *stream_name
,
132 const pa_sample_spec
*ss
,
133 const pa_buffer_attr
*attr
,
137 int error
= PA_ERR_INTERNAL
, r
;
139 CHECK_VALIDITY_RETURN_ANY(rerror
, !server
|| *server
, PA_ERR_INVALID
, NULL
);
140 CHECK_VALIDITY_RETURN_ANY(rerror
, dir
== PA_STREAM_PLAYBACK
|| dir
== PA_STREAM_RECORD
, PA_ERR_INVALID
, NULL
);
141 CHECK_VALIDITY_RETURN_ANY(rerror
, !dev
|| *dev
, PA_ERR_INVALID
, NULL
);
142 CHECK_VALIDITY_RETURN_ANY(rerror
, ss
&& pa_sample_spec_valid(ss
), PA_ERR_INVALID
, NULL
);
144 p
= pa_xnew(pa_simple
, 1);
147 p
->mainloop
= pa_mainloop_new();
152 p
->read_index
= p
->read_length
= 0;
155 if (!(p
->context
= pa_context_new(pa_mainloop_get_api(p
->mainloop
), name
)))
158 if (pa_context_connect(p
->context
, server
, 0, NULL
) < 0) {
159 error
= pa_context_errno(p
->context
);
163 /* Wait until the context is ready */
164 while (pa_context_get_state(p
->context
) != PA_CONTEXT_READY
) {
165 if (iterate(p
, 1, &error
) < 0)
169 if (!(p
->stream
= pa_stream_new(p
->context
, stream_name
, ss
, NULL
))) {
170 error
= pa_context_errno(p
->context
);
174 if (dir
== PA_STREAM_PLAYBACK
)
175 r
= pa_stream_connect_playback(p
->stream
, dev
, attr
, 0, NULL
, NULL
);
177 r
= pa_stream_connect_record(p
->stream
, dev
, attr
, 0);
180 error
= pa_context_errno(p
->context
);
184 /* Wait until the stream is ready */
185 while (pa_stream_get_state(p
->stream
) != PA_STREAM_READY
) {
186 if (iterate(p
, 1, &error
) < 0)
199 void pa_simple_free(pa_simple
*s
) {
203 pa_stream_unref(s
->stream
);
206 pa_context_unref(s
->context
);
209 pa_mainloop_free(s
->mainloop
);
214 int pa_simple_write(pa_simple
*p
, const void*data
, size_t length
, int *rerror
) {
218 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
, -1);
222 *rerror
= pa_context_errno(p
->context
);
230 while (!(l
= pa_stream_writable_size(p
->stream
)))
231 if (iterate(p
, 1, rerror
) < 0)
237 pa_stream_write(p
->stream
, data
, l
, NULL
, 0, PA_SEEK_RELATIVE
);
238 data
= (const uint8_t*) data
+ l
;
242 /* Make sure that no data is pending for write */
243 if (iterate(p
, 0, rerror
) < 0)
249 int pa_simple_read(pa_simple
*p
, void*data
, size_t length
, int *rerror
) {
253 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
, -1);
257 *rerror
= pa_context_errno(p
->context
);
265 if (pa_stream_peek(p
->stream
, &p
->read_data
, &p
->read_length
) >= 0)
271 if (p
->read_length
<= l
)
274 memcpy(data
, (const uint8_t*) p
->read_data
+p
->read_index
, l
);
276 data
= (uint8_t*) data
+ l
;
282 if (!p
->read_length
) {
283 pa_stream_drop(p
->stream
);
292 assert(!p
->read_data
);
295 if (iterate(p
, 1, rerror
) < 0)
302 static void drain_or_flush_complete(pa_stream
*s
, int success
, void *userdata
) {
303 pa_simple
*p
= userdata
;
312 int pa_simple_drain(pa_simple
*p
, int *rerror
) {
317 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
, -1);
321 *rerror
= pa_context_errno(p
->context
);
326 if (!(o
= pa_stream_drain(p
->stream
, drain_or_flush_complete
, p
))) {
328 *rerror
= pa_context_errno(p
->context
);
332 while (pa_operation_get_state(o
) == PA_OPERATION_RUNNING
) {
333 if (iterate(p
, 1, rerror
) < 0) {
334 pa_operation_cancel(o
);
335 pa_operation_unref(o
);
340 pa_operation_unref(o
);
342 if (p
->dead
&& rerror
)
343 *rerror
= pa_context_errno(p
->context
);
345 return p
->dead
? -1 : 0;
348 static void timing_complete(pa_stream
*s
, int success
, void *userdata
) {
349 pa_simple
*p
= userdata
;
358 if (pa_stream_get_latency(s
, &p
->latency
, &negative
) < 0)
365 pa_usec_t
pa_simple_get_playback_latency(pa_simple
*p
, int *rerror
) {
370 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
, -1);
374 *rerror
= pa_context_errno(p
->context
);
376 return (pa_usec_t
) -1;
380 if (!(o
= pa_stream_update_timing_info(p
->stream
, timing_complete
, p
))) {
382 *rerror
= pa_context_errno(p
->context
);
383 return (pa_usec_t
) -1;
386 while (pa_operation_get_state(o
) == PA_OPERATION_RUNNING
) {
388 if (iterate(p
, 1, rerror
) < 0) {
389 pa_operation_cancel(o
);
390 pa_operation_unref(o
);
395 pa_operation_unref(o
);
397 if (p
->dead
&& rerror
)
398 *rerror
= pa_context_errno(p
->context
);
400 return p
->dead
? (pa_usec_t
) -1 : p
->latency
;
403 int pa_simple_flush(pa_simple
*p
, int *rerror
) {
408 CHECK_VALIDITY_RETURN_ANY(rerror
, p
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
, -1);
412 *rerror
= pa_context_errno(p
->context
);
417 if (!(o
= pa_stream_flush(p
->stream
, drain_or_flush_complete
, p
))) {
419 *rerror
= pa_context_errno(p
->context
);
423 while (pa_operation_get_state(o
) == PA_OPERATION_RUNNING
) {
424 if (iterate(p
, 1, rerror
) < 0) {
425 pa_operation_cancel(o
);
426 pa_operation_unref(o
);
431 pa_operation_unref(o
);
433 if (p
->dead
&& rerror
)
434 *rerror
= pa_context_errno(p
->context
);
436 return p
->dead
? -1 : 0;