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

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

Issue 2717623003: Add the ability to read/write to WAV files in FakeAudioDevice (Closed)
Patch Set: Don't run the thread all the time; add more checks 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
(Empty)
1 /*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "webrtc/test/file_audio_device.h"
12
13 #include <algorithm>
14
15 #include "webrtc/base/checks.h"
16 #include "webrtc/system_wrappers/include/event_wrapper.h"
17
18 namespace webrtc {
19
20 namespace {
21
22 constexpr int kFrameLengthMs = 10;
23 constexpr int kFramesPerSecond = 1000 / kFrameLengthMs;
24
25 int NumSamplesPerFrame(int sample_rate_hz) {
26 return rtc::CheckedDivExact(sample_rate_hz, kFramesPerSecond);
27 }
28
29 } // namespace
30 namespace test {
31
32 FileReaderAudioDevice::FileReaderAudioDevice(std::string filename, float speed,
33 int sample_rate_hz)
34 : filename_(filename),
35 sample_rate_hz_(sample_rate_hz),
36 speed_(speed),
37 audio_callback_(nullptr),
38 wav_reader_(nullptr),
kwiberg-webrtc 2017/03/07 10:11:57 unique_ptrs are null by default, so you don't need
39 playout_buffer_(NumSamplesPerFrame(sample_rate_hz), 0),
40 tick_(EventTimerWrapper::Create()),
41 thread_(FileReaderAudioDevice::Run, this, "FileReaderAudioDevice"),
42 done_reading_(true, true) {
43 RTC_CHECK(
44 sample_rate_hz == 8000 || sample_rate_hz == 16000 ||
45 sample_rate_hz == 32000 || sample_rate_hz == 44100 ||
46 sample_rate_hz == 48000);
47 }
48
49 FileReaderAudioDevice::~FileReaderAudioDevice() {
50 StopRecording();
51 }
52
53 int32_t FileReaderAudioDevice::StartRecording() {
54 rtc::CritScope cs(&lock_);
55 RTC_CHECK(!wav_reader_);
56 wav_reader_.reset(new WavReader(filename_));
57 RTC_CHECK(wav_reader_->sample_rate() == sample_rate_hz_);
58 RTC_CHECK(wav_reader_->num_channels() == 1);
kwiberg-webrtc 2017/03/07 10:11:57 RTC_CHECK_EQ for these two.
oprypin_webrtc 2017/03/09 08:23:24 Done.
59
60 RTC_CHECK(tick_->StartTimer(true, kFrameLengthMs / speed_));
61 thread_.Start();
62 thread_.SetPriority(rtc::kHighPriority);
63 done_reading_.Reset();
64 return 0;
65 }
66
67 int32_t FileReaderAudioDevice::StopRecording() {
68 rtc::CritScope cs(&lock_);
69 wav_reader_.reset();
70 thread_.Stop();
71 return 0;
72 }
73
74 int32_t FileReaderAudioDevice::RegisterAudioCallback(AudioTransport* callback) {
75 rtc::CritScope cs(&lock_);
76 RTC_DCHECK(callback || audio_callback_ != nullptr);
kwiberg-webrtc 2017/03/07 10:11:57 Test code, so CHECK instead of DCHECK everywhere?
oprypin_webrtc 2017/03/09 08:23:24 Done.
77 audio_callback_ = callback;
78 return 0;
79 }
80
81 bool FileReaderAudioDevice::Recording() const {
82 rtc::CritScope cs(&lock_);
83 return static_cast<bool>(wav_reader_);
84 }
85
86 bool FileReaderAudioDevice::WaitForFileEnd(int timeout_ms) {
87 RTC_DCHECK(Recording());
88 return done_reading_.Wait(timeout_ms);
89 }
90
91 bool FileReaderAudioDevice::Run(void* obj) {
92 static_cast<FileReaderAudioDevice*>(obj)->ProcessAudio();
93 return true;
94 }
95
96 void FileReaderAudioDevice::ProcessAudio() {
97 {
98 rtc::CritScope cs(&lock_);
99 // Capture 10ms of audio. 2 bytes per sample.
100 const size_t samples_out = wav_reader_->ReadSamples(
101 playout_buffer_.size(), playout_buffer_.data());
102 if (samples_out) {
kwiberg-webrtc 2017/03/07 10:11:57 if (samples_out > 0) to emphasize that samples_ou
oprypin_webrtc 2017/03/09 08:23:24 Done.
103 RTC_CHECK(samples_out <= playout_buffer_.size());
kwiberg-webrtc 2017/03/07 10:11:57 RTC_CHECK_LE Also, move this check before line 10
oprypin_webrtc 2017/03/09 08:23:24 Done.
104 uint32_t new_mic_level;
105 audio_callback_->RecordedDataIsAvailable(
106 playout_buffer_.data(), samples_out, 2, 1,
107 sample_rate_hz_, 0, 0, 0, false, new_mic_level);
108 } else {
109 done_reading_.Set();
110 }
111 }
112 tick_->Wait(WEBRTC_EVENT_INFINITE);
113 }
114
115
116 FileWriterAudioDevice::FileWriterAudioDevice(std::string filename, float speed,
117 int sample_rate_hz)
118 : filename_(filename),
119 sample_rate_hz_(sample_rate_hz),
120 speed_(speed),
121 audio_callback_(nullptr),
122 wav_writer_(nullptr),
123 playout_buffer_(NumSamplesPerFrame(sample_rate_hz), 0),
124 tick_(EventTimerWrapper::Create()),
125 thread_(FileWriterAudioDevice::Run, this, "FileWriterAudioDevice") {
126 RTC_CHECK(
127 sample_rate_hz == 8000 || sample_rate_hz == 16000 ||
128 sample_rate_hz == 32000 || sample_rate_hz == 44100 ||
129 sample_rate_hz == 48000);
130 }
131
132 FileWriterAudioDevice::~FileWriterAudioDevice() {
133 StopPlayout();
134 }
135
136 int32_t FileWriterAudioDevice::StartPlayout() {
137 rtc::CritScope cs(&lock_);
138 RTC_CHECK(!wav_writer_);
139 wav_writer_.reset(new WavWriter(filename_, sample_rate_hz_, 1));
140
141 RTC_CHECK(tick_->StartTimer(true, kFrameLengthMs / speed_));
142 thread_.Start();
143 thread_.SetPriority(rtc::kHighPriority);
144 return 0;
145 }
146
147 int32_t FileWriterAudioDevice::StopPlayout() {
148 rtc::CritScope cs(&lock_);
149 wav_writer_.reset(); // This also finalizes and closes the file
150 thread_.Stop();
151 return 0;
152 }
153
154 int32_t FileWriterAudioDevice::RegisterAudioCallback(AudioTransport* callback) {
155 rtc::CritScope cs(&lock_);
156 RTC_DCHECK(callback || audio_callback_ != nullptr);
157 audio_callback_ = callback;
158 return 0;
159 }
160
161 bool FileWriterAudioDevice::Playing() const {
162 rtc::CritScope cs(&lock_);
163 return static_cast<bool>(wav_writer_);
164 }
165
166 bool FileWriterAudioDevice::Run(void* obj) {
167 static_cast<FileWriterAudioDevice*>(obj)->ProcessAudio();
168 return true;
169 }
170
171 void FileWriterAudioDevice::ProcessAudio() {
172 {
173 rtc::CritScope cs(&lock_);
174 size_t samples_out;
175 int64_t elapsed_time_ms;
176 int64_t ntp_time_ms;
177 audio_callback_->NeedMorePlayData(
178 playout_buffer_.size(), 2, 1, sample_rate_hz_,
179 playout_buffer_.data(), samples_out, &elapsed_time_ms, &ntp_time_ms);
180 wav_writer_->WriteSamples(playout_buffer_.data(), samples_out);
181 }
182 tick_->Wait(WEBRTC_EVENT_INFINITE);
183 }
184
185 } // namespace test
186 } // namespace webrtc
OLDNEW
« webrtc/test/file_audio_device.h ('K') | « webrtc/test/file_audio_device.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698