[pulseaudio-discuss] [PATCH 10/22 v2] source/sink: Allow pa_{source, sink}_get_latency_within_thread() to return negative values

Georg Chini georg at chini.tk
Tue Feb 14 14:00:06 UTC 2017


The reported latency of source or sink is based on measured initial conditions.
If the conditions contain an error, the estimated latency values may become negative.
This does not indicate that the latency is indeed negative but can be considered
merely an offset error. The current get_latency_in_thread() calls and the
implementations of the PA_{SINK,SOURCE}_MESSAGE_GET_LATENCY messages truncate negative
latencies because they do not make sense from a physical point of view. In fact,
the values are truncated twice, once in the message handler and a second time in
the pa_{source,sink}_get_latency_within_thread() call itself.
This leads to two problems for the latency controller:

- Truncating leads to discontinuities in the latency reports which then trigger
  unwanted end to end latency corrections
- If a large negative port latency offsets is set, the reported latency is always 0,
  making it impossible to control the end to end latency at all

This patch adds a flag to pa_{sink,source}_get_latency_within_thread() to allow
negative return values. Truncating is also removed in all implementations of the
PA_{SINK,SOURCE}_MESSAGE_GET_LATENCY message handlers. The allow_negative flag
is set to false for all calls of pa_{sink,source}_get_latency_within_thread().
This means that the original behavior is not altered in most cases, only if
a positive latency offset is set and the message returns a negative value, the
reported latency is smaller because the values are not truncated twice.

---
After a change module-bluez5-device the original patch no longer applies cleanly.

 src/modules/alsa/alsa-sink.c                 | 13 +++++--------
 src/modules/alsa/alsa-source.c               |  8 ++++----
 src/modules/bluetooth/module-bluez4-device.c | 16 ++++++++--------
 src/modules/bluetooth/module-bluez5-device.c | 10 +++++-----
 src/modules/echo-cancel/module-echo-cancel.c | 16 ++++++++--------
 src/modules/jack/module-jack-sink.c          | 12 +++++++++---
 src/modules/jack/module-jack-source.c        |  2 +-
 src/modules/macosx/module-coreaudio-device.c |  4 ++--
 src/modules/module-combine-sink.c            |  5 +----
 src/modules/module-equalizer-sink.c          |  6 +++---
 src/modules/module-esound-sink.c             |  2 +-
 src/modules/module-ladspa-sink.c             |  6 +++---
 src/modules/module-loopback.c                | 10 +++++-----
 src/modules/module-null-sink.c               |  2 +-
 src/modules/module-null-source.c             |  2 +-
 src/modules/module-pipe-sink.c               |  2 +-
 src/modules/module-pipe-source.c             |  2 +-
 src/modules/module-remap-sink.c              |  6 +++---
 src/modules/module-remap-source.c            |  6 +++---
 src/modules/module-sine-source.c             |  2 +-
 src/modules/module-solaris.c                 |  2 +-
 src/modules/module-tunnel-sink-new.c         | 10 +++++-----
 src/modules/module-tunnel-source-new.c       | 12 ++++++------
 src/modules/module-tunnel.c                  |  4 ++--
 src/modules/module-virtual-sink.c            |  8 ++++----
 src/modules/module-virtual-source.c          |  6 +++---
 src/modules/module-virtual-surround-sink.c   |  6 +++---
 src/modules/module-waveout.c                 |  2 +-
 src/modules/oss/module-oss.c                 |  2 +-
 src/modules/raop/raop-sink.c                 | 11 +++++------
 src/modules/rtp/module-rtp-recv.c            |  2 +-
 src/pulsecore/protocol-native.c              |  6 +++---
 src/pulsecore/sink-input.c                   |  2 +-
 src/pulsecore/sink.c                         | 26 ++++++++++++--------------
 src/pulsecore/sink.h                         |  2 +-
 src/pulsecore/source-output.c                |  4 ++--
 src/pulsecore/source.c                       | 22 ++++++++++------------
 src/pulsecore/source.h                       |  2 +-
 38 files changed, 128 insertions(+), 133 deletions(-)

diff --git a/src/modules/alsa/alsa-sink.c b/src/modules/alsa/alsa-sink.c
index 886c735..b6d9a42 100644
--- a/src/modules/alsa/alsa-sink.c
+++ b/src/modules/alsa/alsa-sink.c
@@ -892,8 +892,7 @@ static void update_smoother(struct userdata *u) {
     u->smoother_interval = PA_MIN (u->smoother_interval * 2, SMOOTHER_MAX_INTERVAL);
 }
 
