Artem Titov | 0f03973 | 2018-03-07 12:20:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | */ |
| 10 | #ifndef MODULES_AUDIO_DEVICE_INCLUDE_TEST_AUDIO_DEVICE_H_ |
| 11 | #define MODULES_AUDIO_DEVICE_INCLUDE_TEST_AUDIO_DEVICE_H_ |
| 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 13 | #include <stddef.h> |
| 14 | #include <stdint.h> |
Artem Titov | 0f03973 | 2018-03-07 12:20:51 +0100 | [diff] [blame] | 15 | #include <memory> |
| 16 | #include <string> |
Artem Titov | 0f03973 | 2018-03-07 12:20:51 +0100 | [diff] [blame] | 17 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 18 | #include "api/array_view.h" |
Artem Titov | 0f03973 | 2018-03-07 12:20:51 +0100 | [diff] [blame] | 19 | #include "modules/audio_device/include/audio_device.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 20 | #include "modules/audio_device/include/audio_device_defines.h" |
Artem Titov | 0f03973 | 2018-03-07 12:20:51 +0100 | [diff] [blame] | 21 | #include "rtc_base/buffer.h" |
| 22 | #include "rtc_base/event.h" |
Artem Titov | 8458cff | 2018-03-22 16:50:06 +0100 | [diff] [blame] | 23 | #include "rtc_base/platform_file.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 24 | #include "rtc_base/scoped_ref_ptr.h" |
Artem Titov | 0f03973 | 2018-03-07 12:20:51 +0100 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
| 28 | // TestAudioDeviceModule implements an AudioDevice module that can act both as a |
| 29 | // capturer and a renderer. It will use 10ms audio frames. |
| 30 | class TestAudioDeviceModule : public AudioDeviceModule { |
| 31 | public: |
| 32 | // Returns the number of samples that Capturers and Renderers with this |
| 33 | // sampling frequency will work with every time Capture or Render is called. |
| 34 | static size_t SamplesPerFrame(int sampling_frequency_in_hz); |
| 35 | |
| 36 | class Capturer { |
| 37 | public: |
| 38 | virtual ~Capturer() {} |
| 39 | // Returns the sampling frequency in Hz of the audio data that this |
| 40 | // capturer produces. |
| 41 | virtual int SamplingFrequency() const = 0; |
Pengyu Liao | ba907f0 | 2018-03-09 09:25:46 -0800 | [diff] [blame] | 42 | // Returns the number of channels of captured audio data. |
| 43 | virtual int NumChannels() const = 0; |
Artem Titov | 0f03973 | 2018-03-07 12:20:51 +0100 | [diff] [blame] | 44 | // Replaces the contents of |buffer| with 10ms of captured audio data |
| 45 | // (see TestAudioDeviceModule::SamplesPerFrame). Returns true if the |
| 46 | // capturer can keep producing data, or false when the capture finishes. |
| 47 | virtual bool Capture(rtc::BufferT<int16_t>* buffer) = 0; |
| 48 | }; |
| 49 | |
| 50 | class Renderer { |
| 51 | public: |
| 52 | virtual ~Renderer() {} |
| 53 | // Returns the sampling frequency in Hz of the audio data that this |
| 54 | // renderer receives. |
| 55 | virtual int SamplingFrequency() const = 0; |
Pengyu Liao | ba907f0 | 2018-03-09 09:25:46 -0800 | [diff] [blame] | 56 | // Returns the number of channels of audio data to be required. |
| 57 | virtual int NumChannels() const = 0; |
Artem Titov | 0f03973 | 2018-03-07 12:20:51 +0100 | [diff] [blame] | 58 | // Renders the passed audio data and returns true if the renderer wants |
| 59 | // to keep receiving data, or false otherwise. |
| 60 | virtual bool Render(rtc::ArrayView<const int16_t> data) = 0; |
| 61 | }; |
| 62 | |
| 63 | // A fake capturer that generates pulses with random samples between |
| 64 | // -max_amplitude and +max_amplitude. |
| 65 | class PulsedNoiseCapturer : public Capturer { |
| 66 | public: |
| 67 | virtual ~PulsedNoiseCapturer() {} |
| 68 | |
| 69 | virtual void SetMaxAmplitude(int16_t amplitude) = 0; |
| 70 | }; |
| 71 | |
| 72 | virtual ~TestAudioDeviceModule() {} |
| 73 | |
| 74 | // Creates a new TestAudioDeviceModule. When capturing or playing, 10 ms audio |
| 75 | // frames will be processed every 10ms / |speed|. |
| 76 | // |capturer| is an object that produces audio data. Can be nullptr if this |
| 77 | // device is never used for recording. |
| 78 | // |renderer| is an object that receives audio data that would have been |
| 79 | // played out. Can be nullptr if this device is never used for playing. |
| 80 | // Use one of the Create... functions to get these instances. |
| 81 | static rtc::scoped_refptr<TestAudioDeviceModule> CreateTestAudioDeviceModule( |
| 82 | std::unique_ptr<Capturer> capturer, |
| 83 | std::unique_ptr<Renderer> renderer, |
| 84 | float speed = 1); |
| 85 | |
Pengyu Liao | ba907f0 | 2018-03-09 09:25:46 -0800 | [diff] [blame] | 86 | // Returns a Capturer instance that generates a signal of |num_channels| |
| 87 | // channels where every second frame is zero and every second frame is evenly |
| 88 | // distributed random noise with max amplitude |max_amplitude|. |
| 89 | static std::unique_ptr<PulsedNoiseCapturer> CreatePulsedNoiseCapturer( |
| 90 | int16_t max_amplitude, |
| 91 | int sampling_frequency_in_hz, |
Artem Titov | 8458cff | 2018-03-22 16:50:06 +0100 | [diff] [blame] | 92 | int num_channels = 1); |
Pengyu Liao | ba907f0 | 2018-03-09 09:25:46 -0800 | [diff] [blame] | 93 | |
Artem Titov | 8458cff | 2018-03-22 16:50:06 +0100 | [diff] [blame] | 94 | // Returns a Renderer instance that does nothing with the audio data. |
| 95 | static std::unique_ptr<Renderer> CreateDiscardRenderer( |
| 96 | int sampling_frequency_in_hz, |
| 97 | int num_channels = 1); |
| 98 | |
| 99 | // WavReader and WavWriter creation based on file name. |
Artem Titov | 0f03973 | 2018-03-07 12:20:51 +0100 | [diff] [blame] | 100 | |
Pengyu Liao | ba907f0 | 2018-03-09 09:25:46 -0800 | [diff] [blame] | 101 | // Returns a Capturer instance that gets its data from a file. The sample rate |
Artem Titov | 8458cff | 2018-03-22 16:50:06 +0100 | [diff] [blame] | 102 | // and channels will be checked against the Wav file. |
Pengyu Liao | ba907f0 | 2018-03-09 09:25:46 -0800 | [diff] [blame] | 103 | static std::unique_ptr<Capturer> CreateWavFileReader( |
| 104 | std::string filename, |
| 105 | int sampling_frequency_in_hz, |
Artem Titov | 8458cff | 2018-03-22 16:50:06 +0100 | [diff] [blame] | 106 | int num_channels = 1); |
Artem Titov | 0f03973 | 2018-03-07 12:20:51 +0100 | [diff] [blame] | 107 | |
| 108 | // Returns a Capturer instance that gets its data from a file. |
Artem Titov | 8458cff | 2018-03-22 16:50:06 +0100 | [diff] [blame] | 109 | // Automatically detects sample rate and num of channels. |
Artem Titov | 0f03973 | 2018-03-07 12:20:51 +0100 | [diff] [blame] | 110 | static std::unique_ptr<Capturer> CreateWavFileReader(std::string filename); |
| 111 | |
| 112 | // Returns a Renderer instance that writes its data to a file. |
| 113 | static std::unique_ptr<Renderer> CreateWavFileWriter( |
| 114 | std::string filename, |
Pengyu Liao | ba907f0 | 2018-03-09 09:25:46 -0800 | [diff] [blame] | 115 | int sampling_frequency_in_hz, |
Artem Titov | 8458cff | 2018-03-22 16:50:06 +0100 | [diff] [blame] | 116 | int num_channels = 1); |
Artem Titov | 0f03973 | 2018-03-07 12:20:51 +0100 | [diff] [blame] | 117 | |
| 118 | // Returns a Renderer instance that writes its data to a WAV file, cutting |
| 119 | // off silence at the beginning (not necessarily perfect silence, see |
| 120 | // kAmplitudeThreshold) and at the end (only actual 0 samples in this case). |
| 121 | static std::unique_ptr<Renderer> CreateBoundedWavFileWriter( |
| 122 | std::string filename, |
Pengyu Liao | ba907f0 | 2018-03-09 09:25:46 -0800 | [diff] [blame] | 123 | int sampling_frequency_in_hz, |
Artem Titov | 8458cff | 2018-03-22 16:50:06 +0100 | [diff] [blame] | 124 | int num_channels = 1); |
Pengyu Liao | ba907f0 | 2018-03-09 09:25:46 -0800 | [diff] [blame] | 125 | |
Artem Titov | 8458cff | 2018-03-22 16:50:06 +0100 | [diff] [blame] | 126 | // WavReader and WavWriter creation based on rtc::PlatformFile. |
Artem Titov | 0f03973 | 2018-03-07 12:20:51 +0100 | [diff] [blame] | 127 | |
Artem Titov | 8458cff | 2018-03-22 16:50:06 +0100 | [diff] [blame] | 128 | // Returns a Capturer instance that gets its data from a file. The sample rate |
| 129 | // and channels will be checked against the Wav file. |
| 130 | static std::unique_ptr<Capturer> CreateWavFileReader( |
| 131 | rtc::PlatformFile file, |
Pengyu Liao | ba907f0 | 2018-03-09 09:25:46 -0800 | [diff] [blame] | 132 | int sampling_frequency_in_hz, |
Artem Titov | 8458cff | 2018-03-22 16:50:06 +0100 | [diff] [blame] | 133 | int num_channels = 1); |
Pengyu Liao | ba907f0 | 2018-03-09 09:25:46 -0800 | [diff] [blame] | 134 | |
Artem Titov | 8458cff | 2018-03-22 16:50:06 +0100 | [diff] [blame] | 135 | // Returns a Capturer instance that gets its data from a file. |
| 136 | // Automatically detects sample rate and num of channels. |
| 137 | static std::unique_ptr<Capturer> CreateWavFileReader(rtc::PlatformFile file); |
| 138 | |
| 139 | // Returns a Renderer instance that writes its data to a file. |
| 140 | static std::unique_ptr<Renderer> CreateWavFileWriter( |
| 141 | rtc::PlatformFile file, |
| 142 | int sampling_frequency_in_hz, |
| 143 | int num_channels = 1); |
| 144 | |
| 145 | // Returns a Renderer instance that writes its data to a WAV file, cutting |
| 146 | // off silence at the beginning (not necessarily perfect silence, see |
| 147 | // kAmplitudeThreshold) and at the end (only actual 0 samples in this case). |
| 148 | static std::unique_ptr<Renderer> CreateBoundedWavFileWriter( |
| 149 | rtc::PlatformFile file, |
| 150 | int sampling_frequency_in_hz, |
| 151 | int num_channels = 1); |
Artem Titov | 0f03973 | 2018-03-07 12:20:51 +0100 | [diff] [blame] | 152 | |
| 153 | virtual int32_t Init() = 0; |
| 154 | virtual int32_t RegisterAudioCallback(AudioTransport* callback) = 0; |
| 155 | |
| 156 | virtual int32_t StartPlayout() = 0; |
| 157 | virtual int32_t StopPlayout() = 0; |
| 158 | virtual int32_t StartRecording() = 0; |
| 159 | virtual int32_t StopRecording() = 0; |
| 160 | |
| 161 | virtual bool Playing() const = 0; |
| 162 | virtual bool Recording() const = 0; |
| 163 | |
| 164 | // Blocks until the Renderer refuses to receive data. |
| 165 | // Returns false if |timeout_ms| passes before that happens. |
| 166 | virtual bool WaitForPlayoutEnd(int timeout_ms = rtc::Event::kForever) = 0; |
| 167 | // Blocks until the Recorder stops producing data. |
| 168 | // Returns false if |timeout_ms| passes before that happens. |
| 169 | virtual bool WaitForRecordingEnd(int timeout_ms = rtc::Event::kForever) = 0; |
| 170 | }; |
| 171 | |
| 172 | } // namespace webrtc |
| 173 | |
| 174 | #endif // MODULES_AUDIO_DEVICE_INCLUDE_TEST_AUDIO_DEVICE_H_ |