OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
12 #include "base/sys_byteorder.h" | 12 #include "base/sys_byteorder.h" |
| 13 #include "base/test/scoped_task_environment.h" |
13 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
14 #include "media/audio/audio_debug_file_writer.h" | 15 #include "media/audio/audio_debug_file_writer.h" |
15 #include "media/base/audio_bus.h" | 16 #include "media/base/audio_bus.h" |
16 #include "media/base/audio_sample_types.h" | 17 #include "media/base/audio_sample_types.h" |
17 #include "media/base/test_helpers.h" | 18 #include "media/base/test_helpers.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 namespace media { | 21 namespace media { |
21 | 22 |
22 namespace { | 23 namespace { |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 int read = file.Read(kWavHeaderSize, | 150 int read = file.Read(kWavHeaderSize, |
150 reinterpret_cast<char*>(result_interleaved.get()), | 151 reinterpret_cast<char*>(result_interleaved.get()), |
151 source_samples_ * kBytesPerSample); | 152 source_samples_ * kBytesPerSample); |
152 EXPECT_EQ(static_cast<int>(file.GetLength() - kWavHeaderSize), read); | 153 EXPECT_EQ(static_cast<int>(file.GetLength() - kWavHeaderSize), read); |
153 | 154 |
154 VerifyDataRecording(source_interleaved_.get(), result_interleaved.get(), | 155 VerifyDataRecording(source_interleaved_.get(), result_interleaved.get(), |
155 source_samples_); | 156 source_samples_); |
156 } | 157 } |
157 } | 158 } |
158 | 159 |
159 void TestDoneOnFileThread(const base::Closure& callback) { | |
160 DCHECK(file_thread_.task_runner()->BelongsToCurrentThread()); | |
161 | |
162 callback.Run(); | |
163 } | |
164 | |
165 void DoDebugRecording() { | 160 void DoDebugRecording() { |
166 // Write tasks are posted to |file_thread_|. | 161 // Write tasks are posted to |file_thread_|. |
167 for (int i = 0; i < writes_; ++i) { | 162 for (int i = 0; i < writes_; ++i) { |
168 std::unique_ptr<AudioBus> bus = | 163 std::unique_ptr<AudioBus> bus = |
169 AudioBus::Create(params_.channels(), params_.frames_per_buffer()); | 164 AudioBus::Create(params_.channels(), params_.frames_per_buffer()); |
170 | 165 |
171 bus->FromInterleaved<media::SignedInt16SampleTypeTraits>( | 166 bus->FromInterleaved<media::SignedInt16SampleTypeTraits>( |
172 source_interleaved_.get() + | 167 source_interleaved_.get() + |
173 i * params_.channels() * params_.frames_per_buffer(), | 168 i * params_.channels() * params_.frames_per_buffer(), |
174 params_.frames_per_buffer()); | 169 params_.frames_per_buffer()); |
175 | 170 |
176 debug_writer_->Write(std::move(bus)); | 171 debug_writer_->Write(std::move(bus)); |
177 } | 172 } |
178 } | 173 } |
179 | 174 |
180 void WaitForRecordingCompletion() { | |
181 WaitableMessageLoopEvent event; | |
182 | |
183 // Post a task to the file thread indicating that all the writes are done. | |
184 file_thread_.task_runner()->PostTask( | |
185 FROM_HERE, | |
186 base::Bind(&AudioDebugFileWriterTest::TestDoneOnFileThread, | |
187 base::Unretained(this), event.GetClosure())); | |
188 | |
189 // Wait for TestDoneOnFileThread() to call event's closure. | |
190 event.RunAndWait(); | |
191 } | |
192 | |
193 void RecordAndVerifyOnce() { | 175 void RecordAndVerifyOnce() { |
194 base::FilePath file_path; | 176 base::FilePath file_path; |
195 EXPECT_TRUE(base::CreateTemporaryFile(&file_path)); | 177 EXPECT_TRUE(base::CreateTemporaryFile(&file_path)); |
196 | 178 |
197 debug_writer_->Start(file_path); | 179 debug_writer_->Start(file_path); |
198 | 180 |
199 DoDebugRecording(); | 181 DoDebugRecording(); |
200 | 182 |
201 debug_writer_->Stop(); | 183 debug_writer_->Stop(); |
202 | 184 |
203 WaitForRecordingCompletion(); | 185 scoped_task_environment_.RunUntilIdle(); |
204 | 186 |
205 VerifyRecording(file_path); | 187 VerifyRecording(file_path); |
206 | 188 |
207 if (::testing::Test::HasFailure()) { | 189 if (::testing::Test::HasFailure()) { |
208 LOG(ERROR) << "Test failed; keeping recording(s) at [" | 190 LOG(ERROR) << "Test failed; keeping recording(s) at [" |
209 << file_path.value().c_str() << "]."; | 191 << file_path.value().c_str() << "]."; |
210 } else { | 192 } else { |
211 EXPECT_TRUE(base::DeleteFile(file_path, false)); | 193 EXPECT_TRUE(base::DeleteFile(file_path, false)); |
212 } | 194 } |
213 } | 195 } |
214 | 196 |
215 protected: | 197 protected: |
216 // |file_thread_| must to be declared before |debug_writer_| so that it's | 198 // |file_thread_| must to be declared before |debug_writer_| so that it's |
217 // destroyed after. This ensures all tasks posted in |debug_writer_| to the | 199 // destroyed after. This ensures all tasks posted in |debug_writer_| to the |
218 // file thread are run before exiting the test. | 200 // file thread are run before exiting the test. |
219 base::Thread file_thread_; | 201 base::Thread file_thread_; |
220 | 202 |
221 // Message loop for the test main thread. | 203 // The test task environment. |
222 base::MessageLoop message_loop_; | 204 base::test::ScopedTaskEnvironment scoped_task_environment_; |
223 | 205 |
224 // Writer under test. | 206 // Writer under test. |
225 std::unique_ptr<AudioDebugFileWriter> debug_writer_; | 207 std::unique_ptr<AudioDebugFileWriter> debug_writer_; |
226 | 208 |
227 // AudioBus parameters. | 209 // AudioBus parameters. |
228 AudioParameters params_; | 210 AudioParameters params_; |
229 | 211 |
230 // Number of times to write AudioBus to the file. | 212 // Number of times to write AudioBus to the file. |
231 int writes_; | 213 int writes_; |
232 | 214 |
233 // Number of samples in the source data. | 215 // Number of samples in the source data. |
234 int source_samples_; | 216 int source_samples_; |
235 | 217 |
236 // Source data. | 218 // Source data. |
237 std::unique_ptr<int16_t[]> source_interleaved_; | 219 std::unique_ptr<int16_t[]> source_interleaved_; |
238 | 220 |
239 private: | 221 private: |
240 DISALLOW_COPY_AND_ASSIGN(AudioDebugFileWriterTest); | 222 DISALLOW_COPY_AND_ASSIGN(AudioDebugFileWriterTest); |
241 }; | 223 }; |
242 | 224 |
243 class AudioDebugFileWriterBehavioralTest : public AudioDebugFileWriterTest {}; | 225 class AudioDebugFileWriterBehavioralTest : public AudioDebugFileWriterTest {}; |
244 | 226 |
245 TEST_P(AudioDebugFileWriterTest, WaveRecordingTest) { | 227 TEST_P(AudioDebugFileWriterTest, WaveRecordingTest) { |
246 debug_writer_.reset( | 228 debug_writer_.reset(new AudioDebugFileWriter(params_)); |
247 new AudioDebugFileWriter(params_, file_thread_.task_runner())); | |
248 RecordAndVerifyOnce(); | 229 RecordAndVerifyOnce(); |
249 } | 230 } |
250 | 231 |
251 TEST_P(AudioDebugFileWriterTest, GetFileNameExtension) { | 232 TEST_P(AudioDebugFileWriterTest, GetFileNameExtension) { |
252 debug_writer_.reset( | 233 debug_writer_.reset(new AudioDebugFileWriter(params_)); |
253 new AudioDebugFileWriter(params_, file_thread_.task_runner())); | |
254 EXPECT_EQ(FILE_PATH_LITERAL("wav"), | 234 EXPECT_EQ(FILE_PATH_LITERAL("wav"), |
255 base::FilePath::StringType(debug_writer_->GetFileNameExtension())); | 235 base::FilePath::StringType(debug_writer_->GetFileNameExtension())); |
256 } | 236 } |
257 | 237 |
258 TEST_P(AudioDebugFileWriterBehavioralTest, | 238 TEST_P(AudioDebugFileWriterBehavioralTest, |
259 DeletedBeforeRecordingFinishedOnFileThread) { | 239 DeletedBeforeRecordingFinishedOnFileThread) { |
260 debug_writer_.reset( | 240 debug_writer_.reset(new AudioDebugFileWriter(params_)); |
261 new AudioDebugFileWriter(params_, file_thread_.task_runner())); | |
262 | 241 |
263 base::FilePath file_path; | 242 base::FilePath file_path; |
264 EXPECT_TRUE(base::CreateTemporaryFile(&file_path)); | 243 EXPECT_TRUE(base::CreateTemporaryFile(&file_path)); |
265 | 244 |
266 base::WaitableEvent* wait_for_deletion = | 245 base::WaitableEvent* wait_for_deletion = |
267 new base::WaitableEvent(base::WaitableEvent::ResetPolicy::MANUAL, | 246 new base::WaitableEvent(base::WaitableEvent::ResetPolicy::MANUAL, |
268 base::WaitableEvent::InitialState::NOT_SIGNALED); | 247 base::WaitableEvent::InitialState::NOT_SIGNALED); |
269 | 248 |
270 file_thread_.task_runner()->PostTask( | 249 file_thread_.task_runner()->PostTask( |
271 FROM_HERE, | 250 FROM_HERE, |
272 base::Bind(&base::WaitableEvent::Wait, base::Owned(wait_for_deletion))); | 251 base::Bind(&base::WaitableEvent::Wait, base::Owned(wait_for_deletion))); |
273 | 252 |
274 debug_writer_->Start(file_path); | 253 debug_writer_->Start(file_path); |
275 | 254 |
276 DoDebugRecording(); | 255 DoDebugRecording(); |
277 | 256 |
278 debug_writer_.reset(); | 257 debug_writer_.reset(); |
279 wait_for_deletion->Signal(); | 258 wait_for_deletion->Signal(); |
280 | 259 |
281 WaitForRecordingCompletion(); | 260 scoped_task_environment_.RunUntilIdle(); |
282 | 261 |
283 VerifyRecording(file_path); | 262 VerifyRecording(file_path); |
284 | 263 |
285 if (::testing::Test::HasFailure()) { | 264 if (::testing::Test::HasFailure()) { |
286 LOG(ERROR) << "Test failed; keeping recording(s) at [" | 265 LOG(ERROR) << "Test failed; keeping recording(s) at [" |
287 << file_path.value().c_str() << "]."; | 266 << file_path.value().c_str() << "]."; |
288 } else { | 267 } else { |
289 EXPECT_TRUE(base::DeleteFile(file_path, false)); | 268 EXPECT_TRUE(base::DeleteFile(file_path, false)); |
290 } | 269 } |
291 } | 270 } |
292 | 271 |
293 TEST_P(AudioDebugFileWriterBehavioralTest, FileCreationError) { | 272 TEST_P(AudioDebugFileWriterBehavioralTest, FileCreationError) { |
294 debug_writer_.reset( | 273 debug_writer_.reset(new AudioDebugFileWriter(params_)); |
295 new AudioDebugFileWriter(params_, file_thread_.task_runner())); | |
296 base::FilePath file_path; // Empty file name. | 274 base::FilePath file_path; // Empty file name. |
297 debug_writer_->Start(file_path); | 275 debug_writer_->Start(file_path); |
298 DoDebugRecording(); | 276 DoDebugRecording(); |
299 } | 277 } |
300 | 278 |
301 TEST_P(AudioDebugFileWriterBehavioralTest, StartStopStartStop) { | 279 TEST_P(AudioDebugFileWriterBehavioralTest, StartStopStartStop) { |
302 debug_writer_.reset( | 280 debug_writer_.reset(new AudioDebugFileWriter(params_)); |
303 new AudioDebugFileWriter(params_, file_thread_.task_runner())); | |
304 RecordAndVerifyOnce(); | 281 RecordAndVerifyOnce(); |
305 RecordAndVerifyOnce(); | 282 RecordAndVerifyOnce(); |
306 } | 283 } |
307 | 284 |
308 TEST_P(AudioDebugFileWriterBehavioralTest, DestroyNotStarted) { | 285 TEST_P(AudioDebugFileWriterBehavioralTest, DestroyNotStarted) { |
309 debug_writer_.reset( | 286 debug_writer_.reset(new AudioDebugFileWriter(params_)); |
310 new AudioDebugFileWriter(params_, file_thread_.task_runner())); | |
311 debug_writer_.reset(); | 287 debug_writer_.reset(); |
312 } | 288 } |
313 | 289 |
314 TEST_P(AudioDebugFileWriterBehavioralTest, DestroyStarted) { | 290 TEST_P(AudioDebugFileWriterBehavioralTest, DestroyStarted) { |
315 debug_writer_.reset( | 291 debug_writer_.reset(new AudioDebugFileWriter(params_)); |
316 new AudioDebugFileWriter(params_, file_thread_.task_runner())); | |
317 base::FilePath file_path; | 292 base::FilePath file_path; |
318 EXPECT_TRUE(base::CreateTemporaryFile(&file_path)); | 293 EXPECT_TRUE(base::CreateTemporaryFile(&file_path)); |
319 debug_writer_->Start(file_path); | 294 debug_writer_->Start(file_path); |
320 debug_writer_.reset(); | 295 debug_writer_.reset(); |
321 } | 296 } |
322 | 297 |
323 INSTANTIATE_TEST_CASE_P( | 298 INSTANTIATE_TEST_CASE_P( |
324 AudioDebugFileWriterTest, | 299 AudioDebugFileWriterTest, |
325 AudioDebugFileWriterTest, | 300 AudioDebugFileWriterTest, |
326 // Using 10ms frames per buffer everywhere. | 301 // Using 10ms frames per buffer everywhere. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 AudioDebugFileWriterBehavioralTest, | 336 AudioDebugFileWriterBehavioralTest, |
362 // Using 10ms frames per buffer everywhere. | 337 // Using 10ms frames per buffer everywhere. |
363 testing::Values( | 338 testing::Values( |
364 // No writes. | 339 // No writes. |
365 std::tr1::make_tuple(ChannelLayout::CHANNEL_LAYOUT_MONO, | 340 std::tr1::make_tuple(ChannelLayout::CHANNEL_LAYOUT_MONO, |
366 44100, | 341 44100, |
367 44100 / 100, | 342 44100 / 100, |
368 100))); | 343 100))); |
369 | 344 |
370 } // namespace media | 345 } // namespace media |
OLD | NEW |