[PATCH 2/2] source/sink: Allow pa_{source, sink}_get_latency_within_thread() to return negative values

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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()
except when used within PA_{SINK,SOURCE}_MESSAGE_GET_LATENCY. 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.

Additionally let PA_SOURCE_MESSAGE_GET_LATENCY return -pa_sink_get_latency_within_thread()
for monitor sources because the source gets the data before it is played.
---
 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            |  8 +++-----
 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                |  8 ++++----
 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                  | 10 ++++++----
 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                 |  4 ++--
 src/modules/oss/module-oss.c                 |  4 ++--
 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                         | 28 +++++++++++++---------------
 src/pulsecore/sink.h                         |  2 +-
 src/pulsecore/source-output.c                |  4 ++--
 src/pulsecore/source.c                       | 24 +++++++++++-------------
 src/pulsecore/source.h                       |  2 +-
 38 files changed, 137 insertions(+), 139 deletions(-)

diff --git a/src/modules/alsa/alsa-sink.c b/src/modules/alsa/alsa-sink.c
index 073413f0..827a6508 100644
--- a/src/modules/alsa/alsa-sink.c
+++ b/src/modules/alsa/alsa-sink.c
@@ -908,8 +908,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;
 
@@ -920,12 +919,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) {
@@ -1166,12 +1163,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 b788df2b..6bec188e 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 4c8ec8c7..b3f7790c 100644
--- a/src/modules/bluetooth/module-bluez4-device.c
+++ b/src/modules/bluetooth/module-bluez4-device.c
@@ -431,22 +431,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;
         }
     }
@@ -508,15 +508,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 afcc4576..a96da17d 100644
--- a/src/modules/bluetooth/module-bluez5-device.c
+++ b/src/modules/bluetooth/module-bluez5-device.c
@@ -886,15 +886,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) = u->source->thread_info.fixed_latency + wi > ri ? u->source->thread_info.fixed_latency + wi - ri : 0;
+                *((int64_t*) data) = u->source->thread_info.fixed_latency + wi - ri;
             } else
-                *((pa_usec_t*) data) = 0;
+                *((int64_t*) data) = 0;
 
             return 0;
         }
@@ -1047,7 +1047,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());
@@ -1057,7 +1057,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 4f80be40..be045315 100644
--- a/src/modules/echo-cancel/module-echo-cancel.c
+++ b/src/modules/echo-cancel/module-echo-cancel.c
@@ -410,14 +410,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 */
@@ -446,14 +446,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);
@@ -1019,7 +1019,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);
@@ -1098,7 +1098,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 4d314930..74053865 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 e45f3048..1f020c07 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 503b80e7..48faf083 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 71d1fbac..4aa7de92 100644
--- a/src/modules/module-combine-sink.c
+++ b/src/modules/module-combine-sink.c
@@ -870,17 +870,15 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
         }
 
         case PA_SINK_MESSAGE_GET_LATENCY: {
-            pa_usec_t x, y, c, *delay = data;
+            pa_usec_t x, y, c;
+            int64_t *delay = data;
 
             x = pa_rtclock_now();
             y = pa_smoother_get(u->thread_info.smoother, x);
 
             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 eb461815..96eda11e 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 2ce0c854..59bef483 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 2409d610..67a858a6 100644
--- a/src/modules/module-ladspa-sink.c
+++ b/src/modules/module-ladspa-sink.c
@@ -348,14 +348,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 157bb32b..ded59e1a 100644
--- a/src/modules/module-loopback.c
+++ b/src/modules/module-loopback.c
@@ -500,7 +500,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;
@@ -531,7 +531,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();
 
@@ -820,7 +820,7 @@ static int sink_input_process_msg_cb(pa_msgobject *obj, int code, void *data, in
                 /* Add the time between push and post */
                 time_delta += pa_rtclock_now() - (pa_usec_t) offset;
                 /* Add the sink latency */
-                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);
 
                 /* The source latency report includes the audio in the chunk,
                  * but since we already pushed the chunk to the memblockq, we need
@@ -895,7 +895,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 b8157e8f..9237656a 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 a75a04f5..5bfa1e18 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 1c688d77..8396a632 100644
--- a/src/modules/module-pipe-sink.c
+++ b/src/modules/module-pipe-sink.c
@@ -112,7 +112,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 f39fc557..411f1fd6 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 1b1c5e15..37f4f56c 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 3aa8f115..0bdeb381 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 cdeb2c03..3f275e2a 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 ccff69fc..fee69e74 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 92f99df7..dd6c8866 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 e159c33f..2db928c8 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 e08816bd..94ea4fb4 100644
--- a/src/modules/module-tunnel.c
+++ b/src/modules/module-tunnel.c
@@ -513,12 +513,13 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
         }
 
         case PA_SINK_MESSAGE_GET_LATENCY: {
-            pa_usec_t yl, yr, *usec = data;
+            pa_usec_t yl, yr;
+            int64_t *usec = data;
 
             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;
         }
 
@@ -618,12 +619,13 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
         }
 
         case PA_SOURCE_MESSAGE_GET_LATENCY: {
-            pa_usec_t yr, yl, *usec = data;
+            pa_usec_t yr, yl;
+            int64_t *usec = data;
 
             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 3316e930..4fa4a56e 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 0fab31e4..42aefd05 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 */
@@ -402,7 +402,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 && o->source) {
 
-        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 47716da2..e63b8a6b 100644
--- a/src/modules/module-virtual-surround-sink.c
+++ b/src/modules/module-virtual-surround-sink.c
@@ -123,14 +123,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 610522b2..8d32e7b4 100644
--- a/src/modules/module-waveout.c
+++ b/src/modules/module-waveout.c
@@ -378,7 +378,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) = (int64_t)r;
                 return 0;
             }
 
