| 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/renderers/audio_renderer_impl.h" | 5 #include "media/renderers/audio_renderer_impl.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 // Always post |init_cb_| because |this| could be destroyed if initialization | 356 // Always post |init_cb_| because |this| could be destroyed if initialization |
| 357 // failed. | 357 // failed. |
| 358 init_cb_ = BindToCurrentLoop(init_cb); | 358 init_cb_ = BindToCurrentLoop(init_cb); |
| 359 | 359 |
| 360 const AudioParameters& hw_params = | 360 const AudioParameters& hw_params = |
| 361 sink_->GetOutputDeviceInfo().output_params(); | 361 sink_->GetOutputDeviceInfo().output_params(); |
| 362 expecting_config_changes_ = stream->SupportsConfigChanges(); | 362 expecting_config_changes_ = stream->SupportsConfigChanges(); |
| 363 if (!expecting_config_changes_ || !hw_params.IsValid() || | 363 if (!expecting_config_changes_ || !hw_params.IsValid() || |
| 364 hw_params.format() == AudioParameters::AUDIO_FAKE) { | 364 hw_params.format() == AudioParameters::AUDIO_FAKE) { |
| 365 // The actual buffer size is controlled via the size of the AudioBus | 365 // The actual buffer size is controlled via the size of the AudioBus |
| 366 // provided to Render(), so just choose something reasonable here for looks. | 366 // provided to Render(), but we should choose a value here based on hardware |
| 367 int buffer_size = stream->audio_decoder_config().samples_per_second() / 100; | 367 // parameters if possible since it affects the initial buffer size used by |
| 368 audio_parameters_.Reset( | 368 // the algorithm. Too little will cause underflow on Bluetooth devices. |
| 369 AudioParameters::AUDIO_PCM_LOW_LATENCY, | 369 int buffer_size = |
| 370 stream->audio_decoder_config().channel_layout(), | 370 std::max(stream->audio_decoder_config().samples_per_second() / 100, |
| 371 stream->audio_decoder_config().samples_per_second(), | 371 hw_params.IsValid() ? hw_params.frames_per_buffer() : 0); |
| 372 stream->audio_decoder_config().bits_per_channel(), | 372 audio_parameters_.Reset(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 373 buffer_size); | 373 stream->audio_decoder_config().channel_layout(), |
| 374 stream->audio_decoder_config().samples_per_second(), |
| 375 stream->audio_decoder_config().bits_per_channel(), |
| 376 buffer_size); |
| 374 buffer_converter_.reset(); | 377 buffer_converter_.reset(); |
| 375 } else { | 378 } else { |
| 376 // To allow for seamless sample rate adaptations (i.e. changes from say | 379 // To allow for seamless sample rate adaptations (i.e. changes from say |
| 377 // 16kHz to 48kHz), always resample to the hardware rate. | 380 // 16kHz to 48kHz), always resample to the hardware rate. |
| 378 int sample_rate = hw_params.sample_rate(); | 381 int sample_rate = hw_params.sample_rate(); |
| 379 int preferred_buffer_size = hw_params.frames_per_buffer(); | 382 int preferred_buffer_size = hw_params.frames_per_buffer(); |
| 380 | 383 |
| 381 #if defined(OS_CHROMEOS) | 384 #if defined(OS_CHROMEOS) |
| 382 // On ChromeOS let the OS level resampler handle resampling unless the | 385 // On ChromeOS let the OS level resampler handle resampling unless the |
| 383 // initial sample rate is too low; this allows support for sample rate | 386 // initial sample rate is too low; this allows support for sample rate |
| (...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 858 if (frames_written == 0) { | 861 if (frames_written == 0) { |
| 859 if (received_end_of_stream_) { | 862 if (received_end_of_stream_) { |
| 860 if (ended_timestamp_ == kInfiniteDuration) | 863 if (ended_timestamp_ == kInfiniteDuration) |
| 861 ended_timestamp_ = audio_clock_->back_timestamp(); | 864 ended_timestamp_ = audio_clock_->back_timestamp(); |
| 862 frames_after_end_of_stream = frames_requested; | 865 frames_after_end_of_stream = frames_requested; |
| 863 } else if (state_ == kPlaying && | 866 } else if (state_ == kPlaying && |
| 864 buffering_state_ != BUFFERING_HAVE_NOTHING) { | 867 buffering_state_ != BUFFERING_HAVE_NOTHING) { |
| 865 algorithm_->IncreaseQueueCapacity(); | 868 algorithm_->IncreaseQueueCapacity(); |
| 866 SetBufferingState_Locked(BUFFERING_HAVE_NOTHING); | 869 SetBufferingState_Locked(BUFFERING_HAVE_NOTHING); |
| 867 } | 870 } |
| 871 } else if (frames_written < frames_requested && !received_end_of_stream_) { |
| 872 // If we only partially filled the request and should have more data, go |
| 873 // ahead and increase queue capacity to try and meet the next request. |
| 874 algorithm_->IncreaseQueueCapacity(); |
| 868 } | 875 } |
| 869 | 876 |
| 870 audio_clock_->WroteAudio(frames_written + frames_after_end_of_stream, | 877 audio_clock_->WroteAudio(frames_written + frames_after_end_of_stream, |
| 871 frames_requested, frames_delayed, playback_rate_); | 878 frames_requested, frames_delayed, playback_rate_); |
| 872 | 879 |
| 873 if (CanRead_Locked()) { | 880 if (CanRead_Locked()) { |
| 874 task_runner_->PostTask(FROM_HERE, | 881 task_runner_->PostTask(FROM_HERE, |
| 875 base::Bind(&AudioRendererImpl::AttemptRead, | 882 base::Bind(&AudioRendererImpl::AttemptRead, |
| 876 weak_factory_.GetWeakPtr())); | 883 weak_factory_.GetWeakPtr())); |
| 877 } | 884 } |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 961 DCHECK_NE(buffering_state_, buffering_state); | 968 DCHECK_NE(buffering_state_, buffering_state); |
| 962 lock_.AssertAcquired(); | 969 lock_.AssertAcquired(); |
| 963 buffering_state_ = buffering_state; | 970 buffering_state_ = buffering_state; |
| 964 | 971 |
| 965 task_runner_->PostTask( | 972 task_runner_->PostTask( |
| 966 FROM_HERE, base::Bind(&AudioRendererImpl::OnBufferingStateChange, | 973 FROM_HERE, base::Bind(&AudioRendererImpl::OnBufferingStateChange, |
| 967 weak_factory_.GetWeakPtr(), buffering_state_)); | 974 weak_factory_.GetWeakPtr(), buffering_state_)); |
| 968 } | 975 } |
| 969 | 976 |
| 970 } // namespace media | 977 } // namespace media |
| OLD | NEW |