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/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 13 matching lines...) Expand all Loading... | |
| 24 audio_sink_->Initialize(output_params, this); | 24 audio_sink_->Initialize(output_params, this); |
| 25 audio_sink_->Start(); | 25 audio_sink_->Start(); |
| 26 } | 26 } |
| 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(error_callbacks_.size(), 0U); |
| 35 } | 35 } |
| 36 | 36 |
| 37 void AudioRendererMixer::AddMixerInput(AudioConverter::InputCallback* input, | 37 void AudioRendererMixer::AddMixerInput(AudioConverter::InputCallback* input) { |
| 38 const base::Closure& error_cb) { | 38 base::AutoLock auto_lock(lock_); |
| 39 base::AutoLock auto_lock(mixer_inputs_lock_); | |
| 40 | |
| 41 if (!playing_) { | 39 if (!playing_) { |
| 42 playing_ = true; | 40 playing_ = true; |
| 43 last_play_time_ = base::TimeTicks::Now(); | 41 last_play_time_ = base::TimeTicks::Now(); |
| 44 audio_sink_->Play(); | 42 audio_sink_->Play(); |
| 45 } | 43 } |
| 46 | 44 |
| 47 DCHECK(mixer_inputs_.find(input) == mixer_inputs_.end()); | 45 DCHECK(error_callbacks_.find(input) != error_callbacks_.end()); |
| 48 mixer_inputs_[input] = error_cb; | |
| 49 audio_converter_.AddInput(input); | 46 audio_converter_.AddInput(input); |
| 50 } | 47 } |
| 51 | 48 |
| 52 void AudioRendererMixer::RemoveMixerInput( | 49 void AudioRendererMixer::RemoveMixerInput( |
| 53 AudioConverter::InputCallback* input) { | 50 AudioConverter::InputCallback* input) { |
| 54 base::AutoLock auto_lock(mixer_inputs_lock_); | 51 base::AutoLock auto_lock(lock_); |
| 55 audio_converter_.RemoveInput(input); | 52 audio_converter_.RemoveInput(input); |
| 53 } | |
| 56 | 54 |
| 57 DCHECK(mixer_inputs_.find(input) != mixer_inputs_.end()); | 55 void AudioRendererMixer::RegisterErrorCallback( |
| 58 mixer_inputs_.erase(input); | 56 AudioConverter::InputCallback* input, |
|
scherkus (not reviewing)
2014/06/03 02:01:31
would using a map/list/vector work? there doesn't
DaleCurtis
2014/06/03 20:26:19
This is using a map so I'm not sure what you're as
| |
| 57 const base::Closure& error_cb) { | |
| 58 base::AutoLock auto_lock(lock_); | |
| 59 DCHECK(error_callbacks_.find(input) == error_callbacks_.end()); | |
| 60 error_callbacks_[input] = error_cb; | |
| 61 } | |
| 62 | |
| 63 void AudioRendererMixer::UnregisterErrorCallback( | |
| 64 AudioConverter::InputCallback* input) { | |
| 65 base::AutoLock auto_lock(lock_); | |
| 66 DCHECK(error_callbacks_.find(input) != error_callbacks_.end()); | |
| 67 error_callbacks_.erase(input); | |
| 59 } | 68 } |
| 60 | 69 |
| 61 int AudioRendererMixer::Render(AudioBus* audio_bus, | 70 int AudioRendererMixer::Render(AudioBus* audio_bus, |
| 62 int audio_delay_milliseconds) { | 71 int audio_delay_milliseconds) { |
| 63 base::AutoLock auto_lock(mixer_inputs_lock_); | 72 base::AutoLock auto_lock(lock_); |
| 64 | 73 |
| 65 // If there are no mixer inputs and we haven't seen one for a while, pause the | 74 // If there are no mixer inputs and we haven't seen one for a while, pause the |
| 66 // sink to avoid wasting resources when media elements are present but remain | 75 // sink to avoid wasting resources when media elements are present but remain |
| 67 // in the pause state. | 76 // in the pause state. |
| 68 const base::TimeTicks now = base::TimeTicks::Now(); | 77 const base::TimeTicks now = base::TimeTicks::Now(); |
| 69 if (!mixer_inputs_.empty()) { | 78 if (!audio_converter_.empty()) { |
| 70 last_play_time_ = now; | 79 last_play_time_ = now; |
| 71 } else if (now - last_play_time_ >= pause_delay_ && playing_) { | 80 } else if (now - last_play_time_ >= pause_delay_ && playing_) { |
| 72 audio_sink_->Pause(); | 81 audio_sink_->Pause(); |
| 73 playing_ = false; | 82 playing_ = false; |
| 74 } | 83 } |
| 75 | 84 |
| 76 audio_converter_.ConvertWithDelay( | 85 audio_converter_.ConvertWithDelay( |
| 77 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds), audio_bus); | 86 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds), audio_bus); |
| 78 return audio_bus->frames(); | 87 return audio_bus->frames(); |
| 79 } | 88 } |
| 80 | 89 |
| 81 void AudioRendererMixer::OnRenderError() { | 90 void AudioRendererMixer::OnRenderError() { |
| 82 base::AutoLock auto_lock(mixer_inputs_lock_); | 91 base::AutoLock auto_lock(lock_); |
| 83 | 92 |
| 84 // Call each mixer input and signal an error. | 93 // Call each mixer input and signal an error. |
| 85 for (AudioRendererMixerInputSet::iterator it = mixer_inputs_.begin(); | 94 for (AudioRendererMixerErrorCallbackMap::iterator it = |
| 86 it != mixer_inputs_.end(); ++it) { | 95 error_callbacks_.begin(); |
| 96 it != error_callbacks_.end(); | |
| 97 ++it) { | |
| 87 it->second.Run(); | 98 it->second.Run(); |
| 88 } | 99 } |
| 89 } | 100 } |
| 90 | 101 |
| 91 } // namespace media | 102 } // namespace media |
| OLD | NEW |