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"
38 struct pa_mainloop
*mainloop
;
39 struct pa_context
*context
;
40 struct pa_stream
*stream
;
41 enum pa_stream_direction direction
;
46 size_t read_index
, read_length
;
49 static void read_callback(struct pa_stream
*s
, const void*data
, size_t length
, void *userdata
);
51 static int check_error(struct pa_simple
*p
, int *perror
) {
52 enum pa_context_state cst
;
53 enum pa_stream_state sst
;
56 if ((cst
= pa_context_get_state(p
->context
)) == PA_CONTEXT_FAILED
)
59 assert(cst
!= PA_CONTEXT_TERMINATED
);
62 if ((sst
= pa_stream_get_state(p
->stream
)) == PA_STREAM_FAILED
)
65 assert(sst
!= PA_STREAM_TERMINATED
);
72 *perror
= pa_context_errno(p
->context
);
76 static int iterate(struct pa_simple
*p
, int block
, int *perror
) {
77 assert(p
&& p
->context
&& p
->mainloop
);
79 if (check_error(p
, perror
) < 0)
82 if (!block
&& !pa_context_is_pending(p
->context
))
86 if (pa_mainloop_iterate(p
->mainloop
, 1, NULL
) < 0) {
88 *perror
= PA_ERROR_INTERNAL
;
92 if (check_error(p
, perror
) < 0)
95 } while (pa_context_is_pending(p
->context
));
100 struct pa_simple
* pa_simple_new(
103 enum pa_stream_direction dir
,
105 const char *stream_name
,
106 const struct pa_sample_spec
*ss
,
107 const struct pa_buffer_attr
*attr
,
111 int error
= PA_ERROR_INTERNAL
;
112 assert(ss
&& (dir
== PA_STREAM_PLAYBACK
|| dir
== PA_STREAM_RECORD
));
114 p
= pa_xmalloc(sizeof(struct pa_simple
));
117 p
->mainloop
= pa_mainloop_new();
122 p
->read_index
= p
->read_length
= 0;
124 if (!(p
->context
= pa_context_new(pa_mainloop_get_api(p
->mainloop
), name
)))
127 pa_context_connect(p
->context
, server
);
129 /* Wait until the context is ready */
130 while (pa_context_get_state(p
->context
) != PA_CONTEXT_READY
) {
131 if (iterate(p
, 1, &error
) < 0)
135 if (!(p
->stream
= pa_stream_new(p
->context
, stream_name
, ss
)))
138 if (dir
== PA_STREAM_PLAYBACK
)
139 pa_stream_connect_playback(p
->stream
, dev
, attr
);
141 pa_stream_connect_record(p
->stream
, dev
, attr
);
143 /* Wait until the stream is ready */
144 while (pa_stream_get_state(p
->stream
) != PA_STREAM_READY
) {
145 if (iterate(p
, 1, &error
) < 0)
149 pa_stream_set_read_callback(p
->stream
, read_callback
, p
);
160 void pa_simple_free(struct pa_simple
*s
) {
163 pa_xfree(s
->read_data
);
166 pa_stream_unref(s
->stream
);
169 pa_context_unref(s
->context
);
172 pa_mainloop_free(s
->mainloop
);
177 int pa_simple_write(struct pa_simple
*p
, const void*data
, size_t length
, int *perror
) {
178 assert(p
&& data
&& p
->direction
== PA_STREAM_PLAYBACK
);
183 while (!(l
= pa_stream_writable_size(p
->stream
)))
184 if (iterate(p
, 1, perror
) < 0)
190 pa_stream_write(p
->stream
, data
, l
, NULL
, 0);
191 data
= (uint8_t*) data
+ l
;
195 /* Make sure that no data is pending for write */
196 if (iterate(p
, 0, perror
) < 0)
202 static void read_callback(struct pa_stream
*s
, const void*data
, size_t length
, void *userdata
) {
203 struct pa_simple
*p
= userdata
;
204 assert(s
&& data
&& length
&& p
);
207 fprintf(stderr
, __FILE__
": Buffer overflow, dropping incoming memory blocks.\n");
208 pa_xfree(p
->read_data
);
211 p
->read_data
= pa_xmemdup(data
, p
->read_length
= length
);
215 int pa_simple_read(struct pa_simple
*p
, void*data
, size_t length
, int *perror
) {
216 assert(p
&& data
&& p
->direction
== PA_STREAM_RECORD
);
222 if (p
->read_length
<= l
)
225 memcpy(data
, (uint8_t*) p
->read_data
+p
->read_index
, l
);
227 data
= (uint8_t*) data
+ l
;
233 if (!p
->read_length
) {
234 pa_xfree(p
->read_data
);
242 assert(!p
->read_data
);
245 if (iterate(p
, 1, perror
) < 0)
252 static void drain_complete(struct pa_stream
*s
, int success
, void *userdata
) {
253 struct pa_simple
*p
= userdata
;
255 p
->drained
= success
? 1 : -1;
258 int pa_simple_drain(struct pa_simple
*p
, int *perror
) {
259 struct pa_operation
*o
;
261 assert(p
&& p
->direction
== PA_STREAM_PLAYBACK
);
263 o
= pa_stream_drain(p
->stream
, drain_complete
, p
);
265 while (!p
->drained
) {
266 if (iterate(p
, 1, perror
) < 0) {
267 pa_operation_cancel(o
);
268 pa_operation_unref(o
);
273 pa_operation_unref(o
);
275 if (p
->drained
< 0 && perror
)
276 *perror
= pa_context_errno(p
->context
);