| 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/location.h" | 10 #include "base/location.h" |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 | 183 |
| 184 // The number of milliseconds to idle when we do not have anything to do. | 184 // The number of milliseconds to idle when we do not have anything to do. |
| 185 // Nothing special about the value, other than we're being more OS-friendly | 185 // Nothing special about the value, other than we're being more OS-friendly |
| 186 // than sleeping for 1 millisecond. | 186 // than sleeping for 1 millisecond. |
| 187 // | 187 // |
| 188 // TODO(scherkus): switch to pure event-driven frame timing instead of this | 188 // TODO(scherkus): switch to pure event-driven frame timing instead of this |
| 189 // kIdleTimeDelta business http://crbug.com/106874 | 189 // kIdleTimeDelta business http://crbug.com/106874 |
| 190 const base::TimeDelta kIdleTimeDelta = | 190 const base::TimeDelta kIdleTimeDelta = |
| 191 base::TimeDelta::FromMilliseconds(10); | 191 base::TimeDelta::FromMilliseconds(10); |
| 192 | 192 |
| 193 // If we have no frames and haven't painted any frame for certain amount of | |
| 194 // time, declare BUFFERING_HAVE_NOTHING. | |
| 195 const base::TimeDelta kTimeToDeclareHaveNothing = | |
| 196 base::TimeDelta::FromSeconds(3); | |
| 197 | |
| 198 for (;;) { | 193 for (;;) { |
| 199 base::AutoLock auto_lock(lock_); | 194 base::AutoLock auto_lock(lock_); |
| 200 | 195 |
| 201 // Thread exit condition. | 196 // Thread exit condition. |
| 202 if (is_shutting_down_) | 197 if (is_shutting_down_) |
| 203 return; | 198 return; |
| 204 | 199 |
| 205 // Remain idle as long as we're not playing. | 200 // Remain idle as long as we're not playing. |
| 206 if (state_ != kPlaying || buffering_state_ != BUFFERING_HAVE_ENOUGH) { | 201 if (state_ != kPlaying || buffering_state_ != BUFFERING_HAVE_ENOUGH) { |
| 207 UpdateStatsAndWait_Locked(kIdleTimeDelta); | 202 UpdateStatsAndWait_Locked(kIdleTimeDelta); |
| 208 continue; | 203 continue; |
| 209 } | 204 } |
| 210 | 205 |
| 211 base::TimeTicks now = tick_clock_->NowTicks(); | 206 base::TimeTicks now = tick_clock_->NowTicks(); |
| 212 | 207 |
| 213 // Remain idle until we have the next frame ready for rendering. | 208 // Remain idle until we have the next frame ready for rendering. |
| 214 if (ready_frames_.empty()) { | 209 if (ready_frames_.empty()) { |
| 210 base::TimeDelta wait_time = kIdleTimeDelta; |
| 215 if (received_end_of_stream_) { | 211 if (received_end_of_stream_) { |
| 216 if (!rendered_end_of_stream_) { | 212 if (!rendered_end_of_stream_) { |
| 217 rendered_end_of_stream_ = true; | 213 rendered_end_of_stream_ = true; |
| 218 task_runner_->PostTask(FROM_HERE, ended_cb_); | 214 task_runner_->PostTask(FROM_HERE, ended_cb_); |
| 219 } | 215 } |
| 220 } else if (!last_painted_time_.is_null() && | 216 } else if (now >= latest_possible_paint_time_) { |
| 221 now - last_painted_time_ >= kTimeToDeclareHaveNothing) { | 217 // Declare HAVE_NOTHING if we don't have another frame by the time we |
| 218 // are ready to paint the next one. |
| 222 buffering_state_ = BUFFERING_HAVE_NOTHING; | 219 buffering_state_ = BUFFERING_HAVE_NOTHING; |
| 223 task_runner_->PostTask( | 220 task_runner_->PostTask( |
| 224 FROM_HERE, base::Bind(buffering_state_cb_, BUFFERING_HAVE_NOTHING)); | 221 FROM_HERE, base::Bind(buffering_state_cb_, BUFFERING_HAVE_NOTHING)); |
| 222 } else { |
| 223 wait_time = std::min(kIdleTimeDelta, latest_possible_paint_time_ - now); |
| 225 } | 224 } |
| 226 | 225 |
| 227 UpdateStatsAndWait_Locked(kIdleTimeDelta); | 226 UpdateStatsAndWait_Locked(wait_time); |
| 228 continue; | 227 continue; |
| 229 } | 228 } |
| 230 | 229 |
| 231 base::TimeTicks target_paint_time = | 230 base::TimeTicks target_paint_time = |
| 232 wall_clock_time_cb_.Run(ready_frames_.front()->timestamp()); | 231 wall_clock_time_cb_.Run(ready_frames_.front()->timestamp()); |
| 233 | 232 |
| 234 // If media time has stopped, don't attempt to paint any more frames. | 233 // If media time has stopped, don't attempt to paint any more frames. |
| 235 if (target_paint_time.is_null()) { | 234 if (target_paint_time.is_null()) { |
| 236 UpdateStatsAndWait_Locked(kIdleTimeDelta); | 235 UpdateStatsAndWait_Locked(kIdleTimeDelta); |
| 237 continue; | 236 continue; |
| 238 } | 237 } |
| 239 | 238 |
| 240 base::TimeTicks latest_possible_paint_time; | |
| 241 | |
| 242 // Deadline is defined as the duration between this frame and the next | 239 // Deadline is defined as the duration between this frame and the next |
| 243 // frame, using the delta between this frame and the previous frame as the | 240 // frame, using the delta between this frame and the previous frame as the |
| 244 // assumption for frame duration. | 241 // assumption for frame duration. |
| 245 // | 242 // |
| 246 // TODO(scherkus): This can be vastly improved. Use a histogram to measure | 243 // TODO(scherkus): This can be vastly improved. Use a histogram to measure |
| 247 // the accuracy of our frame timing code. http://crbug.com/149829 | 244 // the accuracy of our frame timing code. http://crbug.com/149829 |
| 248 if (last_media_time_.is_null()) { | 245 if (last_media_time_.is_null()) { |
| 249 latest_possible_paint_time = now; | 246 latest_possible_paint_time_ = now; |
| 250 } else { | 247 } else { |
| 251 base::TimeDelta duration = target_paint_time - last_media_time_; | 248 base::TimeDelta duration = target_paint_time - last_media_time_; |
| 252 latest_possible_paint_time = target_paint_time + duration; | 249 latest_possible_paint_time_ = target_paint_time + duration; |
| 253 } | 250 } |
| 254 | 251 |
| 255 // Remain idle until we've reached our target paint window. | 252 // Remain idle until we've reached our target paint window. |
| 256 if (now < target_paint_time) { | 253 if (now < target_paint_time) { |
| 257 UpdateStatsAndWait_Locked( | 254 UpdateStatsAndWait_Locked( |
| 258 std::min(target_paint_time - now, kIdleTimeDelta)); | 255 std::min(target_paint_time - now, kIdleTimeDelta)); |
| 259 continue; | 256 continue; |
| 260 } | 257 } |
| 261 | 258 |
| 262 if (ready_frames_.size() > 1 && now > latest_possible_paint_time && | 259 if (ready_frames_.size() > 1 && now > latest_possible_paint_time_ && |
| 263 drop_frames_) { | 260 drop_frames_) { |
| 264 DropNextReadyFrame_Locked(); | 261 DropNextReadyFrame_Locked(); |
| 265 continue; | 262 continue; |
| 266 } | 263 } |
| 267 | 264 |
| 268 // Congratulations! You've made it past the video frame timing gauntlet. | 265 // Congratulations! You've made it past the video frame timing gauntlet. |
| 269 // | 266 // |
| 270 // At this point enough time has passed that the next frame that ready for | 267 // At this point enough time has passed that the next frame that ready for |
| 271 // rendering. | 268 // rendering. |
| 272 PaintNextReadyFrame_Locked(); | 269 PaintNextReadyFrame_Locked(); |
| 273 } | 270 } |
| 274 } | 271 } |
| 275 | 272 |
| 276 void VideoRendererImpl::SetTickClockForTesting( | 273 void VideoRendererImpl::SetTickClockForTesting( |
| 277 scoped_ptr<base::TickClock> tick_clock) { | 274 scoped_ptr<base::TickClock> tick_clock) { |
| 278 tick_clock_.swap(tick_clock); | 275 tick_clock_.swap(tick_clock); |
| 279 } | 276 } |
| 280 | 277 |
| 281 void VideoRendererImpl::PaintNextReadyFrame_Locked() { | 278 void VideoRendererImpl::PaintNextReadyFrame_Locked() { |
| 282 lock_.AssertAcquired(); | 279 lock_.AssertAcquired(); |
| 283 | 280 |
| 284 scoped_refptr<VideoFrame> next_frame = ready_frames_.front(); | 281 scoped_refptr<VideoFrame> next_frame = ready_frames_.front(); |
| 285 ready_frames_.pop_front(); | 282 ready_frames_.pop_front(); |
| 286 frames_decoded_++; | 283 frames_decoded_++; |
| 287 | 284 |
| 288 last_media_time_ = last_painted_time_ = | 285 last_media_time_ = wall_clock_time_cb_.Run(next_frame->timestamp()); |
| 289 wall_clock_time_cb_.Run(next_frame->timestamp()); | |
| 290 | 286 |
| 291 paint_cb_.Run(next_frame); | 287 paint_cb_.Run(next_frame); |
| 292 | 288 |
| 293 task_runner_->PostTask( | 289 task_runner_->PostTask( |
| 294 FROM_HERE, | 290 FROM_HERE, |
| 295 base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr())); | 291 base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr())); |
| 296 } | 292 } |
| 297 | 293 |
| 298 void VideoRendererImpl::DropNextReadyFrame_Locked() { | 294 void VideoRendererImpl::DropNextReadyFrame_Locked() { |
| 299 TRACE_EVENT0("media", "VideoRendererImpl:frameDropped"); | 295 TRACE_EVENT0("media", "VideoRendererImpl:frameDropped"); |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 void VideoRendererImpl::OnVideoFrameStreamResetDone() { | 432 void VideoRendererImpl::OnVideoFrameStreamResetDone() { |
| 437 base::AutoLock auto_lock(lock_); | 433 base::AutoLock auto_lock(lock_); |
| 438 DCHECK_EQ(kFlushing, state_); | 434 DCHECK_EQ(kFlushing, state_); |
| 439 DCHECK(!pending_read_); | 435 DCHECK(!pending_read_); |
| 440 DCHECK(ready_frames_.empty()); | 436 DCHECK(ready_frames_.empty()); |
| 441 DCHECK(!received_end_of_stream_); | 437 DCHECK(!received_end_of_stream_); |
| 442 DCHECK(!rendered_end_of_stream_); | 438 DCHECK(!rendered_end_of_stream_); |
| 443 DCHECK_EQ(buffering_state_, BUFFERING_HAVE_NOTHING); | 439 DCHECK_EQ(buffering_state_, BUFFERING_HAVE_NOTHING); |
| 444 | 440 |
| 445 state_ = kFlushed; | 441 state_ = kFlushed; |
| 446 last_media_time_ = last_painted_time_ = base::TimeTicks(); | 442 latest_possible_paint_time_ = last_media_time_ = base::TimeTicks(); |
| 447 base::ResetAndReturn(&flush_cb_).Run(); | 443 base::ResetAndReturn(&flush_cb_).Run(); |
| 448 } | 444 } |
| 449 | 445 |
| 450 void VideoRendererImpl::UpdateStatsAndWait_Locked( | 446 void VideoRendererImpl::UpdateStatsAndWait_Locked( |
| 451 base::TimeDelta wait_duration) { | 447 base::TimeDelta wait_duration) { |
| 452 lock_.AssertAcquired(); | 448 lock_.AssertAcquired(); |
| 453 DCHECK_GE(frames_decoded_, 0); | 449 DCHECK_GE(frames_decoded_, 0); |
| 454 DCHECK_LE(frames_dropped_, frames_decoded_); | 450 DCHECK_LE(frames_dropped_, frames_decoded_); |
| 455 | 451 |
| 456 if (frames_decoded_) { | 452 if (frames_decoded_) { |
| 457 PipelineStatistics statistics; | 453 PipelineStatistics statistics; |
| 458 statistics.video_frames_decoded = frames_decoded_; | 454 statistics.video_frames_decoded = frames_decoded_; |
| 459 statistics.video_frames_dropped = frames_dropped_; | 455 statistics.video_frames_dropped = frames_dropped_; |
| 460 task_runner_->PostTask(FROM_HERE, base::Bind(statistics_cb_, statistics)); | 456 task_runner_->PostTask(FROM_HERE, base::Bind(statistics_cb_, statistics)); |
| 461 | 457 |
| 462 frames_decoded_ = 0; | 458 frames_decoded_ = 0; |
| 463 frames_dropped_ = 0; | 459 frames_dropped_ = 0; |
| 464 } | 460 } |
| 465 | 461 |
| 466 frame_available_.TimedWait(wait_duration); | 462 frame_available_.TimedWait(wait_duration); |
| 467 } | 463 } |
| 468 | 464 |
| 469 } // namespace media | 465 } // namespace media |
| OLD | NEW |