| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 last_decoded_sample_rate_(0), | 50 last_decoded_sample_rate_(0), |
| 51 playback_rate_(0.0), | 51 playback_rate_(0.0), |
| 52 state_(kUninitialized), | 52 state_(kUninitialized), |
| 53 buffering_state_(BUFFERING_HAVE_NOTHING), | 53 buffering_state_(BUFFERING_HAVE_NOTHING), |
| 54 rendering_(false), | 54 rendering_(false), |
| 55 sink_playing_(false), | 55 sink_playing_(false), |
| 56 pending_read_(false), | 56 pending_read_(false), |
| 57 received_end_of_stream_(false), | 57 received_end_of_stream_(false), |
| 58 rendered_end_of_stream_(false), | 58 rendered_end_of_stream_(false), |
| 59 is_suspending_(false), | 59 is_suspending_(false), |
| 60 last_reported_media_time_(kNoTimestamp), | |
| 61 weak_factory_(this) { | 60 weak_factory_(this) { |
| 62 audio_buffer_stream_->set_splice_observer(base::Bind( | 61 audio_buffer_stream_->set_splice_observer(base::Bind( |
| 63 &AudioRendererImpl::OnNewSpliceBuffer, weak_factory_.GetWeakPtr())); | 62 &AudioRendererImpl::OnNewSpliceBuffer, weak_factory_.GetWeakPtr())); |
| 64 audio_buffer_stream_->set_config_change_observer(base::Bind( | 63 audio_buffer_stream_->set_config_change_observer(base::Bind( |
| 65 &AudioRendererImpl::OnConfigChange, weak_factory_.GetWeakPtr())); | 64 &AudioRendererImpl::OnConfigChange, weak_factory_.GetWeakPtr())); |
| 66 | 65 |
| 67 // Tests may not have a power monitor. | 66 // Tests may not have a power monitor. |
| 68 base::PowerMonitor* monitor = base::PowerMonitor::Get(); | 67 base::PowerMonitor* monitor = base::PowerMonitor::Get(); |
| 69 if (!monitor) | 68 if (!monitor) |
| 70 return; | 69 return; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 | 168 |
| 170 base::AutoLock auto_lock(lock_); | 169 base::AutoLock auto_lock(lock_); |
| 171 DCHECK(!rendering_); | 170 DCHECK(!rendering_); |
| 172 DCHECK_EQ(state_, kFlushed); | 171 DCHECK_EQ(state_, kFlushed); |
| 173 | 172 |
| 174 start_timestamp_ = time; | 173 start_timestamp_ = time; |
| 175 ended_timestamp_ = kInfiniteDuration; | 174 ended_timestamp_ = kInfiniteDuration; |
| 176 last_render_time_ = stop_rendering_time_ = base::TimeTicks(); | 175 last_render_time_ = stop_rendering_time_ = base::TimeTicks(); |
| 177 first_packet_timestamp_ = kNoTimestamp; | 176 first_packet_timestamp_ = kNoTimestamp; |
| 178 audio_clock_.reset(new AudioClock(time, audio_parameters_.sample_rate())); | 177 audio_clock_.reset(new AudioClock(time, audio_parameters_.sample_rate())); |
| 179 last_reported_media_time_ = kNoTimestamp; | |
| 180 } | 178 } |
| 181 | 179 |
| 182 base::TimeDelta AudioRendererImpl::CurrentMediaTime() { | 180 base::TimeDelta AudioRendererImpl::CurrentMediaTime() { |
| 183 base::AutoLock auto_lock(lock_); | 181 base::AutoLock auto_lock(lock_); |
| 184 | 182 |
| 185 // Re-use last reported time if rendering has stopped to avoid confusing blink | |
| 186 // layer (avoid currentTime advancing after signaling buffer underflow). | |
| 187 // TODO(chcunningham): Do this in blink instead. Patching it here for now | |
| 188 // because HTMLMediaElement's currentTime is a mess. | |
| 189 if (!rendering_ && last_reported_media_time_ != kNoTimestamp) { | |
| 190 // If rendering stops, be sure to at least report the front time of the most | |
| 191 // recently rendered audio buffer. | |
| 192 if (last_reported_media_time_ < audio_clock_->front_timestamp()) | |
| 193 last_reported_media_time_ = audio_clock_->front_timestamp(); | |
| 194 | |
| 195 DVLOG(3) << __func__ << " Returning cached time while rendering stopped:" | |
| 196 << last_reported_media_time_.InMicroseconds(); | |
| 197 return last_reported_media_time_; | |
| 198 } | |
| 199 | |
| 200 // Return the current time based on the known extents of the rendered audio | 183 // Return the current time based on the known extents of the rendered audio |
| 201 // data plus an estimate based on the last time those values were calculated. | 184 // data plus an estimate based on the last time those values were calculated. |
| 202 base::TimeDelta current_media_time = audio_clock_->front_timestamp(); | 185 base::TimeDelta current_media_time = audio_clock_->front_timestamp(); |
| 203 if (!last_render_time_.is_null()) { | 186 if (!last_render_time_.is_null()) { |
| 204 current_media_time += | 187 current_media_time += |
| 205 (tick_clock_->NowTicks() - last_render_time_) * playback_rate_; | 188 (tick_clock_->NowTicks() - last_render_time_) * playback_rate_; |
| 206 if (current_media_time > audio_clock_->back_timestamp()) | 189 if (current_media_time > audio_clock_->back_timestamp()) |
| 207 current_media_time = audio_clock_->back_timestamp(); | 190 current_media_time = audio_clock_->back_timestamp(); |
| 208 } | 191 } |
| 209 | 192 |
| 210 last_reported_media_time_ = current_media_time; | |
| 211 return current_media_time; | 193 return current_media_time; |
| 212 } | 194 } |
| 213 | 195 |
| 214 bool AudioRendererImpl::GetWallClockTimes( | 196 bool AudioRendererImpl::GetWallClockTimes( |
| 215 const std::vector<base::TimeDelta>& media_timestamps, | 197 const std::vector<base::TimeDelta>& media_timestamps, |
| 216 std::vector<base::TimeTicks>* wall_clock_times) { | 198 std::vector<base::TimeTicks>* wall_clock_times) { |
| 217 base::AutoLock auto_lock(lock_); | 199 base::AutoLock auto_lock(lock_); |
| 218 DCHECK(wall_clock_times->empty()); | 200 DCHECK(wall_clock_times->empty()); |
| 219 | 201 |
| 220 // When playback is paused (rate is zero), assume a rate of 1.0. | 202 // When playback is paused (rate is zero), assume a rate of 1.0. |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 : stream->audio_decoder_config().channel_layout(); | 427 : stream->audio_decoder_config().channel_layout(); |
| 446 | 428 |
| 447 audio_parameters_.Reset(hw_params.format(), renderer_channel_layout, | 429 audio_parameters_.Reset(hw_params.format(), renderer_channel_layout, |
| 448 sample_rate, hw_params.bits_per_sample(), | 430 sample_rate, hw_params.bits_per_sample(), |
| 449 media::AudioLatency::GetHighLatencyBufferSize( | 431 media::AudioLatency::GetHighLatencyBufferSize( |
| 450 sample_rate, preferred_buffer_size)); | 432 sample_rate, preferred_buffer_size)); |
| 451 } | 433 } |
| 452 | 434 |
| 453 audio_clock_.reset( | 435 audio_clock_.reset( |
| 454 new AudioClock(base::TimeDelta(), audio_parameters_.sample_rate())); | 436 new AudioClock(base::TimeDelta(), audio_parameters_.sample_rate())); |
| 455 last_reported_media_time_ = kNoTimestamp; | |
| 456 | 437 |
| 457 audio_buffer_stream_->Initialize( | 438 audio_buffer_stream_->Initialize( |
| 458 stream, base::Bind(&AudioRendererImpl::OnAudioBufferStreamInitialized, | 439 stream, base::Bind(&AudioRendererImpl::OnAudioBufferStreamInitialized, |
| 459 weak_factory_.GetWeakPtr()), | 440 weak_factory_.GetWeakPtr()), |
| 460 cdm_context, base::Bind(&AudioRendererImpl::OnStatisticsUpdate, | 441 cdm_context, base::Bind(&AudioRendererImpl::OnStatisticsUpdate, |
| 461 weak_factory_.GetWeakPtr()), | 442 weak_factory_.GetWeakPtr()), |
| 462 base::Bind(&AudioRendererImpl::OnWaitingForDecryptionKey, | 443 base::Bind(&AudioRendererImpl::OnWaitingForDecryptionKey, |
| 463 weak_factory_.GetWeakPtr())); | 444 weak_factory_.GetWeakPtr())); |
| 464 } | 445 } |
| 465 | 446 |
| (...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 977 DCHECK_NE(buffering_state_, buffering_state); | 958 DCHECK_NE(buffering_state_, buffering_state); |
| 978 lock_.AssertAcquired(); | 959 lock_.AssertAcquired(); |
| 979 buffering_state_ = buffering_state; | 960 buffering_state_ = buffering_state; |
| 980 | 961 |
| 981 task_runner_->PostTask( | 962 task_runner_->PostTask( |
| 982 FROM_HERE, base::Bind(&AudioRendererImpl::OnBufferingStateChange, | 963 FROM_HERE, base::Bind(&AudioRendererImpl::OnBufferingStateChange, |
| 983 weak_factory_.GetWeakPtr(), buffering_state_)); | 964 weak_factory_.GetWeakPtr(), buffering_state_)); |
| 984 } | 965 } |
| 985 | 966 |
| 986 } // namespace media | 967 } // namespace media |
| OLD | NEW |