| 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/filters/audio_renderer_impl.h" | 5 #include "media/filters/audio_renderer_impl.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 base::AutoLock auto_lock(lock_); | 325 base::AutoLock auto_lock(lock_); |
| 326 return algorithm_->playback_rate(); | 326 return algorithm_->playback_rate(); |
| 327 } | 327 } |
| 328 | 328 |
| 329 bool AudioRendererImpl::IsBeforePrerollTime( | 329 bool AudioRendererImpl::IsBeforePrerollTime( |
| 330 const scoped_refptr<Buffer>& buffer) { | 330 const scoped_refptr<Buffer>& buffer) { |
| 331 return (state_ == kPrerolling) && buffer && !buffer->IsEndOfStream() && | 331 return (state_ == kPrerolling) && buffer && !buffer->IsEndOfStream() && |
| 332 (buffer->GetTimestamp() + buffer->GetDuration()) < preroll_timestamp_; | 332 (buffer->GetTimestamp() + buffer->GetDuration()) < preroll_timestamp_; |
| 333 } | 333 } |
| 334 | 334 |
| 335 int AudioRendererImpl::Render(const std::vector<float*>& audio_data, | 335 int AudioRendererImpl::Render(AudioBus* audio_bus, |
| 336 int number_of_frames, | |
| 337 int audio_delay_milliseconds) { | 336 int audio_delay_milliseconds) { |
| 338 if (stopped_ || GetPlaybackRate() == 0.0f) { | 337 if (stopped_ || GetPlaybackRate() == 0.0f) { |
| 339 // Output silence if stopped. | 338 // Output silence if stopped. |
| 340 for (size_t i = 0; i < audio_data.size(); ++i) | 339 audio_bus->Zero(); |
| 341 memset(audio_data[i], 0, sizeof(float) * number_of_frames); | |
| 342 return 0; | 340 return 0; |
| 343 } | 341 } |
| 344 | 342 |
| 345 // Adjust the playback delay. | 343 // Adjust the playback delay. |
| 346 base::TimeDelta request_delay = | 344 base::TimeDelta request_delay = |
| 347 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds); | 345 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds); |
| 348 | 346 |
| 349 // Finally we need to adjust the delay according to playback rate. | 347 // Finally we need to adjust the delay according to playback rate. |
| 350 if (GetPlaybackRate() != 1.0f) { | 348 if (GetPlaybackRate() != 1.0f) { |
| 351 request_delay = base::TimeDelta::FromMicroseconds( | 349 request_delay = base::TimeDelta::FromMicroseconds( |
| 352 static_cast<int64>(ceil(request_delay.InMicroseconds() * | 350 static_cast<int64>(ceil(request_delay.InMicroseconds() * |
| 353 GetPlaybackRate()))); | 351 GetPlaybackRate()))); |
| 354 } | 352 } |
| 355 | 353 |
| 356 int bytes_per_frame = audio_parameters_.GetBytesPerFrame(); | 354 int bytes_per_frame = audio_parameters_.GetBytesPerFrame(); |
| 357 | 355 |
| 358 const int buf_size = number_of_frames * bytes_per_frame; | 356 const int buf_size = audio_bus->frames() * bytes_per_frame; |
| 359 scoped_array<uint8> buf(new uint8[buf_size]); | 357 scoped_array<uint8> buf(new uint8[buf_size]); |
| 360 | 358 |
| 361 int frames_filled = FillBuffer(buf.get(), number_of_frames, request_delay); | 359 int frames_filled = FillBuffer(buf.get(), audio_bus->frames(), request_delay); |
| 362 int bytes_filled = frames_filled * bytes_per_frame; | 360 int bytes_filled = frames_filled * bytes_per_frame; |
| 363 DCHECK_LE(bytes_filled, buf_size); | 361 DCHECK_LE(bytes_filled, buf_size); |
| 364 UpdateEarliestEndTime(bytes_filled, request_delay, base::Time::Now()); | 362 UpdateEarliestEndTime(bytes_filled, request_delay, base::Time::Now()); |
| 365 | 363 |
| 366 // Deinterleave each audio channel. | 364 // Deinterleave each audio channel. |
| 367 int channels = audio_data.size(); | 365 int channels = audio_bus->channels(); |
| 368 for (int channel_index = 0; channel_index < channels; ++channel_index) { | 366 for (int channel_index = 0; channel_index < channels; ++channel_index) { |
| 369 media::DeinterleaveAudioChannel(buf.get(), | 367 media::DeinterleaveAudioChannel(buf.get(), |
| 370 audio_data[channel_index], | 368 audio_bus->channel(channel_index), |
| 371 channels, | 369 channels, |
| 372 channel_index, | 370 channel_index, |
| 373 bytes_per_frame / channels, | 371 bytes_per_frame / channels, |
| 374 frames_filled); | 372 frames_filled); |
| 375 | 373 |
| 376 // If FillBuffer() didn't give us enough data then zero out the remainder. | 374 // If FillBuffer() didn't give us enough data then zero out the remainder. |
| 377 if (frames_filled < number_of_frames) { | 375 if (frames_filled < audio_bus->frames()) { |
| 378 int frames_to_zero = number_of_frames - frames_filled; | 376 int frames_to_zero = audio_bus->frames() - frames_filled; |
| 379 memset(audio_data[channel_index] + frames_filled, | 377 memset(audio_bus->channel(channel_index) + frames_filled, 0, |
| 380 0, | 378 sizeof(*audio_bus->channel(channel_index)) * frames_to_zero); |
| 381 sizeof(float) * frames_to_zero); | |
| 382 } | 379 } |
| 383 } | 380 } |
| 384 return frames_filled; | 381 return frames_filled; |
| 385 } | 382 } |
| 386 | 383 |
| 387 uint32 AudioRendererImpl::FillBuffer(uint8* dest, | 384 uint32 AudioRendererImpl::FillBuffer(uint8* dest, |
| 388 uint32 requested_frames, | 385 uint32 requested_frames, |
| 389 const base::TimeDelta& playback_delay) { | 386 const base::TimeDelta& playback_delay) { |
| 390 base::TimeDelta current_time = kNoTimestamp(); | 387 base::TimeDelta current_time = kNoTimestamp(); |
| 391 base::TimeDelta max_time = kNoTimestamp(); | 388 base::TimeDelta max_time = kNoTimestamp(); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 case kUnderflow: | 538 case kUnderflow: |
| 542 case kRebuffering: | 539 case kRebuffering: |
| 543 case kStopped: | 540 case kStopped: |
| 544 if (status != PIPELINE_OK) | 541 if (status != PIPELINE_OK) |
| 545 error_cb_.Run(status); | 542 error_cb_.Run(status); |
| 546 return; | 543 return; |
| 547 } | 544 } |
| 548 } | 545 } |
| 549 | 546 |
| 550 } // namespace media | 547 } // namespace media |
| OLD | NEW |