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

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_rtp_asterisk: Improve video performance in certain networks.<br><br>The receive buffer will now grow if we end up flushing the<br>receive queue after not receiving the expected packet in time.<br>This is done in hopes that if this is encountered again the<br>extra buffer size will allow more time to pass and any missing<br>packets to be received.<br><br>The send buffer will now grow if we are asked for packets and<br>can't find them. This is done in hopes that the packets are<br>from the past and have simply been expired. If so then in<br>the future with the extra buffer space the packets should be<br>available.<br><br>Sequence number cycling has been handled so that the<br>correct sequence number is calculated and used in<br>various places, including for sorting packets and<br>for determining if a packet is old or not.<br><br>NACK sending is now more aggressive. If a substantial number<br>of missing sequence numbers are added a NACK will be sent<br>immediately. Afterwards once the receive buffer reaches 25%<br>a single NACK is sent. If the buffer continues to grow and<br>reaches 50% or greater a NACK will be sent for each received<br>future packet to aggressively ask the remote endpoint to<br>retransmit.<br><br>ASTERISK-28764<br><br>Change-Id: I97633dfa8a09a7889cef815b2be369f3f0314b41<br>---<br>M res/res_rtp_asterisk.c<br>1 file changed, 178 insertions(+), 70 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/res_rtp_asterisk.c b/res/res_rtp_asterisk.c</span><br><span>index 969fc2d..bdcce1f 100644</span><br><span>--- a/res/res_rtp_asterisk.c</span><br><span>+++ b/res/res_rtp_asterisk.c</span><br><span>@@ -103,8 +103,13 @@</span><br><span> </span><br><span> #define TURN_STATE_WAIT_TIME 2000</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#define DEFAULT_RTP_SEND_BUFFER_SIZE       250</span><br><span style="color: hsl(0, 100%, 40%);">-#define DEFAULT_RTP_RECV_BUFFER_SIZE 20</span><br><span style="color: hsl(120, 100%, 40%);">+#define DEFAULT_RTP_SEND_BUFFER_SIZE        250     /*!< The initial size of the RTP send buffer */</span><br><span style="color: hsl(120, 100%, 40%);">+#define MAXIMUM_RTP_SEND_BUFFER_SIZE        (DEFAULT_RTP_SEND_BUFFER_SIZE + 200)    /*!< Maximum RTP send buffer size */</span><br><span style="color: hsl(120, 100%, 40%);">+#define DEFAULT_RTP_RECV_BUFFER_SIZE   20      /*!< The initial size of the RTP receiver buffer */</span><br><span style="color: hsl(120, 100%, 40%);">+#define MAXIMUM_RTP_RECV_BUFFER_SIZE    (DEFAULT_RTP_RECV_BUFFER_SIZE + 20)     /*!< Maximum RTP receive buffer size */</span><br><span style="color: hsl(120, 100%, 40%);">+#define OLD_PACKET_COUNT            1000    /*!< The number of previous packets that are considered old */</span><br><span style="color: hsl(120, 100%, 40%);">+#define MISSING_SEQNOS_ADDED_TRIGGER         2       /*!< The number of immediate missing packets that will trigger an immediate NACK */</span><br><span style="color: hsl(120, 100%, 40%);">+#define SEQNO_CYCLE_OVER                65536   /*!< The number after the maximum allowed sequence number */</span><br><span> </span><br><span> /*! Full INTRA-frame Request / Fast Update Request (From RFC2032) */</span><br><span> #define RTCP_PT_FUR     192</span><br><span>@@ -4587,10 +4592,10 @@</span><br><span> {</span><br><span>    struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);</span><br><span>   int packet_len;</span><br><span style="color: hsl(0, 100%, 40%);">- int blp_index;</span><br><span style="color: hsl(120, 100%, 40%);">+        int blp_index = -1;</span><br><span>  int current_seqno;</span><br><span style="color: hsl(0, 100%, 40%);">-      int seqno;</span><br><span style="color: hsl(0, 100%, 40%);">-      unsigned int fci;</span><br><span style="color: hsl(120, 100%, 40%);">+     unsigned int fci = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t remaining_missing_seqno;</span><br><span> </span><br><span>  if (!rtp || !rtp->rtcp) {</span><br><span>                 return 0;</span><br><span>@@ -4601,32 +4606,50 @@</span><br><span>  }</span><br><span> </span><br><span>        current_seqno = rtp->expectedrxseqno;</span><br><span style="color: hsl(0, 100%, 40%);">-        seqno = rtp->lastrxseqno;</span><br><span style="color: hsl(120, 100%, 40%);">+  remaining_missing_seqno = AST_VECTOR_SIZE(&rtp->missing_seqno);</span><br><span>       packet_len = 12; /* The header length is 12 (version line, packet source SSRC, media source SSRC) */</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-        /* Get the missing sequence numbers for the FCI section of the NACK request */</span><br><span style="color: hsl(0, 100%, 40%);">-  for (blp_index = 0, fci = 0; current_seqno < seqno; current_seqno++, blp_index++) {</span><br><span style="color: hsl(120, 100%, 40%);">+        /* If there are no missing sequence numbers then don't bother sending a NACK needlessly */</span><br><span style="color: hsl(120, 100%, 40%);">+        if (!remaining_missing_seqno) {</span><br><span style="color: hsl(120, 100%, 40%);">+               return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* This iterates through the possible forward sequence numbers seeing which ones we</span><br><span style="color: hsl(120, 100%, 40%);">+    * have no packet for, adding it to the NACK until we are out of missing packets.</span><br><span style="color: hsl(120, 100%, 40%);">+      */</span><br><span style="color: hsl(120, 100%, 40%);">+   while (remaining_missing_seqno) {</span><br><span>            int *missing_seqno;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+               /* On the first entry to this loop blp_index will be -1, so this will become 0</span><br><span style="color: hsl(120, 100%, 40%);">+                 * and the sequence number will be placed into the packet as the PID.</span><br><span style="color: hsl(120, 100%, 40%);">+          */</span><br><span style="color: hsl(120, 100%, 40%);">+           blp_index++;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>               missing_seqno = AST_VECTOR_GET_CMP(&rtp->missing_seqno, current_seqno,</span><br><span>                                find_by_value);</span><br><span style="color: hsl(120, 100%, 40%);">+               if (missing_seqno) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  /* We hit the max blp size, reset */</span><br><span style="color: hsl(120, 100%, 40%);">+                  if (blp_index >= 17) {</span><br><span style="color: hsl(120, 100%, 40%);">+                             put_unaligned_uint32(rtcpheader + packet_len, htonl(fci));</span><br><span style="color: hsl(120, 100%, 40%);">+                            fci = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+                              blp_index = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+                                packet_len += 4;</span><br><span style="color: hsl(120, 100%, 40%);">+                      }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-           if (!missing_seqno) {</span><br><span style="color: hsl(0, 100%, 40%);">-                   continue;</span><br><span style="color: hsl(120, 100%, 40%);">+                     if (blp_index == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+                         fci |= (current_seqno << 16);</span><br><span style="color: hsl(120, 100%, 40%);">+                   } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                              fci |= (1 << (blp_index - 1));</span><br><span style="color: hsl(120, 100%, 40%);">+                  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                   /* Since we've used a missing sequence number, we're down one */</span><br><span style="color: hsl(120, 100%, 40%);">+                      remaining_missing_seqno--;</span><br><span>           }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-           /* We hit the max blp size, reset */</span><br><span style="color: hsl(0, 100%, 40%);">-            if (blp_index >= 17) {</span><br><span style="color: hsl(0, 100%, 40%);">-                       put_unaligned_uint32(rtcpheader + packet_len, htonl(fci));</span><br><span style="color: hsl(0, 100%, 40%);">-                      fci = 0;</span><br><span style="color: hsl(0, 100%, 40%);">-                        blp_index = 0;</span><br><span style="color: hsl(0, 100%, 40%);">-                  packet_len += 4;</span><br><span style="color: hsl(0, 100%, 40%);">-                }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-               if (blp_index == 0) {</span><br><span style="color: hsl(0, 100%, 40%);">-                   fci |= (current_seqno << 16);</span><br><span style="color: hsl(0, 100%, 40%);">-             } else {</span><br><span style="color: hsl(0, 100%, 40%);">-                        fci |= (1 << (blp_index - 1));</span><br><span style="color: hsl(120, 100%, 40%);">+          /* Handle cycling of the sequence number */</span><br><span style="color: hsl(120, 100%, 40%);">+           current_seqno++;</span><br><span style="color: hsl(120, 100%, 40%);">+              if (current_seqno == SEQNO_CYCLE_OVER) {</span><br><span style="color: hsl(120, 100%, 40%);">+                      current_seqno = 0;</span><br><span>           }</span><br><span>    }</span><br><span> </span><br><span>@@ -5793,6 +5816,7 @@</span><br><span>        int abs_send_time_id;</span><br><span>        unsigned int now_msw = 0;</span><br><span>    unsigned int now_lsw = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     unsigned int packets_not_found = 0;</span><br><span> </span><br><span>      if (!rtp->send_buffer) {</span><br><span>          ast_debug(1, "Tried to handle NACK request, but we don't have a RTP packet storage!\n");</span><br><span>@@ -5825,6 +5849,7 @@</span><br><span>                       res += rtp_sendto(instance, payload->buf, payload->size, 0, &remote_address, &ice);</span><br><span>            } else {</span><br><span>                     ast_debug(1, "Received NACK request for RTP packet with seqno %d, but we don't have it\n", pid);</span><br><span style="color: hsl(120, 100%, 40%);">+                        packets_not_found++;</span><br><span>                 }</span><br><span>            /*</span><br><span>            * The bitmask. Denoting the least significant bit as 1 and its most significant bit</span><br><span>@@ -5846,6 +5871,7 @@</span><br><span>                                         res += rtp_sendto(instance, payload->buf, payload->size, 0, &remote_address, &ice);</span><br><span>                            } else {</span><br><span>                                     ast_debug(1, "Remote end also requested RTP packet with seqno %d, but we don't have it\n", seqno);</span><br><span style="color: hsl(120, 100%, 40%);">+                                      packets_not_found++;</span><br><span>                                 }</span><br><span>                    }</span><br><span>                    blp >>= 1;</span><br><span>@@ -5853,6 +5879,15 @@</span><br><span>            }</span><br><span>    }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ if (packets_not_found) {</span><br><span style="color: hsl(120, 100%, 40%);">+              /* Grow the send buffer based on how many packets were not found in the buffer, but</span><br><span style="color: hsl(120, 100%, 40%);">+            * enforce a maximum.</span><br><span style="color: hsl(120, 100%, 40%);">+          */</span><br><span style="color: hsl(120, 100%, 40%);">+           ast_data_buffer_resize(rtp->send_buffer, MIN(MAXIMUM_RTP_SEND_BUFFER_SIZE,</span><br><span style="color: hsl(120, 100%, 40%);">+                 ast_data_buffer_max(rtp->send_buffer) + packets_not_found));</span><br><span style="color: hsl(120, 100%, 40%);">+               ast_debug(2, "Send buffer on RTP instance '%p' is now at maximum of %zu\n", instance, ast_data_buffer_max(rtp->send_buffer));</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>  return res;</span><br><span> }</span><br><span> </span><br><span>@@ -7625,6 +7660,11 @@</span><br><span>        } else if (rtp->expectedrxseqno == -1 || seqno == rtp->expectedrxseqno) {</span><br><span>              rtp->expectedrxseqno = seqno + 1;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+              /* We've cycled over, so go back to 0 */</span><br><span style="color: hsl(120, 100%, 40%);">+          if (rtp->expectedrxseqno == SEQNO_CYCLE_OVER) {</span><br><span style="color: hsl(120, 100%, 40%);">+                    rtp->expectedrxseqno = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+          }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>          /* If there are no buffered packets that will be placed after this frame then we can</span><br><span>                  * return it directly without duplicating it.</span><br><span>                 */</span><br><span>@@ -7643,7 +7683,7 @@</span><br><span>          /* If we don't have the next packet after this we can directly return the frame, as there is no</span><br><span>           * chance it will be overwritten.</span><br><span>             */</span><br><span style="color: hsl(0, 100%, 40%);">-             if (!ast_data_buffer_get(rtp->recv_buffer, seqno + 1)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           if (!ast_data_buffer_get(rtp->recv_buffer, rtp->expectedrxseqno)) {</span><br><span>                    frame = ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno);</span><br><span>                    AST_LIST_INSERT_TAIL(&frames, frame, frame_list);</span><br><span>                        return AST_LIST_FIRST(&frames);</span><br><span>@@ -7654,9 +7694,10 @@</span><br><span>                  * to the head of the frames list and avoid having to duplicate it but this would result in out</span><br><span>               * of order packet processing by libsrtp which we are trying to avoid.</span><br><span>                */</span><br><span style="color: hsl(0, 100%, 40%);">-             frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, read_area, res, seqno - 1));</span><br><span style="color: hsl(120, 100%, 40%);">+           frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno));</span><br><span>                 if (frame) {</span><br><span>                         AST_LIST_INSERT_TAIL(&frames, frame, frame_list);</span><br><span style="color: hsl(120, 100%, 40%);">+                 prev_seqno = seqno;</span><br><span>          }</span><br><span> </span><br><span>                /* Add any additional packets that we have buffered and that are available */</span><br><span>@@ -7668,7 +7709,7 @@</span><br><span>                                break;</span><br><span>                       }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-                   frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, payload->buf, payload->size, rtp->expectedrxseqno - 1));</span><br><span style="color: hsl(120, 100%, 40%);">+                      frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, payload->buf, payload->size, prev_seqno));</span><br><span>                      ast_free(payload);</span><br><span> </span><br><span>                       if (!frame) {</span><br><span>@@ -7681,11 +7722,15 @@</span><br><span>                      ast_debug(2, "Pulled buffered packet with sequence number '%d' to additionally return on RTP instance '%p'\n",</span><br><span>                             frame->seqno, instance);</span><br><span>                  AST_LIST_INSERT_TAIL(&frames, frame, frame_list);</span><br><span style="color: hsl(120, 100%, 40%);">+                 prev_seqno = rtp->expectedrxseqno;</span><br><span>                        rtp->expectedrxseqno++;</span><br><span style="color: hsl(120, 100%, 40%);">+                    if (rtp->expectedrxseqno == SEQNO_CYCLE_OVER) {</span><br><span style="color: hsl(120, 100%, 40%);">+                            rtp->expectedrxseqno = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+                  }</span><br><span>            }</span><br><span> </span><br><span>                return AST_LIST_FIRST(&frames);</span><br><span style="color: hsl(0, 100%, 40%);">-     } else if ((abs(seqno - rtp->expectedrxseqno) > 100) ||</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (((abs(seqno - rtp->expectedrxseqno) > 100) && timestamp > rtp->lastividtimestamp) ||</span><br><span>          ast_data_buffer_count(rtp->recv_buffer) == ast_data_buffer_max(rtp->recv_buffer)) {</span><br><span>            int inserted = 0;</span><br><span> </span><br><span>@@ -7701,43 +7746,46 @@</span><br><span>                      rtp_write_rtcp_fir(instance, rtp, &remote_address);</span><br><span>              }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+         /* This works by going through the progression of the sequence number retrieving buffered packets</span><br><span style="color: hsl(120, 100%, 40%);">+              * or inserting the current received packet until we've run out of packets. This ensures that the</span><br><span style="color: hsl(120, 100%, 40%);">+          * packets are in the correct sequence number order.</span><br><span style="color: hsl(120, 100%, 40%);">+           */</span><br><span>          while (ast_data_buffer_count(rtp->recv_buffer)) {</span><br><span>                         struct ast_rtp_rtcp_nack_payload *payload;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-                  payload = (struct ast_rtp_rtcp_nack_payload *)ast_data_buffer_remove_head(rtp->recv_buffer);</span><br><span style="color: hsl(0, 100%, 40%);">-                 if (!payload) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       /* If the packet we received is the one we are expecting at this point then add it in */</span><br><span style="color: hsl(120, 100%, 40%);">+                      if (rtp->expectedrxseqno == seqno) {</span><br><span style="color: hsl(120, 100%, 40%);">+                               frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno));</span><br><span style="color: hsl(120, 100%, 40%);">+                          if (frame) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                  AST_LIST_INSERT_TAIL(&frames, frame, frame_list);</span><br><span style="color: hsl(120, 100%, 40%);">+                                 prev_seqno = seqno;</span><br><span style="color: hsl(120, 100%, 40%);">+                                   ast_debug(2, "Inserted just received packet with sequence number '%d' in correct order on RTP instance '%p'\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                                             seqno, instance);</span><br><span style="color: hsl(120, 100%, 40%);">+                             }</span><br><span style="color: hsl(120, 100%, 40%);">+                             rtp->expectedrxseqno++;</span><br><span style="color: hsl(120, 100%, 40%);">+                            if (rtp->expectedrxseqno == SEQNO_CYCLE_OVER) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                    rtp->expectedrxseqno = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+                          }</span><br><span style="color: hsl(120, 100%, 40%);">+                             inserted = 1;</span><br><span>                                continue;</span><br><span>                    }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-                   /* Even when dumping the receive buffer we do our best to order things, so we ensure that the</span><br><span style="color: hsl(0, 100%, 40%);">-                    * packet we just received is processed in the correct order, so see if we need to insert it now.</span><br><span style="color: hsl(0, 100%, 40%);">-                        */</span><br><span style="color: hsl(0, 100%, 40%);">-                     if (!inserted) {</span><br><span style="color: hsl(0, 100%, 40%);">-                                int buffer_seqno;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-                               buffer_seqno = ntohl(payload->buf[0]) & 0xffff;</span><br><span style="color: hsl(0, 100%, 40%);">-                          if (seqno < buffer_seqno) {</span><br><span style="color: hsl(0, 100%, 40%);">-                                  frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno));</span><br><span style="color: hsl(0, 100%, 40%);">-                                    if (frame) {</span><br><span style="color: hsl(0, 100%, 40%);">-                                            AST_LIST_INSERT_TAIL(&frames, frame, frame_list);</span><br><span style="color: hsl(0, 100%, 40%);">-                                           rtp->expectedrxseqno = seqno + 1;</span><br><span style="color: hsl(0, 100%, 40%);">-                                            prev_seqno = seqno;</span><br><span style="color: hsl(0, 100%, 40%);">-                                             ast_debug(2, "Inserted just received packet with sequence number '%d' in correct order on RTP instance '%p'\n",</span><br><span style="color: hsl(0, 100%, 40%);">-                                                       seqno, instance);</span><br><span style="color: hsl(0, 100%, 40%);">-                                       }</span><br><span style="color: hsl(0, 100%, 40%);">-                                       inserted = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+                 payload = (struct ast_rtp_rtcp_nack_payload *)ast_data_buffer_remove(rtp->recv_buffer, rtp->expectedrxseqno);</span><br><span style="color: hsl(120, 100%, 40%);">+                   if (payload) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, payload->buf, payload->size, prev_seqno));</span><br><span style="color: hsl(120, 100%, 40%);">+                               if (frame) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                  AST_LIST_INSERT_TAIL(&frames, frame, frame_list);</span><br><span style="color: hsl(120, 100%, 40%);">+                                 prev_seqno = rtp->expectedrxseqno;</span><br><span style="color: hsl(120, 100%, 40%);">+                                 ast_debug(2, "Emptying queue and returning packet with sequence number '%d' from RTP instance '%p'\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                                              frame->seqno, instance);</span><br><span>                          }</span><br><span style="color: hsl(120, 100%, 40%);">+                             ast_free(payload);</span><br><span>                   }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-                   frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, payload->buf, payload->size, prev_seqno));</span><br><span style="color: hsl(0, 100%, 40%);">-                 if (frame) {</span><br><span style="color: hsl(0, 100%, 40%);">-                            AST_LIST_INSERT_TAIL(&frames, frame, frame_list);</span><br><span style="color: hsl(0, 100%, 40%);">-                           prev_seqno = frame->seqno;</span><br><span style="color: hsl(0, 100%, 40%);">-                           ast_debug(2, "Emptying queue and returning packet with sequence number '%d' from RTP instance '%p'\n",</span><br><span style="color: hsl(0, 100%, 40%);">-                                        frame->seqno, instance);</span><br><span style="color: hsl(120, 100%, 40%);">+                   rtp->expectedrxseqno++;</span><br><span style="color: hsl(120, 100%, 40%);">+                    if (rtp->expectedrxseqno == SEQNO_CYCLE_OVER) {</span><br><span style="color: hsl(120, 100%, 40%);">+                            rtp->expectedrxseqno = 0;</span><br><span>                         }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-                       ast_free(payload);</span><br><span>           }</span><br><span> </span><br><span>                if (!inserted) {</span><br><span>@@ -7749,11 +7797,21 @@</span><br><span>                   frame = ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno);</span><br><span>                    AST_LIST_INSERT_TAIL(&frames, frame, frame_list);</span><br><span>                        rtp->expectedrxseqno = seqno + 1;</span><br><span style="color: hsl(120, 100%, 40%);">+                  if (rtp->expectedrxseqno == SEQNO_CYCLE_OVER) {</span><br><span style="color: hsl(120, 100%, 40%);">+                            rtp->expectedrxseqno = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+                  }</span><br><span> </span><br><span>                        ast_debug(2, "Adding just received packet with sequence number '%d' to end of dumped queue on RTP instance '%p'\n",</span><br><span>                                seqno, instance);</span><br><span>            }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+         /* When we flush increase our chance for next time by growing the receive buffer when possible</span><br><span style="color: hsl(120, 100%, 40%);">+                 * by how many packets we missed, to give ourselves a bit more breathing room.</span><br><span style="color: hsl(120, 100%, 40%);">+                 */</span><br><span style="color: hsl(120, 100%, 40%);">+           ast_data_buffer_resize(rtp->recv_buffer, MIN(MAXIMUM_RTP_RECV_BUFFER_SIZE,</span><br><span style="color: hsl(120, 100%, 40%);">+                 ast_data_buffer_max(rtp->recv_buffer) + AST_VECTOR_SIZE(&rtp->missing_seqno)));</span><br><span style="color: hsl(120, 100%, 40%);">+             ast_debug(2, "Receive buffer on RTP instance '%p' is now at maximum of %zu\n", instance, ast_data_buffer_max(rtp->recv_buffer));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>              /* As there is such a large gap we don't want to flood the order side with missing packets, so we</span><br><span>                 * give up and start anew.</span><br><span>            */</span><br><span>@@ -7765,7 +7823,18 @@</span><br><span>         /* We're finished with the frames list */</span><br><span>        ast_frame_free(AST_LIST_FIRST(&frames), 0);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-     if (seqno < rtp->expectedrxseqno) {</span><br><span style="color: hsl(120, 100%, 40%);">+     /* Determine if the received packet is from the last OLD_PACKET_COUNT (1000 by default) packets or not.</span><br><span style="color: hsl(120, 100%, 40%);">+        * For the case where the received sequence number exceeds that of the expected sequence number we calculate</span><br><span style="color: hsl(120, 100%, 40%);">+   * the past sequence number that would be 1000 sequence numbers ago. If the received sequence number</span><br><span style="color: hsl(120, 100%, 40%);">+   * exceeds or meets that then it is within OLD_PACKET_COUNT packets ago. For example if the expected</span><br><span style="color: hsl(120, 100%, 40%);">+   * sequence number is 100 and we receive 65530, then it would be considered old. This is because</span><br><span style="color: hsl(120, 100%, 40%);">+       * 65535 - 1000 + 100 = 64635 which gives us the sequence number at which we would consider the packets</span><br><span style="color: hsl(120, 100%, 40%);">+        * old. Since 65530 is above that, it would be considered old.</span><br><span style="color: hsl(120, 100%, 40%);">+         * For the case where the received sequence number is less than the expected sequence number we can do</span><br><span style="color: hsl(120, 100%, 40%);">+         * a simple subtraction to see if it is 1000 packets ago or not.</span><br><span style="color: hsl(120, 100%, 40%);">+       */</span><br><span style="color: hsl(120, 100%, 40%);">+   if ((seqno < rtp->expectedrxseqno && ((rtp->expectedrxseqno - seqno) <= OLD_PACKET_COUNT)) ||</span><br><span style="color: hsl(120, 100%, 40%);">+             (seqno > rtp->expectedrxseqno && (seqno >= (65535 - OLD_PACKET_COUNT + rtp->expectedrxseqno)))) {</span><br><span>                /* If this is a packet from the past then we have received a duplicate packet, so just drop it */</span><br><span>            ast_debug(2, "Received an old packet with sequence number '%d' on RTP instance '%p', dropping it\n",</span><br><span>                       seqno, instance);</span><br><span>@@ -7778,10 +7847,12 @@</span><br><span>  } else {</span><br><span>             /* This is an out of order packet from the future */</span><br><span>                 struct ast_rtp_rtcp_nack_payload *payload;</span><br><span style="color: hsl(0, 100%, 40%);">-              int difference;</span><br><span style="color: hsl(120, 100%, 40%);">+               int missing_seqno;</span><br><span style="color: hsl(120, 100%, 40%);">+            int remove_failed;</span><br><span style="color: hsl(120, 100%, 40%);">+            unsigned int missing_seqnos_added = 0;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-              ast_debug(2, "Received an out of order packet with sequence number '%d' from the future on RTP instance '%p'\n",</span><br><span style="color: hsl(0, 100%, 40%);">-                      seqno, instance);</span><br><span style="color: hsl(120, 100%, 40%);">+             ast_debug(2, "Received an out of order packet with sequence number '%d' while expecting '%d' from the future on RTP instance '%p'\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                       seqno, rtp->expectedrxseqno, instance);</span><br><span> </span><br><span>               payload = ast_malloc(sizeof(*payload) + res);</span><br><span>                if (!payload) {</span><br><span>@@ -7797,11 +7868,38 @@</span><br><span>            payload->size = res;</span><br><span>              memcpy(payload->buf, rtpheader, res);</span><br><span>             ast_data_buffer_put(rtp->recv_buffer, seqno, payload);</span><br><span style="color: hsl(0, 100%, 40%);">-               AST_VECTOR_REMOVE_CMP_ORDERED(&rtp->missing_seqno, seqno, find_by_value,</span><br><span style="color: hsl(0, 100%, 40%);">-                 AST_VECTOR_ELEM_CLEANUP_NOOP);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-              difference = seqno - (prev_seqno + 1);</span><br><span style="color: hsl(0, 100%, 40%);">-          while (difference > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+           /* If this sequence number is removed that means we had a gap and this packet has filled it in</span><br><span style="color: hsl(120, 100%, 40%);">+                 * some. Since it was part of the gap we will have already added any other missing sequence numbers</span><br><span style="color: hsl(120, 100%, 40%);">+            * before it (and possibly after it) to the vector so we don't need to do that again. Note that</span><br><span style="color: hsl(120, 100%, 40%);">+            * remove_failed will be set to -1 if the sequence number isn't removed, and 0 if it is.</span><br><span style="color: hsl(120, 100%, 40%);">+           */</span><br><span style="color: hsl(120, 100%, 40%);">+           remove_failed = AST_VECTOR_REMOVE_CMP_ORDERED(&rtp->missing_seqno, seqno, find_by_value,</span><br><span style="color: hsl(120, 100%, 40%);">+                       AST_VECTOR_ELEM_CLEANUP_NOOP);</span><br><span style="color: hsl(120, 100%, 40%);">+                if (!remove_failed) {</span><br><span style="color: hsl(120, 100%, 40%);">+                 ast_debug(2, "Packet with sequence number '%d' on RTP instance '%p' is no longer missing\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                                seqno, instance);</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           /* The missing sequence number code works by taking the sequence number of the</span><br><span style="color: hsl(120, 100%, 40%);">+                 * packet we've just received and going backwards until we hit the sequence number</span><br><span style="color: hsl(120, 100%, 40%);">+                 * of the last packet we've received. While doing so we check to make sure that the</span><br><span style="color: hsl(120, 100%, 40%);">+                * sequence number is not already missing and that it is not already buffered.</span><br><span style="color: hsl(120, 100%, 40%);">+                 */</span><br><span style="color: hsl(120, 100%, 40%);">+           missing_seqno = seqno;</span><br><span style="color: hsl(120, 100%, 40%);">+                while (remove_failed) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       missing_seqno -= 1;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                 /* If we've cycled backwards then start back at the top */</span><br><span style="color: hsl(120, 100%, 40%);">+                        if (missing_seqno < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+                           missing_seqno = 65535;</span><br><span style="color: hsl(120, 100%, 40%);">+                        }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                   /* We've gone backwards enough such that we've hit the previous sequence number */</span><br><span style="color: hsl(120, 100%, 40%);">+                    if (missing_seqno == prev_seqno) {</span><br><span style="color: hsl(120, 100%, 40%);">+                            break;</span><br><span style="color: hsl(120, 100%, 40%);">+                        }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>                  /* We don't want missing sequence number duplicates. If, for some reason,</span><br><span>                         * packets are really out of order, we could end up in this scenario:</span><br><span>                         *</span><br><span>@@ -7814,24 +7912,34 @@</span><br><span>                          *</span><br><span>                    * This will prevent the duplicate from being added.</span><br><span>                          */</span><br><span style="color: hsl(0, 100%, 40%);">-                     if (AST_VECTOR_GET_CMP(&rtp->missing_seqno, seqno - difference,</span><br><span style="color: hsl(120, 100%, 40%);">+                        if (AST_VECTOR_GET_CMP(&rtp->missing_seqno, missing_seqno,</span><br><span>                                            find_by_value)) {</span><br><span style="color: hsl(0, 100%, 40%);">-                               difference--;</span><br><span style="color: hsl(120, 100%, 40%);">+                         continue;</span><br><span style="color: hsl(120, 100%, 40%);">+                     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                   /* If this packet has been buffered already then don't count it amongst the</span><br><span style="color: hsl(120, 100%, 40%);">+                        * missing.</span><br><span style="color: hsl(120, 100%, 40%);">+                    */</span><br><span style="color: hsl(120, 100%, 40%);">+                   if (ast_data_buffer_get(rtp->recv_buffer, missing_seqno)) {</span><br><span>                               continue;</span><br><span>                    }</span><br><span> </span><br><span>                        ast_debug(2, "Added missing sequence number '%d' to RTP instance '%p'\n",</span><br><span style="color: hsl(0, 100%, 40%);">-                             seqno - difference, instance);</span><br><span style="color: hsl(0, 100%, 40%);">-                  AST_VECTOR_ADD_SORTED(&rtp->missing_seqno, seqno - difference,</span><br><span style="color: hsl(120, 100%, 40%);">+                         missing_seqno, instance);</span><br><span style="color: hsl(120, 100%, 40%);">+                     AST_VECTOR_ADD_SORTED(&rtp->missing_seqno, missing_seqno,</span><br><span>                                     compare_by_value);</span><br><span style="color: hsl(0, 100%, 40%);">-                      difference--;</span><br><span style="color: hsl(120, 100%, 40%);">+                 missing_seqnos_added++;</span><br><span>              }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-           /* When our data buffer is half full we assume that the packets aren't just out of order but</span><br><span style="color: hsl(0, 100%, 40%);">-                 * have actually been lost. To get them back we construct and send a NACK causing the sender to</span><br><span style="color: hsl(0, 100%, 40%);">-          * retransmit them.</span><br><span style="color: hsl(120, 100%, 40%);">+           /* When we add a large number of missing sequence numbers we assume there was a substantial</span><br><span style="color: hsl(120, 100%, 40%);">+            * gap in reception so we trigger an immediate NACK. When our data buffer is 1/4 full we</span><br><span style="color: hsl(120, 100%, 40%);">+               * assume that the packets aren't just out of order but have actually been lost. At 1/2</span><br><span style="color: hsl(120, 100%, 40%);">+            * full we get more aggressive and ask for retransmission when we get a new packet.</span><br><span style="color: hsl(120, 100%, 40%);">+            * To get them back we construct and send a NACK causing the sender to retransmit them.</span><br><span>               */</span><br><span style="color: hsl(0, 100%, 40%);">-             if (ast_data_buffer_count(rtp->recv_buffer) == ast_data_buffer_max(rtp->recv_buffer) / 2) {</span><br><span style="color: hsl(120, 100%, 40%);">+             if (missing_seqnos_added >= MISSING_SEQNOS_ADDED_TRIGGER ||</span><br><span style="color: hsl(120, 100%, 40%);">+                        ast_data_buffer_count(rtp->recv_buffer) == ast_data_buffer_max(rtp->recv_buffer) / 4 ||</span><br><span style="color: hsl(120, 100%, 40%);">+                 ast_data_buffer_count(rtp->recv_buffer) >= ast_data_buffer_max(rtp->recv_buffer) / 2) {</span><br><span>                     int packet_len = 0;</span><br><span>                  int res = 0;</span><br><span>                         int ice;</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/13849">change 13849</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/+/13849"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: I97633dfa8a09a7889cef815b2be369f3f0314b41 </div>
<div style="display:none"> Gerrit-Change-Number: 13849 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: Joshua Colp <jcolp@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@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>