Chromium Code Reviews| 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() { | 175 void WaitForRecordingCompletion() { scoped_task_environment_.RunUntilIdle(); } |
|
gab
2017/05/18 16:10:10
inline scoped_task_environment_.RunUntilIdle() ins
Sébastien Marchand
2017/05/20 00:28:22
Done.
| |
| 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 | 176 |
| 193 void RecordAndVerifyOnce() { | 177 void RecordAndVerifyOnce() { |
| 194 base::FilePath file_path; | 178 base::FilePath file_path; |
| 195 EXPECT_TRUE(base::CreateTemporaryFile(&file_path)); | 179 EXPECT_TRUE(base::CreateTemporaryFile(&file_path)); |
| 196 | 180 |
| 197 debug_writer_->Start(file_path); | 181 debug_writer_->Start(file_path); |
| 198 | 182 |
| 199 DoDebugRecording(); | 183 DoDebugRecording(); |
| 200 | 184 |
| 201 debug_writer_->Stop(); | 185 debug_writer_->Stop(); |
| 202 | 186 |
| 203 WaitForRecordingCompletion(); | 187 WaitForRecordingCompletion(); |
| 204 | 188 |
| 205 VerifyRecording(file_path); | 189 VerifyRecording(file_path); |
| 206 | 190 |
| 207 if (::testing::Test::HasFailure()) { | 191 if (::testing::Test::HasFailure()) { |
| 208 LOG(ERROR) << "Test failed; keeping recording(s) at [" | 192 LOG(ERROR) << "Test failed; keeping recording(s) at [" |
| 209 << file_path.value().c_str() << "]."; | 193 << file_path.value().c_str() << "]."; |
| 210 } else { | 194 } else { |
| 211 EXPECT_TRUE(base::DeleteFile(file_path, false)); | 195 EXPECT_TRUE(base::DeleteFile(file_path, false)); |
| 212 } | 196 } |
| 213 } | 197 } |
| 214 | 198 |
| 215 protected: | 199 protected: |
| 216 // |file_thread_| must to be declared before |debug_writer_| so that it's | 200 // |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 | 201 // destroyed after. This ensures all tasks posted in |debug_writer_| to the |
| 218 // file thread are run before exiting the test. | 202 // file thread are run before exiting the test. |
| 219 base::Thread file_thread_; | 203 base::Thread file_thread_; |
| 220 | 204 |
| 221 // Message loop for the test main thread. | 205 // The test task environment. |
| 222 base::MessageLoop message_loop_; | 206 base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 223 | 207 |
| 224 // Writer under test. | 208 // Writer under test. |
| 225 std::unique_ptr<AudioDebugFileWriter> debug_writer_; | 209 std::unique_ptr<AudioDebugFileWriter> debug_writer_; |
| 226 | 210 |
| 227 // AudioBus parameters. | 211 // AudioBus parameters. |
| 228 AudioParameters params_; | 212 AudioParameters params_; |
| 229 | 213 |
| 230 // Number of times to write AudioBus to the file. | 214 // Number of times to write AudioBus to the file. |
| 231 int writes_; | 215 int writes_; |
| 232 | 216 |
| 233 // Number of samples in the source data. | 217 // Number of samples in the source data. |
| 234 int source_samples_; | 218 int source_samples_; |
| 235 | 219 |
| 236 // Source data. | 220 // Source data. |
| 237 std::unique_ptr<int16_t[]> source_interleaved_; | 221 std::unique_ptr<int16_t[]> source_interleaved_; |
| 238 | 222 |
| 239 private: | 223 private: |
| 240 DISALLOW_COPY_AND_ASSIGN(AudioDebugFileWriterTest); | 224 DISALLOW_COPY_AND_ASSIGN(AudioDebugFileWriterTest); |
| 241 }; | 225 }; |
| 242 | 226 |
| 243 class AudioDebugFileWriterBehavioralTest : public AudioDebugFileWriterTest {}; | 227 class AudioDebugFileWriterBehavioralTest : public AudioDebugFileWriterTest {}; |
| 244 | 228 |
| 245 TEST_P(AudioDebugFileWriterTest, WaveRecordingTest) { | 229 TEST_P(AudioDebugFileWriterTest, WaveRecordingTest) { |
| 246 debug_writer_.reset( | 230 debug_writer_.reset(new AudioDebugFileWriter(params_)); |
| 247 new AudioDebugFileWriter(params_, file_thread_.task_runner())); | |
| 248 RecordAndVerifyOnce(); | 231 RecordAndVerifyOnce(); |
| 249 } | 232 } |
| 250 | 233 |
| 251 TEST_P(AudioDebugFileWriterTest, GetFileNameExtension) { | 234 TEST_P(AudioDebugFileWriterTest, GetFileNameExtension) { |
| 252 debug_writer_.reset( | 235 debug_writer_.reset(new AudioDebugFileWriter(params_)); |
| 253 new AudioDebugFileWriter(params_, file_thread_.task_runner())); | |
| 254 EXPECT_EQ(FILE_PATH_LITERAL("wav"), | 236 EXPECT_EQ(FILE_PATH_LITERAL("wav"), |
| 255 base::FilePath::StringType(debug_writer_->GetFileNameExtension())); | 237 base::FilePath::StringType(debug_writer_->GetFileNameExtension())); |
| 256 } | 238 } |
| 257 | 239 |
| 258 TEST_P(AudioDebugFileWriterBehavioralTest, | 240 TEST_P(AudioDebugFileWriterBehavioralTest, |
| 259 DeletedBeforeRecordingFinishedOnFileThread) { | 241 DeletedBeforeRecordingFinishedOnFileThread) { |
| 260 debug_writer_.reset( | 242 debug_writer_.reset(new AudioDebugFileWriter(params_)); |
| 261 new AudioDebugFileWriter(params_, file_thread_.task_runner())); | |
| 262 | 243 |
| 263 base::FilePath file_path; | 244 base::FilePath file_path; |
| 264 EXPECT_TRUE(base::CreateTemporaryFile(&file_path)); | 245 EXPECT_TRUE(base::CreateTemporaryFile(&file_path)); |
| 265 | 246 |
| 266 base::WaitableEvent* wait_for_deletion = | 247 base::WaitableEvent* wait_for_deletion = |
| 267 new base::WaitableEvent(base::WaitableEvent::ResetPolicy::MANUAL, | 248 new base::WaitableEvent(base::WaitableEvent::ResetPolicy::MANUAL, |
| 268 base::WaitableEvent::InitialState::NOT_SIGNALED); | 249 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 269 | 250 |
| 270 file_thread_.task_runner()->PostTask( | 251 file_thread_.task_runner()->PostTask( |
| 271 FROM_HERE, | 252 FROM_HERE, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 284 | 265 |
| 285 if (::testing::Test::HasFailure()) { | 266 if (::testing::Test::HasFailure()) { |
| 286 LOG(ERROR) << "Test failed; keeping recording(s) at [" | 267 LOG(ERROR) << "Test failed; keeping recording(s) at [" |
| 287 << file_path.value().c_str() << "]."; | 268 << file_path.value().c_str() << "]."; |
| 288 } else { | 269 } else { |
| 289 EXPECT_TRUE(base::DeleteFile(file_path, false)); | 270 EXPECT_TRUE(base::DeleteFile(file_path, false)); |
| 290 } | 271 } |
| 291 } | 272 } |
| 292 | 273 |
| 293 TEST_P(AudioDebugFileWriterBehavioralTest, FileCreationError) { | 274 TEST_P(AudioDebugFileWriterBehavioralTest, FileCreationError) { |
| 294 debug_writer_.reset( | 275 debug_writer_.reset(new AudioDebugFileWriter(params_)); |
| 295 new AudioDebugFileWriter(params_, file_thread_.task_runner())); | |
| 296 base::FilePath file_path; // Empty file name. | 276 base::FilePath file_path; // Empty file name. |
| 297 debug_writer_->Start(file_path); | 277 debug_writer_->Start(file_path); |
| 298 DoDebugRecording(); | 278 DoDebugRecording(); |
| 299 } | 279 } |
| 300 | 280 |
| 301 TEST_P(AudioDebugFileWriterBehavioralTest, StartStopStartStop) { | 281 TEST_P(AudioDebugFileWriterBehavioralTest, StartStopStartStop) { |
| 302 debug_writer_.reset( | 282 debug_writer_.reset(new AudioDebugFileWriter(params_)); |
| 303 new AudioDebugFileWriter(params_, file_thread_.task_runner())); | |
| 304 RecordAndVerifyOnce(); | 283 RecordAndVerifyOnce(); |
| 305 RecordAndVerifyOnce(); | 284 RecordAndVerifyOnce(); |
| 306 } | 285 } |
| 307 | 286 |
| 308 TEST_P(AudioDebugFileWriterBehavioralTest, DestroyNotStarted) { | 287 TEST_P(AudioDebugFileWriterBehavioralTest, DestroyNotStarted) { |
| 309 debug_writer_.reset( | 288 debug_writer_.reset(new AudioDebugFileWriter(params_)); |
| 310 new AudioDebugFileWriter(params_, file_thread_.task_runner())); | |
| 311 debug_writer_.reset(); | 289 debug_writer_.reset(); |
| 312 } | 290 } |
| 313 | 291 |
| 314 TEST_P(AudioDebugFileWriterBehavioralTest, DestroyStarted) { | 292 TEST_P(AudioDebugFileWriterBehavioralTest, DestroyStarted) { |
| 315 debug_writer_.reset( | 293 debug_writer_.reset(new AudioDebugFileWriter(params_)); |
| 316 new AudioDebugFileWriter(params_, file_thread_.task_runner())); | |
| 317 base::FilePath file_path; | 294 base::FilePath file_path; |
| 318 EXPECT_TRUE(base::CreateTemporaryFile(&file_path)); | 295 EXPECT_TRUE(base::CreateTemporaryFile(&file_path)); |
| 319 debug_writer_->Start(file_path); | 296 debug_writer_->Start(file_path); |
| 320 debug_writer_.reset(); | 297 debug_writer_.reset(); |
| 321 } | 298 } |
| 322 | 299 |
| 323 INSTANTIATE_TEST_CASE_P( | 300 INSTANTIATE_TEST_CASE_P( |
| 324 AudioDebugFileWriterTest, | 301 AudioDebugFileWriterTest, |
| 325 AudioDebugFileWriterTest, | 302 AudioDebugFileWriterTest, |
| 326 // Using 10ms frames per buffer everywhere. | 303 // Using 10ms frames per buffer everywhere. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 361 AudioDebugFileWriterBehavioralTest, | 338 AudioDebugFileWriterBehavioralTest, |
| 362 // Using 10ms frames per buffer everywhere. | 339 // Using 10ms frames per buffer everywhere. |
| 363 testing::Values( | 340 testing::Values( |
| 364 // No writes. | 341 // No writes. |
| 365 std::tr1::make_tuple(ChannelLayout::CHANNEL_LAYOUT_MONO, | 342 std::tr1::make_tuple(ChannelLayout::CHANNEL_LAYOUT_MONO, |
| 366 44100, | 343 44100, |
| 367 44100 / 100, | 344 44100 / 100, |
| 368 100))); | 345 100))); |
| 369 | 346 |
| 370 } // namespace media | 347 } // namespace media |
| OLD | NEW |