| 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 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 } | 53 } |
| 54 } | 54 } |
| 55 | 55 |
| 56 bool AudioRendererImpl::OnInitialize(int bits_per_channel, | 56 bool AudioRendererImpl::OnInitialize(int bits_per_channel, |
| 57 ChannelLayout channel_layout, | 57 ChannelLayout channel_layout, |
| 58 int sample_rate) { | 58 int sample_rate) { |
| 59 // We use the AUDIO_PCM_LINEAR flag because AUDIO_PCM_LOW_LATENCY | 59 // We use the AUDIO_PCM_LINEAR flag because AUDIO_PCM_LOW_LATENCY |
| 60 // does not currently support all the sample-rates that we require. | 60 // does not currently support all the sample-rates that we require. |
| 61 // Please see: http://code.google.com/p/chromium/issues/detail?id=103627 | 61 // Please see: http://code.google.com/p/chromium/issues/detail?id=103627 |
| 62 // for more details. | 62 // for more details. |
| 63 audio_parameters_ = AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, | 63 audio_parameters_.Reset( |
| 64 channel_layout, | 64 AudioParameters::AUDIO_PCM_LINEAR, |
| 65 sample_rate, | 65 channel_layout, sample_rate, bits_per_channel, |
| 66 bits_per_channel, | 66 audio_hardware::GetHighLatencyOutputBufferSize(sample_rate)); |
| 67 0); | |
| 68 | 67 |
| 69 bytes_per_second_ = audio_parameters_.GetBytesPerSecond(); | 68 bytes_per_second_ = audio_parameters_.GetBytesPerSecond(); |
| 70 | 69 |
| 71 DCHECK(sink_.get()); | 70 DCHECK(sink_.get()); |
| 72 | 71 |
| 73 if (!is_initialized_) { | 72 if (!is_initialized_) { |
| 74 sink_->Initialize( | 73 sink_->Initialize(audio_parameters_, this); |
| 75 audio_hardware::GetHighLatencyOutputBufferSize(sample_rate), | |
| 76 audio_parameters_.channels, | |
| 77 audio_parameters_.sample_rate, | |
| 78 audio_parameters_.format, | |
| 79 this); | |
| 80 | 74 |
| 81 sink_->Start(); | 75 sink_->Start(); |
| 82 is_initialized_ = true; | 76 is_initialized_ = true; |
| 83 return true; | 77 return true; |
| 84 } | 78 } |
| 85 | 79 |
| 86 return false; | 80 return false; |
| 87 } | 81 } |
| 88 | 82 |
| 89 void AudioRendererImpl::OnStop() { | 83 void AudioRendererImpl::OnStop() { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 base::TimeDelta request_delay = | 180 base::TimeDelta request_delay = |
| 187 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds); | 181 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds); |
| 188 | 182 |
| 189 // Finally we need to adjust the delay according to playback rate. | 183 // Finally we need to adjust the delay according to playback rate. |
| 190 if (GetPlaybackRate() != 1.0f) { | 184 if (GetPlaybackRate() != 1.0f) { |
| 191 request_delay = base::TimeDelta::FromMicroseconds( | 185 request_delay = base::TimeDelta::FromMicroseconds( |
| 192 static_cast<int64>(ceil(request_delay.InMicroseconds() * | 186 static_cast<int64>(ceil(request_delay.InMicroseconds() * |
| 193 GetPlaybackRate()))); | 187 GetPlaybackRate()))); |
| 194 } | 188 } |
| 195 | 189 |
| 196 uint32 bytes_per_frame = | 190 int bytes_per_frame = audio_parameters_.GetBytesPerFrame(); |
| 197 audio_parameters_.bits_per_sample * audio_parameters_.channels / 8; | |
| 198 | 191 |
| 199 const size_t buf_size = number_of_frames * bytes_per_frame; | 192 const size_t buf_size = number_of_frames * bytes_per_frame; |
| 200 scoped_array<uint8> buf(new uint8[buf_size]); | 193 scoped_array<uint8> buf(new uint8[buf_size]); |
| 201 | 194 |
| 202 uint32 frames_filled = FillBuffer(buf.get(), number_of_frames, request_delay); | 195 uint32 frames_filled = FillBuffer(buf.get(), number_of_frames, request_delay); |
| 203 uint32 bytes_filled = frames_filled * bytes_per_frame; | 196 uint32 bytes_filled = frames_filled * bytes_per_frame; |
| 204 DCHECK_LE(bytes_filled, buf_size); | 197 DCHECK_LE(bytes_filled, buf_size); |
| 205 UpdateEarliestEndTime(bytes_filled, request_delay, base::Time::Now()); | 198 UpdateEarliestEndTime(bytes_filled, request_delay, base::Time::Now()); |
| 206 | 199 |
| 207 // Deinterleave each audio channel. | 200 // Deinterleave each audio channel. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 227 | 220 |
| 228 void AudioRendererImpl::OnRenderError() { | 221 void AudioRendererImpl::OnRenderError() { |
| 229 host()->DisableAudioRenderer(); | 222 host()->DisableAudioRenderer(); |
| 230 } | 223 } |
| 231 | 224 |
| 232 void AudioRendererImpl::OnRenderEndOfStream() { | 225 void AudioRendererImpl::OnRenderEndOfStream() { |
| 233 // TODO(enal): schedule callback instead of polling. | 226 // TODO(enal): schedule callback instead of polling. |
| 234 if (base::Time::Now() >= earliest_end_time_) | 227 if (base::Time::Now() >= earliest_end_time_) |
| 235 SignalEndOfStream(); | 228 SignalEndOfStream(); |
| 236 } | 229 } |
| OLD | NEW |