1f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org/*
2f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org *
4f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org *  Use of this source code is governed by a BSD-style license
5f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org *  that can be found in the LICENSE file in the root of the source
6f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org *  tree. An additional intellectual property rights grant can be found
7f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org *  in the file PATENTS.  All contributing project authors may
8f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org */
10f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org#include <string>
11f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
12f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org#include "testing/gtest/include/gtest/gtest.h"
13f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org#include "webrtc/modules/audio_coding/codecs/isac/main/interface/isac.h"
14f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org#include "webrtc/test/testsupport/fileutils.h"
15f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
16f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.orgstruct WebRtcISACStruct;
17f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
18f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.orgnamespace webrtc {
19f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
20f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org// Number of samples in a 60 ms, sampled at 32 kHz.
21f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.orgconst int kIsacNumberOfSamples = 320 * 6;
22f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org// Maximum number of bytes in output bitstream.
23f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.orgconst size_t kMaxBytes = 1000;
24f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
25f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.orgclass IsacTest : public ::testing::Test {
26f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org protected:
27f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  IsacTest();
28f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  virtual void SetUp();
29f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
30f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  WebRtcISACStruct* isac_codec_;
31f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
32f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  int16_t speech_data_[kIsacNumberOfSamples];
33f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  int16_t output_data_[kIsacNumberOfSamples];
3483a60f3561a5a6d75f0044cf3161424d511f4066kwiberg@webrtc.org  uint8_t bitstream_[kMaxBytes];
35f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  uint8_t bitstream_small_[7];  // Simulate sync packets.
36f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org};
37f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
38f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.orgIsacTest::IsacTest()
39f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org    : isac_codec_(NULL) {
40f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org}
41f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
42f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.orgvoid IsacTest::SetUp() {
43f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  // Read some samples from a speech file, to be used in the encode test.
44f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  FILE* input_file;
45f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  const std::string file_name =
46f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org        webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
47f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  input_file = fopen(file_name.c_str(), "rb");
48f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  ASSERT_TRUE(input_file != NULL);
49f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  ASSERT_EQ(kIsacNumberOfSamples,
50f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org            static_cast<int32_t>(fread(speech_data_, sizeof(int16_t),
51f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org                                       kIsacNumberOfSamples, input_file)));
52f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  fclose(input_file);
53f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  input_file = NULL;
54f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org}
55f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
56f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org// Test failing Create.
57f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.orgTEST_F(IsacTest, IsacCreateFail) {
58f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  // Test to see that an invalid pointer is caught.
59f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  EXPECT_EQ(-1, WebRtcIsac_Create(NULL));
60f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org}
61f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
62f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org// Test failing Free.
63f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.orgTEST_F(IsacTest, IsacFreeFail) {
64f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  // Test to see that free function doesn't crash.
65f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  EXPECT_EQ(0, WebRtcIsac_Free(NULL));
66f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org}
67f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
68f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org// Test normal Create and Free.
69f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.orgTEST_F(IsacTest, IsacCreateFree) {
70f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  EXPECT_EQ(0, WebRtcIsac_Create(&isac_codec_));
71f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  EXPECT_TRUE(isac_codec_ != NULL);
72f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  EXPECT_EQ(0, WebRtcIsac_Free(isac_codec_));}
73f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
74f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.orgTEST_F(IsacTest, IsacUpdateBWE) {
75f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  // Create encoder memory.
76f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  EXPECT_EQ(0, WebRtcIsac_Create(&isac_codec_));
77f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
78f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  // Init encoder (adaptive mode) and decoder.
79f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  WebRtcIsac_EncoderInit(isac_codec_, 0);
80f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  WebRtcIsac_DecoderInit(isac_codec_);
81f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
82f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  // Encode & decode.
83f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  int16_t encoded_bytes;
84f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  uint16_t* coded = reinterpret_cast<uint16_t*>(bitstream_);
85f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  uint16_t* coded_small = reinterpret_cast<uint16_t*>(bitstream_small_);
86f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
87f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  // Test with call with a small packet (sync packet).
88f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  EXPECT_EQ(-1, WebRtcIsac_UpdateBwEstimate(isac_codec_, coded_small, 7, 1,
89f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org                                            12345, 56789));
90f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
91f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  // Encode 60 ms of data (needed to create a first packet).
92f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
93f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  EXPECT_EQ(0, encoded_bytes);
94f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
95f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  EXPECT_EQ(0, encoded_bytes);
96f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
97f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  EXPECT_EQ(0, encoded_bytes);
98f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
99f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  EXPECT_EQ(0, encoded_bytes);
100f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
101f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  EXPECT_EQ(0, encoded_bytes);
102f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  encoded_bytes =  WebRtcIsac_Encode(isac_codec_, speech_data_, bitstream_);
103f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
104f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  // Call to update bandwidth estimator with real data.
105f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  EXPECT_EQ(0, WebRtcIsac_UpdateBwEstimate(isac_codec_, coded, encoded_bytes, 1,
106f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org                                           12345, 56789));
107f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
108f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  // Free memory.
109f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org  EXPECT_EQ(0, WebRtcIsac_Free(isac_codec_));
110f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org}
111f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org
112f6e0404878530323f642d00b758b4d6105969c93turaj@webrtc.org}  // namespace webrtc
113