summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/audio_device/test_audio_device_impl_test.cc
blob: e81bb2f807c97105382fbd076241ff7119da52a0 (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
/*
 *  Copyright (c) 2023 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
#include "modules/audio_device/test_audio_device_impl.h"

#include <memory>
#include <utility>

#include "absl/types/optional.h"
#include "api/task_queue/task_queue_factory.h"
#include "api/units/time_delta.h"
#include "api/units/timestamp.h"
#include "modules/audio_device/audio_device_buffer.h"
#include "modules/audio_device/audio_device_generic.h"
#include "modules/audio_device/include/audio_device.h"
#include "modules/audio_device/include/audio_device_defines.h"
#include "modules/audio_device/include/test_audio_device.h"
#include "rtc_base/checks.h"
#include "rtc_base/synchronization/mutex.h"
#include "test/gmock.h"
#include "test/gtest.h"
#include "test/time_controller/simulated_time_controller.h"

namespace webrtc {
namespace {

using ::testing::ElementsAre;

constexpr Timestamp kStartTime = Timestamp::Millis(10000);

class TestAudioTransport : public AudioTransport {
 public:
  enum class Mode { kPlaying, kRecording };

  explicit TestAudioTransport(Mode mode) : mode_(mode) {}
  ~TestAudioTransport() override = default;

  int32_t RecordedDataIsAvailable(
      const void* audioSamples,
      size_t samples_per_channel,
      size_t bytes_per_sample,
      size_t number_of_channels,
      uint32_t samples_per_second,
      uint32_t total_delay_ms,
      int32_t clock_drift,
      uint32_t current_mic_level,
      bool key_pressed,
      uint32_t& new_mic_level,
      absl::optional<int64_t> estimated_capture_time_ns) override {
    new_mic_level = 1;

    if (mode_ != Mode::kRecording) {
      EXPECT_TRUE(false) << "RecordedDataIsAvailable mustn't be called when "
                            "mode isn't kRecording";
      return -1;
    }

    MutexLock lock(&mutex_);
    samples_per_channel_.push_back(samples_per_channel);
    number_of_channels_.push_back(number_of_channels);
    bytes_per_sample_.push_back(bytes_per_sample);
    samples_per_second_.push_back(samples_per_second);
    return 0;
  }

  int32_t NeedMorePlayData(size_t samples_per_channel,
                           size_t bytes_per_sample,
                           size_t number_of_channels,
                           uint32_t samples_per_second,
                           void* audio_samples,
                           size_t& samples_out,
                           int64_t* elapsed_time_ms,
                           int64_t* ntp_time_ms) override {
    const size_t num_bytes = samples_per_channel * number_of_channels;
    std::memset(audio_samples, 1, num_bytes);
    samples_out = samples_per_channel * number_of_channels;
    *elapsed_time_ms = 0;
    *ntp_time_ms = 0;

    if (mode_ != Mode::kPlaying) {
      EXPECT_TRUE(false)
          << "NeedMorePlayData mustn't be called when mode isn't kPlaying";
      return -1;
    }

    MutexLock lock(&mutex_);
    samples_per_channel_.push_back(samples_per_channel);
    number_of_channels_.push_back(number_of_channels);
    bytes_per_sample_.push_back(bytes_per_sample);
    samples_per_second_.push_back(samples_per_second);
    return 0;
  }

  int32_t RecordedDataIsAvailable(const void* audio_samples,
                                  size_t samples_per_channel,
                                  size_t bytes_per_sample,
                                  size_t number_of_channels,
                                  uint32_t samples_per_second,
                                  uint32_t total_delay_ms,
                                  int32_t clockDrift,
                                  uint32_t current_mic_level,
                                  bool key_pressed,
                                  uint32_t& new_mic_level) override {
    RTC_CHECK(false) << "This methods should be never executed";
  }

  void PullRenderData(int bits_per_sample,
                      int sample_rate,
                      size_t number_of_channels,
                      size_t number_of_frames,
                      void* audio_data,
                      int64_t* elapsed_time_ms,
                      int64_t* ntp_time_ms) override {
    RTC_CHECK(false) << "This methods should be never executed";
  }

  std::vector<size_t> samples_per_channel() const {
    MutexLock lock(&mutex_);
    return samples_per_channel_;
  }
  std::vector<size_t> number_of_channels() const {
    MutexLock lock(&mutex_);
    return number_of_channels_;
  }
  std::vector<size_t> bytes_per_sample() const {
    MutexLock lock(&mutex_);
    return bytes_per_sample_;
  }
  std::vector<size_t> samples_per_second() const {
    MutexLock lock(&mutex_);
    return samples_per_second_;
  }

 private:
  const Mode mode_;

  mutable Mutex mutex_;
  std::vector<size_t> samples_per_channel_ RTC_GUARDED_BY(mutex_);
  std::vector<size_t> number_of_channels_ RTC_GUARDED_BY(mutex_);
  std::vector<size_t> bytes_per_sample_ RTC_GUARDED_BY(mutex_);
  std::vector<size_t> samples_per_second_ RTC_GUARDED_BY(mutex_);
};

TEST(TestAudioDeviceTest, EnablingRecordingProducesAudio) {
  GlobalSimulatedTimeController time_controller(kStartTime);
  TestAudioTransport audio_transport(TestAudioTransport::Mode::kRecording);
  AudioDeviceBuffer audio_buffer(time_controller.GetTaskQueueFactory());
  ASSERT_EQ(audio_buffer.RegisterAudioCallback(&audio_transport), 0);
  std::unique_ptr<TestAudioDeviceModule::PulsedNoiseCapturer> capturer =
      TestAudioDeviceModule::CreatePulsedNoiseCapturer(
          /*max_amplitude=*/1000,
          /*sampling_frequency_in_hz=*/48000, /*num_channels=*/2);

  TestAudioDevice audio_device(time_controller.GetTaskQueueFactory(),
                               std::move(capturer),
                               /*renderer=*/nullptr);
  ASSERT_EQ(audio_device.Init(), AudioDeviceGeneric::InitStatus::OK);
  audio_device.AttachAudioBuffer(&audio_buffer);

  EXPECT_FALSE(audio_device.RecordingIsInitialized());
  ASSERT_EQ(audio_device.InitRecording(), 0);
  EXPECT_TRUE(audio_device.RecordingIsInitialized());
  audio_buffer.StartRecording();
  ASSERT_EQ(audio_device.StartRecording(), 0);
  time_controller.AdvanceTime(TimeDelta::Millis(10));
  ASSERT_TRUE(audio_device.Recording());
  time_controller.AdvanceTime(TimeDelta::Millis(10));
  ASSERT_EQ(audio_device.StopRecording(), 0);
  audio_buffer.StopRecording();

  EXPECT_THAT(audio_transport.samples_per_channel(),
              ElementsAre(480, 480, 480));
  EXPECT_THAT(audio_transport.number_of_channels(), ElementsAre(2, 2, 2));
  EXPECT_THAT(audio_transport.bytes_per_sample(), ElementsAre(4, 4, 4));
  EXPECT_THAT(audio_transport.samples_per_second(),
              ElementsAre(48000, 48000, 48000));
}