-static pa_usec_t sink_get_latency(struct userdata *u) {
-    pa_usec_t r;
+static int64_t sink_get_latency(struct userdata *u) {
     int64_t delay;
     pa_usec_t now1, now2;
 
@@ -904,12 +903,10 @@ static pa_usec_t sink_get_latency(struct userdata *u) {
 
     delay = (int64_t) pa_bytes_to_usec(u->write_count, &u->sink->sample_spec) - (int64_t) now2;
 
-    r = delay >= 0 ? (pa_usec_t) delay : 0;
-
     if (u->memchunk.memblock)
-        r += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
+        delay += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
 
-    return r;
+    return delay;
 }
 
 static int build_pollfd(struct userdata *u) {
@@ -1150,12 +1147,12 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
     switch (code) {
 
         case PA_SINK_MESSAGE_GET_LATENCY: {
-            pa_usec_t r = 0;
+            int64_t r = 0;
 
             if (u->pcm_handle)
                 r = sink_get_latency(u);
 
-            *((pa_usec_t*) data) = r;
+            *((int64_t*) data) = r;
 
             return 0;
         }
diff --git a/src/modules/alsa/alsa-source.c b/src/modules/alsa/alsa-source.c
index b788df2..6bec188 100644
--- a/src/modules/alsa/alsa-source.c
+++ b/src/modules/alsa/alsa-source.c
@@ -809,7 +809,7 @@ static void update_smoother(struct userdata *u) {
     u->smoother_interval = PA_MIN (u->smoother_interval * 2, SMOOTHER_MAX_INTERVAL);
 }
 
-static pa_usec_t source_get_latency(struct userdata *u) {
+static int64_t source_get_latency(struct userdata *u) {
     int64_t delay;
     pa_usec_t now1, now2;
 
@@ -820,7 +820,7 @@ static pa_usec_t source_get_latency(struct userdata *u) {
 
     delay = (int64_t) now2 - (int64_t) pa_bytes_to_usec(u->read_count, &u->source->sample_spec);
 
-    return delay >= 0 ? (pa_usec_t) delay : 0;
+    return delay;
 }
 
 static int build_pollfd(struct userdata *u) {
@@ -1032,12 +1032,12 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
     switch (code) {
 
         case PA_SOURCE_MESSAGE_GET_LATENCY: {
-            pa_usec_t r = 0;
+            int64_t r = 0;
 
             if (u->pcm_handle)
                 r = source_get_latency(u);
 
-            *((pa_usec_t*) data) = r;
+            *((int64_t*) data) = r;
 
             return 0;
         }
diff --git a/src/modules/bluetooth/module-bluez4-device.c b/src/modules/bluetooth/module-bluez4-device.c
index ac4ed63..7fb8905 100644
--- a/src/modules/bluetooth/module-bluez4-device.c
+++ b/src/modules/bluetooth/module-bluez4-device.c
@@ -430,22 +430,22 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
         case PA_SINK_MESSAGE_GET_LATENCY: {
 
             if (u->read_smoother) {
-                pa_usec_t wi, ri;
+                int64_t wi, ri;
 
                 ri = pa_smoother_get(u->read_smoother, pa_rtclock_now());
                 wi = pa_bytes_to_usec(u->write_index + u->write_block_size, &u->sample_spec);
 
-                *((pa_usec_t*) data) = wi > ri ? wi - ri : 0;
+                *((int64_t*) data) = wi - ri;
             } else {
-                pa_usec_t ri, wi;
+                int64_t ri, wi;
 
                 ri = pa_rtclock_now() - u->started_at;
                 wi = pa_bytes_to_usec(u->write_index, &u->sample_spec);
 
-                *((pa_usec_t*) data) = wi > ri ? wi - ri : 0;
+                *((int64_t*) data) = wi - ri;
             }
 
-            *((pa_usec_t*) data) += u->sink->thread_info.fixed_latency;
+            *((int64_t*) data) += u->sink->thread_info.fixed_latency;
             return 0;
         }
     }
@@ -507,15 +507,15 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
             break;
 
         case PA_SOURCE_MESSAGE_GET_LATENCY: {
-            pa_usec_t wi, ri;
+            int64_t wi, ri;
 
             if (u->read_smoother) {
                 wi = pa_smoother_get(u->read_smoother, pa_rtclock_now());
                 ri = pa_bytes_to_usec(u->read_index, &u->sample_spec);
 
-                *((pa_usec_t*) data) = (wi > ri ? wi - ri : 0) + u->source->thread_info.fixed_latency;
+                *((int64_t*) data) = wi - ri + u->source->thread_info.fixed_latency;
             } else
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
 
             return 0;
         }
diff --git a/src/modules/bluetooth/module-bluez5-device.c b/src/modules/bluetooth/module-bluez5-device.c
index 99b5c3f..10e603d 100644
--- a/src/modules/bluetooth/module-bluez5-device.c
+++ b/src/modules/bluetooth/module-bluez5-device.c
@@ -888,15 +888,15 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
             break;
 
         case PA_SOURCE_MESSAGE_GET_LATENCY: {
-            pa_usec_t wi, ri;
+            int64_t wi, ri;
 
             if (u->read_smoother) {
                 wi = pa_smoother_get(u->read_smoother, pa_rtclock_now());
                 ri = pa_bytes_to_usec(u->read_index, &u->sample_spec);
 
-                *((pa_usec_t*) data) = FIXED_LATENCY_RECORD_A2DP + wi > ri ? FIXED_LATENCY_RECORD_A2DP + wi - ri : 0;
+                *((int64_t*) data) = FIXED_LATENCY_RECORD_A2DP + wi - ri;
             } else
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
 
             return 0;
         }
@@ -1050,7 +1050,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
             break;
 
         case PA_SINK_MESSAGE_GET_LATENCY: {
-            pa_usec_t wi, ri;
+            int64_t wi, ri;
 
             if (u->read_smoother) {
                 ri = pa_smoother_get(u->read_smoother, pa_rtclock_now());
@@ -1060,7 +1060,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
                 wi = pa_bytes_to_usec(u->write_index, &u->sample_spec);
             }
 
-            *((pa_usec_t*) data) = u->sink->thread_info.fixed_latency + wi > ri ? u->sink->thread_info.fixed_latency + wi - ri : 0;
+            *((int64_t*) data) = u->sink->thread_info.fixed_latency + wi - ri;
 
             return 0;
         }
diff --git a/src/modules/echo-cancel/module-echo-cancel.c b/src/modules/echo-cancel/module-echo-cancel.c
index dfd05b6..bd46ede 100644
--- a/src/modules/echo-cancel/module-echo-cancel.c
+++ b/src/modules/echo-cancel/module-echo-cancel.c
@@ -408,14 +408,14 @@ static int source_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t
              * source output is first shut down, the source second. */
             if (!PA_SOURCE_IS_LINKED(u->source->thread_info.state) ||
                 !PA_SOURCE_OUTPUT_IS_LINKED(u->source_output->thread_info.state)) {
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
                 return 0;
             }
 
-            *((pa_usec_t*) data) =
+            *((int64_t*) data) =
 
                 /* Get the latency of the master source */
-                pa_source_get_latency_within_thread(u->source_output->source) +
+                pa_source_get_latency_within_thread(u->source_output->source, true) +
                 /* Add the latency internal to our source output on top */
                 pa_bytes_to_usec(pa_memblockq_get_length(u->source_output->thread_info.delay_memblockq), &u->source_output->source->sample_spec) +
                 /* and the buffering we do on the source */
@@ -444,14 +444,14 @@ static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t of
              * sink input is first shut down, the sink second. */
             if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
                 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
                 return 0;
             }
 
-            *((pa_usec_t*) data) =
+            *((int64_t*) data) =
 
                 /* Get the latency of the master sink */
-                pa_sink_get_latency_within_thread(u->sink_input->sink) +
+                pa_sink_get_latency_within_thread(u->sink_input->sink, true) +
 
                 /* Add the latency internal to our sink input on top */
                 pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
@@ -1010,7 +1010,7 @@ static void source_output_snapshot_within_thread(struct userdata *u, struct snap
     pa_usec_t now, latency;
 
     now = pa_rtclock_now();
-    latency = pa_source_get_latency_within_thread(u->source_output->source);
+    latency = pa_source_get_latency_within_thread(u->source_output->source, false);
     delay = pa_memblockq_get_length(u->source_output->thread_info.delay_memblockq);
 
     delay = (u->source_output->thread_info.resampler ? pa_resampler_request(u->source_output->thread_info.resampler, delay) : delay);
@@ -1089,7 +1089,7 @@ static int sink_input_process_msg_cb(pa_msgobject *obj, int code, void *data, in
             pa_sink_input_assert_io_context(u->sink_input);
 
             now = pa_rtclock_now();
-            latency = pa_sink_get_latency_within_thread(u->sink_input->sink);
+            latency = pa_sink_get_latency_within_thread(u->sink_input->sink, false);
             delay = pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq);
 
             delay = (u->sink_input->thread_info.resampler ? pa_resampler_request(u->sink_input->thread_info.resampler, delay) : delay);
diff --git a/src/modules/jack/module-jack-sink.c b/src/modules/jack/module-jack-sink.c
index 4d31493..7405386 100644
--- a/src/modules/jack/module-jack-sink.c
+++ b/src/modules/jack/module-jack-sink.c
@@ -168,6 +168,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
             jack_nframes_t l, ft, d;
             jack_latency_range_t r;
             size_t n;
+            int32_t number_of_frames;
 
             /* This is the "worst-case" latency */
             jack_port_get_latency_range(u->port[0], JackPlaybackLatency, &r);
@@ -179,12 +180,17 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
 
                 ft = jack_frame_time(u->client);
                 d = ft > u->saved_frame_time ? ft - u->saved_frame_time : 0;
-                l = l > d ? l - d : 0;
+                number_of_frames = (int32_t)l - d;
             }
 
             /* Convert it to usec */
-            n = l * pa_frame_size(&u->sink->sample_spec);
-            *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
+            if (number_of_frames > 0) {
+                n = number_of_frames * pa_frame_size(&u->sink->sample_spec);
+                *((int64_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
+            } else {
+                n = - number_of_frames * pa_frame_size(&u->sink->sample_spec);
+                *((int64_t*) data) = - (int64_t)pa_bytes_to_usec(n, &u->sink->sample_spec);
+            }
 
             return 0;
         }
diff --git a/src/modules/jack/module-jack-source.c b/src/modules/jack/module-jack-source.c
index e45f304..1f020c0 100644
--- a/src/modules/jack/module-jack-source.c
+++ b/src/modules/jack/module-jack-source.c
@@ -141,7 +141,7 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
 
             /* Convert it to usec */
             n = l * pa_frame_size(&u->source->sample_spec);
-            *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
+            *((int64_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
 
             return 0;
         }
diff --git a/src/modules/macosx/module-coreaudio-device.c b/src/modules/macosx/module-coreaudio-device.c
index 502fc51..d9c453f 100644
--- a/src/modules/macosx/module-coreaudio-device.c
+++ b/src/modules/macosx/module-coreaudio-device.c
@@ -305,7 +305,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
         }
 
         case PA_SINK_MESSAGE_GET_LATENCY: {
-            *((pa_usec_t *) data) = get_latency_us(PA_OBJECT(o));
+            *((int64_t *) data) = get_latency_us(PA_OBJECT(o));
             return 0;
         }
     }
@@ -343,7 +343,7 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
         }
 
         case PA_SOURCE_MESSAGE_GET_LATENCY: {
-            *((pa_usec_t *) data) = get_latency_us(PA_OBJECT(o));
+            *((int64_t *) data) = get_latency_us(PA_OBJECT(o));
             return 0;
         }
     }
diff --git a/src/modules/module-combine-sink.c b/src/modules/module-combine-sink.c
index 250240a..e1554fb 100644
--- a/src/modules/module-combine-sink.c
+++ b/src/modules/module-combine-sink.c
@@ -877,10 +877,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
 
             c = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec);
 
-            if (y < c)
-                *delay = c - y;
-            else
-                *delay = 0;
+            *delay = (int64_t)c - y;
 
             return 0;
         }
diff --git a/src/modules/module-equalizer-sink.c b/src/modules/module-equalizer-sink.c
index 9c25f3f..068db70 100644
--- a/src/modules/module-equalizer-sink.c
+++ b/src/modules/module-equalizer-sink.c
@@ -251,13 +251,13 @@ static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t of
              * sink input is first shut down, the sink second. */
             if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
                 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
                 return 0;
             }
 
-            *((pa_usec_t*) data) =
+            *((int64_t*) data) =
                 /* Get the latency of the master sink */
-                pa_sink_get_latency_within_thread(u->sink_input->sink) +
+                pa_sink_get_latency_within_thread(u->sink_input->sink, true) +
 
                 /* Add the latency internal to our sink input on top */
                 pa_bytes_to_usec(pa_memblockq_get_length(u->output_q) +
diff --git a/src/modules/module-esound-sink.c b/src/modules/module-esound-sink.c
index 2ce0c85..59bef48 100644
--- a/src/modules/module-esound-sink.c
+++ b/src/modules/module-esound-sink.c
@@ -175,7 +175,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
             r = pa_smoother_get(u->smoother, pa_rtclock_now());
             w = pa_bytes_to_usec((uint64_t) u->offset + u->memchunk.length, &u->sink->sample_spec);
 
-            *((pa_usec_t*) data) = w > r ? w - r : 0;
+            *((int64_t*) data) = (int64_t)w - r;
             return 0;
         }
 
diff --git a/src/modules/module-ladspa-sink.c b/src/modules/module-ladspa-sink.c
index c11fa5e..36240bc 100644
--- a/src/modules/module-ladspa-sink.c
+++ b/src/modules/module-ladspa-sink.c
@@ -342,14 +342,14 @@ static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t of
          * sink input is first shut down, the sink second. */
         if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
                 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
-            *((pa_usec_t*) data) = 0;
+            *((int64_t*) data) = 0;
             return 0;
         }
 
