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 "content/renderer/media/audio_renderer_impl.h" | 5 #include "content/renderer/media/audio_renderer_impl.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "content/common/child_process.h" | 12 #include "content/common/child_process.h" |
13 #include "content/common/media/audio_messages.h" | 13 #include "content/common/media/audio_messages.h" |
| 14 #include "content/renderer/media/audio_hardware.h" |
14 #include "content/renderer/render_thread_impl.h" | 15 #include "content/renderer/render_thread_impl.h" |
15 #include "media/audio/audio_buffers_state.h" | 16 #include "media/audio/audio_buffers_state.h" |
16 #include "media/audio/audio_util.h" | 17 #include "media/audio/audio_util.h" |
17 #include "media/base/filter_host.h" | 18 #include "media/base/filter_host.h" |
18 | 19 |
19 AudioRendererImpl::AudioRendererImpl(media::AudioRendererSink* sink) | 20 AudioRendererImpl::AudioRendererImpl(media::AudioRendererSink* sink) |
20 : AudioRendererBase(), | 21 : AudioRendererBase(), |
21 bytes_per_second_(0), | 22 bytes_per_second_(0), |
22 stopped_(false), | 23 stopped_(false), |
23 sink_(sink), | 24 sink_(sink), |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 sample_rate, | 65 sample_rate, |
65 bits_per_channel, | 66 bits_per_channel, |
66 0); | 67 0); |
67 | 68 |
68 bytes_per_second_ = audio_parameters_.GetBytesPerSecond(); | 69 bytes_per_second_ = audio_parameters_.GetBytesPerSecond(); |
69 | 70 |
70 DCHECK(sink_.get()); | 71 DCHECK(sink_.get()); |
71 | 72 |
72 if (!is_initialized_) { | 73 if (!is_initialized_) { |
73 sink_->Initialize( | 74 sink_->Initialize( |
74 media::SelectSamplesPerPacket(sample_rate), | 75 audio_hardware::GetHighLatencyOutputBufferSize(sample_rate), |
75 audio_parameters_.channels, | 76 audio_parameters_.channels, |
76 audio_parameters_.sample_rate, | 77 audio_parameters_.sample_rate, |
77 audio_parameters_.format, | 78 audio_parameters_.format, |
78 this); | 79 this); |
79 | 80 |
80 sink_->Start(); | 81 sink_->Start(); |
81 is_initialized_ = true; | 82 is_initialized_ = true; |
82 return true; | 83 return true; |
83 } | 84 } |
84 | 85 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 static_cast<int64>(ceil(request_delay.InMicroseconds() * | 192 static_cast<int64>(ceil(request_delay.InMicroseconds() * |
192 GetPlaybackRate()))); | 193 GetPlaybackRate()))); |
193 } | 194 } |
194 | 195 |
195 uint32 bytes_per_frame = | 196 uint32 bytes_per_frame = |
196 audio_parameters_.bits_per_sample * audio_parameters_.channels / 8; | 197 audio_parameters_.bits_per_sample * audio_parameters_.channels / 8; |
197 | 198 |
198 const size_t buf_size = number_of_frames * bytes_per_frame; | 199 const size_t buf_size = number_of_frames * bytes_per_frame; |
199 scoped_array<uint8> buf(new uint8[buf_size]); | 200 scoped_array<uint8> buf(new uint8[buf_size]); |
200 | 201 |
201 uint32 filled = FillBuffer(buf.get(), buf_size, request_delay); | 202 uint32 frames_filled = FillBuffer(buf.get(), number_of_frames, request_delay); |
202 DCHECK_LE(filled, buf_size); | 203 uint32 bytes_filled = frames_filled * bytes_per_frame; |
203 UpdateEarliestEndTime(filled, request_delay, base::Time::Now()); | 204 DCHECK_LE(bytes_filled, buf_size); |
204 | 205 UpdateEarliestEndTime(bytes_filled, request_delay, base::Time::Now()); |
205 uint32 filled_frames = filled / bytes_per_frame; | |
206 | 206 |
207 // Deinterleave each audio channel. | 207 // Deinterleave each audio channel. |
208 int channels = audio_data.size(); | 208 int channels = audio_data.size(); |
209 for (int channel_index = 0; channel_index < channels; ++channel_index) { | 209 for (int channel_index = 0; channel_index < channels; ++channel_index) { |
210 media::DeinterleaveAudioChannel(buf.get(), | 210 media::DeinterleaveAudioChannel(buf.get(), |
211 audio_data[channel_index], | 211 audio_data[channel_index], |
212 channels, | 212 channels, |
213 channel_index, | 213 channel_index, |
214 bytes_per_frame / channels, | 214 bytes_per_frame / channels, |
215 filled_frames); | 215 frames_filled); |
216 | 216 |
217 // If FillBuffer() didn't give us enough data then zero out the remainder. | 217 // If FillBuffer() didn't give us enough data then zero out the remainder. |
218 if (filled_frames < number_of_frames) { | 218 if (frames_filled < number_of_frames) { |
219 int frames_to_zero = number_of_frames - filled_frames; | 219 int frames_to_zero = number_of_frames - frames_filled; |
220 memset(audio_data[channel_index] + filled_frames, | 220 memset(audio_data[channel_index] + frames_filled, |
221 0, | 221 0, |
222 sizeof(float) * frames_to_zero); | 222 sizeof(float) * frames_to_zero); |
223 } | 223 } |
224 } | 224 } |
225 return filled_frames; | 225 return frames_filled; |
226 } | 226 } |
227 | 227 |
228 void AudioRendererImpl::OnRenderError() { | 228 void AudioRendererImpl::OnRenderError() { |
229 host()->DisableAudioRenderer(); | 229 host()->DisableAudioRenderer(); |
230 } | 230 } |
231 | 231 |
232 void AudioRendererImpl::OnRenderEndOfStream() { | 232 void AudioRendererImpl::OnRenderEndOfStream() { |
233 // TODO(enal): schedule callback instead of polling. | 233 // TODO(enal): schedule callback instead of polling. |
234 if (base::Time::Now() >= earliest_end_time_) | 234 if (base::Time::Now() >= earliest_end_time_) |
235 SignalEndOfStream(); | 235 SignalEndOfStream(); |
236 } | 236 } |
OLD | NEW |