blob: 00d3b19b8fa4b2ed099f88ad7c965b5ab037dfe9 [file] [log] [blame]
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_
12#define WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_
13
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000014#include "webrtc/modules/interface/module.h"
15#include "webrtc/system_wrappers/interface/constructor_magic.h"
16#include "webrtc/system_wrappers/interface/scoped_ptr.h"
17
18namespace webrtc {
19
20class Clock;
21class CriticalSectionWrapper;
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000022class CpuOveruseObserver;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000023
pbos@webrtc.orga9575702013-08-30 17:16:32 +000024// TODO(pbos): Move this somewhere appropriate
25class Statistics {
26 public:
27 Statistics();
28
29 void AddSample(double sample);
30 void Reset();
31
32 double Mean() const;
33 double Variance() const;
34 double StdDev() const;
35 uint64_t Samples() const;
36
37 private:
38 double sum_;
39 double sum_squared_;
40 uint64_t count_;
41};
42
43// Use to detect system overuse based on jitter in incoming frames.
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000044class OveruseFrameDetector : public Module {
45 public:
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000046 explicit OveruseFrameDetector(Clock* clock);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000047 ~OveruseFrameDetector();
48
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000049 // Registers an observer receiving overuse and underuse callbacks. Set
50 // 'observer' to NULL to disable callbacks.
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000051 void SetObserver(CpuOveruseObserver* observer);
52
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000053 // Called for each captured frame.
54 void FrameCaptured();
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000055
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000056 // Implements Module.
pbos@webrtc.orga9575702013-08-30 17:16:32 +000057 virtual int32_t TimeUntilNextProcess() OVERRIDE;
58 virtual int32_t Process() OVERRIDE;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000059
60 private:
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000061 bool IsOverusing();
62 bool IsUnderusing(int64_t time_now);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000063
64 // Protecting all members.
65 scoped_ptr<CriticalSectionWrapper> crit_;
66
67 // Observer getting overuse reports.
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000068 CpuOveruseObserver* observer_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000069
70 Clock* clock_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000071 int64_t next_process_time_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000072
pbos@webrtc.orga9575702013-08-30 17:16:32 +000073 Statistics capture_deltas_;
74 int64_t last_capture_time_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000075
pbos@webrtc.orga9575702013-08-30 17:16:32 +000076 int64_t last_overuse_time_;
77 int checks_above_threshold_;
78
79 int64_t last_rampup_time_;
80 bool in_quick_rampup_;
81 int current_rampup_delay_ms_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000082
83 DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
84};
85
86} // namespace webrtc
87
88#endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_