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"
35 /*#include "polyp-error.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 = malloc(sizeof(struct pa_simple));
105 p->mainloop = pa_mainloop_new();
110 p->read_index = p->read_length = 0;
112 if (!(p->context = pa_context_new(pa_mainloop_get_api(p->mainloop), name)))
115 if (pa_context_connect(p->context, server, NULL, NULL) < 0) {
116 error = pa_context_errno(p->context);
120 /* Wait until the context is ready */
121 while (!pa_context_is_ready(p->context)) {
122 if (iterate(p, 1, &error) < 0)
126 if (!(p->stream = pa_stream_new(p->context, dir, dev, stream_name, ss, attr, NULL, NULL)))
129 /* Wait until the stream is ready */
130 while (!pa_stream_is_ready(p->stream)) {
131 if (iterate(p, 1, &error) < 0)
135 pa_stream_set_read_callback(p->stream, read_callback, p);
146 void pa_simple_free(struct pa_simple *s) {
152 pa_stream_free(s->stream);
155 pa_context_free(s->context);
158 pa_mainloop_free(s->mainloop);
163 int pa_simple_write(struct pa_simple *p, const void*data, size_t length, int *perror) {
164 assert(p && data && p->direction == PA_STREAM_PLAYBACK);
169 while (!(l = pa_stream_writable_size(p->stream)))
170 if (iterate(p, 1, perror) < 0)
176 pa_stream_write(p->stream, data, l);
181 /* Make sure that no data is pending for write */
182 if (iterate(p, 0, perror) < 0)
188 static void read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) {
189 struct pa_simple *p = userdata;
190 assert(s && data && length && p);
193 fprintf(stderr, __FILE__": Buffer overflow, dropping incoming memory blocks.\n");
197 p->read_data = malloc(p->read_length = length);
198 assert(p->read_data);
199 memcpy(p->read_data, data, length);
203 int pa_simple_read(struct pa_simple *p, void*data, size_t length, int *perror) {
204 assert(p && data && p->direction == PA_STREAM_RECORD);
210 if (p->read_length <= l)
213 memcpy(data, p->read_data+p->read_index, l);
221 if (!p->read_length) {
230 assert(!p->read_data);
233 if (iterate(p, 1, perror) < 0)
240 static void drain_complete(struct pa_stream *s, void *userdata) {
241 struct pa_simple *p = userdata;
246 int pa_simple_drain(struct pa_simple *p, int *perror) {
247 assert(p && p->direction == PA_STREAM_PLAYBACK);
249 pa_stream_drain(p->stream, drain_complete, p);
251 while (!p->drained) {
252 if (iterate(p, 1, perror) < 0) {
253 pa_stream_drain(p->stream, NULL, NULL);