OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/audio/audio_output_dispatcher.h" |
| 6 |
| 7 #include "base/compiler_specific.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "media/audio/audio_io.h" |
| 10 |
| 11 AudioOutputDispatcher::AudioOutputDispatcher( |
| 12 AudioManager* audio_manager, const AudioParameters& params, |
| 13 int close_delay_ms) |
| 14 : audio_manager_(audio_manager), |
| 15 message_loop_(audio_manager->GetMessageLoop()), |
| 16 params_(params), |
| 17 paused_proxies_(0), |
| 18 ALLOW_THIS_IN_INITIALIZER_LIST(close_timer_( |
| 19 base::TimeDelta::FromMilliseconds(close_delay_ms), |
| 20 this, &AudioOutputDispatcher::ClosePendingStreams)) { |
| 21 } |
| 22 |
| 23 AudioOutputDispatcher::~AudioOutputDispatcher() { |
| 24 } |
| 25 |
| 26 bool AudioOutputDispatcher::StreamOpened() { |
| 27 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 28 paused_proxies_++; |
| 29 |
| 30 // Ensure that there is at least one open stream. |
| 31 if (streams_.empty() && !CreateAndOpenStream()) { |
| 32 return false; |
| 33 } |
| 34 |
| 35 close_timer_.Reset(); |
| 36 |
| 37 return true; |
| 38 } |
| 39 |
| 40 AudioOutputStream* AudioOutputDispatcher::StreamStarted() { |
| 41 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 42 |
| 43 if (streams_.empty() && !CreateAndOpenStream()) { |
| 44 return NULL; |
| 45 } |
| 46 |
| 47 AudioOutputStream* stream = streams_.back(); |
| 48 streams_.pop_back(); |
| 49 |
| 50 DCHECK_GT(paused_proxies_, 0u); |
| 51 paused_proxies_--; |
| 52 |
| 53 close_timer_.Reset(); |
| 54 |
| 55 // Schedule task to allocate streams for other proxies if we need to. |
| 56 message_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 57 this, &AudioOutputDispatcher::OpenTask)); |
| 58 |
| 59 return stream; |
| 60 } |
| 61 |
| 62 void AudioOutputDispatcher::StreamStopped(AudioOutputStream* stream) { |
| 63 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 64 paused_proxies_++; |
| 65 streams_.push_back(stream); |
| 66 close_timer_.Reset(); |
| 67 } |
| 68 |
| 69 void AudioOutputDispatcher::StreamClosed() { |
| 70 DCHECK_EQ(MessageLoop::current(), message_loop_); |
| 71 |
| 72 DCHECK_GT(paused_proxies_, 0u); |
| 73 paused_proxies_--; |
| 74 |
| 75 while (streams_.size() > paused_proxies_) { |
| 76 streams_.back()->Close(); |
| 77 streams_.pop_back(); |
| 78 } |
| 79 } |
| 80 |
| 81 MessageLoop* AudioOutputDispatcher::message_loop() { |
| 82 return message_loop_; |
| 83 } |
| 84 |
| 85 bool AudioOutputDispatcher::CreateAndOpenStream() { |
| 86 AudioOutputStream* stream = |
| 87 audio_manager_->MakeAudioOutputStream(params_); |
| 88 if (!stream) { |
| 89 return false; |
| 90 } |
| 91 if (!stream->Open()) { |
| 92 stream->Close(); |
| 93 return false; |
| 94 } |
| 95 streams_.push_back(stream); |
| 96 return true; |
| 97 } |
| 98 |
| 99 void AudioOutputDispatcher::OpenTask() { |
| 100 // Make sure that we have at least one stream allocated if there |
| 101 // are paused streams. |
| 102 if (paused_proxies_ > 0 && streams_.empty()) { |
| 103 CreateAndOpenStream(); |
| 104 } |
| 105 |
| 106 close_timer_.Reset(); |
| 107 } |
| 108 |
| 109 // This method is called by |close_timer_|. |
| 110 void AudioOutputDispatcher::ClosePendingStreams() { |
| 111 while (!streams_.empty()) { |
| 112 streams_.back()->Close(); |
| 113 streams_.pop_back(); |
| 114 } |
| 115 } |
OLD | NEW |