henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | // Unit tests for FilePlayer. |
| 12 | |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 13 | #include "webrtc/modules/utility/include/file_player.h" |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <string> |
| 17 | |
| 18 | #include "gflags/gflags.h" |
| 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | #include "webrtc/base/md5digest.h" |
| 21 | #include "webrtc/base/stringencode.h" |
| 22 | #include "webrtc/test/testsupport/fileutils.h" |
| 23 | |
| 24 | DEFINE_bool(file_player_output, false, "Generate reference files."); |
| 25 | |
| 26 | namespace webrtc { |
| 27 | |
| 28 | class FilePlayerTest : public ::testing::Test { |
| 29 | protected: |
| 30 | static const uint32_t kId = 0; |
| 31 | static const FileFormats kFileFormat = kFileFormatWavFile; |
| 32 | static const int kSampleRateHz = 8000; |
| 33 | |
| 34 | FilePlayerTest() |
| 35 | : player_(FilePlayer::CreateFilePlayer(kId, kFileFormat)), |
| 36 | output_file_(NULL) {} |
| 37 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 38 | void SetUp() override { |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 39 | if (FLAGS_file_player_output) { |
| 40 | std::string output_file = |
| 41 | webrtc::test::OutputPath() + "file_player_unittest_out.pcm"; |
| 42 | output_file_ = fopen(output_file.c_str(), "wb"); |
| 43 | ASSERT_TRUE(output_file_ != NULL); |
| 44 | } |
| 45 | } |
| 46 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 47 | void TearDown() override { |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 48 | if (output_file_) |
| 49 | fclose(output_file_); |
| 50 | } |
| 51 | |
| 52 | ~FilePlayerTest() { FilePlayer::DestroyFilePlayer(player_); } |
| 53 | |
| 54 | void PlayFileAndCheck(const std::string& input_file, |
| 55 | const std::string& ref_checksum, |
| 56 | int output_length_ms) { |
| 57 | const float kScaling = 1; |
| 58 | ASSERT_EQ(0, |
| 59 | player_->StartPlayingFile( |
| 60 | input_file.c_str(), false, 0, kScaling, 0, 0, NULL)); |
| 61 | rtc::Md5Digest checksum; |
| 62 | for (int i = 0; i < output_length_ms / 10; ++i) { |
| 63 | int16_t out[10 * kSampleRateHz / 1000] = {0}; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 64 | size_t num_samples; |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 65 | EXPECT_EQ(0, |
| 66 | player_->Get10msAudioFromFile(out, num_samples, kSampleRateHz)); |
| 67 | checksum.Update(out, num_samples * sizeof(out[0])); |
| 68 | if (FLAGS_file_player_output) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 69 | ASSERT_EQ(num_samples, |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 70 | fwrite(out, sizeof(out[0]), num_samples, output_file_)); |
| 71 | } |
| 72 | } |
| 73 | char checksum_result[rtc::Md5Digest::kSize]; |
| 74 | EXPECT_EQ(rtc::Md5Digest::kSize, |
| 75 | checksum.Finish(checksum_result, rtc::Md5Digest::kSize)); |
| 76 | EXPECT_EQ(ref_checksum, |
| 77 | rtc::hex_encode(checksum_result, sizeof(checksum_result))); |
| 78 | } |
| 79 | |
| 80 | FilePlayer* player_; |
| 81 | FILE* output_file_; |
| 82 | }; |
| 83 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame^] | 84 | #if defined(WEBRTC_IOS) |
| 85 | #define MAYBE_PlayWavPcmuFile DISABLED_PlayWavPcmuFile |
| 86 | #else |
| 87 | #define MAYBE_PlayWavPcmuFile PlayWavPcmuFile |
| 88 | #endif |
| 89 | TEST_F(FilePlayerTest, MAYBE_PlayWavPcmuFile) { |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 90 | const std::string kFileName = |
| 91 | test::ResourcePath("utility/encapsulated_pcmu_8khz", "wav"); |
| 92 | // The file is longer than this, but keeping the output shorter limits the |
| 93 | // runtime for the test. |
| 94 | const int kOutputLengthMs = 10000; |
| 95 | const std::string kRefChecksum = "c74e7fd432d439b1311e1c16815b3e9a"; |
| 96 | |
| 97 | PlayFileAndCheck(kFileName, kRefChecksum, kOutputLengthMs); |
| 98 | } |
| 99 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame^] | 100 | #if defined(WEBRTC_IOS) |
| 101 | #define MAYBE_PlayWavPcm16File DISABLED_PlayWavPcm16File |
| 102 | #else |
| 103 | #define MAYBE_PlayWavPcm16File PlayWavPcm16File |
| 104 | #endif |
| 105 | TEST_F(FilePlayerTest, MAYBE_PlayWavPcm16File) { |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 106 | const std::string kFileName = |
| 107 | test::ResourcePath("utility/encapsulated_pcm16b_8khz", "wav"); |
| 108 | // The file is longer than this, but keeping the output shorter limits the |
| 109 | // runtime for the test. |
| 110 | const int kOutputLengthMs = 10000; |
| 111 | const std::string kRefChecksum = "e41d7e1dac8aeae9f21e8e03cd7ecd71"; |
| 112 | |
| 113 | PlayFileAndCheck(kFileName, kRefChecksum, kOutputLengthMs); |
| 114 | } |
| 115 | |
| 116 | } // namespace webrtc |