Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(674)

Side by Side Diff: webrtc/modules/audio_coding/neteq/tools/neteq_quality_test.cc

Issue 2995363002: Replace gflags usages with rtc_base/flags in all targets based on test_main (Closed)
Patch Set: Fix flag validation message Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include <math.h> 11 #include <math.h>
12 #include <stdio.h> 12 #include <stdio.h>
13 13
14 #include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.h" 14 #include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.h"
15 #include "webrtc/modules/audio_coding/neteq/tools/neteq_quality_test.h" 15 #include "webrtc/modules/audio_coding/neteq/tools/neteq_quality_test.h"
16 #include "webrtc/modules/audio_coding/neteq/tools/output_audio_file.h" 16 #include "webrtc/modules/audio_coding/neteq/tools/output_audio_file.h"
17 #include "webrtc/modules/audio_coding/neteq/tools/output_wav_file.h" 17 #include "webrtc/modules/audio_coding/neteq/tools/output_wav_file.h"
18 #include "webrtc/modules/audio_coding/neteq/tools/resample_input_audio_file.h" 18 #include "webrtc/modules/audio_coding/neteq/tools/resample_input_audio_file.h"
19 #include "webrtc/rtc_base/checks.h" 19 #include "webrtc/rtc_base/checks.h"
20 #include "webrtc/test/testsupport/fileutils.h" 20 #include "webrtc/test/testsupport/fileutils.h"
21 21
22 namespace webrtc { 22 namespace webrtc {
23 namespace test { 23 namespace test {
24 24
25 const uint8_t kPayloadType = 95; 25 const uint8_t kPayloadType = 95;
26 const int kOutputSizeMs = 10; 26 const int kOutputSizeMs = 10;
27 const int kInitSeed = 0x12345678; 27 const int kInitSeed = 0x12345678;
28 const int kPacketLossTimeUnitMs = 10; 28 const int kPacketLossTimeUnitMs = 10;
29 const std::string kDefaultInFilename =
30 ResourcePath("audio_coding/speech_mono_16kHz", "pcm");
31 const std::string kDefaultOutFilename =
32 OutputPath() + "neteq_quality_test_out.pcm";
kwiberg-webrtc 2017/08/24 09:03:44 "Variables of class type with static storage durat
oprypin_webrtc 2017/08/26 07:38:52 Done. Thanks for also providing the solution :)
29 33
30 // Common validator for file names. 34 // Common validator for file names.
31 static bool ValidateFilename(const std::string& value, bool write) { 35 static bool ValidateFilename(const std::string& value, bool write) {
32 FILE* fid = write ? fopen(value.c_str(), "wb") : fopen(value.c_str(), "rb"); 36 FILE* fid = write ? fopen(value.c_str(), "wb") : fopen(value.c_str(), "rb");
33 if (fid == nullptr) 37 if (fid == nullptr)
34 return false; 38 return false;
35 fclose(fid); 39 fclose(fid);
36 return true; 40 return true;
37 } 41 }
38 42
39 // Define switch for input file name. 43 DEFINE_string(in_filename, kDefaultInFilename.c_str(),
40 static bool ValidateInFilename(const char* flagname, const std::string& value) { 44 "Filename for input audio (specify sample rate with --input_sample_rate, "
41 if (!ValidateFilename(value, false)) {
42 printf("Invalid input filename.");
43 return false;
44 }
45 return true;
46 }
47
48 DEFINE_string(
49 in_filename,
50 ResourcePath("audio_coding/speech_mono_16kHz", "pcm"),
51 "Filename for input audio (specify sample rate with --input_sample_rate ,"
52 "and channels with --channels)."); 45 "and channels with --channels).");
53 46
54 static const bool in_filename_dummy = 47 DEFINE_int(input_sample_rate, 16000, "Sample rate of input file in Hz.");
55 RegisterFlagValidator(&FLAGS_in_filename, &ValidateInFilename);
56 48
57 // Define switch for sample rate. 49 DEFINE_int(channels, 1, "Number of channels in input audio.");
58 static bool ValidateSampleRate(const char* flagname, int32_t value) {
59 if (value == 8000 || value == 16000 || value == 32000 || value == 48000)
60 return true;
61 printf("Invalid sample rate should be 8000, 16000, 32000 or 48000 Hz.");
62 return false;
63 }
64 50
65 DEFINE_int32(input_sample_rate, 16000, "Sample rate of input file in Hz."); 51 DEFINE_string(out_filename, kDefaultOutFilename.c_str(),
52 "Name of output audio file.");
66 53
67 static const bool sample_rate_dummy = 54 DEFINE_int(runtime_ms, 10000, "Simulated runtime (milliseconds).");
68 RegisterFlagValidator(&FLAGS_input_sample_rate, &ValidateSampleRate);
69 55
70 // Define switch for channels. 56 DEFINE_int(packet_loss_rate, 10, "Percentile of packet loss.");
71 static bool ValidateChannels(const char* flagname, int32_t value) {
72 if (value == 1)
73 return true;
74 printf("Invalid number of channels, current support only 1.");
75 return false;
76 }
77 57
78 DEFINE_int32(channels, 1, "Number of channels in input audio."); 58 DEFINE_int(random_loss_mode, 1,
59 "Random loss mode: 0--no loss, 1--uniform loss, 2--Gilbert Elliot loss.");
79 60
80 static const bool channels_dummy = 61 DEFINE_int(burst_length, 30,
81 RegisterFlagValidator(&FLAGS_channels, &ValidateChannels);
82
83 // Define switch for output file name.
84 static bool ValidateOutFilename(const char* flagname,
85 const std::string& value) {
86 if (!ValidateFilename(value, true)) {
87 printf("Invalid output filename.");
88 return false;
89 }
90 return true;
91 }
92
93 DEFINE_string(out_filename,
94 OutputPath() + "neteq_quality_test_out.pcm",
95 "Name of output audio file.");
96
97 static const bool out_filename_dummy =
98 RegisterFlagValidator(&FLAGS_out_filename, &ValidateOutFilename);
99
100 // Define switch for packet loss rate.
101 static bool ValidatePacketLossRate(const char* /* flag_name */, int32_t value) {
102 if (value >= 0 && value <= 100)
103 return true;
104 printf("Invalid packet loss percentile, should be between 0 and 100.");
105 return false;
106 }
107
108 // Define switch for runtime.
109 static bool ValidateRuntime(const char* flagname, int32_t value) {
110 if (value > 0)
111 return true;
112 printf("Invalid runtime, should be greater than 0.");
113 return false;
114 }
115
116 DEFINE_int32(runtime_ms, 10000, "Simulated runtime (milliseconds).");
117
118 static const bool runtime_dummy =
119 RegisterFlagValidator(&FLAGS_runtime_ms, &ValidateRuntime);
120
121 DEFINE_int32(packet_loss_rate, 10, "Percentile of packet loss.");
122
123 static const bool packet_loss_rate_dummy =
124 RegisterFlagValidator(&FLAGS_packet_loss_rate, &ValidatePacketLossRate);
125
126 // Define switch for random loss mode.
127 static bool ValidateRandomLossMode(const char* /* flag_name */, int32_t value) {
128 if (value >= 0 && value <= 2)
129 return true;
130 printf("Invalid random packet loss mode, should be between 0 and 2.");
131 return false;
132 }
133
134 DEFINE_int32(random_loss_mode, 1,
135 "Random loss mode: 0--no loss, 1--uniform loss, 2--Gilbert Elliot loss.");
136 static const bool random_loss_mode_dummy =
137 RegisterFlagValidator(&FLAGS_random_loss_mode, &ValidateRandomLossMode);
138
139 // Define switch for burst length.
140 static bool ValidateBurstLength(const char* /* flag_name */, int32_t value) {
141 if (value >= kPacketLossTimeUnitMs)
142 return true;
143 printf("Invalid burst length, should be greater than %d ms.",
144 kPacketLossTimeUnitMs);
145 return false;
146 }
147
148 DEFINE_int32(burst_length, 30,
149 "Burst length in milliseconds, only valid for Gilbert Elliot loss."); 62 "Burst length in milliseconds, only valid for Gilbert Elliot loss.");
150 63
151 static const bool burst_length_dummy = 64 DEFINE_float(drift_factor, 0.0, "Time drift factor.");
152 RegisterFlagValidator(&FLAGS_burst_length, &ValidateBurstLength);
153
154 // Define switch for drift factor.
155 static bool ValidateDriftFactor(const char* /* flag_name */, double value) {
156 if (value > -0.1)
157 return true;
158 printf("Invalid drift factor, should be greater than -0.1.");
159 return false;
160 }
161
162 DEFINE_double(drift_factor, 0.0, "Time drift factor.");
163
164 static const bool drift_factor_dummy =
165 RegisterFlagValidator(&FLAGS_drift_factor, &ValidateDriftFactor);
166 65
167 // ProbTrans00Solver() is to calculate the transition probability from no-loss 66 // ProbTrans00Solver() is to calculate the transition probability from no-loss
168 // state to itself in a modified Gilbert Elliot packet loss model. The result is 67 // state to itself in a modified Gilbert Elliot packet loss model. The result is
169 // to achieve the target packet loss rate |loss_rate|, when a packet is not 68 // to achieve the target packet loss rate |loss_rate|, when a packet is not
170 // lost only if all |units| drawings within the duration of the packet result in 69 // lost only if all |units| drawings within the duration of the packet result in
171 // no-loss. 70 // no-loss.
172 static double ProbTrans00Solver(int units, double loss_rate, 71 static double ProbTrans00Solver(int units, double loss_rate,
173 double prob_trans_10) { 72 double prob_trans_10) {
174 if (units == 1) 73 if (units == 1)
175 return prob_trans_10 / (1.0f - loss_rate) - prob_trans_10; 74 return prob_trans_10 / (1.0f - loss_rate) - prob_trans_10;
(...skipping 28 matching lines...) Expand all
204 iter ++; 103 iter ++;
205 } 104 }
206 return x; 105 return x;
207 } 106 }
208 107
209 NetEqQualityTest::NetEqQualityTest(int block_duration_ms, 108 NetEqQualityTest::NetEqQualityTest(int block_duration_ms,
210 int in_sampling_khz, 109 int in_sampling_khz,
211 int out_sampling_khz, 110 int out_sampling_khz,
212 NetEqDecoder decoder_type) 111 NetEqDecoder decoder_type)
213 : decoder_type_(decoder_type), 112 : decoder_type_(decoder_type),
214 channels_(static_cast<size_t>(FLAGS_channels)), 113 channels_(static_cast<size_t>(FLAG_channels)),
215 decoded_time_ms_(0), 114 decoded_time_ms_(0),
216 decodable_time_ms_(0), 115 decodable_time_ms_(0),
217 drift_factor_(FLAGS_drift_factor), 116 drift_factor_(FLAG_drift_factor),
218 packet_loss_rate_(FLAGS_packet_loss_rate), 117 packet_loss_rate_(FLAG_packet_loss_rate),
219 block_duration_ms_(block_duration_ms), 118 block_duration_ms_(block_duration_ms),
220 in_sampling_khz_(in_sampling_khz), 119 in_sampling_khz_(in_sampling_khz),
221 out_sampling_khz_(out_sampling_khz), 120 out_sampling_khz_(out_sampling_khz),
222 in_size_samples_( 121 in_size_samples_(
223 static_cast<size_t>(in_sampling_khz_ * block_duration_ms_)), 122 static_cast<size_t>(in_sampling_khz_ * block_duration_ms_)),
224 payload_size_bytes_(0), 123 payload_size_bytes_(0),
225 max_payload_bytes_(0), 124 max_payload_bytes_(0),
226 in_file_(new ResampleInputAudioFile(FLAGS_in_filename, 125 in_file_(new ResampleInputAudioFile(FLAG_in_filename,
227 FLAGS_input_sample_rate, 126 FLAG_input_sample_rate,
228 in_sampling_khz * 1000)), 127 in_sampling_khz * 1000)),
229 rtp_generator_( 128 rtp_generator_(
230 new RtpGenerator(in_sampling_khz_, 0, 0, decodable_time_ms_)), 129 new RtpGenerator(in_sampling_khz_, 0, 0, decodable_time_ms_)),
231 total_payload_size_bytes_(0) { 130 total_payload_size_bytes_(0) {
232 const std::string out_filename = FLAGS_out_filename; 131 // Flag validation
132 RTC_CHECK(ValidateFilename(FLAG_in_filename, false))
133 << "Invalid input filename.";
134
135 RTC_CHECK(FLAG_input_sample_rate == 8000 || FLAG_input_sample_rate == 16000 ||
136 FLAG_input_sample_rate == 32000 || FLAG_input_sample_rate == 48000)
137 << "Invalid sample rate should be 8000, 16000, 32000 or 48000 Hz.";
138
139 RTC_CHECK_EQ(FLAG_channels, 1)
140 << "Invalid number of channels, current support only 1.";
141
142 RTC_CHECK(ValidateFilename(FLAG_out_filename, true))
143 << "Invalid output filename.";
144
145 RTC_CHECK_GT(FLAG_runtime_ms, 0)
146 << "Invalid runtime, should be greater than 0.";
147
148 RTC_CHECK(FLAG_packet_loss_rate >= 0 && FLAG_packet_loss_rate <= 100)
149 << "Invalid packet loss percentile, should be between 0 and 100.";
150
151 RTC_CHECK(FLAG_random_loss_mode >= 0 && FLAG_random_loss_mode <= 2)
152 << "Invalid random packet loss mode, should be between 0 and 2.";
153
154 RTC_CHECK_GE(FLAG_burst_length, kPacketLossTimeUnitMs)
155 << "Invalid burst length, should be greater than or equal to "
156 << kPacketLossTimeUnitMs << " ms.";
157
158 RTC_CHECK_GT(FLAG_drift_factor, -0.1)
159 << "Invalid drift factor, should be greater than -0.1.";
160
161 const std::string out_filename = FLAG_out_filename;
233 const std::string log_filename = out_filename + ".log"; 162 const std::string log_filename = out_filename + ".log";
234 log_file_.open(log_filename.c_str(), std::ofstream::out); 163 log_file_.open(log_filename.c_str(), std::ofstream::out);
235 RTC_CHECK(log_file_.is_open()); 164 RTC_CHECK(log_file_.is_open());
236 165
237 if (out_filename.size() >= 4 && 166 if (out_filename.size() >= 4 &&
238 out_filename.substr(out_filename.size() - 4) == ".wav") { 167 out_filename.substr(out_filename.size() - 4) == ".wav") {
239 // Open a wav file. 168 // Open a wav file.
240 output_.reset( 169 output_.reset(
241 new webrtc::test::OutputWavFile(out_filename, 1000 * out_sampling_khz)); 170 new webrtc::test::OutputWavFile(out_filename, 1000 * out_sampling_khz));
242 } else { 171 } else {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 return lost_last_ = uniform_loss_model_->Lost(); 220 return lost_last_ = uniform_loss_model_->Lost();
292 } 221 }
293 } 222 }
294 223
295 void NetEqQualityTest::SetUp() { 224 void NetEqQualityTest::SetUp() {
296 ASSERT_EQ(0, 225 ASSERT_EQ(0,
297 neteq_->RegisterPayloadType(decoder_type_, "noname", kPayloadType)); 226 neteq_->RegisterPayloadType(decoder_type_, "noname", kPayloadType));
298 rtp_generator_->set_drift_factor(drift_factor_); 227 rtp_generator_->set_drift_factor(drift_factor_);
299 228
300 int units = block_duration_ms_ / kPacketLossTimeUnitMs; 229 int units = block_duration_ms_ / kPacketLossTimeUnitMs;
301 switch (FLAGS_random_loss_mode) { 230 switch (FLAG_random_loss_mode) {
302 case 1: { 231 case 1: {
303 // |unit_loss_rate| is the packet loss rate for each unit time interval 232 // |unit_loss_rate| is the packet loss rate for each unit time interval
304 // (kPacketLossTimeUnitMs). Since a packet loss event is generated if any 233 // (kPacketLossTimeUnitMs). Since a packet loss event is generated if any
305 // of |block_duration_ms_ / kPacketLossTimeUnitMs| unit time intervals of 234 // of |block_duration_ms_ / kPacketLossTimeUnitMs| unit time intervals of
306 // a full packet duration is drawn with a loss, |unit_loss_rate| fulfills 235 // a full packet duration is drawn with a loss, |unit_loss_rate| fulfills
307 // (1 - unit_loss_rate) ^ (block_duration_ms_ / kPacketLossTimeUnitMs) == 236 // (1 - unit_loss_rate) ^ (block_duration_ms_ / kPacketLossTimeUnitMs) ==
308 // 1 - packet_loss_rate. 237 // 1 - packet_loss_rate.
309 double unit_loss_rate = (1.0f - pow(1.0f - 0.01f * packet_loss_rate_, 238 double unit_loss_rate = (1.0f - pow(1.0f - 0.01f * packet_loss_rate_,
310 1.0f / units)); 239 1.0f / units));
311 loss_model_.reset(new UniformLoss(unit_loss_rate)); 240 loss_model_.reset(new UniformLoss(unit_loss_rate));
312 break; 241 break;
313 } 242 }
314 case 2: { 243 case 2: {
315 // |FLAGS_burst_length| should be integer times of kPacketLossTimeUnitMs. 244 // |FLAG_burst_length| should be integer times of kPacketLossTimeUnitMs.
316 ASSERT_EQ(0, FLAGS_burst_length % kPacketLossTimeUnitMs); 245 ASSERT_EQ(0, FLAG_burst_length % kPacketLossTimeUnitMs);
317 246
318 // We do not allow 100 percent packet loss in Gilbert Elliot model, which 247 // We do not allow 100 percent packet loss in Gilbert Elliot model, which
319 // makes no sense. 248 // makes no sense.
320 ASSERT_GT(100, packet_loss_rate_); 249 ASSERT_GT(100, packet_loss_rate_);
321 250
322 // To guarantee the overall packet loss rate, transition probabilities 251 // To guarantee the overall packet loss rate, transition probabilities
323 // need to satisfy: 252 // need to satisfy:
324 // pi_0 * (1 - prob_trans_01_) ^ units + 253 // pi_0 * (1 - prob_trans_01_) ^ units +
325 // pi_1 * prob_trans_10_ ^ (units - 1) == 1 - loss_rate 254 // pi_1 * prob_trans_10_ ^ (units - 1) == 1 - loss_rate
326 // pi_0 = prob_trans_10 / (prob_trans_10 + prob_trans_01_) 255 // pi_0 = prob_trans_10 / (prob_trans_10 + prob_trans_01_)
327 // is the stationary state probability of no-loss 256 // is the stationary state probability of no-loss
328 // pi_1 = prob_trans_01_ / (prob_trans_10 + prob_trans_01_) 257 // pi_1 = prob_trans_01_ / (prob_trans_10 + prob_trans_01_)
329 // is the stationary state probability of loss 258 // is the stationary state probability of loss
330 // After a derivation prob_trans_00 should satisfy: 259 // After a derivation prob_trans_00 should satisfy:
331 // prob_trans_00 ^ (units - 1) = (loss_rate - 1) / prob_trans_10 * 260 // prob_trans_00 ^ (units - 1) = (loss_rate - 1) / prob_trans_10 *
332 // prob_trans_00 + (1 - loss_rate) * (1 + 1 / prob_trans_10). 261 // prob_trans_00 + (1 - loss_rate) * (1 + 1 / prob_trans_10).
333 double loss_rate = 0.01f * packet_loss_rate_; 262 double loss_rate = 0.01f * packet_loss_rate_;
334 double prob_trans_10 = 1.0f * kPacketLossTimeUnitMs / FLAGS_burst_length; 263 double prob_trans_10 = 1.0f * kPacketLossTimeUnitMs / FLAG_burst_length;
335 double prob_trans_00 = ProbTrans00Solver(units, loss_rate, prob_trans_10); 264 double prob_trans_00 = ProbTrans00Solver(units, loss_rate, prob_trans_10);
336 loss_model_.reset(new GilbertElliotLoss(1.0f - prob_trans_10, 265 loss_model_.reset(new GilbertElliotLoss(1.0f - prob_trans_10,
337 1.0f - prob_trans_00)); 266 1.0f - prob_trans_00));
338 break; 267 break;
339 } 268 }
340 default: { 269 default: {
341 loss_model_.reset(new NoLoss); 270 loss_model_.reset(new NoLoss);
342 break; 271 break;
343 } 272 }
344 } 273 }
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 RTC_CHECK(output_->WriteArray( 337 RTC_CHECK(output_->WriteArray(
409 out_frame_.data(), 338 out_frame_.data(),
410 out_frame_.samples_per_channel_ * out_frame_.num_channels_)); 339 out_frame_.samples_per_channel_ * out_frame_.num_channels_));
411 return static_cast<int>(out_frame_.samples_per_channel_); 340 return static_cast<int>(out_frame_.samples_per_channel_);
412 } 341 }
413 } 342 }
414 343
415 void NetEqQualityTest::Simulate() { 344 void NetEqQualityTest::Simulate() {
416 int audio_size_samples; 345 int audio_size_samples;
417 346
418 while (decoded_time_ms_ < FLAGS_runtime_ms) { 347 while (decoded_time_ms_ < FLAG_runtime_ms) {
419 // Assume 10 packets in packets buffer. 348 // Assume 10 packets in packets buffer.
420 while (decodable_time_ms_ - 10 * block_duration_ms_ < decoded_time_ms_) { 349 while (decodable_time_ms_ - 10 * block_duration_ms_ < decoded_time_ms_) {
421 ASSERT_TRUE(in_file_->Read(in_size_samples_ * channels_, &in_data_[0])); 350 ASSERT_TRUE(in_file_->Read(in_size_samples_ * channels_, &in_data_[0]));
422 payload_.Clear(); 351 payload_.Clear();
423 payload_size_bytes_ = EncodeBlock(&in_data_[0], 352 payload_size_bytes_ = EncodeBlock(&in_data_[0],
424 in_size_samples_, &payload_, 353 in_size_samples_, &payload_,
425 max_payload_bytes_); 354 max_payload_bytes_);
426 total_payload_size_bytes_ += payload_size_bytes_; 355 total_payload_size_bytes_ += payload_size_bytes_;
427 decodable_time_ms_ = Transmit() + block_duration_ms_; 356 decodable_time_ms_ = Transmit() + block_duration_ms_;
428 } 357 }
429 audio_size_samples = DecodeBlock(); 358 audio_size_samples = DecodeBlock();
430 if (audio_size_samples > 0) { 359 if (audio_size_samples > 0) {
431 decoded_time_ms_ += audio_size_samples / out_sampling_khz_; 360 decoded_time_ms_ += audio_size_samples / out_sampling_khz_;
432 } 361 }
433 } 362 }
434 Log() << "Average bit rate was " 363 Log() << "Average bit rate was "
435 << 8.0f * total_payload_size_bytes_ / FLAGS_runtime_ms 364 << 8.0f * total_payload_size_bytes_ / FLAG_runtime_ms
436 << " kbps" 365 << " kbps"
437 << std::endl; 366 << std::endl;
438 } 367 }
439 368
440 } // namespace test 369 } // namespace test
441 } // namespace webrtc 370 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698