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
) {
54 if (pa_context_is_dead(p
->context
) || (p
->stream
&& pa_stream_is_dead(p
->stream
))) {
56 *perror
= pa_context_errno(p
->context
);
63 static int iterate(struct pa_simple
*p
, int block
, int *perror
) {
64 assert(p
&& p
->context
&& p
->mainloop
);
66 if (check_error(p
, perror
) < 0)
69 if (!block
&& !pa_context_is_pending(p
->context
))
73 if (pa_mainloop_iterate(p
->mainloop
, 1, NULL
) < 0) {
75 *perror
= PA_ERROR_INTERNAL
;
79 if (check_error(p
, perror
) < 0)
82 } while (pa_context_is_pending(p
->context
));
87 struct pa_simple
* pa_simple_new(
90 enum pa_stream_direction dir
,
92 const char *stream_name
,
93 const struct pa_sample_spec
*ss
,
94 const struct pa_buffer_attr
*attr
,
98 int error
= PA_ERROR_INTERNAL
;
101 p
= pa_xmalloc(sizeof(struct pa_simple
));
104 p
->mainloop
= pa_mainloop_new();
109 p
->read_index
= p
->read_length
= 0;
111 if (!(p
->context
= pa_context_new(pa_mainloop_get_api(p
->mainloop
), name
)))
114 if (pa_context_connect(p
->context
, server
, NULL
, NULL
) < 0) {
115 error
= pa_context_errno(p
->context
);
119 /* Wait until the context is ready */
120 while (!pa_context_is_ready(p
->context
)) {
121 if (iterate(p
, 1, &error
) < 0)
125 if (!(p
->stream
= pa_stream_new(p
->context
, dir
, dev
, stream_name
, ss
, attr
, NULL
, NULL
)))
128 /* Wait until the stream is ready */
129 while (!pa_stream_is_ready(p
->stream
)) {
130 if (iterate(p
, 1, &error
) < 0)
134 pa_stream_set_read_callback(p
->stream
, read_callback
, p
);
145 void pa_simple_free(struct pa_simple
*s
) {
148 pa_xfree(s
->read_data
);
151 pa_stream_free(s
->stream
);
154 pa_context_free(s
->context
);
157 pa_mainloop_free(s
->mainloop
);
162 int pa_simple_write(struct pa_simple
*p
, const void*data
, size_t length
, int *perror
) {
163 assert(p
&& data
&& p
->direction
== PA_STREAM_PLAYBACK
);
168 while (!(l
= pa_stream_writable_size(p
->stream
)))
169 if (iterate(p
, 1, perror
) < 0)
175 pa_stream_write(p
->stream
, data
, l
);
180 /* Make sure that no data is pending for write */
181 if (iterate(p
, 0, perror
) < 0)
187 static void read_callback(struct pa_stream
*s
, const void*data
, size_t length
, void *userdata
) {
188 struct pa_simple
*p
= userdata
;
189 assert(s
&& data
&& length
&& p
);
192 fprintf(stderr
, __FILE__
": Buffer overflow, dropping incoming memory blocks.\n");
193 pa_xfree(p
->read_data
);
196 p
->read_data
= pa_xmalloc(p
->read_length
= length
);
197 memcpy(p
->read_data
, data
, length
);
201 int pa_simple_read(struct pa_simple
*p
, void*data
, size_t length
, int *perror
) {
202 assert(p
&& data
&& p
->direction
== PA_STREAM_RECORD
);
208 if (p
->read_length
<= l
)
211 memcpy(data
, p
->read_data
+p
->read_index
, l
);
219 if (!p
->read_length
) {
220 pa_xfree(p
->read_data
);
228 assert(!p
->read_data
);
231 if (iterate(p
, 1, perror
) < 0)
238 static void drain_complete(struct pa_stream
*s
, void *userdata
) {
239 struct pa_simple
*p
= userdata
;
244 int pa_simple_drain(struct pa_simple
*p
, int *perror
) {
245 assert(p
&& p
->direction
== PA_STREAM_PLAYBACK
);
247 pa_stream_drain(p
->stream
, drain_complete
, p
);
249 while (!p
->drained
) {
250 if (iterate(p
, 1, perror
) < 0) {
251 pa_stream_drain(p
->stream
, NULL
, NULL
);