OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "remoting/host/capture_scheduler.h" | |
6 | |
7 #include <algorithm> | |
8 | |
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" | |
14 | |
15 namespace { | |
16 | |
17 // Number of samples to average the most recent capture and encode time | |
18 // over. | |
19 const int kStatisticsWindow = 3; | |
20 | |
21 // The hard limit is 30fps or 33ms per recording cycle. | |
22 const int64 kDefaultMinimumIntervalMs = 33; | |
23 | |
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; | |
28 | |
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; | |
32 | |
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; | |
38 | |
39 } // namespace | |
40 | |
41 namespace remoting { | |
42 | |
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_); | |
59 } | |
60 | |
61 CaptureScheduler::~CaptureScheduler() { | |
62 } | |
63 | |
64 void CaptureScheduler::Start() { | |
65 DCHECK(thread_checker_.CalledOnValidThread()); | |
66 | |
67 ScheduleNextCapture(); | |
68 } | |
69 | |
70 void CaptureScheduler::Pause(bool pause) { | |
71 DCHECK(thread_checker_.CalledOnValidThread()); | |
72 | |
73 if (is_paused_ != pause) { | |
74 is_paused_ = pause; | |
75 | |
76 if (is_paused_) { | |
77 capture_timer_->Stop(); | |
78 } else { | |
79 ScheduleNextCapture(); | |
80 } | |
81 } | |
82 } | |
83 | |
84 void CaptureScheduler::OnCaptureCompleted() { | |
85 DCHECK(thread_checker_.CalledOnValidThread()); | |
86 | |
87 capture_pending_ = false; | |
88 capture_time_.Record( | |
89 (tick_clock_->NowTicks() - last_capture_started_time_).InMilliseconds()); | |
90 | |
91 ++num_encoding_frames_; | |
92 | |
93 ScheduleNextCapture(); | |
94 } | |
95 | |
96 void CaptureScheduler::OnFrameEncoded(VideoPacket* packet) { | |
97 DCHECK(thread_checker_.CalledOnValidThread()); | |
98 | |
99 // Set packet_id for the outgoing packet. | |
100 packet->set_frame_id(next_frame_id_); | |
101 ++next_frame_id_; | |
102 | |
103 // Update internal stats. | |
104 encode_time_.Record(packet->encode_time_ms()); | |
105 | |
106 --num_encoding_frames_; | |
107 ++num_unacknowledged_frames_; | |
108 | |
109 ScheduleNextCapture(); | |
110 } | |
111 | |
112 void CaptureScheduler::OnFrameSent() { | |
113 DCHECK(thread_checker_.CalledOnValidThread()); | |
114 | |
115 ScheduleNextCapture(); | |
116 } | |
117 | |
118 void CaptureScheduler::ProcessVideoAck(scoped_ptr<VideoAck> video_ack) { | |
119 DCHECK(thread_checker_.CalledOnValidThread()); | |
120 | |
121 --num_unacknowledged_frames_; | |
122 DCHECK_GE(num_unacknowledged_frames_, 0); | |
123 | |
124 ScheduleNextCapture(); | |
125 } | |
126 | |
127 void CaptureScheduler::SetTickClockForTest( | |
128 scoped_ptr<base::TickClock> tick_clock) { | |
129 tick_clock_ = tick_clock.Pass(); | |
130 } | |
131 | |
132 void CaptureScheduler::SetTimerForTest(scoped_ptr<base::Timer> timer) { | |
133 capture_timer_ = timer.Pass(); | |
134 } | |
135 | |
136 void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) { | |
137 num_of_processors_ = num_of_processors; | |
138 } | |
139 | |
140 void CaptureScheduler::ScheduleNextCapture() { | |
141 DCHECK(thread_checker_.CalledOnValidThread()); | |
142 | |
143 if (is_paused_ || capture_pending_ || | |
144 num_encoding_frames_ >= kMaxFramesInEncodingQueue) { | |
145 return; | |
146 } | |
147 | |
148 if (num_encoding_frames_ + num_unacknowledged_frames_ >= | |
149 kMaxUnacknowledgedFrames) { | |
150 return; | |
151 } | |
152 | |
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_))); | |
161 | |
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_)); | |
165 | |
166 capture_timer_->Start( | |
167 FROM_HERE, delay, | |
168 base::Bind(&CaptureScheduler::CaptureNextFrame, base::Unretained(this))); | |
169 } | |
170 | |
171 void CaptureScheduler::CaptureNextFrame() { | |
172 DCHECK(thread_checker_.CalledOnValidThread()); | |
173 DCHECK(!is_paused_); | |
174 DCHECK(!capture_pending_); | |
175 | |
176 capture_pending_ = true; | |
177 last_capture_started_time_ = tick_clock_->NowTicks(); | |
178 capture_closure_.Run(); | |
179 } | |
180 | |
181 } // namespace remoting | |
OLD | NEW |