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