| 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/base/audio_renderer_mixer.h" | 5 #include "media/base/audio_renderer_mixer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 | 10 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 AudioRendererMixer::~AudioRendererMixer() { | 28 AudioRendererMixer::~AudioRendererMixer() { |
| 29 // AudioRendererSinks must be stopped before being destructed. | 29 // AudioRendererSinks must be stopped before being destructed. |
| 30 audio_sink_->Stop(); | 30 audio_sink_->Stop(); |
| 31 | 31 |
| 32 // Ensures that all mixer inputs have stopped themselves prior to destruction | 32 // Ensures that all mixer inputs have stopped themselves prior to destruction |
| 33 // and have called RemoveMixerInput(). | 33 // and have called RemoveMixerInput(). |
| 34 DCHECK_EQ(mixer_inputs_.size(), 0U); | 34 DCHECK_EQ(mixer_inputs_.size(), 0U); |
| 35 } | 35 } |
| 36 | 36 |
| 37 void AudioRendererMixer::AddMixerInput( | 37 void AudioRendererMixer::AddMixerInput(AudioConverter::InputCallback* input, |
| 38 const scoped_refptr<AudioRendererMixerInput>& input) { | 38 const base::Closure& error_cb) { |
| 39 base::AutoLock auto_lock(mixer_inputs_lock_); | 39 base::AutoLock auto_lock(mixer_inputs_lock_); |
| 40 | 40 |
| 41 if (!playing_) { | 41 if (!playing_) { |
| 42 playing_ = true; | 42 playing_ = true; |
| 43 last_play_time_ = base::Time::Now(); | 43 last_play_time_ = base::Time::Now(); |
| 44 audio_sink_->Play(); | 44 audio_sink_->Play(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 mixer_inputs_.push_back(input); | 47 DCHECK(mixer_inputs_.find(input) == mixer_inputs_.end()); |
| 48 mixer_inputs_[input] = error_cb; |
| 48 audio_converter_.AddInput(input); | 49 audio_converter_.AddInput(input); |
| 49 } | 50 } |
| 50 | 51 |
| 51 void AudioRendererMixer::RemoveMixerInput( | 52 void AudioRendererMixer::RemoveMixerInput( |
| 52 const scoped_refptr<AudioRendererMixerInput>& input) { | 53 AudioConverter::InputCallback* input) { |
| 53 base::AutoLock auto_lock(mixer_inputs_lock_); | 54 base::AutoLock auto_lock(mixer_inputs_lock_); |
| 54 audio_converter_.RemoveInput(input); | 55 audio_converter_.RemoveInput(input); |
| 55 mixer_inputs_.remove(input); | 56 |
| 57 DCHECK(mixer_inputs_.find(input) != mixer_inputs_.end()); |
| 58 mixer_inputs_.erase(input); |
| 56 } | 59 } |
| 57 | 60 |
| 58 int AudioRendererMixer::Render(AudioBus* audio_bus, | 61 int AudioRendererMixer::Render(AudioBus* audio_bus, |
| 59 int audio_delay_milliseconds) { | 62 int audio_delay_milliseconds) { |
| 60 base::AutoLock auto_lock(mixer_inputs_lock_); | 63 base::AutoLock auto_lock(mixer_inputs_lock_); |
| 61 | 64 |
| 62 // If there are no mixer inputs and we haven't seen one for a while, pause the | 65 // If there are no mixer inputs and we haven't seen one for a while, pause the |
| 63 // sink to avoid wasting resources when media elements are present but remain | 66 // sink to avoid wasting resources when media elements are present but remain |
| 64 // in the pause state. | 67 // in the pause state. |
| 65 base::Time now = base::Time::Now(); | 68 base::Time now = base::Time::Now(); |
| 66 if (!mixer_inputs_.empty()) { | 69 if (!mixer_inputs_.empty()) { |
| 67 last_play_time_ = now; | 70 last_play_time_ = now; |
| 68 } else if (now - last_play_time_ >= pause_delay_ && playing_) { | 71 } else if (now - last_play_time_ >= pause_delay_ && playing_) { |
| 69 audio_sink_->Pause(); | 72 audio_sink_->Pause(); |
| 70 playing_ = false; | 73 playing_ = false; |
| 71 } | 74 } |
| 72 | 75 |
| 73 // Set the delay information for each mixer input. | 76 audio_converter_.ConvertWithDelay( |
| 74 for (AudioRendererMixerInputSet::iterator it = mixer_inputs_.begin(); | 77 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds), audio_bus); |
| 75 it != mixer_inputs_.end(); ++it) { | |
| 76 (*it)->set_audio_delay_milliseconds(audio_delay_milliseconds); | |
| 77 } | |
| 78 | |
| 79 audio_converter_.Convert(audio_bus); | |
| 80 return audio_bus->frames(); | 78 return audio_bus->frames(); |
| 81 } | 79 } |
| 82 | 80 |
| 83 void AudioRendererMixer::OnRenderError() { | 81 void AudioRendererMixer::OnRenderError() { |
| 84 base::AutoLock auto_lock(mixer_inputs_lock_); | 82 base::AutoLock auto_lock(mixer_inputs_lock_); |
| 85 | 83 |
| 86 // Call each mixer input and signal an error. | 84 // Call each mixer input and signal an error. |
| 87 for (AudioRendererMixerInputSet::iterator it = mixer_inputs_.begin(); | 85 for (AudioRendererMixerInputSet::iterator it = mixer_inputs_.begin(); |
| 88 it != mixer_inputs_.end(); ++it) { | 86 it != mixer_inputs_.end(); ++it) { |
| 89 (*it)->OnRenderError(); | 87 it->second.Run(); |
| 90 } | 88 } |
| 91 } | 89 } |
| 92 | 90 |
| 93 } // namespace media | 91 } // namespace media |
| OLD | NEW |