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

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: Addressed comments. Created 3 years, 11 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 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 #include <cmath>
14 15
15 #include "webrtc/base/platform_thread.h" 16 #include "webrtc/base/platform_thread.h"
16 #include "webrtc/modules/media_file/media_file_utility.h"
17 #include "webrtc/system_wrappers/include/clock.h" 17 #include "webrtc/system_wrappers/include/clock.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" 19 #include "webrtc/system_wrappers/include/file_wrapper.h"
20 #include "webrtc/test/gtest.h"
21 20
22 namespace webrtc { 21 namespace webrtc {
23 namespace test { 22 namespace test {
24 23
25 FakeAudioDevice::FakeAudioDevice(Clock* clock, 24 namespace {
26 const std::string& filename, 25
27 float speed) 26 const double kPi = std::acos(-1);
27
28 } // namespace
29
30 FakeAudioDevice::FakeAudioDevice(Clock* clock, float speed)
28 : audio_callback_(NULL), 31 : audio_callback_(NULL),
29 capturing_(false), 32 recording_(false),
33 playing_(false),
34 normalized_frequency_(0),
35 peak_to_peak_(0),
36 n_(0),
30 captured_audio_(), 37 captured_audio_(),
31 playout_buffer_(), 38 playout_buffer_(),
32 speed_(speed), 39 speed_(speed),
33 last_playout_ms_(-1), 40 last_playout_ms_(-1),
34 clock_(clock, speed), 41 clock_(clock, speed),
35 tick_(EventTimerWrapper::Create()), 42 tick_(EventTimerWrapper::Create()),
36 thread_(FakeAudioDevice::Run, this, "FakeAudioDevice"), 43 thread_(FakeAudioDevice::Run, this, "FakeAudioDevice") {
37 file_utility_(new ModuleFileUtility(0)),
38 input_stream_(FileWrapper::Create()) {
39 memset(captured_audio_, 0, sizeof(captured_audio_)); 44 memset(captured_audio_, 0, sizeof(captured_audio_));
40 memset(playout_buffer_, 0, sizeof(playout_buffer_)); 45 memset(playout_buffer_, 0, sizeof(playout_buffer_));
41 // Open audio input file as read-only and looping.
42 EXPECT_TRUE(input_stream_->OpenFile(filename.c_str(), true)) << filename;
43 } 46 }
44 47
45 FakeAudioDevice::~FakeAudioDevice() { 48 FakeAudioDevice::~FakeAudioDevice() {
46 Stop(); 49 StopPlayout();
47 50 StopRecording();
48 thread_.Stop(); 51 thread_.Stop();
49 } 52 }
50 53
51 int32_t FakeAudioDevice::Init() { 54 int32_t FakeAudioDevice::Init() {
52 rtc::CritScope cs(&lock_); 55 RTC_CHECK(tick_->StartTimer(true, 10 / 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(); 56 thread_.Start();
59 thread_.SetPriority(rtc::kHighPriority); 57 thread_.SetPriority(rtc::kHighPriority);
60 return 0; 58 return 0;
61 } 59 }
62 60
63 int32_t FakeAudioDevice::RegisterAudioCallback(AudioTransport* callback) { 61 int32_t FakeAudioDevice::RegisterAudioCallback(AudioTransport* callback) {
64 rtc::CritScope cs(&lock_); 62 rtc::CritScope cs(&lock_);
65 audio_callback_ = callback; 63 audio_callback_ = callback;
66 return 0; 64 return 0;
67 } 65 }
68 66
69 bool FakeAudioDevice::Playing() const { 67 bool FakeAudioDevice::Playing() const {
70 rtc::CritScope cs(&lock_); 68 rtc::CritScope cs(&lock_);
71 return capturing_; 69 return playing_;
72 } 70 }
73 71
74 int32_t FakeAudioDevice::PlayoutDelay(uint16_t* delay_ms) const { 72 int32_t FakeAudioDevice::PlayoutDelay(uint16_t* delay_ms) const {
75 *delay_ms = 0; 73 *delay_ms = 0;
76 return 0; 74 return 0;
77 } 75 }
78 76
79 bool FakeAudioDevice::Recording() const { 77 bool FakeAudioDevice::Recording() const {
80 rtc::CritScope cs(&lock_); 78 rtc::CritScope cs(&lock_);
81 return capturing_; 79 return recording_;
82 } 80 }
83 81
84 bool FakeAudioDevice::Run(void* obj) { 82 bool FakeAudioDevice::Run(void* obj) {
85 static_cast<FakeAudioDevice*>(obj)->CaptureAudio(); 83 static_cast<FakeAudioDevice*>(obj)->ProcessAudio();
86 return true; 84 return true;
87 } 85 }
88 86
89 void FakeAudioDevice::CaptureAudio() { 87 void FakeAudioDevice::ProcessAudio() {
90 { 88 {
91 rtc::CritScope cs(&lock_); 89 rtc::CritScope cs(&lock_);
92 if (capturing_) { 90 if (recording_) {
peah-webrtc 2017/01/24 12:42:21 Is there a certain reason for changing the capturi
perkj_webrtc 2017/01/24 14:38:15 done and yes, there are current calltests that sta
93 int bytes_read = file_utility_->ReadPCMData( 91 // Get 10ms of audio. 2 bytes per sample.
94 *input_stream_.get(), captured_audio_, kBufferSizeBytes); 92 for (uint32_t i = 0; i < kFrequencyHz / 100; i = i + 2) {
peah-webrtc 2017/01/24 12:42:21 uint32_t -> size_t ?
perkj_webrtc 2017/01/24 14:38:15 if you prefer that sure.
95 if (bytes_read <= 0) 93 n_ += normalized_frequency_;
peah-webrtc 2017/01/24 12:42:21 There are a lot of members to this class that only
96 return; 94 if (n_ >= 2 * kPi)
97 // 2 bytes per sample. 95 n_ -= 2 * kPi;
98 size_t num_samples = static_cast<size_t>(bytes_read / 2); 96
97 uint16_t sample =
98 static_cast<uint16_t>((1.0 + std::sin(n_) / 2) * peak_to_peak_);
peah-webrtc 2017/01/24 12:42:20 Since 1.0 + sin()/2 obtains values in the range [0
perkj_webrtc 2017/01/24 14:38:15 oops, I wanted it to be between 0 and 1 just to ma
99 captured_audio_[i] = sample;
peah-webrtc 2017/01/24 12:42:21 What is the sample format for this? Is that a stan
perkj_webrtc 2017/01/24 14:38:15 I was hoping you guys know. The old comments for t
100 captured_audio_[i + 1] = sample >> 8;
101 }
102
103 size_t num_samples = kFrequencyHz / 100;
99 uint32_t new_mic_level; 104 uint32_t new_mic_level;
100 EXPECT_EQ(0, 105 RTC_CHECK_EQ(0, audio_callback_->RecordedDataIsAvailable(
101 audio_callback_->RecordedDataIsAvailable(captured_audio_, 106 captured_audio_, num_samples, 2, 1, kFrequencyHz, 0,
102 num_samples, 107 0, 0, false, new_mic_level));
103 2, 108 }
104 1, 109 if (playing_) {
105 kFrequencyHz,
106 0,
107 0,
108 0,
109 false,
110 new_mic_level));
111 size_t samples_needed = kFrequencyHz / 100; 110 size_t samples_needed = kFrequencyHz / 100;
112 int64_t now_ms = clock_.TimeInMilliseconds(); 111 int64_t now_ms = clock_.TimeInMilliseconds();
113 uint32_t time_since_last_playout_ms = now_ms - last_playout_ms_; 112 uint32_t time_since_last_playout_ms = now_ms - last_playout_ms_;
114 if (last_playout_ms_ > 0 && time_since_last_playout_ms > 0) { 113 if (last_playout_ms_ > 0 && time_since_last_playout_ms > 0) {
115 samples_needed = std::min( 114 samples_needed = std::min(
116 static_cast<size_t>(kFrequencyHz / time_since_last_playout_ms), 115 static_cast<size_t>(kFrequencyHz / time_since_last_playout_ms),
117 kBufferSizeBytes / 2); 116 kBufferSizeBytes / 2);
118 } 117 }
119 size_t samples_out = 0; 118 size_t samples_out = 0;
120 int64_t elapsed_time_ms = -1; 119 int64_t elapsed_time_ms = -1;
121 int64_t ntp_time_ms = -1; 120 int64_t ntp_time_ms = -1;
122 EXPECT_EQ(0, 121 RTC_CHECK_EQ(0, audio_callback_->NeedMorePlayData(
123 audio_callback_->NeedMorePlayData(samples_needed, 122 samples_needed, 2, 1, kFrequencyHz, playout_buffer_,
124 2, 123 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 } 124 }
132 } 125 }
133 tick_->Wait(WEBRTC_EVENT_INFINITE); 126 tick_->Wait(WEBRTC_EVENT_INFINITE);
peah-webrtc 2017/01/24 12:42:21 As it is now, tick controls the rate of calls for
perkj_webrtc 2017/01/24 14:38:15 Maybe, but that is not a change in this cl and not
134 } 127 }
135 128
136 void FakeAudioDevice::Start() { 129 int32_t FakeAudioDevice::StartPlayout() {
137 rtc::CritScope cs(&lock_); 130 rtc::CritScope cs(&lock_);
138 capturing_ = true; 131 playing_ = true;
132 return 0;
139 } 133 }
140 134
141 void FakeAudioDevice::Stop() { 135 int32_t FakeAudioDevice::StopPlayout() {
142 rtc::CritScope cs(&lock_); 136 rtc::CritScope cs(&lock_);
143 capturing_ = false; 137 playing_ = false;
138 return 0;
144 } 139 }
140
141 void FakeAudioDevice::StartRecordingSine(int frequency_in_hz,
142 uint16_t peak_to_peak) {
peah-webrtc 2017/01/24 12:42:21 Please add DCHECKS for the allowed range of peak_t
perkj_webrtc 2017/01/24 14:38:15 should not happen now.
143 rtc::CritScope cs(&lock_);
144 normalized_frequency_ = (2 * kPi * frequency_in_hz) / kFrequencyHz;
145 peak_to_peak_ = peak_to_peak;
146 recording_ = true;
147 }
148
149 int32_t FakeAudioDevice::StopRecording() {
150 rtc::CritScope cs(&lock_);
151 recording_ = false;
152 return 0;
153 }
154
145 } // namespace test 155 } // namespace test
146 } // namespace webrtc 156 } // namespace webrtc
OLDNEW
« webrtc/test/fake_audio_device.h ('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