From 5ea96e312be3aa495e77786283e1edea7592047f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sat, 10 Jul 2004 19:04:21 +0000 Subject: implement parec-simple and matching simple recording API add support for killing source outputs in native protocol fix channel management in client library git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@56 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/simple.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 4 deletions(-) (limited to 'src/simple.c') diff --git a/src/simple.c b/src/simple.c index ba0a8f0c..5f86c5bf 100644 --- a/src/simple.c +++ b/src/simple.c @@ -1,3 +1,5 @@ +#include +#include #include #include @@ -10,10 +12,16 @@ struct pa_simple { struct pa_mainloop *mainloop; struct pa_context *context; struct pa_stream *stream; + enum pa_stream_direction direction; int dead, drained; + + void *read_data; + size_t read_index, read_length; }; +static void read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata); + static int check_error(struct pa_simple *p, int *perror) { assert(p); @@ -71,6 +79,9 @@ struct pa_simple* pa_simple_new( p->mainloop = pa_mainloop_new(); assert(p->mainloop); p->dead = 0; + p->direction = dir; + p->read_data = NULL; + p->read_index = p->read_length = 0; if (!(p->context = pa_context_new(pa_mainloop_get_api(p->mainloop), name))) goto fail; @@ -95,6 +106,8 @@ struct pa_simple* pa_simple_new( goto fail; } + pa_stream_set_read_callback(p->stream, read_callback, p); + return p; fail: @@ -107,6 +120,8 @@ fail: void pa_simple_free(struct pa_simple *s) { assert(s); + free(s->read_data); + if (s->stream) pa_stream_free(s->stream); @@ -120,7 +135,7 @@ void pa_simple_free(struct pa_simple *s) { } int pa_simple_write(struct pa_simple *p, const void*data, size_t length, int *perror) { - assert(p && data); + assert(p && data && p->direction == PA_STREAM_PLAYBACK); while (length > 0) { size_t l; @@ -144,10 +159,57 @@ int pa_simple_write(struct pa_simple *p, const void*data, size_t length, int *pe return 0; } -int pa_simple_read(struct pa_simple *s, void*data, size_t length, int *perror) { - assert(0); +static void read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) { + struct pa_simple *p = userdata; + assert(s && data && length && p); + + if (p->read_data) { + fprintf(stderr, __FILE__": Buffer overflow, dropping incoming memory blocks.\n"); + free(p->read_data); + } + + p->read_data = malloc(p->read_length = length); + assert(p->read_data); + memcpy(p->read_data, data, length); + p->read_index = 0; } +int pa_simple_read(struct pa_simple *p, void*data, size_t length, int *perror) { + assert(p && data && p->direction == PA_STREAM_RECORD); + + while (length > 0) { + if (p->read_data) { + size_t l = length; + + if (p->read_length <= l) + l = p->read_length; + + memcpy(data, p->read_data+p->read_index, l); + + data += l; + length -= l; + + p->read_index += l; + p->read_length -= l; + + if (!p->read_length) { + free(p->read_data); + p->read_data = NULL; + p->read_index = 0; + } + + if (!length) + return 0; + + assert(!p->read_data); + } + + if (iterate(p, 1, perror) < 0) + return -1; + } + + return 0; +} static void drain_complete(struct pa_stream *s, void *userdata) { struct pa_simple *p = userdata; @@ -156,7 +218,7 @@ static void drain_complete(struct pa_stream *s, void *userdata) { } int pa_simple_drain(struct pa_simple *p, int *perror) { - assert(p); + assert(p && p->direction == PA_STREAM_PLAYBACK); p->drained = 0; pa_stream_drain(p->stream, drain_complete, p); -- cgit