source/sink: Allow pa_{source, sink}_get_latency_within_thread() to return negative...
authorGeorg Chini <georg@chini.tk>
Mon, 17 Apr 2017 17:50:10 +0000 (19:50 +0200)
committerGeorg Chini <georg@chini.tk>
Mon, 17 Apr 2017 17:50:10 +0000 (19:50 +0200)
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 within module-loopback:

- 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 is a pre-condition for solving these problems.
It adds a new 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.

38 files changed:
src/modules/alsa/alsa-sink.c
src/modules/alsa/alsa-source.c
src/modules/bluetooth/module-bluez4-device.c
src/modules/bluetooth/module-bluez5-device.c
src/modules/echo-cancel/module-echo-cancel.c
src/modules/jack/module-jack-sink.c
src/modules/jack/module-jack-source.c
src/modules/macosx/module-coreaudio-device.c
src/modules/module-combine-sink.c
src/modules/module-equalizer-sink.c
src/modules/module-esound-sink.c
src/modules/module-ladspa-sink.c
src/modules/module-loopback.c
src/modules/module-null-sink.c
src/modules/module-null-source.c
src/modules/module-pipe-sink.c
src/modules/module-pipe-source.c
src/modules/module-remap-sink.c
src/modules/module-remap-source.c
src/modules/module-sine-source.c
src/modules/module-solaris.c
src/modules/module-tunnel-sink-new.c
src/modules/module-tunnel-source-new.c
src/modules/module-tunnel.c
src/modules/module-virtual-sink.c
src/modules/module-virtual-source.c
src/modules/module-virtual-surround-sink.c
src/modules/module-waveout.c
src/modules/oss/module-oss.c
src/modules/raop/raop-sink.c
src/modules/rtp/module-rtp-recv.c
src/pulsecore/protocol-native.c
src/pulsecore/sink-input.c
src/pulsecore/sink.c
src/pulsecore/sink.h
src/pulsecore/source-output.c
src/pulsecore/source.c
src/pulsecore/source.h

index 073413f..827a650 100644 (file)
@@ -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;
         }
index b788df2..6bec188 100644 (file)
@@ -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;
         }
index 4c8ec8c..b3f7790 100644 (file)
@@ -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;
         }
index afcc457..a96da17 100644 (file)
@@ -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;
         }
index 4f80be4..be04531 100644 (file)
@@ -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);
index 4d31493..7405386 100644 (file)
@@ -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;
         }
index e45f304..1f020c0 100644 (file)
@@ -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;
         }
index 503b80e..48faf08 100644 (file)
@@ -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;
         }
     }
index 71d1fba..4aa7de9 100644 (file)
@@ -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;
         }
index eb46181..96eda11 100644 (file)
@@ -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) +
index 2ce0c85..59bef48 100644 (file)
@@ -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;
         }
 
index 7b0c0d6..7b2e651 100644 (file)
@@ -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);
index 157bb32..ded59e1 100644 (file)
@@ -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();
 
index b8157e8..9237656 100644 (file)
@@ -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;
         }
index a75a04f..5bfa1e1 100644 (file)
@@ -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;
         }
index 1c688d7..8396a63 100644 (file)
@@ -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;
         }
     }
index f39fc55..411f1fd 100644 (file)
@@ -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;
         }
     }
index 1b1c5e1..37f4f56 100644 (file)
@@ -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);
index 3aa8f11..0bdeb38 100644 (file)
@@ -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);
 
index cdeb2c0..3f275e2 100644 (file)
@@ -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;
         }
index ccff69f..fee69e7 100644 (file)
@@ -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:
index 92f99df..dd6c886 100644 (file)
@@ -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:
index e159c33..2db928c 100644 (file)
@@ -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;
         }
index e08816b..94ea4fb 100644 (file)
@@ -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;
         }
 
index 3316e93..4fa4a56 100644 (file)
@@ -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);
index 0fab31e..42aefd0 100644 (file)
@@ -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);
 
index 60093e6..23c6bdc 100644 (file)
@@ -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);
index 610522b..8d32e7b 100644 (file)
@@ -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;
             }
 
index 5ad765d..4631ff3 100644 (file)
@@ -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;
         }
 
index 4ca625f..e5d219e 100644 (file)
@@ -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;
         }
index 5977500..f512a8a 100644 (file)
@@ -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)
index efe9bd2..ce99f87 100644 (file)
@@ -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;
     }
index bcc783a..2ceed41 100644 (file)
@@ -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;
         }
index a6a3a2f..6692066 100644 (file)
@@ -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;
index bc2a200..0e79cf3 100644 (file)
@@ -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
index 7cbc5ad..fa32a56 100644 (file)
@@ -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;
         }
index 12aa19d..6af1d67 100644 (file)
@@ -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) {
index e5b9333..1e33cde 100644 (file)
@@ -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