OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/video_renderer_impl.h" | 5 #include "media/renderers/video_renderer_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
206 | 206 |
207 // Due to how the |algorithm_| holds frames, this should never be null if | 207 // Due to how the |algorithm_| holds frames, this should never be null if |
208 // we've had a proper startup sequence. | 208 // we've had a proper startup sequence. |
209 DCHECK(result); | 209 DCHECK(result); |
210 | 210 |
211 // Declare HAVE_NOTHING if we reach a state where we can't progress playback | 211 // Declare HAVE_NOTHING if we reach a state where we can't progress playback |
212 // any further. We don't want to do this if we've already done so, reached | 212 // any further. We don't want to do this if we've already done so, reached |
213 // end of stream, or have frames available. We also don't want to do this in | 213 // end of stream, or have frames available. We also don't want to do this in |
214 // background rendering mode unless this isn't the first background render | 214 // background rendering mode unless this isn't the first background render |
215 // tick and we haven't seen any decoded frames since the last one. | 215 // tick and we haven't seen any decoded frames since the last one. |
216 const size_t effective_frames = MaybeFireEndedCallback(); | 216 // |
217 // We use the inverse of |render_first_frame_and_stop_| as a proxy for the | |
218 // value of |time_progressing_| here since we can't access it from the | |
219 // compositor thread. If we're here (in Render()) the sink must have been | |
220 // started -- but if it was started only to render the first frame and stop, | |
221 // then |time_progressing_| is likely false. If we're still in Render() when | |
222 // |render_first_frame_and_stop_| is false, then |time_progressing_| is true. | |
223 // If |time_progressing_| is actually true when |render_first_frame_and_stop_| | |
224 // is also true, then the ended callback will be harmlessly delayed until | |
225 // MaybeStopSinkAfterFirstPaint() runs and the next Render() call comes in. | |
xhwang
2015/07/22 00:47:00
I hope we can simplify this a bit one day :)
| |
226 const size_t effective_frames = | |
227 MaybeFireEndedCallback_Locked(!render_first_frame_and_stop_); | |
217 if (buffering_state_ == BUFFERING_HAVE_ENOUGH && !received_end_of_stream_ && | 228 if (buffering_state_ == BUFFERING_HAVE_ENOUGH && !received_end_of_stream_ && |
218 !effective_frames && (!background_rendering || | 229 !effective_frames && (!background_rendering || |
219 (!frames_decoded_ && was_background_rendering_))) { | 230 (!frames_decoded_ && was_background_rendering_))) { |
220 // Do not set |buffering_state_| here as the lock in FrameReady() may be | 231 // Do not set |buffering_state_| here as the lock in FrameReady() may be |
221 // held already and it fire the state changes in the wrong order. | 232 // held already and it fire the state changes in the wrong order. |
222 task_runner_->PostTask( | 233 task_runner_->PostTask( |
223 FROM_HERE, base::Bind(&VideoRendererImpl::TransitionToHaveNothing, | 234 FROM_HERE, base::Bind(&VideoRendererImpl::TransitionToHaveNothing, |
224 weak_factory_.GetWeakPtr())); | 235 weak_factory_.GetWeakPtr())); |
225 } | 236 } |
226 | 237 |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
489 DCHECK_EQ(status, VideoFrameStream::DEMUXER_READ_ABORTED); | 500 DCHECK_EQ(status, VideoFrameStream::DEMUXER_READ_ABORTED); |
490 return; | 501 return; |
491 } | 502 } |
492 | 503 |
493 if (frame->metadata()->IsTrue(VideoFrameMetadata::END_OF_STREAM)) { | 504 if (frame->metadata()->IsTrue(VideoFrameMetadata::END_OF_STREAM)) { |
494 DCHECK(!received_end_of_stream_); | 505 DCHECK(!received_end_of_stream_); |
495 received_end_of_stream_ = true; | 506 received_end_of_stream_ = true; |
496 | 507 |
497 // See if we can fire EOS immediately instead of waiting for Render(). | 508 // See if we can fire EOS immediately instead of waiting for Render(). |
498 if (use_new_video_renderering_path_) | 509 if (use_new_video_renderering_path_) |
499 MaybeFireEndedCallback(); | 510 MaybeFireEndedCallback_Locked(time_progressing_); |
500 } else { | 511 } else { |
501 // Maintain the latest frame decoded so the correct frame is displayed | 512 // Maintain the latest frame decoded so the correct frame is displayed |
502 // after prerolling has completed. | 513 // after prerolling has completed. |
503 if (frame->timestamp() <= start_timestamp_) { | 514 if (frame->timestamp() <= start_timestamp_) { |
504 if (use_new_video_renderering_path_) | 515 if (use_new_video_renderering_path_) |
505 algorithm_->Reset(); | 516 algorithm_->Reset(); |
506 ready_frames_.clear(); | 517 ready_frames_.clear(); |
507 } | 518 } |
508 AddReadyFrame_Locked(frame); | 519 AddReadyFrame_Locked(frame); |
509 } | 520 } |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
701 } | 712 } |
702 | 713 |
703 if (wait_duration > base::TimeDelta()) | 714 if (wait_duration > base::TimeDelta()) |
704 frame_available_.TimedWait(wait_duration); | 715 frame_available_.TimedWait(wait_duration); |
705 } | 716 } |
706 | 717 |
707 void VideoRendererImpl::MaybeStopSinkAfterFirstPaint() { | 718 void VideoRendererImpl::MaybeStopSinkAfterFirstPaint() { |
708 DCHECK(task_runner_->BelongsToCurrentThread()); | 719 DCHECK(task_runner_->BelongsToCurrentThread()); |
709 DCHECK(use_new_video_renderering_path_); | 720 DCHECK(use_new_video_renderering_path_); |
710 | 721 |
711 { | |
712 base::AutoLock auto_lock(lock_); | |
713 render_first_frame_and_stop_ = false; | |
714 } | |
715 | |
716 if (!time_progressing_ && sink_started_) | 722 if (!time_progressing_ && sink_started_) |
717 StopSink(); | 723 StopSink(); |
724 | |
725 base::AutoLock auto_lock(lock_); | |
726 render_first_frame_and_stop_ = false; | |
718 } | 727 } |
719 | 728 |
720 bool VideoRendererImpl::HaveReachedBufferingCap() { | 729 bool VideoRendererImpl::HaveReachedBufferingCap() { |
721 DCHECK(task_runner_->BelongsToCurrentThread()); | 730 DCHECK(task_runner_->BelongsToCurrentThread()); |
722 const size_t kMaxVideoFrames = limits::kMaxVideoFrames; | 731 const size_t kMaxVideoFrames = limits::kMaxVideoFrames; |
723 | 732 |
724 if (use_new_video_renderering_path_) { | 733 if (use_new_video_renderering_path_) { |
725 // When the display rate is less than the frame rate, the effective frames | 734 // When the display rate is less than the frame rate, the effective frames |
726 // queued may be much smaller than the actual number of frames queued. Here | 735 // queued may be much smaller than the actual number of frames queued. Here |
727 // we ensure that frames_queued() doesn't get excessive. | 736 // we ensure that frames_queued() doesn't get excessive. |
(...skipping 13 matching lines...) Expand all Loading... | |
741 } | 750 } |
742 | 751 |
743 void VideoRendererImpl::StopSink() { | 752 void VideoRendererImpl::StopSink() { |
744 DCHECK(task_runner_->BelongsToCurrentThread()); | 753 DCHECK(task_runner_->BelongsToCurrentThread()); |
745 sink_->Stop(); | 754 sink_->Stop(); |
746 algorithm_->set_time_stopped(); | 755 algorithm_->set_time_stopped(); |
747 sink_started_ = false; | 756 sink_started_ = false; |
748 was_background_rendering_ = false; | 757 was_background_rendering_ = false; |
749 } | 758 } |
750 | 759 |
751 size_t VideoRendererImpl::MaybeFireEndedCallback() { | 760 size_t VideoRendererImpl::MaybeFireEndedCallback_Locked(bool time_progressing) { |
761 lock_.AssertAcquired(); | |
762 | |
752 // If there's only one frame in the video or Render() was never called, the | 763 // If there's only one frame in the video or Render() was never called, the |
753 // algorithm will have one frame linger indefinitely. So in cases where the | 764 // algorithm will have one frame linger indefinitely. So in cases where the |
754 // frame duration is unknown and we've received EOS, fire it once we get down | 765 // frame duration is unknown and we've received EOS, fire it once we get down |
755 // to a single frame. | 766 // to a single frame. |
756 const size_t effective_frames = algorithm_->EffectiveFramesQueued(); | 767 const size_t effective_frames = algorithm_->EffectiveFramesQueued(); |
757 | 768 |
758 // Don't fire ended if we haven't received EOS or have already done so. | 769 // Don't fire ended if we haven't received EOS or have already done so. |
759 if (!received_end_of_stream_ || rendered_end_of_stream_) | 770 if (!received_end_of_stream_ || rendered_end_of_stream_) |
760 return effective_frames; | 771 return effective_frames; |
761 | 772 |
762 // Don't fire ended if time isn't moving and we have frames. | 773 // Don't fire ended if time isn't moving and we have frames. |
763 if (!time_progressing_ && algorithm_->frames_queued()) | 774 if (!time_progressing && algorithm_->frames_queued()) |
764 return effective_frames; | 775 return effective_frames; |
765 | 776 |
766 // Fire ended if we have no more effective frames or only ever had one frame. | 777 // Fire ended if we have no more effective frames or only ever had one frame. |
767 if (!effective_frames || | 778 if (!effective_frames || |
768 (algorithm_->frames_queued() == 1u && | 779 (algorithm_->frames_queued() == 1u && |
769 algorithm_->average_frame_duration() == base::TimeDelta())) { | 780 algorithm_->average_frame_duration() == base::TimeDelta())) { |
770 rendered_end_of_stream_ = true; | 781 rendered_end_of_stream_ = true; |
771 task_runner_->PostTask(FROM_HERE, ended_cb_); | 782 task_runner_->PostTask(FROM_HERE, ended_cb_); |
772 } | 783 } |
773 | 784 |
774 return effective_frames; | 785 return effective_frames; |
775 } | 786 } |
776 | 787 |
777 base::TimeTicks VideoRendererImpl::ConvertMediaTimestamp( | 788 base::TimeTicks VideoRendererImpl::ConvertMediaTimestamp( |
778 base::TimeDelta media_time) { | 789 base::TimeDelta media_time) { |
779 std::vector<base::TimeDelta> media_times(1, media_time); | 790 std::vector<base::TimeDelta> media_times(1, media_time); |
780 std::vector<base::TimeTicks> wall_clock_times; | 791 std::vector<base::TimeTicks> wall_clock_times; |
781 if (!wall_clock_time_cb_.Run(media_times, &wall_clock_times)) | 792 if (!wall_clock_time_cb_.Run(media_times, &wall_clock_times)) |
782 return base::TimeTicks(); | 793 return base::TimeTicks(); |
783 return wall_clock_times[0]; | 794 return wall_clock_times[0]; |
784 } | 795 } |
785 | 796 |
786 } // namespace media | 797 } // namespace media |
OLD | NEW |