+ return pa_operation_ref(o);
+}
+
+uint64_t pa_stream_get_counter(struct pa_stream *s) {
+ assert(s);
+ return s->counter;
+}
+
+pa_usec_t pa_stream_get_time(struct pa_stream *s, const struct pa_latency_info *i) {
+ pa_usec_t usec;
+ assert(s);
+
+ usec = pa_bytes_to_usec(i->counter, &s->sample_spec);
+
+ if (i) {
+ if (s->direction == PA_STREAM_PLAYBACK) {
+ pa_usec_t latency = i->transport_usec + i->buffer_usec + i->sink_usec;
+ if (usec < latency)
+ usec = 0;
+ else
+ usec -= latency;
+
+ } else if (s->direction == PA_STREAM_RECORD) {
+ usec += i->source_usec + i->buffer_usec + i->transport_usec;
+
+ if (usec > i->sink_usec)
+ usec -= i->sink_usec;
+ else
+ usec = 0;
+ }
+ }
+
+ if (usec < s->previous_time)
+ usec = s->previous_time;
+
+ s->previous_time = usec;
+
+ return usec;
+}
+
+static pa_usec_t time_counter_diff(struct pa_stream *s, pa_usec_t t, pa_usec_t c, int *negative) {
+ assert(s);
+
+ if (negative)
+ *negative = 0;
+
+ if (c < t) {
+ if (s->direction == PA_STREAM_RECORD) {
+ if (negative)
+ *negative = 1;
+
+ return t-c;
+ } else
+ return 0;
+ } else
+ return c-t;
+}
+
+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);
+
+ t = pa_stream_get_time(s, i);
+ c = pa_bytes_to_usec(s->counter, &s->sample_spec);
+
+ return time_counter_diff(s, t, c, negative);
+}
+
+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);
+
+ return time_counter_diff(s, t, c, negative);