| 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_resampler.h" | 5 #include "media/audio/audio_output_resampler.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/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 void AudioOutputResampler::CloseStream(AudioOutputProxy* stream_proxy) { | 120 void AudioOutputResampler::CloseStream(AudioOutputProxy* stream_proxy) { |
| 121 Reset(); | 121 Reset(); |
| 122 dispatcher_->CloseStream(stream_proxy); | 122 dispatcher_->CloseStream(stream_proxy); |
| 123 } | 123 } |
| 124 | 124 |
| 125 void AudioOutputResampler::Shutdown() { | 125 void AudioOutputResampler::Shutdown() { |
| 126 Reset(); | 126 Reset(); |
| 127 dispatcher_->Shutdown(); | 127 dispatcher_->Shutdown(); |
| 128 } | 128 } |
| 129 | 129 |
| 130 int AudioOutputResampler::OnMoreData(AudioBus* audio_bus, | 130 int AudioOutputResampler::OnMoreData(AudioBus* dest, |
| 131 AudioBuffersState buffers_state) { | 131 AudioBuffersState buffers_state) { |
| 132 return OnMoreIOData(NULL, dest, buffers_state); |
| 133 } |
| 134 |
| 135 int AudioOutputResampler::OnMoreIOData(AudioBus* source, |
| 136 AudioBus* dest, |
| 137 AudioBuffersState buffers_state) { |
| 132 base::AutoLock auto_lock(source_lock_); | 138 base::AutoLock auto_lock(source_lock_); |
| 133 // While we waited for |source_lock_| the callback might have been cleared. | 139 // While we waited for |source_lock_| the callback might have been cleared. |
| 134 if (!source_callback_) { | 140 if (!source_callback_) { |
| 135 audio_bus->Zero(); | 141 dest->Zero(); |
| 136 return audio_bus->frames(); | 142 return dest->frames(); |
| 137 } | 143 } |
| 138 | 144 |
| 139 current_buffers_state_ = buffers_state; | 145 current_buffers_state_ = buffers_state; |
| 140 | 146 |
| 141 if (!resampler_.get() && !audio_fifo_.get()) { | 147 if (!resampler_.get() && !audio_fifo_.get()) { |
| 142 // We have no internal buffers, so clear any outstanding audio data. | 148 // We have no internal buffers, so clear any outstanding audio data. |
| 143 outstanding_audio_bytes_ = 0; | 149 outstanding_audio_bytes_ = 0; |
| 144 SourceCallback_Locked(audio_bus); | 150 SourceCallback_Locked(dest); |
| 145 return audio_bus->frames(); | 151 return dest->frames(); |
| 146 } | 152 } |
| 147 | 153 |
| 148 if (resampler_.get()) | 154 if (resampler_.get()) |
| 149 resampler_->Resample(audio_bus, audio_bus->frames()); | 155 resampler_->Resample(dest, dest->frames()); |
| 150 else | 156 else |
| 151 ProvideInput(audio_bus); | 157 ProvideInput(dest); |
| 152 | 158 |
| 153 // Calculate how much data is left in the internal FIFO and resampler buffers. | 159 // Calculate how much data is left in the internal FIFO and resampler buffers. |
| 154 outstanding_audio_bytes_ -= audio_bus->frames() * output_bytes_per_frame_; | 160 outstanding_audio_bytes_ -= dest->frames() * output_bytes_per_frame_; |
| 155 // Due to rounding errors while multiplying against |io_ratio_|, | 161 // Due to rounding errors while multiplying against |io_ratio_|, |
| 156 // |outstanding_audio_bytes_| might (rarely) slip below zero. | 162 // |outstanding_audio_bytes_| might (rarely) slip below zero. |
| 157 if (outstanding_audio_bytes_ < 0) { | 163 if (outstanding_audio_bytes_ < 0) { |
| 158 DLOG(ERROR) << "Outstanding audio bytes went negative! Value: " | 164 DLOG(ERROR) << "Outstanding audio bytes went negative! Value: " |
| 159 << outstanding_audio_bytes_; | 165 << outstanding_audio_bytes_; |
| 160 outstanding_audio_bytes_ = 0; | 166 outstanding_audio_bytes_ = 0; |
| 161 } | 167 } |
| 162 | 168 |
| 163 // Always return the full number of frames requested, ProvideInput() will pad | 169 // Always return the full number of frames requested, ProvideInput() will pad |
| 164 // with silence if it wasn't able to acquire enough data. | 170 // with silence if it wasn't able to acquire enough data. |
| 165 return audio_bus->frames(); | 171 return dest->frames(); |
| 166 } | 172 } |
| 167 | 173 |
| 168 void AudioOutputResampler::SourceCallback_Locked(AudioBus* audio_bus) { | 174 void AudioOutputResampler::SourceCallback_Locked(AudioBus* audio_bus) { |
| 169 source_lock_.AssertAcquired(); | 175 source_lock_.AssertAcquired(); |
| 170 | 176 |
| 171 // Adjust playback delay to include the state of the internal buffers used by | 177 // Adjust playback delay to include the state of the internal buffers used by |
| 172 // the resampler and/or the FIFO. Since the sample rate and bits per channel | 178 // the resampler and/or the FIFO. Since the sample rate and bits per channel |
| 173 // may be different, we need to scale this value appropriately. | 179 // may be different, we need to scale this value appropriately. |
| 174 AudioBuffersState new_buffers_state; | 180 AudioBuffersState new_buffers_state; |
| 175 new_buffers_state.pending_bytes = io_ratio_ * | 181 new_buffers_state.pending_bytes = io_ratio_ * |
| (...skipping 20 matching lines...) Expand all Loading... |
| 196 source_callback_->OnError(stream, code); | 202 source_callback_->OnError(stream, code); |
| 197 } | 203 } |
| 198 | 204 |
| 199 void AudioOutputResampler::WaitTillDataReady() { | 205 void AudioOutputResampler::WaitTillDataReady() { |
| 200 base::AutoLock auto_lock(source_lock_); | 206 base::AutoLock auto_lock(source_lock_); |
| 201 if (source_callback_ && !outstanding_audio_bytes_) | 207 if (source_callback_ && !outstanding_audio_bytes_) |
| 202 source_callback_->WaitTillDataReady(); | 208 source_callback_->WaitTillDataReady(); |
| 203 } | 209 } |
| 204 | 210 |
| 205 } // namespace media | 211 } // namespace media |
| OLD | NEW |