Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / codecs / isac / main / source / isac_unittest.cc
1 /*
2  *  Copyright (c) 2013 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 #include <string>
11
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "webrtc/modules/audio_coding/codecs/isac/main/interface/isac.h"
14 #include "webrtc/test/testsupport/fileutils.h"
15
16 struct WebRtcISACStruct;
17
18 namespace webrtc {
19
20 // Number of samples in a 60 ms, sampled at 32 kHz.
21 const int kIsacNumberOfSamples = 320 * 6;
22 // Maximum number of bytes in output bitstream.
23 const size_t kMaxBytes = 1000;
24
25 class IsacTest : public ::testing::Test {
26  protected:
27   IsacTest();
28   virtual void SetUp();
29
30   WebRtcISACStruct* isac_codec_;
31
32   int16_t speech_data_[kIsacNumberOfSamples];
33   int16_t output_data_[kIsacNumberOfSamples];
34   uint8_t bitstream_[kMaxBytes];
35   uint8_t bitstream_small_[7];  // Simulate sync packets.
36 };
37
38 IsacTest::IsacTest()
39     : isac_codec_(NULL) {
40 }
41
42 void IsacTest::SetUp() {
43   // Read some samples from a speech file, to be used in the encode test.
44   FILE* input_file;
45   const std::string file_name =
46         webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
47   input_file = fopen(file_name.c_str(), "rb");
48   ASSERT_TRUE(input_file != NULL);
49   ASSERT_EQ(kIsacNumberOfSamples,
50             static_cast<int32_t>(fread(speech_data_, sizeof(int16_t),
51                                        kIsacNumberOfSamples, input_file)));
52   fclose(input_file);
53   input_file = NULL;
54 }
55
56 // Test failing Create.
57 TEST_F(IsacTest, IsacCreateFail) {
58   // Test to see that an invalid pointer is caught.
59   EXPECT_EQ(-1, WebRtcIsac_Create(NULL));
60 }
61
62 // Test failing Free.
63 TEST_F(IsacTest, IsacFreeFail) {
64   // Test to see that free function doesn't crash.
65   EXPECT_EQ(0, WebRtcIsac_Free(NULL));
66 }
67
68 // Test normal Create and Free.
69 TEST_F(IsacTest, IsacCreateFree) {
70   EXPECT_EQ(0, WebRtcIsac_Create(&isac_codec_));
71   EXPECT_TRUE(isac_codec_ != NULL);
72   EXPECT_EQ(0, WebRtcIsac_Free(isac_codec_));}
73
74 TEST_F(IsacTest, IsacUpdateBWE) {
75   // Create encoder memory.
76   EXPECT_EQ(0, WebRtcIsac_Create(&isac_codec_));
77
78   // Init encoder (adaptive mode) and decoder.
79   WebRtcIsac_EncoderInit(isac_codec_, 0);
80   WebRtcIsac_DecoderInit(isac_codec_);
81
82   int16_t encoded_bytes;
83
84   // Test with call with a small packet (sync packet).
85   EXPECT_EQ(-1, WebRtcIsac_UpdateBwEstimate(isac_codec_, bitstream_small_, 7, 1,
86                                             12345, 56789));
87
88   // Encode 60 ms of data (needed to create a first packet).
89   encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
90   EXPECT_EQ(0, encoded_bytes);
91   encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
92   EXPECT_EQ(0, encoded_bytes);
93   encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
94   EXPECT_EQ(0, encoded_bytes);
95   encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
96   EXPECT_EQ(0, encoded_bytes);
97   encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
98   EXPECT_EQ(0, encoded_bytes);
99   encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
100
101   // Call to update bandwidth estimator with real data.
102   EXPECT_EQ(0, WebRtcIsac_UpdateBwEstimate(isac_codec_, bitstream_,
103                                            encoded_bytes, 1, 12345, 56789));
104
105   // Free memory.
106   EXPECT_EQ(0, WebRtcIsac_Free(isac_codec_));
107 }
108
109 }  // namespace webrtc