TEST(TestAudioDeviceTest, RecordingIsAvailableWhenCapturerIsSet) {
  GlobalSimulatedTimeController time_controller(kStartTime);
  std::unique_ptr<TestAudioDeviceModule::PulsedNoiseCapturer> capturer =
      TestAudioDeviceModule::CreatePulsedNoiseCapturer(
          /*max_amplitude=*/1000,
          /*sampling_frequency_in_hz=*/48000, /*num_channels=*/2);

  TestAudioDevice audio_device(time_controller.GetTaskQueueFactory(),
                               std::move(capturer),
                               /*renderer=*/nullptr);
  ASSERT_EQ(audio_device.Init(), AudioDeviceGeneric::InitStatus::OK);

  bool available;
  EXPECT_EQ(audio_device.RecordingIsAvailable(available), 0);
  EXPECT_TRUE(available);
}

TEST(TestAudioDeviceTest, RecordingIsNotAvailableWhenCapturerIsNotSet) {
  GlobalSimulatedTimeController time_controller(kStartTime);
  TestAudioDevice audio_device(time_controller.GetTaskQueueFactory(),
                               /*capturer=*/nullptr,
                               /*renderer=*/nullptr);
  ASSERT_EQ(audio_device.Init(), AudioDeviceGeneric::InitStatus::OK);

  bool available;
  EXPECT_EQ(audio_device.RecordingIsAvailable(available), 0);
  EXPECT_FALSE(available);
}

