niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
stefan@webrtc.org | 8fe03af | 2012-01-23 14:56:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame^] | 11 | #ifndef WEBRTC_MODULES_VIDEO_CODING_MEDIA_OPTIMIZATION_H_ |
| 12 | #define WEBRTC_MODULES_VIDEO_CODING_MEDIA_OPTIMIZATION_H_ |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 13 | |
| 14 | #include <list> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 16 | #include "webrtc/base/scoped_ptr.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 17 | #include "webrtc/modules/include/module_common_types.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame^] | 18 | #include "webrtc/modules/video_coding/include/video_coding.h" |
| 19 | #include "webrtc/modules/video_coding/media_opt_util.h" |
| 20 | #include "webrtc/modules/video_coding/qm_select.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 21 | #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 23 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 25 | // Forward declarations. |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 26 | class Clock; |
stefan@webrtc.org | eb91792 | 2013-02-18 14:40:18 +0000 | [diff] [blame] | 27 | class FrameDropper; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | class VCMContentMetricsProcessing; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 30 | namespace media_optimization { |
| 31 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 32 | class MediaOptimization { |
| 33 | public: |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 34 | explicit MediaOptimization(Clock* clock); |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 35 | ~MediaOptimization(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 37 | // TODO(andresp): Can Reset and SetEncodingData be done at construction time |
| 38 | // only? |
| 39 | void Reset(); |
| 40 | |
| 41 | // Informs media optimization of initial encoding state. |
| 42 | void SetEncodingData(VideoCodecType send_codec_type, |
| 43 | int32_t max_bit_rate, |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 44 | uint32_t bit_rate, |
| 45 | uint16_t width, |
| 46 | uint16_t height, |
Peter Boström | df66453 | 2015-05-12 12:22:14 +0200 | [diff] [blame] | 47 | uint32_t frame_rate, |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 48 | int num_temporal_layers, |
| 49 | int32_t mtu); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 51 | // Sets target rates for the encoder given the channel parameters. |
| 52 | // Inputs: target bitrate - the encoder target bitrate in bits/s. |
| 53 | // fraction_lost - packet loss rate in % in the network. |
| 54 | // round_trip_time_ms - round trip time in milliseconds. |
| 55 | // min_bit_rate - the bit rate of the end-point with lowest rate. |
| 56 | // max_bit_rate - the bit rate of the end-point with highest rate. |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 57 | // TODO(andresp): Find if the callbacks can be triggered only after releasing |
| 58 | // an internal critical section. |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 59 | uint32_t SetTargetRates(uint32_t target_bitrate, |
| 60 | uint8_t fraction_lost, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 61 | int64_t round_trip_time_ms, |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 62 | VCMProtectionCallback* protection_callback, |
| 63 | VCMQMSettingsCallback* qmsettings_callback); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 64 | |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 65 | void SetProtectionMethod(VCMProtectionMethodEnum method); |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 66 | void EnableQM(bool enable); |
| 67 | void EnableFrameDropper(bool enable); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 69 | // Lets the sender suspend video when the rate drops below |
| 70 | // |threshold_bps|, and turns back on when the rate goes back up above |
| 71 | // |threshold_bps| + |window_bps|. |
| 72 | void SuspendBelowMinBitrate(int threshold_bps, int window_bps); |
| 73 | bool IsVideoSuspended() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 75 | bool DropFrame(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 77 | void UpdateContentData(const VideoContentMetrics* content_metrics); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 78 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 79 | // Informs Media Optimization of encoded output. |
| 80 | int32_t UpdateWithEncodedData(const EncodedImage& encoded_image); |
mikhal@google.com | b29d940 | 2011-08-01 16:39:20 +0000 | [diff] [blame] | 81 | |
Erik Språng | 66a641a | 2015-06-11 14:20:07 +0200 | [diff] [blame] | 82 | // InputFrameRate 0 = no frame rate estimate available. |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 83 | uint32_t InputFrameRate(); |
| 84 | uint32_t SentFrameRate(); |
| 85 | uint32_t SentBitRate(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 87 | private: |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 88 | enum { |
| 89 | kFrameCountHistorySize = 90 |
| 90 | }; |
| 91 | enum { |
| 92 | kFrameHistoryWinMs = 2000 |
| 93 | }; |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 94 | enum { |
| 95 | kBitrateAverageWinMs = 1000 |
| 96 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 98 | struct EncodedFrameSample; |
| 99 | typedef std::list<EncodedFrameSample> FrameSampleList; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 101 | void UpdateIncomingFrameRate() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
| 102 | void PurgeOldFrameSamples(int64_t now_ms) |
| 103 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
| 104 | void UpdateSentBitrate(int64_t now_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
| 105 | void UpdateSentFramerate() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 107 | // Computes new Quality Mode. |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 108 | int32_t SelectQuality(VCMQMSettingsCallback* qmsettings_callback) |
| 109 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 110 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 111 | // Verifies if QM settings differ from default, i.e. if an update is required. |
| 112 | // Computes actual values, as will be sent to the encoder. |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 113 | bool QMUpdate(VCMResolutionScale* qm, |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 114 | VCMQMSettingsCallback* qmsettings_callback) |
| 115 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 116 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 117 | // Checks if we should make a QM change. Return true if yes, false otherwise. |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 118 | bool CheckStatusForQMchange() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 119 | |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 120 | void ProcessIncomingFrameRate(int64_t now) |
| 121 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 123 | // Checks conditions for suspending the video. The method compares |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 124 | // |video_target_bitrate_| with the threshold values for suspension, and |
| 125 | // changes the state of |video_suspended_| accordingly. |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 126 | void CheckSuspendConditions() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 127 | |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 128 | void SetEncodingDataInternal(VideoCodecType send_codec_type, |
| 129 | int32_t max_bit_rate, |
| 130 | uint32_t frame_rate, |
| 131 | uint32_t bit_rate, |
| 132 | uint16_t width, |
| 133 | uint16_t height, |
| 134 | int num_temporal_layers, |
| 135 | int32_t mtu) |
| 136 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
| 137 | |
| 138 | uint32_t InputFrameRateInternal() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
| 139 | |
| 140 | uint32_t SentFrameRateInternal() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
| 141 | |
| 142 | // Protect all members. |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 143 | rtc::scoped_ptr<CriticalSectionWrapper> crit_sect_; |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 144 | |
| 145 | Clock* clock_ GUARDED_BY(crit_sect_); |
| 146 | int32_t max_bit_rate_ GUARDED_BY(crit_sect_); |
| 147 | VideoCodecType send_codec_type_ GUARDED_BY(crit_sect_); |
| 148 | uint16_t codec_width_ GUARDED_BY(crit_sect_); |
| 149 | uint16_t codec_height_ GUARDED_BY(crit_sect_); |
| 150 | float user_frame_rate_ GUARDED_BY(crit_sect_); |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 151 | rtc::scoped_ptr<FrameDropper> frame_dropper_ GUARDED_BY(crit_sect_); |
| 152 | rtc::scoped_ptr<VCMLossProtectionLogic> loss_prot_logic_ |
| 153 | GUARDED_BY(crit_sect_); |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 154 | uint8_t fraction_lost_ GUARDED_BY(crit_sect_); |
| 155 | uint32_t send_statistics_[4] GUARDED_BY(crit_sect_); |
| 156 | uint32_t send_statistics_zero_encode_ GUARDED_BY(crit_sect_); |
| 157 | int32_t max_payload_size_ GUARDED_BY(crit_sect_); |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 158 | int video_target_bitrate_ GUARDED_BY(crit_sect_); |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 159 | float incoming_frame_rate_ GUARDED_BY(crit_sect_); |
| 160 | int64_t incoming_frame_times_[kFrameCountHistorySize] GUARDED_BY(crit_sect_); |
| 161 | bool enable_qm_ GUARDED_BY(crit_sect_); |
| 162 | std::list<EncodedFrameSample> encoded_frame_samples_ GUARDED_BY(crit_sect_); |
| 163 | uint32_t avg_sent_bit_rate_bps_ GUARDED_BY(crit_sect_); |
| 164 | uint32_t avg_sent_framerate_ GUARDED_BY(crit_sect_); |
| 165 | uint32_t key_frame_cnt_ GUARDED_BY(crit_sect_); |
| 166 | uint32_t delta_frame_cnt_ GUARDED_BY(crit_sect_); |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 167 | rtc::scoped_ptr<VCMContentMetricsProcessing> content_ GUARDED_BY(crit_sect_); |
| 168 | rtc::scoped_ptr<VCMQmResolution> qm_resolution_ GUARDED_BY(crit_sect_); |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 169 | int64_t last_qm_update_time_ GUARDED_BY(crit_sect_); |
| 170 | int64_t last_change_time_ GUARDED_BY(crit_sect_); // Content/user triggered. |
| 171 | int num_layers_ GUARDED_BY(crit_sect_); |
| 172 | bool suspension_enabled_ GUARDED_BY(crit_sect_); |
| 173 | bool video_suspended_ GUARDED_BY(crit_sect_); |
| 174 | int suspension_threshold_bps_ GUARDED_BY(crit_sect_); |
| 175 | int suspension_window_bps_ GUARDED_BY(crit_sect_); |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 176 | }; |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 177 | } // namespace media_optimization |
| 178 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 179 | |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame^] | 180 | #endif // WEBRTC_MODULES_VIDEO_CODING_MEDIA_OPTIMIZATION_H_ |