Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "media/audio/audio_output_dispatcher.h" | 5 #include "media/audio/audio_output_dispatcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 8 #include "base/compiler_specific.h" | |
| 9 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 10 #include "base/time.h" | |
| 11 #include "media/audio/audio_io.h" | |
| 12 | 8 |
| 13 AudioOutputDispatcher::AudioOutputDispatcher( | 9 AudioOutputDispatcher::AudioOutputDispatcher( |
| 14 AudioManager* audio_manager, const AudioParameters& params, | 10 AudioManager* audio_manager, |
| 15 base::TimeDelta close_delay) | 11 const AudioParameters& params) |
| 16 : audio_manager_(audio_manager), | 12 : audio_manager_(audio_manager), |
| 17 message_loop_(MessageLoop::current()), | 13 message_loop_(MessageLoop::current()), |
| 18 params_(params), | 14 params_(params) { |
| 19 pause_delay_(base::TimeDelta::FromMilliseconds( | |
| 20 2 * params.frames_per_buffer() * | |
| 21 base::Time::kMillisecondsPerSecond / params.sample_rate())), | |
| 22 paused_proxies_(0), | |
| 23 ALLOW_THIS_IN_INITIALIZER_LIST(weak_this_(this)), | |
| 24 close_timer_(FROM_HERE, | |
| 25 close_delay, | |
| 26 weak_this_.GetWeakPtr(), | |
| 27 &AudioOutputDispatcher::ClosePendingStreams) { | |
| 28 // We expect to be instantiated on the audio thread. Otherwise the | 15 // We expect to be instantiated on the audio thread. Otherwise the |
| 29 // message_loop_ member will point to the wrong message loop! | 16 // message_loop_ member will point to the wrong message loop! |
| 30 DCHECK(audio_manager->GetMessageLoop()->BelongsToCurrentThread()); | 17 DCHECK(audio_manager->GetMessageLoop()->BelongsToCurrentThread()); |
| 31 } | 18 } |
| 32 | 19 |
| 33 AudioOutputDispatcher::~AudioOutputDispatcher() { | 20 AudioOutputDispatcher::~AudioOutputDispatcher() { |
| 34 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
|
vrk (LEFT CHROMIUM)
2012/04/06 23:37:05
Why is this DCHECK deleted?
enal1
2012/04/16 22:01:35
Done.
| |
| 35 } | 21 } |
| 36 | |
| 37 bool AudioOutputDispatcher::StreamOpened() { | |
| 38 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
| 39 paused_proxies_++; | |
| 40 | |
| 41 // Ensure that there is at least one open stream. | |
| 42 if (idle_streams_.empty() && !CreateAndOpenStream()) { | |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 close_timer_.Reset(); | |
| 47 | |
| 48 return true; | |
| 49 } | |
| 50 | |
| 51 AudioOutputStream* AudioOutputDispatcher::StreamStarted() { | |
| 52 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
| 53 | |
| 54 if (idle_streams_.empty() && !CreateAndOpenStream()) { | |
| 55 return NULL; | |
| 56 } | |
| 57 | |
| 58 AudioOutputStream* stream = idle_streams_.back(); | |
| 59 idle_streams_.pop_back(); | |
| 60 | |
| 61 DCHECK_GT(paused_proxies_, 0u); | |
| 62 paused_proxies_--; | |
| 63 | |
| 64 close_timer_.Reset(); | |
| 65 | |
| 66 // Schedule task to allocate streams for other proxies if we need to. | |
| 67 message_loop_->PostTask(FROM_HERE, base::Bind( | |
| 68 &AudioOutputDispatcher::OpenTask, weak_this_.GetWeakPtr())); | |
| 69 | |
| 70 return stream; | |
| 71 } | |
| 72 | |
| 73 void AudioOutputDispatcher::StreamStopped(AudioOutputStream* stream) { | |
| 74 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
| 75 | |
| 76 paused_proxies_++; | |
| 77 | |
| 78 pausing_streams_.push_front(stream); | |
| 79 | |
| 80 // Don't recycle stream until two buffers worth of time has elapsed. | |
| 81 message_loop_->PostDelayedTask( | |
| 82 FROM_HERE, | |
| 83 base::Bind(&AudioOutputDispatcher::StopStreamTask, | |
| 84 weak_this_.GetWeakPtr()), | |
| 85 pause_delay_); | |
| 86 } | |
| 87 | |
| 88 void AudioOutputDispatcher::StopStreamTask() { | |
| 89 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
| 90 | |
| 91 if (pausing_streams_.empty()) | |
| 92 return; | |
| 93 | |
| 94 AudioOutputStream* stream = pausing_streams_.back(); | |
| 95 pausing_streams_.pop_back(); | |
| 96 idle_streams_.push_back(stream); | |
| 97 close_timer_.Reset(); | |
| 98 } | |
| 99 | |
| 100 void AudioOutputDispatcher::StreamClosed() { | |
| 101 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
| 102 | |
| 103 while (!pausing_streams_.empty()) { | |
| 104 idle_streams_.push_back(pausing_streams_.back()); | |
| 105 pausing_streams_.pop_back(); | |
| 106 } | |
| 107 | |
| 108 DCHECK_GT(paused_proxies_, 0u); | |
| 109 paused_proxies_--; | |
| 110 | |
| 111 while (idle_streams_.size() > paused_proxies_) { | |
| 112 idle_streams_.back()->Close(); | |
| 113 idle_streams_.pop_back(); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 void AudioOutputDispatcher::Shutdown() { | |
| 118 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
| 119 | |
| 120 // Cancel any pending tasks to close paused streams or create new ones. | |
| 121 weak_this_.InvalidateWeakPtrs(); | |
| 122 | |
| 123 // No AudioOutputProxy objects should hold a reference to us when we get | |
| 124 // to this stage. | |
| 125 DCHECK(HasOneRef()) << "Only the AudioManager should hold a reference"; | |
| 126 | |
| 127 AudioOutputStreamList::iterator it = idle_streams_.begin(); | |
| 128 for (; it != idle_streams_.end(); ++it) | |
| 129 (*it)->Close(); | |
| 130 idle_streams_.clear(); | |
| 131 | |
| 132 it = pausing_streams_.begin(); | |
| 133 for (; it != pausing_streams_.end(); ++it) | |
| 134 (*it)->Close(); | |
| 135 pausing_streams_.clear(); | |
| 136 } | |
| 137 | |
| 138 bool AudioOutputDispatcher::CreateAndOpenStream() { | |
| 139 AudioOutputStream* stream = audio_manager_->MakeAudioOutputStream(params_); | |
| 140 if (!stream) | |
| 141 return false; | |
| 142 | |
| 143 if (!stream->Open()) { | |
| 144 stream->Close(); | |
| 145 return false; | |
| 146 } | |
| 147 idle_streams_.push_back(stream); | |
| 148 return true; | |
| 149 } | |
| 150 | |
| 151 void AudioOutputDispatcher::OpenTask() { | |
| 152 // Make sure that we have at least one stream allocated if there | |
| 153 // are paused streams. | |
| 154 if (paused_proxies_ > 0 && idle_streams_.empty() && | |
| 155 pausing_streams_.empty()) { | |
| 156 CreateAndOpenStream(); | |
| 157 } | |
| 158 | |
| 159 close_timer_.Reset(); | |
| 160 } | |
| 161 | |
| 162 // This method is called by |close_timer_|. | |
| 163 void AudioOutputDispatcher::ClosePendingStreams() { | |
| 164 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
| 165 | |
| 166 while (!idle_streams_.empty()) { | |
| 167 idle_streams_.back()->Close(); | |
| 168 idle_streams_.pop_back(); | |
| 169 } | |
| 170 } | |
| OLD | NEW |