2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
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.
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"
15 using google::RegisterFlagValidator;
16 using google::ParseCommandLineFlags;
18 using testing::InitGoogleTest;
23 static const int kOpusBlockDurationMs = 20;
24 static const int kOpusSamplingKhz = 48;
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");
33 printf("Invalid input filename.");
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).");
41 static const bool in_filename_dummy =
42 RegisterFlagValidator(&FLAGS_in_filename, &ValidateInFilename);
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");
51 printf("Invalid output filename.");
55 DEFINE_string(out_filename, OutputPath() + "neteq4_opus_fec_quality_test.pcm",
56 "Name of output audio file.");
58 static const bool out_filename_dummy =
59 RegisterFlagValidator(&FLAGS_out_filename, &ValidateOutFilename);
61 // Define switch for channels.
62 static bool ValidateChannels(const char* flagname, int32_t value) {
63 if (value == 1 || value == 2)
65 printf("Invalid number of channels, should be either 1 or 2.");
69 DEFINE_int32(channels, 1, "Number of channels in input audio.");
71 static const bool channels_dummy =
72 RegisterFlagValidator(&FLAGS_channels, &ValidateChannels);
74 // Define switch for bit rate.
75 static bool ValidateBitRate(const char* flagname, int32_t value) {
76 if (value >= 6 && value <= 510)
78 printf("Invalid bit rate, should be between 6 and 510 kbps.");
82 DEFINE_int32(bit_rate_kbps, 32, "Target bit rate (kbps).");
84 static const bool bit_rate_dummy =
85 RegisterFlagValidator(&FLAGS_bit_rate_kbps, &ValidateBitRate);
87 // Define switch for reported packet loss rate.
88 static bool ValidatePacketLossRate(const char* flagname, int32_t value) {
89 if (value >= 0 && value <= 100)
91 printf("Invalid packet loss percentile, should be between 0 and 100.");
95 DEFINE_int32(reported_loss_rate, 10, "Reported percentile of packet loss.");
97 static const bool reported_loss_rate_dummy =
98 RegisterFlagValidator(&FLAGS_reported_loss_rate, &ValidatePacketLossRate);
100 // Define switch for runtime.
101 static bool ValidateRuntime(const char* flagname, int32_t value) {
104 printf("Invalid runtime, should be greater than 0.");
108 DEFINE_int32(runtime_ms, 10000, "Simulated runtime (milliseconds).");
109 static const bool runtime_dummy =
110 RegisterFlagValidator(&FLAGS_runtime_ms, &ValidateRuntime);
112 DEFINE_bool(fec, true, "Whether to enable FEC for encoding.");
114 class NetEqOpusFecQualityTest : public NetEqQualityTest {
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);
122 WebRtcOpusEncInst* opus_encoder_;
126 int target_loss_rate_;
129 NetEqOpusFecQualityTest::NetEqOpusFecQualityTest()
130 : NetEqQualityTest(kOpusBlockDurationMs, kOpusSamplingKhz,
132 (FLAGS_channels == 1) ? kDecoderOpus : kDecoderOpus_2ch,
137 channels_(FLAGS_channels),
138 bit_rate_kbps_(FLAGS_bit_rate_kbps),
140 target_loss_rate_(FLAGS_reported_loss_rate) {
143 void NetEqOpusFecQualityTest::SetUp() {
144 // Create encoder memory.
145 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_);
146 ASSERT_TRUE(opus_encoder_ != NULL);
148 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, bit_rate_kbps_ * 1000));
150 EXPECT_EQ(0, WebRtcOpus_EnableFec(opus_encoder_));
152 EXPECT_EQ(0, WebRtcOpus_SetPacketLossRate(opus_encoder_,
154 NetEqQualityTest::SetUp();
157 void NetEqOpusFecQualityTest::TearDown() {
159 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
160 NetEqQualityTest::TearDown();
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,
173 TEST_F(NetEqOpusFecQualityTest, Test) {
174 Simulate(FLAGS_runtime_ms);
178 } // namespace webrtc