diff options
author | phoglund <phoglund@chromium.org> | 2015-02-23 02:20:08 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-02-23 10:20:32 +0000 |
commit | 9e0a7c10163c9c22adad3135a1d9fc5c3682a592 (patch) | |
tree | e36c74d83f879d0a33cca5354cfd0c2b1aaaa542 /media/audio/fake_audio_worker_unittest.cc | |
parent | 2c8ef923fc3a440aa99b09372fde8f3d0893f551 (diff) | |
download | chromium_src-9e0a7c10163c9c22adad3135a1d9fc5c3682a592.zip chromium_src-9e0a7c10163c9c22adad3135a1d9fc5c3682a592.tar.gz chromium_src-9e0a7c10163c9c22adad3135a1d9fc5c3682a592.tar.bz2 |
Moved the fake input stream's processing onto the audio worker thread.
The reason for this is that the fake input stream would not work on Mac
where the audio non-worker code runs on the UI thread. Therefore, the
fake input stream would get starved for instance in the WebRTC audio
quality tests and not play any audio (those tests block the UI thread
while recording).
This patch introduces a worker precisely like the fake audio consumer
used by the fake output stream.
BUG=453907,446859
Review URL: https://codereview.chromium.org/922663002
Cr-Commit-Position: refs/heads/master@{#317557}
Diffstat (limited to 'media/audio/fake_audio_worker_unittest.cc')
-rw-r--r-- | media/audio/fake_audio_worker_unittest.cc | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/media/audio/fake_audio_worker_unittest.cc b/media/audio/fake_audio_worker_unittest.cc new file mode 100644 index 0000000..49a69d2 --- /dev/null +++ b/media/audio/fake_audio_worker_unittest.cc @@ -0,0 +1,141 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/bind.h" +#include "base/message_loop/message_loop.h" +#include "base/time/time.h" +#include "media/audio/audio_parameters.h" +#include "media/audio/fake_audio_worker.h" +#include "media/audio/simple_sources.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace media { + +static const int kTestCallbacks = 5; + +class FakeAudioWorkerTest : public testing::Test { + public: + FakeAudioWorkerTest() + : params_( + AudioParameters::AUDIO_FAKE, CHANNEL_LAYOUT_STEREO, 44100, 8, 128), + fake_worker_(message_loop_.message_loop_proxy(), params_), + seen_callbacks_(0) { + time_between_callbacks_ = base::TimeDelta::FromMicroseconds( + params_.frames_per_buffer() * base::Time::kMicrosecondsPerSecond / + static_cast<float>(params_.sample_rate())); + } + + ~FakeAudioWorkerTest() override {} + + void CalledByFakeWorker() { + seen_callbacks_++; + } + + void RunOnAudioThread() { + ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread()); + fake_worker_.Start(base::Bind( + &FakeAudioWorkerTest::CalledByFakeWorker, base::Unretained(this))); + } + + void RunOnceOnAudioThread() { + ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread()); + RunOnAudioThread(); + // Start() should immediately post a task to run the callback, so we + // should end up with only a single callback being run. + message_loop_.PostTask(FROM_HERE, base::Bind( + &FakeAudioWorkerTest::EndTest, base::Unretained(this), 1)); + } + + void StopStartOnAudioThread() { + ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread()); + fake_worker_.Stop(); + RunOnAudioThread(); + } + + void TimeCallbacksOnAudioThread(int callbacks) { + ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread()); + + if (seen_callbacks_ == 0) { + RunOnAudioThread(); + start_time_ = base::TimeTicks::Now(); + } + + // Keep going until we've seen the requested number of callbacks. + if (seen_callbacks_ < callbacks) { + message_loop_.PostDelayedTask(FROM_HERE, base::Bind( + &FakeAudioWorkerTest::TimeCallbacksOnAudioThread, + base::Unretained(this), callbacks), time_between_callbacks_ / 2); + } else { + end_time_ = base::TimeTicks::Now(); + EndTest(callbacks); + } + } + + void EndTest(int callbacks) { + ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread()); + fake_worker_.Stop(); + EXPECT_LE(callbacks, seen_callbacks_); + message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); + } + + protected: + base::MessageLoop message_loop_; + AudioParameters params_; + FakeAudioWorker fake_worker_; + base::TimeTicks start_time_; + base::TimeTicks end_time_; + base::TimeDelta time_between_callbacks_; + int seen_callbacks_; + + private: + DISALLOW_COPY_AND_ASSIGN(FakeAudioWorkerTest); +}; + +// Ensure the worker runs on the audio thread and fires callbacks. +TEST_F(FakeAudioWorkerTest, FakeBasicCallback) { + message_loop_.PostTask(FROM_HERE, base::Bind( + &FakeAudioWorkerTest::RunOnceOnAudioThread, + base::Unretained(this))); + message_loop_.Run(); +} + +// Ensure the time between callbacks is sane. +TEST_F(FakeAudioWorkerTest, TimeBetweenCallbacks) { + message_loop_.PostTask(FROM_HERE, base::Bind( + &FakeAudioWorkerTest::TimeCallbacksOnAudioThread, + base::Unretained(this), kTestCallbacks)); + message_loop_.Run(); + + // There are only (kTestCallbacks - 1) intervals between kTestCallbacks. + base::TimeDelta actual_time_between_callbacks = + (end_time_ - start_time_) / (seen_callbacks_ - 1); + + // Ensure callback time is no faster than the expected time between callbacks. + EXPECT_GE(actual_time_between_callbacks, time_between_callbacks_); + + // Softly check if the callback time is no slower than twice the expected time + // between callbacks. Since this test runs on the bots we can't be too strict + // with the bounds. + if (actual_time_between_callbacks > 2 * time_between_callbacks_) + LOG(ERROR) << "Time between fake audio callbacks is too large!"; +} + +// Ensure Start()/Stop() on the worker doesn't generate too many callbacks. See +// http://crbug.com/159049. +TEST_F(FakeAudioWorkerTest, StartStopClearsCallbacks) { + message_loop_.PostTask(FROM_HERE, base::Bind( + &FakeAudioWorkerTest::TimeCallbacksOnAudioThread, + base::Unretained(this), kTestCallbacks)); + + // Issue a Stop() / Start() in between expected callbacks to maximize the + // chance of catching the worker doing the wrong thing. + message_loop_.PostDelayedTask(FROM_HERE, base::Bind( + &FakeAudioWorkerTest::StopStartOnAudioThread, + base::Unretained(this)), time_between_callbacks_ / 2); + + // EndTest() will ensure the proper number of callbacks have occurred. + message_loop_.Run(); +} + +} // namespace media |