-        *((pa_usec_t*) data) =
+        *((int64_t*) data) =
 
             /* Get the latency of the master sink */
-            pa_sink_get_latency_within_thread(u->sink_input->sink) +
+            pa_sink_get_latency_within_thread(u->sink_input->sink, true) +
 
             /* Add the latency internal to our sink input on top */
             pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
diff --git a/src/modules/module-loopback.c b/src/modules/module-loopback.c
index b84a26a..f505295 100644
--- a/src/modules/module-loopback.c
+++ b/src/modules/module-loopback.c
@@ -508,7 +508,7 @@ static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk)
 
     /* Send current source latency and timestamp with the message */
     push_time = pa_rtclock_now();
-    current_source_latency = pa_source_get_latency_within_thread(u->source_output->source);
+    current_source_latency = pa_source_get_latency_within_thread(u->source_output->source, false);
 
     pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_POST, PA_UINT_TO_PTR(current_source_latency), push_time, chunk, NULL);
     u->send_counter += (int64_t) chunk->length;
@@ -539,7 +539,7 @@ static int source_output_process_msg_cb(pa_msgobject *obj, int code, void *data,
 
             u->latency_snapshot.send_counter = u->send_counter;
             /* Add content of delay memblockq to the source latency */
-            u->latency_snapshot.source_latency = pa_source_get_latency_within_thread(u->source_output->source) +
+            u->latency_snapshot.source_latency = pa_source_get_latency_within_thread(u->source_output->source, false) +
                                                  pa_bytes_to_usec(length, &u->source_output->source->sample_spec);
             u->latency_snapshot.source_timestamp = pa_rtclock_now();
 
@@ -819,7 +819,7 @@ static int sink_input_process_msg_cb(pa_msgobject *obj, int code, void *data, in
 
                 time_delta = PA_PTR_TO_UINT(data);
                 time_delta += pa_rtclock_now() - offset;
-                time_delta += pa_sink_get_latency_within_thread(u->sink_input->sink);
+                time_delta += pa_sink_get_latency_within_thread(u->sink_input->sink, false);
 
                 /* If the source has overrun, assume that the maximum it should have pushed is
                  * one full source latency. It may still be possible that the next push also
@@ -838,7 +838,7 @@ static int sink_input_process_msg_cb(pa_msgobject *obj, int code, void *data, in
             /* If pop has not been called yet, make sure the latency does not grow too much.
              * Don't push any silence here, because we already have new data in the queue */
             if (!u->output_thread_info.pop_called)
-                 memblockq_adjust(u, pa_sink_get_latency_within_thread(u->sink_input->sink), false);
+                 memblockq_adjust(u, pa_sink_get_latency_within_thread(u->sink_input->sink, false), false);
 
             /* Is this the end of an underrun? Then let's start things
              * right-away */
@@ -876,7 +876,7 @@ static int sink_input_process_msg_cb(pa_msgobject *obj, int code, void *data, in
             u->latency_snapshot.recv_counter = u->output_thread_info.recv_counter;
             u->latency_snapshot.loopback_memblockq_length = pa_memblockq_get_length(u->memblockq);
             /* Add content of render memblockq to sink latency */
-            u->latency_snapshot.sink_latency = pa_sink_get_latency_within_thread(u->sink_input->sink) +
+            u->latency_snapshot.sink_latency = pa_sink_get_latency_within_thread(u->sink_input->sink, false) +
                                                pa_bytes_to_usec(length, &u->sink_input->sink->sample_spec);
             u->latency_snapshot.sink_timestamp = pa_rtclock_now();
 
diff --git a/src/modules/module-null-sink.c b/src/modules/module-null-sink.c
index b8157e8..9237656 100644
--- a/src/modules/module-null-sink.c
+++ b/src/modules/module-null-sink.c
@@ -104,7 +104,7 @@ static int sink_process_msg(
             pa_usec_t now;
 
             now = pa_rtclock_now();
-            *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0ULL;
+            *((int64_t*) data) = (int64_t)u->timestamp - (int64_t)now;
 
             return 0;
         }
diff --git a/src/modules/module-null-source.c b/src/modules/module-null-source.c
index a75a04f..5bfa1e1 100644
--- a/src/modules/module-null-source.c
+++ b/src/modules/module-null-source.c
@@ -102,7 +102,7 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
             pa_usec_t now;
 
             now = pa_rtclock_now();
-            *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0;
+            *((int64_t*) data) = (int64_t)u->timestamp - (int64_t)now;
 
             return 0;
         }
diff --git a/src/modules/module-pipe-sink.c b/src/modules/module-pipe-sink.c
index da65021..a55bd98 100644
--- a/src/modules/module-pipe-sink.c
+++ b/src/modules/module-pipe-sink.c
@@ -111,7 +111,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
 
             n += u->memchunk.length;
 
-            *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
+            *((int64_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
             return 0;
         }
     }
diff --git a/src/modules/module-pipe-source.c b/src/modules/module-pipe-source.c
index f39fc55..411f1fd 100644
--- a/src/modules/module-pipe-source.c
+++ b/src/modules/module-pipe-source.c
@@ -113,7 +113,7 @@ static int source_process_msg(
                 n = (size_t) l;
 #endif
 
-            *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
+            *((int64_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
             return 0;
         }
     }
diff --git a/src/modules/module-remap-sink.c b/src/modules/module-remap-sink.c
index 8a21c3c..6ed2747 100644
--- a/src/modules/module-remap-sink.c
+++ b/src/modules/module-remap-sink.c
@@ -84,13 +84,13 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
              * make sure we don't access it yet */
             if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
                 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
                 return 0;
             }
 
-            *((pa_usec_t*) data) =
+            *((int64_t*) data) =
                 /* Get the latency of the master sink */
-                pa_sink_get_latency_within_thread(u->sink_input->sink) +
+                pa_sink_get_latency_within_thread(u->sink_input->sink, true) +
 
                 /* Add the latency internal to our sink input on top */
                 pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
diff --git a/src/modules/module-remap-source.c b/src/modules/module-remap-source.c
index 9b62c51..b8c8792 100644
--- a/src/modules/module-remap-source.c
+++ b/src/modules/module-remap-source.c
@@ -92,14 +92,14 @@ static int source_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t
              * source output is first shut down, the source second. */
             if (!PA_SOURCE_IS_LINKED(u->source->thread_info.state) ||
                 !PA_SOURCE_OUTPUT_IS_LINKED(u->source_output->thread_info.state)) {
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
                 return 0;
             }
 
-            *((pa_usec_t*) data) =
+            *((int64_t*) data) =
 
                 /* Get the latency of the master source */
-                pa_source_get_latency_within_thread(u->source_output->source) +
+                pa_source_get_latency_within_thread(u->source_output->source, true) +
                 /* Add the latency internal to our source output on top */
                 pa_bytes_to_usec(pa_memblockq_get_length(u->source_output->thread_info.delay_memblockq), &u->source_output->source->sample_spec);
 
diff --git a/src/modules/module-sine-source.c b/src/modules/module-sine-source.c
index cdeb2c0..3f275e2 100644
--- a/src/modules/module-sine-source.c
+++ b/src/modules/module-sine-source.c
@@ -102,7 +102,7 @@ static int source_process_msg(
             now = pa_rtclock_now();
             left_to_fill = u->timestamp > now ? u->timestamp - now : 0ULL;
 
-            *((pa_usec_t*) data) = u->block_usec > left_to_fill ? u->block_usec - left_to_fill : 0ULL;
+            *((int64_t*) data) = (int64_t)u->block_usec - left_to_fill;
 
             return 0;
         }
diff --git a/src/modules/module-solaris.c b/src/modules/module-solaris.c
index ccff69f..fee69e7 100644
--- a/src/modules/module-solaris.c
+++ b/src/modules/module-solaris.c
@@ -385,7 +385,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
     switch (code) {
 
         case PA_SINK_MESSAGE_GET_LATENCY:
-            *((pa_usec_t*) data) = sink_get_latency(u, &PA_SINK(o)->sample_spec);
+            *((int64_t*) data) = sink_get_latency(u, &PA_SINK(o)->sample_spec);
             return 0;
 
         case PA_SINK_MESSAGE_SET_STATE:
diff --git a/src/modules/module-tunnel-sink-new.c b/src/modules/module-tunnel-sink-new.c
index 92f99df..dd6c886 100644
--- a/src/modules/module-tunnel-sink-new.c
+++ b/src/modules/module-tunnel-sink-new.c
@@ -407,26 +407,26 @@ static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t of
             pa_usec_t remote_latency;
 
             if (!PA_SINK_IS_LINKED(u->sink->thread_info.state)) {
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
                 return 0;
             }
 
             if (!u->stream) {
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
                 return 0;
             }
 
             if (pa_stream_get_state(u->stream) != PA_STREAM_READY) {
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
                 return 0;
             }
 
             if (pa_stream_get_latency(u->stream, &remote_latency, &negative) < 0) {
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
                 return 0;
             }
 
-            *((pa_usec_t*) data) = remote_latency;
+            *((int64_t*) data) = remote_latency;
             return 0;
         }
         case PA_SINK_MESSAGE_SET_STATE:
diff --git a/src/modules/module-tunnel-source-new.c b/src/modules/module-tunnel-source-new.c
index e159c33..2db928c 100644
--- a/src/modules/module-tunnel-source-new.c
+++ b/src/modules/module-tunnel-source-new.c
@@ -402,29 +402,29 @@ static int source_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t
             pa_usec_t remote_latency;
 
             if (!PA_SOURCE_IS_LINKED(u->source->thread_info.state)) {
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
                 return 0;
             }
 
             if (!u->stream) {
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
                 return 0;
             }
 
             if (pa_stream_get_state(u->stream) != PA_STREAM_READY) {
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
                 return 0;
             }
 
             if (pa_stream_get_latency(u->stream, &remote_latency, &negative) < 0) {
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
                 return 0;
             }
 
             if (negative)
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = - (int64_t)remote_latency;
             else
-                *((pa_usec_t*) data) = remote_latency;
+                *((int64_t*) data) = remote_latency;
 
             return 0;
         }
diff --git a/src/modules/module-tunnel.c b/src/modules/module-tunnel.c
index e08816b..db06a01 100644
--- a/src/modules/module-tunnel.c
+++ b/src/modules/module-tunnel.c
@@ -518,7 +518,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
             yl = pa_bytes_to_usec((uint64_t) u->counter, &u->sink->sample_spec);
             yr = pa_smoother_get(u->smoother, pa_rtclock_now());
 
-            *usec = yl > yr ? yl - yr : 0;
+            *usec = (int64_t)yl - yr;
             return 0;
         }
 
