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
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306

media / audio / audio_input_device_unittest.cc [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/audio/audio_input_device.h"

#include <utility>

#include "base/memory/ptr_util.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/process/process_handle.h"
#include "base/run_loop.h"
#include "base/sync_socket.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "media/base/audio_glitch_info.h"
#include "media/base/media_switches.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::CancelableSyncSocket;
using base::SyncSocket;
using testing::_;
using testing::DoAll;
using testing::Invoke;
using testing::InvokeWithoutArgs;

namespace media {

namespace {

const size_t kMemorySegmentCount = 10u;

class MockAudioInputIPC : public AudioInputIPC {
 public:
  MockAudioInputIPC() = default;
  ~MockAudioInputIPC() override = default;

  MOCK_METHOD4(CreateStream,
               void(AudioInputIPCDelegate* delegate,
                    const AudioParameters& params,
                    bool automatic_gain_control,
                    uint32_t total_segments));
  MOCK_METHOD0(RecordStream, void());
  MOCK_METHOD1(SetVolume, void(double volume));
  MOCK_METHOD1(SetOutputDeviceForAec, void(const std::string&));
  MOCK_METHOD0(CloseStream, void());
};

class MockCaptureCallback : public AudioCapturerSource::CaptureCallback {
 public:
  MockCaptureCallback() = default;
  ~MockCaptureCallback() override = default;

  MOCK_METHOD0(OnCaptureStarted, void());
  MOCK_METHOD4(Capture,
               void(const AudioBus* audio_source,
                    base::TimeTicks audio_capture_time,
                    const AudioGlitchInfo& glitch_info,
                    double volume));

  MOCK_METHOD2(OnCaptureError,
               void(AudioCapturerSource::ErrorCode code,
                    const std::string& message));
  MOCK_METHOD1(OnCaptureMuted, void(bool is_muted));
};

// Verifies that the capture time and glitch info passed to Capture() are
// correct.
class AssertingCaptureCallback : public AudioCapturerSource::CaptureCallback {
 public:
  AssertingCaptureCallback() = default;
  ~AssertingCaptureCallback() override = default;

  MOCK_METHOD2(VerifyCapture,
               void(base::TimeTicks audio_capture_time,
                    const AudioGlitchInfo& glitch_info));

  void Capture(const AudioBus* audio_source,
               base::TimeTicks audio_capture_time,
               const AudioGlitchInfo& glitch_info,
               double volume) override {
    VerifyCapture(audio_capture_time, glitch_info);
    capture_called_event_.Signal();
  }

  void WaitForCapture() { capture_called_event_.Wait(); }

  MOCK_METHOD0(OnCaptureStarted, void());
  MOCK_METHOD2(OnCaptureError,
               void(AudioCapturerSource::ErrorCode code,
                    const std::string& message));
  MOCK_METHOD1(OnCaptureMuted, void(bool is_muted));

 private:
  base::WaitableEvent capture_called_event_;
};

}  // namespace.

class AudioInputDeviceTest
    : public ::testing::TestWithParam<AudioInputDevice::DeadStreamDetection> {
 protected:
  void CreateInputDevice() {
    AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
                           ChannelLayoutConfig::Stereo(), 48000, 480);

    const uint32_t memory_size =
        ComputeAudioInputBufferSize(params, kMemorySegmentCount);

    shared_memory_ = base::UnsafeSharedMemoryRegion::Create(memory_size);
    shared_memory_mapping_ = shared_memory_.Map();
    ASSERT_TRUE(shared_memory_.IsValid());
    memset(shared_memory_mapping_.memory(), 0xff, memory_size);

    ASSERT_TRUE(
        CancelableSyncSocket::CreatePair(&browser_socket_, &renderer_socket_));

    MockAudioInputIPC* input_ipc = new MockAudioInputIPC();

    device_ = base::MakeRefCounted<AudioInputDevice>(
        base::WrapUnique(input_ipc), AudioInputDevice::Purpose::kUserInput,
        AudioInputDeviceTest::GetParam());

    capture_callback_.emplace();
    device_->Initialize(params, &capture_callback_.value());

    EXPECT_CALL(*input_ipc, CreateStream(_, _, _, _))
        .WillOnce(InvokeWithoutArgs([&]() {
          auto duplicated_shared_memory_region = shared_memory_.Duplicate();
          CHECK(duplicated_shared_memory_region.IsValid());
          static_cast<AudioInputIPCDelegate*>(device_.get())
              ->OnStreamCreated(std::move(duplicated_shared_memory_region),
                                renderer_socket_.Take(), false);
        }));
    EXPECT_CALL(*input_ipc, RecordStream());
    EXPECT_CALL(*capture_callback_, OnCaptureStarted());
    EXPECT_CALL(*input_ipc, CloseStream());

    uint8_t* ptr = static_cast<uint8_t*>(shared_memory_mapping_.memory());
    buffer_ = reinterpret_cast<AudioInputBuffer*>(ptr);
    buffer_->params.id = 0;
    buffer_->params.capture_time_us =
        (capture_time_ - base::TimeTicks()).InMicroseconds();
    buffer_->params.glitch_duration_us = glitch_info_.duration.InMicroseconds();
    buffer_->params.glitch_count = glitch_info_.count;
  }

  base::UnsafeSharedMemoryRegion shared_memory_;
  base::WritableSharedMemoryMapping shared_memory_mapping_;
  CancelableSyncSocket browser_socket_;
  CancelableSyncSocket renderer_socket_;
  std::optional<AssertingCaptureCallback> capture_callback_;
  scoped_refptr<AudioInputDevice> device_;
  const base::TimeTicks capture_time_ =
      base::TimeTicks() + base::Microseconds(123);
  const AudioGlitchInfo glitch_info_{.duration = base::Microseconds(20000),
                                     .count = 2};
  raw_ptr<AudioInputBuffer> buffer_;
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Regular construction.
TEST_P(AudioInputDeviceTest, Noop) {
  base::test::SingleThreadTaskEnvironment task_environment(
      base::test::SingleThreadTaskEnvironment::MainThreadType::IO);
  MockAudioInputIPC* input_ipc = new MockAudioInputIPC();
  scoped_refptr<AudioInputDevice> device(new AudioInputDevice(
      base::WrapUnique(input_ipc), AudioInputDevice::Purpose::kUserInput,
      AudioInputDeviceTest::GetParam()));
}

ACTION_P(ReportStateChange, device) {
  static_cast<AudioInputIPCDelegate*>(device)->OnError(
      AudioCapturerSource::ErrorCode::kUnknown);
}

// Verify that we get an OnCaptureError() callback if CreateStream fails.
TEST_P(AudioInputDeviceTest, FailToCreateStream) {
  AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
                         ChannelLayoutConfig::Stereo(), 48000, 480);

  MockCaptureCallback callback;
  MockAudioInputIPC* input_ipc = new MockAudioInputIPC();
  scoped_refptr<AudioInputDevice> device(new AudioInputDevice(
      base::WrapUnique(input_ipc), AudioInputDevice::Purpose::kUserInput,
      AudioInputDeviceTest::GetParam()));
  device->Initialize(params, &callback);
  EXPECT_CALL(*input_ipc, CreateStream(_, _, _, _))
      .WillOnce(ReportStateChange(device.get()));
  EXPECT_CALL(callback,
              OnCaptureError(AudioCapturerSource::ErrorCode::kUnknown, _));
  EXPECT_CALL(*input_ipc, CloseStream());
  device->Start();
  device->Stop();
}

TEST_P(AudioInputDeviceTest, CreateStream) {
  base::test::TaskEnvironment ste;
  CreateInputDevice();

  device_->Start();
  device_->Stop();
}

TEST_P(AudioInputDeviceTest, CaptureCallback) {
  base::test::TaskEnvironment ste;
  CreateInputDevice();

  uint32_t buffer_index = 0;
  browser_socket_.Send(base::byte_span_from_ref(buffer_index));

  EXPECT_CALL(*capture_callback_, OnCaptureError(_, _)).Times(0);
  EXPECT_CALL(*capture_callback_, VerifyCapture(capture_time_, glitch_info_));

  device_->Start();
  ste.RunUntilIdle();

  // The capture occurs on another thread, wait for it.
  capture_callback_->WaitForCapture();

  // We expect to get 1 as the confirmation that the AudioInputDevice has read
  // the buffer.
  uint32_t confirmation_signal;
  size_t bytes_read =
      browser_socket_.Receive(base::byte_span_from_ref(confirmation_signal));
  EXPECT_EQ(bytes_read, sizeof(confirmation_signal));
  EXPECT_EQ(confirmation_signal, 1u);

  device_->Stop();
}

TEST_P(AudioInputDeviceTest, ConfirmReadsViaShmemFlag) {
  base::test::TaskEnvironment ste;

  scoped_feature_list_.InitWithFeatures(
      {base::test::FeatureRef(media::kAudioInputConfirmReadsViaShmem)}, {});

  CreateInputDevice();

  // Set the confirmation flag to 1. The AudioInputDevice should reset this to 0
  // after delivering audio.
  base::subtle::Release_Store(&(buffer_->params.has_unread_data), 1);
  uint32_t buffer_index = 0;
  browser_socket_.Send(base::byte_span_from_ref(buffer_index));

  EXPECT_CALL(*capture_callback_, OnCaptureError(_, _)).Times(0);
  EXPECT_CALL(*capture_callback_, VerifyCapture(capture_time_, glitch_info_));

  device_->Start();
  ste.RunUntilIdle();

  // The capture occurs on another thread, wait for it.
  capture_callback_->WaitForCapture();

  // Wait 10 seconds for the confirmation signal to be written to shared memory.
  // The loop is expected to finish immediately, but we wait 10 seconds to
  // ensure that the test does not become flaky.
  base::TimeTicks started_wait = base::TimeTicks::Now();
  bool got_confirmation_signal = false;
  while (!got_confirmation_signal &&
         base::TimeTicks::Now() - started_wait < base::Seconds(10)) {
    got_confirmation_signal =
        base::subtle::NoBarrier_Load(&(buffer_->params.has_unread_data)) == 0;
  }
  EXPECT_TRUE(got_confirmation_signal);

  // When the optimization is enabled, we don't send confirmation signals.
  EXPECT_EQ(browser_socket_.Peek(), 0u);

  device_->Stop();
}

TEST_P(AudioInputDeviceTest, CaptureCallbackSocketError) {
  base::test::TaskEnvironment ste;
  CreateInputDevice();

  uint32_t buffer_index = 0;
  browser_socket_.Send(base::byte_span_from_ref(buffer_index));

  EXPECT_CALL(*capture_callback_,
              OnCaptureError(AudioCapturerSource::ErrorCode::kSocketError, _))
      .WillOnce(base::test::RunClosure(ste.QuitClosure()));

  device_->Start();
  ste.RunUntilIdle();

  // The capture occurs on another thread, wait for it.
  capture_callback_->WaitForCapture();

  browser_socket_.Close();
  ste.RunUntilQuit();

  device_->Stop();
}

INSTANTIATE_TEST_SUITE_P(
    AudioInputDeviceGroup,
    AudioInputDeviceTest,
    ::testing::Values(AudioInputDevice::DeadStreamDetection::kDisabled,
                      AudioInputDevice::DeadStreamDetection::kEnabled));

}  // namespace media.