summaryrefslogtreecommitdiff
path: root/chromium/media/webrtc/audio_processor_unittest.cc
blob: 31d5d1eeea2e4b1f33c3b6d127ceade03c3ca80b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>
#include <stdint.h>

#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/aligned_memory.h"
#include "base/path_service.h"
#include "base/stl_util.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "media/base/audio_bus.h"
#include "media/base/audio_parameters.h"
#include "media/webrtc/audio_processor.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
using ::testing::AnyNumber;
using ::testing::AtLeast;
using ::testing::Return;

namespace media {

namespace {

const int kAudioProcessingSampleRate = 48000;
const int kAudioProcessingNumberOfChannels = 1;

// The number of packers used for testing.
const int kNumberOfPacketsForTest = 100;
const int kMaxNumberOfPlayoutDataChannels = 2;

void ReadDataFromSpeechFile(char* data, int length) {
  base::FilePath file;
  CHECK(base::PathService::Get(base::DIR_SOURCE_ROOT, &file));
  file = file.Append(FILE_PATH_LITERAL("media"))
             .Append(FILE_PATH_LITERAL("test"))
             .Append(FILE_PATH_LITERAL("data"))
             .Append(FILE_PATH_LITERAL("speech_16b_stereo_48kHz.raw"));
  DCHECK(base::PathExists(file));
  int64_t data_file_size64 = 0;
  DCHECK(base::GetFileSize(file, &data_file_size64));
  EXPECT_EQ(length, base::ReadFile(file, data, length));
  DCHECK(data_file_size64 > length);
}

}  // namespace

class WebRtcAudioProcessorTest : public ::testing::Test {
 public:
  WebRtcAudioProcessorTest()
      : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
                media::CHANNEL_LAYOUT_STEREO,
                48000,
                480) {}

 protected:
  // Helper method to save duplicated code.
  void ProcessDataAndVerifyFormat(AudioProcessor* audio_processor,
                                  int expected_output_sample_rate,
                                  int expected_output_channels,
                                  int expected_output_buffer_size) {
    // Read the audio data from a file.
    const media::AudioParameters& params = audio_processor->audio_parameters_;
    const int packet_size = params.frames_per_buffer() * 2 * params.channels();
    const size_t length = packet_size * kNumberOfPacketsForTest;
    std::unique_ptr<char[]> capture_data(new char[length]);
    ReadDataFromSpeechFile(capture_data.get(), length);
    const int16_t* data_ptr =
        reinterpret_cast<const int16_t*>(capture_data.get());
    std::unique_ptr<media::AudioBus> data_bus =
        media::AudioBus::Create(params.channels(), params.frames_per_buffer());

    // |data_bus_playout| is used if the number of capture channels is larger
    // than max allowed playout channels. |data_bus_playout_to_use| points to
    // the AudioBus to use, either |data_bus| or |data_bus_playout|.
    std::unique_ptr<media::AudioBus> data_bus_playout;
    media::AudioBus* data_bus_playout_to_use = data_bus.get();
    media::AudioParameters playout_params = params;
    if (params.channels() > kMaxNumberOfPlayoutDataChannels) {
      data_bus_playout =
          media::AudioBus::CreateWrapper(kMaxNumberOfPlayoutDataChannels);
      data_bus_playout->set_frames(params.frames_per_buffer());
      data_bus_playout_to_use = data_bus_playout.get();
      playout_params.Reset(params.format(), CHANNEL_LAYOUT_STEREO,
                           params.sample_rate(), params.frames_per_buffer());
    }

    const base::TimeDelta input_capture_delay =
        base::TimeDelta::FromMilliseconds(20);
    for (int i = 0; i < kNumberOfPacketsForTest; ++i) {
      data_bus->FromInterleaved<SignedInt16SampleTypeTraits>(
          data_ptr, data_bus->frames());
      // |audio_processor| does nothing when the audio processing is off in
      // the processor.
      webrtc::AudioProcessing* ap = audio_processor->audio_processing_.get();
      const bool is_aec_enabled = ap && ap->GetConfig().echo_canceller.enabled;
      if (is_aec_enabled) {
        if (params.channels() > kMaxNumberOfPlayoutDataChannels) {
          for (int i = 0; i < kMaxNumberOfPlayoutDataChannels; ++i) {
            data_bus_playout->SetChannelData(
                i, const_cast<float*>(data_bus->channel(i)));
          }
        }
        base::TimeTicks in_the_future =
            base::TimeTicks::Now() + base::TimeDelta::FromMilliseconds(10);
        audio_processor->AnalyzePlayout(*data_bus_playout_to_use,
                                        playout_params, in_the_future);
      }

      auto result = audio_processor->ProcessCapture(
          *data_bus, base::TimeTicks::Now() - input_capture_delay, 1.0, false);
      data_ptr += params.frames_per_buffer() * params.channels();
    }
  }

