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

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: Removed drifting clock. 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
« webrtc/test/call_test.cc ('K') | « 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(int num_samples_per_frame, int16_t max_amplitude)
29 capturing_(false), 34 : fill_with_zero_(false),
30 captured_audio_(), 35 random_generator_(1),
31 playout_buffer_(), 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 webrtc::Random random_generator_;
peah-webrtc 2017/01/30 06:45:08 Since you are in the namespace webrtc, I don't thi
perkj_webrtc 2017/01/30 12:02:12 Done.
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, int sampling_frequency_in_hz)
61 : sampling_frequency_in_hz_(sampling_frequency_in_hz),
62 num_samples_per_frame_(
63 rtc::CheckedDivExact(sampling_frequency_in_hz_, kFramesPerSecond)),
64 audio_callback_(nullptr),
65 rendering_(false),
66 playout_buffer_(num_samples_per_frame_, 0),
32 speed_(speed), 67 speed_(speed),
33 last_playout_ms_(-1),
34 clock_(clock, speed),
35 tick_(EventTimerWrapper::Create()), 68 tick_(EventTimerWrapper::Create()),
36 thread_(FakeAudioDevice::Run, this, "FakeAudioDevice"), 69 thread_(FakeAudioDevice::Run, this, "FakeAudioDevice") {
37 file_utility_(new ModuleFileUtility(0)), 70 RTC_DCHECK(
38 input_stream_(FileWrapper::Create()) { 71 sampling_frequency_in_hz == 8000 || sampling_frequency_in_hz == 16000 ||
39 memset(captured_audio_, 0, sizeof(captured_audio_)); 72 sampling_frequency_in_hz == 32000 || sampling_frequency_in_hz == 44100 ||
40 memset(playout_buffer_, 0, sizeof(playout_buffer_)); 73 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 } 74 }
44 75
45 FakeAudioDevice::~FakeAudioDevice() { 76 FakeAudioDevice::~FakeAudioDevice() {
46 Stop(); 77 StopPlayout();
47 78 StopRecording();
48 thread_.Stop(); 79 thread_.Stop();
49 } 80 }
50 81
82 int32_t FakeAudioDevice::StartPlayout() {
83 rtc::CritScope cs(&lock_);
84 rendering_ = true;
85 return 0;
86 }
87
88 int32_t FakeAudioDevice::StopPlayout() {
89 rtc::CritScope cs(&lock_);
90 rendering_ = false;
91 return 0;
92 }
93
94 void FakeAudioDevice::StartRecordingPulsedNoise(int16_t max_amplitude) {
95 rtc::CritScope cs(&lock_);
96 capturer_.reset(new FakeAudioDevice::PulsedNoiseCapturer(
97 num_samples_per_frame_, max_amplitude));
98 }
99
100 int32_t FakeAudioDevice::StopRecording() {
101 rtc::CritScope cs(&lock_);
102 capturer_.reset();
103 return 0;
104 }
105
51 int32_t FakeAudioDevice::Init() { 106 int32_t FakeAudioDevice::Init() {
52 rtc::CritScope cs(&lock_); 107 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(); 108 thread_.Start();
59 thread_.SetPriority(rtc::kHighPriority); 109 thread_.SetPriority(rtc::kHighPriority);
60 return 0; 110 return 0;
61 } 111 }
62 112
63 int32_t FakeAudioDevice::RegisterAudioCallback(AudioTransport* callback) { 113 int32_t FakeAudioDevice::RegisterAudioCallback(AudioTransport* callback) {
peah-webrtc 2017/01/30 06:45:08 Since this is not used anywhere in the class and i
perkj_webrtc 2017/01/30 12:02:12 ? audio_callback_ is used.
peah-webrtc 2017/01/30 17:06:39 I cannot see that but I'm probably missing somethi
perkj_webrtc 2017/01/31 06:50:35 audio_callback_ is used both for recording an play
peah-webrtc 2017/01/31 16:37:32 True, of course, my mistake. Thanks for the explan
64 rtc::CritScope cs(&lock_); 114 rtc::CritScope cs(&lock_);
115 RTC_DCHECK(callback || audio_callback_ != nullptr);
peah-webrtc 2017/01/30 06:45:08 This DCHECK I don't follow. It basically checks th
perkj_webrtc 2017/01/30 12:02:12 No, it allows callback == nullptr if audio_callbac
peah-webrtc 2017/01/30 17:06:39 In my mind that is a too strict restriction as tha
perkj_webrtc 2017/01/31 06:50:35 that is not harmfull but is probably not what the
peah-webrtc 2017/01/31 16:37:32 Acknowledged.
65 audio_callback_ = callback; 116 audio_callback_ = callback;
66 return 0; 117 return 0;
67 } 118 }
68 119
69 bool FakeAudioDevice::Playing() const { 120 bool FakeAudioDevice::Playing() const {
70 rtc::CritScope cs(&lock_); 121 rtc::CritScope cs(&lock_);
71 return capturing_; 122 return rendering_;
72 } 123 }
73 124
74 int32_t FakeAudioDevice::PlayoutDelay(uint16_t* delay_ms) const { 125 int32_t FakeAudioDevice::PlayoutDelay(uint16_t* delay_ms) const {
peah-webrtc 2017/01/30 06:45:08 Since PlayoutDelay, and it is not used inside this
perkj_webrtc 2017/01/30 12:02:12 We could move the implementation to the base class
126 RTC_DCHECK(delay_ms);
75 *delay_ms = 0; 127 *delay_ms = 0;
76 return 0; 128 return 0;
77 } 129 }
78 130
79 bool FakeAudioDevice::Recording() const { 131 bool FakeAudioDevice::Recording() const {
80 rtc::CritScope cs(&lock_); 132 rtc::CritScope cs(&lock_);
81 return capturing_; 133 return !!capturer_;
82 } 134 }
83 135
84 bool FakeAudioDevice::Run(void* obj) { 136 bool FakeAudioDevice::Run(void* obj) {
85 static_cast<FakeAudioDevice*>(obj)->CaptureAudio(); 137 static_cast<FakeAudioDevice*>(obj)->ProcessAudio();
86 return true; 138 return true;
87 } 139 }
88 140
89 void FakeAudioDevice::CaptureAudio() { 141 void FakeAudioDevice::ProcessAudio() {
90 { 142 {
91 rtc::CritScope cs(&lock_); 143 rtc::CritScope cs(&lock_);
92 if (capturing_) { 144 if (capturer_) {
93 int bytes_read = file_utility_->ReadPCMData( 145 // Capture 10ms of audio. 2 bytes per sample.
94 *input_stream_.get(), captured_audio_, kBufferSizeBytes); 146 rtc::ArrayView<const int16_t> audio_data = capturer_->Capture();
95 if (bytes_read <= 0) 147 uint32_t new_mic_level = 0;
96 return; 148 RTC_CHECK_EQ(
peah-webrtc 2017/01/30 06:45:09 Do we really want to crash this code if the return
perkj_webrtc 2017/01/30 12:02:12 yes I think so. We can not use gtest.
97 // 2 bytes per sample. 149 0, audio_callback_->RecordedDataIsAvailable(
98 size_t num_samples = static_cast<size_t>(bytes_read / 2); 150 audio_data.data(), audio_data.size(), 2, 1,
99 uint32_t new_mic_level; 151 sampling_frequency_in_hz_, 0, 0, 0, false, new_mic_level));
100 EXPECT_EQ(0, 152 }
101 audio_callback_->RecordedDataIsAvailable(captured_audio_, 153 if (rendering_) {
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; 154 size_t samples_out = 0;
120 int64_t elapsed_time_ms = -1; 155 int64_t elapsed_time_ms = -1;
121 int64_t ntp_time_ms = -1; 156 int64_t ntp_time_ms = -1;
122 EXPECT_EQ(0, 157 RTC_CHECK_EQ(0, audio_callback_->NeedMorePlayData(
peah-webrtc 2017/01/30 06:45:08 Do we really want to crash this code if the return
perkj_webrtc 2017/01/30 12:02:12 dito
123 audio_callback_->NeedMorePlayData(samples_needed, 158 num_samples_per_frame_, 2, 1,
124 2, 159 sampling_frequency_in_hz_, playout_buffer_.data(),
125 1, 160 samples_out, &elapsed_time_ms, &ntp_time_ms));
126 kFrequencyHz,
127 playout_buffer_,
128 samples_out,
129 &elapsed_time_ms,
130 &ntp_time_ms));
131 } 161 }
132 } 162 }
133 tick_->Wait(WEBRTC_EVENT_INFINITE); 163 tick_->Wait(WEBRTC_EVENT_INFINITE);
134 } 164 }
135 165
136 void FakeAudioDevice::Start() {
137 rtc::CritScope cs(&lock_);
138 capturing_ = true;
139 }
140 166
141 void FakeAudioDevice::Stop() {
142 rtc::CritScope cs(&lock_);
143 capturing_ = false;
144 }
145 } // namespace test 167 } // namespace test
146 } // namespace webrtc 168 } // namespace webrtc
OLDNEW
« webrtc/test/call_test.cc ('K') | « webrtc/test/fake_audio_device.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698