Fix for crash when reading from audio file in NetEq.

The neteq_rtpplay tool can crash when the replacement audio file is too short. The desired behavior is that the audio file is looped as much as necessary.

Bug: webrtc:9061
Change-Id: Iefba4c47271584845662a415598bf2197dba0fae
Reviewed-on: https://webrtc-review.googlesource.com/64460
Commit-Queue: Ivo Creusen <ivoc@webrtc.org>
Reviewed-by: Henrik Lundin <henrik.lundin@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22585}
diff --git a/modules/audio_coding/neteq/tools/input_audio_file.cc b/modules/audio_coding/neteq/tools/input_audio_file.cc
index 31ebf98..330a874 100644
--- a/modules/audio_coding/neteq/tools/input_audio_file.cc
+++ b/modules/audio_coding/neteq/tools/input_audio_file.cc
@@ -56,13 +56,18 @@
   RTC_CHECK_NE(EOF, file_size) << "Error returned when getting file position.";
   // Find new position.
   long new_pos = current_pos + sizeof(int16_t) * samples;  // Samples to bytes.
-  RTC_CHECK_GE(new_pos, 0)
-      << "Trying to move to before the beginning of the file";
   if (loop_at_end_) {
     new_pos = new_pos % file_size;  // Wrap around the end of the file.
+    if (new_pos < 0) {
+      // For negative values of new_pos, newpos % file_size will also be
+      // negative. To get the correct result it's needed to add file_size.
+      new_pos += file_size;
+    }
   } else {
     new_pos = new_pos > file_size ? file_size : new_pos;  // Don't loop.
   }
+  RTC_CHECK_GE(new_pos, 0)
+      << "Trying to move to before the beginning of the file";
   // Move to new position relative to the beginning of the file.
   RTC_CHECK_EQ(0, fseek(fp_, new_pos, SEEK_SET));
   return true;