  void VerifyEnabledComponents(AudioProcessor* audio_processor) {
    webrtc::AudioProcessing* audio_processing =
        audio_processor->audio_processing_.get();
    webrtc::AudioProcessing::Config ap_config = audio_processing->GetConfig();
    EXPECT_TRUE(ap_config.echo_canceller.enabled);
    EXPECT_FALSE(ap_config.echo_canceller.mobile_mode);
    EXPECT_TRUE(ap_config.high_pass_filter.enabled);
    EXPECT_TRUE(ap_config.gain_controller1.enabled);
    EXPECT_EQ(ap_config.gain_controller1.mode,
              ap_config.gain_controller1.kAdaptiveAnalog);
    EXPECT_TRUE(ap_config.noise_suppression.enabled);
    EXPECT_EQ(ap_config.noise_suppression.level,
              ap_config.noise_suppression.kHigh);
    EXPECT_TRUE(ap_config.voice_detection.enabled);
  }

  AudioProcessingSettings GetEnabledAudioProcessingSettings() const {
    AudioProcessingSettings settings;
    settings.echo_cancellation = EchoCancellationType::kAec3;
    settings.noise_suppression = NoiseSuppressionType::kExperimental;
    settings.automatic_gain_control = AutomaticGainControlType::kExperimental;
    settings.high_pass_filter = true;
    settings.typing_detection = true;
    return settings;
  }

  base::test::ScopedTaskEnvironment scoped_task_environment_;
  media::AudioParameters params_;
};

TEST_F(WebRtcAudioProcessorTest, WithAudioProcessing) {
  AudioProcessor audio_processor(params_, GetEnabledAudioProcessingSettings());
  VerifyEnabledComponents(&audio_processor);
  ProcessDataAndVerifyFormat(&audio_processor, kAudioProcessingSampleRate,
                             kAudioProcessingNumberOfChannels,
                             kAudioProcessingSampleRate / 100);
}

TEST_F(WebRtcAudioProcessorTest, WithoutAnyProcessing) {
  // All processing settings are disabled by default
  AudioProcessingSettings settings;
  const media::AudioParameters source_params(
      media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
      media::CHANNEL_LAYOUT_STEREO, kAudioProcessingSampleRate,
      kAudioProcessingSampleRate / 100);
  AudioProcessor audio_processor(source_params, settings);

  ProcessDataAndVerifyFormat(&audio_processor, params_.sample_rate(),
                             params_.channels(), params_.sample_rate() / 100);
}

TEST_F(WebRtcAudioProcessorTest, TestAllSampleRates) {
  for (int sample_rate : {8000, 16000, 32000, 44100, 48000}) {
    int buffer_size = sample_rate / 100;
    media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
                                  media::CHANNEL_LAYOUT_STEREO, sample_rate,
                                  buffer_size);
    AudioProcessor audio_processor(params, GetEnabledAudioProcessingSettings());

    VerifyEnabledComponents(&audio_processor);

    ProcessDataAndVerifyFormat(&audio_processor, kAudioProcessingSampleRate,
                               kAudioProcessingNumberOfChannels,
                               kAudioProcessingSampleRate / 100);
  }
}

