rework the simple API to make use of the new threaded mainloop implementation

git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@832 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
Lennart Poettering 2006-05-06 20:58:28 +00:00
parent 4b4c8fd152
commit df3306c4af

View file

@ -29,7 +29,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <polyp/polypaudio.h> #include <polyp/polypaudio.h>
#include <polyp/mainloop.h> #include <polyp/thread-mainloop.h>
#include <polypcore/native-common.h> #include <polypcore/native-common.h>
#include <polypcore/xmalloc.h> #include <polypcore/xmalloc.h>
@ -37,91 +37,92 @@
#include "simple.h" #include "simple.h"
#define CHECK_VALIDITY_RETURN_ANY(rerror, expression, error, ret) do { \
if (!(expression)) { \
if (rerror) \
*(rerror) = error; \
return ret; \
} \
} while(0);
struct pa_simple { struct pa_simple {
pa_mainloop *mainloop; pa_threaded_mainloop *mainloop;
pa_context *context; pa_context *context;
pa_stream *stream; pa_stream *stream;
pa_stream_direction_t direction; pa_stream_direction_t direction;
int dead;
const void *read_data; const void *read_data;
size_t read_index, read_length; size_t read_index, read_length;
pa_usec_t latency;
int operation_success;
}; };
static int check_error(pa_simple *p, int *rerror) { #define CHECK_VALIDITY_RETURN_ANY(rerror, expression, error, ret) do { \
pa_context_state_t cst; if (!(expression)) { \
pa_stream_state_t sst; if (rerror) \
*(rerror) = error; \
return (ret); \
} \
} while(0);
#define CHECK_SUCCESS_GOTO(p, rerror, expression, label) do { \
if (!(expression)) { \
if (rerror) \
*(rerror) = pa_context_errno((p)->context); \
goto label; \
} \
} while(0);
#define CHECK_DEAD_GOTO(p, rerror, label) do { \
if (!(p)->context || pa_context_get_state((p)->context) != PA_CONTEXT_READY || \
!(p)->stream || pa_stream_get_state((p)->stream) != PA_STREAM_READY) { \
if (((p)->context && pa_context_get_state((p)->context) == PA_CONTEXT_FAILED) || \
((p)->stream && pa_stream_get_state((p)->stream) == PA_STREAM_FAILED)) { \
if (rerror) \
*(rerror) = pa_context_errno((p)->context); \
} else \
if (rerror) \
*(rerror) = PA_ERR_BADSTATE; \
goto label; \
} \
} while(0);
static void context_state_cb(pa_context *c, void *userdata) {
pa_simple *p = userdata;
assert(c);
assert(p); assert(p);
if ((cst = pa_context_get_state(p->context)) == PA_CONTEXT_FAILED) switch (pa_context_get_state(c)) {
goto fail; case PA_CONTEXT_READY:
case PA_CONTEXT_TERMINATED:
assert(cst != PA_CONTEXT_TERMINATED); case PA_CONTEXT_FAILED:
pa_threaded_mainloop_signal(p->mainloop, 0);
if (p->stream) {
if ((sst = pa_stream_get_state(p->stream)) == PA_STREAM_FAILED)
goto fail;
assert(sst != PA_STREAM_TERMINATED);
}
return 0;
fail:
if (rerror)
*rerror = pa_context_errno(p->context);
p->dead = 1;
return -1;
}
static int iterate(pa_simple *p, int block, int *rerror) {
assert(p && p->context && p->mainloop);
if (check_error(p, rerror) < 0)
return -1;
if (block || pa_context_is_pending(p->context)) {
do {
if (pa_mainloop_iterate(p->mainloop, 1, NULL) < 0) {
if (rerror)
*rerror = PA_ERR_INTERNAL;
return -1;
}
if (check_error(p, rerror) < 0)
return -1;
} while (pa_context_is_pending(p->context));
}
for (;;) {
int r;
if ((r = pa_mainloop_iterate(p->mainloop, 0, NULL)) < 0) {
if (rerror)
*rerror = PA_ERR_INTERNAL;
return -1;
}
if (r == 0)
break; break;
if (check_error(p, rerror) < 0) case PA_CONTEXT_UNCONNECTED:
return -1; case PA_CONTEXT_CONNECTING:
case PA_CONTEXT_AUTHORIZING:
case PA_CONTEXT_SETTING_NAME:
break;
} }
}
return 0; static void stream_state_cb(pa_stream *s, void * userdata) {
pa_simple *p = userdata;
assert(s);
assert(p);
switch (pa_stream_get_state(s)) {
case PA_STREAM_READY:
case PA_STREAM_FAILED:
case PA_STREAM_TERMINATED:
pa_threaded_mainloop_signal(p->mainloop, 0);
break;
case PA_STREAM_UNCONNECTED:
case PA_STREAM_CREATING:
break;
}
}
static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
pa_simple *p = userdata;
assert(p);
pa_threaded_mainloop_signal(p->mainloop, 0);
} }
pa_simple* pa_simple_new( pa_simple* pa_simple_new(
@ -145,51 +146,71 @@ pa_simple* pa_simple_new(
p = pa_xnew(pa_simple, 1); p = pa_xnew(pa_simple, 1);
p->context = NULL; p->context = NULL;
p->stream = NULL; p->stream = NULL;
p->mainloop = pa_mainloop_new();
assert(p->mainloop);
p->dead = 0;
p->direction = dir; p->direction = dir;
p->read_data = NULL; p->read_data = NULL;
p->read_index = p->read_length = 0; p->read_index = p->read_length = 0;
p->latency = 0;
if (!(p->context = pa_context_new(pa_mainloop_get_api(p->mainloop), name))) if (!(p->mainloop = pa_threaded_mainloop_new()))
goto fail; goto fail;
if (!(p->context = pa_context_new(pa_threaded_mainloop_get_api(p->mainloop), name)))
goto fail;
pa_context_set_state_callback(p->context, context_state_cb, p);
if (pa_context_connect(p->context, server, 0, NULL) < 0) { if (pa_context_connect(p->context, server, 0, NULL) < 0) {
error = pa_context_errno(p->context); error = pa_context_errno(p->context);
goto fail; goto fail;
} }
pa_threaded_mainloop_lock(p->mainloop);
if (pa_threaded_mainloop_start(p->mainloop) < 0)
goto unlock_and_fail;
/* Wait until the context is ready */ /* Wait until the context is ready */
while (pa_context_get_state(p->context) != PA_CONTEXT_READY) { pa_threaded_mainloop_wait(p->mainloop);
if (iterate(p, 1, &error) < 0)
goto fail; if (pa_context_get_state(p->context) != PA_CONTEXT_READY) {
error = pa_context_errno(p->context);
goto unlock_and_fail;
} }
if (!(p->stream = pa_stream_new(p->context, stream_name, ss, NULL))) { if (!(p->stream = pa_stream_new(p->context, stream_name, ss, NULL))) {
error = pa_context_errno(p->context); error = pa_context_errno(p->context);
goto fail; goto unlock_and_fail;
} }
pa_stream_set_state_callback(p->stream, stream_state_cb, p);
pa_stream_set_read_callback(p->stream, stream_request_cb, p);
pa_stream_set_write_callback(p->stream, stream_request_cb, p);
if (dir == PA_STREAM_PLAYBACK) if (dir == PA_STREAM_PLAYBACK)
r = pa_stream_connect_playback(p->stream, dev, attr, 0, NULL, NULL); r = pa_stream_connect_playback(p->stream, dev, attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL);
else else
r = pa_stream_connect_record(p->stream, dev, attr, 0); r = pa_stream_connect_record(p->stream, dev, attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE);
if (r < 0) { if (r < 0) {
error = pa_context_errno(p->context); error = pa_context_errno(p->context);
goto fail; goto unlock_and_fail;
} }
/* Wait until the stream is ready */ /* Wait until the stream is ready */
while (pa_stream_get_state(p->stream) != PA_STREAM_READY) { pa_threaded_mainloop_wait(p->mainloop);
if (iterate(p, 1, &error) < 0)
goto fail; /* Wait until the stream is ready */
if (pa_stream_get_state(p->stream) != PA_STREAM_READY) {
error = pa_context_errno(p->context);
goto unlock_and_fail;
} }
pa_threaded_mainloop_unlock(p->mainloop);
return p; return p;
unlock_and_fail:
pa_threaded_mainloop_unlock(p->mainloop);
fail: fail:
if (rerror) if (rerror)
*rerror = error; *rerror = error;
@ -200,6 +221,9 @@ fail:
void pa_simple_free(pa_simple *s) { void pa_simple_free(pa_simple *s) {
assert(s); assert(s);
if (s->mainloop)
pa_threaded_mainloop_stop(s->mainloop);
if (s->stream) if (s->stream)
pa_stream_unref(s->stream); pa_stream_unref(s->stream);
@ -207,232 +231,208 @@ void pa_simple_free(pa_simple *s) {
pa_context_unref(s->context); pa_context_unref(s->context);
if (s->mainloop) if (s->mainloop)
pa_mainloop_free(s->mainloop); pa_threaded_mainloop_free(s->mainloop);
pa_xfree(s); pa_xfree(s);
} }
int pa_simple_write(pa_simple *p, const void*data, size_t length, int *rerror) { int pa_simple_write(pa_simple *p, const void*data, size_t length, int *rerror) {
assert(p); assert(p);
assert(data);
CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1); CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
CHECK_VALIDITY_RETURN_ANY(rerror, data && length, PA_ERR_INVALID, -1);
if (p->dead) { pa_threaded_mainloop_lock(p->mainloop);
if (rerror)
*rerror = pa_context_errno(p->context);
return -1; CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
}
while (length > 0) { while (length > 0) {
size_t l; size_t l;
int r;
while (!(l = pa_stream_writable_size(p->stream))) while (!(l = pa_stream_writable_size(p->stream))) {
if (iterate(p, 1, rerror) < 0) pa_threaded_mainloop_wait(p->mainloop);
return -1; CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
}
CHECK_SUCCESS_GOTO(p, rerror, l != (size_t) -1, unlock_and_fail);
if (l > length) if (l > length)
l = length; l = length;
pa_stream_write(p->stream, data, l, NULL, 0, PA_SEEK_RELATIVE); r = pa_stream_write(p->stream, data, l, NULL, 0, PA_SEEK_RELATIVE);
CHECK_SUCCESS_GOTO(p, rerror, r >= 0, unlock_and_fail);
data = (const uint8_t*) data + l; data = (const uint8_t*) data + l;
length -= l; length -= l;
} }
/* Make sure that no data is pending for write */ pa_threaded_mainloop_unlock(p->mainloop);
if (iterate(p, 0, rerror) < 0)
return -1;
return 0; return 0;
unlock_and_fail:
pa_threaded_mainloop_unlock(p->mainloop);
return -1;
} }
int pa_simple_read(pa_simple *p, void*data, size_t length, int *rerror) { int pa_simple_read(pa_simple *p, void*data, size_t length, int *rerror) {
assert(p); assert(p);
assert(data);
CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE, -1); CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE, -1);
CHECK_VALIDITY_RETURN_ANY(rerror, data && length, PA_ERR_INVALID, -1);
if (p->dead) { pa_threaded_mainloop_lock(p->mainloop);
if (rerror)
*rerror = pa_context_errno(p->context);
return -1; CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
}
while (length > 0) { while (length > 0) {
size_t l;
if (!p->read_data) while (!p->read_data) {
if (pa_stream_peek(p->stream, &p->read_data, &p->read_length) >= 0) int r;
r = pa_stream_peek(p->stream, &p->read_data, &p->read_length);
CHECK_SUCCESS_GOTO(p, rerror, r == 0, unlock_and_fail);
if (!p->read_data) {
pa_threaded_mainloop_wait(p->mainloop);
CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
} else
p->read_index = 0; p->read_index = 0;
if (p->read_data) {
size_t l = length;
if (p->read_length <= l)
l = p->read_length;
memcpy(data, (const uint8_t*) p->read_data+p->read_index, l);
data = (uint8_t*) data + l;
length -= l;
p->read_index += l;
p->read_length -= l;
if (!p->read_length) {
pa_stream_drop(p->stream);
p->read_data = NULL;
p->read_length = 0;
p->read_index = 0;
}
if (!length)
return 0;
assert(!p->read_data);
} }
if (iterate(p, 1, rerror) < 0) l = p->read_length < length ? p->read_length : length;
return -1; memcpy(data, (const uint8_t*) p->read_data+p->read_index, l);
data = (uint8_t*) data + l;
length -= l;
p->read_index += l;
p->read_length -= l;
if (!p->read_length) {
int r;
r = pa_stream_drop(p->stream);
p->read_data = NULL;
p->read_length = 0;
p->read_index = 0;
CHECK_SUCCESS_GOTO(p, rerror, r == 0, unlock_and_fail);
}
} }
pa_threaded_mainloop_unlock(p->mainloop);
return 0; return 0;
unlock_and_fail:
pa_threaded_mainloop_unlock(p->mainloop);
return -1;
} }
static void drain_or_flush_complete(pa_stream *s, int success, void *userdata) { static void success_cb(pa_stream *s, int success, void *userdata) {
pa_simple *p = userdata; pa_simple *p = userdata;
assert(s); assert(s);
assert(p); assert(p);
if (!success) p->operation_success = success;
p->dead = 1; pa_threaded_mainloop_signal(p->mainloop, 0);
} }
int pa_simple_drain(pa_simple *p, int *rerror) { int pa_simple_drain(pa_simple *p, int *rerror) {
pa_operation *o; pa_operation *o = NULL;
assert(p); assert(p);
CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1); CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
if (p->dead) { pa_threaded_mainloop_lock(p->mainloop);
if (rerror) CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
*rerror = pa_context_errno(p->context);
return -1; o = pa_stream_drain(p->stream, success_cb, p);
} CHECK_SUCCESS_GOTO(p, rerror, o, unlock_and_fail);
if (!(o = pa_stream_drain(p->stream, drain_or_flush_complete, p))) { p->operation_success = 0;
if (rerror) while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
*rerror = pa_context_errno(p->context); pa_threaded_mainloop_wait(p->mainloop);
return -1; CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
}
while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) {
if (iterate(p, 1, rerror) < 0) {
pa_operation_cancel(o);
pa_operation_unref(o);
return -1;
}
} }
CHECK_SUCCESS_GOTO(p, rerror, p->operation_success, unlock_and_fail);
pa_operation_unref(o); pa_operation_unref(o);
pa_threaded_mainloop_unlock(p->mainloop);
if (p->dead && rerror) return 0;
*rerror = pa_context_errno(p->context);
return p->dead ? -1 : 0; unlock_and_fail:
}
static void timing_complete(pa_stream *s, int success, void *userdata) { if (o) {
pa_simple *p = userdata; pa_operation_cancel(o);
pa_operation_unref(o);
assert(s);
assert(p);
if (!success)
p->dead = 1;
else {
int negative = 0;
if (pa_stream_get_latency(s, &p->latency, &negative) < 0)
p->dead = 1;
else if (negative)
p->latency = 0;
}
}
pa_usec_t pa_simple_get_playback_latency(pa_simple *p, int *rerror) {
pa_operation *o;
assert(p);
CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
if (p->dead) {
if (rerror)
*rerror = pa_context_errno(p->context);
return (pa_usec_t) -1;
} }
p->latency = 0; pa_threaded_mainloop_unlock(p->mainloop);
if (!(o = pa_stream_update_timing_info(p->stream, timing_complete, p))) { return -1;
if (rerror)
*rerror = pa_context_errno(p->context);
return (pa_usec_t) -1;
}
while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) {
if (iterate(p, 1, rerror) < 0) {
pa_operation_cancel(o);
pa_operation_unref(o);
return -1;
}
}
pa_operation_unref(o);
if (p->dead && rerror)
*rerror = pa_context_errno(p->context);
return p->dead ? (pa_usec_t) -1 : p->latency;
} }
int pa_simple_flush(pa_simple *p, int *rerror) { int pa_simple_flush(pa_simple *p, int *rerror) {
pa_operation *o; pa_operation *o = NULL;
assert(p); assert(p);
CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1); CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
if (p->dead) { pa_threaded_mainloop_lock(p->mainloop);
if (rerror) CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
*rerror = pa_context_errno(p->context);
return -1; o = pa_stream_flush(p->stream, success_cb, p);
} CHECK_SUCCESS_GOTO(p, rerror, o, unlock_and_fail);
if (!(o = pa_stream_flush(p->stream, drain_or_flush_complete, p))) { p->operation_success = 0;
if (rerror) while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
*rerror = pa_context_errno(p->context); pa_threaded_mainloop_wait(p->mainloop);
return -1; CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
}
while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) {
if (iterate(p, 1, rerror) < 0) {
pa_operation_cancel(o);
pa_operation_unref(o);
return -1;
}
} }
CHECK_SUCCESS_GOTO(p, rerror, p->operation_success, unlock_and_fail);
pa_operation_unref(o); pa_operation_unref(o);
pa_threaded_mainloop_unlock(p->mainloop);
if (p->dead && rerror) return 0;
*rerror = pa_context_errno(p->context);
return p->dead ? -1 : 0; unlock_and_fail:
if (o) {
pa_operation_cancel(o);
pa_operation_unref(o);
}
pa_threaded_mainloop_unlock(p->mainloop);
return -1;
} }
pa_usec_t pa_simple_get_playback_latency(pa_simple *p, int *rerror) {
pa_usec_t t;
int r, negative;
assert(p);
CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, (pa_usec_t) -1);
pa_threaded_mainloop_lock(p->mainloop);
CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
r = pa_stream_get_latency(p->stream, &t, &negative);
CHECK_SUCCESS_GOTO(p, rerror, r >= 0, unlock_and_fail);
pa_threaded_mainloop_unlock(p->mainloop);
return negative ? 0 : t;
unlock_and_fail:
pa_threaded_mainloop_unlock(p->mainloop);
return (pa_usec_t) -1;
}