| 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()) { |
| 215 if (received_end_of_stream_) { | 210 if (received_end_of_stream_) { |
| 216 if (!rendered_end_of_stream_) { | 211 if (!rendered_end_of_stream_) { |
| 217 rendered_end_of_stream_ = true; | 212 rendered_end_of_stream_ = true; |
| 218 task_runner_->PostTask(FROM_HERE, ended_cb_); | 213 task_runner_->PostTask(FROM_HERE, ended_cb_); |
| 219 } | 214 } |
| 220 } else if (!last_painted_time_.is_null() && | 215 } else { |
| 221 now - last_painted_time_ >= kTimeToDeclareHaveNothing) { | |
| 222 buffering_state_ = BUFFERING_HAVE_NOTHING; | 216 buffering_state_ = BUFFERING_HAVE_NOTHING; |
| 223 task_runner_->PostTask( | 217 task_runner_->PostTask( |
| 224 FROM_HERE, base::Bind(buffering_state_cb_, BUFFERING_HAVE_NOTHING)); | 218 FROM_HERE, base::Bind(buffering_state_cb_, BUFFERING_HAVE_NOTHING)); |
| 225 } | 219 } |
| 226 | 220 |
| 227 UpdateStatsAndWait_Locked(kIdleTimeDelta); | 221 UpdateStatsAndWait_Locked(kIdleTimeDelta); |
| 228 continue; | 222 continue; |
| 229 } | 223 } |
| 230 | 224 |
| 231 base::TimeTicks target_paint_time = | 225 base::TimeTicks target_paint_time = |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 tick_clock_.swap(tick_clock); | 272 tick_clock_.swap(tick_clock); |
| 279 } | 273 } |
| 280 | 274 |
| 281 void VideoRendererImpl::PaintNextReadyFrame_Locked() { | 275 void VideoRendererImpl::PaintNextReadyFrame_Locked() { |
| 282 lock_.AssertAcquired(); | 276 lock_.AssertAcquired(); |
| 283 | 277 |
| 284 scoped_refptr<VideoFrame> next_frame = ready_frames_.front(); | 278 scoped_refptr<VideoFrame> next_frame = ready_frames_.front(); |
| 285 ready_frames_.pop_front(); | 279 ready_frames_.pop_front(); |
| 286 frames_decoded_++; | 280 frames_decoded_++; |
| 287 | 281 |
| 288 last_media_time_ = last_painted_time_ = | 282 last_media_time_ = wall_clock_time_cb_.Run(next_frame->timestamp()); |
| 289 wall_clock_time_cb_.Run(next_frame->timestamp()); | |
| 290 | 283 |
| 291 paint_cb_.Run(next_frame); | 284 paint_cb_.Run(next_frame); |
| 292 | 285 |
| 293 task_runner_->PostTask( | 286 task_runner_->PostTask( |
| 294 FROM_HERE, | 287 FROM_HERE, |
| 295 base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr())); | 288 base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr())); |
| 296 } | 289 } |
| 297 | 290 |
| 298 void VideoRendererImpl::DropNextReadyFrame_Locked() { | 291 void VideoRendererImpl::DropNextReadyFrame_Locked() { |
| 299 TRACE_EVENT0("media", "VideoRendererImpl:frameDropped"); | 292 TRACE_EVENT0("media", "VideoRendererImpl:frameDropped"); |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 void VideoRendererImpl::OnVideoFrameStreamResetDone() { | 429 void VideoRendererImpl::OnVideoFrameStreamResetDone() { |
| 437 base::AutoLock auto_lock(lock_); | 430 base::AutoLock auto_lock(lock_); |
| 438 DCHECK_EQ(kFlushing, state_); | 431 DCHECK_EQ(kFlushing, state_); |
| 439 DCHECK(!pending_read_); | 432 DCHECK(!pending_read_); |
| 440 DCHECK(ready_frames_.empty()); | 433 DCHECK(ready_frames_.empty()); |
| 441 DCHECK(!received_end_of_stream_); | 434 DCHECK(!received_end_of_stream_); |
| 442 DCHECK(!rendered_end_of_stream_); | 435 DCHECK(!rendered_end_of_stream_); |
| 443 DCHECK_EQ(buffering_state_, BUFFERING_HAVE_NOTHING); | 436 DCHECK_EQ(buffering_state_, BUFFERING_HAVE_NOTHING); |
| 444 | 437 |
| 445 state_ = kFlushed; | 438 state_ = kFlushed; |
| 446 last_media_time_ = last_painted_time_ = base::TimeTicks(); | 439 last_media_time_ = base::TimeTicks(); |
| 447 base::ResetAndReturn(&flush_cb_).Run(); | 440 base::ResetAndReturn(&flush_cb_).Run(); |
| 448 } | 441 } |
| 449 | 442 |
| 450 void VideoRendererImpl::UpdateStatsAndWait_Locked( | 443 void VideoRendererImpl::UpdateStatsAndWait_Locked( |
| 451 base::TimeDelta wait_duration) { | 444 base::TimeDelta wait_duration) { |
| 452 lock_.AssertAcquired(); | 445 lock_.AssertAcquired(); |
| 453 DCHECK_GE(frames_decoded_, 0); | 446 DCHECK_GE(frames_decoded_, 0); |
| 454 DCHECK_LE(frames_dropped_, frames_decoded_); | 447 DCHECK_LE(frames_dropped_, frames_decoded_); |
| 455 | 448 |
| 456 if (frames_decoded_) { | 449 if (frames_decoded_) { |
| 457 PipelineStatistics statistics; | 450 PipelineStatistics statistics; |
| 458 statistics.video_frames_decoded = frames_decoded_; | 451 statistics.video_frames_decoded = frames_decoded_; |
| 459 statistics.video_frames_dropped = frames_dropped_; | 452 statistics.video_frames_dropped = frames_dropped_; |
| 460 task_runner_->PostTask(FROM_HERE, base::Bind(statistics_cb_, statistics)); | 453 task_runner_->PostTask(FROM_HERE, base::Bind(statistics_cb_, statistics)); |
| 461 | 454 |
| 462 frames_decoded_ = 0; | 455 frames_decoded_ = 0; |
| 463 frames_dropped_ = 0; | 456 frames_dropped_ = 0; |
| 464 } | 457 } |
| 465 | 458 |
| 466 frame_available_.TimedWait(wait_duration); | 459 frame_available_.TimedWait(wait_duration); |
| 467 } | 460 } |
| 468 | 461 |
| 469 } // namespace media | 462 } // namespace media |
| OLD | NEW |