<div dir="ltr"><div><div><div><div>Hi Tanu,<br><br></div>I think you weren't rebased on master upstream with this patch or I have a git problem :)<br>I can't find this negation of pa_bytes_to_usec "extra = -pa_bytes_to_usec(p->read_<wbr>index, pa_stream_get_sample_spec(p-><wbr>stream));"<br><br></div>Last commit which touch this was made by Tanu Kaskinen - Sun Jul 13 17:48:59 2014.<br><br></div>Best regards,<br></div>Grzegorz<br></div><div class="gmail_extra"><br><div class="gmail_quote">2017-07-11 20:21 GMT+02:00 Tanu Kaskinen <span dir="ltr"><<a href="mailto:tanuk@iki.fi" target="_blank">tanuk@iki.fi</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">pa_usec_t is an unsigned type, but there were calculations that used it<br>
as if it were a signed type.<br>
<br>
If the latency is negative, pa_simple_get_latency() now reports 0.<br>
<br>
Added some comments too.<br>
---<br>
<br>
This patch doesn't apply to the master branch, so if you want to apply<br>
the patch, use the next branch.<br>
<br>
 src/pulse/simple.c | 40 ++++++++++++++++++++++++++++--<wbr>----------<br>
 1 file changed, 28 insertions(+), 12 deletions(-)<br>
<br>
diff --git a/src/pulse/simple.c b/src/pulse/simple.c<br>
index c8e89b09b..7b66f62d7 100644<br>
--- a/src/pulse/simple.c<br>
+++ b/src/pulse/simple.c<br>
@@ -463,18 +463,34 @@ pa_usec_t pa_simple_get_latency(pa_<wbr>simple *p, int *rerror) {<br>
         CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);<br>
<br>
         if (pa_stream_get_latency(p-><wbr>stream, &t, &negative) >= 0) {<br>
-            pa_usec_t extra = 0;<br>
-<br>
-            if (p->direction == PA_STREAM_RECORD)<br>
-                extra = -pa_bytes_to_usec(p->read_<wbr>index, pa_stream_get_sample_spec(p-><wbr>stream));<br>
-<br>
-            if (negative) {<br>
-                if (extra > t)<br>
-                    t = extra - t;<br>
-                else<br>
-                    t = 0;<br>
-            } else<br>
-                t += extra;<br>
+            if (p->direction == PA_STREAM_RECORD) {<br>
+                pa_usec_t already_read;<br>
+<br>
+                /* pa_simple_read() calls pa_stream_peek() to get the next<br>
+                 * chunk of audio. If the next chunk is larger than what the<br>
+                 * pa_simple_read() caller wanted, the leftover data is stored<br>
+                 * in p->read_data until pa_simple_read() is called again.<br>
+                 * pa_stream_drop() won't be called until the whole chunk has<br>
+                 * been consumed, which means that pa_stream_get_latency() will<br>
+                 * return too large values, because the whole size of the<br>
+                 * partially read chunk is included in the latency. Therefore,<br>
+                 * we need to substract the already-read amount from the<br>
+                 * latency. */<br>
+                already_read = pa_bytes_to_usec(p->read_<wbr>index, pa_stream_get_sample_spec(p-><wbr>stream));<br>
+<br>
+                if (!negative) {<br>
+                    if (t > already_read)<br>
+                        t -= already_read;<br>
+                    else<br>
+                        t = 0;<br>
+                }<br>
+            }<br>
+<br>
+            /* We don't have a way to report negative latencies from<br>
+             * pa_simple_get_latency(). If the latency is negative, let's<br>
+             * report zero. */<br>
+            if (negative)<br>
+                t = 0;<br>
<br>
             break;<br>
         }<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.13.2<br>
<br>
______________________________<wbr>_________________<br>
pulseaudio-discuss mailing list<br>
<a href="mailto:pulseaudio-discuss@lists.freedesktop.org">pulseaudio-discuss@lists.<wbr>freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/pulseaudio-discuss" rel="noreferrer" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/pulseaudio-<wbr>discuss</a><br>
</font></span></blockquote></div><br></div>