Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / main / acm2 / acm_opus_unittest.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/main/acm2/acm_opus.h"
12
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webrtc/modules/audio_coding/main/acm2/acm_codec_database.h"
15
16 namespace webrtc {
17
18 namespace acm2 {
19
20 namespace {
21   const CodecInst kOpusCodecInst = {105, "opus", 48000, 960, 1, 32000};
22   // These constants correspond to those used in ACMOpus::SetPacketLossRate().
23   const int kPacketLossRate20 = 20;
24   const int kPacketLossRate10 = 10;
25   const int kPacketLossRate5 = 5;
26   const int kPacketLossRate1 = 1;
27   const int kLossRate20Margin = 2;
28   const int kLossRate10Margin = 1;
29   const int kLossRate5Margin = 1;
30 }  // namespace
31
32 class AcmOpusTest : public ACMOpus {
33  public:
34   explicit AcmOpusTest(int16_t codec_id)
35       : ACMOpus(codec_id) {}
36   ~AcmOpusTest() {}
37   int packet_loss_rate() { return packet_loss_rate_; }
38
39   void TestSetPacketLossRate(int from, int to, int expected_return);
40 };
41
42 #ifdef WEBRTC_CODEC_OPUS
43 void AcmOpusTest::TestSetPacketLossRate(int from, int to, int expected_return) {
44   for (int loss = from; loss <= to; (to >= from) ? ++loss : --loss) {
45     EXPECT_EQ(0, SetPacketLossRate(loss));
46     EXPECT_EQ(expected_return, packet_loss_rate());
47   }
48 }
49
50 TEST(AcmOpusTest, PacketLossRateOptimized) {
51   AcmOpusTest opus(ACMCodecDB::kOpus);
52   WebRtcACMCodecParams params;
53   memcpy(&(params.codec_inst), &kOpusCodecInst, sizeof(CodecInst));
54   EXPECT_EQ(0, opus.InitEncoder(&params, true));
55   EXPECT_EQ(0, opus.SetFEC(true));
56
57   // Note that the order of the following calls is critical.
58   opus.TestSetPacketLossRate(0, 0, 0);
59   opus.TestSetPacketLossRate(kPacketLossRate1,
60                              kPacketLossRate5 + kLossRate5Margin - 1,
61                              kPacketLossRate1);
62   opus.TestSetPacketLossRate(kPacketLossRate5 + kLossRate5Margin,
63                              kPacketLossRate10 + kLossRate10Margin - 1,
64                              kPacketLossRate5);
65   opus.TestSetPacketLossRate(kPacketLossRate10 + kLossRate10Margin,
66                              kPacketLossRate20 + kLossRate20Margin - 1,
67                              kPacketLossRate10);
68   opus.TestSetPacketLossRate(kPacketLossRate20 + kLossRate20Margin,
69                              100,
70                              kPacketLossRate20);
71   opus.TestSetPacketLossRate(kPacketLossRate20 + kLossRate20Margin,
72                              kPacketLossRate20 - kLossRate20Margin,
73                              kPacketLossRate20);
74   opus.TestSetPacketLossRate(kPacketLossRate20 - kLossRate20Margin - 1,
75                              kPacketLossRate10 - kLossRate10Margin,
76                              kPacketLossRate10);
77   opus.TestSetPacketLossRate(kPacketLossRate10 - kLossRate10Margin - 1,
78                              kPacketLossRate5 - kLossRate5Margin,
79                              kPacketLossRate5);
80   opus.TestSetPacketLossRate(kPacketLossRate5 - kLossRate5Margin - 1,
81                              kPacketLossRate1,
82                              kPacketLossRate1);
83   opus.TestSetPacketLossRate(0, 0, 0);
84 }
85 #else
86 void AcmOpusTest:TestSetPacketLossRate(int /* from */, int /* to */,
87                                        int /* expected_return */) {
88   return;
89 }
90 #endif  // WEBRTC_CODEC_OPUS
91
92 }  // namespace acm2
93
94 }  // namespace webrtc