Reduce the runtime of some ACM tests in modules_tests

By reducing the length of the audio input, the total runtime of
$ out/Debug/modules_tests --gtest_filter=AudioCodingModuleTest.*
is reduced by more than 10x, when run single-threaded.

The PCMFile helper class is extended with a FastForward method (to
skip initial silence in the test files) and a limiter on how much to
read.

BUG=webrtc:2463
R=ivoc@webrtc.org

Review URL: https://codereview.webrtc.org/1513223002 .

Cr-Commit-Position: refs/heads/master@{#10973}
diff --git a/webrtc/modules/audio_coding/test/EncodeDecodeTest.cc b/webrtc/modules/audio_coding/test/EncodeDecodeTest.cc
index ef45705..e0222af 100644
--- a/webrtc/modules/audio_coding/test/EncodeDecodeTest.cc
+++ b/webrtc/modules/audio_coding/test/EncodeDecodeTest.cc
@@ -63,6 +63,10 @@
   if (channels == 2) {
     _pcmFile.ReadStereo(true);
   }
+  // Set test length to 500 ms (50 blocks of 10 ms each).
+  _pcmFile.SetNum10MsBlocksToRead(50);
+  // Fast-forward 1 second (100 blocks) since the file starts with silence.
+  _pcmFile.FastForward(100);
 
   // Set the codec for the current test.
   if ((testMode == 0) || (testMode == 1)) {
diff --git a/webrtc/modules/audio_coding/test/PCMFile.cc b/webrtc/modules/audio_coding/test/PCMFile.cc
index 0466e02..9289d73 100644
--- a/webrtc/modules/audio_coding/test/PCMFile.cc
+++ b/webrtc/modules/audio_coding/test/PCMFile.cc
@@ -8,7 +8,7 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include "PCMFile.h"
+#include "webrtc/modules/audio_coding/test/PCMFile.h"
 
 #include <ctype.h>
 #include <stdio.h>
@@ -137,6 +137,9 @@
   audio_frame.num_channels_ = channels;
   audio_frame.timestamp_ = timestamp_;
   timestamp_ += samples_10ms_;
+  ++blocks_read_;
+  if (num_10ms_blocks_to_read_ && blocks_read_ >= *num_10ms_blocks_to_read_)
+    end_of_file_ = true;
   return samples_10ms_;
 }
 
@@ -182,11 +185,21 @@
 void PCMFile::Close() {
   fclose(pcm_file_);
   pcm_file_ = NULL;
+  blocks_read_ = 0;
+}
+
+void PCMFile::FastForward(int num_10ms_blocks) {
+  const int channels = read_stereo_ ? 2 : 1;
+  long num_bytes_to_move =
+      num_10ms_blocks * sizeof(int16_t) * samples_10ms_ * channels;
+  int error = fseek(pcm_file_, num_bytes_to_move, SEEK_CUR);
+  RTC_DCHECK_EQ(error, 0);
 }
 
 void PCMFile::Rewind() {
   rewind(pcm_file_);
   end_of_file_ = false;
+  blocks_read_ = 0;
 }
 
 bool PCMFile::Rewinded() {
@@ -201,4 +214,8 @@
   read_stereo_ = is_stereo;
 }
 
+void PCMFile::SetNum10MsBlocksToRead(int value) {
+  num_10ms_blocks_to_read_ = rtc::Optional<int>(value);
+}
+
 }  // namespace webrtc
diff --git a/webrtc/modules/audio_coding/test/PCMFile.h b/webrtc/modules/audio_coding/test/PCMFile.h
index 9365180..840933a 100644
--- a/webrtc/modules/audio_coding/test/PCMFile.h
+++ b/webrtc/modules/audio_coding/test/PCMFile.h
@@ -16,6 +16,7 @@
 
 #include <string>
 
+#include "webrtc/base/optional.h"
 #include "webrtc/modules/include/module_common_types.h"
 #include "webrtc/typedefs.h"
 
@@ -45,12 +46,21 @@
   bool EndOfFile() const {
     return end_of_file_;
   }
+  // Moves forward the specified number of 10 ms blocks. If a limit has been set
+  // with SetNum10MsBlocksToRead, fast-forwarding does not count towards this
+  // limit.
+  void FastForward(int num_10ms_blocks);
   void Rewind();
   static int16_t ChooseFile(std::string* file_name, int16_t max_len,
                             uint16_t* frequency_hz);
   bool Rewinded();
   void SaveStereo(bool is_stereo = true);
   void ReadStereo(bool is_stereo = true);
+  // If set, the reading will stop after the specified number of blocks have
+  // been read. When that has happened, EndOfFile() will return true. Calling
+  // Rewind() will reset the counter and start over.
+  void SetNum10MsBlocksToRead(int value);
+
  private:
   FILE* pcm_file_;
   uint16_t samples_10ms_;
@@ -61,6 +71,8 @@
   uint32_t timestamp_;
   bool read_stereo_;
   bool save_stereo_;
+  rtc::Optional<int> num_10ms_blocks_to_read_;
+  int blocks_read_ = 0;
 };
 
 }  // namespace webrtc
diff --git a/webrtc/modules/audio_coding/test/TestAllCodecs.cc b/webrtc/modules/audio_coding/test/TestAllCodecs.cc
index 21ce7c1..bacfd37 100644
--- a/webrtc/modules/audio_coding/test/TestAllCodecs.cc
+++ b/webrtc/modules/audio_coding/test/TestAllCodecs.cc
@@ -422,8 +422,12 @@
   uint32_t timestamp_diff;
   channel->reset_payload_size();
   int error_count = 0;