TEST(TestAudioDeviceTest, EnablingPlayoutProducesAudio) {
  GlobalSimulatedTimeController time_controller(kStartTime);
  TestAudioTransport audio_transport(TestAudioTransport::Mode::kPlaying);
  AudioDeviceBuffer audio_buffer(time_controller.GetTaskQueueFactory());
  ASSERT_EQ(audio_buffer.RegisterAudioCallback(&audio_transport), 0);
  std::unique_ptr<TestAudioDeviceModule::Renderer> renderer =
      TestAudioDeviceModule::CreateDiscardRenderer(
          /*sampling_frequency_in_hz=*/48000, /*num_channels=*/2);

  TestAudioDevice audio_device(time_controller.GetTaskQueueFactory(),
                               /*capturer=*/nullptr, std::move(renderer));
  ASSERT_EQ(audio_device.Init(), AudioDeviceGeneric::InitStatus::OK);
  audio_device.AttachAudioBuffer(&audio_buffer);

  EXPECT_FALSE(audio_device.PlayoutIsInitialized());
  ASSERT_EQ(audio_device.InitPlayout(), 0);
  EXPECT_TRUE(audio_device.PlayoutIsInitialized());
  audio_buffer.StartPlayout();
  ASSERT_EQ(audio_device.StartPlayout(), 0);
  time_controller.AdvanceTime(TimeDelta::Millis(10));
  ASSERT_TRUE(audio_device.Playing());
  time_controller.AdvanceTime(TimeDelta::Millis(10));
  ASSERT_EQ(audio_device.StopPlayout(), 0);
  audio_buffer.StopPlayout();

  EXPECT_THAT(audio_transport.samples_per_channel(),
              ElementsAre(480, 480, 480));
  EXPECT_THAT(audio_transport.number_of_channels(), ElementsAre(2, 2, 2));
  EXPECT_THAT(audio_transport.bytes_per_sample(), ElementsAre(4, 4, 4));
  EXPECT_THAT(audio_transport.samples_per_second(),
              ElementsAre(48000, 48000, 48000));
}

TEST(TestAudioDeviceTest, PlayoutIsAvailableWhenRendererIsSet) {
  GlobalSimulatedTimeController time_controller(kStartTime);
  std::unique_ptr<TestAudioDeviceModule::Renderer> renderer =
      TestAudioDeviceModule::CreateDiscardRenderer(
          /*sampling_frequency_in_hz=*/48000, /*num_channels=*/2);

  TestAudioDevice audio_device(time_controller.GetTaskQueueFactory(),
                               /*capturer=*/nullptr, std::move(renderer));
  ASSERT_EQ(audio_device.Init(), AudioDeviceGeneric::InitStatus::OK);

  bool available;
  EXPECT_EQ(audio_device.PlayoutIsAvailable(available), 0);
  EXPECT_TRUE(available);
}

TEST(TestAudioDeviceTest, PlayoutIsNotAvailableWhenRendererIsNotSet) {
  GlobalSimulatedTimeController time_controller(kStartTime);
  TestAudioDevice audio_device(time_controller.GetTaskQueueFactory(),
                               /*capturer=*/nullptr,
                               /*renderer=*/nullptr);
  ASSERT_EQ(audio_device.Init(), AudioDeviceGeneric::InitStatus::OK);

  bool available;
  EXPECT_EQ(audio_device.PlayoutIsAvailable(available), 0);
  EXPECT_FALSE(available);
}

}  // namespace
}  // namespace webrtc