blob: 5734e6647e653dd4c99a74145b047e1d48aa4e1e [file] [log] [blame]
pbos@webrtc.orga0d78272014-09-12 11:51:47 +00001/*
2 * Copyright (c) 2014 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_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_
12#define WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_
13
kthelgason876222f2016-11-29 01:44:11 -080014#include <utility>
15
kthelgason478681e2016-09-28 08:17:43 -070016#include "webrtc/common_types.h"
kthelgason876222f2016-11-29 01:44:11 -080017#include "webrtc/video_encoder.h"
18#include "webrtc/base/optional.h"
19#include "webrtc/base/sequenced_task_checker.h"
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010020#include "webrtc/modules/video_coding/utility/moving_average.h"
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000021
22namespace webrtc {
kthelgason876222f2016-11-29 01:44:11 -080023
24// An interface for a class that receives scale up/down requests.
25class ScalingObserverInterface {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000026 public:
kthelgason876222f2016-11-29 01:44:11 -080027 enum ScaleReason : size_t { kQuality = 0, kCpu = 1 };
28 static const size_t kScaleReasonSize = 2;
29 // Called to signal that we can handle larger frames.
30 virtual void ScaleUp(ScaleReason reason) = 0;
31 // Called to signal that encoder to scale down.
32 virtual void ScaleDown(ScaleReason reason) = 0;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000033
kthelgason876222f2016-11-29 01:44:11 -080034 protected:
35 virtual ~ScalingObserverInterface() {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000036};
37
kthelgason876222f2016-11-29 01:44:11 -080038// QualityScaler runs asynchronously and monitors QP values of encoded frames.
39// It holds a reference to a ScalingObserverInterface implementation to signal
40// an intent to scale up or down.
41class QualityScaler {
42 public:
43 // Construct a QualityScaler with a given |observer|.
44 // This starts the quality scaler periodically checking what the average QP
45 // has been recently.
46 QualityScaler(ScalingObserverInterface* observer, VideoCodecType codec_type);
47 // If specific thresholds are desired these can be supplied as |thresholds|.
48 QualityScaler(ScalingObserverInterface* observer,
49 VideoEncoder::QpThresholds thresholds);
50 virtual ~QualityScaler();
51 // Should be called each time the encoder drops a frame
52 void ReportDroppedFrame();
53 // Inform the QualityScaler of the last seen QP.
54 void ReportQP(int qp);
55
56 // The following members declared protected for testing purposes
57 protected:
58 QualityScaler(ScalingObserverInterface* observer,
59 VideoEncoder::QpThresholds thresholds,
60 int64_t sampling_period);
61
62 private:
63 class CheckQPTask;
64 void CheckQP();
65 void ClearSamples();
66 void ReportQPLow();
67 void ReportQPHigh();
68 int64_t GetSamplingPeriodMs() const;
69
70 CheckQPTask* check_qp_task_ GUARDED_BY(&task_checker_);
71 ScalingObserverInterface* const observer_ GUARDED_BY(&task_checker_);
72 rtc::SequencedTaskChecker task_checker_;
73
74 const int64_t sampling_period_ms_;
75 bool fast_rampup_ GUARDED_BY(&task_checker_);
76 MovingAverage average_qp_ GUARDED_BY(&task_checker_);
77 MovingAverage framedrop_percent_ GUARDED_BY(&task_checker_);
78
79 VideoEncoder::QpThresholds thresholds_ GUARDED_BY(&task_checker_);
80};
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000081} // namespace webrtc
82
83#endif // WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_