1 /*
2  *  Copyright (c) 2015 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 "testing/gtest/include/gtest/gtest.h"
12 #include "webrtc/base/scoped_ptr.h"
13 #include "webrtc/modules/audio_coding/codecs/opus/interface/audio_encoder_opus.h"
14 
15 namespace webrtc {
16 
17 class AudioEncoderOpusTest : public ::testing::Test {
18  protected:
19   // The constructor simply creates an Opus encoder with default configuration.
AudioEncoderOpusTest()20   AudioEncoderOpusTest()
21       : opus_(new AudioEncoderOpus(AudioEncoderOpus::Config())) {}
22 
23   // Repeatedly sets packet loss rates in the range [from, to], increasing by
24   // 0.01 in each step. The function verifies that the actual loss rate is
25   // |expected_return|.
TestSetPacketLossRate(double from,double to,double expected_return)26   void TestSetPacketLossRate(double from, double to, double expected_return) {
27     ASSERT_TRUE(opus_);
28     for (double loss = from; loss <= to;
29          (to >= from) ? loss += 0.01 : loss -= 0.01) {
30       opus_->SetProjectedPacketLossRate(loss);
31       EXPECT_DOUBLE_EQ(expected_return, opus_->packet_loss_rate());
32     }
33   }
34 
35   rtc::scoped_ptr<AudioEncoderOpus> opus_;
36 };
37 
38 namespace {
39 // These constants correspond to those used in
40 // AudioEncoderOpus::SetProjectedPacketLossRate.
41 const double kPacketLossRate20 = 0.20;
42 const double kPacketLossRate10 = 0.10;
43 const double kPacketLossRate5 = 0.05;
44 const double kPacketLossRate1 = 0.01;
45 const double kLossRate20Margin = 0.02;
46 const double kLossRate10Margin = 0.01;
47 const double kLossRate5Margin = 0.01;
48 }  // namespace
49 
TEST_F(AudioEncoderOpusTest,PacketLossRateOptimized)50 TEST_F(AudioEncoderOpusTest, PacketLossRateOptimized) {
51   // Note that the order of the following calls is critical.
52   TestSetPacketLossRate(0.0, 0.0, 0.0);
53   TestSetPacketLossRate(kPacketLossRate1,
54                         kPacketLossRate5 + kLossRate5Margin - 0.01,
55                         kPacketLossRate1);
56   TestSetPacketLossRate(kPacketLossRate5 + kLossRate5Margin,
57                         kPacketLossRate10 + kLossRate10Margin - 0.01,
58                         kPacketLossRate5);
59   TestSetPacketLossRate(kPacketLossRate10 + kLossRate10Margin,
60                         kPacketLossRate20 + kLossRate20Margin - 0.01,
61                         kPacketLossRate10);
62   TestSetPacketLossRate(kPacketLossRate20 + kLossRate20Margin,
63                         1.0,
64                         kPacketLossRate20);
65   TestSetPacketLossRate(kPacketLossRate20 + kLossRate20Margin,
66                         kPacketLossRate20 - kLossRate20Margin,
67                         kPacketLossRate20);
68   TestSetPacketLossRate(kPacketLossRate20 - kLossRate20Margin - 0.01,
69                         kPacketLossRate10 - kLossRate10Margin,
70                         kPacketLossRate10);
71   TestSetPacketLossRate(kPacketLossRate10 - kLossRate10Margin - 0.01,
72                         kPacketLossRate5 - kLossRate5Margin,
73                         kPacketLossRate5);
74   TestSetPacketLossRate(kPacketLossRate5 - kLossRate5Margin - 0.01,
75                         kPacketLossRate1,
76                         kPacketLossRate1);
77   TestSetPacketLossRate(0.0, 0.0, 0.0);
78 }
79 
80 }  // namespace webrtc
81