OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/capture_scheduler.h" | 5 #include "remoting/host/capture_scheduler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/sys_info.h" | 10 #include "base/sys_info.h" |
11 #include "base/time/default_tick_clock.h" | |
11 #include "base/time/time.h" | 12 #include "base/time/time.h" |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 // Number of samples to average the most recent capture and encode time | 16 // Number of samples to average the most recent capture and encode time |
16 // over. | 17 // over. |
17 const int kStatisticsWindow = 3; | 18 const int kStatisticsWindow = 3; |
18 | 19 |
19 // The hard limit is 30fps or 33ms per recording cycle. | 20 // The hard limit is 30fps or 33ms per recording cycle. |
20 const int64 kDefaultMinimumIntervalMs = 33; | 21 const int64 kDefaultMinimumIntervalMs = 33; |
21 | 22 |
22 // Controls how much CPU time we can use for encode and capture. | 23 // Controls how much CPU time we can use for encode and capture. |
23 // Range of this value is between 0 to 1. 0 means using 0% of of all CPUs | 24 // Range of this value is between 0 to 1. 0 means using 0% of of all CPUs |
24 // available while 1 means using 100% of all CPUs available. | 25 // available while 1 means using 100% of all CPUs available. |
25 const double kRecordingCpuConsumption = 0.5; | 26 const double kRecordingCpuConsumption = 0.5; |
26 | 27 |
28 // Maximum number of frames that can be processed simultaneously. | |
29 static const int kMaxPendingFrames = 2; | |
30 | |
27 } // namespace | 31 } // namespace |
28 | 32 |
29 namespace remoting { | 33 namespace remoting { |
30 | 34 |
31 // We assume that the number of available cores is constant. | 35 // We assume that the number of available cores is constant. |
32 CaptureScheduler::CaptureScheduler() | 36 CaptureScheduler::CaptureScheduler(const base::Closure& capture_closure) |
33 : minimum_interval_( | 37 : capture_closure_(capture_closure), |
38 tick_clock_(new base::DefaultTickClock()), | |
39 capture_timer_(new base::Timer(false, false)), | |
40 minimum_interval_( | |
34 base::TimeDelta::FromMilliseconds(kDefaultMinimumIntervalMs)), | 41 base::TimeDelta::FromMilliseconds(kDefaultMinimumIntervalMs)), |
35 num_of_processors_(base::SysInfo::NumberOfProcessors()), | 42 num_of_processors_(base::SysInfo::NumberOfProcessors()), |
36 capture_time_(kStatisticsWindow), | 43 capture_time_(kStatisticsWindow), |
37 encode_time_(kStatisticsWindow) { | 44 encode_time_(kStatisticsWindow), |
45 pending_frames_(0), | |
46 capture_pending_(false), | |
47 is_paused_(false) { | |
38 DCHECK(num_of_processors_); | 48 DCHECK(num_of_processors_); |
39 } | 49 } |
40 | 50 |
41 CaptureScheduler::~CaptureScheduler() { | 51 CaptureScheduler::~CaptureScheduler() { |
42 } | 52 } |
43 | 53 |
44 base::TimeDelta CaptureScheduler::NextCaptureDelay() { | 54 void CaptureScheduler::Start() { |
55 DCHECK(CalledOnValidThread()); | |
56 | |
57 ScheduleNextCapture(); | |
58 } | |
59 | |
60 void CaptureScheduler::Pause(bool pause) { | |
61 DCHECK(CalledOnValidThread()); | |
62 | |
63 if (is_paused_ != pause) { | |
64 is_paused_ = pause; | |
65 | |
66 if (is_paused_) { | |
67 capture_timer_->Stop(); | |
Wez
2015/02/02 21:40:48
Is it the case that |capture_timer_| is always run
Sergey Ulanov
2015/02/03 23:49:52
No. capture_timer_ may not be running when not pau
Wez
2015/02/05 02:40:25
Acknowledged.
| |
68 } else { | |
69 ScheduleNextCapture(); | |
70 } | |
71 } | |
72 } | |
73 | |
74 void CaptureScheduler::OnCaptureCompleted() { | |
75 DCHECK(CalledOnValidThread()); | |
76 | |
77 capture_pending_ = false; | |
78 capture_time_.Record( | |
79 (tick_clock_->NowTicks() - last_capture_started_time_).InMilliseconds()); | |
80 | |
81 ScheduleNextCapture(); | |
82 } | |
83 | |
84 void CaptureScheduler::OnFrameSent() { | |
85 DCHECK(CalledOnValidThread()); | |
86 | |
87 // Decrement the pending capture count. | |
88 pending_frames_--; | |
89 DCHECK_GE(pending_frames_, 0); | |
90 | |
91 ScheduleNextCapture(); | |
92 } | |
93 | |
94 void CaptureScheduler::OnFrameEncoded(base::TimeDelta encode_time) { | |
95 DCHECK(CalledOnValidThread()); | |
96 | |
97 encode_time_.Record(encode_time.InMilliseconds()); | |
98 ScheduleNextCapture(); | |
99 } | |
100 | |
101 void CaptureScheduler::ScheduleNextCapture() { | |
102 DCHECK(CalledOnValidThread()); | |
103 | |
104 if (is_paused_ || pending_frames_ >= kMaxPendingFrames || capture_pending_) | |
105 return; | |
106 | |
45 // Delay by an amount chosen such that if capture and encode times | 107 // Delay by an amount chosen such that if capture and encode times |
46 // continue to follow the averages, then we'll consume the target | 108 // continue to follow the averages, then we'll consume the target |
47 // fraction of CPU across all cores. | 109 // fraction of CPU across all cores. |
48 base::TimeDelta delay = base::TimeDelta::FromMilliseconds( | 110 base::TimeDelta delay = |
49 (capture_time_.Average() + encode_time_.Average()) / | 111 std::max(minimum_interval_, |
50 (kRecordingCpuConsumption * num_of_processors_)); | 112 base::TimeDelta::FromMilliseconds( |
113 (capture_time_.Average() + encode_time_.Average()) / | |
114 (kRecordingCpuConsumption * num_of_processors_))); | |
51 | 115 |
52 if (delay < minimum_interval_) | 116 // Account for the time that has passed since the last capture. |
53 return minimum_interval_; | 117 delay = std::max(base::TimeDelta(), delay - (tick_clock_->NowTicks() - |
54 return delay; | 118 last_capture_started_time_)); |
119 | |
120 capture_timer_->Start( | |
121 FROM_HERE, delay, | |
122 base::Bind(&CaptureScheduler::CaptureNextFrame, base::Unretained(this))); | |
55 } | 123 } |
56 | 124 |
57 void CaptureScheduler::RecordCaptureTime(base::TimeDelta capture_time) { | 125 void CaptureScheduler::CaptureNextFrame() { |
58 capture_time_.Record(capture_time.InMilliseconds()); | 126 DCHECK(CalledOnValidThread()); |
59 } | 127 DCHECK(!is_paused_); |
128 DCHECK(!capture_pending_); | |
60 | 129 |
61 void CaptureScheduler::RecordEncodeTime(base::TimeDelta encode_time) { | 130 pending_frames_++; |
62 encode_time_.Record(encode_time.InMilliseconds()); | 131 DCHECK_LE(pending_frames_, kMaxPendingFrames); |
63 } | |
64 | 132 |
65 void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) { | 133 capture_pending_ = true; |
66 num_of_processors_ = num_of_processors; | 134 last_capture_started_time_ = tick_clock_->NowTicks(); |
135 capture_closure_.Run(); | |
Wez
2015/02/02 21:40:48
IIUC OnCaptureCompleted() will be called before Ru
Sergey Ulanov
2015/02/03 23:49:52
That's not true anymore, now that CaptureScheduler
Wez
2015/02/05 02:40:25
Acknowledged.
| |
67 } | 136 } |
68 | 137 |
69 } // namespace remoting | 138 } // namespace remoting |
OLD | NEW |