TEST_F(WebRtcAudioProcessorTest, TestStereoAudio) {
  // All processing settings are disabled by default
  AudioProcessingSettings settings;
  settings.stereo_mirroring = true;
  const media::AudioParameters source_params(
      media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
      media::CHANNEL_LAYOUT_STEREO, kAudioProcessingSampleRate,
      kAudioProcessingSampleRate / 100);
  AudioProcessor audio_processor(source_params, settings);

  // Construct left and right channels, and assign different values to the
  // first data of the left channel and right channel.
  const int size = media::AudioBus::CalculateMemorySize(source_params);
  std::unique_ptr<float, base::AlignedFreeDeleter> left_channel(
      static_cast<float*>(base::AlignedAlloc(size, 32)));
  std::unique_ptr<float, base::AlignedFreeDeleter> right_channel(
      static_cast<float*>(base::AlignedAlloc(size, 32)));
  std::unique_ptr<media::AudioBus> wrapper =
      media::AudioBus::CreateWrapper(source_params.channels());
  wrapper->set_frames(source_params.frames_per_buffer());
  wrapper->SetChannelData(0, left_channel.get());
  wrapper->SetChannelData(1, right_channel.get());
  wrapper->Zero();
  float* left_channel_ptr = left_channel.get();
  left_channel_ptr[0] = 1.0f;

  // Run the test consecutively to make sure the stereo channels are not
  // flipped back and forth.
  static const int kNumberOfPacketsForTest = 100;
  const base::TimeDelta pushed_capture_delay =
      base::TimeDelta::FromMilliseconds(42);
  for (int i = 0; i < kNumberOfPacketsForTest; ++i) {
    auto result = audio_processor.ProcessCapture(
        *wrapper, base::TimeTicks::Now() + pushed_capture_delay, 1.0, false);

    EXPECT_EQ(result.audio.channel(0)[0], 0);
    EXPECT_NE(result.audio.channel(1)[0], 0);
  }
}

TEST_F(WebRtcAudioProcessorTest, TestWithKeyboardMicChannel) {
  AudioProcessingSettings settings = GetEnabledAudioProcessingSettings();
  media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
                                media::CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC,
                                kAudioProcessingSampleRate,
                                kAudioProcessingSampleRate / 100);
  AudioProcessor audio_processor(params, settings);
  ProcessDataAndVerifyFormat(&audio_processor, kAudioProcessingSampleRate,
                             kAudioProcessingNumberOfChannels,
                             kAudioProcessingSampleRate / 100);
}

TEST_F(WebRtcAudioProcessorTest, StartStopAecDump) {
  base::ScopedTempDir temp_directory;
  ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
  base::FilePath temp_file_path;
  ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory.GetPath(),
                                             &temp_file_path));
  {
    AudioProcessor audio_processor(params_,
                                   GetEnabledAudioProcessingSettings());

    // Start and stop recording.
    audio_processor.StartEchoCancellationDump(base::File(
        temp_file_path, base::File::FLAG_WRITE | base::File::FLAG_OPEN));
    audio_processor.StopEchoCancellationDump();

    // Start and wait for d-tor.
    audio_processor.StartEchoCancellationDump(base::File(
        temp_file_path, base::File::FLAG_WRITE | base::File::FLAG_OPEN));
  }

  // Check that dump file is non-empty after audio processor has been
  // destroyed. Note that this test fails when compiling WebRTC
  // without protobuf support, rtc_enable_protobuf=false.
  std::string output;
  ASSERT_TRUE(base::ReadFileToString(temp_file_path, &output));
  ASSERT_FALSE(output.empty());
  // The temporary file is deleted when temp_directory exists scope.
}

}  // namespace media