Fix a bug preventing FilePlayer from playing encoded wav files

A bug in ACM2 prevented decoding and playout of wav files where the
audio data was encoded (i.e., not just linear PCM 16 bit data).

This CL fixes the issue, and adds a unit test for the FilePlayer.

BUG=3386
R=henrike@webrtc.org, tina.legrand@webrtc.org, turaj@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/21499005

git-svn-id: http://webrtc.googlecode.com/svn/trunk@6248 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/resources/utility/encapsulated_pcm16b_8khz.wav.sha1 b/resources/utility/encapsulated_pcm16b_8khz.wav.sha1
new file mode 100644
index 0000000..480cfea
--- /dev/null
+++ b/resources/utility/encapsulated_pcm16b_8khz.wav.sha1
@@ -0,0 +1 @@
+43092df43f4093e474c41cd74e3085e7ca401c7d
\ No newline at end of file
diff --git a/resources/utility/encapsulated_pcmu_8khz.wav.sha1 b/resources/utility/encapsulated_pcmu_8khz.wav.sha1
new file mode 100644
index 0000000..e2269f7
--- /dev/null
+++ b/resources/utility/encapsulated_pcmu_8khz.wav.sha1
@@ -0,0 +1 @@
+d96caa3ff9c48559ec37784791887994d28f3b5a
\ No newline at end of file
diff --git a/webrtc/modules/audio_coding/main/acm2/audio_coding_module_impl.cc b/webrtc/modules/audio_coding/main/acm2/audio_coding_module_impl.cc
index ed4b086..b8bb12c 100644
--- a/webrtc/modules/audio_coding/main/acm2/audio_coding_module_impl.cc
+++ b/webrtc/modules/audio_coding/main/acm2/audio_coding_module_impl.cc
@@ -1835,6 +1835,7 @@
     aux_rtp_header_->type.Audio.channel = 1;
   }
 
+  aux_rtp_header_->header.timestamp = timestamp;
   IncomingPacket(incoming_payload, payload_length, *aux_rtp_header_);
   // Get ready for the next payload.
   aux_rtp_header_->header.sequenceNumber++;
diff --git a/webrtc/modules/modules.gyp b/webrtc/modules/modules.gyp
index 47e0a74..10fa7ba 100644
--- a/webrtc/modules/modules.gyp
+++ b/webrtc/modules/modules.gyp
@@ -221,6 +221,7 @@
             'rtp_rtcp/test/testAPI/test_api_rtcp.cc',
             'rtp_rtcp/test/testAPI/test_api_video.cc',
             'utility/source/audio_frame_operations_unittest.cc',
+            'utility/source/file_player_unittests.cc',
             'video_coding/codecs/test/packet_manipulator_unittest.cc',
             'video_coding/codecs/test/stats_unittest.cc',
             'video_coding/codecs/test/videoprocessor_unittest.cc',
diff --git a/webrtc/modules/modules_unittests.isolate b/webrtc/modules/modules_unittests.isolate
index 57d1307..74b713f 100644
--- a/webrtc/modules/modules_unittests.isolate
+++ b/webrtc/modules/modules_unittests.isolate
@@ -93,6 +93,8 @@
           '../../resources/synthetic-trace.rx',
           '../../resources/tmobile-downlink.rx',
           '../../resources/tmobile-uplink.rx',
+          '../../resources/utility/encapsulated_pcm16b_8khz.wav',
+          '../../resources/utility/encapsulated_pcmu_8khz.wav',
           '../../resources/verizon3g-downlink.rx',
           '../../resources/verizon3g-uplink.rx',
           '../../resources/verizon4g-downlink.rx',
