blob: 4717a60617125dbf6626f83531f7e9a4960be97f [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;
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000021class CpuOveruseObserver;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000022class CriticalSectionWrapper;
23class VCMExpFilter;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000024
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000025// Limits on standard deviation for under/overuse.
26#ifdef WEBRTC_LINUX
27const float kOveruseStdDevMs = 15.0f;
28const float kNormalUseStdDevMs = 7.0f;
29#elif WEBRTC_MAC
30const float kOveruseStdDevMs = 22.0f;
31const float kNormalUseStdDevMs = 12.0f;
32#else
33const float kOveruseStdDevMs = 17.0f;
34const float kNormalUseStdDevMs = 10.0f;
35#endif
36
37// TODO(pbos): Move this somewhere appropriate.
pbos@webrtc.orga9575702013-08-30 17:16:32 +000038class Statistics {
39 public:
40 Statistics();
41
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000042 void AddSample(float sample_ms);
pbos@webrtc.orga9575702013-08-30 17:16:32 +000043 void Reset();
44
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000045 float Mean() const;
46 float StdDev() const;
47 uint64_t Count() const;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000048
49 private:
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000050 float InitialMean() const;
51 float InitialVariance() const;
52
53 float sum_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000054 uint64_t count_;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000055 scoped_ptr<VCMExpFilter> filtered_samples_;
56 scoped_ptr<VCMExpFilter> filtered_variance_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000057};
58
59// Use to detect system overuse based on jitter in incoming frames.
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000060class OveruseFrameDetector : public Module {
61 public:
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000062 explicit OveruseFrameDetector(Clock* clock,
63 float normaluse_stddev_ms,
64 float overuse_stddev_ms);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000065 ~OveruseFrameDetector();
66
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000067 // Registers an observer receiving overuse and underuse callbacks. Set
68 // 'observer' to NULL to disable callbacks.
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000069 void SetObserver(CpuOveruseObserver* observer);
70
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000071 // Called for each captured frame.
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000072 void FrameCaptured(int width, int height);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000073
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000074 // Implements Module.
pbos@webrtc.orga9575702013-08-30 17:16:32 +000075 virtual int32_t TimeUntilNextProcess() OVERRIDE;
76 virtual int32_t Process() OVERRIDE;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000077
78 private:
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000079 bool IsOverusing();
80 bool IsUnderusing(int64_t time_now);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000081
82 // Protecting all members.
83 scoped_ptr<CriticalSectionWrapper> crit_;
84
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000085 // Limits on standard deviation for under/overuse.
86 const float normaluse_stddev_ms_;
87 const float overuse_stddev_ms_;
88
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000089 // Observer getting overuse reports.
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000090 CpuOveruseObserver* observer_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000091
92 Clock* clock_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000093 int64_t next_process_time_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000094
pbos@webrtc.orga9575702013-08-30 17:16:32 +000095 Statistics capture_deltas_;
96 int64_t last_capture_time_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000097
pbos@webrtc.orga9575702013-08-30 17:16:32 +000098 int64_t last_overuse_time_;
99 int checks_above_threshold_;
100
101 int64_t last_rampup_time_;
102 bool in_quick_rampup_;
103 int current_rampup_delay_ms_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000104
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000105 // Number of pixels of last captured frame.
106 int num_pixels_;
107
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000108 DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
109};
110
111} // namespace webrtc
112
113#endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_