Home | History | Annotate | Download | only in host
      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 30fps or 33ms per recording cycle.
     20 const int64 kDefaultMinimumIntervalMs = 33;
     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     : minimum_interval_(
     34           base::TimeDelta::FromMilliseconds(kDefaultMinimumIntervalMs)),
     35       num_of_processors_(base::SysInfo::NumberOfProcessors()),
     36       capture_time_(kStatisticsWindow),
     37       encode_time_(kStatisticsWindow) {
     38   DCHECK(num_of_processors_);
     39 }
     40 
     41 CaptureScheduler::~CaptureScheduler() {
     42 }
     43 
     44 base::TimeDelta CaptureScheduler::NextCaptureDelay() {
     45   // Delay by an amount chosen such that if capture and encode times
     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 
     52   if (delay < minimum_interval_)
     53     return minimum_interval_;
     54   return delay;
     55 }
     56 
     57 void CaptureScheduler::RecordCaptureTime(base::TimeDelta capture_time) {
     58   capture_time_.Record(capture_time.InMilliseconds());
     59 }
     60 
     61 void CaptureScheduler::RecordEncodeTime(base::TimeDelta encode_time) {
     62   encode_time_.Record(encode_time.InMilliseconds());
     63 }
     64 
     65 void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) {
     66   num_of_processors_ = num_of_processors;
     67 }
     68 
     69 }  // namespace remoting
     70