1 /*
2 * Copyright (c) 2014 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
11 #include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
12
13 #include <memory>
14 #include <vector>
15
16 #include "common_audio/vad/mock/mock_vad.h"
17 #include "rtc_base/constructor_magic.h"
18 #include "rtc_base/numerics/safe_conversions.h"
19 #include "test/gtest.h"
20 #include "test/mock_audio_encoder.h"
21 #include "test/testsupport/rtc_expect_death.h"
22
23 using ::testing::_;
24 using ::testing::Eq;
25 using ::testing::InSequence;
26 using ::testing::Invoke;
27 using ::testing::Not;
28 using ::testing::Optional;
29 using ::testing::Return;
30 using ::testing::SetArgPointee;
31
32 namespace webrtc {
33
34 namespace {
35 static const size_t kMaxNumSamples = 48 * 10 * 2; // 10 ms @ 48 kHz stereo.
36 static const size_t kMockReturnEncodedBytes = 17;
37 static const int kCngPayloadType = 18;
38 } // namespace
39
40 class AudioEncoderCngTest : public ::testing::Test {
41 protected:
AudioEncoderCngTest()42 AudioEncoderCngTest()
43 : mock_encoder_owner_(new MockAudioEncoder),
44 mock_encoder_(mock_encoder_owner_.get()),
45 mock_vad_(new MockVad),
46 timestamp_(4711),
47 num_audio_samples_10ms_(0),
48 sample_rate_hz_(8000) {
49 memset(audio_, 0, kMaxNumSamples * 2);
50 EXPECT_CALL(*mock_encoder_, NumChannels()).WillRepeatedly(Return(1));
51 }
52
TearDown()53 void TearDown() override {
54 EXPECT_CALL(*mock_vad_, Die()).Times(1);
55 cng_.reset();
56 }
57
MakeCngConfig()58 AudioEncoderCngConfig MakeCngConfig() {
59 AudioEncoderCngConfig config;
60 config.speech_encoder = std::move(mock_encoder_owner_);
61 EXPECT_TRUE(config.speech_encoder);
62
63 // Let the AudioEncoderCng object use a MockVad instead of its internally
64 // created Vad object.
65 config.vad = mock_vad_;
66 config.payload_type = kCngPayloadType;
67
68 return config;
69 }
70
CreateCng(AudioEncoderCngConfig && config)71 void CreateCng(AudioEncoderCngConfig&& config) {
72 num_audio_samples_10ms_ = static_cast<size_t>(10 * sample_rate_hz_ / 1000);
73 ASSERT_LE(num_audio_samples_10ms_, kMaxNumSamples);
74 if (config.speech_encoder) {
75 EXPECT_CALL(*mock_encoder_, SampleRateHz())
76 .WillRepeatedly(Return(sample_rate_hz_));
77 // Max10MsFramesInAPacket() is just used to verify that the SID frame
78 // period is not too small. The return value does not matter that much,
79 // as long as it is smaller than 10.
80 EXPECT_CALL(*mock_encoder_, Max10MsFramesInAPacket())
81 .WillOnce(Return(1u));
82 }
83 cng_ = CreateComfortNoiseEncoder(std::move(config));
84 }
85
Encode()86 void Encode() {
87 ASSERT_TRUE(cng_) << "Must call CreateCng() first.";
88 encoded_info_ = cng_->Encode(
89 timestamp_,
90 rtc::ArrayView<const int16_t>(audio_, num_audio_samples_10ms_),
91 &encoded_);
92 timestamp_ += static_cast<uint32_t>(num_audio_samples_10ms_);
93 }
94
95 // Expect |num_calls| calls to the encoder, all successful. The last call
96 // claims to have encoded |kMockReturnEncodedBytes| bytes, and all the
97 // preceding ones 0 bytes.
ExpectEncodeCalls(size_t num_calls)98 void ExpectEncodeCalls(size_t num_calls) {
99 InSequence s;
100 AudioEncoder::EncodedInfo info;
101 for (size_t j = 0; j < num_calls - 1; ++j) {
102 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _)).WillOnce(Return(info));
103 }
104 info.encoded_bytes = kMockReturnEncodedBytes;
105 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
106 .WillOnce(
107 Invoke(MockAudioEncoder::FakeEncoding(kMockReturnEncodedBytes)));
108 }
109
110 // Verifies that the cng_ object waits until it has collected
111 // |blocks_per_frame| blocks of audio, and then dispatches all of them to
112 // the underlying codec (speech or cng).
CheckBlockGrouping(size_t blocks_per_frame,bool active_speech)113 void CheckBlockGrouping(size_t blocks_per_frame, bool active_speech) {
114 EXPECT_CALL(*mock_encoder_, Num10MsFramesInNextPacket())
115 .WillRepeatedly(Return(blocks_per_frame));
116 auto config = MakeCngConfig();
117 const int num_cng_coefficients = config.num_cng_coefficients;
118 CreateCng(std::move(config));
119 EXPECT_CALL(*mock_vad_, VoiceActivity(_, _, _))
120 .WillRepeatedly(Return(active_speech ? Vad::kActive : Vad::kPassive));
121
122 // Don't expect any calls to the encoder yet.
123 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _)).Times(0);
124 for (size_t i = 0; i < blocks_per_frame - 1; ++i) {
125 Encode();
126 EXPECT_EQ(0u, encoded_info_.encoded_bytes);
127 }
128 if (active_speech)
129 ExpectEncodeCalls(blocks_per_frame);
130 Encode();
131 if (active_speech) {
132 EXPECT_EQ(kMockReturnEncodedBytes, encoded_info_.encoded_bytes);
133 } else {
134 EXPECT_EQ(static_cast<size_t>(num_cng_coefficients + 1),
135 encoded_info_.encoded_bytes);
136 }
137 }
138
139 // Verifies that the audio is partitioned into larger blocks before calling
140 // the VAD.
CheckVadInputSize(int input_frame_size_ms,int expected_first_block_size_ms,int expected_second_block_size_ms)141 void CheckVadInputSize(int input_frame_size_ms,
142 int expected_first_block_size_ms,
143 int expected_second_block_size_ms) {
144 const size_t blocks_per_frame =
145 static_cast<size_t>(input_frame_size_ms / 10);
146
147 EXPECT_CALL(*mock_encoder_, Num10MsFramesInNextPacket())
148 .WillRepeatedly(Return(blocks_per_frame));
149
150 // Expect nothing to happen before the last block is sent to cng_.
151 EXPECT_CALL(*mock_vad_, VoiceActivity(_, _, _)).Times(0);
152 for (size_t i = 0; i < blocks_per_frame - 1; ++i) {
153 Encode();
154 }
155
156 // Let the VAD decision be passive, since an active decision may lead to
157 // early termination of the decision loop.
158 InSequence s;
159 EXPECT_CALL(
160 *mock_vad_,
161 VoiceActivity(_, expected_first_block_size_ms * sample_rate_hz_ / 1000,
162 sample_rate_hz_))
163 .WillOnce(Return(Vad::kPassive));
164 if (expected_second_block_size_ms > 0) {
165 EXPECT_CALL(*mock_vad_,
166 VoiceActivity(
167 _, expected_second_block_size_ms * sample_rate_hz_ / 1000,
168 sample_rate_hz_))
169 .WillOnce(Return(Vad::kPassive));
170 }
171
172 // With this call to Encode(), |mock_vad_| should be called according to the
173 // above expectations.
174 Encode();
175 }
176
177 // Tests a frame with both active and passive speech. Returns true if the
178 // decision was active speech, false if it was passive.
CheckMixedActivePassive(Vad::Activity first_type,Vad::Activity second_type)179 bool CheckMixedActivePassive(Vad::Activity first_type,
180 Vad::Activity second_type) {
181 // Set the speech encoder frame size to 60 ms, to ensure that the VAD will
182 // be called twice.
183 const size_t blocks_per_frame = 6;
184 EXPECT_CALL(*mock_encoder_, Num10MsFramesInNextPacket())
185 .WillRepeatedly(Return(blocks_per_frame));
186 InSequence s;
187 EXPECT_CALL(*mock_vad_, VoiceActivity(_, _, _))
188 .WillOnce(Return(first_type));
189 if (first_type == Vad::kPassive) {
190 // Expect a second call to the VAD only if the first frame was passive.
191 EXPECT_CALL(*mock_vad_, VoiceActivity(_, _, _))
192 .WillOnce(Return(second_type));
193 }
194 encoded_info_.payload_type = 0;
195 for (size_t i = 0; i < blocks_per_frame; ++i) {
196 Encode();
197 }
198 return encoded_info_.payload_type != kCngPayloadType;
199 }
200
201 std::unique_ptr<AudioEncoder> cng_;
202 std::unique_ptr<MockAudioEncoder> mock_encoder_owner_;
203 MockAudioEncoder* mock_encoder_;
204 MockVad* mock_vad_; // Ownership is transferred to |cng_|.
205 uint32_t timestamp_;
206 int16_t audio_[kMaxNumSamples];
207 size_t num_audio_samples_10ms_;
208 rtc::Buffer encoded_;
209 AudioEncoder::EncodedInfo encoded_info_;
210 int sample_rate_hz_;
211
212 RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderCngTest);
213 };
214
TEST_F(AudioEncoderCngTest,CreateAndDestroy)215 TEST_F(AudioEncoderCngTest, CreateAndDestroy) {
216 CreateCng(MakeCngConfig());
217 }
218
TEST_F(AudioEncoderCngTest,CheckFrameSizePropagation)219 TEST_F(AudioEncoderCngTest, CheckFrameSizePropagation) {
220 CreateCng(MakeCngConfig());
221 EXPECT_CALL(*mock_encoder_, Num10MsFramesInNextPacket())
222 .WillOnce(Return(17U));
223 EXPECT_EQ(17U, cng_->Num10MsFramesInNextPacket());
224 }
225
TEST_F(AudioEncoderCngTest,CheckTargetAudioBitratePropagation)226 TEST_F(AudioEncoderCngTest, CheckTargetAudioBitratePropagation) {
227 CreateCng(MakeCngConfig());
228 EXPECT_CALL(*mock_encoder_,
229 OnReceivedUplinkBandwidth(4711, absl::optional<int64_t>()));
230 cng_->OnReceivedUplinkBandwidth(4711, absl::nullopt);
231 }
232
TEST_F(AudioEncoderCngTest,CheckPacketLossFractionPropagation)233 TEST_F(AudioEncoderCngTest, CheckPacketLossFractionPropagation) {
234 CreateCng(MakeCngConfig());
235 EXPECT_CALL(*mock_encoder_, OnReceivedUplinkPacketLossFraction(0.5));
236 cng_->OnReceivedUplinkPacketLossFraction(0.5);
237 }
238
TEST_F(AudioEncoderCngTest,CheckGetFrameLengthRangePropagation)239 TEST_F(AudioEncoderCngTest, CheckGetFrameLengthRangePropagation) {
240 CreateCng(MakeCngConfig());
241 auto expected_range =
242 std::make_pair(TimeDelta::Millis(20), TimeDelta::Millis(20));
243 EXPECT_CALL(*mock_encoder_, GetFrameLengthRange())
244 .WillRepeatedly(Return(absl::make_optional(expected_range)));
245 EXPECT_THAT(cng_->GetFrameLengthRange(), Optional(Eq(expected_range)));
246 }
247
TEST_F(AudioEncoderCngTest,EncodeCallsVad)248 TEST_F(AudioEncoderCngTest, EncodeCallsVad) {
249 EXPECT_CALL(*mock_encoder_, Num10MsFramesInNextPacket())
250 .WillRepeatedly(Return(1U));
251 CreateCng(MakeCngConfig());
252 EXPECT_CALL(*mock_vad_, VoiceActivity(_, _, _))
253 .WillOnce(Return(Vad::kPassive));
254 Encode();
255 }
256
TEST_F(AudioEncoderCngTest,EncodeCollects1BlockPassiveSpeech)257 TEST_F(AudioEncoderCngTest, EncodeCollects1BlockPassiveSpeech) {
258 CheckBlockGrouping(1, false);
259 }
260
TEST_F(AudioEncoderCngTest,EncodeCollects2BlocksPassiveSpeech)261 TEST_F(AudioEncoderCngTest, EncodeCollects2BlocksPassiveSpeech) {
262 CheckBlockGrouping(2, false);
263 }
264
TEST_F(AudioEncoderCngTest,EncodeCollects3BlocksPassiveSpeech)265 TEST_F(AudioEncoderCngTest, EncodeCollects3BlocksPassiveSpeech) {
266 CheckBlockGrouping(3, false);
267 }
268
TEST_F(AudioEncoderCngTest,EncodeCollects1BlockActiveSpeech)269 TEST_F(AudioEncoderCngTest, EncodeCollects1BlockActiveSpeech) {
270 CheckBlockGrouping(1, true);
271 }
272
TEST_F(AudioEncoderCngTest,EncodeCollects2BlocksActiveSpeech)273 TEST_F(AudioEncoderCngTest, EncodeCollects2BlocksActiveSpeech) {
274 CheckBlockGrouping(2, true);
275 }
276
TEST_F(AudioEncoderCngTest,EncodeCollects3BlocksActiveSpeech)277 TEST_F(AudioEncoderCngTest, EncodeCollects3BlocksActiveSpeech) {
278 CheckBlockGrouping(3, true);
279 }
280
TEST_F(AudioEncoderCngTest,EncodePassive)281 TEST_F(AudioEncoderCngTest, EncodePassive) {
282 const size_t kBlocksPerFrame = 3;
283 EXPECT_CALL(*mock_encoder_, Num10MsFramesInNextPacket())
284 .WillRepeatedly(Return(kBlocksPerFrame));
285 auto config = MakeCngConfig();
286 const auto sid_frame_interval_ms = config.sid_frame_interval_ms;
287 const auto num_cng_coefficients = config.num_cng_coefficients;
288 CreateCng(std::move(config));
289 EXPECT_CALL(*mock_vad_, VoiceActivity(_, _, _))
290 .WillRepeatedly(Return(Vad::kPassive));
291 // Expect no calls at all to the speech encoder mock.
292 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _)).Times(0);
293 uint32_t expected_timestamp = timestamp_;
294 for (size_t i = 0; i < 100; ++i) {
295 Encode();
296 // Check if it was time to call the cng encoder. This is done once every
297 // |kBlocksPerFrame| calls.
298 if ((i + 1) % kBlocksPerFrame == 0) {
299 // Now check if a SID interval has elapsed.
300 if ((i % (sid_frame_interval_ms / 10)) < kBlocksPerFrame) {
301 // If so, verify that we got a CNG encoding.
302 EXPECT_EQ(kCngPayloadType, encoded_info_.payload_type);
303 EXPECT_FALSE(encoded_info_.speech);
304 EXPECT_EQ(static_cast<size_t>(num_cng_coefficients) + 1,
305 encoded_info_.encoded_bytes);
306 EXPECT_EQ(expected_timestamp, encoded_info_.encoded_timestamp);
307 }
308 expected_timestamp += rtc::checked_cast<uint32_t>(
309 kBlocksPerFrame * num_audio_samples_10ms_);
310 } else {
311 // Otherwise, expect no output.
312 EXPECT_EQ(0u, encoded_info_.encoded_bytes);
313 }
314 }
315 }
316
317 // Verifies that the correct action is taken for frames with both active and
318 // passive speech.
TEST_F(AudioEncoderCngTest,MixedActivePassive)319 TEST_F(AudioEncoderCngTest, MixedActivePassive) {
320 CreateCng(MakeCngConfig());
321
322 // All of the frame is active speech.
323 ExpectEncodeCalls(6);
324 EXPECT_TRUE(CheckMixedActivePassive(Vad::kActive, Vad::kActive));
325 EXPECT_TRUE(encoded_info_.speech);
326
327 // First half of the frame is active speech.
328 ExpectEncodeCalls(6);
329 EXPECT_TRUE(CheckMixedActivePassive(Vad::kActive, Vad::kPassive));
330 EXPECT_TRUE(encoded_info_.speech);
331
332 // Second half of the frame is active speech.
333 ExpectEncodeCalls(6);
334 EXPECT_TRUE(CheckMixedActivePassive(Vad::kPassive, Vad::kActive));
335 EXPECT_TRUE(encoded_info_.speech);
336
337 // All of the frame is passive speech. Expect no calls to |mock_encoder_|.
338 EXPECT_FALSE(CheckMixedActivePassive(Vad::kPassive, Vad::kPassive));
339 EXPECT_FALSE(encoded_info_.speech);
340 }
341
342 // These tests verify that the audio is partitioned into larger blocks before
343 // calling the VAD.
344 // The parameters for CheckVadInputSize are:
345 // CheckVadInputSize(frame_size, expected_first_block_size,
346 // expected_second_block_size);
TEST_F(AudioEncoderCngTest,VadInputSize10Ms)347 TEST_F(AudioEncoderCngTest, VadInputSize10Ms) {
348 CreateCng(MakeCngConfig());
349 CheckVadInputSize(10, 10, 0);
350 }
TEST_F(AudioEncoderCngTest,VadInputSize20Ms)351 TEST_F(AudioEncoderCngTest, VadInputSize20Ms) {
352 CreateCng(MakeCngConfig());
353 CheckVadInputSize(20, 20, 0);
354 }
TEST_F(AudioEncoderCngTest,VadInputSize30Ms)355 TEST_F(AudioEncoderCngTest, VadInputSize30Ms) {
356 CreateCng(MakeCngConfig());
357 CheckVadInputSize(30, 30, 0);
358 }
TEST_F(AudioEncoderCngTest,VadInputSize40Ms)359 TEST_F(AudioEncoderCngTest, VadInputSize40Ms) {
360 CreateCng(MakeCngConfig());
361 CheckVadInputSize(40, 20, 20);
362 }
TEST_F(AudioEncoderCngTest,VadInputSize50Ms)363 TEST_F(AudioEncoderCngTest, VadInputSize50Ms) {
364 CreateCng(MakeCngConfig());
365 CheckVadInputSize(50, 30, 20);
366 }
TEST_F(AudioEncoderCngTest,VadInputSize60Ms)367 TEST_F(AudioEncoderCngTest, VadInputSize60Ms) {
368 CreateCng(MakeCngConfig());
369 CheckVadInputSize(60, 30, 30);
370 }
371
372 // Verifies that the correct payload type is set when CNG is encoded.
TEST_F(AudioEncoderCngTest,VerifyCngPayloadType)373 TEST_F(AudioEncoderCngTest, VerifyCngPayloadType) {
374 CreateCng(MakeCngConfig());
375 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _)).Times(0);
376 EXPECT_CALL(*mock_encoder_, Num10MsFramesInNextPacket()).WillOnce(Return(1U));
377 EXPECT_CALL(*mock_vad_, VoiceActivity(_, _, _))
378 .WillOnce(Return(Vad::kPassive));
379 encoded_info_.payload_type = 0;
380 Encode();
381 EXPECT_EQ(kCngPayloadType, encoded_info_.payload_type);
382 }
383
384 // Verifies that a SID frame is encoded immediately as the signal changes from
385 // active speech to passive.
TEST_F(AudioEncoderCngTest,VerifySidFrameAfterSpeech)386 TEST_F(AudioEncoderCngTest, VerifySidFrameAfterSpeech) {
387 auto config = MakeCngConfig();
388 const auto num_cng_coefficients = config.num_cng_coefficients;
389 CreateCng(std::move(config));
390 EXPECT_CALL(*mock_encoder_, Num10MsFramesInNextPacket())
391 .WillRepeatedly(Return(1U));
392 // Start with encoding noise.
393 EXPECT_CALL(*mock_vad_, VoiceActivity(_, _, _))
394 .Times(2)
395 .WillRepeatedly(Return(Vad::kPassive));
396 Encode();
397 EXPECT_EQ(kCngPayloadType, encoded_info_.payload_type);
398 EXPECT_EQ(static_cast<size_t>(num_cng_coefficients) + 1,
399 encoded_info_.encoded_bytes);
400 // Encode again, and make sure we got no frame at all (since the SID frame
401 // period is 100 ms by default).
402 Encode();
403 EXPECT_EQ(0u, encoded_info_.encoded_bytes);
404
405 // Now encode active speech.
406 encoded_info_.payload_type = 0;
407 EXPECT_CALL(*mock_vad_, VoiceActivity(_, _, _))
408 .WillOnce(Return(Vad::kActive));
409 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
410 .WillOnce(
411 Invoke(MockAudioEncoder::FakeEncoding(kMockReturnEncodedBytes)));
412 Encode();
413 EXPECT_EQ(kMockReturnEncodedBytes, encoded_info_.encoded_bytes);
414
415 // Go back to noise again, and verify that a SID frame is emitted.
416 EXPECT_CALL(*mock_vad_, VoiceActivity(_, _, _))
417 .WillOnce(Return(Vad::kPassive));
418 Encode();
419 EXPECT_EQ(kCngPayloadType, encoded_info_.payload_type);
420 EXPECT_EQ(static_cast<size_t>(num_cng_coefficients) + 1,
421 encoded_info_.encoded_bytes);
422 }
423
424 // Resetting the CNG should reset both the VAD and the encoder.
TEST_F(AudioEncoderCngTest,Reset)425 TEST_F(AudioEncoderCngTest, Reset) {
426 CreateCng(MakeCngConfig());
427 EXPECT_CALL(*mock_encoder_, Reset()).Times(1);
428 EXPECT_CALL(*mock_vad_, Reset()).Times(1);
429 cng_->Reset();
430 }
431
432 #if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
433
434 // This test fixture tests various error conditions that makes the
435 // AudioEncoderCng die via CHECKs.
436 class AudioEncoderCngDeathTest : public AudioEncoderCngTest {
437 protected:
AudioEncoderCngDeathTest()438 AudioEncoderCngDeathTest() : AudioEncoderCngTest() {
439 EXPECT_CALL(*mock_vad_, Die()).Times(1);
440 delete mock_vad_;
441 mock_vad_ = nullptr;
442 }
443
444 // Override AudioEncoderCngTest::TearDown, since that one expects a call to
445 // the destructor of |mock_vad_|. In this case, that object is already
446 // deleted.
TearDown()447 void TearDown() override { cng_.reset(); }
448
MakeCngConfig()449 AudioEncoderCngConfig MakeCngConfig() {
450 // Don't provide a Vad mock object, since it would leak when the test dies.
451 auto config = AudioEncoderCngTest::MakeCngConfig();
452 config.vad = nullptr;
453 return config;
454 }
455
TryWrongNumCoefficients(int num)456 void TryWrongNumCoefficients(int num) {
457 RTC_EXPECT_DEATH(
458 [&] {
459 auto config = MakeCngConfig();
460 config.num_cng_coefficients = num;
461 CreateCng(std::move(config));
462 }(),
463 "Invalid configuration");
464 }
465 };
466
TEST_F(AudioEncoderCngDeathTest,WrongFrameSize)467 TEST_F(AudioEncoderCngDeathTest, WrongFrameSize) {
468 CreateCng(MakeCngConfig());
469 num_audio_samples_10ms_ *= 2; // 20 ms frame.
470 RTC_EXPECT_DEATH(Encode(), "");
471 num_audio_samples_10ms_ = 0; // Zero samples.
472 RTC_EXPECT_DEATH(Encode(), "");
473 }
474
TEST_F(AudioEncoderCngDeathTest,WrongNumCoefficientsA)475 TEST_F(AudioEncoderCngDeathTest, WrongNumCoefficientsA) {
476 TryWrongNumCoefficients(-1);
477 }
478
TEST_F(AudioEncoderCngDeathTest,WrongNumCoefficientsB)479 TEST_F(AudioEncoderCngDeathTest, WrongNumCoefficientsB) {
480 TryWrongNumCoefficients(0);
481 }
482
TEST_F(AudioEncoderCngDeathTest,WrongNumCoefficientsC)483 TEST_F(AudioEncoderCngDeathTest, WrongNumCoefficientsC) {
484 TryWrongNumCoefficients(13);
485 }
486
TEST_F(AudioEncoderCngDeathTest,NullSpeechEncoder)487 TEST_F(AudioEncoderCngDeathTest, NullSpeechEncoder) {
488 auto config = MakeCngConfig();
489 config.speech_encoder = nullptr;
490 RTC_EXPECT_DEATH(CreateCng(std::move(config)), "");
491 }
492
TEST_F(AudioEncoderCngDeathTest,StereoEncoder)493 TEST_F(AudioEncoderCngDeathTest, StereoEncoder) {
494 EXPECT_CALL(*mock_encoder_, NumChannels()).WillRepeatedly(Return(2));
495 RTC_EXPECT_DEATH(CreateCng(MakeCngConfig()), "Invalid configuration");
496 }
497
TEST_F(AudioEncoderCngDeathTest,StereoConfig)498 TEST_F(AudioEncoderCngDeathTest, StereoConfig) {
499 RTC_EXPECT_DEATH(
500 [&] {
501 auto config = MakeCngConfig();
502 config.num_channels = 2;
503 CreateCng(std::move(config));
504 }(),
505 "Invalid configuration");
506 }
507
TEST_F(AudioEncoderCngDeathTest,EncoderFrameSizeTooLarge)508 TEST_F(AudioEncoderCngDeathTest, EncoderFrameSizeTooLarge) {
509 CreateCng(MakeCngConfig());
510 EXPECT_CALL(*mock_encoder_, Num10MsFramesInNextPacket())
511 .WillRepeatedly(Return(7U));
512 for (int i = 0; i < 6; ++i)
513 Encode();
514 RTC_EXPECT_DEATH(
515 Encode(), "Frame size cannot be larger than 60 ms when using VAD/CNG.");
516 }
517
518 #endif // GTEST_HAS_DEATH_TEST
519
520 } // namespace webrtc
521