summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/test/pc/e2e/echo/echo_emulation.h
blob: 359a481e46881d4abab103765213341b301c295f (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
/*
 *  Copyright (c) 2019 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.
 */

#ifndef TEST_PC_E2E_ECHO_ECHO_EMULATION_H_
#define TEST_PC_E2E_ECHO_ECHO_EMULATION_H_

#include <atomic>
#include <deque>
#include <memory>
#include <vector>

#include "api/test/pclf/media_configuration.h"
#include "modules/audio_device/include/test_audio_device.h"
#include "rtc_base/swap_queue.h"

namespace webrtc {
namespace webrtc_pc_e2e {

// Reduces audio input strength from provided capturer twice and adds input
// provided into EchoEmulatingCapturer::OnAudioRendered(...).
class EchoEmulatingCapturer : public TestAudioDeviceModule::Capturer {
 public:
  EchoEmulatingCapturer(
      std::unique_ptr<TestAudioDeviceModule::Capturer> capturer,
      EchoEmulationConfig config);

  void OnAudioRendered(rtc::ArrayView<const int16_t> data);

  int SamplingFrequency() const override {
    return delegate_->SamplingFrequency();
  }
  int NumChannels() const override { return delegate_->NumChannels(); }
  bool Capture(rtc::BufferT<int16_t>* buffer) override;

 private:
  std::unique_ptr<TestAudioDeviceModule::Capturer> delegate_;
  const EchoEmulationConfig config_;

  SwapQueue<std::vector<int16_t>> renderer_queue_;

  SequenceChecker renderer_thread_;
  std::vector<int16_t> queue_input_ RTC_GUARDED_BY(renderer_thread_);
  bool recording_started_ RTC_GUARDED_BY(renderer_thread_) = false;

  SequenceChecker capturer_thread_;
  std::vector<int16_t> queue_output_ RTC_GUARDED_BY(capturer_thread_);
  bool delay_accumulated_ RTC_GUARDED_BY(capturer_thread_) = false;
};

// Renders output into provided renderer and also copy output into provided
// EchoEmulationCapturer.
class EchoEmulatingRenderer : public TestAudioDeviceModule::Renderer {
 public:
  EchoEmulatingRenderer(
      std::unique_ptr<TestAudioDeviceModule::Renderer> renderer,
      EchoEmulatingCapturer* echo_emulating_capturer);

  int SamplingFrequency() const override {
    return delegate_->SamplingFrequency();
  }
  int NumChannels() const override { return delegate_->NumChannels(); }
  bool Render(rtc::ArrayView<const int16_t> data) override;

 private:
  std::unique_ptr<TestAudioDeviceModule::Renderer> delegate_;
  EchoEmulatingCapturer* echo_emulating_capturer_;
};

}  // namespace webrtc_pc_e2e
}  // namespace webrtc

#endif  // TEST_PC_E2E_ECHO_ECHO_EMULATION_H_