forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture_scheduler.cc
184 lines (140 loc) · 5.22 KB
/
capture_scheduler.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/protocol/capture_scheduler.h"
#include <algorithm>
#include <utility>
#include "base/logging.h"
#include "base/sys_info.h"
#include "base/time/default_tick_clock.h"
#include "base/time/time.h"
#include "remoting/proto/video.pb.h"
namespace {
// Number of samples to average the most recent capture and encode time
// over.
const int kStatisticsWindow = 3;
// The hard limit is 30fps or 33ms per recording cycle.
const int64_t kDefaultMinimumIntervalMs = 33;
// Controls how much CPU time we can use for encode and capture.
// Range of this value is between 0 to 1. 0 means using 0% of of all CPUs
// available while 1 means using 100% of all CPUs available.
const double kRecordingCpuConsumption = 0.5;
// Maximum number of captured frames in the encoding queue. Currently capturer
// implementations do not allow to keep more than 2 DesktopFrame objects.
static const int kMaxFramesInEncodingQueue = 2;
// Maximum number of unacknowledged frames. Ignored if the client doesn't
// support ACKs. This value was chosen experimentally, using synthetic
// performance tests (see ProtocolPerfTest), to maximize frame rate, while
// keeping round-trip latency low.
static const int kMaxUnacknowledgedFrames = 4;
} // namespace
namespace remoting {
namespace protocol {
// We assume that the number of available cores is constant.
CaptureScheduler::CaptureScheduler(const base::Closure& capture_closure)
: capture_closure_(capture_closure),
tick_clock_(new base::DefaultTickClock()),
capture_timer_(new base::Timer(false, false)),
minimum_interval_(
base::TimeDelta::FromMilliseconds(kDefaultMinimumIntervalMs)),
num_of_processors_(base::SysInfo::NumberOfProcessors()),
capture_time_(kStatisticsWindow),
encode_time_(kStatisticsWindow),
num_encoding_frames_(0),
num_unacknowledged_frames_(0),
capture_pending_(false),
is_paused_(false),
next_frame_id_(0) {
DCHECK(num_of_processors_);
}
CaptureScheduler::~CaptureScheduler() {
}
void CaptureScheduler::Start() {
DCHECK(thread_checker_.CalledOnValidThread());
ScheduleNextCapture();
}
void CaptureScheduler::Pause(bool pause) {
DCHECK(thread_checker_.CalledOnValidThread());
if (is_paused_ != pause) {
is_paused_ = pause;
if (is_paused_) {
capture_timer_->Stop();
} else {
ScheduleNextCapture();
}
}
}
void CaptureScheduler::OnCaptureCompleted() {
DCHECK(thread_checker_.CalledOnValidThread());
capture_pending_ = false;
capture_time_.Record(
(tick_clock_->NowTicks() - last_capture_started_time_).InMilliseconds());
++num_encoding_frames_;
ScheduleNextCapture();
}
void CaptureScheduler::OnFrameEncoded(VideoPacket* packet) {
DCHECK(thread_checker_.CalledOnValidThread());
// Set packet_id for the outgoing packet.
packet->set_frame_id(next_frame_id_);
++next_frame_id_;
// Update internal stats.
encode_time_.Record(packet->encode_time_ms());
--num_encoding_frames_;
++num_unacknowledged_frames_;
ScheduleNextCapture();
}
void CaptureScheduler::OnFrameSent() {
DCHECK(thread_checker_.CalledOnValidThread());
ScheduleNextCapture();
}
void CaptureScheduler::ProcessVideoAck(scoped_ptr<VideoAck> video_ack) {
DCHECK(thread_checker_.CalledOnValidThread());
--num_unacknowledged_frames_;
DCHECK_GE(num_unacknowledged_frames_, 0);
ScheduleNextCapture();
}
void CaptureScheduler::SetTickClockForTest(
scoped_ptr<base::TickClock> tick_clock) {
tick_clock_ = std::move(tick_clock);
}
void CaptureScheduler::SetTimerForTest(scoped_ptr<base::Timer> timer) {
capture_timer_ = std::move(timer);
}
void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) {
num_of_processors_ = num_of_processors;
}
void CaptureScheduler::ScheduleNextCapture() {
DCHECK(thread_checker_.CalledOnValidThread());
if (is_paused_ || capture_pending_ ||
num_encoding_frames_ >= kMaxFramesInEncodingQueue) {
return;
}
if (num_encoding_frames_ + num_unacknowledged_frames_ >=
kMaxUnacknowledgedFrames) {
return;
}
// Delay by an amount chosen such that if capture and encode times
// continue to follow the averages, then we'll consume the target
// fraction of CPU across all cores.
base::TimeDelta delay =
std::max(minimum_interval_,
base::TimeDelta::FromMilliseconds(
(capture_time_.Average() + encode_time_.Average()) /
(kRecordingCpuConsumption * num_of_processors_)));
// Account for the time that has passed since the last capture.
delay = std::max(base::TimeDelta(), delay - (tick_clock_->NowTicks() -
last_capture_started_time_));
capture_timer_->Start(
FROM_HERE, delay,
base::Bind(&CaptureScheduler::CaptureNextFrame, base::Unretained(this)));
}
void CaptureScheduler::CaptureNextFrame() {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!is_paused_);
DCHECK(!capture_pending_);
capture_pending_ = true;
last_capture_started_time_ = tick_clock_->NowTicks();
capture_closure_.Run();
}
} // namespace protocol
} // namespace remoting