@@ -623,7 +623,7 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
             yl = pa_bytes_to_usec((uint64_t) u->counter, &PA_SOURCE(o)->sample_spec);
             yr = pa_smoother_get(u->smoother, pa_rtclock_now());
 
-            *usec = yr > yl ? yr - yl : 0;
+            *usec = (int64_t)yr - yl;
             return 0;
         }
 
diff --git a/src/modules/module-virtual-sink.c b/src/modules/module-virtual-sink.c
index 02cc1ac..f779b3c 100644
--- a/src/modules/module-virtual-sink.c
+++ b/src/modules/module-virtual-sink.c
@@ -95,14 +95,14 @@ static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t of
              * sink input is first shut down, the sink second. */
             if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
                 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
                 return 0;
             }
 
-            *((pa_usec_t*) data) =
+            *((int64_t*) data) =
 
                 /* Get the latency of the master sink */
-                pa_sink_get_latency_within_thread(u->sink_input->sink) +
+                pa_sink_get_latency_within_thread(u->sink_input->sink, true) +
 
                 /* Add the latency internal to our sink input on top */
                 pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
@@ -255,7 +255,7 @@ static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk
     /* (4) IF YOU NEED THE LATENCY FOR SOMETHING ACQUIRE IT LIKE THIS: */
     current_latency =
         /* Get the latency of the master sink */