-
   int counter = 0;
+  // Set test length to 500 ms (50 blocks of 10 ms each).
+  infile_a_.SetNum10MsBlocksToRead(50);
+  // Fast-forward 1 second (100 blocks) since the file starts with silence.
+  infile_a_.FastForward(100);
+
   while (!infile_a_.EndOfFile()) {
     // Add 10 msec to ACM.
     infile_a_.Read10MsData(audio_frame);
diff --git a/webrtc/modules/audio_coding/test/TestRedFec.cc b/webrtc/modules/audio_coding/test/TestRedFec.cc
index d544026..a1bdc04 100644
--- a/webrtc/modules/audio_coding/test/TestRedFec.cc
+++ b/webrtc/modules/audio_coding/test/TestRedFec.cc
@@ -453,6 +453,10 @@
 void TestRedFec::Run() {
   AudioFrame audioFrame;
   int32_t outFreqHzB = _outFileB.SamplingFrequency();
+  // Set test length to 500 ms (50 blocks of 10 ms each).
+  _inFileA.SetNum10MsBlocksToRead(50);
+  // Fast-forward 1 second (100 blocks) since the file starts with silence.
+  _inFileA.FastForward(100);
 
   while (!_inFileA.EndOfFile()) {
     EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0);
diff --git a/webrtc/modules/audio_coding/test/TestStereo.cc b/webrtc/modules/audio_coding/test/TestStereo.cc
index 19f027b..9bf560d 100644
--- a/webrtc/modules/audio_coding/test/TestStereo.cc
+++ b/webrtc/modules/audio_coding/test/TestStereo.cc
@@ -735,6 +735,12 @@
   int error_count = 0;
   int variable_bytes = 0;
   int variable_packets = 0;
+  // Set test length to 500 ms (50 blocks of 10 ms each).
+  in_file_mono_->SetNum10MsBlocksToRead(50);
+  in_file_stereo_->SetNum10MsBlocksToRead(50);
+  // Fast-forward 1 second (100 blocks) since the files start with silence.
+  in_file_stereo_->FastForward(100);
+  in_file_mono_->FastForward(100);
 
   while (1) {
     // Simulate packet loss by setting |packet_loss_| to "true" in
@@ -800,7 +806,7 @@
   // such as Opus.
   if (variable_packets > 0) {
     variable_bytes /= variable_packets;
-    EXPECT_NEAR(variable_bytes, pack_size_bytes_, 3);
+    EXPECT_NEAR(variable_bytes, pack_size_bytes_, 18);
   }
 
   if (in_file_mono_->EndOfFile()) {
diff --git a/webrtc/modules/audio_coding/test/TestVADDTX.cc b/webrtc/modules/audio_coding/test/TestVADDTX.cc
index 98b1224..a7d2c21 100644
--- a/webrtc/modules/audio_coding/test/TestVADDTX.cc
+++ b/webrtc/modules/audio_coding/test/TestVADDTX.cc
@@ -87,6 +87,11 @@
   PCMFile in_file;
   in_file.Open(in_filename, frequency, "rb");
   in_file.ReadStereo(channels > 1);
+  // Set test length to 1000 ms (100 blocks of 10 ms each).
+  in_file.SetNum10MsBlocksToRead(100);
+  // Fast-forward both files 500 ms (50 blocks). The first second of the file is
+  // silence, but we want to keep half of that to test silence periods.
+  in_file.FastForward(50);
 
   PCMFile out_file;
   if (append) {
diff --git a/webrtc/modules/audio_coding/test/iSACTest.cc b/webrtc/modules/audio_coding/test/iSACTest.cc
index 09744b1..9f223fb 100644
--- a/webrtc/modules/audio_coding/test/iSACTest.cc
+++ b/webrtc/modules/audio_coding/test/iSACTest.cc
@@ -117,6 +117,10 @@
   EXPECT_EQ(0, _acmA->RegisterSendCodec(_paramISAC32kHz));
 
   _inFileA.Open(file_name_swb_, 32000, "rb");
+  // Set test length to 500 ms (50 blocks of 10 ms each).
+  _inFileA.SetNum10MsBlocksToRead(50);
+  // Fast-forward 1 second (100 blocks) since the files start with silence.
+  _inFileA.FastForward(100);
   std::string fileNameA = webrtc::test::OutputPath() + "testisac_a.pcm";
   std::string fileNameB = webrtc::test::OutputPath() + "testisac_b.pcm";
   _outFileA.Open(fileNameA, 32000, "wb");
diff --git a/webrtc/modules/audio_coding/test/opus_test.cc b/webrtc/modules/audio_coding/test/opus_test.cc
index 3372a2a..a68db91 100644
--- a/webrtc/modules/audio_coding/test/opus_test.cc
+++ b/webrtc/modules/audio_coding/test/opus_test.cc
@@ -235,8 +235,12 @@
                                         kOpusComplexity5));
 #endif
 
-  // Make sure the runtime is less than 60 seconds to pass Android test.
-  for (size_t audio_length = 0; audio_length < 10000; audio_length += 10) {
+  // Fast-forward 1 second (100 blocks) since the files start with silence.
+  in_file_stereo_.FastForward(100);
+  in_file_mono_.FastForward(100);
+
+  // Limit the runtime to 1000 blocks of 10 ms each.
+  for (size_t audio_length = 0; audio_length < 1000; audio_length += 10) {
     bool lost_packet = false;
 
     // Get 10 msec of audio.