- add sources.
[platform/framework/web/crosswalk.git] / src / remoting / host / capture_scheduler.cc
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/time.h"
12
13 namespace {
14
15 // Number of samples to average the most recent capture and encode time
16 // over.
17 const int kStatisticsWindow = 3;
18
19 // The hard limit is 20fps or 50ms per recording cycle.
20 const int64 kMinimumRecordingDelay = 50;
21
22 // 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 // available while 1 means using 100% of all CPUs available.
25 const double kRecordingCpuConsumption = 0.5;
26
27 }  // namespace
28
29 namespace remoting {
30
31 // We assume that the number of available cores is constant.
32 CaptureScheduler::CaptureScheduler()
33     : num_of_processors_(base::SysInfo::NumberOfProcessors()),
34       capture_time_(kStatisticsWindow),
35       encode_time_(kStatisticsWindow) {
36   DCHECK(num_of_processors_);
37 }
38
39 CaptureScheduler::~CaptureScheduler() {
40 }
41
42 base::TimeDelta CaptureScheduler::NextCaptureDelay() {
43   // Delay by an amount chosen such that if capture and encode times
44   // continue to follow the averages, then we'll consume the target
45   // fraction of CPU across all cores.
46   double delay =
47       (capture_time_.Average() + encode_time_.Average()) /
48       (kRecordingCpuConsumption * num_of_processors_);
49
50   if (delay < kMinimumRecordingDelay)
51     return base::TimeDelta::FromMilliseconds(kMinimumRecordingDelay);
52   return base::TimeDelta::FromMilliseconds(delay);
53 }
54
55 void CaptureScheduler::RecordCaptureTime(base::TimeDelta capture_time) {
56   capture_time_.Record(capture_time.InMilliseconds());
57 }
58
59 void CaptureScheduler::RecordEncodeTime(base::TimeDelta encode_time) {
60   encode_time_.Record(encode_time.InMilliseconds());
61 }
62
63 void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) {
64   num_of_processors_ = num_of_processors;
65 }
66
67 }  // namespace remoting