blob: ef6d6aba2e38034cbe2e3dd037e864859fa6027d [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
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000014#include "webrtc/base/constructormagic.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000015#include "webrtc/modules/interface/module.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000016#include "webrtc/system_wrappers/interface/scoped_ptr.h"
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000017#include "webrtc/video_engine/include/vie_base.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000018
19namespace webrtc {
20
21class Clock;
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000022class CpuOveruseObserver;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000023class CriticalSectionWrapper;
24class VCMExpFilter;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000025
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000026// TODO(pbos): Move this somewhere appropriate.
pbos@webrtc.orga9575702013-08-30 17:16:32 +000027class Statistics {
28 public:
29 Statistics();
30
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000031 void AddSample(float sample_ms);
pbos@webrtc.orga9575702013-08-30 17:16:32 +000032 void Reset();
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000033 void SetOptions(const CpuOveruseOptions& options);
pbos@webrtc.orga9575702013-08-30 17:16:32 +000034
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000035 float Mean() const;
36 float StdDev() const;
37 uint64_t Count() const;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000038
39 private:
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000040 float InitialMean() const;
41 float InitialVariance() const;
42
43 float sum_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000044 uint64_t count_;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000045 CpuOveruseOptions options_;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000046 scoped_ptr<VCMExpFilter> filtered_samples_;
47 scoped_ptr<VCMExpFilter> filtered_variance_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000048};
49
50// Use to detect system overuse based on jitter in incoming frames.
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000051class OveruseFrameDetector : public Module {
52 public:
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000053 explicit OveruseFrameDetector(Clock* clock);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000054 ~OveruseFrameDetector();
55
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000056 // Registers an observer receiving overuse and underuse callbacks. Set
57 // 'observer' to NULL to disable callbacks.
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000058 void SetObserver(CpuOveruseObserver* observer);
59
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000060 // Sets options for overuse detection.
61 void SetOptions(const CpuOveruseOptions& options);
62
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000063 // Called for each captured frame.
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000064 void FrameCaptured(int width, int height);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000065
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000066 // Called when the processing of a captured frame is started.
67 void FrameProcessingStarted();
68
69 // Called for each encoded frame.
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +000070 void FrameEncoded(int encode_time_ms);
71
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000072 // Accessors.
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +000073
asapersson@webrtc.orgab6bf4f2014-05-27 07:43:15 +000074 // Returns CpuOveruseMetrics where
75 // capture_jitter_ms: The estimated jitter based on incoming captured frames.
76 // avg_encode_time_ms: Running average of reported encode time
77 // (FrameEncoded()). Only used for stats.
78 // encode_usage_percent: The average encode time divided by the average time
79 // difference between incoming captured frames.
80 // capture_queue_delay_ms_per_s: The current time delay between an incoming
81 // captured frame (FrameCaptured()) until the
82 // frame is being processed
83 // (FrameProcessingStarted()). (Note: if a new
84 // frame is received before an old frame has
85 // been processed, the old frame is skipped).
86 // The delay is expressed in ms delay per sec.
87 // Only used for stats.
88 void GetCpuOveruseMetrics(CpuOveruseMetrics* metrics) const;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000089
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000090 int CaptureQueueDelayMsPerS() const;
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +000091
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000092 // Implements Module.
pbos@webrtc.orga9575702013-08-30 17:16:32 +000093 virtual int32_t TimeUntilNextProcess() OVERRIDE;
94 virtual int32_t Process() OVERRIDE;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000095
96 private:
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000097 class EncodeTimeAvg;
asapersson@webrtc.org734a5322014-06-10 06:35:22 +000098 class EncodeTimeRsd;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000099 class EncodeUsage;
100 class CaptureQueueDelay;
101
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000102 bool IsOverusing();
103 bool IsUnderusing(int64_t time_now);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000104
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000105 bool FrameTimeoutDetected(int64_t now) const;
106 bool FrameSizeChanged(int num_pixels) const;
107
108 void ResetAll(int num_pixels);
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000109
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000110 // Protecting all members.
111 scoped_ptr<CriticalSectionWrapper> crit_;
112
113 // Observer getting overuse reports.
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000114 CpuOveruseObserver* observer_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000115
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000116 CpuOveruseOptions options_;
117
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000118 Clock* clock_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000119 int64_t next_process_time_;
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000120 int64_t num_process_times_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000121
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000122 Statistics capture_deltas_;
123 int64_t last_capture_time_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000124
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000125 int64_t last_overuse_time_;
126 int checks_above_threshold_;
127
128 int64_t last_rampup_time_;
129 bool in_quick_rampup_;
130 int current_rampup_delay_ms_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000131
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000132 // Number of pixels of last captured frame.
133 int num_pixels_;
134
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000135 int64_t last_encode_sample_ms_;
136 scoped_ptr<EncodeTimeAvg> encode_time_;
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000137 scoped_ptr<EncodeTimeRsd> encode_rsd_;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000138 scoped_ptr<EncodeUsage> encode_usage_;
139
140 scoped_ptr<CaptureQueueDelay> capture_queue_delay_;
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000141
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000142 DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
143};
144
145} // namespace webrtc
146
147#endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_