BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / remoting / host / capture_scheduler.cc
blob556c20763c6e54534cb27dabdc4ab173970b8c7d
1 // Copyright (c) 2011 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.
5 #include "remoting/host/capture_scheduler.h"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "base/sys_info.h"
11 #include "base/time/default_tick_clock.h"
12 #include "base/time/time.h"
13 #include "remoting/proto/video.pb.h"
15 namespace {
17 // Number of samples to average the most recent capture and encode time
18 // over.
19 const int kStatisticsWindow = 3;
21 // The hard limit is 30fps or 33ms per recording cycle.
22 const int64 kDefaultMinimumIntervalMs = 33;
24 // Controls how much CPU time we can use for encode and capture.
25 // Range of this value is between 0 to 1. 0 means using 0% of of all CPUs
26 // available while 1 means using 100% of all CPUs available.
27 const double kRecordingCpuConsumption = 0.5;
29 // Maximum number of captured frames in the encoding queue. Currently capturer
30 // implementations do not allow to keep more than 2 DesktopFrame objects.
31 static const int kMaxFramesInEncodingQueue = 2;
33 // Maximum number of unacknowledged frames. Ignored if the client doesn't
34 // support ACKs. This value was chosen experimentally, using synthetic
35 // performance tests (see ProtocolPerfTest), to maximize frame rate, while
36 // keeping round-trip latency low.
37 static const int kMaxUnacknowledgedFrames = 4;
39 } // namespace
41 namespace remoting {
43 // We assume that the number of available cores is constant.
44 CaptureScheduler::CaptureScheduler(const base::Closure& capture_closure)
45 : capture_closure_(capture_closure),
46 tick_clock_(new base::DefaultTickClock()),
47 capture_timer_(new base::Timer(false, false)),
48 minimum_interval_(
49 base::TimeDelta::FromMilliseconds(kDefaultMinimumIntervalMs)),
50 num_of_processors_(base::SysInfo::NumberOfProcessors()),
51 capture_time_(kStatisticsWindow),
52 encode_time_(kStatisticsWindow),
53 num_encoding_frames_(0),
54 num_unacknowledged_frames_(0),
55 capture_pending_(false),
56 is_paused_(false),
57 next_frame_id_(0) {
58 DCHECK(num_of_processors_);
61 CaptureScheduler::~CaptureScheduler() {
64 void CaptureScheduler::Start() {
65 DCHECK(thread_checker_.CalledOnValidThread());
67 ScheduleNextCapture();
70 void CaptureScheduler::Pause(bool pause) {
71 DCHECK(thread_checker_.CalledOnValidThread());
73 if (is_paused_ != pause) {
74 is_paused_ = pause;
76 if (is_paused_) {
77 capture_timer_->Stop();
78 } else {
79 ScheduleNextCapture();
84 void CaptureScheduler::OnCaptureCompleted() {
85 DCHECK(thread_checker_.CalledOnValidThread());
87 capture_pending_ = false;
88 capture_time_.Record(
89 (tick_clock_->NowTicks() - last_capture_started_time_).InMilliseconds());
91 ++num_encoding_frames_;
93 ScheduleNextCapture();
96 void CaptureScheduler::OnFrameEncoded(VideoPacket* packet) {
97 DCHECK(thread_checker_.CalledOnValidThread());
99 // Set packet_id for the outgoing packet.
100 packet->set_frame_id(next_frame_id_);
101 ++next_frame_id_;
103 // Update internal stats.
104 encode_time_.Record(packet->encode_time_ms());
106 --num_encoding_frames_;
107 ++num_unacknowledged_frames_;
109 ScheduleNextCapture();
112 void CaptureScheduler::OnFrameSent() {
113 DCHECK(thread_checker_.CalledOnValidThread());
115 ScheduleNextCapture();
118 void CaptureScheduler::ProcessVideoAck(scoped_ptr<VideoAck> video_ack) {
119 DCHECK(thread_checker_.CalledOnValidThread());
121 --num_unacknowledged_frames_;
122 DCHECK_GE(num_unacknowledged_frames_, 0);
124 ScheduleNextCapture();
127 void CaptureScheduler::SetTickClockForTest(
128 scoped_ptr<base::TickClock> tick_clock) {
129 tick_clock_ = tick_clock.Pass();
132 void CaptureScheduler::SetTimerForTest(scoped_ptr<base::Timer> timer) {
133 capture_timer_ = timer.Pass();
136 void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) {
137 num_of_processors_ = num_of_processors;
140 void CaptureScheduler::ScheduleNextCapture() {
141 DCHECK(thread_checker_.CalledOnValidThread());
143 if (is_paused_ || capture_pending_ ||
144 num_encoding_frames_ >= kMaxFramesInEncodingQueue) {
145 return;
148 if (num_encoding_frames_ + num_unacknowledged_frames_ >=
149 kMaxUnacknowledgedFrames) {
150 return;
153 // Delay by an amount chosen such that if capture and encode times
154 // continue to follow the averages, then we'll consume the target
155 // fraction of CPU across all cores.
156 base::TimeDelta delay =
157 std::max(minimum_interval_,
158 base::TimeDelta::FromMilliseconds(
159 (capture_time_.Average() + encode_time_.Average()) /
160 (kRecordingCpuConsumption * num_of_processors_)));
162 // Account for the time that has passed since the last capture.
163 delay = std::max(base::TimeDelta(), delay - (tick_clock_->NowTicks() -
164 last_capture_started_time_));
166 capture_timer_->Start(
167 FROM_HERE, delay,
168 base::Bind(&CaptureScheduler::CaptureNextFrame, base::Unretained(this)));
171 void CaptureScheduler::CaptureNextFrame() {
172 DCHECK(thread_checker_.CalledOnValidThread());
173 DCHECK(!is_paused_);
174 DCHECK(!capture_pending_);
176 capture_pending_ = true;
177 last_capture_started_time_ = tick_clock_->NowTicks();
178 capture_closure_.Run();
181 } // namespace remoting