OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_controller.h" | 5 #include "media/audio/audio_output_controller.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... |
26 state_(kEmpty), | 26 state_(kEmpty), |
27 buffer_(0, capacity), | 27 buffer_(0, capacity), |
28 pending_request_(false), | 28 pending_request_(false), |
29 sync_reader_(sync_reader), | 29 sync_reader_(sync_reader), |
30 message_loop_(NULL), | 30 message_loop_(NULL), |
31 number_polling_attempts_left_(0) { | 31 number_polling_attempts_left_(0) { |
32 } | 32 } |
33 | 33 |
34 AudioOutputController::~AudioOutputController() { | 34 AudioOutputController::~AudioOutputController() { |
35 DCHECK_EQ(kClosed, state_); | 35 DCHECK_EQ(kClosed, state_); |
| 36 StopCloseAndClearStream(); |
36 } | 37 } |
37 | 38 |
38 // static | 39 // static |
39 scoped_refptr<AudioOutputController> AudioOutputController::Create( | 40 scoped_refptr<AudioOutputController> AudioOutputController::Create( |
40 EventHandler* event_handler, | 41 EventHandler* event_handler, |
41 const AudioParameters& params, | 42 const AudioParameters& params, |
42 uint32 buffer_capacity) { | 43 uint32 buffer_capacity) { |
43 | 44 |
44 if (!params.IsValid()) | 45 if (!params.IsValid()) |
45 return NULL; | 46 return NULL; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 DCHECK_EQ(message_loop_, MessageLoop::current()); | 132 DCHECK_EQ(message_loop_, MessageLoop::current()); |
132 | 133 |
133 // Close() can be called before DoCreate() is executed. | 134 // Close() can be called before DoCreate() is executed. |
134 if (state_ == kClosed) | 135 if (state_ == kClosed) |
135 return; | 136 return; |
136 DCHECK_EQ(kEmpty, state_); | 137 DCHECK_EQ(kEmpty, state_); |
137 | 138 |
138 if (!AudioManager::GetAudioManager()) | 139 if (!AudioManager::GetAudioManager()) |
139 return; | 140 return; |
140 | 141 |
| 142 StopCloseAndClearStream(); |
141 stream_ = AudioManager::GetAudioManager()->MakeAudioOutputStreamProxy(params); | 143 stream_ = AudioManager::GetAudioManager()->MakeAudioOutputStreamProxy(params); |
142 if (!stream_) { | 144 if (!stream_) { |
143 // TODO(hclam): Define error types. | 145 // TODO(hclam): Define error types. |
144 handler_->OnError(this, 0); | 146 handler_->OnError(this, 0); |
145 return; | 147 return; |
146 } | 148 } |
147 | 149 |
148 if (!stream_->Open()) { | 150 if (!stream_->Open()) { |
149 stream_->Close(); | 151 StopCloseAndClearStream(); |
150 stream_ = NULL; | |
151 | 152 |
152 // TODO(hclam): Define error types. | 153 // TODO(hclam): Define error types. |
153 handler_->OnError(this, 0); | 154 handler_->OnError(this, 0); |
154 return; | 155 return; |
155 } | 156 } |
156 | 157 |
157 // We have successfully opened the stream. Set the initial volume. | 158 // We have successfully opened the stream. Set the initial volume. |
158 stream_->SetVolume(volume_); | 159 stream_->SetVolume(volume_); |
159 | 160 |
160 // Finally set the state to kCreated. | 161 // Finally set the state to kCreated. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 stream_->Start(this); | 220 stream_->Start(this); |
220 | 221 |
221 // Tell the event handler that we are now playing. | 222 // Tell the event handler that we are now playing. |
222 handler_->OnPlaying(this); | 223 handler_->OnPlaying(this); |
223 } | 224 } |
224 | 225 |
225 void AudioOutputController::DoPause() { | 226 void AudioOutputController::DoPause() { |
226 DCHECK_EQ(message_loop_, MessageLoop::current()); | 227 DCHECK_EQ(message_loop_, MessageLoop::current()); |
227 | 228 |
228 // We can pause from started state. | 229 // We can pause from started state. |
229 if (state_ != kPlaying) | 230 if (state_ != kPlaying) { |
| 231 if (stream_) |
| 232 stream_->Stop(); |
230 return; | 233 return; |
| 234 } |
231 state_ = kPaused; | 235 state_ = kPaused; |
232 | 236 |
233 // Then we stop the audio device. This is not the perfect solution because | 237 // Then we stop the audio device. This is not the perfect solution because |
234 // it discards all the internal buffer in the audio device. | 238 // it discards all the internal buffer in the audio device. |
235 // TODO(hclam): Actually pause the audio device. | 239 // TODO(hclam): Actually pause the audio device. |
236 stream_->Stop(); | 240 stream_->Stop(); |
237 | 241 |
238 if (LowLatencyMode()) { | 242 if (LowLatencyMode()) { |
239 // Send a special pause mark to the low-latency audio thread. | 243 // Send a special pause mark to the low-latency audio thread. |
240 sync_reader_->UpdatePendingBytes(kPauseMark); | 244 sync_reader_->UpdatePendingBytes(kPauseMark); |
(...skipping 13 matching lines...) Expand all Loading... |
254 return; | 258 return; |
255 base::AutoLock auto_lock(lock_); | 259 base::AutoLock auto_lock(lock_); |
256 buffer_.Clear(); | 260 buffer_.Clear(); |
257 } | 261 } |
258 } | 262 } |
259 | 263 |
260 void AudioOutputController::DoClose(const base::Closure& closed_task) { | 264 void AudioOutputController::DoClose(const base::Closure& closed_task) { |
261 DCHECK_EQ(message_loop_, MessageLoop::current()); | 265 DCHECK_EQ(message_loop_, MessageLoop::current()); |
262 | 266 |
263 if (state_ != kClosed) { | 267 if (state_ != kClosed) { |
264 // |stream_| can be null if creating the device failed in DoCreate(). | 268 StopCloseAndClearStream(); |
265 if (stream_) { | |
266 stream_->Stop(); | |
267 stream_->Close(); | |
268 // After stream is closed it is destroyed, so don't keep a reference to | |
269 // it. | |
270 stream_ = NULL; | |
271 } | |
272 | 269 |
273 if (LowLatencyMode()) { | 270 if (LowLatencyMode()) { |
274 sync_reader_->Close(); | 271 sync_reader_->Close(); |
275 } | 272 } |
276 | 273 |
277 state_ = kClosed; | 274 state_ = kClosed; |
278 } | 275 } |
279 | 276 |
280 closed_task.Run(); | 277 closed_task.Run(); |
281 } | 278 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 AudioBuffersState buffers_state = buffers_state_; | 352 AudioBuffersState buffers_state = buffers_state_; |
356 buffers_state.pending_bytes += buffer_.forward_bytes(); | 353 buffers_state.pending_bytes += buffer_.forward_bytes(); |
357 | 354 |
358 // If we need more data then call the event handler to ask for more data. | 355 // If we need more data then call the event handler to ask for more data. |
359 // It is okay that we don't lock in this block because the parameters are | 356 // It is okay that we don't lock in this block because the parameters are |
360 // correct and in the worst case we are just asking more data than needed. | 357 // correct and in the worst case we are just asking more data than needed. |
361 base::AutoUnlock auto_unlock(lock_); | 358 base::AutoUnlock auto_unlock(lock_); |
362 handler_->OnMoreData(this, buffers_state); | 359 handler_->OnMoreData(this, buffers_state); |
363 } | 360 } |
364 | 361 |
| 362 void AudioOutputController::StopCloseAndClearStream() { |
| 363 // Allow calling unconditionally and bail if we don't have a stream_ to close. |
| 364 if (!stream_) |
| 365 return; |
| 366 stream_->Stop(); |
| 367 stream_->Close(); |
| 368 stream_ = NULL; |
| 369 } |
| 370 |
365 } // namespace media | 371 } // namespace media |
OLD | NEW |