diff --git a/webrtc/modules/utility/source/file_player_unittests.cc b/webrtc/modules/utility/source/file_player_unittests.cc
new file mode 100644
index 0000000..d430d9f
--- /dev/null
+++ b/webrtc/modules/utility/source/file_player_unittests.cc
@@ -0,0 +1,106 @@
+/*
+ *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+// Unit tests for FilePlayer.
+
+#include "webrtc/modules/utility/interface/file_player.h"
+
+#include <stdio.h>
+#include <string>
+
+#include "gflags/gflags.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "webrtc/base/md5digest.h"
+#include "webrtc/base/stringencode.h"
+#include "webrtc/test/testsupport/fileutils.h"
+
+DEFINE_bool(file_player_output, false, "Generate reference files.");
+
+namespace webrtc {
+
+class FilePlayerTest : public ::testing::Test {
+ protected:
+  static const uint32_t kId = 0;
+  static const FileFormats kFileFormat = kFileFormatWavFile;
+  static const int kSampleRateHz = 8000;
+
+  FilePlayerTest()
+      : player_(FilePlayer::CreateFilePlayer(kId, kFileFormat)),
+        output_file_(NULL) {}
+
+  virtual void SetUp() OVERRIDE {
+    if (FLAGS_file_player_output) {
+      std::string output_file =
+          webrtc::test::OutputPath() + "file_player_unittest_out.pcm";
+      output_file_ = fopen(output_file.c_str(), "wb");
+      ASSERT_TRUE(output_file_ != NULL);
+    }
+  }
+
+  virtual void TearDown() OVERRIDE {
+    if (output_file_)
+      fclose(output_file_);
+  }
+
+  ~FilePlayerTest() { FilePlayer::DestroyFilePlayer(player_); }
+
+  void PlayFileAndCheck(const std::string& input_file,
+                        const std::string& ref_checksum,
+                        int output_length_ms) {
+    const float kScaling = 1;
+    ASSERT_EQ(0,
+              player_->StartPlayingFile(
+                  input_file.c_str(), false, 0, kScaling, 0, 0, NULL));
+    rtc::Md5Digest checksum;
+    for (int i = 0; i < output_length_ms / 10; ++i) {
+      int16_t out[10 * kSampleRateHz / 1000] = {0};
+      int num_samples;
+      EXPECT_EQ(0,
+                player_->Get10msAudioFromFile(out, num_samples, kSampleRateHz));
+      checksum.Update(out, num_samples * sizeof(out[0]));
+      if (FLAGS_file_player_output) {
+        ASSERT_EQ(static_cast<size_t>(num_samples),
+                  fwrite(out, sizeof(out[0]), num_samples, output_file_));
+      }
+    }
+    char checksum_result[rtc::Md5Digest::kSize];
+    EXPECT_EQ(rtc::Md5Digest::kSize,
+              checksum.Finish(checksum_result, rtc::Md5Digest::kSize));
+    EXPECT_EQ(ref_checksum,
+              rtc::hex_encode(checksum_result, sizeof(checksum_result)));
+  }
+
+  FilePlayer* player_;
+  FILE* output_file_;
+};
+
+TEST_F(FilePlayerTest, PlayWavPcmuFile) {
+  const std::string kFileName =
+      test::ResourcePath("utility/encapsulated_pcmu_8khz", "wav");
+  // The file is longer than this, but keeping the output shorter limits the
+  // runtime for the test.
+  const int kOutputLengthMs = 10000;
+  const std::string kRefChecksum = "c74e7fd432d439b1311e1c16815b3e9a";
+
+  PlayFileAndCheck(kFileName, kRefChecksum, kOutputLengthMs);
+}
+
+TEST_F(FilePlayerTest, PlayWavPcm16File) {
+  const std::string kFileName =
+      test::ResourcePath("utility/encapsulated_pcm16b_8khz", "wav");
+  // The file is longer than this, but keeping the output shorter limits the
+  // runtime for the test.
+  const int kOutputLengthMs = 10000;
+  const std::string kRefChecksum = "e41d7e1dac8aeae9f21e8e03cd7ecd71";
+
+  PlayFileAndCheck(kFileName, kRefChecksum, kOutputLengthMs);
+}
+
+}  // namespace webrtc