2013-06-26 11:23:01 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_
|
|
|
|
|
#define WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_
|
|
|
|
|
|
2014-05-21 21:18:46 +00:00
|
|
|
#include "webrtc/base/constructormagic.h"
|
2015-02-08 18:27:46 +00:00
|
|
|
#include "webrtc/base/criticalsection.h"
|
2015-02-26 14:34:55 +00:00
|
|
|
#include "webrtc/base/scoped_ptr.h"
|
2014-07-16 21:28:26 +00:00
|
|
|
#include "webrtc/base/exp_filter.h"
|
2014-11-11 09:40:19 +00:00
|
|
|
#include "webrtc/base/thread_annotations.h"
|
2015-02-08 18:27:46 +00:00
|
|
|
#include "webrtc/base/thread_checker.h"
|
2015-11-04 08:31:52 +01:00
|
|
|
#include "webrtc/modules/include/module.h"
|
2013-06-26 11:23:01 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
class Clock;
|
2015-05-12 16:51:11 +02:00
|
|
|
|
|
|
|
|
// CpuOveruseObserver is called when a system overuse is detected and
|
|
|
|
|
// VideoEngine cannot keep up the encoding frequency.
|
|
|
|
|
class CpuOveruseObserver {
|
|
|
|
|
public:
|
|
|
|
|
// Called as soon as an overuse is detected.
|
|
|
|
|
virtual void OveruseDetected() = 0;
|
|
|
|
|
// Called periodically when the system is not overused any longer.
|
|
|
|
|
virtual void NormalUsage() = 0;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
virtual ~CpuOveruseObserver() {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct CpuOveruseOptions {
|
|
|
|
|
CpuOveruseOptions()
|
2015-09-08 10:52:42 +02:00
|
|
|
: enable_encode_usage_method(true),
|
2015-05-12 16:51:11 +02:00
|
|
|
low_encode_usage_threshold_percent(55),
|
|
|
|
|
high_encode_usage_threshold_percent(85),
|
|
|
|
|
enable_extended_processing_usage(true),
|
|
|
|
|
frame_timeout_interval_ms(1500),
|
|
|
|
|
min_frame_samples(120),
|
|
|
|
|
min_process_count(3),
|
|
|
|
|
high_threshold_consecutive_count(2) {}
|
|
|
|
|
|
|
|
|
|
// Method based on encode time of frames.
|
|
|
|
|
bool enable_encode_usage_method;
|
|
|
|
|
int low_encode_usage_threshold_percent; // Threshold for triggering underuse.
|
|
|
|
|
int high_encode_usage_threshold_percent; // Threshold for triggering overuse.
|
|
|
|
|
bool enable_extended_processing_usage; // Include a larger time span (in
|
|
|
|
|
// addition to encode time) for
|
|
|
|
|
// measuring the processing time of a
|
|
|
|
|
// frame.
|
|
|
|
|
// General settings.
|
|
|
|
|
int frame_timeout_interval_ms; // The maximum allowed interval between two
|
|
|
|
|
// frames before resetting estimations.
|
|
|
|
|
int min_frame_samples; // The minimum number of frames required.
|
|
|
|
|
int min_process_count; // The number of initial process times required before
|
|
|
|
|
// triggering an overuse/underuse.
|
|
|
|
|
int high_threshold_consecutive_count; // The number of consecutive checks
|
|
|
|
|
// above the high threshold before
|
|
|
|
|
// triggering an overuse.
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct CpuOveruseMetrics {
|
|
|
|
|
CpuOveruseMetrics()
|
2015-09-08 10:52:42 +02:00
|
|
|
: avg_encode_time_ms(-1),
|
2015-07-16 08:08:03 +02:00
|
|
|
encode_usage_percent(-1) {}
|
2015-05-12 16:51:11 +02:00
|
|
|
|
|
|
|
|
int avg_encode_time_ms; // The average encode time in ms.
|
|
|
|
|
int encode_usage_percent; // The average encode time divided by the average
|
|
|
|
|
// time difference between incoming captured frames.
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class CpuOveruseMetricsObserver {
|
|
|
|
|
public:
|
|
|
|
|
virtual ~CpuOveruseMetricsObserver() {}
|
|
|
|
|
virtual void CpuOveruseMetricsUpdated(const CpuOveruseMetrics& metrics) = 0;
|
|
|
|
|
};
|
2013-09-23 20:05:39 +00:00
|
|
|
|
2013-08-30 17:16:32 +00:00
|
|
|
|
2015-09-08 10:52:42 +02:00
|
|
|
// Use to detect system overuse based on the send-side processing time of
|
|
|
|
|
// incoming frames.
|
2013-06-26 11:23:01 +00:00
|
|
|
class OveruseFrameDetector : public Module {
|
|
|
|
|
public:
|
2015-02-26 12:19:31 +00:00
|
|
|
OveruseFrameDetector(Clock* clock,
|
2015-06-26 06:58:16 +02:00
|
|
|
const CpuOveruseOptions& options,
|
|
|
|
|
CpuOveruseObserver* overuse_observer,
|
2015-02-26 12:19:31 +00:00
|
|
|
CpuOveruseMetricsObserver* metrics_observer);
|
2013-06-26 11:23:01 +00:00
|
|
|
~OveruseFrameDetector();
|
|
|
|
|
|
2013-07-31 16:42:21 +00:00
|
|
|
// Called for each captured frame.
|
2014-10-16 06:57:12 +00:00
|
|
|
void FrameCaptured(int width, int height, int64_t capture_time_ms);
|
2013-06-26 11:23:01 +00:00
|
|
|
|
2013-12-04 13:47:44 +00:00
|
|
|
// Called for each encoded frame.
|
2013-11-26 11:12:33 +00:00
|
|
|
void FrameEncoded(int encode_time_ms);
|
|
|
|
|
|
2014-10-16 06:57:12 +00:00
|
|
|
// Called for each sent frame.
|
|
|
|
|
void FrameSent(int64_t capture_time_ms);
|
|
|
|
|
|
|
|
|
|
// Only public for testing.
|
|
|
|
|
int LastProcessingTimeMs() const;
|
|
|
|
|
int FramesInQueue() const;
|
2013-11-20 13:51:40 +00:00
|
|
|
|
2013-06-26 11:23:01 +00:00
|
|
|
// Implements Module.
|
2015-03-04 12:58:35 +00:00
|
|
|
int64_t TimeUntilNextProcess() override;
|
|
|
|
|
int32_t Process() override;
|
2013-06-26 11:23:01 +00:00
|
|
|
|
|
|
|
|
private:
|
2013-12-04 13:47:44 +00:00
|
|
|
class EncodeTimeAvg;
|
2014-10-16 06:57:12 +00:00
|
|
|
class SendProcessingUsage;
|
|
|
|
|
class FrameQueue;
|
|
|
|
|
|
2015-02-26 12:19:31 +00:00
|
|
|
void UpdateCpuOveruseMetrics() EXCLUSIVE_LOCKS_REQUIRED(crit_);
|
|
|
|
|
|
2015-02-08 18:27:46 +00:00
|
|
|
// TODO(asapersson): This method is only used on one thread, so it shouldn't
|
|
|
|
|
// need a guard.
|
2014-11-11 09:40:19 +00:00
|
|
|
void AddProcessingTime(int elapsed_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_);
|
2013-12-04 13:47:44 +00:00
|
|
|
|
2015-09-24 00:53:32 -07:00
|
|
|
// Only called on the processing thread.
|
|
|
|
|
bool IsOverusing(const CpuOveruseMetrics& metrics);
|
|
|
|
|
bool IsUnderusing(const CpuOveruseMetrics& metrics, int64_t time_now);
|
2013-06-26 11:23:01 +00:00
|
|
|
|
2014-11-11 09:40:19 +00:00
|
|
|
bool FrameTimeoutDetected(int64_t now) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
|
|
|
|
|
bool FrameSizeChanged(int num_pixels) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
|
2014-03-20 13:15:01 +00:00
|
|
|
|
2014-11-11 09:40:19 +00:00
|
|
|
void ResetAll(int num_pixels) EXCLUSIVE_LOCKS_REQUIRED(crit_);
|
2014-02-17 19:02:15 +00:00
|
|
|
|
2015-02-08 18:27:46 +00:00
|
|
|
// Protecting all members except const and those that are only accessed on the
|
|
|
|
|
// processing thread.
|
|
|
|
|
// TODO(asapersson): See if we can reduce locking. As is, video frame
|
|
|
|
|
// processing contends with reading stats and the processing thread.
|
|
|
|
|
mutable rtc::CriticalSection crit_;
|
2013-06-26 11:23:01 +00:00
|
|
|
|
2015-06-26 06:58:16 +02:00
|
|
|
const CpuOveruseOptions options_;
|
2013-06-26 11:23:01 +00:00
|
|
|
|
2015-06-26 06:58:16 +02:00
|
|
|
// Observer getting overuse reports.
|
|
|
|
|
CpuOveruseObserver* const observer_;
|
2014-03-20 13:15:01 +00:00
|
|
|
|
2015-02-26 12:19:31 +00:00
|
|
|
// Stats metrics.
|
|
|
|
|
CpuOveruseMetricsObserver* const metrics_observer_;
|
|
|
|
|
CpuOveruseMetrics metrics_ GUARDED_BY(crit_);
|
|
|
|
|
|
2015-01-28 17:33:12 +00:00
|
|
|
Clock* const clock_;
|
2014-11-11 09:40:19 +00:00
|
|
|
int64_t num_process_times_ GUARDED_BY(crit_);
|
2013-08-30 17:16:32 +00:00
|
|
|
|
2014-11-11 09:40:19 +00:00
|
|
|
int64_t last_capture_time_ GUARDED_BY(crit_);
|
2013-08-30 17:16:32 +00:00
|
|
|
|
2015-09-24 00:53:32 -07:00
|
|
|
// Number of pixels of last captured frame.
|
|
|
|
|
int num_pixels_ GUARDED_BY(crit_);
|
|
|
|
|
|
|
|
|
|
// These seven members are only accessed on the processing thread.
|
|
|
|
|
int64_t next_process_time_;
|
2013-08-30 17:16:32 +00:00
|
|
|
int64_t last_overuse_time_;
|
|
|
|
|
int checks_above_threshold_;
|
2014-06-16 14:27:19 +00:00
|
|
|
int num_overuse_detections_;
|
2013-08-30 17:16:32 +00:00
|
|
|
int64_t last_rampup_time_;
|
|
|
|
|
bool in_quick_rampup_;
|
|
|
|
|
int current_rampup_delay_ms_;
|
2013-06-26 11:23:01 +00:00
|
|
|
|
2015-02-04 08:34:47 +00:00
|
|
|
int64_t last_encode_sample_ms_; // Only accessed by one thread.
|
2015-09-24 00:53:32 -07:00
|
|
|
int64_t last_sample_time_ms_; // Only accessed by one thread.
|
2015-02-04 08:34:47 +00:00
|
|
|
|
2015-02-08 18:27:46 +00:00
|
|
|
// TODO(asapersson): Can these be regular members (avoid separate heap
|
|
|
|
|
// allocs)?
|
2015-02-26 14:34:55 +00:00
|
|
|
const rtc::scoped_ptr<EncodeTimeAvg> encode_time_ GUARDED_BY(crit_);
|
|
|
|
|
const rtc::scoped_ptr<SendProcessingUsage> usage_ GUARDED_BY(crit_);
|
|
|
|
|
const rtc::scoped_ptr<FrameQueue> frame_queue_ GUARDED_BY(crit_);
|
2015-02-04 08:34:47 +00:00
|
|
|
|
2015-02-08 18:27:46 +00:00
|
|
|
rtc::ThreadChecker processing_thread_;
|
2013-11-20 13:51:40 +00:00
|
|
|
|
2015-09-16 05:37:44 -07:00
|
|
|
RTC_DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
|
2013-06-26 11:23:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|
|
|
|
|
|
|
|
|
|
#endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_
|