Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: remoting/host/capture_scheduler.cc

Issue 872433005: Move capture scheduling logic from VideoScheduler to CaptureScheduler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/host/capture_scheduler.h ('k') | remoting/host/capture_scheduler_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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() {
45 // Delay by an amount chosen such that if capture and encode times 55 DCHECK(CalledOnValidThread());
46 // continue to follow the averages, then we'll consume the target
47 // fraction of CPU across all cores.
48 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(
49 (capture_time_.Average() + encode_time_.Average()) /
50 (kRecordingCpuConsumption * num_of_processors_));
51 56
52 if (delay < minimum_interval_) 57 ScheduleNextCapture();
53 return minimum_interval_;
54 return delay;
55 } 58 }
56 59
57 void CaptureScheduler::RecordCaptureTime(base::TimeDelta capture_time) { 60 void CaptureScheduler::Pause(bool pause) {
58 capture_time_.Record(capture_time.InMilliseconds()); 61 DCHECK(CalledOnValidThread());
62
63 if (is_paused_ != pause) {
64 is_paused_ = pause;
65
66 if (is_paused_) {
67 capture_timer_->Stop();
68 } else {
69 ScheduleNextCapture();
70 }
71 }
59 } 72 }
60 73
61 void CaptureScheduler::RecordEncodeTime(base::TimeDelta encode_time) { 74 void CaptureScheduler::OnCaptureCompleted() {
62 encode_time_.Record(encode_time.InMilliseconds()); 75 DCHECK(CalledOnValidThread());
76
77 capture_pending_ = false;
78 capture_time_.Record(
79 (tick_clock_->NowTicks() - last_capture_started_time_).InMilliseconds());
80
81 ScheduleNextCapture();
63 } 82 }
64 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::SetTickClockForTest(
102 scoped_ptr<base::TickClock> tick_clock) {
103 tick_clock_ = tick_clock.Pass();
104 }
105 void CaptureScheduler::SetTimerForTest(scoped_ptr<base::Timer> timer) {
106 capture_timer_ = timer.Pass();
107 }
65 void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) { 108 void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) {
66 num_of_processors_ = num_of_processors; 109 num_of_processors_ = num_of_processors;
67 } 110 }
68 111
112 void CaptureScheduler::ScheduleNextCapture() {
113 DCHECK(CalledOnValidThread());
114
115 if (is_paused_ || pending_frames_ >= kMaxPendingFrames || capture_pending_)
116 return;
117
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_)));
126
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_));
130
131 capture_timer_->Start(
132 FROM_HERE, delay,
133 base::Bind(&CaptureScheduler::CaptureNextFrame, base::Unretained(this)));
134 }
135
136 void CaptureScheduler::CaptureNextFrame() {
137 DCHECK(CalledOnValidThread());
138 DCHECK(!is_paused_);
139 DCHECK(!capture_pending_);
140
141 pending_frames_++;
142 DCHECK_LE(pending_frames_, kMaxPendingFrames);
143
144 capture_pending_ = true;
145 last_capture_started_time_ = tick_clock_->NowTicks();
146 capture_closure_.Run();
147 }
148
69 } // namespace remoting 149 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/capture_scheduler.h ('k') | remoting/host/capture_scheduler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698