-        pa_sink_get_latency_within_thread(i->sink) +
+        pa_sink_get_latency_within_thread(i->sink, false) +
 
         /* Add the latency internal to our sink input on top */
         pa_bytes_to_usec(pa_memblockq_get_length(i->thread_info.render_memblockq), &i->sink->sample_spec);
diff --git a/src/modules/module-virtual-source.c b/src/modules/module-virtual-source.c
index 36edf78..0b5339c 100644
--- a/src/modules/module-virtual-source.c
+++ b/src/modules/module-virtual-source.c
@@ -104,7 +104,7 @@ static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t of
         case PA_SINK_MESSAGE_GET_LATENCY:
 
             /* there's no real latency here */
-            *((pa_usec_t*) data) = 0;
+            *((int64_t*) data) = 0;
 
             return 0;
     }
@@ -183,7 +183,7 @@ static int source_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t
             *((pa_usec_t*) data) =
 
                 /* Get the latency of the master source */
-                pa_source_get_latency_within_thread(u->source_output->source) +
+                pa_source_get_latency_within_thread(u->source_output->source, true) +
 
                 /* Add the latency internal to our source output on top */
                 /* FIXME, no idea what I am doing here */
@@ -403,7 +403,7 @@ static void source_output_state_change_cb(pa_source_output *o, pa_source_output_
 #if 0
     if (PA_SOURCE_OUTPUT_IS_LINKED(state) && o->thread_info.state == PA_SOURCE_OUTPUT_INIT) {
 
-        u->skip = pa_usec_to_bytes(PA_CLIP_SUB(pa_source_get_latency_within_thread(o->source),
+        u->skip = pa_usec_to_bytes(PA_CLIP_SUB(pa_source_get_latency_within_thread(o->source, false),
                                                u->latency),
                                    &o->sample_spec);
 
diff --git a/src/modules/module-virtual-surround-sink.c b/src/modules/module-virtual-surround-sink.c
index 6c7120a..6920b02 100644
--- a/src/modules/module-virtual-surround-sink.c
+++ b/src/modules/module-virtual-surround-sink.c
@@ -116,14 +116,14 @@ static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t of
              * sink input is first shut down, the sink second. */
             if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
                 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
                 return 0;
             }
 
-            *((pa_usec_t*) data) =
+            *((int64_t*) data) =
 
                 /* Get the latency of the master sink */
-                pa_sink_get_latency_within_thread(u->sink_input->sink) +
+                pa_sink_get_latency_within_thread(u->sink_input->sink, true) +
 
                 /* Add the latency internal to our sink input on top */
                 pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
diff --git a/src/modules/module-waveout.c b/src/modules/module-waveout.c
index 0b219f1..d4882d3 100644
--- a/src/modules/module-waveout.c
+++ b/src/modules/module-waveout.c
@@ -369,7 +369,7 @@ static int process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa
                 pa_usec_t r = 0;
                 if (u->hwo)
                     r = sink_get_latency(u);
-                *((pa_usec_t*) data) = r;
+                *((int64_t*) data) = r;
                 return 0;
             }
 
diff --git a/src/modules/oss/module-oss.c b/src/modules/oss/module-oss.c
index 8a5a692..fd93770 100644
--- a/src/modules/oss/module-oss.c
+++ b/src/modules/oss/module-oss.c
@@ -659,7 +659,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
                     r = io_sink_get_latency(u);
             }
 
-            *((pa_usec_t*) data) = r;
+            *((int64_t*) data) = r;
 
             return 0;
         }
