X-Git-Url: https://code.delx.au/pulseaudio/blobdiff_plain/36550f4a66ae28e1b81b9b818c38cd0fcd1302a1..fa499dad06ba6558111cdef64c18f2401e803cff:/polyp/polyplib-simple.c diff --git a/polyp/polyplib-simple.c b/polyp/polyplib-simple.c index ccd39c2a..a73aacfa 100644 --- a/polyp/polyplib-simple.c +++ b/polyp/polyplib-simple.c @@ -4,7 +4,7 @@ This file is part of polypaudio. polypaudio is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published + it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. @@ -13,7 +13,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License + You should have received a copy of the GNU Lesser General Public License along with polypaudio; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. @@ -33,6 +33,7 @@ #include "mainloop.h" #include "native-common.h" #include "xmalloc.h" +#include "log.h" struct pa_simple { struct pa_mainloop *mainloop; @@ -40,10 +41,11 @@ struct pa_simple { struct pa_stream *stream; enum pa_stream_direction direction; - int dead, drained; + int dead; void *read_data; size_t read_index, read_length; + pa_usec_t latency; }; static void read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata); @@ -70,6 +72,9 @@ static int check_error(struct pa_simple *p, int *perror) { fail: if (perror) *perror = pa_context_errno(p->context); + + p->dead = 1; + return -1; } @@ -94,6 +99,19 @@ static int iterate(struct pa_simple *p, int block, int *perror) { } while (pa_context_is_pending(p->context)); + + while (pa_mainloop_deferred_pending(p->mainloop)) { + + if (pa_mainloop_iterate(p->mainloop, 0, NULL) < 0) { + if (perror) + *perror = PA_ERROR_INTERNAL; + return -1; + } + + if (check_error(p, perror) < 0) + return -1; + } + return 0; } @@ -105,6 +123,7 @@ struct pa_simple* pa_simple_new( const char *stream_name, const struct pa_sample_spec *ss, const struct pa_buffer_attr *attr, + pa_volume_t volume, int *perror) { struct pa_simple *p; @@ -120,11 +139,12 @@ struct pa_simple* pa_simple_new( p->direction = dir; p->read_data = NULL; p->read_index = p->read_length = 0; + p->latency = 0; if (!(p->context = pa_context_new(pa_mainloop_get_api(p->mainloop), name))) goto fail; - pa_context_connect(p->context, server); + pa_context_connect(p->context, server, 1, NULL); /* Wait until the context is ready */ while (pa_context_get_state(p->context) != PA_CONTEXT_READY) { @@ -136,9 +156,9 @@ struct pa_simple* pa_simple_new( goto fail; if (dir == PA_STREAM_PLAYBACK) - pa_stream_connect_playback(p->stream, dev, attr); + pa_stream_connect_playback(p->stream, dev, attr, 0, volume); else - pa_stream_connect_record(p->stream, dev, attr); + pa_stream_connect_record(p->stream, dev, attr, 0); /* Wait until the stream is ready */ while (pa_stream_get_state(p->stream) != PA_STREAM_READY) { @@ -177,6 +197,13 @@ 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 && p->direction == PA_STREAM_PLAYBACK); + if (p->dead) { + if (perror) + *perror = pa_context_errno(p->context); + + return -1; + } + while (length > 0) { size_t l; @@ -204,7 +231,7 @@ static void read_callback(struct pa_stream *s, const void*data, size_t length, v assert(s && data && length && p); if (p->read_data) { - fprintf(stderr, __FILE__": Buffer overflow, dropping incoming memory blocks.\n"); + pa_log(__FILE__": Buffer overflow, dropping incoming memory blocks.\n"); pa_xfree(p->read_data); } @@ -215,6 +242,13 @@ static void read_callback(struct pa_stream *s, const void*data, size_t length, v int pa_simple_read(struct pa_simple *p, void*data, size_t length, int *perror) { assert(p && data && p->direction == PA_STREAM_RECORD); + if (p->dead) { + if (perror) + *perror = pa_context_errno(p->context); + + return -1; + } + while (length > 0) { if (p->read_data) { size_t l = length; @@ -249,20 +283,101 @@ int pa_simple_read(struct pa_simple *p, void*data, size_t length, int *perror) { return 0; } -static void drain_complete(struct pa_stream *s, int success, void *userdata) { +static void drain_or_flush_complete(struct pa_stream *s, int success, void *userdata) { struct pa_simple *p = userdata; assert(s && p); - p->drained = success ? 1 : -1; + if (!success) + p->dead = 1; } int pa_simple_drain(struct pa_simple *p, int *perror) { struct pa_operation *o; + assert(p && p->direction == PA_STREAM_PLAYBACK); + + if (p->dead) { + if (perror) + *perror = pa_context_errno(p->context); + + return -1; + } + + o = pa_stream_drain(p->stream, drain_or_flush_complete, p); + + while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) { + if (iterate(p, 1, perror) < 0) { + pa_operation_cancel(o); + pa_operation_unref(o); + return -1; + } + } + + pa_operation_unref(o); + + if (p->dead && perror) + *perror = pa_context_errno(p->context); + + return p->dead ? -1 : 0; +} + +static void latency_complete(struct pa_stream *s, const struct pa_latency_info *l, void *userdata) { + struct pa_simple *p = userdata; + assert(s && p); + + if (!l) + p->dead = 1; + else { + int negative = 0; + p->latency = pa_stream_get_latency(s, l, &negative); + if (negative) + p->latency = 0; + } +} + +pa_usec_t pa_simple_get_playback_latency(struct pa_simple *p, int *perror) { + struct pa_operation *o; + assert(p && p->direction == PA_STREAM_PLAYBACK); + + if (p->dead) { + if (perror) + *perror = pa_context_errno(p->context); + + return (pa_usec_t) -1; + } + + p->latency = 0; + o = pa_stream_get_latency_info(p->stream, latency_complete, p); + while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) { + + if (iterate(p, 1, perror) < 0) { + pa_operation_cancel(o); + pa_operation_unref(o); + return -1; + } + } + + pa_operation_unref(o); + + if (p->dead && perror) + *perror = pa_context_errno(p->context); + + return p->dead ? (pa_usec_t) -1 : p->latency; +} + +int pa_simple_flush(struct pa_simple *p, int *perror) { + struct pa_operation *o; assert(p && p->direction == PA_STREAM_PLAYBACK); - p->drained = 0; - o = pa_stream_drain(p->stream, drain_complete, p); - while (!p->drained) { + if (p->dead) { + if (perror) + *perror = pa_context_errno(p->context); + + return -1; + } + + o = pa_stream_flush(p->stream, drain_or_flush_complete, p); + + while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) { if (iterate(p, 1, perror) < 0) { pa_operation_cancel(o); pa_operation_unref(o); @@ -272,8 +387,8 @@ int pa_simple_drain(struct pa_simple *p, int *perror) { pa_operation_unref(o); - if (p->drained < 0 && perror) + if (p->dead && perror) *perror = pa_context_errno(p->context); - return 0; + return p->dead ? -1 : 0; }