OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/base/null_video_sink.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
| 9 #include "base/location.h" |
| 10 #include "base/single_thread_task_runner.h" |
| 11 |
| 12 namespace media { |
| 13 |
| 14 NullVideoSink::NullVideoSink( |
| 15 bool clockless, |
| 16 base::TimeDelta interval, |
| 17 const NewFrameCB& new_frame_cb, |
| 18 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
| 19 : clockless_(clockless), |
| 20 interval_(interval), |
| 21 new_frame_cb_(new_frame_cb), |
| 22 task_runner_(task_runner), |
| 23 started_(false), |
| 24 callback_(nullptr), |
| 25 tick_clock_(&default_tick_clock_) { |
| 26 } |
| 27 |
| 28 NullVideoSink::~NullVideoSink() { |
| 29 DCHECK(!started_); |
| 30 } |
| 31 |
| 32 void NullVideoSink::Start(RenderCallback* callback) { |
| 33 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 34 DCHECK(!started_); |
| 35 callback_ = callback; |
| 36 started_ = true; |
| 37 last_now_ = current_render_time_ = tick_clock_->NowTicks(); |
| 38 cancelable_worker_.Reset( |
| 39 base::Bind(&NullVideoSink::CallRender, base::Unretained(this))); |
| 40 task_runner_->PostTask(FROM_HERE, cancelable_worker_.callback()); |
| 41 } |
| 42 |
| 43 void NullVideoSink::Stop() { |
| 44 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 45 cancelable_worker_.Cancel(); |
| 46 started_ = false; |
| 47 if (!stop_cb_.is_null()) |
| 48 base::ResetAndReturn(&stop_cb_).Run(); |
| 49 } |
| 50 |
| 51 void NullVideoSink::CallRender() { |
| 52 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 53 DCHECK(started_); |
| 54 |
| 55 const base::TimeTicks end_of_interval = current_render_time_ + interval_; |
| 56 if (current_render_time_ > pause_end_time_) { |
| 57 scoped_refptr<VideoFrame> new_frame = |
| 58 callback_->Render(current_render_time_, end_of_interval); |
| 59 const bool is_new_frame = new_frame != last_frame_; |
| 60 last_frame_ = new_frame; |
| 61 if (is_new_frame) |
| 62 new_frame_cb_.Run(new_frame); |
| 63 } |
| 64 |
| 65 current_render_time_ += interval_; |
| 66 |
| 67 if (clockless_) { |
| 68 task_runner_->PostTask(FROM_HERE, cancelable_worker_.callback()); |
| 69 return; |
| 70 } |
| 71 |
| 72 // Recompute now to compensate for the cost of Render(). |
| 73 const base::TimeTicks now = tick_clock_->NowTicks(); |
| 74 base::TimeDelta delay = current_render_time_ - now; |
| 75 |
| 76 // If we're behind, find the next nearest on time interval. |
| 77 if (delay < base::TimeDelta()) |
| 78 delay += interval_ * (-delay / interval_ + 1); |
| 79 current_render_time_ = now + delay; |
| 80 |
| 81 // The tick clock is frozen in this case, so clamp delay to the interval time. |
| 82 // We still want the interval passed to Render() to grow, but we also don't |
| 83 // want the delay used here to increase slowly over time. |
| 84 if (last_now_ == now && delay > interval_) |
| 85 delay = interval_; |
| 86 last_now_ = now; |
| 87 |
| 88 task_runner_->PostDelayedTask(FROM_HERE, cancelable_worker_.callback(), |
| 89 delay); |
| 90 } |
| 91 |
| 92 void NullVideoSink::PaintFrameUsingOldRenderingPath( |
| 93 const scoped_refptr<VideoFrame>& frame) { |
| 94 new_frame_cb_.Run(frame); |
| 95 } |
| 96 |
| 97 } // namespace media |
OLD | NEW |