9 #include "polyp-error.h"
12 struct pa_mainloop *mainloop;
13 struct pa_context *context;
14 struct pa_stream *stream;
15 enum pa_stream_direction direction;
20 size_t read_index, read_length;
23 static void read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata);
25 static int check_error(struct pa_simple *p, int *perror) {
28 if (pa_context_is_dead(p->context) || (p->stream && pa_stream_is_dead(p->stream))) {
30 *perror = pa_context_errno(p->context);
37 static int iterate(struct pa_simple *p, int block, int *perror) {
38 assert(p && p->context && p->mainloop);
40 if (check_error(p, perror) < 0)
43 if (!block && !pa_context_is_pending(p->context))
47 if (pa_mainloop_iterate(p->mainloop, 1, NULL) < 0) {
49 *perror = PA_ERROR_INTERNAL;
53 if (check_error(p, perror) < 0)
56 } while (pa_context_is_pending(p->context));
61 struct pa_simple* pa_simple_new(
64 enum pa_stream_direction dir,
66 const char *stream_name,
67 const struct pa_sample_spec *ss,
68 const struct pa_buffer_attr *attr,
72 int error = PA_ERROR_INTERNAL;
75 p = malloc(sizeof(struct pa_simple));
79 p->mainloop = pa_mainloop_new();
84 p->read_index = p->read_length = 0;
86 if (!(p->context = pa_context_new(pa_mainloop_get_api(p->mainloop), name)))
89 if (pa_context_connect(p->context, server, NULL, NULL) < 0) {
90 error = pa_context_errno(p->context);
94 /* Wait until the context is ready */
95 while (!pa_context_is_ready(p->context)) {
96 if (iterate(p, 1, &error) < 0)
100 if (!(p->stream = pa_stream_new(p->context, dir, dev, stream_name, ss, attr, NULL, NULL)))
103 /* Wait until the stream is ready */
104 while (!pa_stream_is_ready(p->stream)) {
105 if (iterate(p, 1, &error) < 0)
109 pa_stream_set_read_callback(p->stream, read_callback, p);
120 void pa_simple_free(struct pa_simple *s) {
126 pa_stream_free(s->stream);
129 pa_context_free(s->context);
132 pa_mainloop_free(s->mainloop);
137 int pa_simple_write(struct pa_simple *p, const void*data, size_t length, int *perror) {
138 assert(p && data && p->direction == PA_STREAM_PLAYBACK);
143 while (!(l = pa_stream_writable_size(p->stream)))
144 if (iterate(p, 1, perror) < 0)
150 pa_stream_write(p->stream, data, l);
155 /* Make sure that no data is pending for write */
156 if (iterate(p, 0, perror) < 0)
162 static void read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) {
163 struct pa_simple *p = userdata;
164 assert(s && data && length && p);
167 fprintf(stderr, __FILE__": Buffer overflow, dropping incoming memory blocks.\n");
171 p->read_data = malloc(p->read_length = length);
172 assert(p->read_data);
173 memcpy(p->read_data, data, length);
177 int pa_simple_read(struct pa_simple *p, void*data, size_t length, int *perror) {
178 assert(p && data && p->direction == PA_STREAM_RECORD);
184 if (p->read_length <= l)
187 memcpy(data, p->read_data+p->read_index, l);
195 if (!p->read_length) {
204 assert(!p->read_data);
207 if (iterate(p, 1, perror) < 0)
214 static void drain_complete(struct pa_stream *s, void *userdata) {
215 struct pa_simple *p = userdata;
220 int pa_simple_drain(struct pa_simple *p, int *perror) {
221 assert(p && p->direction == PA_STREAM_PLAYBACK);
223 pa_stream_drain(p->stream, drain_complete, p);
225 while (!p->drained) {
226 if (iterate(p, 1, perror) < 0) {
227 pa_stream_drain(p->stream, NULL, NULL);