1 /*
2 * Copyright (c) 2017 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_processing/aec3/render_delay_buffer.h"
12
13 #include <memory>
14 #include <string>
15 #include <vector>
16
17 #include "api/array_view.h"
18 #include "modules/audio_processing/aec3/aec3_common.h"
19 #include "modules/audio_processing/logging/apm_data_dumper.h"
20 #include "rtc_base/random.h"
21 #include "rtc_base/strings/string_builder.h"
22 #include "test/gtest.h"
23
24 namespace webrtc {
25 namespace {
26
ProduceDebugText(int sample_rate_hz)27 std::string ProduceDebugText(int sample_rate_hz) {
28 rtc::StringBuilder ss;
29 ss << "Sample rate: " << sample_rate_hz;
30 return ss.Release();
31 }
32
33 } // namespace
34
35 // Verifies that the buffer overflow is correctly reported.
TEST(RenderDelayBuffer,BufferOverflow)36 TEST(RenderDelayBuffer, BufferOverflow) {
37 const EchoCanceller3Config config;
38 for (auto num_channels : {1, 2, 8}) {
39 for (auto rate : {16000, 32000, 48000}) {
40 SCOPED_TRACE(ProduceDebugText(rate));
41 std::unique_ptr<RenderDelayBuffer> delay_buffer(
42 RenderDelayBuffer::Create(config, rate, num_channels));
43 std::vector<std::vector<std::vector<float>>> block_to_insert(
44 NumBandsForRate(rate),
45 std::vector<std::vector<float>>(num_channels,
46 std::vector<float>(kBlockSize, 0.f)));
47 for (size_t k = 0; k < 10; ++k) {
48 EXPECT_EQ(RenderDelayBuffer::BufferingEvent::kNone,
49 delay_buffer->Insert(block_to_insert));
50 }
51 bool overrun_occurred = false;
52 for (size_t k = 0; k < 1000; ++k) {
53 RenderDelayBuffer::BufferingEvent event =
54 delay_buffer->Insert(block_to_insert);
55 overrun_occurred =
56 overrun_occurred ||
57 RenderDelayBuffer::BufferingEvent::kRenderOverrun == event;
58 }
59
60 EXPECT_TRUE(overrun_occurred);
61 }
62 }
63 }
64
65 // Verifies that the check for available block works.
TEST(RenderDelayBuffer,AvailableBlock)66 TEST(RenderDelayBuffer, AvailableBlock) {
67 constexpr size_t kNumChannels = 1;
68 constexpr int kSampleRateHz = 48000;
69 constexpr size_t kNumBands = NumBandsForRate(kSampleRateHz);
70 std::unique_ptr<RenderDelayBuffer> delay_buffer(RenderDelayBuffer::Create(
71 EchoCanceller3Config(), kSampleRateHz, kNumChannels));
72 std::vector<std::vector<std::vector<float>>> input_block(
73 kNumBands, std::vector<std::vector<float>>(
74 kNumChannels, std::vector<float>(kBlockSize, 1.f)));
75 EXPECT_EQ(RenderDelayBuffer::BufferingEvent::kNone,
76 delay_buffer->Insert(input_block));
77 delay_buffer->PrepareCaptureProcessing();
78 }
79
80 // Verifies the AlignFromDelay method.
TEST(RenderDelayBuffer,AlignFromDelay)81 TEST(RenderDelayBuffer, AlignFromDelay) {
82 EchoCanceller3Config config;
83 std::unique_ptr<RenderDelayBuffer> delay_buffer(
84 RenderDelayBuffer::Create(config, 16000, 1));
85 ASSERT_TRUE(delay_buffer->Delay());
86 delay_buffer->Reset();
87 size_t initial_internal_delay = 0;
88 for (size_t delay = initial_internal_delay;
89 delay < initial_internal_delay + 20; ++delay) {
90 ASSERT_TRUE(delay_buffer->AlignFromDelay(delay));
91 EXPECT_EQ(delay, delay_buffer->Delay());
92 }
93 }
94
95 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
96
97 // Verifies the check for feasible delay.
98 // TODO(peah): Re-enable the test once the issue with memory leaks during DEATH
99 // tests on test bots has been fixed.
TEST(RenderDelayBufferDeathTest,DISABLED_WrongDelay)100 TEST(RenderDelayBufferDeathTest, DISABLED_WrongDelay) {
101 std::unique_ptr<RenderDelayBuffer> delay_buffer(
102 RenderDelayBuffer::Create(EchoCanceller3Config(), 48000, 1));
103 EXPECT_DEATH(delay_buffer->AlignFromDelay(21), "");
104 }
105
106 // Verifies the check for the number of bands in the inserted blocks.
TEST(RenderDelayBufferDeathTest,WrongNumberOfBands)107 TEST(RenderDelayBufferDeathTest, WrongNumberOfBands) {
108 for (auto rate : {16000, 32000, 48000}) {
109 for (size_t num_channels : {1, 2, 8}) {
110 SCOPED_TRACE(ProduceDebugText(rate));
111 std::unique_ptr<RenderDelayBuffer> delay_buffer(RenderDelayBuffer::Create(
112 EchoCanceller3Config(), rate, num_channels));
113 std::vector<std::vector<std::vector<float>>> block_to_insert(
114 NumBandsForRate(rate < 48000 ? rate + 16000 : 16000),
115 std::vector<std::vector<float>>(num_channels,
116 std::vector<float>(kBlockSize, 0.f)));
117 EXPECT_DEATH(delay_buffer->Insert(block_to_insert), "");
118 }
119 }
120 }
121
122 // Verifies the check for the number of channels in the inserted blocks.
TEST(RenderDelayBufferDeathTest,WrongNumberOfChannels)123 TEST(RenderDelayBufferDeathTest, WrongNumberOfChannels) {
124 for (auto rate : {16000, 32000, 48000}) {
125 for (size_t num_channels : {1, 2, 8}) {
126 SCOPED_TRACE(ProduceDebugText(rate));
127 std::unique_ptr<RenderDelayBuffer> delay_buffer(RenderDelayBuffer::Create(
128 EchoCanceller3Config(), rate, num_channels));
129 std::vector<std::vector<std::vector<float>>> block_to_insert(
130 NumBandsForRate(rate),
131 std::vector<std::vector<float>>(num_channels + 1,
132 std::vector<float>(kBlockSize, 0.f)));
133 EXPECT_DEATH(delay_buffer->Insert(block_to_insert), "");
134 }
135 }
136 }
137
138 // Verifies the check of the length of the inserted blocks.
TEST(RenderDelayBufferDeathTest,WrongBlockLength)139 TEST(RenderDelayBufferDeathTest, WrongBlockLength) {
140 for (auto rate : {16000, 32000, 48000}) {
141 for (size_t num_channels : {1, 2, 8}) {
142 SCOPED_TRACE(ProduceDebugText(rate));
143 std::unique_ptr<RenderDelayBuffer> delay_buffer(RenderDelayBuffer::Create(
144 EchoCanceller3Config(), rate, num_channels));
145 std::vector<std::vector<std::vector<float>>> block_to_insert(
146 NumBandsForRate(rate),
147 std::vector<std::vector<float>>(
148 num_channels, std::vector<float>(kBlockSize - 1, 0.f)));
149 EXPECT_DEATH(delay_buffer->Insert(block_to_insert), "");
150 }
151 }
152 }
153
154 #endif
155
156 } // namespace webrtc
157