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"
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"
16 // Number of samples to average the most recent capture and encode time
18 const int kStatisticsWindow
= 3;
20 // The hard limit is 30fps or 33ms per recording cycle.
21 const int64 kDefaultMinimumIntervalMs
= 33;
23 // Controls how much CPU time we can use for encode and capture.
24 // Range of this value is between 0 to 1. 0 means using 0% of of all CPUs
25 // available while 1 means using 100% of all CPUs available.
26 const double kRecordingCpuConsumption
= 0.5;
28 // Maximum number of frames that can be processed simultaneously.
29 static const int kMaxPendingFrames
= 2;
35 // We assume that the number of available cores is constant.
36 CaptureScheduler::CaptureScheduler(const base::Closure
& capture_closure
)
37 : capture_closure_(capture_closure
),
38 tick_clock_(new base::DefaultTickClock()),
39 capture_timer_(new base::Timer(false, false)),
41 base::TimeDelta::FromMilliseconds(kDefaultMinimumIntervalMs
)),
42 num_of_processors_(base::SysInfo::NumberOfProcessors()),
43 capture_time_(kStatisticsWindow
),
44 encode_time_(kStatisticsWindow
),
46 capture_pending_(false),
48 DCHECK(num_of_processors_
);
51 CaptureScheduler::~CaptureScheduler() {
54 void CaptureScheduler::Start() {
55 DCHECK(CalledOnValidThread());
57 ScheduleNextCapture();
60 void CaptureScheduler::Pause(bool pause
) {
61 DCHECK(CalledOnValidThread());
63 if (is_paused_
!= pause
) {
67 capture_timer_
->Stop();
69 ScheduleNextCapture();
74 void CaptureScheduler::OnCaptureCompleted() {
75 DCHECK(CalledOnValidThread());
77 capture_pending_
= false;
79 (tick_clock_
->NowTicks() - last_capture_started_time_
).InMilliseconds());
81 ScheduleNextCapture();
84 void CaptureScheduler::OnFrameSent() {
85 DCHECK(CalledOnValidThread());
87 // Decrement the pending capture count.
89 DCHECK_GE(pending_frames_
, 0);
91 ScheduleNextCapture();
94 void CaptureScheduler::OnFrameEncoded(base::TimeDelta encode_time
) {
95 DCHECK(CalledOnValidThread());
97 encode_time_
.Record(encode_time
.InMilliseconds());
98 ScheduleNextCapture();
101 void CaptureScheduler::SetTickClockForTest(
102 scoped_ptr
<base::TickClock
> tick_clock
) {
103 tick_clock_
= tick_clock
.Pass();
105 void CaptureScheduler::SetTimerForTest(scoped_ptr
<base::Timer
> timer
) {
106 capture_timer_
= timer
.Pass();
108 void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors
) {
109 num_of_processors_
= num_of_processors
;
112 void CaptureScheduler::ScheduleNextCapture() {
113 DCHECK(CalledOnValidThread());
115 if (is_paused_
|| pending_frames_
>= kMaxPendingFrames
|| capture_pending_
)
118 // Delay by an amount chosen such that if capture and encode times
119 // continue to follow the averages, then we'll consume the target
120 // fraction of CPU across all cores.
121 base::TimeDelta delay
=
122 std::max(minimum_interval_
,
123 base::TimeDelta::FromMilliseconds(
124 (capture_time_
.Average() + encode_time_
.Average()) /
125 (kRecordingCpuConsumption
* num_of_processors_
)));
127 // Account for the time that has passed since the last capture.
128 delay
= std::max(base::TimeDelta(), delay
- (tick_clock_
->NowTicks() -
129 last_capture_started_time_
));
131 capture_timer_
->Start(
133 base::Bind(&CaptureScheduler::CaptureNextFrame
, base::Unretained(this)));
136 void CaptureScheduler::CaptureNextFrame() {
137 DCHECK(CalledOnValidThread());
139 DCHECK(!capture_pending_
);
142 DCHECK_LE(pending_frames_
, kMaxPendingFrames
);
144 capture_pending_
= true;
145 last_capture_started_time_
= tick_clock_
->NowTicks();
146 capture_closure_
.Run();
149 } // namespace remoting