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 "modules/audio_coding/codecs/isac/main/include/isac.h"
11 
12 #include <string>
13 
14 #include "test/gtest.h"
15 #include "test/testsupport/file_utils.h"
16 
17 struct WebRtcISACStruct;
18 
19 namespace webrtc {
20 
21 // Number of samples in a 60 ms, sampled at 32 kHz.
22 const int kIsacNumberOfSamples = 320 * 6;
23 // Maximum number of bytes in output bitstream.
24 const size_t kMaxBytes = 1000;
25 
26 class IsacTest : public ::testing::Test {
27  protected:
28   IsacTest();
29   virtual void SetUp();
30 
31   WebRtcISACStruct* isac_codec_;
32 
33   int16_t speech_data_[kIsacNumberOfSamples];
34   int16_t output_data_[kIsacNumberOfSamples];
35   uint8_t bitstream_[kMaxBytes];
36   uint8_t bitstream_small_[7];  // Simulate sync packets.
37 };
38 
IsacTest()39 IsacTest::IsacTest() : isac_codec_(NULL) {}
40 
SetUp()41 void IsacTest::SetUp() {
42   // Read some samples from a speech file, to be used in the encode test.
43   FILE* input_file;
44   const std::string file_name =
45       webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
46   input_file = fopen(file_name.c_str(), "rb");
47   ASSERT_TRUE(input_file != NULL);
48   ASSERT_EQ(kIsacNumberOfSamples,
49             static_cast<int32_t>(fread(speech_data_, sizeof(int16_t),
50                                        kIsacNumberOfSamples, input_file)));
51   fclose(input_file);
52   input_file = NULL;
53 }
54 
55 // Test failing Create.
TEST_F(IsacTest,IsacCreateFail)56 TEST_F(IsacTest, IsacCreateFail) {
57   // Test to see that an invalid pointer is caught.
58   EXPECT_EQ(-1, WebRtcIsac_Create(NULL));
59 }
60 
61 // Test failing Free.
TEST_F(IsacTest,IsacFreeFail)62 TEST_F(IsacTest, IsacFreeFail) {
63   // Test to see that free function doesn't crash.
64   EXPECT_EQ(0, WebRtcIsac_Free(NULL));
65 }
66 
67 // Test normal Create and Free.
TEST_F(IsacTest,IsacCreateFree)68 TEST_F(IsacTest, IsacCreateFree) {
69   EXPECT_EQ(0, WebRtcIsac_Create(&isac_codec_));
70   EXPECT_TRUE(isac_codec_ != NULL);
71   EXPECT_EQ(0, WebRtcIsac_Free(isac_codec_));
72 }
73 
TEST_F(IsacTest,IsacUpdateBWE)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   int 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   EXPECT_GT(encoded_bytes, 0);
101 
102   // Call to update bandwidth estimator with real data.
103   EXPECT_EQ(0, WebRtcIsac_UpdateBwEstimate(isac_codec_, bitstream_,
104                                            static_cast<size_t>(encoded_bytes),
105                                            1, 12345, 56789));
106 
107   // Free memory.
108   EXPECT_EQ(0, WebRtcIsac_Free(isac_codec_));
109 }
110 
111 }  // namespace webrtc
112