diff options
author | xians@chromium.org <xians@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-17 18:30:24 +0000 |
---|---|---|
committer | xians@chromium.org <xians@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-17 18:30:24 +0000 |
commit | d5ef7d4bcd820355cce7fc5a1815123c882ff1fc (patch) | |
tree | 0b2b5d8b9741ce1773589b07c5ffaf67f0a55a72 /media | |
parent | 90f0413f946c241a74714b593191adab041e5283 (diff) | |
download | chromium_src-d5ef7d4bcd820355cce7fc5a1815123c882ff1fc.zip chromium_src-d5ef7d4bcd820355cce7fc5a1815123c882ff1fc.tar.gz chromium_src-d5ef7d4bcd820355cce7fc5a1815123c882ff1fc.tar.bz2 |
Use audio thread for the fake input audio stream.
Previously the fake input stream is creating a new thread to pump the callbacks, this is unnecessary and FakeAudioInputStream::Stop() might take time to stop the thread. I suspect that this is one of the potential causes for the timeout in WebRtc content browser tests on Android bots.
This patch changes the code to use AudioManager audio thread instead, hopefully this can reduce the flakiness on Android tests.
Also it changes |beep_lock| from a global lock to a lock used only by BeepContext privately.
BUG=387895
TEST=bots
Review URL: https://codereview.chromium.org/399623008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283822 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media')
-rw-r--r-- | media/audio/fake_audio_input_stream.cc | 66 | ||||
-rw-r--r-- | media/audio/fake_audio_input_stream.h | 6 |
2 files changed, 50 insertions, 22 deletions
diff --git a/media/audio/fake_audio_input_stream.cc b/media/audio/fake_audio_input_stream.cc index 384adcb..74ac579 100644 --- a/media/audio/fake_audio_input_stream.cc +++ b/media/audio/fake_audio_input_stream.cc @@ -26,11 +26,31 @@ const int kAutomaticBeepIntervalInMs = 500; // Automatic beep will be triggered every |kAutomaticBeepIntervalInMs| unless // users explicitly call BeepOnce(), which will disable the automatic beep. -struct BeepContext { - BeepContext() : beep_once(false), automatic(true) {} - base::Lock beep_lock; - bool beep_once; - bool automatic; +class BeepContext { + public: + BeepContext() : beep_once_(false), automatic_beep_(true) {} + + void SetBeepOnce(bool enable) { + base::AutoLock auto_lock(lock_); + beep_once_ = enable; + + // Disable the automatic beep if users explicit set |beep_once_| to true. + if (enable) + automatic_beep_ = false; + } + bool beep_once() const { + base::AutoLock auto_lock(lock_); + return beep_once_; + } + bool automatic_beep() const { + base::AutoLock auto_lock(lock_); + return automatic_beep_; + } + + private: + mutable base::Lock lock_; + bool beep_once_; + bool automatic_beep_; }; static base::LazyInstance<BeepContext> g_beep_context = @@ -52,7 +72,7 @@ FakeAudioInputStream::FakeAudioInputStream(AudioManagerBase* manager, params.frames_per_buffer()) / 8), params_(params), - thread_("FakeAudioRecordingThread"), + task_runner_(manager->GetTaskRunner()), callback_interval_(base::TimeDelta::FromMilliseconds( (params.frames_per_buffer() * 1000) / params.sample_rate())), beep_duration_in_buffers_(kBeepDurationMilliseconds * @@ -62,12 +82,15 @@ FakeAudioInputStream::FakeAudioInputStream(AudioManagerBase* manager, beep_generated_in_buffers_(0), beep_period_in_frames_(params.sample_rate() / kBeepFrequency), frames_elapsed_(0), - audio_bus_(AudioBus::Create(params)) { + audio_bus_(AudioBus::Create(params)), + weak_factory_(this) { + DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); } FakeAudioInputStream::~FakeAudioInputStream() {} bool FakeAudioInputStream::Open() { + DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); buffer_.reset(new uint8[buffer_size_]); memset(buffer_.get(), 0, buffer_size_); audio_bus_->Zero(); @@ -75,14 +98,13 @@ bool FakeAudioInputStream::Open() { } void FakeAudioInputStream::Start(AudioInputCallback* callback) { - DCHECK(!thread_.IsRunning()); + DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); DCHECK(!callback_); callback_ = callback; last_callback_time_ = TimeTicks::Now(); - thread_.Start(); - thread_.message_loop()->PostDelayedTask( + task_runner_->PostDelayedTask( FROM_HERE, - base::Bind(&FakeAudioInputStream::DoCallback, base::Unretained(this)), + base::Bind(&FakeAudioInputStream::DoCallback, weak_factory_.GetWeakPtr()), callback_interval_); } @@ -108,8 +130,7 @@ void FakeAudioInputStream::DoCallback() { bool should_beep = false; { BeepContext* beep_context = g_beep_context.Pointer(); - base::AutoLock auto_lock(beep_context->beep_lock); - if (beep_context->automatic) { + if (beep_context->automatic_beep()) { base::TimeDelta delta = interval_from_last_beep_ - TimeDelta::FromMilliseconds(kAutomaticBeepIntervalInMs); if (delta > base::TimeDelta()) { @@ -117,8 +138,8 @@ void FakeAudioInputStream::DoCallback() { interval_from_last_beep_ = delta; } } else { - should_beep = beep_context->beep_once; - beep_context->beep_once = false; + should_beep = beep_context->beep_once(); + beep_context->SetBeepOnce(false); } } @@ -151,29 +172,34 @@ void FakeAudioInputStream::DoCallback() { callback_->OnData(this, audio_bus_.get(), buffer_size_, 1.0); frames_elapsed_ += params_.frames_per_buffer(); - thread_.message_loop()->PostDelayedTask( + task_runner_->PostDelayedTask( FROM_HERE, - base::Bind(&FakeAudioInputStream::DoCallback, base::Unretained(this)), + base::Bind(&FakeAudioInputStream::DoCallback, weak_factory_.GetWeakPtr()), next_callback_time); } void FakeAudioInputStream::Stop() { - thread_.Stop(); + DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); + weak_factory_.InvalidateWeakPtrs(); callback_ = NULL; } void FakeAudioInputStream::Close() { + DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); audio_manager_->ReleaseInputStream(this); } double FakeAudioInputStream::GetMaxVolume() { + DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); return 1.0; } void FakeAudioInputStream::SetVolume(double volume) { + DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); } double FakeAudioInputStream::GetVolume() { + DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); return 1.0; } @@ -186,9 +212,7 @@ bool FakeAudioInputStream::GetAutomaticGainControl() { // static void FakeAudioInputStream::BeepOnce() { BeepContext* beep_context = g_beep_context.Pointer(); - base::AutoLock auto_lock(beep_context->beep_lock); - beep_context->beep_once = true; - beep_context->automatic = false; + beep_context->SetBeepOnce(true); } } // namespace media diff --git a/media/audio/fake_audio_input_stream.h b/media/audio/fake_audio_input_stream.h index e6c625e..4c3c24c 100644 --- a/media/audio/fake_audio_input_stream.h +++ b/media/audio/fake_audio_input_stream.h @@ -61,7 +61,7 @@ class MEDIA_EXPORT FakeAudioInputStream scoped_ptr<uint8[]> buffer_; int buffer_size_; AudioParameters params_; - base::Thread thread_; + const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; base::TimeTicks last_callback_time_; base::TimeDelta callback_interval_; base::TimeDelta interval_from_last_beep_; @@ -71,6 +71,10 @@ class MEDIA_EXPORT FakeAudioInputStream int frames_elapsed_; scoped_ptr<media::AudioBus> audio_bus_; + // Allows us to run tasks on the FakeAudioInputStream instance which are + // bound by its lifetime. + base::WeakPtrFactory<FakeAudioInputStream> weak_factory_; + DISALLOW_COPY_AND_ASSIGN(FakeAudioInputStream); }; |