From b772564a4eeab6d32ba9b7be9fb9beed1c12c999 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 12 Sep 2004 19:37:04 +0000 Subject: update simple API git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@193 fefdeb5f-60dc-0310-8127-8f9354f1896f --- doc/todo | 1 + polyp/cli-text.c | 4 +- polyp/pacat-simple.c | 11 +++++ polyp/polyplib-simple.c | 112 ++++++++++++++++++++++++++++++++++++++++++++---- polyp/polyplib-simple.h | 6 +++ polyp/util.c | 25 +++++++++++ polyp/util.h | 3 ++ 7 files changed, 152 insertions(+), 10 deletions(-) diff --git a/doc/todo b/doc/todo index af54c692..2509f76b 100644 --- a/doc/todo +++ b/doc/todo @@ -18,6 +18,7 @@ - fix or work around libtool bug - merge pa_context_connect_* - input latency +- check if all mainloop stuff works still ** later *** - xmlrpc/http diff --git a/polyp/cli-text.c b/polyp/cli-text.c index 1d6711df..2a48d576 100644 --- a/polyp/cli-text.c +++ b/polyp/cli-text.c @@ -93,7 +93,7 @@ char *pa_sink_list_to_string(struct pa_core *c) { assert(sink->monitor_source); pa_strbuf_printf( s, - " %c index: %u\n\tname: <%s>\n\tvolume: <0x%04x> (%0.2fdB)\n\tlatency: <%f usec>\n\tmonitor_source: <%u>\n\tsample_spec: <%s>\n", + " %c index: %u\n\tname: <%s>\n\tvolume: <0x%04x> (%0.2fdB)\n\tlatency: <%0.0f usec>\n\tmonitor_source: <%u>\n\tsample_spec: <%s>\n", c->default_sink_name && !strcmp(sink->name, c->default_sink_name) ? '*' : ' ', sink->index, sink->name, (unsigned) sink->volume, @@ -189,7 +189,7 @@ char *pa_sink_input_list_to_string(struct pa_core *c) { pa_sample_spec_snprint(ss, sizeof(ss), &i->sample_spec); assert(i->sink); pa_strbuf_printf( - s, " index: %u\n\tname: <%s>\n\tsink: <%u>\n\tvolume: <0x%04x> (%0.2fdB)\n\tlatency: <%f usec>\n\tsample_spec: <%s>\n", + s, " index: %u\n\tname: <%s>\n\tsink: <%u>\n\tvolume: <0x%04x> (%0.2fdB)\n\tlatency: <%0.0f usec>\n\tsample_spec: <%s>\n", i->index, i->name, i->sink->index, diff --git a/polyp/pacat-simple.c b/polyp/pacat-simple.c index f5b696a8..956728fb 100644 --- a/polyp/pacat-simple.c +++ b/polyp/pacat-simple.c @@ -56,6 +56,17 @@ int main(int argc, char*argv[]) { uint8_t buf[BUFSIZE]; ssize_t r; +#if 0 + pa_usec_t latency; + + if ((latency = pa_simple_get_playback_latency(s, &error)) == (pa_usec_t) -1) { + fprintf(stderr, __FILE__": pa_simple_get_playback_latency() failed: %s\n", pa_strerror(error)); + goto finish; + } + + fprintf(stderr, "%0.0f usec \r", (float)latency); +#endif + /* Read some data ... */ if ((r = read(STDIN_FILENO, buf, sizeof(buf))) <= 0) { if (r == 0) /* EOF */ diff --git a/polyp/polyplib-simple.c b/polyp/polyplib-simple.c index 8caae87e..1ac08869 100644 --- a/polyp/polyplib-simple.c +++ b/polyp/polyplib-simple.c @@ -41,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); @@ -71,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; } @@ -121,6 +125,7 @@ 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; @@ -178,6 +183,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; @@ -216,6 +228,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; @@ -250,20 +269,97 @@ 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 + p->latency = l->buffer_usec + l->sink_usec + l->transport_usec; +} + +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(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); @@ -273,8 +369,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; } diff --git a/polyp/polyplib-simple.h b/polyp/polyplib-simple.h index ed552cb4..b37bdec6 100644 --- a/polyp/polyplib-simple.h +++ b/polyp/polyplib-simple.h @@ -69,6 +69,12 @@ int pa_simple_drain(struct pa_simple *s, int *error); /** Read some data from the server */ int pa_simple_read(struct pa_simple *s, void*data, size_t length, int *error); +/** Return the playback latency. \since 0.5 */ +pa_usec_t pa_simple_get_playback_latency(struct pa_simple *s, int *perror); + +/** Flush the playback buffer. \since 0.5 */ +int pa_simple_flush(struct pa_simple *s, int *perror); + PA_C_DECL_END #endif diff --git a/polyp/util.c b/polyp/util.c index 45e1b605..1dbb8697 100644 --- a/polyp/util.c +++ b/polyp/util.c @@ -39,6 +39,7 @@ #include #include #include +#include #include "util.h" #include "xmalloc.h" @@ -322,3 +323,27 @@ int pa_fd_set_cloexec(int fd, int b) { return 0; } + +char *pa_get_binary_name(char *s, size_t l) { + char path[PATH_MAX]; + int i; + assert(s && l); + + /* This works on Linux only */ + + snprintf(path, sizeof(path), "/proc/%u/exe", (unsigned) getpid()); + if ((i = readlink(path, s, l-1)) < 0) + return NULL; + + s[i] = 0; + return s; +} + +char *pa_path_get_filename(const char *p) { + char *fn; + + if ((fn = strrchr(p, '/'))) + return fn+1; + + return (char*) p; +} diff --git a/polyp/util.h b/polyp/util.h index adc4429b..f34ba4c0 100644 --- a/polyp/util.h +++ b/polyp/util.h @@ -43,6 +43,9 @@ char *pa_vsprintf_malloc(const char *format, va_list ap); char *pa_get_user_name(char *s, size_t l); char *pa_get_host_name(char *s, size_t l); +char *pa_get_binary_name(char *s, size_t l); + +char *pa_path_get_filename(const char *p); pa_usec_t pa_timeval_diff(const struct timeval *a, const struct timeval *b); int pa_timeval_cmp(const struct timeval *a, const struct timeval *b); -- cgit