Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Unified Diff: webrtc/test/fake_audio_device.h

Issue 2717623003: Add the ability to read/write to WAV files in FakeAudioDevice (Closed)
Patch Set: Address review feedback Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: webrtc/test/fake_audio_device.h
diff --git a/webrtc/test/fake_audio_device.h b/webrtc/test/fake_audio_device.h
index 4daeab43650066c258d0bb172a12d14f36fe64c3..386f8390f98fc0eee7f81d1faccf9f4eb433bed8 100644
--- a/webrtc/test/fake_audio_device.h
+++ b/webrtc/test/fake_audio_device.h
@@ -14,7 +14,10 @@
#include <string>
#include <vector>
+#include "webrtc/base/array_view.h"
+#include "webrtc/base/buffer.h"
#include "webrtc/base/criticalsection.h"
+#include "webrtc/base/event.h"
#include "webrtc/base/platform_thread.h"
#include "webrtc/modules/audio_device/include/fake_audio_device.h"
#include "webrtc/typedefs.h"
@@ -29,18 +32,68 @@ namespace test {
// capturer and a renderer. It will use 10ms audio frames.
class FakeAudioDevice : public FakeAudioDeviceModule {
public:
+ // Returns the number of samples that Capturers and Renderers with this
+ // sampling frequency will work with every time Capture or Render is called.
+ static size_t SamplesPerFrame(int sampling_frequency_in_hz);
kwiberg-webrtc 2017/03/13 14:22:28 Can this function be in the anonymous namespace in
oprypin_webrtc 2017/03/13 15:11:36 I intend to implement more specialized Capturers/R
kwiberg-webrtc 2017/03/14 10:02:03 Acknowledged.
+
+ class Capturer {
+ public:
+ virtual ~Capturer() {}
+ // Returns the sampling frequency in Hz of the audio data that this
+ // capturer produces.
+ virtual int SamplingFrequency() const = 0;
+ // Captures some audio data and puts it into the passed buffer. The final
+ // size of the buffer should not exceed its initial size, which is equal to
+ // SamplesPerFrame. Returns true if the capturer can keep producing data,
kwiberg-webrtc 2017/03/13 14:22:29 Hmmm... why the behavior described in the second s
oprypin_webrtc 2017/03/13 15:11:36 It's just not obvious what 10ms of data is, I thou
kwiberg-webrtc 2017/03/14 10:02:03 Thanks, looks good now.
+ // or false when the capture finishes.
+ virtual bool Capture(rtc::BufferT<int16_t>* buffer) = 0;
+ };
+
+ class Renderer {
+ public:
+ virtual ~Renderer() {}
+ // Returns the sampling frequency in Hz of the audio data that this
+ // renderer receives.
+ virtual int SamplingFrequency() const = 0;
+ // Renders the passed audio data and returns true if the renderer wants
+ // to keep receiving data, or false otherwise.
+ virtual bool Render(rtc::ArrayView<const int16_t> data) = 0;
+ };
+
// Creates a new FakeAudioDevice. When capturing or playing, 10 ms audio
- // frames will be processed every 100ms / |speed|.
- // |sampling_frequency_in_hz| can be 8, 16, 32, 44.1 or 48kHz.
- // When recording is started, it will generates a signal where every second
+ // frames will be processed every 10ms / |speed|.
+ // |capturer| is an object that produces audio data. Can be nullptr if this
+ // device is never used for recording.
+ // |renderer| is an object that receives audio data that would have been
+ // played out. Can be nullptr if this device is never used for playing.
+ // Use one of the Create... functions to get these instances.
+ FakeAudioDevice(std::unique_ptr<Capturer> capturer,
+ std::unique_ptr<Renderer> renderer,
+ float speed = 1);
+ ~FakeAudioDevice() override;
+
+ // Returns a Capturer instance that generates a signal where every second
// frame is zero and every second frame is evenly distributed random noise
// with max amplitude |max_amplitude|.
- FakeAudioDevice(float speed,
- int sampling_frequency_in_hz,
- int16_t max_amplitude);
- ~FakeAudioDevice() override;
+ static std::unique_ptr<Capturer> CreatePulsedNoiseCapturer(
+ int16_t max_amplitude, int sampling_frequency_in_hz);
+
+ // Returns a Capturer instance that gets its data from a file.
+ static std::unique_ptr<Capturer> CreateWavFileReader(
+ std::string filename, int sampling_frequency_in_hz);
+
+ // Returns a Capturer instance that gets its data from a file.
+ // Automatically detects sample rate.
+ static std::unique_ptr<Capturer> CreateWavFileReader(std::string filename);
+
+ // Returns a Renderer instance that writes its data to a file.
+ static std::unique_ptr<Renderer> CreateWavFileWriter(
+ std::string filename, int sampling_frequency_in_hz);
+
+ // Returns a Renderer instance that does nothing with the audio data.
+ static std::unique_ptr<Renderer> CreateDiscardRenderer(
+ int sampling_frequency_in_hz);
- private:
int32_t Init() override;
int32_t RegisterAudioCallback(AudioTransport* callback) override;
@@ -52,22 +105,30 @@ class FakeAudioDevice : public FakeAudioDeviceModule {
bool Playing() const override;
bool Recording() const override;
+ // Blocks until the Renderer refuses to receive data.
+ // Returns false if |timeout_ms| passes before that happens.
+ bool WaitForPlayoutEnd(int timeout_ms = rtc::Event::kForever);
+ // Blocks until the Recorder stops producing data.
+ // Returns false if |timeout_ms| passes before that happens.
+ bool WaitForRecordingEnd(int timeout_ms = rtc::Event::kForever);
+
+ private:
static bool Run(void* obj);
void ProcessAudio();
- const int sampling_frequency_in_hz_;
- const size_t num_samples_per_frame_;
+ const std::unique_ptr<Capturer> capturer_ GUARDED_BY(lock_);
+ const std::unique_ptr<Renderer> renderer_ GUARDED_BY(lock_);
const float speed_;
rtc::CriticalSection lock_;
AudioTransport* audio_callback_ GUARDED_BY(lock_);
bool rendering_ GUARDED_BY(lock_);
bool capturing_ GUARDED_BY(lock_);
-
- class PulsedNoiseCapturer;
- const std::unique_ptr<PulsedNoiseCapturer> capturer_ GUARDED_BY(lock_);
+ rtc::Event done_rendering_;
+ rtc::Event done_capturing_;
std::vector<int16_t> playout_buffer_ GUARDED_BY(lock_);
+ rtc::BufferT<int16_t> recording_buffer_ GUARDED_BY(lock_);
std::unique_ptr<EventTimerWrapper> tick_;
rtc::PlatformThread thread_;

Powered by Google App Engine
This is Rietveld 408576698