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

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: Prevent nested CritScope to fix 'use of an invalid mutex' 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 {
34 private:
35 class Streamer {
36 public:
37 explicit Streamer(int sampling_frequency_in_hz);
38 int SamplingFrequency() const {
39 return sampling_frequency_in_hz_;
40 }
41 size_t SamplesPerFrame() const {
42 return num_samples_per_frame_;
43 }
44
45 private:
46 const int sampling_frequency_in_hz_;
47 const size_t num_samples_per_frame_;
48 };
kwiberg-webrtc 2017/03/13 10:18:22 I still think it's wrong to have this one here---s
oprypin_webrtc 2017/03/13 13:55:14 Done.
49
31 public: 50 public:
51 class Capturer : public Streamer {
52 public:
53 using Streamer::Streamer;
kwiberg-webrtc 2017/03/13 10:18:22 This using statement is an implementation detail t
oprypin_webrtc 2017/03/13 13:55:14 Done.
54 virtual ~Capturer() {}
55 // Should capture and return some data (limited to SamplesPerFrame), or
56 // return an empty ArrayView when the capture finishes.
kwiberg-webrtc 2017/03/13 10:18:22 This comment is out of date. Also, instead of "Sho
oprypin_webrtc 2017/03/13 13:55:14 Done.
57 virtual bool Capture(rtc::BufferT<int16_t>* buffer) = 0;
58 };
59
60 class Renderer : public Streamer {
61 public:
62 using Streamer::Streamer;
63 virtual ~Renderer() {}
64 // Should render the passed audio data and return true if the renderer wants
65 // to keep receiving data, or false otherwise.
66 virtual bool Render(rtc::ArrayView<const int16_t> data) = 0;
67 };
68
32 // Creates a new FakeAudioDevice. When capturing or playing, 10 ms audio 69 // Creates a new FakeAudioDevice. When capturing or playing, 10 ms audio
33 // frames will be processed every 100ms / |speed|. 70 // frames will be processed every 100ms / |speed|.
34 // |sampling_frequency_in_hz| can be 8, 16, 32, 44.1 or 48kHz. 71 // |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 72 // 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 73 // frame is zero and every second frame is evenly distributed random noise
37 // with max amplitude |max_amplitude|. 74 // with max amplitude |max_amplitude|.
38 FakeAudioDevice(float speed, 75 FakeAudioDevice(std::unique_ptr<Capturer> capturer,
39 int sampling_frequency_in_hz, 76 std::unique_ptr<Renderer> renderer,
40 int16_t max_amplitude); 77 float speed = 1);
41 ~FakeAudioDevice() override; 78 ~FakeAudioDevice() override;
42 79
43 private: 80 // Returns a Capturer instance that generates a signal where every second
81 // frame is zero and every second frame is evenly distributed random noise
82 // with max amplitude |max_amplitude|.
83 static std::unique_ptr<Capturer> CreatePulsedNoiseCapturer(
84 int16_t max_amplitude, int sampling_frequency_in_hz);
85
86 // Returns a Capturer instance that gets its data from a file.
87 static std::unique_ptr<Capturer> CreateWavFileReader(
kwiberg-webrtc 2017/03/13 10:18:22 CreateWavFileCapturer?
88 std::string filename, int sampling_frequency_in_hz);
89
90 // Returns a Capturer instance that gets its data from a file.
91 // Automatically detects sample rate.
92 static std::unique_ptr<Capturer> CreateWavFileReader(std::string filename);
93
94 // Returns a Renderer instance that writes its data to a file.
95 static std::unique_ptr<Renderer> CreateWavFileWriter(
kwiberg-webrtc 2017/03/13 10:18:22 CreateWavFileRenderer?
oprypin_webrtc 2017/03/13 13:55:14 I avoided this name because it would confuse even
96 std::string filename, int sampling_frequency_in_hz);
97
98 static std::unique_ptr<Renderer> CreateDiscarder(
kwiberg-webrtc 2017/03/13 10:18:22 CreateDiscardRenderer?
oprypin_webrtc 2017/03/13 13:55:14 Done.
99 int sampling_frequency_in_hz);
100
44 int32_t Init() override; 101 int32_t Init() override;
45 int32_t RegisterAudioCallback(AudioTransport* callback) override; 102 int32_t RegisterAudioCallback(AudioTransport* callback) override;
46 103
47 int32_t StartPlayout() override; 104 int32_t StartPlayout() override;
48 int32_t StopPlayout() override; 105 int32_t StopPlayout() override;
49 int32_t StartRecording() override; 106 int32_t StartRecording() override;
50 int32_t StopRecording() override; 107 int32_t StopRecording() override;
51 108
52 bool Playing() const override; 109 bool Playing() const override;
53 bool Recording() const override; 110 bool Recording() const override;
54 111
112 // Blocks until the Renderer refuses to receive data.
113 // Returns false if |timeout_ms| passes before that happens.
114 bool WaitForPlayoutEnd(int timeout_ms = rtc::Event::kForever);
115 // Blocks until the Recorder stops producing data.
116 // Returns false if |timeout_ms| passes before that happens.
117 bool WaitForRecordingEnd(int timeout_ms = rtc::Event::kForever);
118
119 private:
55 static bool Run(void* obj); 120 static bool Run(void* obj);
56 void ProcessAudio(); 121 void ProcessAudio();
57 122
58 const int sampling_frequency_in_hz_; 123 const std::unique_ptr<Capturer> capturer_ GUARDED_BY(lock_);
59 const size_t num_samples_per_frame_; 124 const std::unique_ptr<Renderer> renderer_ GUARDED_BY(lock_);
60 const float speed_; 125 const float speed_;
61 126
62 rtc::CriticalSection lock_; 127 rtc::CriticalSection lock_;
63 AudioTransport* audio_callback_ GUARDED_BY(lock_); 128 AudioTransport* audio_callback_ GUARDED_BY(lock_);
64 bool rendering_ GUARDED_BY(lock_); 129 bool rendering_ GUARDED_BY(lock_);
65 bool capturing_ GUARDED_BY(lock_); 130 bool capturing_ GUARDED_BY(lock_);
66 131 rtc::Event done_rendering_;
67 class PulsedNoiseCapturer; 132 rtc::Event done_capturing_;
68 const std::unique_ptr<PulsedNoiseCapturer> capturer_ GUARDED_BY(lock_);
69 133
70 std::vector<int16_t> playout_buffer_ GUARDED_BY(lock_); 134 std::vector<int16_t> playout_buffer_ GUARDED_BY(lock_);
135 rtc::BufferT<int16_t> recording_buffer_ GUARDED_BY(lock_);
kwiberg-webrtc 2017/03/13 10:18:22 As I said before, since the sample rate is at most
oprypin_webrtc 2017/03/13 13:55:14 Now that it's a Buffer, it manages a heap allocate
kwiberg-webrtc 2017/03/13 14:22:26 Since allocating an array on the stack is ~free, t
oprypin_webrtc 2017/03/13 15:35:20 I don't see how I can have an array on the stack w
kwiberg-webrtc 2017/03/14 10:02:03 Yes, rtc::Buffer is heap-only. If you're allocatin
oprypin_webrtc 2017/03/14 11:58:12 I still don't understand how I can make an improve
kwiberg-webrtc 2017/03/14 13:46:46 By removing this member, and doing something like
oprypin_webrtc 2017/03/14 14:01:40 I understand now. So you meant that a stack alloca
kwiberg-webrtc 2017/03/14 14:08:56 Well, no wonder you were set on using BufferT, bec
71 136
72 std::unique_ptr<EventTimerWrapper> tick_; 137 std::unique_ptr<EventTimerWrapper> tick_;
73 rtc::PlatformThread thread_; 138 rtc::PlatformThread thread_;
74 }; 139 };
75 } // namespace test 140 } // namespace test
76 } // namespace webrtc 141 } // namespace webrtc
77 142
78 #endif // WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_ 143 #endif // WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698