Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / neteq / test / neteq_opus_fec_quality_test.cc
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 #include "webrtc/modules/audio_coding/codecs/opus/interface/opus_interface.h"
12 #include "webrtc/modules/audio_coding/neteq/tools/neteq_quality_test.h"
13 #include "webrtc/test/testsupport/fileutils.h"
14
15 using google::RegisterFlagValidator;
16 using google::ParseCommandLineFlags;
17 using std::string;
18 using testing::InitGoogleTest;
19
20 namespace webrtc {
21 namespace test {
22
23 static const int kOpusBlockDurationMs = 20;
24 static const int kOpusSamplingKhz = 48;
25
26 // Define switch for input file name.
27 static bool ValidateInFilename(const char* flagname, const string& value) {
28   FILE* fid = fopen(value.c_str(), "rb");
29   if (fid != NULL) {
30     fclose(fid);
31     return true;
32   }
33   printf("Invalid input filename.");
34   return false;
35 }
36
37 DEFINE_string(in_filename,
38               ResourcePath("audio_coding/speech_mono_32_48kHz", "pcm"),
39               "Filename for input audio (should be 48 kHz sampled raw data).");
40
41 static const bool in_filename_dummy =
42     RegisterFlagValidator(&FLAGS_in_filename, &ValidateInFilename);
43
44 // Define switch for output file name.
45 static bool ValidateOutFilename(const char* flagname, const string& value) {
46   FILE* fid = fopen(value.c_str(), "wb");
47   if (fid != NULL) {
48     fclose(fid);
49     return true;
50   }
51   printf("Invalid output filename.");
52   return false;
53 }
54
55 DEFINE_string(out_filename, OutputPath() + "neteq4_opus_fec_quality_test.pcm",
56               "Name of output audio file.");
57
58 static const bool out_filename_dummy =
59     RegisterFlagValidator(&FLAGS_out_filename, &ValidateOutFilename);
60
61 // Define switch for channels.
62 static bool ValidateChannels(const char* flagname, int32_t value) {
63   if (value == 1 || value == 2)
64     return true;
65   printf("Invalid number of channels, should be either 1 or 2.");
66   return false;
67 }
68
69 DEFINE_int32(channels, 1, "Number of channels in input audio.");
70
71 static const bool channels_dummy =
72     RegisterFlagValidator(&FLAGS_channels, &ValidateChannels);
73
74 // Define switch for bit rate.
75 static bool ValidateBitRate(const char* flagname, int32_t value) {
76   if (value >= 6 && value <= 510)
77     return true;
78   printf("Invalid bit rate, should be between 6 and 510 kbps.");
79   return false;
80 }
81
82 DEFINE_int32(bit_rate_kbps, 32, "Target bit rate (kbps).");
83
84 static const bool bit_rate_dummy =
85     RegisterFlagValidator(&FLAGS_bit_rate_kbps, &ValidateBitRate);
86
87 // Define switch for reported packet loss rate.
88 static bool ValidatePacketLossRate(const char* flagname, int32_t value) {
89   if (value >= 0 && value <= 100)
90     return true;
91   printf("Invalid packet loss percentile, should be between 0 and 100.");
92   return false;
93 }
94
95 DEFINE_int32(reported_loss_rate, 10, "Reported percentile of packet loss.");
96
97 static const bool reported_loss_rate_dummy =
98     RegisterFlagValidator(&FLAGS_reported_loss_rate, &ValidatePacketLossRate);
99
100 // Define switch for runtime.
101 static bool ValidateRuntime(const char* flagname, int32_t value) {
102   if (value > 0)
103     return true;
104   printf("Invalid runtime, should be greater than 0.");
105   return false;
106 }
107
108 DEFINE_int32(runtime_ms, 10000, "Simulated runtime (milliseconds).");
109 static const bool runtime_dummy =
110     RegisterFlagValidator(&FLAGS_runtime_ms, &ValidateRuntime);
111
112 DEFINE_bool(fec, true, "Whether to enable FEC for encoding.");
113
114 class NetEqOpusFecQualityTest : public NetEqQualityTest {
115  protected:
116   NetEqOpusFecQualityTest();
117   virtual void SetUp() OVERRIDE;
118   virtual void TearDown() OVERRIDE;
119   virtual int EncodeBlock(int16_t* in_data, int block_size_samples,
120                           uint8_t* payload, int max_bytes);
121  private:
122   WebRtcOpusEncInst* opus_encoder_;
123   int channels_;
124   int bit_rate_kbps_;
125   bool fec_;
126   int target_loss_rate_;
127 };
128
129 NetEqOpusFecQualityTest::NetEqOpusFecQualityTest()
130     : NetEqQualityTest(kOpusBlockDurationMs, kOpusSamplingKhz,
131                        kOpusSamplingKhz,
132                        (FLAGS_channels == 1) ? kDecoderOpus : kDecoderOpus_2ch,
133                        FLAGS_channels,
134                        FLAGS_in_filename,
135                        FLAGS_out_filename),
136       opus_encoder_(NULL),
137       channels_(FLAGS_channels),
138       bit_rate_kbps_(FLAGS_bit_rate_kbps),
139       fec_(FLAGS_fec),
140       target_loss_rate_(FLAGS_reported_loss_rate) {
141 }
142
143 void NetEqOpusFecQualityTest::SetUp() {
144   // Create encoder memory.
145   WebRtcOpus_EncoderCreate(&opus_encoder_, channels_);
146   ASSERT_TRUE(opus_encoder_ != NULL);
147   // Set bitrate.
148   EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, bit_rate_kbps_ * 1000));
149   if (fec_) {
150     EXPECT_EQ(0, WebRtcOpus_EnableFec(opus_encoder_));
151   }
152   EXPECT_EQ(0, WebRtcOpus_SetPacketLossRate(opus_encoder_,
153                                             target_loss_rate_));
154   NetEqQualityTest::SetUp();
155 }
156
157 void NetEqOpusFecQualityTest::TearDown() {
158   // Free memory.
159   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
160   NetEqQualityTest::TearDown();
161 }
162
163 int NetEqOpusFecQualityTest::EncodeBlock(int16_t* in_data,
164                                          int block_size_samples,
165                                          uint8_t* payload, int max_bytes) {
166   int value = WebRtcOpus_Encode(opus_encoder_, in_data,
167                                 block_size_samples, max_bytes,
168                                 payload);
169   EXPECT_GT(value, 0);
170   return value;
171 }
172
173 TEST_F(NetEqOpusFecQualityTest, Test) {
174   Simulate(FLAGS_runtime_ms);
175 }
176
177 }  // namespace test
178 }  // namespace webrtc