blob: 2c1cd1395e93cbcd794263e9c793e2d5aca9dd1d [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"
minyue@webrtc.org74aaf292014-07-16 21:28:26 +000015#include "webrtc/base/exp_filter.h"
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +000016#include "webrtc/base/thread_annotations.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000017#include "webrtc/modules/interface/module.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000018#include "webrtc/system_wrappers/interface/scoped_ptr.h"
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000019#include "webrtc/video_engine/include/vie_base.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000020
21namespace webrtc {
22
23class Clock;
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000024class CpuOveruseObserver;
tommi@webrtc.org7a37bfc2015-01-29 16:08:20 +000025class CriticalSectionWrapper;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000026
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000027// TODO(pbos): Move this somewhere appropriate.
pbos@webrtc.orga9575702013-08-30 17:16:32 +000028class Statistics {
29 public:
30 Statistics();
31
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000032 void AddSample(float sample_ms);
pbos@webrtc.orga9575702013-08-30 17:16:32 +000033 void Reset();
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000034 void SetOptions(const CpuOveruseOptions& options);
pbos@webrtc.orga9575702013-08-30 17:16:32 +000035
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000036 float Mean() const;
37 float StdDev() const;
38 uint64_t Count() const;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000039
40 private:
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000041 float InitialMean() const;
42 float InitialVariance() const;
43
44 float sum_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000045 uint64_t count_;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000046 CpuOveruseOptions options_;
minyue@webrtc.org74aaf292014-07-16 21:28:26 +000047 scoped_ptr<rtc::ExpFilter> filtered_samples_;
48 scoped_ptr<rtc::ExpFilter> filtered_variance_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000049};
50
51// Use to detect system overuse based on jitter in incoming frames.
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000052class OveruseFrameDetector : public Module {
53 public:
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000054 explicit OveruseFrameDetector(Clock* clock);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000055 ~OveruseFrameDetector();
56
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000057 // Registers an observer receiving overuse and underuse callbacks. Set
58 // 'observer' to NULL to disable callbacks.
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000059 void SetObserver(CpuOveruseObserver* observer);
60
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000061 // Sets options for overuse detection.
62 void SetOptions(const CpuOveruseOptions& options);
63
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000064 // Called for each captured frame.
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000065 void FrameCaptured(int width, int height, int64_t capture_time_ms);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000066
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000067 // Called when the processing of a captured frame is started.
68 void FrameProcessingStarted();
69
70 // Called for each encoded frame.
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +000071 void FrameEncoded(int encode_time_ms);
72
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000073 // Called for each sent frame.
74 void FrameSent(int64_t capture_time_ms);
75
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000076 // Accessors.
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +000077
asapersson@webrtc.orgab6bf4f2014-05-27 07:43:15 +000078 // Returns CpuOveruseMetrics where
79 // capture_jitter_ms: The estimated jitter based on incoming captured frames.
80 // avg_encode_time_ms: Running average of reported encode time
81 // (FrameEncoded()). Only used for stats.
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000082 // TODO(asapersson): Rename metric.
83 // encode_usage_percent: The average processing time of a frame on the
84 // send-side divided by the average time difference
85 // between incoming captured frames.
asapersson@webrtc.orgab6bf4f2014-05-27 07:43:15 +000086 // capture_queue_delay_ms_per_s: The current time delay between an incoming
87 // captured frame (FrameCaptured()) until the
88 // frame is being processed
89 // (FrameProcessingStarted()). (Note: if a new
90 // frame is received before an old frame has
91 // been processed, the old frame is skipped).
92 // The delay is expressed in ms delay per sec.
93 // Only used for stats.
94 void GetCpuOveruseMetrics(CpuOveruseMetrics* metrics) const;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000095
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000096 // Only public for testing.
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000097 int CaptureQueueDelayMsPerS() const;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000098 int LastProcessingTimeMs() const;
99 int FramesInQueue() const;
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000100
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000101 // Implements Module.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000102 virtual int64_t TimeUntilNextProcess() OVERRIDE;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000103 virtual int32_t Process() OVERRIDE;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000104
105 private:
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000106 class EncodeTimeAvg;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000107 class SendProcessingUsage;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000108 class CaptureQueueDelay;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000109 class FrameQueue;
110
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000111 void AddProcessingTime(int elapsed_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000112
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000113 bool IsOverusing() EXCLUSIVE_LOCKS_REQUIRED(crit_);
114 bool IsUnderusing(int64_t time_now) EXCLUSIVE_LOCKS_REQUIRED(crit_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000115
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000116 bool FrameTimeoutDetected(int64_t now) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
117 bool FrameSizeChanged(int num_pixels) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000118
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000119 void ResetAll(int num_pixels) EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000120
tommi@webrtc.org7a37bfc2015-01-29 16:08:20 +0000121 // Protecting all members.
122 scoped_ptr<CriticalSectionWrapper> crit_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000123
124 // Observer getting overuse reports.
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000125 CpuOveruseObserver* observer_ GUARDED_BY(crit_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000126
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000127 CpuOveruseOptions options_ GUARDED_BY(crit_);
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000128
tommi@webrtc.orga907e012015-01-28 17:33:12 +0000129 Clock* const clock_;
tommi@webrtc.org7a37bfc2015-01-29 16:08:20 +0000130 int64_t next_process_time_;
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000131 int64_t num_process_times_ GUARDED_BY(crit_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000132
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000133 Statistics capture_deltas_ GUARDED_BY(crit_);
134 int64_t last_capture_time_ GUARDED_BY(crit_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000135
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000136 int64_t last_overuse_time_;
137 int checks_above_threshold_;
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000138 int num_overuse_detections_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000139
140 int64_t last_rampup_time_;
141 bool in_quick_rampup_;
142 int current_rampup_delay_ms_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000143
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000144 // Number of pixels of last captured frame.
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000145 int num_pixels_ GUARDED_BY(crit_);
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000146
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000147 int64_t last_encode_sample_ms_ GUARDED_BY(crit_);
tommi@webrtc.org7a37bfc2015-01-29 16:08:20 +0000148 scoped_ptr<EncodeTimeAvg> encode_time_ GUARDED_BY(crit_);
149 scoped_ptr<SendProcessingUsage> usage_ GUARDED_BY(crit_);
150 scoped_ptr<FrameQueue> frame_queue_ GUARDED_BY(crit_);
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000151 int64_t last_sample_time_ms_ GUARDED_BY(crit_);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000152
tommi@webrtc.org7a37bfc2015-01-29 16:08:20 +0000153 scoped_ptr<CaptureQueueDelay> capture_queue_delay_ GUARDED_BY(crit_);
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000154
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000155 DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
156};
157
158} // namespace webrtc
159
160#endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_