7 #include "polyp-error.h"
10 struct pa_mainloop *mainloop;
11 struct pa_context *context;
12 struct pa_stream *stream;
17 static int iterate(struct pa_simple *p, int block, int *perror) {
18 assert(p && p->context && p->mainloop && perror);
20 if (!block && !pa_context_is_pending(p->context))
24 if (pa_context_is_dead(p->context) || (p->stream && pa_stream_is_dead(p->stream))) {
25 *perror = pa_context_errno(p->context);
29 if (pa_mainloop_iterate(p->mainloop, 1, NULL) < 0) {
30 *perror = PA_ERROR_INTERNAL;
33 } while (pa_context_is_pending(p->context));
38 struct pa_simple* pa_simple_new(
41 enum pa_stream_direction dir,
43 const char *stream_name,
44 const struct pa_sample_spec *ss,
45 const struct pa_buffer_attr *attr,
49 int error = PA_ERROR_INTERNAL;
52 p = malloc(sizeof(struct pa_simple));
56 p->mainloop = pa_mainloop_new();
60 if (!(p->context = pa_context_new(pa_mainloop_get_api(p->mainloop), name)))
63 if (pa_context_connect(p->context, server, NULL, NULL) < 0) {
64 error = pa_context_errno(p->context);
68 /* Wait until the context is ready */
69 while (!pa_context_is_ready(p->context)) {
70 if (iterate(p, 1, &error) < 0)
74 if (!(p->stream = pa_stream_new(p->context, dir, dev, stream_name, ss, attr, NULL, NULL)))
77 /* Wait until the stream is ready */
78 while (!pa_stream_is_ready(p->stream)) {
79 if (iterate(p, 1, &error) < 0)
91 void pa_simple_free(struct pa_simple *s) {
95 pa_stream_free(s->stream);
98 pa_context_free(s->context);
101 pa_mainloop_free(s->mainloop);
106 int pa_simple_write(struct pa_simple *p, const void*data, size_t length, int *perror) {
112 while (!(l = pa_stream_writable_size(p->stream)))
113 if (iterate(p, 1, perror) < 0)
119 pa_stream_write(p->stream, data, l);
124 /* Make sure that no data is pending for write */
125 if (iterate(p, 0, perror) < 0)
131 int pa_simple_read(struct pa_simple *s, void*data, size_t length, int *perror) {