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 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 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 "polyplib-simple.h"
34 #include "native-common.h"
39 struct pa_mainloop
*mainloop
;
40 struct pa_context
*context
;
41 struct pa_stream
*stream
;
42 enum pa_stream_direction direction
;
47 size_t read_index
, read_length
;
51 static void read_callback(struct pa_stream
*s
, const void*data
, size_t length
, void *userdata
);
53 static int check_error(struct pa_simple
*p
, int *perror
) {
54 enum pa_context_state cst
;
55 enum pa_stream_state 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 *perror
= pa_context_errno(p
->context
);
81 static int iterate(struct pa_simple
*p
, int block
, int *perror
) {
82 assert(p
&& p
->context
&& p
->mainloop
);
84 if (check_error(p
, perror
) < 0)
87 if (!block
&& !pa_context_is_pending(p
->context
))
91 if (pa_mainloop_iterate(p
->mainloop
, 1, NULL
) < 0) {
93 *perror
= PA_ERROR_INTERNAL
;
97 if (check_error(p
, perror
) < 0)
100 } while (pa_context_is_pending(p
->context
));
105 struct pa_simple
* pa_simple_new(
108 enum pa_stream_direction dir
,
110 const char *stream_name
,
111 const struct pa_sample_spec
*ss
,
112 const struct pa_buffer_attr
*attr
,
116 int error
= PA_ERROR_INTERNAL
;
117 assert(ss
&& (dir
== PA_STREAM_PLAYBACK
|| dir
== PA_STREAM_RECORD
));
119 p
= pa_xmalloc(sizeof(struct pa_simple
));
122 p
->mainloop
= pa_mainloop_new();
127 p
->read_index
= p
->read_length
= 0;
130 if (!(p
->context
= pa_context_new(pa_mainloop_get_api(p
->mainloop
), name
)))
133 pa_context_connect(p
->context
, server
);
135 /* Wait until the context is ready */
136 while (pa_context_get_state(p
->context
) != PA_CONTEXT_READY
) {
137 if (iterate(p
, 1, &error
) < 0)
141 if (!(p
->stream
= pa_stream_new(p
->context
, stream_name
, ss
)))
144 if (dir
== PA_STREAM_PLAYBACK
)
145 pa_stream_connect_playback(p
->stream
, dev
, attr
);
147 pa_stream_connect_record(p
->stream
, dev
, attr
);
149 /* Wait until the stream is ready */
150 while (pa_stream_get_state(p
->stream
) != PA_STREAM_READY
) {
151 if (iterate(p
, 1, &error
) < 0)
155 pa_stream_set_read_callback(p
->stream
, read_callback
, p
);
166 void pa_simple_free(struct pa_simple
*s
) {
169 pa_xfree(s
->read_data
);
172 pa_stream_unref(s
->stream
);
175 pa_context_unref(s
->context
);
178 pa_mainloop_free(s
->mainloop
);
183 int pa_simple_write(struct pa_simple
*p
, const void*data
, size_t length
, int *perror
) {
184 assert(p
&& data
&& p
->direction
== PA_STREAM_PLAYBACK
);
188 *perror
= pa_context_errno(p
->context
);
196 while (!(l
= pa_stream_writable_size(p
->stream
)))
197 if (iterate(p
, 1, perror
) < 0)
203 pa_stream_write(p
->stream
, data
, l
, NULL
, 0);
204 data
= (uint8_t*) data
+ l
;
208 /* Make sure that no data is pending for write */
209 if (iterate(p
, 0, perror
) < 0)
215 static void read_callback(struct pa_stream
*s
, const void*data
, size_t length
, void *userdata
) {
216 struct pa_simple
*p
= userdata
;
217 assert(s
&& data
&& length
&& p
);
220 pa_log(__FILE__
": Buffer overflow, dropping incoming memory blocks.\n");
221 pa_xfree(p
->read_data
);
224 p
->read_data
= pa_xmemdup(data
, p
->read_length
= length
);
228 int pa_simple_read(struct pa_simple
*p
, void*data
, size_t length
, int *perror
) {
229 assert(p
&& data
&& p
->direction
== PA_STREAM_RECORD
);
233 *perror
= pa_context_errno(p
->context
);
242 if (p
->read_length
<= l
)
245 memcpy(data
, (uint8_t*) p
->read_data
+p
->read_index
, l
);
247 data
= (uint8_t*) data
+ l
;
253 if (!p
->read_length
) {
254 pa_xfree(p
->read_data
);
262 assert(!p
->read_data
);
265 if (iterate(p
, 1, perror
) < 0)
272 static void drain_or_flush_complete(struct pa_stream
*s
, int success
, void *userdata
) {
273 struct pa_simple
*p
= userdata
;
279 int pa_simple_drain(struct pa_simple
*p
, int *perror
) {
280 struct pa_operation
*o
;
281 assert(p
&& p
->direction
== PA_STREAM_PLAYBACK
);
285 *perror
= pa_context_errno(p
->context
);
290 o
= pa_stream_drain(p
->stream
, drain_or_flush_complete
, p
);
292 while (pa_operation_get_state(o
) == PA_OPERATION_RUNNING
) {
293 if (iterate(p
, 1, perror
) < 0) {
294 pa_operation_cancel(o
);
295 pa_operation_unref(o
);
300 pa_operation_unref(o
);
302 if (p
->dead
&& perror
)
303 *perror
= pa_context_errno(p
->context
);
305 return p
->dead
? -1 : 0;
308 static void latency_complete(struct pa_stream
*s
, const struct pa_latency_info
*l
, void *userdata
) {
309 struct pa_simple
*p
= userdata
;
315 p
->latency
= l
->buffer_usec
+ l
->sink_usec
+ l
->transport_usec
;
318 pa_usec_t
pa_simple_get_playback_latency(struct pa_simple
*p
, int *perror
) {
319 struct pa_operation
*o
;
320 assert(p
&& p
->direction
== PA_STREAM_PLAYBACK
);
324 *perror
= pa_context_errno(p
->context
);
326 return (pa_usec_t
) -1;
330 o
= pa_stream_get_latency(p
->stream
, latency_complete
, p
);
332 while (pa_operation_get_state(o
) == PA_OPERATION_RUNNING
) {
334 if (iterate(p
, 1, perror
) < 0) {
335 pa_operation_cancel(o
);
336 pa_operation_unref(o
);
341 pa_operation_unref(o
);
343 if (p
->dead
&& perror
)
344 *perror
= pa_context_errno(p
->context
);
346 return p
->dead
? (pa_usec_t
) -1 : p
->latency
;
349 int pa_simple_flush(struct pa_simple
*p
, int *perror
) {
350 struct pa_operation
*o
;
351 assert(p
&& p
->direction
== PA_STREAM_PLAYBACK
);
355 *perror
= pa_context_errno(p
->context
);
360 o
= pa_stream_flush(p
->stream
, drain_or_flush_complete
, p
);
362 while (pa_operation_get_state(o
) == PA_OPERATION_RUNNING
) {
363 if (iterate(p
, 1, perror
) < 0) {
364 pa_operation_cancel(o
);
365 pa_operation_unref(o
);
370 pa_operation_unref(o
);
372 if (p
->dead
&& perror
)
373 *perror
= pa_context_errno(p
->context
);
375 return p
->dead
? -1 : 0;