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 | |
kwiberg | 9774447 | 2017-01-10 01:12:51 -0800 | [diff] [blame] | 13 | #include "webrtc/voice_engine/file_player.h" |
kwiberg | 9d7eb13 | 2016-08-16 04:08:30 -0700 | [diff] [blame] | 14 | |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 16 | |
| 17 | #include <memory> |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 18 | #include <string> |
| 19 | |
oprypin | 9b2f20c | 2017-08-29 05:51:57 -0700 | [diff] [blame] | 20 | #include "webrtc/rtc_base/flags.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 21 | #include "webrtc/rtc_base/md5digest.h" |
| 22 | #include "webrtc/rtc_base/stringencode.h" |
kwiberg | ac9f876 | 2016-09-30 22:29:43 -0700 | [diff] [blame] | 23 | #include "webrtc/test/gtest.h" |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 24 | #include "webrtc/test/testsupport/fileutils.h" |
| 25 | |
| 26 | DEFINE_bool(file_player_output, false, "Generate reference files."); |
| 27 | |
| 28 | namespace webrtc { |
| 29 | |
| 30 | class FilePlayerTest : public ::testing::Test { |
| 31 | protected: |
| 32 | static const uint32_t kId = 0; |
| 33 | static const FileFormats kFileFormat = kFileFormatWavFile; |
| 34 | static const int kSampleRateHz = 8000; |
| 35 | |
| 36 | FilePlayerTest() |
kwiberg | 5b356f4 | 2016-09-08 04:32:33 -0700 | [diff] [blame] | 37 | : player_(FilePlayer::CreateFilePlayer(kId, kFileFormat)), |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 38 | output_file_(NULL) {} |
| 39 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 40 | void SetUp() override { |
oprypin | 9b2f20c | 2017-08-29 05:51:57 -0700 | [diff] [blame] | 41 | if (FLAG_file_player_output) { |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 42 | std::string output_file = |
| 43 | webrtc::test::OutputPath() + "file_player_unittest_out.pcm"; |
| 44 | output_file_ = fopen(output_file.c_str(), "wb"); |
| 45 | ASSERT_TRUE(output_file_ != NULL); |
| 46 | } |
| 47 | } |
| 48 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 49 | void TearDown() override { |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 50 | if (output_file_) |
| 51 | fclose(output_file_); |
| 52 | } |
| 53 | |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 54 | void PlayFileAndCheck(const std::string& input_file, |
| 55 | const std::string& ref_checksum, |
| 56 | int output_length_ms) { |
| 57 | const float kScaling = 1; |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 58 | ASSERT_EQ(0, player_->StartPlayingFile(input_file.c_str(), false, 0, |
| 59 | kScaling, 0, 0, NULL)); |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 60 | rtc::Md5Digest checksum; |
| 61 | for (int i = 0; i < output_length_ms / 10; ++i) { |
| 62 | int16_t out[10 * kSampleRateHz / 1000] = {0}; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 63 | size_t num_samples; |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 64 | EXPECT_EQ( |
| 65 | 0, player_->Get10msAudioFromFile(out, &num_samples, kSampleRateHz)); |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 66 | checksum.Update(out, num_samples * sizeof(out[0])); |
oprypin | 9b2f20c | 2017-08-29 05:51:57 -0700 | [diff] [blame] | 67 | if (FLAG_file_player_output) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 68 | ASSERT_EQ(num_samples, |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 69 | fwrite(out, sizeof(out[0]), num_samples, output_file_)); |
| 70 | } |
| 71 | } |
| 72 | char checksum_result[rtc::Md5Digest::kSize]; |
| 73 | EXPECT_EQ(rtc::Md5Digest::kSize, |
| 74 | checksum.Finish(checksum_result, rtc::Md5Digest::kSize)); |
| 75 | EXPECT_EQ(ref_checksum, |
| 76 | rtc::hex_encode(checksum_result, sizeof(checksum_result))); |
| 77 | } |
| 78 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 79 | std::unique_ptr<FilePlayer> player_; |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 80 | FILE* output_file_; |
| 81 | }; |
| 82 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 83 | #if defined(WEBRTC_IOS) |
| 84 | #define MAYBE_PlayWavPcmuFile DISABLED_PlayWavPcmuFile |
| 85 | #else |
| 86 | #define MAYBE_PlayWavPcmuFile PlayWavPcmuFile |
| 87 | #endif |
| 88 | TEST_F(FilePlayerTest, MAYBE_PlayWavPcmuFile) { |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 89 | const std::string kFileName = |
| 90 | test::ResourcePath("utility/encapsulated_pcmu_8khz", "wav"); |
| 91 | // The file is longer than this, but keeping the output shorter limits the |
| 92 | // runtime for the test. |
| 93 | const int kOutputLengthMs = 10000; |
| 94 | const std::string kRefChecksum = "c74e7fd432d439b1311e1c16815b3e9a"; |
| 95 | |
| 96 | PlayFileAndCheck(kFileName, kRefChecksum, kOutputLengthMs); |
| 97 | } |
| 98 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 99 | #if defined(WEBRTC_IOS) |
| 100 | #define MAYBE_PlayWavPcm16File DISABLED_PlayWavPcm16File |
| 101 | #else |
| 102 | #define MAYBE_PlayWavPcm16File PlayWavPcm16File |
| 103 | #endif |
| 104 | TEST_F(FilePlayerTest, MAYBE_PlayWavPcm16File) { |
henrik.lundin@webrtc.org | 7476740 | 2014-05-26 13:37:45 +0000 | [diff] [blame] | 105 | const std::string kFileName = |
| 106 | test::ResourcePath("utility/encapsulated_pcm16b_8khz", "wav"); |
| 107 | // The file is longer than this, but keeping the output shorter limits the |
| 108 | // runtime for the test. |
| 109 | const int kOutputLengthMs = 10000; |
| 110 | const std::string kRefChecksum = "e41d7e1dac8aeae9f21e8e03cd7ecd71"; |
| 111 | |
| 112 | PlayFileAndCheck(kFileName, kRefChecksum, kOutputLengthMs); |
| 113 | } |
| 114 | |
| 115 | } // namespace webrtc |