blob: 7c04cf7752b584c61701f3deab59f74dc7b1b3e2 [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"
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +000015#include "webrtc/base/criticalsection.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000016#include "webrtc/base/scoped_ptr.h"
minyue@webrtc.org74aaf292014-07-16 21:28:26 +000017#include "webrtc/base/exp_filter.h"
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +000018#include "webrtc/base/thread_annotations.h"
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +000019#include "webrtc/base/thread_checker.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000020#include "webrtc/modules/interface/module.h"
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000021#include "webrtc/video_engine/include/vie_base.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000022
23namespace webrtc {
24
25class Clock;
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000026class CpuOveruseObserver;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000027
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000028// TODO(pbos): Move this somewhere appropriate.
pbos@webrtc.orga9575702013-08-30 17:16:32 +000029class Statistics {
30 public:
31 Statistics();
32
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000033 void AddSample(float sample_ms);
pbos@webrtc.orga9575702013-08-30 17:16:32 +000034 void Reset();
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000035 void SetOptions(const CpuOveruseOptions& options);
pbos@webrtc.orga9575702013-08-30 17:16:32 +000036
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000037 float Mean() const;
38 float StdDev() const;
39 uint64_t Count() const;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000040
41 private:
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000042 float InitialMean() const;
43 float InitialVariance() const;
44
45 float sum_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000046 uint64_t count_;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000047 CpuOveruseOptions options_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000048 rtc::scoped_ptr<rtc::ExpFilter> filtered_samples_;
49 rtc::scoped_ptr<rtc::ExpFilter> filtered_variance_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000050};
51
52// Use to detect system overuse based on jitter in incoming frames.
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000053class OveruseFrameDetector : public Module {
54 public:
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +000055 OveruseFrameDetector(Clock* clock,
56 CpuOveruseMetricsObserver* metrics_observer);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000057 ~OveruseFrameDetector();
58
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000059 // Registers an observer receiving overuse and underuse callbacks. Set
60 // 'observer' to NULL to disable callbacks.
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000061 void SetObserver(CpuOveruseObserver* observer);
62
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000063 // Sets options for overuse detection.
64 void SetOptions(const CpuOveruseOptions& options);
65
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000066 // Called for each captured frame.
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000067 void FrameCaptured(int width, int height, int64_t capture_time_ms);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000068
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000069 // Called when the processing of a captured frame is started.
70 void FrameProcessingStarted();
71
72 // Called for each encoded frame.
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +000073 void FrameEncoded(int encode_time_ms);
74
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000075 // Called for each sent frame.
76 void FrameSent(int64_t capture_time_ms);
77
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000078 // Only public for testing.
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000079 int CaptureQueueDelayMsPerS() const;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000080 int LastProcessingTimeMs() const;
81 int FramesInQueue() const;
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +000082
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000083 // Implements Module.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000084 int64_t TimeUntilNextProcess() override;
85 int32_t Process() override;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000086
87 private:
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000088 class EncodeTimeAvg;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000089 class SendProcessingUsage;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000090 class CaptureQueueDelay;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000091 class FrameQueue;
92
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +000093 void UpdateCpuOveruseMetrics() EXCLUSIVE_LOCKS_REQUIRED(crit_);
94
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +000095 // TODO(asapersson): This method is only used on one thread, so it shouldn't
96 // need a guard.
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +000097 void AddProcessingTime(int elapsed_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000098
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +000099 // TODO(asapersson): This method is always called on the processing thread.
100 // If locking is required, consider doing that locking inside the
101 // implementation and reduce scope as much as possible. We should also
102 // see if we can avoid calling out to other methods while holding the lock.
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000103 bool IsOverusing() EXCLUSIVE_LOCKS_REQUIRED(crit_);
104 bool IsUnderusing(int64_t time_now) EXCLUSIVE_LOCKS_REQUIRED(crit_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000105
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000106 bool FrameTimeoutDetected(int64_t now) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
107 bool FrameSizeChanged(int num_pixels) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000108
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000109 void ResetAll(int num_pixels) EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000110
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000111 // Protecting all members except const and those that are only accessed on the
112 // processing thread.
113 // TODO(asapersson): See if we can reduce locking. As is, video frame
114 // processing contends with reading stats and the processing thread.
115 mutable rtc::CriticalSection crit_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000116
117 // Observer getting overuse reports.
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000118 CpuOveruseObserver* observer_ GUARDED_BY(crit_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000119
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000120 CpuOveruseOptions options_ GUARDED_BY(crit_);
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000121
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000122 // Stats metrics.
123 CpuOveruseMetricsObserver* const metrics_observer_;
124 CpuOveruseMetrics metrics_ GUARDED_BY(crit_);
125
tommi@webrtc.orga907e012015-01-28 17:33:12 +0000126 Clock* const clock_;
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000127 int64_t next_process_time_; // Only accessed on the processing thread.
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000128 int64_t num_process_times_ GUARDED_BY(crit_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000129
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000130 Statistics capture_deltas_ GUARDED_BY(crit_);
131 int64_t last_capture_time_ GUARDED_BY(crit_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000132
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000133 // These six members are only accessed on the processing thread.
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000134 int64_t last_overuse_time_;
135 int checks_above_threshold_;
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000136 int num_overuse_detections_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000137
138 int64_t last_rampup_time_;
139 bool in_quick_rampup_;
140 int current_rampup_delay_ms_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000141
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000142 // Number of pixels of last captured frame.
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000143 int num_pixels_ GUARDED_BY(crit_);
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000144
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000145 int64_t last_encode_sample_ms_; // Only accessed by one thread.
146
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000147 // TODO(asapersson): Can these be regular members (avoid separate heap
148 // allocs)?
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000149 const rtc::scoped_ptr<EncodeTimeAvg> encode_time_ GUARDED_BY(crit_);
150 const rtc::scoped_ptr<SendProcessingUsage> usage_ GUARDED_BY(crit_);
151 const rtc::scoped_ptr<FrameQueue> frame_queue_ GUARDED_BY(crit_);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000152
153 int64_t last_sample_time_ms_; // Only accessed by one thread.
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000154
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000155 const rtc::scoped_ptr<CaptureQueueDelay> capture_queue_delay_
156 GUARDED_BY(crit_);
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000157
158 rtc::ThreadChecker processing_thread_;
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000159
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000160 DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
161};
162
163} // namespace webrtc
164
165#endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_