Compute can_use_lcd_text using property trees.
[chromium-blink-merge.git] / remoting / host / capture_scheduler.cc
blob56a2d28d715f616ca0ef4a88c8bdfc1cfac93bae
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 // Maximum number of frames that can be processed (captured, encoded or sent)
40 // simultaneously. It's used only in the case when the client doesn't support
41 // ACKs.
43 // TODO(sergeyu): Remove this once all current versions support ACKs.
44 // crbug.com/460963 .
45 static const int kMaxPendingFrames = 2;
47 } // namespace
49 namespace remoting {
51 // We assume that the number of available cores is constant.
52 CaptureScheduler::CaptureScheduler(const base::Closure& capture_closure)
53 : capture_closure_(capture_closure),
54 acks_supported_(false),
55 tick_clock_(new base::DefaultTickClock()),
56 capture_timer_(new base::Timer(false, false)),
57 minimum_interval_(
58 base::TimeDelta::FromMilliseconds(kDefaultMinimumIntervalMs)),
59 num_of_processors_(base::SysInfo::NumberOfProcessors()),
60 capture_time_(kStatisticsWindow),
61 encode_time_(kStatisticsWindow),
62 num_encoding_frames_(0),
63 num_sending_frames_(0),
64 num_unacknowledged_frames_(0),
65 capture_pending_(false),
66 is_paused_(false),
67 next_frame_id_(0) {
68 DCHECK(num_of_processors_);
71 CaptureScheduler::~CaptureScheduler() {
74 void CaptureScheduler::Start() {
75 DCHECK(thread_checker_.CalledOnValidThread());
77 ScheduleNextCapture();
80 void CaptureScheduler::Pause(bool pause) {
81 DCHECK(thread_checker_.CalledOnValidThread());
83 if (is_paused_ != pause) {
84 is_paused_ = pause;
86 if (is_paused_) {
87 capture_timer_->Stop();
88 } else {
89 ScheduleNextCapture();
94 void CaptureScheduler::OnCaptureCompleted() {
95 DCHECK(thread_checker_.CalledOnValidThread());
97 capture_pending_ = false;
98 capture_time_.Record(
99 (tick_clock_->NowTicks() - last_capture_started_time_).InMilliseconds());
101 ++num_encoding_frames_;
103 ScheduleNextCapture();
106 void CaptureScheduler::OnFrameEncoded(VideoPacket* packet) {
107 DCHECK(thread_checker_.CalledOnValidThread());
109 // Set packet_id for the outgoing packet.
110 packet->set_frame_id(next_frame_id_);
111 ++next_frame_id_;
113 // Update internal stats.
114 encode_time_.Record(packet->encode_time_ms());
116 --num_encoding_frames_;
117 ++num_sending_frames_;
118 ++num_unacknowledged_frames_;
120 ScheduleNextCapture();
123 void CaptureScheduler::OnFrameSent() {
124 DCHECK(thread_checker_.CalledOnValidThread());
126 --num_sending_frames_;
127 DCHECK_GE(num_sending_frames_, 0);
129 ScheduleNextCapture();
132 void CaptureScheduler::ProcessVideoAck(scoped_ptr<VideoAck> video_ack) {
133 DCHECK(thread_checker_.CalledOnValidThread());
135 // Host always sets |frame_id| field to indicated that it expects ACK from the
136 // client. It's assumed that the client doesn't support ACKs until the first
137 // ACK message is received.
138 acks_supported_ = true;
140 --num_unacknowledged_frames_;
141 DCHECK_GE(num_unacknowledged_frames_, 0);
143 ScheduleNextCapture();
146 void CaptureScheduler::SetTickClockForTest(
147 scoped_ptr<base::TickClock> tick_clock) {
148 tick_clock_ = tick_clock.Pass();
151 void CaptureScheduler::SetTimerForTest(scoped_ptr<base::Timer> timer) {
152 capture_timer_ = timer.Pass();
155 void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) {
156 num_of_processors_ = num_of_processors;
159 void CaptureScheduler::ScheduleNextCapture() {
160 DCHECK(thread_checker_.CalledOnValidThread());
162 if (is_paused_ || capture_pending_ ||
163 num_encoding_frames_ >= kMaxFramesInEncodingQueue) {
164 return;
167 if (acks_supported_) {
168 if (num_encoding_frames_ + num_unacknowledged_frames_ >=
169 kMaxUnacknowledgedFrames) {
170 return;
172 } else {
173 // TODO(sergeyu): Remove this once all current versions support ACKs.
174 // crbug.com/460963 .
175 if (num_encoding_frames_ + num_sending_frames_ >= kMaxPendingFrames) {
176 return;
180 // Delay by an amount chosen such that if capture and encode times
181 // continue to follow the averages, then we'll consume the target
182 // fraction of CPU across all cores.
183 base::TimeDelta delay =
184 std::max(minimum_interval_,
185 base::TimeDelta::FromMilliseconds(
186 (capture_time_.Average() + encode_time_.Average()) /
187 (kRecordingCpuConsumption * num_of_processors_)));
189 // Account for the time that has passed since the last capture.
190 delay = std::max(base::TimeDelta(), delay - (tick_clock_->NowTicks() -
191 last_capture_started_time_));
193 capture_timer_->Start(
194 FROM_HERE, delay,
195 base::Bind(&CaptureScheduler::CaptureNextFrame, base::Unretained(this)));
198 void CaptureScheduler::CaptureNextFrame() {
199 DCHECK(thread_checker_.CalledOnValidThread());
200 DCHECK(!is_paused_);
201 DCHECK(!capture_pending_);
203 capture_pending_ = true;
204 last_capture_started_time_ = tick_clock_->NowTicks();
205 capture_closure_.Run();
208 } // namespace remoting