stefan@webrtc.org | e028410 | 2013-11-18 11:45:11 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 10 | #ifndef WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_ |
| 11 | #define WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_ |
| 12 | |
kwiberg | b670f85 | 2016-05-01 21:53:46 | [diff] [blame] | 13 | #include <memory> |
stefan@webrtc.org | e028410 | 2013-11-18 11:45:11 | [diff] [blame] | 14 | #include <string> |
perkj | 4c8e9ef | 2017-01-31 21:32:49 | [diff] [blame] | 15 | #include <vector> |
stefan@webrtc.org | e028410 | 2013-11-18 11:45:11 | [diff] [blame] | 16 | |
kwiberg | 23aa43d | 2017-09-04 12:43:17 | [diff] [blame] | 17 | #include "webrtc/api/array_view.h" |
stefan@webrtc.org | e028410 | 2013-11-18 11:45:11 | [diff] [blame] | 18 | #include "webrtc/modules/audio_device/include/fake_audio_device.h" |
Edward Lemur | 76de83e | 2017-07-06 17:44:34 | [diff] [blame] | 19 | #include "webrtc/rtc_base/buffer.h" |
| 20 | #include "webrtc/rtc_base/criticalsection.h" |
| 21 | #include "webrtc/rtc_base/event.h" |
| 22 | #include "webrtc/rtc_base/platform_thread.h" |
stefan@webrtc.org | e028410 | 2013-11-18 11:45:11 | [diff] [blame] | 23 | #include "webrtc/typedefs.h" |
| 24 | |
| 25 | namespace webrtc { |
| 26 | |
Peter Boström | a8c4f43 | 2015-04-08 09:24:19 | [diff] [blame] | 27 | class EventTimerWrapper; |
stefan@webrtc.org | e028410 | 2013-11-18 11:45:11 | [diff] [blame] | 28 | |
| 29 | namespace test { |
| 30 | |
perkj | 4c8e9ef | 2017-01-31 21:32:49 | [diff] [blame] | 31 | // FakeAudioDevice implements an AudioDevice module that can act both as a |
| 32 | // capturer and a renderer. It will use 10ms audio frames. |
stefan@webrtc.org | e028410 | 2013-11-18 11:45:11 | [diff] [blame] | 33 | class FakeAudioDevice : public FakeAudioDeviceModule { |
| 34 | public: |
oprypin | 35a36cd | 2017-03-14 16:01:47 | [diff] [blame] | 35 | // 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 | |
perkj | 4c8e9ef | 2017-01-31 21:32:49 | [diff] [blame] | 62 | // Creates a new FakeAudioDevice. When capturing or playing, 10 ms audio |
oprypin | 35a36cd | 2017-03-14 16:01:47 | [diff] [blame] | 63 | // 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); |
perkj | 4c8e9ef | 2017-01-31 21:32:49 | [diff] [blame] | 72 | ~FakeAudioDevice() override; |
stefan@webrtc.org | e028410 | 2013-11-18 11:45:11 | [diff] [blame] | 73 | |
oprypin | 35a36cd | 2017-03-14 16:01:47 | [diff] [blame] | 74 | // 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 | |
oprypin | 1b0415d | 2017-03-23 10:40:03 | [diff] [blame] | 92 | // 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 | |
oprypin | 35a36cd | 2017-03-14 16:01:47 | [diff] [blame] | 98 | // 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.org | 860ac53 | 2015-03-04 12:58:35 | [diff] [blame] | 102 | int32_t Init() override; |
| 103 | int32_t RegisterAudioCallback(AudioTransport* callback) override; |
stefan@webrtc.org | e028410 | 2013-11-18 11:45:11 | [diff] [blame] | 104 | |
perkj | 4c8e9ef | 2017-01-31 21:32:49 | [diff] [blame] | 105 | int32_t StartPlayout() override; |
| 106 | int32_t StopPlayout() override; |
| 107 | int32_t StartRecording() override; |
| 108 | int32_t StopRecording() override; |
| 109 | |
kjellander@webrtc.org | 860ac53 | 2015-03-04 12:58:35 | [diff] [blame] | 110 | bool Playing() const override; |
kjellander@webrtc.org | 860ac53 | 2015-03-04 12:58:35 | [diff] [blame] | 111 | bool Recording() const override; |
stefan@webrtc.org | e028410 | 2013-11-18 11:45:11 | [diff] [blame] | 112 | |
oprypin | 35a36cd | 2017-03-14 16:01:47 | [diff] [blame] | 113 | // 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.org | e028410 | 2013-11-18 11:45:11 | [diff] [blame] | 121 | static bool Run(void* obj); |
perkj | 4c8e9ef | 2017-01-31 21:32:49 | [diff] [blame] | 122 | void ProcessAudio(); |
stefan@webrtc.org | e028410 | 2013-11-18 11:45:11 | [diff] [blame] | 123 | |
oprypin | 35a36cd | 2017-03-14 16:01:47 | [diff] [blame] | 124 | const std::unique_ptr<Capturer> capturer_ GUARDED_BY(lock_); |
| 125 | const std::unique_ptr<Renderer> renderer_ GUARDED_BY(lock_); |
danilchap | a4777ed | 2016-02-10 18:54:47 | [diff] [blame] | 126 | const float speed_; |
stefan@webrtc.org | e028410 | 2013-11-18 11:45:11 | [diff] [blame] | 127 | |
pbos | 1e5b805 | 2016-01-25 11:52:44 | [diff] [blame] | 128 | rtc::CriticalSection lock_; |
perkj | 4c8e9ef | 2017-01-31 21:32:49 | [diff] [blame] | 129 | AudioTransport* audio_callback_ GUARDED_BY(lock_); |
| 130 | bool rendering_ GUARDED_BY(lock_); |
| 131 | bool capturing_ GUARDED_BY(lock_); |
oprypin | 35a36cd | 2017-03-14 16:01:47 | [diff] [blame] | 132 | rtc::Event done_rendering_; |
| 133 | rtc::Event done_capturing_; |
perkj | 4c8e9ef | 2017-01-31 21:32:49 | [diff] [blame] | 134 | |
| 135 | std::vector<int16_t> playout_buffer_ GUARDED_BY(lock_); |
oprypin | 35a36cd | 2017-03-14 16:01:47 | [diff] [blame] | 136 | rtc::BufferT<int16_t> recording_buffer_ GUARDED_BY(lock_); |
perkj | 4c8e9ef | 2017-01-31 21:32:49 | [diff] [blame] | 137 | |
| 138 | std::unique_ptr<EventTimerWrapper> tick_; |
Peter Boström | daf1aa4 | 2015-11-26 16:45:47 | [diff] [blame] | 139 | rtc::PlatformThread thread_; |
stefan@webrtc.org | e028410 | 2013-11-18 11:45:11 | [diff] [blame] | 140 | }; |
| 141 | } // namespace test |
| 142 | } // namespace webrtc |
| 143 | |
| 144 | #endif // WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_ |