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

Side by Side 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 #ifndef WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_ 10 #ifndef WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_
11 #define WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_ 11 #define WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_
12 12
13 #include <memory> 13 #include <memory>
14 #include <string> 14 #include <string>
15 #include <vector> 15 #include <vector>
16 16
17 #include "webrtc/base/array_view.h"
18 #include "webrtc/base/buffer.h"
17 #include "webrtc/base/criticalsection.h" 19 #include "webrtc/base/criticalsection.h"
20 #include "webrtc/base/event.h"
18 #include "webrtc/base/platform_thread.h" 21 #include "webrtc/base/platform_thread.h"
19 #include "webrtc/modules/audio_device/include/fake_audio_device.h" 22 #include "webrtc/modules/audio_device/include/fake_audio_device.h"
20 #include "webrtc/typedefs.h" 23 #include "webrtc/typedefs.h"
21 24
22 namespace webrtc { 25 namespace webrtc {
23 26
24 class EventTimerWrapper; 27 class EventTimerWrapper;
25 28
26 namespace test { 29 namespace test {
27 30
28 // FakeAudioDevice implements an AudioDevice module that can act both as a 31 // FakeAudioDevice implements an AudioDevice module that can act both as a
29 // capturer and a renderer. It will use 10ms audio frames. 32 // capturer and a renderer. It will use 10ms audio frames.
30 class FakeAudioDevice : public FakeAudioDeviceModule { 33 class FakeAudioDevice : public FakeAudioDeviceModule {
31 public: 34 public:
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);
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.
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 // Captures some audio data and puts it into the passed buffer. The final
46 // size of the buffer should not exceed its initial size, which is equal to
47 // 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.
48 // or false when the capture finishes.
49 virtual bool Capture(rtc::BufferT<int16_t>* buffer) = 0;
50 };
51
52 class Renderer {
53 public:
54 virtual ~Renderer() {}
55 // Returns the sampling frequency in Hz of the audio data that this
56 // renderer receives.
57 virtual int SamplingFrequency() const = 0;
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
32 // Creates a new FakeAudioDevice. When capturing or playing, 10 ms audio 63 // Creates a new FakeAudioDevice. When capturing or playing, 10 ms audio
33 // frames will be processed every 100ms / |speed|. 64 // frames will be processed every 10ms / |speed|.
34 // |sampling_frequency_in_hz| can be 8, 16, 32, 44.1 or 48kHz. 65 // |capturer| is an object that produces audio data. Can be nullptr if this
35 // When recording is started, it will generates a signal where every second 66 // device is never used for recording.
67 // |renderer| is an object that receives audio data that would have been
68 // played out. Can be nullptr if this device is never used for playing.
69 // Use one of the Create... functions to get these instances.
70 FakeAudioDevice(std::unique_ptr<Capturer> capturer,
71 std::unique_ptr<Renderer> renderer,
72 float speed = 1);
73 ~FakeAudioDevice() override;
74
75 // Returns a Capturer instance that generates a signal where every second
36 // frame is zero and every second frame is evenly distributed random noise 76 // frame is zero and every second frame is evenly distributed random noise
37 // with max amplitude |max_amplitude|. 77 // with max amplitude |max_amplitude|.
38 FakeAudioDevice(float speed, 78 static std::unique_ptr<Capturer> CreatePulsedNoiseCapturer(
39 int sampling_frequency_in_hz, 79 int16_t max_amplitude, int sampling_frequency_in_hz);
40 int16_t max_amplitude);
41 ~FakeAudioDevice() override;
42 80
43 private: 81 // Returns a Capturer instance that gets its data from a file.
82 static std::unique_ptr<Capturer> CreateWavFileReader(
83 std::string filename, int sampling_frequency_in_hz);
84
85 // Returns a Capturer instance that gets its data from a file.
86 // Automatically detects sample rate.
87 static std::unique_ptr<Capturer> CreateWavFileReader(std::string filename);
88
89 // Returns a Renderer instance that writes its data to a file.
90 static std::unique_ptr<Renderer> CreateWavFileWriter(
91 std::string filename, int sampling_frequency_in_hz);
92
93 // Returns a Renderer instance that does nothing with the audio data.
94 static std::unique_ptr<Renderer> CreateDiscardRenderer(
95 int sampling_frequency_in_hz);
96
44 int32_t Init() override; 97 int32_t Init() override;
45 int32_t RegisterAudioCallback(AudioTransport* callback) override; 98 int32_t RegisterAudioCallback(AudioTransport* callback) override;
46 99
47 int32_t StartPlayout() override; 100 int32_t StartPlayout() override;
48 int32_t StopPlayout() override; 101 int32_t StopPlayout() override;
49 int32_t StartRecording() override; 102 int32_t StartRecording() override;
50 int32_t StopRecording() override; 103 int32_t StopRecording() override;
51 104
52 bool Playing() const override; 105 bool Playing() const override;
53 bool Recording() const override; 106 bool Recording() const override;
54 107
108 // Blocks until the Renderer refuses to receive data.
109 // Returns false if |timeout_ms| passes before that happens.
110 bool WaitForPlayoutEnd(int timeout_ms = rtc::Event::kForever);
111 // Blocks until the Recorder stops producing data.
112 // Returns false if |timeout_ms| passes before that happens.
113 bool WaitForRecordingEnd(int timeout_ms = rtc::Event::kForever);
114
115 private:
55 static bool Run(void* obj); 116 static bool Run(void* obj);
56 void ProcessAudio(); 117 void ProcessAudio();
57 118
58 const int sampling_frequency_in_hz_; 119 const std::unique_ptr<Capturer> capturer_ GUARDED_BY(lock_);
59 const size_t num_samples_per_frame_; 120 const std::unique_ptr<Renderer> renderer_ GUARDED_BY(lock_);
60 const float speed_; 121 const float speed_;
61 122
62 rtc::CriticalSection lock_; 123 rtc::CriticalSection lock_;
63 AudioTransport* audio_callback_ GUARDED_BY(lock_); 124 AudioTransport* audio_callback_ GUARDED_BY(lock_);
64 bool rendering_ GUARDED_BY(lock_); 125 bool rendering_ GUARDED_BY(lock_);
65 bool capturing_ GUARDED_BY(lock_); 126 bool capturing_ GUARDED_BY(lock_);
66 127 rtc::Event done_rendering_;
67 class PulsedNoiseCapturer; 128 rtc::Event done_capturing_;
68 const std::unique_ptr<PulsedNoiseCapturer> capturer_ GUARDED_BY(lock_);
69 129
70 std::vector<int16_t> playout_buffer_ GUARDED_BY(lock_); 130 std::vector<int16_t> playout_buffer_ GUARDED_BY(lock_);
131 rtc::BufferT<int16_t> recording_buffer_ GUARDED_BY(lock_);
71 132
72 std::unique_ptr<EventTimerWrapper> tick_; 133 std::unique_ptr<EventTimerWrapper> tick_;
73 rtc::PlatformThread thread_; 134 rtc::PlatformThread thread_;
74 }; 135 };
75 } // namespace test 136 } // namespace test
76 } // namespace webrtc 137 } // namespace webrtc
77 138
78 #endif // WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_ 139 #endif // WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698