Chromium Code Reviews| Index: media/audio/audio_output_dispatcher.cc |
| diff --git a/media/audio/audio_output_dispatcher.cc b/media/audio/audio_output_dispatcher.cc |
| index 3f9d848a2c9d1762155e7062aee36f479d6563d0..d771bf3b102ee2012e5bfcd8d6d5b31101afd76f 100644 |
| --- a/media/audio/audio_output_dispatcher.cc |
| +++ b/media/audio/audio_output_dispatcher.cc |
| @@ -1,4 +1,4 @@ |
| -// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| +// Copyright (c) 2011 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. |
| @@ -6,6 +6,7 @@ |
| #include "base/compiler_specific.h" |
| #include "base/message_loop.h" |
| +#include "base/time.h" |
| #include "media/audio/audio_io.h" |
| AudioOutputDispatcher::AudioOutputDispatcher( |
| @@ -14,6 +15,8 @@ AudioOutputDispatcher::AudioOutputDispatcher( |
| : audio_manager_(audio_manager), |
| message_loop_(audio_manager->GetMessageLoop()), |
| params_(params), |
| + pause_delay_milliseconds_(2 * params.samples_per_packet * |
| + base::Time::kMillisecondsPerSecond / params.sample_rate), |
| paused_proxies_(0), |
| ALLOW_THIS_IN_INITIALIZER_LIST(close_timer_( |
| base::TimeDelta::FromMilliseconds(close_delay_ms), |
| @@ -21,6 +24,10 @@ AudioOutputDispatcher::AudioOutputDispatcher( |
| } |
| AudioOutputDispatcher::~AudioOutputDispatcher() { |
| + while (!pausing_streams_.empty()) { |
| + pausing_streams_.back()->Close(); |
| + pausing_streams_.pop_back(); |
| + } |
| } |
| bool AudioOutputDispatcher::StreamOpened() { |
| @@ -28,7 +35,7 @@ bool AudioOutputDispatcher::StreamOpened() { |
| paused_proxies_++; |
| // Ensure that there is at least one open stream. |
| - if (streams_.empty() && !CreateAndOpenStream()) { |
| + if (idle_streams_.empty() && !CreateAndOpenStream()) { |
| return false; |
| } |
| @@ -40,12 +47,12 @@ bool AudioOutputDispatcher::StreamOpened() { |
| AudioOutputStream* AudioOutputDispatcher::StreamStarted() { |
| DCHECK_EQ(MessageLoop::current(), message_loop_); |
| - if (streams_.empty() && !CreateAndOpenStream()) { |
| + if (idle_streams_.empty() && !CreateAndOpenStream()) { |
| return NULL; |
| } |
| - AudioOutputStream* stream = streams_.back(); |
| - streams_.pop_back(); |
| + AudioOutputStream* stream = idle_streams_.back(); |
| + idle_streams_.pop_back(); |
| DCHECK_GT(paused_proxies_, 0u); |
| paused_proxies_--; |
| @@ -61,9 +68,25 @@ AudioOutputStream* AudioOutputDispatcher::StreamStarted() { |
| void AudioOutputDispatcher::StreamStopped(AudioOutputStream* stream) { |
| DCHECK_EQ(MessageLoop::current(), message_loop_); |
| + |
| paused_proxies_++; |
| - streams_.push_back(stream); |
| + |
| + pausing_streams_.push_front(stream); |
| close_timer_.Reset(); |
| + |
| + // Don't recycle stream until two buffers worth of time has elapsed |
|
Sergey Ulanov
2011/04/13 02:02:54
nit: period at the end of the sentence.
|
| + message_loop_->PostDelayedTask( |
| + FROM_HERE, |
| + NewRunnableMethod(this, &AudioOutputDispatcher::StopStreamTask), |
| + pause_delay_milliseconds_); |
| +} |
| + |
| +void AudioOutputDispatcher::StopStreamTask() { |
| + if (pausing_streams_.empty()) |
| + return; |
| + AudioOutputStream* stream = pausing_streams_.back(); |
| + pausing_streams_.pop_back(); |
| + idle_streams_.push_back(stream); |
| } |
| void AudioOutputDispatcher::StreamClosed() { |
| @@ -72,9 +95,9 @@ void AudioOutputDispatcher::StreamClosed() { |
| DCHECK_GT(paused_proxies_, 0u); |
| paused_proxies_--; |
| - while (streams_.size() > paused_proxies_) { |
| - streams_.back()->Close(); |
| - streams_.pop_back(); |
| + while (idle_streams_.size() > paused_proxies_) { |
| + idle_streams_.back()->Close(); |
| + idle_streams_.pop_back(); |
| } |
| } |
| @@ -92,14 +115,14 @@ bool AudioOutputDispatcher::CreateAndOpenStream() { |
| stream->Close(); |
| return false; |
| } |
| - streams_.push_back(stream); |
| + idle_streams_.push_back(stream); |
| return true; |
| } |
| void AudioOutputDispatcher::OpenTask() { |
| // Make sure that we have at least one stream allocated if there |
| // are paused streams. |
| - if (paused_proxies_ > 0 && streams_.empty()) { |
| + if (paused_proxies_ > 0 && idle_streams_.empty()) { |
| CreateAndOpenStream(); |
| } |
| @@ -108,8 +131,8 @@ void AudioOutputDispatcher::OpenTask() { |
| // This method is called by |close_timer_|. |
| void AudioOutputDispatcher::ClosePendingStreams() { |
| - while (!streams_.empty()) { |
| - streams_.back()->Close(); |
| - streams_.pop_back(); |
| + while (!idle_streams_.empty()) { |
| + idle_streams_.back()->Close(); |
| + idle_streams_.pop_back(); |
| } |
| } |