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

Side by Side Diff: webrtc/test/fake_audio_device.cc

Issue 2652803002: Refactor FakeAudioDevice to have separate methods for starting recording and playout. (Closed)
Patch Set: fix size_t Created 3 years, 10 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
« no previous file with comments | « webrtc/test/fake_audio_device.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 10
11 #include "webrtc/test/fake_audio_device.h" 11 #include "webrtc/test/fake_audio_device.h"
12 12
13 #include <algorithm> 13 #include <algorithm>
14 14
15 #include "webrtc/base/platform_thread.h" 15 #include "webrtc/base/array_view.h"
16 #include "webrtc/modules/media_file/media_file_utility.h" 16 #include "webrtc/base/checks.h"
17 #include "webrtc/system_wrappers/include/clock.h" 17 #include "webrtc/base/random.h"
18 #include "webrtc/system_wrappers/include/event_wrapper.h" 18 #include "webrtc/system_wrappers/include/event_wrapper.h"
19 #include "webrtc/system_wrappers/include/file_wrapper.h"
20 #include "webrtc/test/gtest.h"
21 19
22 namespace webrtc { 20 namespace webrtc {
21
22 namespace {
23
24 constexpr int kFrameLengthMs = 10;
25 constexpr int kFramesPerSecond = 1000 / kFrameLengthMs;
26
27 } // namespace
23 namespace test { 28 namespace test {
24 29
25 FakeAudioDevice::FakeAudioDevice(Clock* clock, 30 // Assuming 10ms audio packets..
26 const std::string& filename, 31 class FakeAudioDevice::PulsedNoiseCapturer {
27 float speed) 32 public:
28 : audio_callback_(NULL), 33 PulsedNoiseCapturer(size_t num_samples_per_frame, int16_t max_amplitude)
34 : fill_with_zero_(false),
35 random_generator_(1),
36 max_amplitude_(max_amplitude),
37 random_audio_(num_samples_per_frame),
38 silent_audio_(num_samples_per_frame, 0) {
39 RTC_DCHECK_GT(max_amplitude, 0);
40 }
41
42 rtc::ArrayView<const int16_t> Capture() {
43 fill_with_zero_ = !fill_with_zero_;
44 if (!fill_with_zero_) {
45 std::generate(random_audio_.begin(), random_audio_.end(), [&]() {
46 return random_generator_.Rand(-max_amplitude_, max_amplitude_);
47 });
48 }
49 return fill_with_zero_ ? silent_audio_ : random_audio_;
50 }
51
52 private:
53 bool fill_with_zero_;
54 Random random_generator_;
55 const int16_t max_amplitude_;
56 std::vector<int16_t> random_audio_;
57 std::vector<int16_t> silent_audio_;
58 };
59
60 FakeAudioDevice::FakeAudioDevice(float speed,
61 int sampling_frequency_in_hz,
62 int16_t max_amplitude)
63 : sampling_frequency_in_hz_(sampling_frequency_in_hz),
64 num_samples_per_frame_(
65 rtc::CheckedDivExact(sampling_frequency_in_hz_, kFramesPerSecond)),
66 speed_(speed),
67 audio_callback_(nullptr),
68 rendering_(false),
29 capturing_(false), 69 capturing_(false),
30 captured_audio_(), 70 capturer_(new FakeAudioDevice::PulsedNoiseCapturer(num_samples_per_frame_,
31 playout_buffer_(), 71 max_amplitude)),
32 speed_(speed), 72 playout_buffer_(num_samples_per_frame_, 0),
33 last_playout_ms_(-1),
34 clock_(clock, speed),
35 tick_(EventTimerWrapper::Create()), 73 tick_(EventTimerWrapper::Create()),
36 thread_(FakeAudioDevice::Run, this, "FakeAudioDevice"), 74 thread_(FakeAudioDevice::Run, this, "FakeAudioDevice") {
37 file_utility_(new ModuleFileUtility(0)), 75 RTC_DCHECK(
38 input_stream_(FileWrapper::Create()) { 76 sampling_frequency_in_hz == 8000 || sampling_frequency_in_hz == 16000 ||
39 memset(captured_audio_, 0, sizeof(captured_audio_)); 77 sampling_frequency_in_hz == 32000 || sampling_frequency_in_hz == 44100 ||
40 memset(playout_buffer_, 0, sizeof(playout_buffer_)); 78 sampling_frequency_in_hz == 48000);
41 // Open audio input file as read-only and looping.
42 EXPECT_TRUE(input_stream_->OpenFile(filename.c_str(), true)) << filename;
43 } 79 }
44 80
45 FakeAudioDevice::~FakeAudioDevice() { 81 FakeAudioDevice::~FakeAudioDevice() {
46 Stop(); 82 StopPlayout();
47 83 StopRecording();
48 thread_.Stop(); 84 thread_.Stop();
49 } 85 }
50 86
87 int32_t FakeAudioDevice::StartPlayout() {
88 rtc::CritScope cs(&lock_);
89 rendering_ = true;
90 return 0;
91 }
92
93 int32_t FakeAudioDevice::StopPlayout() {
94 rtc::CritScope cs(&lock_);
95 rendering_ = false;
96 return 0;
97 }
98
99 int32_t FakeAudioDevice::StartRecording() {
100 rtc::CritScope cs(&lock_);
101 capturing_ = true;
102 return 0;
103 }
104
105 int32_t FakeAudioDevice::StopRecording() {
106 rtc::CritScope cs(&lock_);
107 capturing_ = false;
108 return 0;
109 }
110
51 int32_t FakeAudioDevice::Init() { 111 int32_t FakeAudioDevice::Init() {
52 rtc::CritScope cs(&lock_); 112 RTC_CHECK(tick_->StartTimer(true, kFrameLengthMs / speed_));
53 if (file_utility_->InitPCMReading(*input_stream_.get()) != 0)
54 return -1;
55
56 if (!tick_->StartTimer(true, 10 / speed_))
57 return -1;
58 thread_.Start(); 113 thread_.Start();
59 thread_.SetPriority(rtc::kHighPriority); 114 thread_.SetPriority(rtc::kHighPriority);
60 return 0; 115 return 0;
61 } 116 }
62 117
63 int32_t FakeAudioDevice::RegisterAudioCallback(AudioTransport* callback) { 118 int32_t FakeAudioDevice::RegisterAudioCallback(AudioTransport* callback) {
64 rtc::CritScope cs(&lock_); 119 rtc::CritScope cs(&lock_);
120 RTC_DCHECK(callback || audio_callback_ != nullptr);
65 audio_callback_ = callback; 121 audio_callback_ = callback;
66 return 0; 122 return 0;
67 } 123 }
68 124
69 bool FakeAudioDevice::Playing() const { 125 bool FakeAudioDevice::Playing() const {
70 rtc::CritScope cs(&lock_); 126 rtc::CritScope cs(&lock_);
71 return capturing_; 127 return rendering_;
72 }
73
74 int32_t FakeAudioDevice::PlayoutDelay(uint16_t* delay_ms) const {
75 *delay_ms = 0;
76 return 0;
77 } 128 }
78 129
79 bool FakeAudioDevice::Recording() const { 130 bool FakeAudioDevice::Recording() const {
80 rtc::CritScope cs(&lock_); 131 rtc::CritScope cs(&lock_);
81 return capturing_; 132 return capturing_;
82 } 133 }
83 134
84 bool FakeAudioDevice::Run(void* obj) { 135 bool FakeAudioDevice::Run(void* obj) {
85 static_cast<FakeAudioDevice*>(obj)->CaptureAudio(); 136 static_cast<FakeAudioDevice*>(obj)->ProcessAudio();
86 return true; 137 return true;
87 } 138 }
88 139
89 void FakeAudioDevice::CaptureAudio() { 140 void FakeAudioDevice::ProcessAudio() {
90 { 141 {
91 rtc::CritScope cs(&lock_); 142 rtc::CritScope cs(&lock_);
92 if (capturing_) { 143 if (capturing_) {
93 int bytes_read = file_utility_->ReadPCMData( 144 // Capture 10ms of audio. 2 bytes per sample.
94 *input_stream_.get(), captured_audio_, kBufferSizeBytes); 145 rtc::ArrayView<const int16_t> audio_data = capturer_->Capture();
95 if (bytes_read <= 0) 146 uint32_t new_mic_level = 0;
96 return; 147 audio_callback_->RecordedDataIsAvailable(
97 // 2 bytes per sample. 148 audio_data.data(), audio_data.size(), 2, 1, sampling_frequency_in_hz_,
98 size_t num_samples = static_cast<size_t>(bytes_read / 2); 149 0, 0, 0, false, new_mic_level);
99 uint32_t new_mic_level; 150 }
100 EXPECT_EQ(0, 151 if (rendering_) {
101 audio_callback_->RecordedDataIsAvailable(captured_audio_,
102 num_samples,
103 2,
104 1,
105 kFrequencyHz,
106 0,
107 0,
108 0,
109 false,
110 new_mic_level));
111 size_t samples_needed = kFrequencyHz / 100;
112 int64_t now_ms = clock_.TimeInMilliseconds();
113 uint32_t time_since_last_playout_ms = now_ms - last_playout_ms_;
114 if (last_playout_ms_ > 0 && time_since_last_playout_ms > 0) {
115 samples_needed = std::min(
116 static_cast<size_t>(kFrequencyHz / time_since_last_playout_ms),
117 kBufferSizeBytes / 2);
118 }
119 size_t samples_out = 0; 152 size_t samples_out = 0;
120 int64_t elapsed_time_ms = -1; 153 int64_t elapsed_time_ms = -1;
121 int64_t ntp_time_ms = -1; 154 int64_t ntp_time_ms = -1;
122 EXPECT_EQ(0, 155 audio_callback_->NeedMorePlayData(
123 audio_callback_->NeedMorePlayData(samples_needed, 156 num_samples_per_frame_, 2, 1, sampling_frequency_in_hz_,
124 2, 157 playout_buffer_.data(), samples_out, &elapsed_time_ms, &ntp_time_ms);
125 1,
126 kFrequencyHz,
127 playout_buffer_,
128 samples_out,
129 &elapsed_time_ms,
130 &ntp_time_ms));
131 } 158 }
132 } 159 }
133 tick_->Wait(WEBRTC_EVENT_INFINITE); 160 tick_->Wait(WEBRTC_EVENT_INFINITE);
134 } 161 }
135 162
136 void FakeAudioDevice::Start() {
137 rtc::CritScope cs(&lock_);
138 capturing_ = true;
139 }
140 163
141 void FakeAudioDevice::Stop() {
142 rtc::CritScope cs(&lock_);
143 capturing_ = false;
144 }
145 } // namespace test 164 } // namespace test
146 } // namespace webrtc 165 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/test/fake_audio_device.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698