<p>George Joseph <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/16110">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Kevin Harwell: Looks good to me, but someone else must approve
  Benjamin Keith Ford: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved; Approved for Submit

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">jitterbuffer:  Correct signed/unsigned mismatch causing assert<br><br>If the system time has stepped backwards because of a time<br>adjustment between the time a frame is timestamped and the<br>time we check the timestamps in abstract_jb:hook_event_cb(),<br>we get a negative interval, but we don't check for that there.<br>abstract_jb:hook_event_cb() then calls<br>fixedjitterbuffer:fixed_jb_get() (via abstract_jb:jb_get_fixed)<br>and the first thing that does is assert(interval >= 0).<br><br>There are several issues with this...<br><br> * abstract_jb:hook_event_cb() saves the interval in a variable<br>   named "now" which is confusing in itself.<br><br> * "now" is defined as an unsigned int which converts the negative<br>   value returned from ast_tvdiff_ms() to a large positive value.<br><br> * fixed_jb_get()'s parameter is defined as a signed int so the<br>   interval gets converted back to a negative value.<br><br> * fixed_jb_get()'s assert is NOT an ast_assert but a direct define<br>   that points to the system assert() so it triggers even in<br>   production mode.<br><br>So...<br><br> * hook_event_cb()'s "now" was renamed to "relative_frame_start" and<br>   changed to an int64_t.<br> * hook_event_cb() now checks for a negative value right after<br>   retrieving both the current and framedata timestamps and just<br>   returns the frame if the difference is negative.<br> * fixed_jb_get()'s local define of ASSERT() was changed to call<br>   ast_assert() instead of the system assert().<br><br>ASTERISK-29480<br>Reported by: Dan Cropp<br><br>Change-Id: Ic469dec73c2edc3ba134cda6721a999a9714f3c9<br>---<br>M main/abstract_jb.c<br>M main/fixedjitterbuf.c<br>2 files changed, 19 insertions(+), 9 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/main/abstract_jb.c b/main/abstract_jb.c</span><br><span>index 7549008..083ff9e 100644</span><br><span>--- a/main/abstract_jb.c</span><br><span>+++ b/main/abstract_jb.c</span><br><span>@@ -954,7 +954,7 @@</span><br><span> {</span><br><span>        struct jb_framedata *framedata = data;</span><br><span>       struct timeval now_tv;</span><br><span style="color: hsl(0, 100%, 40%);">-  unsigned long now;</span><br><span style="color: hsl(120, 100%, 40%);">+    int64_t relative_frame_start;</span><br><span>        int putframe = 0; /* signifies if audio frame was placed into the buffer or not */</span><br><span> </span><br><span>       switch (event) {</span><br><span>@@ -1064,7 +1064,17 @@</span><br><span>    }</span><br><span> </span><br><span>        now_tv = ast_tvnow();</span><br><span style="color: hsl(0, 100%, 40%);">-   now = ast_tvdiff_ms(now_tv, framedata->start_tv);</span><br><span style="color: hsl(120, 100%, 40%);">+  relative_frame_start = ast_tvdiff_ms(now_tv, framedata->start_tv);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (relative_frame_start < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+            /*</span><br><span style="color: hsl(120, 100%, 40%);">+             * The only way for this to happen is if the system time has</span><br><span style="color: hsl(120, 100%, 40%);">+           * stepped backwards between the time framedata->start_tv was</span><br><span style="color: hsl(120, 100%, 40%);">+               * set and now.  Think an ntpd or systemd-timesyncd adjustment.</span><br><span style="color: hsl(120, 100%, 40%);">+                *</span><br><span style="color: hsl(120, 100%, 40%);">+             * Just pass the frame through.</span><br><span style="color: hsl(120, 100%, 40%);">+                */</span><br><span style="color: hsl(120, 100%, 40%);">+           return frame;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span>        if (frame->frametype == AST_FRAME_VOICE) {</span><br><span>                int res;</span><br><span>@@ -1084,9 +1094,9 @@</span><br><span>             }</span><br><span>            if (!framedata->first) {</span><br><span>                  framedata->first = 1;</span><br><span style="color: hsl(0, 100%, 40%);">-                        res = framedata->jb_impl->put_first(framedata->jb_obj, jbframe, now);</span><br><span style="color: hsl(120, 100%, 40%);">+                        res = framedata->jb_impl->put_first(framedata->jb_obj, jbframe, relative_frame_start);</span><br><span>              } else {</span><br><span style="color: hsl(0, 100%, 40%);">-                        res = framedata->jb_impl->put(framedata->jb_obj, jbframe, now);</span><br><span style="color: hsl(120, 100%, 40%);">+                      res = framedata->jb_impl->put(framedata->jb_obj, jbframe, relative_frame_start);</span><br><span>            }</span><br><span> </span><br><span>                if (res == AST_JB_IMPL_OK) {</span><br><span>@@ -1104,7 +1114,7 @@</span><br><span>                 int res;</span><br><span>             long next = framedata->jb_impl->next(framedata->jb_obj);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-           /* If now is earlier than the next expected output frame</span><br><span style="color: hsl(120, 100%, 40%);">+              /* If relative_frame_start is earlier than the next expected output frame</span><br><span>             * from the jitterbuffer we may choose to pass on retrieving</span><br><span>                  * a frame during this read iteration.  The only exception</span><br><span>            * to this rule is when an audio frame is placed into the buffer</span><br><span>@@ -1113,8 +1123,8 @@</span><br><span>              * doing this we are able to feed off the timing of the input frames</span><br><span>                  * and only rely on our jitterbuffer timer when frames are dropped.</span><br><span>           * During testing, this hybrid form of timing gave more reliable results. */</span><br><span style="color: hsl(0, 100%, 40%);">-            if (now < next) {</span><br><span style="color: hsl(0, 100%, 40%);">-                    long int diff = next - now;</span><br><span style="color: hsl(120, 100%, 40%);">+           if (relative_frame_start < next) {</span><br><span style="color: hsl(120, 100%, 40%);">+                 long int diff = next - relative_frame_start;</span><br><span>                         if (!putframe) {</span><br><span>                             return frame;</span><br><span>                        } else if (diff >= framedata->timer_interval) {</span><br><span>@@ -1124,7 +1134,7 @@</span><br><span> </span><br><span>            ast_frfree(frame);</span><br><span>           frame = &ast_null_frame;</span><br><span style="color: hsl(0, 100%, 40%);">-            res = framedata->jb_impl->get(framedata->jb_obj, &frame, now, framedata->timer_interval);</span><br><span style="color: hsl(120, 100%, 40%);">+             res = framedata->jb_impl->get(framedata->jb_obj, &frame, relative_frame_start, framedata->timer_interval);</span><br><span>           switch (res) {</span><br><span>               case AST_JB_IMPL_OK:</span><br><span>                         /* got it, and pass it through */</span><br><span>diff --git a/main/fixedjitterbuf.c b/main/fixedjitterbuf.c</span><br><span>index 3c350b4..6c92329 100644</span><br><span>--- a/main/fixedjitterbuf.c</span><br><span>+++ b/main/fixedjitterbuf.c</span><br><span>@@ -41,7 +41,7 @@</span><br><span> #ifdef FIXED_JB_DEBUG</span><br><span> #define ASSERT(a)</span><br><span> #else</span><br><span style="color: hsl(0, 100%, 40%);">-#define ASSERT(a) assert(a)</span><br><span style="color: hsl(120, 100%, 40%);">+#define ASSERT(a) ast_assert(a)</span><br><span> #endif</span><br><span> </span><br><span> /*! \brief private fixed_jb structure */</span><br><span></span><br></pre><div style="white-space:pre-wrap"></div><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/16110">change 16110</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/16110"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 18 </div>
<div style="display:none"> Gerrit-Change-Id: Ic469dec73c2edc3ba134cda6721a999a9714f3c9 </div>
<div style="display:none"> Gerrit-Change-Number: 16110 </div>
<div style="display:none"> Gerrit-PatchSet: 3 </div>
<div style="display:none"> Gerrit-Owner: George Joseph <gjoseph@sangoma.com> </div>
<div style="display:none"> Gerrit-Reviewer: Benjamin Keith Ford <bford@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@sangoma.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>