| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 // Unit tests for FilePlayer. | 11 // Unit tests for FilePlayer. |
| 12 | 12 |
| 13 #include "webrtc/modules/utility/include/file_player.h" | 13 #include "webrtc/modules/utility/include/file_player.h" |
| 14 | 14 |
| 15 #include <stdio.h> | 15 #include <stdio.h> |
| 16 |
| 17 #include <memory> |
| 16 #include <string> | 18 #include <string> |
| 17 | 19 |
| 18 #include "gflags/gflags.h" | 20 #include "gflags/gflags.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "webrtc/base/md5digest.h" | 22 #include "webrtc/base/md5digest.h" |
| 21 #include "webrtc/base/stringencode.h" | 23 #include "webrtc/base/stringencode.h" |
| 22 #include "webrtc/test/testsupport/fileutils.h" | 24 #include "webrtc/test/testsupport/fileutils.h" |
| 23 | 25 |
| 24 DEFINE_bool(file_player_output, false, "Generate reference files."); | 26 DEFINE_bool(file_player_output, false, "Generate reference files."); |
| 25 | 27 |
| 26 namespace webrtc { | 28 namespace webrtc { |
| 27 | 29 |
| 28 class FilePlayerTest : public ::testing::Test { | 30 class FilePlayerTest : public ::testing::Test { |
| 29 protected: | 31 protected: |
| 30 static const uint32_t kId = 0; | 32 static const uint32_t kId = 0; |
| 31 static const FileFormats kFileFormat = kFileFormatWavFile; | 33 static const FileFormats kFileFormat = kFileFormatWavFile; |
| 32 static const int kSampleRateHz = 8000; | 34 static const int kSampleRateHz = 8000; |
| 33 | 35 |
| 34 FilePlayerTest() | 36 FilePlayerTest() |
| 35 : player_(FilePlayer::CreateFilePlayer(kId, kFileFormat)), | 37 : player_(FilePlayer::NewFilePlayer(kId, kFileFormat)), |
| 36 output_file_(NULL) {} | 38 output_file_(NULL) {} |
| 37 | 39 |
| 38 void SetUp() override { | 40 void SetUp() override { |
| 39 if (FLAGS_file_player_output) { | 41 if (FLAGS_file_player_output) { |
| 40 std::string output_file = | 42 std::string output_file = |
| 41 webrtc::test::OutputPath() + "file_player_unittest_out.pcm"; | 43 webrtc::test::OutputPath() + "file_player_unittest_out.pcm"; |
| 42 output_file_ = fopen(output_file.c_str(), "wb"); | 44 output_file_ = fopen(output_file.c_str(), "wb"); |
| 43 ASSERT_TRUE(output_file_ != NULL); | 45 ASSERT_TRUE(output_file_ != NULL); |
| 44 } | 46 } |
| 45 } | 47 } |
| 46 | 48 |
| 47 void TearDown() override { | 49 void TearDown() override { |
| 48 if (output_file_) | 50 if (output_file_) |
| 49 fclose(output_file_); | 51 fclose(output_file_); |
| 50 } | 52 } |
| 51 | 53 |
| 52 ~FilePlayerTest() { FilePlayer::DestroyFilePlayer(player_); } | |
| 53 | |
| 54 void PlayFileAndCheck(const std::string& input_file, | 54 void PlayFileAndCheck(const std::string& input_file, |
| 55 const std::string& ref_checksum, | 55 const std::string& ref_checksum, |
| 56 int output_length_ms) { | 56 int output_length_ms) { |
| 57 const float kScaling = 1; | 57 const float kScaling = 1; |
| 58 ASSERT_EQ(0, player_->StartPlayingFile(input_file.c_str(), false, 0, | 58 ASSERT_EQ(0, player_->StartPlayingFile(input_file.c_str(), false, 0, |
| 59 kScaling, 0, 0, NULL)); | 59 kScaling, 0, 0, NULL)); |
| 60 rtc::Md5Digest checksum; | 60 rtc::Md5Digest checksum; |
| 61 for (int i = 0; i < output_length_ms / 10; ++i) { | 61 for (int i = 0; i < output_length_ms / 10; ++i) { |
| 62 int16_t out[10 * kSampleRateHz / 1000] = {0}; | 62 int16_t out[10 * kSampleRateHz / 1000] = {0}; |
| 63 size_t num_samples; | 63 size_t num_samples; |
| 64 EXPECT_EQ(0, | 64 EXPECT_EQ(0, |
| 65 player_->Get10msAudioFromFile(out, num_samples, kSampleRateHz)); | 65 player_->Get10msAudioFromFile(out, num_samples, kSampleRateHz)); |
| 66 checksum.Update(out, num_samples * sizeof(out[0])); | 66 checksum.Update(out, num_samples * sizeof(out[0])); |
| 67 if (FLAGS_file_player_output) { | 67 if (FLAGS_file_player_output) { |
| 68 ASSERT_EQ(num_samples, | 68 ASSERT_EQ(num_samples, |
| 69 fwrite(out, sizeof(out[0]), num_samples, output_file_)); | 69 fwrite(out, sizeof(out[0]), num_samples, output_file_)); |
| 70 } | 70 } |
| 71 } | 71 } |
| 72 char checksum_result[rtc::Md5Digest::kSize]; | 72 char checksum_result[rtc::Md5Digest::kSize]; |
| 73 EXPECT_EQ(rtc::Md5Digest::kSize, | 73 EXPECT_EQ(rtc::Md5Digest::kSize, |
| 74 checksum.Finish(checksum_result, rtc::Md5Digest::kSize)); | 74 checksum.Finish(checksum_result, rtc::Md5Digest::kSize)); |
| 75 EXPECT_EQ(ref_checksum, | 75 EXPECT_EQ(ref_checksum, |
| 76 rtc::hex_encode(checksum_result, sizeof(checksum_result))); | 76 rtc::hex_encode(checksum_result, sizeof(checksum_result))); |
| 77 } | 77 } |
| 78 | 78 |
| 79 FilePlayer* player_; | 79 std::unique_ptr<FilePlayer> player_; |
| 80 FILE* output_file_; | 80 FILE* output_file_; |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 #if defined(WEBRTC_IOS) | 83 #if defined(WEBRTC_IOS) |
| 84 #define MAYBE_PlayWavPcmuFile DISABLED_PlayWavPcmuFile | 84 #define MAYBE_PlayWavPcmuFile DISABLED_PlayWavPcmuFile |
| 85 #else | 85 #else |
| 86 #define MAYBE_PlayWavPcmuFile PlayWavPcmuFile | 86 #define MAYBE_PlayWavPcmuFile PlayWavPcmuFile |
| 87 #endif | 87 #endif |
| 88 TEST_F(FilePlayerTest, MAYBE_PlayWavPcmuFile) { | 88 TEST_F(FilePlayerTest, MAYBE_PlayWavPcmuFile) { |
| 89 const std::string kFileName = | 89 const std::string kFileName = |
| (...skipping 16 matching lines...) Expand all Loading... |
| 106 test::ResourcePath("utility/encapsulated_pcm16b_8khz", "wav"); | 106 test::ResourcePath("utility/encapsulated_pcm16b_8khz", "wav"); |
| 107 // The file is longer than this, but keeping the output shorter limits the | 107 // The file is longer than this, but keeping the output shorter limits the |
| 108 // runtime for the test. | 108 // runtime for the test. |
| 109 const int kOutputLengthMs = 10000; | 109 const int kOutputLengthMs = 10000; |
| 110 const std::string kRefChecksum = "e41d7e1dac8aeae9f21e8e03cd7ecd71"; | 110 const std::string kRefChecksum = "e41d7e1dac8aeae9f21e8e03cd7ecd71"; |
| 111 | 111 |
| 112 PlayFileAndCheck(kFileName, kRefChecksum, kOutputLengthMs); | 112 PlayFileAndCheck(kFileName, kRefChecksum, kOutputLengthMs); |
| 113 } | 113 } |
| 114 | 114 |
| 115 } // namespace webrtc | 115 } // namespace webrtc |
| OLD | NEW |