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 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 167 |
168 void AudioRendererImpl::SetMediaTime(base::TimeDelta time) { | 168 void AudioRendererImpl::SetMediaTime(base::TimeDelta time) { |
169 DVLOG(1) << __FUNCTION__ << "(" << time << ")"; | 169 DVLOG(1) << __FUNCTION__ << "(" << time << ")"; |
170 DCHECK(task_runner_->BelongsToCurrentThread()); | 170 DCHECK(task_runner_->BelongsToCurrentThread()); |
171 | 171 |
172 base::AutoLock auto_lock(lock_); | 172 base::AutoLock auto_lock(lock_); |
173 DCHECK(!rendering_); | 173 DCHECK(!rendering_); |
174 DCHECK_EQ(state_, kFlushed); | 174 DCHECK_EQ(state_, kFlushed); |
175 | 175 |
176 start_timestamp_ = time; | 176 start_timestamp_ = time; |
177 ended_timestamp_ = kInfiniteDuration(); | 177 ended_timestamp_ = kInfiniteDuration; |
178 last_render_time_ = stop_rendering_time_ = base::TimeTicks(); | 178 last_render_time_ = stop_rendering_time_ = base::TimeTicks(); |
179 first_packet_timestamp_ = kNoTimestamp(); | 179 first_packet_timestamp_ = kNoTimestamp; |
180 last_media_timestamp_ = base::TimeDelta(); | 180 last_media_timestamp_ = base::TimeDelta(); |
181 audio_clock_.reset(new AudioClock(time, audio_parameters_.sample_rate())); | 181 audio_clock_.reset(new AudioClock(time, audio_parameters_.sample_rate())); |
182 } | 182 } |
183 | 183 |
184 base::TimeDelta AudioRendererImpl::CurrentMediaTime() { | 184 base::TimeDelta AudioRendererImpl::CurrentMediaTime() { |
185 base::AutoLock auto_lock(lock_); | 185 base::AutoLock auto_lock(lock_); |
186 | 186 |
187 // Return the current time based on the known extents of the rendered audio | 187 // Return the current time based on the known extents of the rendered audio |
188 // data plus an estimate based on the last time those values were calculated. | 188 // data plus an estimate based on the last time those values were calculated. |
189 base::TimeDelta current_media_time = audio_clock_->front_timestamp(); | 189 base::TimeDelta current_media_time = audio_clock_->front_timestamp(); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 DoFlush_Locked(); | 292 DoFlush_Locked(); |
293 } | 293 } |
294 | 294 |
295 void AudioRendererImpl::DoFlush_Locked() { | 295 void AudioRendererImpl::DoFlush_Locked() { |
296 DCHECK(task_runner_->BelongsToCurrentThread()); | 296 DCHECK(task_runner_->BelongsToCurrentThread()); |
297 lock_.AssertAcquired(); | 297 lock_.AssertAcquired(); |
298 | 298 |
299 DCHECK(!pending_read_); | 299 DCHECK(!pending_read_); |
300 DCHECK_EQ(state_, kFlushed); | 300 DCHECK_EQ(state_, kFlushed); |
301 | 301 |
302 ended_timestamp_ = kInfiniteDuration(); | 302 ended_timestamp_ = kInfiniteDuration; |
303 audio_buffer_stream_->Reset(base::Bind(&AudioRendererImpl::ResetDecoderDone, | 303 audio_buffer_stream_->Reset(base::Bind(&AudioRendererImpl::ResetDecoderDone, |
304 weak_factory_.GetWeakPtr())); | 304 weak_factory_.GetWeakPtr())); |
305 } | 305 } |
306 | 306 |
307 void AudioRendererImpl::ResetDecoderDone() { | 307 void AudioRendererImpl::ResetDecoderDone() { |
308 DCHECK(task_runner_->BelongsToCurrentThread()); | 308 DCHECK(task_runner_->BelongsToCurrentThread()); |
309 { | 309 { |
310 base::AutoLock auto_lock(lock_); | 310 base::AutoLock auto_lock(lock_); |
311 | 311 |
312 DCHECK_EQ(state_, kFlushed); | 312 DCHECK_EQ(state_, kFlushed); |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
658 if (!buffer->frame_count()) | 658 if (!buffer->frame_count()) |
659 return true; | 659 return true; |
660 } | 660 } |
661 | 661 |
662 if (state_ != kUninitialized) | 662 if (state_ != kUninitialized) |
663 algorithm_->EnqueueBuffer(buffer); | 663 algorithm_->EnqueueBuffer(buffer); |
664 } | 664 } |
665 | 665 |
666 // Store the timestamp of the first packet so we know when to start actual | 666 // Store the timestamp of the first packet so we know when to start actual |
667 // audio playback. | 667 // audio playback. |
668 if (first_packet_timestamp_ == kNoTimestamp()) | 668 if (first_packet_timestamp_ == kNoTimestamp) |
669 first_packet_timestamp_ = buffer->timestamp(); | 669 first_packet_timestamp_ = buffer->timestamp(); |
670 | 670 |
671 const size_t memory_usage = algorithm_->GetMemoryUsage(); | 671 const size_t memory_usage = algorithm_->GetMemoryUsage(); |
672 PipelineStatistics stats; | 672 PipelineStatistics stats; |
673 stats.audio_memory_usage = memory_usage - last_audio_memory_usage_; | 673 stats.audio_memory_usage = memory_usage - last_audio_memory_usage_; |
674 last_audio_memory_usage_ = memory_usage; | 674 last_audio_memory_usage_ = memory_usage; |
675 task_runner_->PostTask(FROM_HERE, | 675 task_runner_->PostTask(FROM_HERE, |
676 base::Bind(&AudioRendererImpl::OnStatisticsUpdate, | 676 base::Bind(&AudioRendererImpl::OnStatisticsUpdate, |
677 weak_factory_.GetWeakPtr(), stats)); | 677 weak_factory_.GetWeakPtr(), stats)); |
678 | 678 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
803 // Mute audio by returning 0 when not playing. | 803 // Mute audio by returning 0 when not playing. |
804 if (state_ != kPlaying) { | 804 if (state_ != kPlaying) { |
805 audio_clock_->WroteAudio(0, frames_requested, frames_delayed, | 805 audio_clock_->WroteAudio(0, frames_requested, frames_delayed, |
806 playback_rate_); | 806 playback_rate_); |
807 return 0; | 807 return 0; |
808 } | 808 } |
809 | 809 |
810 // Delay playback by writing silence if we haven't reached the first | 810 // Delay playback by writing silence if we haven't reached the first |
811 // timestamp yet; this can occur if the video starts before the audio. | 811 // timestamp yet; this can occur if the video starts before the audio. |
812 if (algorithm_->frames_buffered() > 0) { | 812 if (algorithm_->frames_buffered() > 0) { |
813 CHECK_NE(first_packet_timestamp_, kNoTimestamp()); | 813 CHECK_NE(first_packet_timestamp_, kNoTimestamp); |
814 CHECK_GE(first_packet_timestamp_, base::TimeDelta()); | 814 CHECK_GE(first_packet_timestamp_, base::TimeDelta()); |
815 const base::TimeDelta play_delay = | 815 const base::TimeDelta play_delay = |
816 first_packet_timestamp_ - audio_clock_->back_timestamp(); | 816 first_packet_timestamp_ - audio_clock_->back_timestamp(); |
817 if (play_delay > base::TimeDelta()) { | 817 if (play_delay > base::TimeDelta()) { |
818 DCHECK_EQ(frames_written, 0); | 818 DCHECK_EQ(frames_written, 0); |
819 | 819 |
820 // Don't multiply |play_delay| out since it can be a huge value on | 820 // Don't multiply |play_delay| out since it can be a huge value on |
821 // poorly encoded media and multiplying by the sample rate could cause | 821 // poorly encoded media and multiplying by the sample rate could cause |
822 // the value to overflow. | 822 // the value to overflow. |
823 if (play_delay.InSecondsF() > static_cast<double>(frames_requested) / | 823 if (play_delay.InSecondsF() > static_cast<double>(frames_requested) / |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
857 // we've rendered all known audio data. Doing so simplifies scenarios where | 857 // we've rendered all known audio data. Doing so simplifies scenarios where |
858 // we have other sources of media data that need to be scheduled after audio | 858 // we have other sources of media data that need to be scheduled after audio |
859 // data has ended. | 859 // data has ended. |
860 // | 860 // |
861 // That being said, we don't want to advance time when underflowed as we | 861 // That being said, we don't want to advance time when underflowed as we |
862 // know more decoded frames will eventually arrive. If we did, we would | 862 // know more decoded frames will eventually arrive. If we did, we would |
863 // throw things out of sync when said decoded frames arrive. | 863 // throw things out of sync when said decoded frames arrive. |
864 int frames_after_end_of_stream = 0; | 864 int frames_after_end_of_stream = 0; |
865 if (frames_written == 0) { | 865 if (frames_written == 0) { |
866 if (received_end_of_stream_) { | 866 if (received_end_of_stream_) { |
867 if (ended_timestamp_ == kInfiniteDuration()) | 867 if (ended_timestamp_ == kInfiniteDuration) |
868 ended_timestamp_ = audio_clock_->back_timestamp(); | 868 ended_timestamp_ = audio_clock_->back_timestamp(); |
869 frames_after_end_of_stream = frames_requested; | 869 frames_after_end_of_stream = frames_requested; |
870 } else if (state_ == kPlaying && | 870 } else if (state_ == kPlaying && |
871 buffering_state_ != BUFFERING_HAVE_NOTHING) { | 871 buffering_state_ != BUFFERING_HAVE_NOTHING) { |
872 algorithm_->IncreaseQueueCapacity(); | 872 algorithm_->IncreaseQueueCapacity(); |
873 SetBufferingState_Locked(BUFFERING_HAVE_NOTHING); | 873 SetBufferingState_Locked(BUFFERING_HAVE_NOTHING); |
874 } | 874 } |
875 } | 875 } |
876 | 876 |
877 audio_clock_->WroteAudio(frames_written + frames_after_end_of_stream, | 877 audio_clock_->WroteAudio(frames_written + frames_after_end_of_stream, |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
968 DCHECK_NE(buffering_state_, buffering_state); | 968 DCHECK_NE(buffering_state_, buffering_state); |
969 lock_.AssertAcquired(); | 969 lock_.AssertAcquired(); |
970 buffering_state_ = buffering_state; | 970 buffering_state_ = buffering_state; |
971 | 971 |
972 task_runner_->PostTask( | 972 task_runner_->PostTask( |
973 FROM_HERE, base::Bind(&AudioRendererImpl::OnBufferingStateChange, | 973 FROM_HERE, base::Bind(&AudioRendererImpl::OnBufferingStateChange, |
974 weak_factory_.GetWeakPtr(), buffering_state_)); | 974 weak_factory_.GetWeakPtr(), buffering_state_)); |
975 } | 975 } |
976 | 976 |
977 } // namespace media | 977 } // namespace media |
OLD | NEW |