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

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: Remove obsolete files from build 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"
17 #include "webrtc/base/criticalsection.h" 18 #include "webrtc/base/criticalsection.h"
19 #include "webrtc/base/event.h"
18 #include "webrtc/base/platform_thread.h" 20 #include "webrtc/base/platform_thread.h"
19 #include "webrtc/modules/audio_device/include/fake_audio_device.h" 21 #include "webrtc/modules/audio_device/include/fake_audio_device.h"
20 #include "webrtc/typedefs.h" 22 #include "webrtc/typedefs.h"
21 23
22 namespace webrtc { 24 namespace webrtc {
23 25
24 class EventTimerWrapper; 26 class EventTimerWrapper;
25 27
26 namespace test { 28 namespace test {
27 29
28 // FakeAudioDevice implements an AudioDevice module that can act both as a 30 // FakeAudioDevice implements an AudioDevice module that can act both as a
29 // capturer and a renderer. It will use 10ms audio frames. 31 // capturer and a renderer. It will use 10ms audio frames.
30 class FakeAudioDevice : public FakeAudioDeviceModule { 32 class FakeAudioDevice : public FakeAudioDeviceModule {
33 private:
34 class Streamer {
35 public:
36 explicit Streamer(int sampling_frequency_in_hz);
37 int SamplingFrequency() const {
38 return sampling_frequency_in_hz_;
39 }
40 size_t SamplesPerFrame() const {
41 return num_samples_per_frame_;
42 }
43
44 private:
45 const int sampling_frequency_in_hz_;
46 const size_t num_samples_per_frame_;
47 };
48
49 class PulsedNoiseCapturer;
50 class WavFileReader;
51
52 class Discarder;
53 class WavFileWriter;
kwiberg-webrtc 2017/03/09 10:04:11 Why do you need to mention these four in the .h fi
oprypin_webrtc 2017/03/10 10:44:27 Done. Together with the change to anonymous namesp
54
31 public: 55 public:
56 class Capturer : public Streamer {
57 public:
58 using Streamer::Streamer;
59 // Should capture and return some data (limited to SamplesPerFrame), or
60 // return an empty ArrayView when the capture finishes.
61 virtual rtc::ArrayView<const int16_t> Capture() = 0;
kwiberg-webrtc 2017/03/09 10:04:11 It's not usually a good idea to return an ArrayVie
oprypin_webrtc 2017/03/10 10:44:27 Done.
62 virtual ~Capturer() {}
kwiberg-webrtc 2017/03/09 10:04:11 Put the destructor before the regular methods (jus
oprypin_webrtc 2017/03/10 10:44:27 Done.
63 };
64
65 class Renderer : public Streamer {
66 public:
67 using Streamer::Streamer;
68 // Should render the passed audio data and return true if the renderer wants
69 // to keep receiving data, or false otherwise.
70 virtual bool Render(rtc::ArrayView<const int16_t> data) = 0;
71 virtual ~Renderer() {}
72 };
kwiberg-webrtc 2017/03/09 10:04:10 Unless you have a very good reason, please make Ca
oprypin_webrtc 2017/03/10 10:44:27 I just want to get rid of this num_samples_per_fra
kwiberg-webrtc 2017/03/13 10:18:21 I have two alternative suggestions. The first one
73
32 // Creates a new FakeAudioDevice. When capturing or playing, 10 ms audio 74 // Creates a new FakeAudioDevice. When capturing or playing, 10 ms audio
33 // frames will be processed every 100ms / |speed|. 75 // frames will be processed every 100ms / |speed|.
34 // |sampling_frequency_in_hz| can be 8, 16, 32, 44.1 or 48kHz. 76 // |sampling_frequency_in_hz| can be 8, 16, 32, 44.1 or 48kHz.
35 // When recording is started, it will generates a signal where every second 77 // When recording is started, it will generates a signal where every second
36 // frame is zero and every second frame is evenly distributed random noise 78 // frame is zero and every second frame is evenly distributed random noise
37 // with max amplitude |max_amplitude|. 79 // with max amplitude |max_amplitude|.
80 FakeAudioDevice(std::unique_ptr<Capturer> capturer,
81 std::unique_ptr<Renderer> renderer,
82 float speed = 1);
kwiberg-webrtc 2017/03/09 10:04:10 The default speed is 0.1x realtime?
oprypin_webrtc 2017/03/10 10:44:27 I'm quite sure this is just realtime, because call
kwiberg-webrtc 2017/03/13 10:18:21 OK, then either the existing comment is wrong, or
38 FakeAudioDevice(float speed, 83 FakeAudioDevice(float speed,
39 int sampling_frequency_in_hz, 84 int sampling_frequency_in_hz,
40 int16_t max_amplitude); 85 int16_t max_amplitude);
41 ~FakeAudioDevice() override; 86 ~FakeAudioDevice() override;
42 87
43 private: 88 // Returns a Capturer instance that generates a signal where every second
89 // frame is zero and every second frame is evenly distributed random noise
90 // with max amplitude |max_amplitude|.
91 static std::unique_ptr<Capturer> CreatePulsedNoiseCapturer(
92 int sampling_frequency_in_hz, int16_t max_amplitude);
kwiberg-webrtc 2017/03/09 10:04:11 The sample rate is the last argument in all the ot
oprypin_webrtc 2017/03/10 10:44:27 Done. Made it the last here as well.
93
94 // Returns a Capturer instance that gets its data from a file.
95 static std::unique_ptr<Capturer> CreateWavFileReader(
96 std::string filename, int sampling_frequency_in_hz);
97
98 // Returns a Capturer instance that gets its data from a file.
99 // Automatically detects sample rate.
100 static std::unique_ptr<Capturer> CreateWavFileReader(std::string filename);
101
102 // Returns a Renderer instance that writes its data to a file.
103 static std::unique_ptr<Renderer> CreateWavFileWriter(
104 std::string filename, int sampling_frequency_in_hz);
105
106 static std::unique_ptr<Renderer> CreateDiscarder(
107 int sampling_frequency_in_hz);
108
44 int32_t Init() override; 109 int32_t Init() override;
45 int32_t RegisterAudioCallback(AudioTransport* callback) override; 110 int32_t RegisterAudioCallback(AudioTransport* callback) override;
46 111
47 int32_t StartPlayout() override; 112 int32_t StartPlayout() override;
48 int32_t StopPlayout() override; 113 int32_t StopPlayout() override;
49 int32_t StartRecording() override; 114 int32_t StartRecording() override;
50 int32_t StopRecording() override; 115 int32_t StopRecording() override;
51 116
52 bool Playing() const override; 117 bool Playing() const override;
53 bool Recording() const override; 118 bool Recording() const override;
54 119
120 // Block until the Renderer refuses to receive data.
121 bool WaitForPlayoutEnd(int timeout_ms = rtc::Event::kForever);
122 // Block until the Recorder stops producing data.
123 bool WaitForRecordingEnd(int timeout_ms = rtc::Event::kForever);
kwiberg-webrtc 2017/03/09 10:04:10 What do these two return?
oprypin_webrtc 2017/03/10 10:44:27 Done. Added comment.
124
125 private:
55 static bool Run(void* obj); 126 static bool Run(void* obj);
56 void ProcessAudio(); 127 void ProcessAudio();
57 128
58 const int sampling_frequency_in_hz_; 129 const std::unique_ptr<Capturer> capturer_ GUARDED_BY(lock_);
59 const size_t num_samples_per_frame_; 130 const std::unique_ptr<Renderer> renderer_ GUARDED_BY(lock_);
60 const float speed_; 131 const float speed_;
61 132
62 rtc::CriticalSection lock_; 133 rtc::CriticalSection lock_;
63 AudioTransport* audio_callback_ GUARDED_BY(lock_); 134 AudioTransport* audio_callback_ GUARDED_BY(lock_);
64 bool rendering_ GUARDED_BY(lock_); 135 bool rendering_ GUARDED_BY(lock_);
65 bool capturing_ GUARDED_BY(lock_); 136 bool capturing_ GUARDED_BY(lock_);
66 137 rtc::Event done_rendering_;
67 class PulsedNoiseCapturer; 138 rtc::Event done_capturing_;
68 const std::unique_ptr<PulsedNoiseCapturer> capturer_ GUARDED_BY(lock_);
69 139
70 std::vector<int16_t> playout_buffer_ GUARDED_BY(lock_); 140 std::vector<int16_t> playout_buffer_ GUARDED_BY(lock_);
71 141
72 std::unique_ptr<EventTimerWrapper> tick_; 142 std::unique_ptr<EventTimerWrapper> tick_;
73 rtc::PlatformThread thread_; 143 rtc::PlatformThread thread_;
74 }; 144 };
75 } // namespace test 145 } // namespace test
76 } // namespace webrtc 146 } // namespace webrtc
77 147
78 #endif // WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_ 148 #endif // WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698