summaryrefslogtreecommitdiffstats
path: root/polyp/polyplib-stream.c
diff options
context:
space:
mode:
Diffstat (limited to 'polyp/polyplib-stream.c')
-rw-r--r--polyp/polyplib-stream.c211
1 files changed, 161 insertions, 50 deletions
diff --git a/polyp/polyplib-stream.c b/polyp/polyplib-stream.c
index 291c3cd3..68035b0f 100644
--- a/polyp/polyplib-stream.c
+++ b/polyp/polyplib-stream.c
@@ -26,12 +26,15 @@
#include <assert.h>
#include <string.h>
#include <stdio.h>
+#include <string.h>
#include "polyplib-internal.h"
#include "xmalloc.h"
#include "pstream-util.h"
#include "util.h"
+#define LATENCY_IPOL_INTERVAL_USEC (100000L)
+
struct pa_stream *pa_stream_new(struct pa_context *c, const char *name, const struct pa_sample_spec *ss) {
struct pa_stream *s;
assert(c && ss);
@@ -39,6 +42,7 @@ struct pa_stream *pa_stream_new(struct pa_context *c, const char *name, const st
s = pa_xmalloc(sizeof(struct pa_stream));
s->ref = 1;
s->context = c;
+ s->mainloop = c->mainloop;
s->read_callback = NULL;
s->read_userdata = NULL;
@@ -60,6 +64,13 @@ struct pa_stream *pa_stream_new(struct pa_context *c, const char *name, const st
s->counter = 0;
s->previous_time = 0;
+ s->corked = 0;
+ s->interpolate = 0;
+
+ s->ipol_usec = 0;
+ memset(&s->ipol_timestamp, 0, sizeof(s->ipol_timestamp));
+ s->ipol_event = NULL;
+
PA_LLIST_PREPEND(struct pa_stream, c->streams, s);
return pa_stream_ref(s);
@@ -67,6 +78,12 @@ struct pa_stream *pa_stream_new(struct pa_context *c, const char *name, const st
static void stream_free(struct pa_stream *s) {
assert(s);
+
+ if (s->ipol_event) {
+ assert(s->mainloop);
+ s->mainloop->time_free(s->ipol_event);
+ }
+
pa_xfree(s->name);
pa_xfree(s);
}
@@ -181,6 +198,22 @@ finish:
pa_context_unref(c);
}
+static void ipol_callback(struct pa_mainloop_api *m, struct pa_time_event *e, const struct timeval *tv, void *userdata) {
+ struct timeval tv2;
+ struct pa_stream *s = userdata;
+
+ pa_stream_ref(s);
+ pa_operation_unref(pa_stream_get_latency_info(s, NULL, NULL));
+
+ gettimeofday(&tv2, NULL);
+ tv2.tv_usec += LATENCY_IPOL_INTERVAL_USEC;
+
+ m->time_restart(e, &tv2);
+
+ pa_stream_unref(s);
+}
+
+
void pa_create_stream_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
struct pa_stream *s = userdata;
assert(pd && s && s->state == PA_STREAM_CREATING);
@@ -207,6 +240,17 @@ void pa_create_stream_callback(struct pa_pdispatch *pd, uint32_t command, uint32
pa_dynarray_put((s->direction == PA_STREAM_RECORD) ? s->context->record_streams : s->context->playback_streams, s->channel, s);
pa_stream_set_state(s, PA_STREAM_READY);
+ if (s->interpolate) {
+ struct timeval tv;
+ pa_operation_unref(pa_stream_get_latency_info(s, NULL, NULL));
+
+ gettimeofday(&tv, NULL);
+ tv.tv_usec += LATENCY_IPOL_INTERVAL_USEC; /* every 100 ms */
+
+ assert(!s->ipol_event);
+ s->ipol_event = s->mainloop->time_new(s->mainloop, &tv, &ipol_callback, s);
+ }
+
if (s->requested_bytes && s->ref > 1 && s->write_callback)
s->write_callback(s, s->requested_bytes, s->write_userdata);
@@ -221,6 +265,9 @@ static void create_stream(struct pa_stream *s, const char *dev, const struct pa_
pa_stream_ref(s);
+ s->interpolate = !!(flags & PA_STREAM_INTERPOLATE_LATENCY);
+ pa_stream_trash_ipol(s);
+
if (attr)
s->buffer_attr = *attr;
else {
@@ -330,7 +377,7 @@ struct pa_operation * pa_stream_drain(struct pa_stream *s, void (*cb) (struct pa
return pa_operation_ref(o);
}
-static void stream_get_latency_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
+static void stream_get_latency_info_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
struct pa_operation *o = userdata;
struct pa_latency_info i, *p = NULL;
struct timeval local, remote, now;
@@ -347,32 +394,39 @@ static void stream_get_latency_callback(struct pa_pdispatch *pd, uint32_t comman
pa_tagstruct_getu32(t, &i.queue_length) < 0 ||
pa_tagstruct_get_timeval(t, &local) < 0 ||
pa_tagstruct_get_timeval(t, &remote) < 0 ||
+ pa_tagstruct_getu64(t, &i.counter) < 0 ||
!pa_tagstruct_eof(t)) {
pa_context_fail(o->context, PA_ERROR_PROTOCOL);
goto finish;
- } else
- p = &i;
-
- gettimeofday(&now, NULL);
-
- if (pa_timeval_cmp(&local, &remote) < 0 && pa_timeval_cmp(&remote, &now)) {
- /* local and remote seem to have synchronized clocks */
-
- if (o->stream->direction == PA_STREAM_PLAYBACK)
- i.transport_usec = pa_timeval_diff(&remote, &local);
- else
- i.transport_usec = pa_timeval_diff(&now, &remote);
-
- i.synchronized_clocks = 1;
- i.timestamp = remote;
} else {
- /* clocks are not synchronized, let's estimate latency then */
- i.transport_usec = pa_timeval_diff(&now, &local)/2;
- i.synchronized_clocks = 0;
- i.timestamp = local;
- pa_timeval_add(&i.timestamp, i.transport_usec);
- }
+ gettimeofday(&now, NULL);
+
+ if (pa_timeval_cmp(&local, &remote) < 0 && pa_timeval_cmp(&remote, &now)) {
+ /* local and remote seem to have synchronized clocks */
+
+ if (o->stream->direction == PA_STREAM_PLAYBACK)
+ i.transport_usec = pa_timeval_diff(&remote, &local);
+ else
+ i.transport_usec = pa_timeval_diff(&now, &remote);
+
+ i.synchronized_clocks = 1;
+ i.timestamp = remote;
+ } else {
+ /* clocks are not synchronized, let's estimate latency then */
+ i.transport_usec = pa_timeval_diff(&now, &local)/2;
+ i.synchronized_clocks = 0;
+ i.timestamp = local;
+ pa_timeval_add(&i.timestamp, i.transport_usec);
+ }
+
+ if (o->stream->interpolate) {
+ o->stream->ipol_timestamp = now;
+ o->stream->ipol_usec = pa_stream_get_time(o->stream, &i);
+ }
+ p = &i;
+ }
+
if (o->callback) {
void (*cb)(struct pa_stream *s, const struct pa_latency_info *i, void *userdata) = o->callback;
cb(o->stream, p, o->userdata);
@@ -383,7 +437,7 @@ finish:
pa_operation_unref(o);
}
-struct pa_operation* pa_stream_get_latency(struct pa_stream *s, void (*cb)(struct pa_stream *p, const struct pa_latency_info*i, void *userdata), void *userdata) {
+struct pa_operation* pa_stream_get_latency_info(struct pa_stream *s, void (*cb)(struct pa_stream *p, const struct pa_latency_info*i, void *userdata), void *userdata) {
uint32_t tag;
struct pa_operation *o;
struct pa_tagstruct *t;
@@ -403,9 +457,10 @@ struct pa_operation* pa_stream_get_latency(struct pa_stream *s, void (*cb)(struc
gettimeofday(&now, NULL);
pa_tagstruct_put_timeval(t, &now);
+ pa_tagstruct_putu64(t, s->counter);
pa_pstream_send_tagstruct(s->context->pstream, t);
- pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, o);
+ pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_info_callback, o);
return pa_operation_ref(o);
}
@@ -505,6 +560,13 @@ struct pa_operation* pa_stream_cork(struct pa_stream *s, int b, void (*cb) (stru
uint32_t tag;
assert(s && s->ref >= 1 && s->state == PA_STREAM_READY);
+ if (!s->corked && b)
+ s->ipol_usec = pa_stream_get_interpolated_time(s);
+ else if (s->corked && !b)
+ gettimeofday(&s->ipol_timestamp, NULL);
+
+ s->corked = b;
+
o = pa_operation_new(s->context, s);
assert(o);
o->callback = cb;
@@ -519,6 +581,8 @@ struct pa_operation* pa_stream_cork(struct pa_stream *s, int b, void (*cb) (stru
pa_pstream_send_tagstruct(s->context->pstream, t);
pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, o);
+ pa_operation_unref(pa_stream_get_latency_info(s, NULL, NULL));
+
return pa_operation_ref(o);
}
@@ -543,15 +607,24 @@ struct pa_operation* pa_stream_send_simple_command(struct pa_stream *s, uint32_t
}
struct pa_operation* pa_stream_flush(struct pa_stream *s, void (*cb)(struct pa_stream *s, int success, void *userdata), void *userdata) {
- return pa_stream_send_simple_command(s, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_FLUSH_PLAYBACK_STREAM : PA_COMMAND_FLUSH_RECORD_STREAM, cb, userdata);
+ struct pa_operation *o;
+ o = pa_stream_send_simple_command(s, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_FLUSH_PLAYBACK_STREAM : PA_COMMAND_FLUSH_RECORD_STREAM, cb, userdata);
+ pa_operation_unref(pa_stream_get_latency_info(s, NULL, NULL));
+ return o;
}
struct pa_operation* pa_stream_prebuf(struct pa_stream *s, void (*cb)(struct pa_stream *s, int success, void *userdata), void *userdata) {
- return pa_stream_send_simple_command(s, PA_COMMAND_PREBUF_PLAYBACK_STREAM, cb, userdata);
+ struct pa_operation *o;
+ o = pa_stream_send_simple_command(s, PA_COMMAND_PREBUF_PLAYBACK_STREAM, cb, userdata);
+ pa_operation_unref(pa_stream_get_latency_info(s, NULL, NULL));
+ return o;
}
struct pa_operation* pa_stream_trigger(struct pa_stream *s, void (*cb)(struct pa_stream *s, int success, void *userdata), void *userdata) {
- return pa_stream_send_simple_command(s, PA_COMMAND_TRIGGER_PLAYBACK_STREAM, cb, userdata);
+ struct pa_operation *o;
+ o = pa_stream_send_simple_command(s, PA_COMMAND_TRIGGER_PLAYBACK_STREAM, cb, userdata);
+ pa_operation_unref(pa_stream_get_latency_info(s, NULL, NULL));
+ return o;
}
struct pa_operation* pa_stream_set_name(struct pa_stream *s, const char *name, void(*cb)(struct pa_stream*c, int success, void *userdata), void *userdata) {
@@ -582,12 +655,6 @@ uint64_t pa_stream_get_counter(struct pa_stream *s) {
return s->counter;
}
-void pa_stream_reset_counter(struct pa_stream *s) {
- assert(s);
- s->counter = 0;
- s->previous_time = 0;
-}
-
pa_usec_t pa_stream_get_time(struct pa_stream *s, const struct pa_latency_info *i) {
pa_usec_t usec;
assert(s);
@@ -620,27 +687,22 @@ pa_usec_t pa_stream_get_time(struct pa_stream *s, const struct pa_latency_info *
return usec;
}
-pa_usec_t pa_stream_get_total_latency(struct pa_stream *s, const struct pa_latency_info *i, int *negative) {
+pa_usec_t pa_stream_get_latency(struct pa_stream *s, const struct pa_latency_info *i, int *negative) {
+ pa_usec_t t, c;
assert(s && i);
- if (s->direction == PA_STREAM_PLAYBACK) {
+ t = pa_stream_get_time(s, i);
+ c = pa_bytes_to_usec(s->counter, &s->sample_spec);
+
+ if (t <= c) {
if (negative)
- *negative = 0;
-
- return i->transport_usec + i->buffer_usec + i->sink_usec;
- } else if (s->direction == PA_STREAM_RECORD) {
- pa_usec_t usec = i->source_usec + i->buffer_usec + i->transport_usec;
-
- if (usec >= i->sink_usec) {
- if (negative)
- *negative = 0;
- return usec - i->sink_usec;
- } else {
- if (negative)
- *negative = 1;
+ *negative = 1;
- return i->sink_usec - usec;
- }
+ return c-t;
+ } else {
+ if (negative)
+ *negative = 0;
+ return t-c;
}
return 0;
@@ -650,3 +712,52 @@ const struct pa_sample_spec* pa_stream_get_sample_spec(struct pa_stream *s) {
assert(s);
return &s->sample_spec;
}
+
+void pa_stream_trash_ipol(struct pa_stream *s) {
+ assert(s);
+
+ if (!s->interpolate)
+ return;
+
+ memset(&s->ipol_timestamp, 0, sizeof(s->ipol_timestamp));
+ s->ipol_usec = 0;
+}
+
+pa_usec_t pa_stream_get_interpolated_time(struct pa_stream *s) {
+ pa_usec_t usec;
+ assert(s && s->interpolate);
+
+ if (s->corked)
+ usec = s->ipol_usec;
+ else {
+ if (s->ipol_timestamp.tv_sec == 0)
+ usec = 0;
+ else
+ usec = s->ipol_usec + pa_timeval_age(&s->ipol_timestamp);
+ }
+
+ if (usec < s->previous_time)
+ usec = s->previous_time;
+
+ s->previous_time = usec;
+ return usec;
+}
+
+pa_usec_t pa_stream_get_interpolated_latency(struct pa_stream *s, int *negative) {
+ pa_usec_t t, c;
+ assert(s && s->interpolate);
+
+ t = pa_stream_get_interpolated_time(s);
+ c = pa_bytes_to_usec(s->counter, &s->sample_spec);
+
+ if (t <= c) {
+ if (negative)
+ *negative = 1;
+
+ return c-t;
+ } else {
+ if (negative)
+ *negative = 0;
+ return t-c;
+ }
+}