2004-07-06 00:08:44 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
#include "simple.h"
|
|
|
|
|
#include "polyp.h"
|
|
|
|
|
#include "mainloop.h"
|
2004-07-06 00:08:44 +00:00
|
|
|
#include "polyp-error.h"
|
2004-06-23 23:17:30 +00:00
|
|
|
|
|
|
|
|
struct pa_simple {
|
2004-07-06 00:08:44 +00:00
|
|
|
struct pa_mainloop *mainloop;
|
2004-06-23 23:17:30 +00:00
|
|
|
struct pa_context *context;
|
|
|
|
|
struct pa_stream *stream;
|
|
|
|
|
|
2004-07-07 22:02:15 +00:00
|
|
|
int dead, drained;
|
2004-06-23 23:17:30 +00:00
|
|
|
};
|
|
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
static int iterate(struct pa_simple *p, int block, int *perror) {
|
2004-07-07 22:02:15 +00:00
|
|
|
assert(p && p->context && p->mainloop);
|
2004-07-07 00:22:46 +00:00
|
|
|
|
|
|
|
|
if (!block && !pa_context_is_pending(p->context))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
if (pa_context_is_dead(p->context) || (p->stream && pa_stream_is_dead(p->stream))) {
|
2004-07-07 22:02:15 +00:00
|
|
|
if (perror)
|
|
|
|
|
*perror = pa_context_errno(p->context);
|
2004-07-07 00:22:46 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pa_mainloop_iterate(p->mainloop, 1, NULL) < 0) {
|
2004-07-07 22:02:15 +00:00
|
|
|
if (perror)
|
|
|
|
|
*perror = PA_ERROR_INTERNAL;
|
2004-07-07 00:22:46 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
} while (pa_context_is_pending(p->context));
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
struct pa_simple* pa_simple_new(
|
|
|
|
|
const char *server,
|
|
|
|
|
const char *name,
|
|
|
|
|
enum pa_stream_direction dir,
|
|
|
|
|
const char *dev,
|
|
|
|
|
const char *stream_name,
|
|
|
|
|
const struct pa_sample_spec *ss,
|
2004-07-06 00:08:44 +00:00
|
|
|
const struct pa_buffer_attr *attr,
|
|
|
|
|
int *perror) {
|
2004-06-23 23:17:30 +00:00
|
|
|
|
|
|
|
|
struct pa_simple *p;
|
2004-07-06 00:08:44 +00:00
|
|
|
int error = PA_ERROR_INTERNAL;
|
2004-06-23 23:17:30 +00:00
|
|
|
assert(ss);
|
|
|
|
|
|
|
|
|
|
p = malloc(sizeof(struct pa_simple));
|
|
|
|
|
assert(p);
|
|
|
|
|
p->context = NULL;
|
|
|
|
|
p->stream = NULL;
|
|
|
|
|
p->mainloop = pa_mainloop_new();
|
|
|
|
|
assert(p->mainloop);
|
|
|
|
|
p->dead = 0;
|
|
|
|
|
|
|
|
|
|
if (!(p->context = pa_context_new(pa_mainloop_get_api(p->mainloop), name)))
|
|
|
|
|
goto fail;
|
|
|
|
|
|
2004-07-06 00:08:44 +00:00
|
|
|
if (pa_context_connect(p->context, server, NULL, NULL) < 0) {
|
|
|
|
|
error = pa_context_errno(p->context);
|
2004-06-23 23:17:30 +00:00
|
|
|
goto fail;
|
2004-07-06 00:08:44 +00:00
|
|
|
}
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
/* Wait until the context is ready */
|
2004-07-06 00:08:44 +00:00
|
|
|
while (!pa_context_is_ready(p->context)) {
|
2004-07-07 00:22:46 +00:00
|
|
|
if (iterate(p, 1, &error) < 0)
|
2004-06-23 23:17:30 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-06 00:08:44 +00:00
|
|
|
if (!(p->stream = pa_stream_new(p->context, dir, dev, stream_name, ss, attr, NULL, NULL)))
|
2004-06-23 23:17:30 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
/* Wait until the stream is ready */
|
2004-07-06 00:08:44 +00:00
|
|
|
while (!pa_stream_is_ready(p->stream)) {
|
2004-07-07 00:22:46 +00:00
|
|
|
if (iterate(p, 1, &error) < 0)
|
2004-06-23 23:17:30 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
|
|
|
|
|
fail:
|
2004-07-07 22:02:15 +00:00
|
|
|
if (perror)
|
|
|
|
|
*perror = error;
|
2004-06-23 23:17:30 +00:00
|
|
|
pa_simple_free(p);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_simple_free(struct pa_simple *s) {
|
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
|
|
if (s->stream)
|
|
|
|
|
pa_stream_free(s->stream);
|
|
|
|
|
|
|
|
|
|
if (s->context)
|
|
|
|
|
pa_context_free(s->context);
|
|
|
|
|
|
|
|
|
|
if (s->mainloop)
|
2004-07-06 00:08:44 +00:00
|
|
|
pa_mainloop_free(s->mainloop);
|
2004-06-23 23:17:30 +00:00
|
|
|
|
|
|
|
|
free(s);
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-06 00:08:44 +00:00
|
|
|
int pa_simple_write(struct pa_simple *p, const void*data, size_t length, int *perror) {
|
|
|
|
|
assert(p && data);
|
2004-06-23 23:17:30 +00:00
|
|
|
|
|
|
|
|
while (length > 0) {
|
|
|
|
|
size_t l;
|
|
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
while (!(l = pa_stream_writable_size(p->stream)))
|
|
|
|
|
if (iterate(p, 1, perror) < 0)
|
2004-06-23 23:17:30 +00:00
|
|
|
return -1;
|
|
|
|
|
|
2004-07-06 00:08:44 +00:00
|
|
|
if (l > length)
|
|
|
|
|
l = length;
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2004-07-06 00:08:44 +00:00
|
|
|
pa_stream_write(p->stream, data, l);
|
2004-06-23 23:17:30 +00:00
|
|
|
data += l;
|
|
|
|
|
length -= l;
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
/* Make sure that no data is pending for write */
|
|
|
|
|
if (iterate(p, 0, perror) < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-06 00:08:44 +00:00
|
|
|
int pa_simple_read(struct pa_simple *s, void*data, size_t length, int *perror) {
|
2004-06-23 23:17:30 +00:00
|
|
|
assert(0);
|
|
|
|
|
}
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2004-07-07 22:02:15 +00:00
|
|
|
|
|
|
|
|
static void drain_complete(struct pa_stream *s, void *userdata) {
|
|
|
|
|
struct pa_simple *p = userdata;
|
|
|
|
|
assert(s && p);
|
|
|
|
|
p->drained = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pa_simple_drain(struct pa_simple *p, int *perror) {
|
|
|
|
|
assert(p);
|
|
|
|
|
p->drained = 0;
|
|
|
|
|
pa_stream_drain(p->stream, drain_complete, p);
|
|
|
|
|
|
|
|
|
|
while (!p->drained) {
|
|
|
|
|
if (iterate(p, 1, perror) < 0) {
|
|
|
|
|
pa_stream_drain(p->stream, NULL, NULL);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|