diff --git a/src/modules/raop/raop-sink.c b/src/modules/raop/raop-sink.c
index 7d8fe36..c7410e9 100644
--- a/src/modules/raop/raop-sink.c
+++ b/src/modules/raop/raop-sink.c
@@ -107,8 +107,8 @@ static void raop_state_cb(pa_raop_state_t state, void *userdata) {
     pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), PA_SINK_MESSAGE_SET_RAOP_STATE, PA_INT_TO_PTR(state), 0, NULL, NULL);
 }
 
-static pa_usec_t sink_get_latency(const struct userdata *u) {
-    pa_usec_t r, now;
+static int64_t sink_get_latency(const struct userdata *u) {
+    pa_usec_t now;
     int64_t latency;
 
     pa_assert(u);
@@ -118,9 +118,8 @@ static pa_usec_t sink_get_latency(const struct userdata *u) {
     now = pa_smoother_get(u->smoother, now);
 
     latency = pa_bytes_to_usec(u->write_count, &u->sink->sample_spec) - (int64_t) now;
-    r = latency >= 0 ? (pa_usec_t) latency : 0;
 
-    return r;
+    return latency;
 }
 
 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
@@ -190,12 +189,12 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
         }
 
         case PA_SINK_MESSAGE_GET_LATENCY: {
-            pa_usec_t r = 0;
+            int64_t r = 0;
 
             if (pa_raop_client_can_stream(u->raop))
                 r = sink_get_latency(u);
 
-            *((pa_usec_t*) data) = r;
+            *((int64_t*) data) = r;
 
             return 0;
         }
diff --git a/src/modules/rtp/module-rtp-recv.c b/src/modules/rtp/module-rtp-recv.c
index 5977500..f512a8a 100644
--- a/src/modules/rtp/module-rtp-recv.c
+++ b/src/modules/rtp/module-rtp-recv.c
@@ -296,7 +296,7 @@ static int rtpoll_work_cb(pa_rtpoll_item *i) {
 
         pa_log_debug("wi=%lu ri=%lu", (unsigned long) wi, (unsigned long) ri);
 
-        sink_delay = pa_sink_get_latency_within_thread(s->sink_input->sink);
+        sink_delay = pa_sink_get_latency_within_thread(s->sink_input->sink, false);
         render_delay = pa_bytes_to_usec(pa_memblockq_get_length(s->sink_input->thread_info.render_memblockq), &s->sink_input->sink->sample_spec);
 
         if (ri > render_delay+sink_delay)
diff --git a/src/pulsecore/protocol-native.c b/src/pulsecore/protocol-native.c
index 13f4f62..353219c 100644
--- a/src/pulsecore/protocol-native.c
+++ b/src/pulsecore/protocol-native.c
@@ -1417,7 +1417,7 @@ static int sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int
             s->read_index = pa_memblockq_get_read_index(s->memblockq);
             s->write_index = pa_memblockq_get_write_index(s->memblockq);
             s->render_memblockq_length = pa_memblockq_get_length(s->sink_input->thread_info.render_memblockq);
-            s->current_sink_latency = pa_sink_get_latency_within_thread(s->sink_input->sink);
+            s->current_sink_latency = pa_sink_get_latency_within_thread(s->sink_input->sink, false);
             s->underrun_for = s->sink_input->thread_info.underrun_for;
             s->playing_for = s->sink_input->thread_info.playing_for;
 
@@ -1686,8 +1686,8 @@ static int source_output_process_msg(pa_msgobject *_o, int code, void *userdata,
     switch (code) {
         case SOURCE_OUTPUT_MESSAGE_UPDATE_LATENCY:
             /* Atomically get a snapshot of all timing parameters... */
-            s->current_monitor_latency = o->source->monitor_of ? pa_sink_get_latency_within_thread(o->source->monitor_of) : 0;
-            s->current_source_latency = pa_source_get_latency_within_thread(o->source);
+            s->current_monitor_latency = o->source->monitor_of ? pa_sink_get_latency_within_thread(o->source->monitor_of, false) : 0;
+            s->current_source_latency = pa_source_get_latency_within_thread(o->source, false);
             s->on_the_fly_snapshot = pa_atomic_load(&s->on_the_fly);
             return 0;
     }
diff --git a/src/pulsecore/sink-input.c b/src/pulsecore/sink-input.c
index b937383..908e407 100644
--- a/src/pulsecore/sink-input.c
+++ b/src/pulsecore/sink-input.c
@@ -2027,7 +2027,7 @@ int pa_sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t
             pa_usec_t *r = userdata;
 
             r[0] += pa_bytes_to_usec(pa_memblockq_get_length(i->thread_info.render_memblockq), &i->sink->sample_spec);
-            r[1] += pa_sink_get_latency_within_thread(i->sink);
+            r[1] += pa_sink_get_latency_within_thread(i->sink, false);
 
             return 0;
         }
diff --git a/src/pulsecore/sink.c b/src/pulsecore/sink.c
index 589bfef..02b186f 100644
--- a/src/pulsecore/sink.c
+++ b/src/pulsecore/sink.c
@@ -1502,7 +1502,7 @@ int pa_sink_update_rate(pa_sink *s, uint32_t rate, bool passthrough) {
 
 /* Called from main thread */
 pa_usec_t pa_sink_get_latency(pa_sink *s) {
-    pa_usec_t usec = 0;
+    int64_t usec = 0;
 
     pa_sink_assert_ref(s);
     pa_assert_ctl_context();
@@ -1520,17 +1520,17 @@ pa_usec_t pa_sink_get_latency(pa_sink *s) {
 
     /* usec is unsigned, so check that the offset can be added to usec without
      * underflowing. */
-    if (-s->port_latency_offset <= (int64_t) usec)
+    if (-s->port_latency_offset <= usec)
         usec += s->port_latency_offset;
     else
         usec = 0;
 
-    return usec;
+    return (pa_usec_t)usec;
 }
 
 /* Called from IO thread */
-pa_usec_t pa_sink_get_latency_within_thread(pa_sink *s) {
-    pa_usec_t usec = 0;
+int64_t pa_sink_get_latency_within_thread(pa_sink *s, bool allow_negative) {
+    int64_t usec = 0;
     pa_msgobject *o;
 
     pa_sink_assert_ref(s);
@@ -1552,11 +1552,9 @@ pa_usec_t pa_sink_get_latency_within_thread(pa_sink *s) {
     if (o->process_msg(o, PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
         return -1;
 
-    /* usec is unsigned, so check that the offset can be added to usec without
-     * underflowing. */
-    if (-s->thread_info.port_latency_offset <= (int64_t) usec)
-        usec += s->thread_info.port_latency_offset;
-    else
+    /* If allow_negative is false, the call should only return positive values, */
+    usec += s->thread_info.port_latency_offset;
+    if (!allow_negative && usec < 0)
         usec = 0;
 
     return usec;
@@ -2630,7 +2628,7 @@ int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offse
                  * same as the read index. */
 
                 /* Get the latency of the sink */
-                usec = pa_sink_get_latency_within_thread(s);
+                usec = pa_sink_get_latency_within_thread(s, false);
                 sink_nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
                 total_nbytes = sink_nbytes + pa_memblockq_get_length(i->thread_info.render_memblockq);
 
@@ -2692,7 +2690,7 @@ int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offse
                  * rewind. */
 
                 /* Get the latency of the sink */
-                usec = pa_sink_get_latency_within_thread(s);
+                usec = pa_sink_get_latency_within_thread(s, false);
                 nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
 
                 if (nbytes > 0)
@@ -3586,7 +3584,7 @@ void pa_sink_volume_change_push(pa_sink *s) {
         return;
     }
 
-    nc->at = pa_sink_get_latency_within_thread(s);
+    nc->at = pa_sink_get_latency_within_thread(s, false);
     nc->at += pa_rtclock_now() + s->thread_info.volume_change_extra_delay;
 
     if (s->thread_info.volume_changes_tail) {
@@ -3696,7 +3694,7 @@ static void pa_sink_volume_change_rewind(pa_sink *s, size_t nbytes) {
     pa_sink_volume_change *c;
     pa_volume_t prev_vol = pa_cvolume_avg(&s->thread_info.current_hw_volume);
     pa_usec_t rewound = pa_bytes_to_usec(nbytes, &s->sample_spec);
-    pa_usec_t limit = pa_sink_get_latency_within_thread(s);
+    pa_usec_t limit = pa_sink_get_latency_within_thread(s, false);
 
     pa_log_debug("latency = %lld", (long long) limit);
     limit += pa_rtclock_now() + s->thread_info.volume_change_extra_delay;
diff --git a/src/pulsecore/sink.h b/src/pulsecore/sink.h
index bc2a200..0e79cf3 100644
--- a/src/pulsecore/sink.h
+++ b/src/pulsecore/sink.h
@@ -526,7 +526,7 @@ void pa_sink_request_rewind(pa_sink*s, size_t nbytes);
 
 void pa_sink_invalidate_requested_latency(pa_sink *s, bool dynamic);
 
-pa_usec_t pa_sink_get_latency_within_thread(pa_sink *s);
+int64_t pa_sink_get_latency_within_thread(pa_sink *s, bool allow_negative);
 
 /* Called from the main thread, from sink-input.c only. The normal way to set
  * the sink reference volume is to call pa_sink_set_volume(), but the flat
diff --git a/src/pulsecore/source-output.c b/src/pulsecore/source-output.c
index 0ba19c8..93eda4a 100644
--- a/src/pulsecore/source-output.c
+++ b/src/pulsecore/source-output.c
@@ -748,7 +748,7 @@ void pa_source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
          * of the queued data is actually still changeable. Hence
          * FIXME! */
 
-        latency = pa_sink_get_latency_within_thread(o->source->monitor_of);
+        latency = pa_sink_get_latency_within_thread(o->source->monitor_of, false);
 
         n = pa_usec_to_bytes(latency, &o->source->sample_spec);
 
@@ -1609,7 +1609,7 @@ int pa_source_output_process_msg(pa_msgobject *mo, int code, void *userdata, int
             pa_usec_t *r = userdata;
 
             r[0] += pa_bytes_to_usec(pa_memblockq_get_length(o->thread_info.delay_memblockq), &o->source->sample_spec);
-            r[1] += pa_source_get_latency_within_thread(o->source);
+            r[1] += pa_source_get_latency_within_thread(o->source, false);
 
             return 0;
         }
diff --git a/src/pulsecore/source.c b/src/pulsecore/source.c
index 1425082..21123fd 100644
--- a/src/pulsecore/source.c
+++ b/src/pulsecore/source.c
@@ -1099,7 +1099,7 @@ int pa_source_update_rate(pa_source *s, uint32_t rate, bool passthrough) {
 
 /* Called from main thread */
 pa_usec_t pa_source_get_latency(pa_source *s) {
-    pa_usec_t usec;
+    int64_t usec;
 
     pa_source_assert_ref(s);
     pa_assert_ctl_context();
@@ -1115,17 +1115,17 @@ pa_usec_t pa_source_get_latency(pa_source *s) {
 
     /* usec is unsigned, so check that the offset can be added to usec without
      * underflowing. */
-    if (-s->port_latency_offset <= (int64_t) usec)
+    if (-s->port_latency_offset <= usec)
         usec += s->port_latency_offset;
     else
         usec = 0;
 
-    return usec;
+    return (pa_usec_t)usec;
 }
 
 /* Called from IO thread */
-pa_usec_t pa_source_get_latency_within_thread(pa_source *s) {
-    pa_usec_t usec = 0;
+int64_t pa_source_get_latency_within_thread(pa_source *s, bool allow_negative) {
+    int64_t usec = 0;
     pa_msgobject *o;
 
     pa_source_assert_ref(s);
@@ -1147,11 +1147,9 @@ pa_usec_t pa_source_get_latency_within_thread(pa_source *s) {
     if (o->process_msg(o, PA_SOURCE_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
         return -1;
 
-    /* usec is unsigned, so check that the offset can be added to usec without
-     * underflowing. */
-    if (-s->thread_info.port_latency_offset <= (int64_t) usec)
-        usec += s->thread_info.port_latency_offset;
-    else
+    /* If allow_negative is false, the call should only return positive values, */
+    usec += s->thread_info.port_latency_offset;
+    if (!allow_negative && usec < 0)
         usec = 0;
 
     return usec;
@@ -2223,7 +2221,7 @@ int pa_source_process_msg(pa_msgobject *object, int code, void *userdata, int64_
         case PA_SOURCE_MESSAGE_GET_LATENCY:
 
             if (s->monitor_of) {
-                *((pa_usec_t*) userdata) = 0;
+                *((int64_t*) userdata) = 0;
                 return 0;
             }
 
@@ -2697,7 +2695,7 @@ void pa_source_volume_change_push(pa_source *s) {
         return;
     }
 
-    nc->at = pa_source_get_latency_within_thread(s);
+    nc->at = pa_source_get_latency_within_thread(s, false);
     nc->at += pa_rtclock_now() + s->thread_info.volume_change_extra_delay;
 
     if (s->thread_info.volume_changes_tail) {
diff --git a/src/pulsecore/source.h b/src/pulsecore/source.h
index e5b9333..1e33cde 100644
--- a/src/pulsecore/source.h
+++ b/src/pulsecore/source.h
@@ -447,7 +447,7 @@ bool pa_source_volume_change_apply(pa_source *s, pa_usec_t *usec_to_next);
 /*** To be called exclusively by source output drivers, from IO context */
 
 void pa_source_invalidate_requested_latency(pa_source *s, bool dynamic);
-pa_usec_t pa_source_get_latency_within_thread(pa_source *s);
+int64_t pa_source_get_latency_within_thread(pa_source *s, bool allow_negative);
 
 /* Called from the main thread, from source-output.c only. The normal way to
  * set the source reference volume is to call pa_source_set_volume(), but the
-- 
2.10.1



More information about the pulseaudio-discuss mailing list