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_mixer.h" | 5 #include "media/audio/audio_output_mixer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 } | 138 } |
139 | 139 |
140 void AudioOutputMixer::ClosePhysicalStream() { | 140 void AudioOutputMixer::ClosePhysicalStream() { |
141 DCHECK_EQ(MessageLoop::current(), message_loop_); | 141 DCHECK_EQ(MessageLoop::current(), message_loop_); |
142 | 142 |
143 if (proxies_.empty() && physical_stream_.get() != NULL) | 143 if (proxies_.empty() && physical_stream_.get() != NULL) |
144 physical_stream_.release()->Close(); | 144 physical_stream_.release()->Close(); |
145 } | 145 } |
146 | 146 |
147 // AudioSourceCallback implementation. | 147 // AudioSourceCallback implementation. |
148 uint32 AudioOutputMixer::OnMoreData(AudioOutputStream* stream, | 148 uint32 AudioOutputMixer::OnMoreData(uint8* dest, |
149 uint8* dest, | |
150 uint32 max_size, | 149 uint32 max_size, |
151 AudioBuffersState buffers_state) { | 150 AudioBuffersState buffers_state) { |
152 max_size = std::min(max_size, | 151 max_size = std::min(max_size, |
153 static_cast<uint32>(params_.GetBytesPerBuffer())); | 152 static_cast<uint32>(params_.GetBytesPerBuffer())); |
154 // TODO(enal): consider getting rid of lock as it is in time-critical code. | 153 // TODO(enal): consider getting rid of lock as it is in time-critical code. |
155 // E.g. swap |proxies_| with local variable, and merge 2 lists | 154 // E.g. swap |proxies_| with local variable, and merge 2 lists |
156 // at the end. That would speed things up but complicate stopping | 155 // at the end. That would speed things up but complicate stopping |
157 // the stream. | 156 // the stream. |
158 base::AutoLock lock(lock_); | 157 base::AutoLock lock(lock_); |
159 if (proxies_.empty()) | 158 if (proxies_.empty()) |
160 return 0; | 159 return 0; |
161 uint32 actual_total_size = 0; | 160 uint32 actual_total_size = 0; |
162 uint32 bytes_per_sample = params_.bits_per_sample() >> 3; | 161 uint32 bytes_per_sample = params_.bits_per_sample() >> 3; |
163 | 162 |
164 // Go through all the streams, getting data for every one of them | 163 // Go through all the streams, getting data for every one of them |
165 // and mixing it into destination. | 164 // and mixing it into destination. |
166 // Minor optimization: for the first stream we are writing data directly into | 165 // Minor optimization: for the first stream we are writing data directly into |
167 // destination. This way we don't have to mix the data when there is only one | 166 // destination. This way we don't have to mix the data when there is only one |
168 // active stream, and net win in other cases, too. | 167 // active stream, and net win in other cases, too. |
169 bool first_stream = true; | 168 bool first_stream = true; |
170 uint8* actual_dest = dest; | 169 uint8* actual_dest = dest; |
171 for (ProxyMap::iterator it = proxies_.begin(); it != proxies_.end(); ++it) { | 170 for (ProxyMap::iterator it = proxies_.begin(); it != proxies_.end(); ++it) { |
172 AudioOutputProxy* stream_proxy = it->first; | |
173 ProxyData* proxy_data = &it->second; | 171 ProxyData* proxy_data = &it->second; |
174 // TODO(enal): We don't know |pending _bytes| for individual stream, and we | 172 // TODO(enal): We don't know |pending _bytes| for individual stream, and we |
175 // should give that value to individual stream's OnMoreData(). I believe it | 173 // should give that value to individual stream's OnMoreData(). I believe it |
176 // can be used there to evaluate exact position of data it should return. | 174 // can be used there to evaluate exact position of data it should return. |
177 // Current code "sorta works" if everything works perfectly, but would have | 175 // Current code "sorta works" if everything works perfectly, but would have |
178 // problems if some of the buffers are only partially filled -- we don't | 176 // problems if some of the buffers are only partially filled -- we don't |
179 // know how how much data was in the buffer OS returned to us, so we cannot | 177 // know how how much data was in the buffer OS returned to us, so we cannot |
180 // correctly calculate new value. If we know number of buffers we can solve | 178 // correctly calculate new value. If we know number of buffers we can solve |
181 // the problem by storing not one value but vector of them. | 179 // the problem by storing not one value but vector of them. |
182 int pending_bytes = std::min(proxy_data->pending_bytes, | 180 int pending_bytes = std::min(proxy_data->pending_bytes, |
183 buffers_state.pending_bytes); | 181 buffers_state.pending_bytes); |
184 // Note: there is no way we can deduce hardware_delay_bytes for the | 182 // Note: there is no way we can deduce hardware_delay_bytes for the |
185 // particular proxy stream. Use zero instead. | 183 // particular proxy stream. Use zero instead. |
186 uint32 actual_size = proxy_data->audio_source_callback->OnMoreData( | 184 uint32 actual_size = proxy_data->audio_source_callback->OnMoreData( |
187 stream_proxy, | |
188 actual_dest, | 185 actual_dest, |
189 max_size, | 186 max_size, |
190 AudioBuffersState(pending_bytes, 0)); | 187 AudioBuffersState(pending_bytes, 0)); |
191 | 188 |
192 // Should update pending_bytes for each proxy. | 189 // Should update pending_bytes for each proxy. |
193 // If stream ended, pending_bytes goes down by max_size. | 190 // If stream ended, pending_bytes goes down by max_size. |
194 if (actual_size == 0) { | 191 if (actual_size == 0) { |
195 pending_bytes -= max_size; | 192 pending_bytes -= max_size; |
196 proxy_data->pending_bytes = std::max(pending_bytes, 0); | 193 proxy_data->pending_bytes = std::max(pending_bytes, 0); |
197 continue; | 194 continue; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 } | 239 } |
243 | 240 |
244 void AudioOutputMixer::WaitTillDataReady() { | 241 void AudioOutputMixer::WaitTillDataReady() { |
245 base::AutoLock lock(lock_); | 242 base::AutoLock lock(lock_); |
246 for (ProxyMap::iterator it = proxies_.begin(); it != proxies_.end(); ++it) { | 243 for (ProxyMap::iterator it = proxies_.begin(); it != proxies_.end(); ++it) { |
247 it->second.audio_source_callback->WaitTillDataReady(); | 244 it->second.audio_source_callback->WaitTillDataReady(); |
248 } | 245 } |
249 } | 246 } |
250 | 247 |
251 } // namespace media | 248 } // namespace media |
OLD | NEW |