Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 #include <memory> | |
| 10 #include <string> | 11 #include <string> |
| 11 | 12 |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "webrtc/test/testsupport/fileutils.h" | 14 #include "webrtc/test/testsupport/fileutils.h" |
| 14 #include "webrtc_cng.h" | 15 #include "webrtc/modules/audio_coding/codecs/cng/webrtc_cng.h" |
| 15 | 16 |
| 16 namespace webrtc { | 17 namespace webrtc { |
| 17 | 18 |
| 18 enum { | 19 enum { |
| 19 kSidShortIntervalUpdate = 1, | 20 kSidShortIntervalUpdate = 1, |
| 20 kSidNormalIntervalUpdate = 100, | 21 kSidNormalIntervalUpdate = 100, |
| 21 kSidLongIntervalUpdate = 10000 | 22 kSidLongIntervalUpdate = 10000 |
| 22 }; | 23 }; |
| 23 | 24 |
| 24 enum { | 25 enum : size_t { |
| 25 kCNGNumParamsLow = 0, | 26 kCNGNumParamsLow = 0, |
| 26 kCNGNumParamsNormal = 8, | 27 kCNGNumParamsNormal = 8, |
| 27 kCNGNumParamsHigh = WEBRTC_CNG_MAX_LPC_ORDER, | 28 kCNGNumParamsHigh = WEBRTC_CNG_MAX_LPC_ORDER, |
| 28 kCNGNumParamsTooHigh = WEBRTC_CNG_MAX_LPC_ORDER + 1 | 29 kCNGNumParamsTooHigh = WEBRTC_CNG_MAX_LPC_ORDER + 1 |
| 29 }; | 30 }; |
| 30 | 31 |
| 31 enum { | 32 enum { |
| 32 kNoSid, | 33 kNoSid, |
| 33 kForceSid | 34 kForceSid |
| 34 }; | 35 }; |
| 35 | 36 |
| 36 class CngTest : public ::testing::Test { | 37 class CngTest : public ::testing::Test { |
| 37 protected: | 38 protected: |
| 38 CngTest(); | |
| 39 virtual void SetUp(); | 39 virtual void SetUp(); |
| 40 | 40 |
| 41 CNG_enc_inst* cng_enc_inst_; | 41 void TestCngEncode(int sample_rate_hz, int quality); |
| 42 CNG_dec_inst* cng_dec_inst_; | 42 |
| 43 int16_t speech_data_[640]; // Max size of CNG internal buffers. | 43 int16_t speech_data_[640]; // Max size of CNG internal buffers. |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 CngTest::CngTest() | |
| 47 : cng_enc_inst_(NULL), | |
| 48 cng_dec_inst_(NULL) { | |
| 49 } | |
| 50 | |
| 51 void CngTest::SetUp() { | 46 void CngTest::SetUp() { |
| 52 FILE* input_file; | 47 FILE* input_file; |
| 53 const std::string file_name = | 48 const std::string file_name = |
| 54 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"); | 49 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"); |
| 55 input_file = fopen(file_name.c_str(), "rb"); | 50 input_file = fopen(file_name.c_str(), "rb"); |
| 56 ASSERT_TRUE(input_file != NULL); | 51 ASSERT_TRUE(input_file != NULL); |
| 57 ASSERT_EQ(640, static_cast<int32_t>(fread(speech_data_, sizeof(int16_t), | 52 ASSERT_EQ(640, static_cast<int32_t>(fread(speech_data_, sizeof(int16_t), |
| 58 640, input_file))); | 53 640, input_file))); |
| 59 fclose(input_file); | 54 fclose(input_file); |
| 60 input_file = NULL; | 55 input_file = NULL; |
| 61 } | 56 } |
| 62 | 57 |
| 63 // Test failing Create. | 58 void CngTest::TestCngEncode(int sample_rate_hz, int quality) { |
| 64 TEST_F(CngTest, CngCreateFail) { | 59 const size_t num_samples_10ms = rtc::CheckedDivExact(sample_rate_hz, 100); |
| 65 // Test to see that an invalid pointer is caught. | 60 rtc::Buffer sid_data; |
| 66 EXPECT_EQ(-1, WebRtcCng_CreateEnc(NULL)); | 61 |
| 67 EXPECT_EQ(-1, WebRtcCng_CreateDec(NULL)); | 62 ComfortNoiseEncoder cng_encoder(sample_rate_hz, kSidNormalIntervalUpdate, |
| 63 quality); | |
| 64 EXPECT_EQ(0U, cng_encoder.Encode(rtc::ArrayView<const int16_t>( | |
| 65 speech_data_, num_samples_10ms), | |
| 66 kNoSid, &sid_data)); | |
| 67 EXPECT_EQ(static_cast<size_t>(quality + 1), | |
| 68 cng_encoder.Encode( | |
| 69 rtc::ArrayView<const int16_t>(speech_data_, num_samples_10ms), | |
| 70 kForceSid, &sid_data)); | |
| 68 } | 71 } |
| 69 | 72 |
| 70 // Test normal Create. | 73 #if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 71 TEST_F(CngTest, CngCreate) { | |
| 72 EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_)); | |
| 73 EXPECT_EQ(0, WebRtcCng_CreateDec(&cng_dec_inst_)); | |
| 74 EXPECT_TRUE(cng_enc_inst_ != NULL); | |
| 75 EXPECT_TRUE(cng_dec_inst_ != NULL); | |
| 76 // Free encoder and decoder memory. | |
| 77 EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_)); | |
| 78 EXPECT_EQ(0, WebRtcCng_FreeDec(cng_dec_inst_)); | |
| 79 } | |
| 80 | |
| 81 // Create CNG encoder, init with faulty values, free CNG encoder. | 74 // Create CNG encoder, init with faulty values, free CNG encoder. |
| 82 TEST_F(CngTest, CngInitFail) { | 75 TEST_F(CngTest, CngInitFail) { |
| 83 // Create encoder memory. | |
| 84 EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_)); | |
| 85 | |
| 86 // Call with too few parameters. | 76 // Call with too few parameters. |
| 87 EXPECT_EQ(-1, WebRtcCng_InitEnc(cng_enc_inst_, 8000, kSidNormalIntervalUpdate, | 77 EXPECT_DEATH({ ComfortNoiseEncoder(8000, kSidNormalIntervalUpdate, |
| 88 kCNGNumParamsLow)); | 78 kCNGNumParamsLow); }, ""); |
| 89 EXPECT_EQ(6130, WebRtcCng_GetErrorCodeEnc(cng_enc_inst_)); | |
| 90 | |
| 91 // Call with too many parameters. | 79 // Call with too many parameters. |
| 92 EXPECT_EQ(-1, WebRtcCng_InitEnc(cng_enc_inst_, 8000, kSidNormalIntervalUpdate, | 80 EXPECT_DEATH({ ComfortNoiseEncoder(8000, kSidNormalIntervalUpdate, |
| 93 kCNGNumParamsTooHigh)); | 81 kCNGNumParamsTooHigh); }, ""); |
| 94 EXPECT_EQ(6130, WebRtcCng_GetErrorCodeEnc(cng_enc_inst_)); | |
| 95 | |
| 96 // Free encoder memory. | |
| 97 EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_)); | |
| 98 } | |
| 99 | |
| 100 TEST_F(CngTest, CngEncode) { | |
| 101 uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1]; | |
| 102 size_t number_bytes; | |
| 103 | |
| 104 // Create encoder memory. | |
| 105 EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_)); | |
| 106 | |
| 107 // 8 kHz, Normal number of parameters | |
| 108 EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 8000, kSidNormalIntervalUpdate, | |
| 109 kCNGNumParamsNormal)); | |
| 110 EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 80, sid_data, | |
| 111 &number_bytes, kNoSid)); | |
| 112 EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode( | |
| 113 cng_enc_inst_, speech_data_, 80, sid_data, &number_bytes, kForceSid)); | |
| 114 | |
| 115 // 16 kHz, Normal number of parameters | |
| 116 EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 16000, kSidNormalIntervalUpdate, | |
| 117 kCNGNumParamsNormal)); | |
| 118 EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 160, sid_data, | |
| 119 &number_bytes, kNoSid)); | |
| 120 EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode( | |
| 121 cng_enc_inst_, speech_data_, 160, sid_data, &number_bytes, kForceSid)); | |
| 122 | |
| 123 // 32 kHz, Max number of parameters | |
| 124 EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 32000, kSidNormalIntervalUpdate, | |
| 125 kCNGNumParamsHigh)); | |
| 126 EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 320, sid_data, | |
| 127 &number_bytes, kNoSid)); | |
| 128 EXPECT_EQ(kCNGNumParamsHigh + 1, WebRtcCng_Encode( | |
| 129 cng_enc_inst_, speech_data_, 320, sid_data, &number_bytes, kForceSid)); | |
| 130 | |
| 131 // 48 kHz, Normal number of parameters | |
| 132 EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 48000, kSidNormalIntervalUpdate, | |
| 133 kCNGNumParamsNormal)); | |
| 134 EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 480, sid_data, | |
| 135 &number_bytes, kNoSid)); | |
| 136 EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode( | |
| 137 cng_enc_inst_, speech_data_, 480, sid_data, &number_bytes, kForceSid)); | |
| 138 | |
| 139 // 64 kHz, Normal number of parameters | |
| 140 EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 64000, kSidNormalIntervalUpdate, | |
| 141 kCNGNumParamsNormal)); | |
| 142 EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 640, sid_data, | |
| 143 &number_bytes, kNoSid)); | |
| 144 EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode( | |
| 145 cng_enc_inst_, speech_data_, 640, sid_data, &number_bytes, kForceSid)); | |
| 146 | |
| 147 // Free encoder memory. | |
| 148 EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_)); | |
| 149 } | 82 } |
| 150 | 83 |
| 151 // Encode Cng with too long input vector. | 84 // Encode Cng with too long input vector. |
| 152 TEST_F(CngTest, CngEncodeTooLong) { | 85 TEST_F(CngTest, CngEncodeTooLong) { |
| 153 uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1]; | 86 rtc::Buffer sid_data; |
| 154 size_t number_bytes; | |
| 155 | 87 |
| 156 // Create and init encoder memory. | 88 // Create encoder. |
| 157 EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_)); | 89 ComfortNoiseEncoder cng_encoder(8000, kSidNormalIntervalUpdate, |
| 158 EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 8000, kSidNormalIntervalUpdate, | 90 kCNGNumParamsNormal); |
| 159 kCNGNumParamsNormal)); | 91 // Run encoder with too much data. |
| 92 EXPECT_DEATH( | |
| 93 cng_encoder.Encode(rtc::ArrayView<const int16_t>(speech_data_, 641), | |
| 94 kNoSid, &sid_data), | |
| 95 ""); | |
| 96 } | |
| 97 #endif // GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) | |
| 160 | 98 |
| 161 // Run encoder with too much data. | 99 TEST_F(CngTest, CngEncode8000) { |
| 162 EXPECT_EQ(-1, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 641, sid_data, | 100 TestCngEncode(8000, kCNGNumParamsNormal); |
| 163 &number_bytes, kNoSid)); | |
| 164 EXPECT_EQ(6140, WebRtcCng_GetErrorCodeEnc(cng_enc_inst_)); | |
| 165 | |
| 166 // Free encoder memory. | |
| 167 EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_)); | |
| 168 } | 101 } |
| 169 | 102 |
| 170 // Call encode without calling init. | 103 TEST_F(CngTest, CngEncode16000) { |
| 171 TEST_F(CngTest, CngEncodeNoInit) { | 104 TestCngEncode(16000, kCNGNumParamsNormal); |
| 172 uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1]; | 105 } |
| 173 size_t number_bytes; | |
| 174 | 106 |
| 175 // Create encoder memory. | 107 TEST_F(CngTest, CngEncode32000) { |
| 176 EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_)); | 108 TestCngEncode(32000, kCNGNumParamsHigh); |
| 109 } | |
| 177 | 110 |
| 178 // Run encoder without calling init. | 111 TEST_F(CngTest, CngEncode48000) { |
| 179 EXPECT_EQ(-1, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 640, sid_data, | 112 TestCngEncode(48000, kCNGNumParamsNormal); |
| 180 &number_bytes, kNoSid)); | 113 } |
| 181 EXPECT_EQ(6120, WebRtcCng_GetErrorCodeEnc(cng_enc_inst_)); | |
| 182 | 114 |
| 183 // Free encoder memory. | 115 TEST_F(CngTest, CngEncode64000) { |
| 184 EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_)); | 116 TestCngEncode(64000, kCNGNumParamsNormal); |
| 185 } | 117 } |
| 186 | 118 |
| 187 // Update SID parameters, for both 9 and 16 parameters. | 119 // Update SID parameters, for both 9 and 16 parameters. |
| 188 TEST_F(CngTest, CngUpdateSid) { | 120 TEST_F(CngTest, CngUpdateSid) { |
| 189 uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1]; | 121 rtc::Buffer sid_data; |
| 190 size_t number_bytes; | |
| 191 | 122 |
| 192 // Create and initialize encoder and decoder memory. | 123 // Create and initialize encoder and decoder. |
| 193 EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_)); | 124 ComfortNoiseEncoder cng_encoder(16000, kSidNormalIntervalUpdate, |
| 194 EXPECT_EQ(0, WebRtcCng_CreateDec(&cng_dec_inst_)); | 125 kCNGNumParamsNormal); |
| 195 EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 16000, kSidNormalIntervalUpdate, | 126 ComfortNoiseDecoder cng_decoder; |
| 196 kCNGNumParamsNormal)); | |
| 197 WebRtcCng_InitDec(cng_dec_inst_); | |
| 198 | 127 |
| 199 // Run normal Encode and UpdateSid. | 128 // Run normal Encode and UpdateSid. |
| 200 EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode( | 129 EXPECT_EQ(kCNGNumParamsNormal + 1, |
| 201 cng_enc_inst_, speech_data_, 160, sid_data, &number_bytes, kForceSid)); | 130 cng_encoder.Encode(rtc::ArrayView<const int16_t>(speech_data_, 160), |
| 202 EXPECT_EQ(0, WebRtcCng_UpdateSid(cng_dec_inst_, sid_data, | 131 kForceSid, &sid_data)); |
| 203 kCNGNumParamsNormal + 1)); | 132 cng_decoder.UpdateSid(sid_data); |
| 204 | 133 |
| 205 // Reinit with new length. | 134 // Reinit with new length. |
| 206 EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 16000, kSidNormalIntervalUpdate, | 135 cng_encoder.Reset(16000, kSidNormalIntervalUpdate, kCNGNumParamsHigh); |
| 207 kCNGNumParamsHigh)); | 136 cng_decoder.Reset(); |
| 208 WebRtcCng_InitDec(cng_dec_inst_); | |
| 209 | 137 |
| 210 // Expect 0 because of unstable parameters after switching length. | 138 // Expect 0 because of unstable parameters after switching length. |
| 211 EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 160, sid_data, | 139 EXPECT_EQ(0U, |
| 212 &number_bytes, kForceSid)); | 140 cng_encoder.Encode(rtc::ArrayView<const int16_t>(speech_data_, 160), |
| 213 EXPECT_EQ(kCNGNumParamsHigh + 1, WebRtcCng_Encode( | 141 kForceSid, &sid_data)); |
| 214 cng_enc_inst_, speech_data_ + 160, 160, sid_data, &number_bytes, | 142 EXPECT_EQ( |
| 215 kForceSid)); | 143 kCNGNumParamsHigh + 1, |
| 216 EXPECT_EQ(0, WebRtcCng_UpdateSid(cng_dec_inst_, sid_data, | 144 cng_encoder.Encode(rtc::ArrayView<const int16_t>(speech_data_ + 160, 160), |
| 217 kCNGNumParamsNormal + 1)); | 145 kForceSid, &sid_data)); |
| 218 | 146 cng_decoder.UpdateSid( |
| 219 // Free encoder and decoder memory. | 147 rtc::ArrayView<const uint8_t>(sid_data.data(), kCNGNumParamsNormal + 1)); |
| 220 EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_)); | |
| 221 EXPECT_EQ(0, WebRtcCng_FreeDec(cng_dec_inst_)); | |
| 222 } | 148 } |
| 223 | 149 |
| 224 // Update SID parameters, with wrong parameters or without calling decode. | 150 // Update SID parameters, with wrong parameters or without calling decode. |
| 225 TEST_F(CngTest, CngUpdateSidErroneous) { | 151 TEST_F(CngTest, CngUpdateSidErroneous) { |
| 226 uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1]; | 152 rtc::Buffer sid_data; |
| 227 size_t number_bytes; | |
| 228 | |
| 229 // Create encoder and decoder memory. | |
| 230 EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_)); | |
| 231 EXPECT_EQ(0, WebRtcCng_CreateDec(&cng_dec_inst_)); | |
| 232 | 153 |
| 233 // Encode. | 154 // Encode. |
| 234 EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 16000, kSidNormalIntervalUpdate, | 155 ComfortNoiseEncoder cng_encoder(16000, kSidNormalIntervalUpdate, |
| 235 kCNGNumParamsNormal)); | 156 kCNGNumParamsNormal); |
| 236 EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode( | 157 ComfortNoiseDecoder cng_decoder; |
| 237 cng_enc_inst_, speech_data_, 160, sid_data, &number_bytes, kForceSid)); | 158 EXPECT_EQ(kCNGNumParamsNormal + 1, |
| 238 | 159 cng_encoder.Encode(rtc::ArrayView<const int16_t>(speech_data_, 160), |
| 239 // Update Sid before initializing decoder. | 160 kForceSid, &sid_data)); |
| 240 EXPECT_EQ(-1, WebRtcCng_UpdateSid(cng_dec_inst_, sid_data, | |
| 241 kCNGNumParamsNormal + 1)); | |
| 242 EXPECT_EQ(6220, WebRtcCng_GetErrorCodeDec(cng_dec_inst_)); | |
| 243 | |
| 244 // Initialize decoder. | |
| 245 WebRtcCng_InitDec(cng_dec_inst_); | |
| 246 | 161 |
| 247 // First run with valid parameters, then with too many CNG parameters. | 162 // First run with valid parameters, then with too many CNG parameters. |
| 248 // The function will operate correctly by only reading the maximum number of | 163 // The function will operate correctly by only reading the maximum number of |
| 249 // parameters, skipping the extra. | 164 // parameters, skipping the extra. |
| 250 EXPECT_EQ(0, WebRtcCng_UpdateSid(cng_dec_inst_, sid_data, | 165 cng_decoder.UpdateSid( |
| 251 kCNGNumParamsNormal + 1)); | 166 rtc::ArrayView<const uint8_t>(sid_data.data(), kCNGNumParamsNormal + 1)); |
| 252 EXPECT_EQ(0, WebRtcCng_UpdateSid(cng_dec_inst_, sid_data, | |
| 253 kCNGNumParamsTooHigh + 1)); | |
| 254 | 167 |
| 255 // Free encoder and decoder memory. | 168 // Make sure the input buffer is large enough. Since Encode() appends data, we |
| 256 EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_)); | 169 // need to set the size manually only afterwards, or the buffer will be bigger |
| 257 EXPECT_EQ(0, WebRtcCng_FreeDec(cng_dec_inst_)); | 170 // than anticipated. |
| 171 sid_data.SetSize(kCNGNumParamsTooHigh + 1); | |
| 172 cng_decoder.UpdateSid( | |
| 173 rtc::ArrayView<const uint8_t>(sid_data.data(), kCNGNumParamsTooHigh + 1)); | |
|
kwiberg-webrtc
2016/04/25 11:35:12
Now that we pass an ArrayView to UpdateSid, it's v
| |
| 258 } | 174 } |
| 259 | 175 |
| 260 // Test to generate cng data, by forcing SID. Both normal and faulty condition. | 176 // Test to generate cng data, by forcing SID. Both normal and faulty condition. |
| 261 TEST_F(CngTest, CngGenerate) { | 177 TEST_F(CngTest, CngGenerate) { |
| 262 uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1]; | 178 rtc::Buffer sid_data; |
| 263 int16_t out_data[640]; | 179 int16_t out_data[640]; |
| 264 size_t number_bytes; | |
| 265 | 180 |
| 266 // Create and initialize encoder and decoder memory. | 181 // Create and initialize encoder and decoder. |
| 267 EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_)); | 182 ComfortNoiseEncoder cng_encoder(16000, kSidNormalIntervalUpdate, |
| 268 EXPECT_EQ(0, WebRtcCng_CreateDec(&cng_dec_inst_)); | 183 kCNGNumParamsNormal); |
| 269 EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 16000, kSidNormalIntervalUpdate, | 184 ComfortNoiseDecoder cng_decoder; |
| 270 kCNGNumParamsNormal)); | |
| 271 WebRtcCng_InitDec(cng_dec_inst_); | |
| 272 | 185 |
| 273 // Normal Encode. | 186 // Normal Encode. |
| 274 EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode( | 187 EXPECT_EQ(kCNGNumParamsNormal + 1, |
| 275 cng_enc_inst_, speech_data_, 160, sid_data, &number_bytes, kForceSid)); | 188 cng_encoder.Encode(rtc::ArrayView<const int16_t>(speech_data_, 160), |
| 189 kForceSid, &sid_data)); | |
| 276 | 190 |
| 277 // Normal UpdateSid. | 191 // Normal UpdateSid. |
| 278 EXPECT_EQ(0, WebRtcCng_UpdateSid(cng_dec_inst_, sid_data, | 192 cng_decoder.UpdateSid(sid_data); |
| 279 kCNGNumParamsNormal + 1)); | |
| 280 | 193 |
| 281 // Two normal Generate, one with new_period. | 194 // Two normal Generate, one with new_period. |
| 282 EXPECT_EQ(0, WebRtcCng_Generate(cng_dec_inst_, out_data, 640, 1)); | 195 EXPECT_TRUE(cng_decoder.Generate(rtc::ArrayView<int16_t>(out_data, 640), 1)); |
| 283 EXPECT_EQ(0, WebRtcCng_Generate(cng_dec_inst_, out_data, 640, 0)); | 196 EXPECT_TRUE(cng_decoder.Generate(rtc::ArrayView<int16_t>(out_data, 640), 0)); |
| 284 | 197 |
| 285 // Call Genereate with too much data. | 198 // Call Genereate with too much data. |
| 286 EXPECT_EQ(-1, WebRtcCng_Generate(cng_dec_inst_, out_data, 641, 0)); | 199 EXPECT_FALSE(cng_decoder.Generate(rtc::ArrayView<int16_t>(out_data, 641), 0)); |
| 287 EXPECT_EQ(6140, WebRtcCng_GetErrorCodeDec(cng_dec_inst_)); | |
| 288 | |
| 289 // Free encoder and decoder memory. | |
| 290 EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_)); | |
| 291 EXPECT_EQ(0, WebRtcCng_FreeDec(cng_dec_inst_)); | |
| 292 } | 200 } |
| 293 | 201 |
| 294 // Test automatic SID. | 202 // Test automatic SID. |
| 295 TEST_F(CngTest, CngAutoSid) { | 203 TEST_F(CngTest, CngAutoSid) { |
| 296 uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1]; | 204 rtc::Buffer sid_data; |
| 297 size_t number_bytes; | |
| 298 | 205 |
| 299 // Create and initialize encoder and decoder memory. | 206 // Create and initialize encoder and decoder. |
| 300 EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_)); | 207 ComfortNoiseEncoder cng_encoder(16000, kSidNormalIntervalUpdate, |
| 301 EXPECT_EQ(0, WebRtcCng_CreateDec(&cng_dec_inst_)); | 208 kCNGNumParamsNormal); |
| 302 EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 16000, kSidNormalIntervalUpdate, | 209 ComfortNoiseDecoder cng_decoder; |
| 303 kCNGNumParamsNormal)); | |
| 304 WebRtcCng_InitDec(cng_dec_inst_); | |
| 305 | 210 |
| 306 // Normal Encode, 100 msec, where no SID data should be generated. | 211 // Normal Encode, 100 msec, where no SID data should be generated. |
| 307 for (int i = 0; i < 10; i++) { | 212 for (int i = 0; i < 10; i++) { |
| 308 EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 160, sid_data, | 213 EXPECT_EQ(0U, cng_encoder.Encode( |
| 309 &number_bytes, kNoSid)); | 214 rtc::ArrayView<const int16_t>(speech_data_, 160), kNoSid, &sid_data)); |
| 310 } | 215 } |
| 311 | 216 |
| 312 // We have reached 100 msec, and SID data should be generated. | 217 // We have reached 100 msec, and SID data should be generated. |
| 313 EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode( | 218 EXPECT_EQ(kCNGNumParamsNormal + 1, cng_encoder.Encode( |
| 314 cng_enc_inst_, speech_data_, 160, sid_data, &number_bytes, kNoSid)); | 219 rtc::ArrayView<const int16_t>(speech_data_, 160), kNoSid, &sid_data)); |
| 315 | |
| 316 // Free encoder and decoder memory. | |
| 317 EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_)); | |
| 318 EXPECT_EQ(0, WebRtcCng_FreeDec(cng_dec_inst_)); | |
| 319 } | 220 } |
| 320 | 221 |
| 321 // Test automatic SID, with very short interval. | 222 // Test automatic SID, with very short interval. |
| 322 TEST_F(CngTest, CngAutoSidShort) { | 223 TEST_F(CngTest, CngAutoSidShort) { |
| 323 uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1]; | 224 rtc::Buffer sid_data; |
| 324 size_t number_bytes; | |
| 325 | 225 |
| 326 // Create and initialize encoder and decoder memory. | 226 // Create and initialize encoder and decoder. |
| 327 EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_)); | 227 ComfortNoiseEncoder cng_encoder(16000, kSidShortIntervalUpdate, |
| 328 EXPECT_EQ(0, WebRtcCng_CreateDec(&cng_dec_inst_)); | 228 kCNGNumParamsNormal); |
| 329 EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 16000, kSidShortIntervalUpdate, | 229 ComfortNoiseDecoder cng_decoder; |
| 330 kCNGNumParamsNormal)); | |
| 331 WebRtcCng_InitDec(cng_dec_inst_); | |
| 332 | 230 |
| 333 // First call will never generate SID, unless forced to. | 231 // First call will never generate SID, unless forced to. |
| 334 EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 160, sid_data, | 232 EXPECT_EQ(0U, cng_encoder.Encode( |
| 335 &number_bytes, kNoSid)); | 233 rtc::ArrayView<const int16_t>(speech_data_, 160), kNoSid, &sid_data)); |
| 336 | 234 |
| 337 // Normal Encode, 100 msec, SID data should be generated all the time. | 235 // Normal Encode, 100 msec, SID data should be generated all the time. |
| 338 for (int i = 0; i < 10; i++) { | 236 for (int i = 0; i < 10; i++) { |
| 339 EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode( | 237 EXPECT_EQ(kCNGNumParamsNormal + 1, cng_encoder.Encode( |
| 340 cng_enc_inst_, speech_data_, 160, sid_data, &number_bytes, kNoSid)); | 238 rtc::ArrayView<const int16_t>(speech_data_, 160), kNoSid, &sid_data)); |
| 341 } | 239 } |
| 342 | |
| 343 // Free encoder and decoder memory. | |
| 344 EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_)); | |
| 345 EXPECT_EQ(0, WebRtcCng_FreeDec(cng_dec_inst_)); | |
| 346 } | 240 } |
| 347 | 241 |
| 348 } // namespace webrtc | 242 } // namespace webrtc |
| OLD | NEW |