blob: c5f6fbaa55d03d26a3a102786e96aa6b78b3a680 [file] [log] [blame]
henrik.lundin@webrtc.org74767402014-05-26 13:37:45 +00001/*
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
13#include "webrtc/modules/utility/interface/file_player.h"
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"
henrikaa2c79402015-06-10 13:24:48 +020023#include "webrtc/test/testsupport/gtest_disable.h"
henrik.lundin@webrtc.org74767402014-05-26 13:37:45 +000024
25DEFINE_bool(file_player_output, false, "Generate reference files.");
26
27namespace webrtc {
28
29class FilePlayerTest : public ::testing::Test {
30 protected:
31 static const uint32_t kId = 0;
32 static const FileFormats kFileFormat = kFileFormatWavFile;
33 static const int kSampleRateHz = 8000;
34
35 FilePlayerTest()
36 : player_(FilePlayer::CreateFilePlayer(kId, kFileFormat)),
37 output_file_(NULL) {}
38
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000039 void SetUp() override {
henrik.lundin@webrtc.org74767402014-05-26 13:37:45 +000040 if (FLAGS_file_player_output) {
41 std::string output_file =
42 webrtc::test::OutputPath() + "file_player_unittest_out.pcm";
43 output_file_ = fopen(output_file.c_str(), "wb");
44 ASSERT_TRUE(output_file_ != NULL);
45 }
46 }
47
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000048 void TearDown() override {
henrik.lundin@webrtc.org74767402014-05-26 13:37:45 +000049 if (output_file_)
50 fclose(output_file_);
51 }
52
53 ~FilePlayerTest() { FilePlayer::DestroyFilePlayer(player_); }
54
55 void PlayFileAndCheck(const std::string& input_file,
56 const std::string& ref_checksum,
57 int output_length_ms) {
58 const float kScaling = 1;
59 ASSERT_EQ(0,
60 player_->StartPlayingFile(
61 input_file.c_str(), false, 0, kScaling, 0, 0, NULL));
62 rtc::Md5Digest checksum;
63 for (int i = 0; i < output_length_ms / 10; ++i) {
64 int16_t out[10 * kSampleRateHz / 1000] = {0};
65 int num_samples;
66 EXPECT_EQ(0,
67 player_->Get10msAudioFromFile(out, num_samples, kSampleRateHz));
68 checksum.Update(out, num_samples * sizeof(out[0]));
69 if (FLAGS_file_player_output) {
70 ASSERT_EQ(static_cast<size_t>(num_samples),
71 fwrite(out, sizeof(out[0]), num_samples, output_file_));
72 }
73 }
74 char checksum_result[rtc::Md5Digest::kSize];
75 EXPECT_EQ(rtc::Md5Digest::kSize,
76 checksum.Finish(checksum_result, rtc::Md5Digest::kSize));
77 EXPECT_EQ(ref_checksum,
78 rtc::hex_encode(checksum_result, sizeof(checksum_result)));
79 }
80
81 FilePlayer* player_;
82 FILE* output_file_;
83};
84
henrikaa2c79402015-06-10 13:24:48 +020085TEST_F(FilePlayerTest, DISABLED_ON_IOS(PlayWavPcmuFile)) {
henrik.lundin@webrtc.org74767402014-05-26 13:37:45 +000086 const std::string kFileName =
87 test::ResourcePath("utility/encapsulated_pcmu_8khz", "wav");
88 // The file is longer than this, but keeping the output shorter limits the
89 // runtime for the test.
90 const int kOutputLengthMs = 10000;
91 const std::string kRefChecksum = "c74e7fd432d439b1311e1c16815b3e9a";
92
93 PlayFileAndCheck(kFileName, kRefChecksum, kOutputLengthMs);
94}
95
henrikaa2c79402015-06-10 13:24:48 +020096TEST_F(FilePlayerTest, DISABLED_ON_IOS(PlayWavPcm16File)) {
henrik.lundin@webrtc.org74767402014-05-26 13:37:45 +000097 const std::string kFileName =
98 test::ResourcePath("utility/encapsulated_pcm16b_8khz", "wav");
99 // The file is longer than this, but keeping the output shorter limits the
100 // runtime for the test.
101 const int kOutputLengthMs = 10000;
102 const std::string kRefChecksum = "e41d7e1dac8aeae9f21e8e03cd7ecd71";
103
104 PlayFileAndCheck(kFileName, kRefChecksum, kOutputLengthMs);
105}
106
107} // namespace webrtc