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
16struct WebRtcISACStruct;
17
18namespace webrtc {
19
20// Number of samples in a 60 ms, sampled at 32 kHz.
21const int kIsacNumberOfSamples = 320 * 6;
22// Maximum number of bytes in output bitstream.
23const size_t kMaxBytes = 1000;
24
25class 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
38IsacTest::IsacTest()
39    : isac_codec_(NULL) {
40}
41
42void 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.
57TEST_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.
63TEST_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.
69TEST_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
74TEST_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  // Encode & decode.
83  int16_t encoded_bytes;
84  uint16_t* coded = reinterpret_cast<uint16_t*>(bitstream_);
85  uint16_t* coded_small = reinterpret_cast<uint16_t*>(bitstream_small_);
86
87  // Test with call with a small packet (sync packet).
88  EXPECT_EQ(-1, WebRtcIsac_UpdateBwEstimate(isac_codec_, coded_small, 7, 1,
89                                            12345, 56789));
90
91  // Encode 60 ms of data (needed to create a first packet).
92  encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
93  EXPECT_EQ(0, encoded_bytes);
94  encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
95  EXPECT_EQ(0, encoded_bytes);
96  encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
97  EXPECT_EQ(0, encoded_bytes);
98  encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
99  EXPECT_EQ(0, encoded_bytes);
100  encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
101  EXPECT_EQ(0, encoded_bytes);
102  encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
103
104  // Call to update bandwidth estimator with real data.
105  EXPECT_EQ(0, WebRtcIsac_UpdateBwEstimate(isac_codec_, coded, encoded_bytes, 1,
106                                           12345, 56789));
107
108  // Free memory.
109  EXPECT_EQ(0, WebRtcIsac_Free(isac_codec_));
110}
111
112}  // namespace webrtc
113