Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(723)

Unified Diff: media/audio/audio_output_dispatcher.cc

Issue 6822019: Fix erratic HTML5 audio playback (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed nits Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..7b7ebd52d56862cd13cb2c9c2dc036f713e2983e 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 *
Sergey Ulanov 2011/04/12 05:09:25 nit: remove space before 2
+ base::Time::kMillisecondsPerSecond / params.sample_rate),
paused_proxies_(0),
ALLOW_THIS_IN_INITIALIZER_LIST(close_timer_(
base::TimeDelta::FromMilliseconds(close_delay_ms),
@@ -28,7 +31,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 +43,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,8 +64,24 @@ 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);
+
+ // Don't recycle stream until two buffers worth of time has elapsed
+ 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);
close_timer_.Reset();
}
@@ -72,9 +91,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 +111,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 +127,12 @@ void AudioOutputDispatcher::OpenTask() {
// This method is called by |close_timer_|.
void AudioOutputDispatcher::ClosePendingStreams() {
- while (!streams_.empty()) {
- streams_.back()->Close();
- streams_.pop_back();
+ while (!pausing_streams_.empty()) {
+ pausing_streams_.back()->Close();
+ pausing_streams_.pop_back();
Sergey Ulanov 2011/04/12 05:09:25 If you close |pausing_streams_| here you may be cl
+ }
+ while (!idle_streams_.empty()) {
+ idle_streams_.back()->Close();
+ idle_streams_.pop_back();
}
}

Powered by Google App Engine
This is Rietveld 408576698