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>
41 pa_mainloop
*mainloop
;
44 pa_stream_direction_t direction
;
48 const void *read_data
;
49 size_t read_index
, read_length
;
53 static int check_error(pa_simple
*p
, int *rerror
) {
54 pa_context_state_t cst
;
55 pa_stream_state_t sst
;
58 if ((cst
= pa_context_get_state(p
->context
)) == PA_CONTEXT_FAILED
)
61 assert(cst
!= PA_CONTEXT_TERMINATED
);
64 if ((sst
= pa_stream_get_state(p
->stream
)) == PA_STREAM_FAILED
)
67 assert(sst
!= PA_STREAM_TERMINATED
);
74 *rerror
= pa_context_errno(p
->context
);
81 static int iterate(pa_simple
*p
, int block
, int *rerror
) {
82 assert(p
&& p
->context
&& p
->mainloop
);
84 if (check_error(p
, rerror
) < 0)
87 if (!block
&& !pa_context_is_pending(p
->context
))
91 if (pa_mainloop_iterate(p
->mainloop
, 1, NULL
) < 0) {
93 *rerror
= PA_ERR_INTERNAL
;
97 if (check_error(p
, rerror
) < 0)
100 } while (pa_context_is_pending(p
->context
));
103 while (pa_mainloop_deferred_pending(p
->mainloop
)) {
105 if (pa_mainloop_iterate(p
->mainloop
, 0, NULL
) < 0) {
107 *rerror
= PA_ERR_INTERNAL
;
111 if (check_error(p
, rerror
) < 0)
118 pa_simple
* pa_simple_new(
121 pa_stream_direction_t dir
,
123 const char *stream_name
,
124 const pa_sample_spec
*ss
,
125 const pa_buffer_attr
*attr
,
129 int error
= PA_ERR_INTERNAL
;
130 assert(ss
&& (dir
== PA_STREAM_PLAYBACK
|| dir
== PA_STREAM_RECORD
));
132 p
= pa_xmalloc(sizeof(pa_simple
));
135 p
->mainloop
= pa_mainloop_new();
140 p
->read_index
= p
->read_length
= 0;
143 if (!(p
->context
= pa_context_new(pa_mainloop_get_api(p
->mainloop
), name
)))
146 pa_context_connect(p
->context
, server
, 1, NULL
);
148 /* Wait until the context is ready */
149 while (pa_context_get_state(p
->context
) != PA_CONTEXT_READY
) {
150 if (iterate(p
, 1, &error
) < 0)
154 if (!(p
->stream
= pa_stream_new(p
->context
, stream_name
, ss
, NULL
)))
157 if (dir
== PA_STREAM_PLAYBACK
)
158 pa_stream_connect_playback(p
->stream
, dev
, attr
, 0, NULL
, NULL
);
160 pa_stream_connect_record(p
->stream
, dev
, attr
, 0);
162 /* Wait until the stream is ready */
163 while (pa_stream_get_state(p
->stream
) != PA_STREAM_READY
) {
164 if (iterate(p
, 1, &error
) < 0)
177 void pa_simple_free(pa_simple
*s
) {
181 pa_stream_unref(s
->stream
);
184 pa_context_unref(s
->context
);
187 pa_mainloop_free(s
->mainloop
);
192 int pa_simple_write(pa_simple
*p
, const void*data
, size_t length
, int *rerror
) {
193 assert(p
&& data
&& p
->direction
== PA_STREAM_PLAYBACK
);
197 *rerror
= pa_context_errno(p
->context
);
205 while (!(l
= pa_stream_writable_size(p
->stream
)))
206 if (iterate(p
, 1, rerror
) < 0)
212 pa_stream_write(p
->stream
, data
, l
, NULL
, 0, PA_SEEK_RELATIVE
);
213 data
= (const uint8_t*) data
+ l
;
217 /* Make sure that no data is pending for write */
218 if (iterate(p
, 0, rerror
) < 0)
224 int pa_simple_read(pa_simple
*p
, void*data
, size_t length
, int *rerror
) {
225 assert(p
&& data
&& p
->direction
== PA_STREAM_RECORD
);
229 *rerror
= pa_context_errno(p
->context
);
237 if (pa_stream_peek(p
->stream
, &p
->read_data
, &p
->read_length
) >= 0)
243 if (p
->read_length
<= l
)
246 memcpy(data
, (const uint8_t*) p
->read_data
+p
->read_index
, l
);
248 data
= (uint8_t*) data
+ l
;
254 if (!p
->read_length
) {
255 pa_stream_drop(p
->stream
);
264 assert(!p
->read_data
);
267 if (iterate(p
, 1, rerror
) < 0)
274 static void drain_or_flush_complete(pa_stream
*s
, int success
, void *userdata
) {
275 pa_simple
*p
= userdata
;
281 int pa_simple_drain(pa_simple
*p
, int *rerror
) {
283 assert(p
&& p
->direction
== PA_STREAM_PLAYBACK
);
287 *rerror
= pa_context_errno(p
->context
);
292 o
= pa_stream_drain(p
->stream
, drain_or_flush_complete
, p
);
294 while (pa_operation_get_state(o
) == PA_OPERATION_RUNNING
) {
295 if (iterate(p
, 1, rerror
) < 0) {
296 pa_operation_cancel(o
);
297 pa_operation_unref(o
);
302 pa_operation_unref(o
);
304 if (p
->dead
&& rerror
)
305 *rerror
= pa_context_errno(p
->context
);
307 return p
->dead
? -1 : 0;
310 static void latency_complete(pa_stream
*s
, const pa_latency_info
*l
, void *userdata
) {
311 pa_simple
*p
= userdata
;
318 p
->latency
= pa_stream_get_latency(s
, l
, &negative
);
324 pa_usec_t
pa_simple_get_playback_latency(pa_simple
*p
, int *rerror
) {
326 assert(p
&& p
->direction
== PA_STREAM_PLAYBACK
);
330 *rerror
= pa_context_errno(p
->context
);
332 return (pa_usec_t
) -1;
336 o
= pa_stream_get_latency_info(p
->stream
, latency_complete
, p
);
338 while (pa_operation_get_state(o
) == PA_OPERATION_RUNNING
) {
340 if (iterate(p
, 1, rerror
) < 0) {
341 pa_operation_cancel(o
);
342 pa_operation_unref(o
);
347 pa_operation_unref(o
);
349 if (p
->dead
&& rerror
)
350 *rerror
= pa_context_errno(p
->context
);
352 return p
->dead
? (pa_usec_t
) -1 : p
->latency
;
355 int pa_simple_flush(pa_simple
*p
, int *rerror
) {
357 assert(p
&& p
->direction
== PA_STREAM_PLAYBACK
);
361 *rerror
= pa_context_errno(p
->context
);
366 o
= pa_stream_flush(p
->stream
, drain_or_flush_complete
, p
);
368 while (pa_operation_get_state(o
) == PA_OPERATION_RUNNING
) {
369 if (iterate(p
, 1, rerror
) < 0) {
370 pa_operation_cancel(o
);
371 pa_operation_unref(o
);
376 pa_operation_unref(o
);
378 if (p
->dead
&& rerror
)
379 *rerror
= pa_context_errno(p
->context
);
381 return p
->dead
? -1 : 0;