@@ -396,7 +396,7 @@ static int process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa
                 pa_usec_t r = 0;
                 if (u->hwi)
                     r = source_get_latency(u);
-                *((pa_usec_t*) data) = r;
+                *((int64_t*) data) = (int64_t)r;
                 return 0;
             }
 
diff --git a/src/modules/oss/module-oss.c b/src/modules/oss/module-oss.c
index 5ad765d8..4631ff3a 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) = (int64_t)r;
 
             return 0;
         }
@@ -744,7 +744,7 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
                     r = io_source_get_latency(u);
             }
 
-            *((pa_usec_t*) data) = r;
+            *((int64_t*) data) = (int64_t)r;
             return 0;
         }
 
diff --git a/src/modules/raop/raop-sink.c b/src/modules/raop/raop-sink.c
index 4ca625f0..e5d219e8 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 5977500c..f512a8a1 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 efe9bd24..ce99f873 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 bcc783ae..2ceed412 100644
--- a/src/pulsecore/sink-input.c
+++ b/src/pulsecore/sink-input.c
@@ -2046,7 +2046,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 a6a3a2fb..6692066a 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();
@@ -1518,19 +1518,19 @@ pa_usec_t pa_sink_get_latency(pa_sink *s) {
 
     pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) == 0);
 
-    /* usec is unsigned, so check that the offset can be added to usec without
+    /* the return value 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);
@@ -1551,11 +1551,9 @@ pa_usec_t pa_sink_get_latency_within_thread(pa_sink *s) {
 
     o->process_msg(o, PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL);
 
-    /* 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;
@@ -2629,7 +2627,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);
 
@@ -2691,7 +2689,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)
@@ -3585,7 +3583,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) {
@@ -3695,7 +3693,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 bc2a2003..0e79cf36 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 7cbc5adf..fa32a566 100644
--- a/src/pulsecore/source-output.c
+++ b/src/pulsecore/source-output.c
@@ -753,7 +753,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);
 
@@ -1613,7 +1613,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 12aa19d1..6af1d675 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();
@@ -1113,19 +1113,19 @@ pa_usec_t pa_source_get_latency(pa_source *s) {
 
     pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_LATENCY, &usec, 0, NULL) == 0);
 
-    /* usec is unsigned, so check that the offset can be added to usec without
+    /* The return value 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);
@@ -1146,11 +1146,9 @@ pa_usec_t pa_source_get_latency_within_thread(pa_source *s) {
 
     o->process_msg(o, PA_SOURCE_MESSAGE_GET_LATENCY, &usec, 0, NULL);
 
-    /* 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;
@@ -2222,7 +2220,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) = -pa_sink_get_latency_within_thread(s->monitor_of, true);
                 return 0;
             }
 
@@ -2696,7 +2694,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 e5b93334..1e33cde2 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.11.0



[Index of Archives]     [Linux Audio Users]     [AMD Graphics]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux