]> code.delx.au - pulseaudio/commitdiff
update simple API
authorLennart Poettering <lennart@poettering.net>
Sun, 12 Sep 2004 19:37:04 +0000 (19:37 +0000)
committerLennart Poettering <lennart@poettering.net>
Sun, 12 Sep 2004 19:37:04 +0000 (19:37 +0000)
git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@193 fefdeb5f-60dc-0310-8127-8f9354f1896f

doc/todo
polyp/cli-text.c
polyp/pacat-simple.c
polyp/polyplib-simple.c
polyp/polyplib-simple.h
polyp/util.c
polyp/util.h

index af54c6929d7feab901cef37c9e8d7faaf1ee93b1..2509f76b1f1bfd0a829dde7af1e1a885130a19dc 100644 (file)
--- 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
index 1d6711dfacbc59d97fb00e8e1941a4775975dcbd..2a48d576303fa80a358021c3149fbcc4e6400340 100644 (file)
@@ -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,
index f5b696a89cdf01e5625477cd2a6d2f1ecdb9d3d9..956728fb7cc033382600e4df383ebbc50d7262d5 100644 (file)
@@ -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 */
index 8caae87e04fde2c82fba9c6116f649a55cc02e59..1ac0886983380c848b263a8bedd050a2b42faaff 100644 (file)
@@ -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;
 }
index ed552cb468f878cab4f5c83ce9e6880f93169c6a..b37bdec603e01068434d9fc2a8ac1966fc31ef15 100644 (file)
@@ -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
index 45e1b605fd732d3c61d243891fd1bc1cdfec7cca..1dbb8697acc8321025c6d46ff16dca0d2d4ad0fb 100644 (file)
@@ -39,6 +39,7 @@
 #include <sys/time.h>
 #include <sched.h>
 #include <sys/resource.h>
+#include <limits.h>
 
 #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;
+}
index adc4429bc9660b48a623859e351b35454b83f124..f34ba4c082fefb687946100ccfa17e0f4e17c393 100644 (file)
@@ -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);