blob: f71fd016d9ad3def0c159e997478a1024d63ea86 [file] [log] [blame]
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +00001/*
2 * Copyright (c) 2013 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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#ifndef TEST_FAKE_AUDIO_DEVICE_H_
11#define TEST_FAKE_AUDIO_DEVICE_H_
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +000012
kwibergbfefb032016-05-01 14:53:46 -070013#include <memory>
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +000014#include <string>
perkjac61b742017-01-31 13:32:49 -080015#include <vector>
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/array_view.h"
18#include "modules/audio_device/include/fake_audio_device.h"
19#include "rtc_base/buffer.h"
20#include "rtc_base/criticalsection.h"
21#include "rtc_base/event.h"
22#include "rtc_base/platform_thread.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020023#include "typedefs.h" // NOLINT(build/include)
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +000024
25namespace webrtc {
26
Peter Boström64c03662015-04-08 11:24:19 +020027class EventTimerWrapper;
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +000028
29namespace test {
30
perkjac61b742017-01-31 13:32:49 -080031// FakeAudioDevice implements an AudioDevice module that can act both as a
32// capturer and a renderer. It will use 10ms audio frames.
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +000033class FakeAudioDevice : public FakeAudioDeviceModule {
34 public:
oprypina5145842017-03-14 09:01:47 -070035 // Returns the number of samples that Capturers and Renderers with this
36 // sampling frequency will work with every time Capture or Render is called.
37 static size_t SamplesPerFrame(int sampling_frequency_in_hz);
38
39 class Capturer {
40 public:
41 virtual ~Capturer() {}
42 // Returns the sampling frequency in Hz of the audio data that this
43 // capturer produces.
44 virtual int SamplingFrequency() const = 0;
45 // Replaces the contents of |buffer| with 10ms of captured audio data
46 // (see FakeAudioDevice::SamplesPerFrame). Returns true if the capturer can
47 // keep producing data, or false when the capture finishes.
48 virtual bool Capture(rtc::BufferT<int16_t>* buffer) = 0;
49 };
50
51 class Renderer {
52 public:
53 virtual ~Renderer() {}
54 // Returns the sampling frequency in Hz of the audio data that this
55 // renderer receives.
56 virtual int SamplingFrequency() const = 0;
57 // Renders the passed audio data and returns true if the renderer wants
58 // to keep receiving data, or false otherwise.
59 virtual bool Render(rtc::ArrayView<const int16_t> data) = 0;
60 };
61
perkjac61b742017-01-31 13:32:49 -080062 // Creates a new FakeAudioDevice. When capturing or playing, 10 ms audio
oprypina5145842017-03-14 09:01:47 -070063 // frames will be processed every 10ms / |speed|.
64 // |capturer| is an object that produces audio data. Can be nullptr if this
65 // device is never used for recording.
66 // |renderer| is an object that receives audio data that would have been
67 // played out. Can be nullptr if this device is never used for playing.
68 // Use one of the Create... functions to get these instances.
69 FakeAudioDevice(std::unique_ptr<Capturer> capturer,
70 std::unique_ptr<Renderer> renderer,
71 float speed = 1);
perkjac61b742017-01-31 13:32:49 -080072 ~FakeAudioDevice() override;
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +000073
oprypina5145842017-03-14 09:01:47 -070074 // Returns a Capturer instance that generates a signal where every second
75 // frame is zero and every second frame is evenly distributed random noise
76 // with max amplitude |max_amplitude|.
77 static std::unique_ptr<Capturer> CreatePulsedNoiseCapturer(
78 int16_t max_amplitude, int sampling_frequency_in_hz);
79
80 // Returns a Capturer instance that gets its data from a file.
81 static std::unique_ptr<Capturer> CreateWavFileReader(
82 std::string filename, int sampling_frequency_in_hz);
83
84 // Returns a Capturer instance that gets its data from a file.
85 // Automatically detects sample rate.
86 static std::unique_ptr<Capturer> CreateWavFileReader(std::string filename);
87
88 // Returns a Renderer instance that writes its data to a file.
89 static std::unique_ptr<Renderer> CreateWavFileWriter(
90 std::string filename, int sampling_frequency_in_hz);
91
oprypin92220ff2017-03-23 03:40:03 -070092 // Returns a Renderer instance that writes its data to a WAV file, cutting
93 // off silence at the beginning (not necessarily perfect silence, see
94 // kAmplitudeThreshold) and at the end (only actual 0 samples in this case).
95 static std::unique_ptr<Renderer> CreateBoundedWavFileWriter(
96 std::string filename, int sampling_frequency_in_hz);
97
oprypina5145842017-03-14 09:01:47 -070098 // Returns a Renderer instance that does nothing with the audio data.
99 static std::unique_ptr<Renderer> CreateDiscardRenderer(
100 int sampling_frequency_in_hz);
101
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000102 int32_t Init() override;
103 int32_t RegisterAudioCallback(AudioTransport* callback) override;
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +0000104
perkjac61b742017-01-31 13:32:49 -0800105 int32_t StartPlayout() override;
106 int32_t StopPlayout() override;
107 int32_t StartRecording() override;
108 int32_t StopRecording() override;
109
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000110 bool Playing() const override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000111 bool Recording() const override;
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +0000112
oprypina5145842017-03-14 09:01:47 -0700113 // Blocks until the Renderer refuses to receive data.
114 // Returns false if |timeout_ms| passes before that happens.
115 bool WaitForPlayoutEnd(int timeout_ms = rtc::Event::kForever);
116 // Blocks until the Recorder stops producing data.
117 // Returns false if |timeout_ms| passes before that happens.
118 bool WaitForRecordingEnd(int timeout_ms = rtc::Event::kForever);
119
120 private:
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +0000121 static bool Run(void* obj);
perkjac61b742017-01-31 13:32:49 -0800122 void ProcessAudio();
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +0000123
danilchapa37de392017-09-09 04:17:22 -0700124 const std::unique_ptr<Capturer> capturer_ RTC_GUARDED_BY(lock_);
125 const std::unique_ptr<Renderer> renderer_ RTC_GUARDED_BY(lock_);
danilchap9c6a0c72016-02-10 10:54:47 -0800126 const float speed_;
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +0000127
pbos5ad935c2016-01-25 03:52:44 -0800128 rtc::CriticalSection lock_;
danilchapa37de392017-09-09 04:17:22 -0700129 AudioTransport* audio_callback_ RTC_GUARDED_BY(lock_);
130 bool rendering_ RTC_GUARDED_BY(lock_);
131 bool capturing_ RTC_GUARDED_BY(lock_);
oprypina5145842017-03-14 09:01:47 -0700132 rtc::Event done_rendering_;
133 rtc::Event done_capturing_;
perkjac61b742017-01-31 13:32:49 -0800134
danilchapa37de392017-09-09 04:17:22 -0700135 std::vector<int16_t> playout_buffer_ RTC_GUARDED_BY(lock_);
136 rtc::BufferT<int16_t> recording_buffer_ RTC_GUARDED_BY(lock_);
perkjac61b742017-01-31 13:32:49 -0800137
138 std::unique_ptr<EventTimerWrapper> tick_;
Peter Boström8c38e8b2015-11-26 17:45:47 +0100139 rtc::PlatformThread thread_;
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +0000140};
141} // namespace test
142} // namespace webrtc
143
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200144#endif // TEST_FAKE_AUDIO_DEVICE_H_