niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +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 | #include "webrtc/modules/video_coding/media_optimization.h" |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 12 | |
pbos | 854e84c | 2015-11-16 16:39:06 -0800 | [diff] [blame] | 13 | #include "webrtc/base/logging.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 14 | #include "webrtc/modules/video_coding/content_metrics_processing.h" |
| 15 | #include "webrtc/modules/video_coding/qm_select.h" |
kjellander@webrtc.org | b7ce964 | 2015-11-18 23:04:10 +0100 | [diff] [blame] | 16 | #include "webrtc/modules/video_coding/utility/frame_dropper.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 17 | #include "webrtc/system_wrappers/include/clock.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 20 | namespace media_optimization { |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 21 | namespace { |
| 22 | void UpdateProtectionCallback( |
| 23 | VCMProtectionMethod* selected_method, |
| 24 | uint32_t* video_rate_bps, |
| 25 | uint32_t* nack_overhead_rate_bps, |
| 26 | uint32_t* fec_overhead_rate_bps, |
| 27 | VCMProtectionCallback* video_protection_callback) { |
| 28 | FecProtectionParams delta_fec_params; |
| 29 | FecProtectionParams key_fec_params; |
| 30 | // Get the FEC code rate for Key frames (set to 0 when NA). |
| 31 | key_fec_params.fec_rate = selected_method->RequiredProtectionFactorK(); |
| 32 | |
| 33 | // Get the FEC code rate for Delta frames (set to 0 when NA). |
| 34 | delta_fec_params.fec_rate = selected_method->RequiredProtectionFactorD(); |
| 35 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 36 | // The RTP module currently requires the same |max_fec_frames| for both |
| 37 | // key and delta frames. |
| 38 | delta_fec_params.max_fec_frames = selected_method->MaxFramesFec(); |
| 39 | key_fec_params.max_fec_frames = selected_method->MaxFramesFec(); |
| 40 | |
| 41 | // Set the FEC packet mask type. |kFecMaskBursty| is more effective for |
| 42 | // consecutive losses and little/no packet re-ordering. As we currently |
| 43 | // do not have feedback data on the degree of correlated losses and packet |
| 44 | // re-ordering, we keep default setting to |kFecMaskRandom| for now. |
| 45 | delta_fec_params.fec_mask_type = kFecMaskRandom; |
| 46 | key_fec_params.fec_mask_type = kFecMaskRandom; |
| 47 | |
| 48 | // TODO(Marco): Pass FEC protection values per layer. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 49 | video_protection_callback->ProtectionRequest( |
| 50 | &delta_fec_params, &key_fec_params, video_rate_bps, |
| 51 | nack_overhead_rate_bps, fec_overhead_rate_bps); |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 52 | } |
| 53 | } // namespace |
| 54 | |
| 55 | struct MediaOptimization::EncodedFrameSample { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 56 | EncodedFrameSample(size_t size_bytes, |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 57 | uint32_t timestamp, |
| 58 | int64_t time_complete_ms) |
| 59 | : size_bytes(size_bytes), |
| 60 | timestamp(timestamp), |
| 61 | time_complete_ms(time_complete_ms) {} |
| 62 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 63 | size_t size_bytes; |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 64 | uint32_t timestamp; |
| 65 | int64_t time_complete_ms; |
| 66 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 68 | MediaOptimization::MediaOptimization(Clock* clock) |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 69 | : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), |
| 70 | clock_(clock), |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 71 | max_bit_rate_(0), |
| 72 | send_codec_type_(kVideoCodecUnknown), |
| 73 | codec_width_(0), |
| 74 | codec_height_(0), |
| 75 | user_frame_rate_(0), |
henrik.lundin@webrtc.org | b426c46 | 2013-09-24 07:41:53 +0000 | [diff] [blame] | 76 | frame_dropper_(new FrameDropper), |
| 77 | loss_prot_logic_( |
| 78 | new VCMLossProtectionLogic(clock_->TimeInMilliseconds())), |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 79 | fraction_lost_(0), |
| 80 | send_statistics_zero_encode_(0), |
| 81 | max_payload_size_(1460), |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 82 | video_target_bitrate_(0), |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 83 | incoming_frame_rate_(0), |
| 84 | enable_qm_(false), |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 85 | encoded_frame_samples_(), |
| 86 | avg_sent_bit_rate_bps_(0), |
| 87 | avg_sent_framerate_(0), |
| 88 | key_frame_cnt_(0), |
| 89 | delta_frame_cnt_(0), |
henrik.lundin@webrtc.org | b426c46 | 2013-09-24 07:41:53 +0000 | [diff] [blame] | 90 | content_(new VCMContentMetricsProcessing()), |
| 91 | qm_resolution_(new VCMQmResolution()), |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 92 | last_qm_update_time_(0), |
| 93 | last_change_time_(0), |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 94 | num_layers_(0), |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 95 | suspension_enabled_(false), |
| 96 | video_suspended_(false), |
| 97 | suspension_threshold_bps_(0), |
| 98 | suspension_window_bps_(0) { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 99 | memset(send_statistics_, 0, sizeof(send_statistics_)); |
| 100 | memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | } |
| 102 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 103 | MediaOptimization::~MediaOptimization(void) { |
| 104 | loss_prot_logic_->Release(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | } |
| 106 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 107 | void MediaOptimization::Reset() { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 108 | CriticalSectionScoped lock(crit_sect_.get()); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 109 | SetEncodingDataInternal(kVideoCodecUnknown, 0, 0, 0, 0, 0, 0, |
| 110 | max_payload_size_); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 111 | memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_)); |
| 112 | incoming_frame_rate_ = 0.0; |
| 113 | frame_dropper_->Reset(); |
| 114 | loss_prot_logic_->Reset(clock_->TimeInMilliseconds()); |
| 115 | frame_dropper_->SetRates(0, 0); |
| 116 | content_->Reset(); |
| 117 | qm_resolution_->Reset(); |
| 118 | loss_prot_logic_->UpdateFrameRate(incoming_frame_rate_); |
| 119 | loss_prot_logic_->Reset(clock_->TimeInMilliseconds()); |
| 120 | send_statistics_zero_encode_ = 0; |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 121 | video_target_bitrate_ = 0; |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 122 | codec_width_ = 0; |
| 123 | codec_height_ = 0; |
| 124 | user_frame_rate_ = 0; |
| 125 | key_frame_cnt_ = 0; |
| 126 | delta_frame_cnt_ = 0; |
| 127 | last_qm_update_time_ = 0; |
| 128 | last_change_time_ = 0; |
| 129 | encoded_frame_samples_.clear(); |
| 130 | avg_sent_bit_rate_bps_ = 0; |
| 131 | num_layers_ = 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | } |
| 133 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 134 | void MediaOptimization::SetEncodingData(VideoCodecType send_codec_type, |
| 135 | int32_t max_bit_rate, |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 136 | uint32_t target_bitrate, |
| 137 | uint16_t width, |
| 138 | uint16_t height, |
Peter Boström | df66453 | 2015-05-12 12:22:14 +0200 | [diff] [blame] | 139 | uint32_t frame_rate, |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 140 | int num_layers, |
| 141 | int32_t mtu) { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 142 | CriticalSectionScoped lock(crit_sect_.get()); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 143 | SetEncodingDataInternal(send_codec_type, max_bit_rate, frame_rate, |
| 144 | target_bitrate, width, height, num_layers, mtu); |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void MediaOptimization::SetEncodingDataInternal(VideoCodecType send_codec_type, |
| 148 | int32_t max_bit_rate, |
| 149 | uint32_t frame_rate, |
| 150 | uint32_t target_bitrate, |
| 151 | uint16_t width, |
| 152 | uint16_t height, |
| 153 | int num_layers, |
| 154 | int32_t mtu) { |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 155 | // Everything codec specific should be reset here since this means the codec |
| 156 | // has changed. If native dimension values have changed, then either user |
| 157 | // initiated change, or QM initiated change. Will be able to determine only |
| 158 | // after the processing of the first frame. |
| 159 | last_change_time_ = clock_->TimeInMilliseconds(); |
| 160 | content_->Reset(); |
| 161 | content_->UpdateFrameRate(frame_rate); |
| 162 | |
| 163 | max_bit_rate_ = max_bit_rate; |
| 164 | send_codec_type_ = send_codec_type; |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 165 | video_target_bitrate_ = target_bitrate; |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 166 | float target_bitrate_kbps = static_cast<float>(target_bitrate) / 1000.0f; |
| 167 | loss_prot_logic_->UpdateBitRate(target_bitrate_kbps); |
| 168 | loss_prot_logic_->UpdateFrameRate(static_cast<float>(frame_rate)); |
| 169 | loss_prot_logic_->UpdateFrameSize(width, height); |
| 170 | loss_prot_logic_->UpdateNumLayers(num_layers); |
| 171 | frame_dropper_->Reset(); |
| 172 | frame_dropper_->SetRates(target_bitrate_kbps, static_cast<float>(frame_rate)); |
| 173 | user_frame_rate_ = static_cast<float>(frame_rate); |
| 174 | codec_width_ = width; |
| 175 | codec_height_ = height; |
| 176 | num_layers_ = (num_layers <= 1) ? 1 : num_layers; // Can also be zero. |
| 177 | max_payload_size_ = mtu; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 178 | qm_resolution_->Initialize(target_bitrate_kbps, user_frame_rate_, |
| 179 | codec_width_, codec_height_, num_layers_); |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | uint32_t MediaOptimization::SetTargetRates( |
| 183 | uint32_t target_bitrate, |
| 184 | uint8_t fraction_lost, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 185 | int64_t round_trip_time_ms, |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 186 | VCMProtectionCallback* protection_callback, |
| 187 | VCMQMSettingsCallback* qmsettings_callback) { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 188 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 189 | VCMProtectionMethod* selected_method = loss_prot_logic_->SelectedMethod(); |
| 190 | float target_bitrate_kbps = static_cast<float>(target_bitrate) / 1000.0f; |
| 191 | loss_prot_logic_->UpdateBitRate(target_bitrate_kbps); |
| 192 | loss_prot_logic_->UpdateRtt(round_trip_time_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 193 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 194 | // Get frame rate for encoder: this is the actual/sent frame rate. |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 195 | float actual_frame_rate = SentFrameRateInternal(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 196 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 197 | // Sanity check. |
| 198 | if (actual_frame_rate < 1.0) { |
| 199 | actual_frame_rate = 1.0; |
| 200 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 201 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 202 | // Update frame rate for the loss protection logic class: frame rate should |
| 203 | // be the actual/sent rate. |
| 204 | loss_prot_logic_->UpdateFrameRate(actual_frame_rate); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 206 | fraction_lost_ = fraction_lost; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 207 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 208 | // Returns the filtered packet loss, used for the protection setting. |
| 209 | // The filtered loss may be the received loss (no filter), or some |
| 210 | // filtered value (average or max window filter). |
| 211 | // Use max window filter for now. |
| 212 | FilterPacketLossMode filter_mode = kMaxFilter; |
| 213 | uint8_t packet_loss_enc = loss_prot_logic_->FilteredLoss( |
| 214 | clock_->TimeInMilliseconds(), filter_mode, fraction_lost); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 215 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 216 | // For now use the filtered loss for computing the robustness settings. |
| 217 | loss_prot_logic_->UpdateFilteredLossPr(packet_loss_enc); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 218 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 219 | // Rate cost of the protection methods. |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 220 | float protection_overhead_rate = 0.0f; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 221 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 222 | // Update protection settings, when applicable. |
| 223 | float sent_video_rate_kbps = 0.0f; |
mflodman | fcf54bd | 2015-04-14 21:28:08 +0200 | [diff] [blame] | 224 | if (loss_prot_logic_->SelectedType() != kNone) { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 225 | // Update method will compute the robustness settings for the given |
| 226 | // protection method and the overhead cost |
| 227 | // the protection method is set by the user via SetVideoProtection. |
| 228 | loss_prot_logic_->UpdateMethod(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 229 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 230 | // Update protection callback with protection settings. |
| 231 | uint32_t sent_video_rate_bps = 0; |
| 232 | uint32_t sent_nack_rate_bps = 0; |
| 233 | uint32_t sent_fec_rate_bps = 0; |
| 234 | // Get the bit cost of protection method, based on the amount of |
| 235 | // overhead data actually transmitted (including headers) the last |
| 236 | // second. |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 237 | if (protection_callback) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 238 | UpdateProtectionCallback(selected_method, &sent_video_rate_bps, |
| 239 | &sent_nack_rate_bps, &sent_fec_rate_bps, |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 240 | protection_callback); |
| 241 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 242 | uint32_t sent_total_rate_bps = |
| 243 | sent_video_rate_bps + sent_nack_rate_bps + sent_fec_rate_bps; |
| 244 | // Estimate the overhead costs of the next second as staying the same |
| 245 | // wrt the source bitrate. |
| 246 | if (sent_total_rate_bps > 0) { |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 247 | protection_overhead_rate = |
| 248 | static_cast<float>(sent_nack_rate_bps + sent_fec_rate_bps) / |
| 249 | sent_total_rate_bps; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 250 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 251 | // Cap the overhead estimate to 50%. |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 252 | if (protection_overhead_rate > 0.5) |
| 253 | protection_overhead_rate = 0.5; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 254 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 255 | // Get the effective packet loss for encoder ER when applicable. Should be |
| 256 | // passed to encoder via fraction_lost. |
| 257 | packet_loss_enc = selected_method->RequiredPacketLossER(); |
| 258 | sent_video_rate_kbps = static_cast<float>(sent_video_rate_bps) / 1000.0f; |
| 259 | } |
stefan@webrtc.org | f4c8286 | 2011-12-13 15:38:14 +0000 | [diff] [blame] | 260 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 261 | // Source coding rate: total rate - protection overhead. |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 262 | video_target_bitrate_ = target_bitrate * (1.0 - protection_overhead_rate); |
| 263 | |
| 264 | // Cap target video bitrate to codec maximum. |
| 265 | if (max_bit_rate_ > 0 && video_target_bitrate_ > max_bit_rate_) { |
| 266 | video_target_bitrate_ = max_bit_rate_; |
| 267 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 268 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 269 | // Update encoding rates following protection settings. |
| 270 | float target_video_bitrate_kbps = |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 271 | static_cast<float>(video_target_bitrate_) / 1000.0f; |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 272 | frame_dropper_->SetRates(target_video_bitrate_kbps, incoming_frame_rate_); |
| 273 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 274 | if (enable_qm_ && qmsettings_callback) { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 275 | // Update QM with rates. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 276 | qm_resolution_->UpdateRates(target_video_bitrate_kbps, sent_video_rate_kbps, |
| 277 | incoming_frame_rate_, fraction_lost_); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 278 | // Check for QM selection. |
| 279 | bool select_qm = CheckStatusForQMchange(); |
| 280 | if (select_qm) { |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 281 | SelectQuality(qmsettings_callback); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 282 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 283 | // Reset the short-term averaged content data. |
| 284 | content_->ResetShortTermAvgData(); |
| 285 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 286 | |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 287 | CheckSuspendConditions(); |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 288 | |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 289 | return video_target_bitrate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 290 | } |
| 291 | |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 292 | void MediaOptimization::SetProtectionMethod(VCMProtectionMethodEnum method) { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 293 | CriticalSectionScoped lock(crit_sect_.get()); |
pbos@webrtc.org | cade82c | 2015-03-12 10:39:24 +0000 | [diff] [blame] | 294 | loss_prot_logic_->SetMethod(method); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 295 | } |
| 296 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 297 | uint32_t MediaOptimization::InputFrameRate() { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 298 | CriticalSectionScoped lock(crit_sect_.get()); |
| 299 | return InputFrameRateInternal(); |
| 300 | } |
| 301 | |
| 302 | uint32_t MediaOptimization::InputFrameRateInternal() { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 303 | ProcessIncomingFrameRate(clock_->TimeInMilliseconds()); |
| 304 | return uint32_t(incoming_frame_rate_ + 0.5f); |
| 305 | } |
| 306 | |
| 307 | uint32_t MediaOptimization::SentFrameRate() { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 308 | CriticalSectionScoped lock(crit_sect_.get()); |
| 309 | return SentFrameRateInternal(); |
| 310 | } |
| 311 | |
| 312 | uint32_t MediaOptimization::SentFrameRateInternal() { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 313 | PurgeOldFrameSamples(clock_->TimeInMilliseconds()); |
| 314 | UpdateSentFramerate(); |
| 315 | return avg_sent_framerate_; |
| 316 | } |
| 317 | |
| 318 | uint32_t MediaOptimization::SentBitRate() { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 319 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 320 | const int64_t now_ms = clock_->TimeInMilliseconds(); |
| 321 | PurgeOldFrameSamples(now_ms); |
| 322 | UpdateSentBitrate(now_ms); |
| 323 | return avg_sent_bit_rate_bps_; |
| 324 | } |
| 325 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 326 | int32_t MediaOptimization::UpdateWithEncodedData( |
| 327 | const EncodedImage& encoded_image) { |
| 328 | size_t encoded_length = encoded_image._length; |
| 329 | uint32_t timestamp = encoded_image._timeStamp; |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 330 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 331 | const int64_t now_ms = clock_->TimeInMilliseconds(); |
| 332 | PurgeOldFrameSamples(now_ms); |
| 333 | if (encoded_frame_samples_.size() > 0 && |
| 334 | encoded_frame_samples_.back().timestamp == timestamp) { |
| 335 | // Frames having the same timestamp are generated from the same input |
| 336 | // frame. We don't want to double count them, but only increment the |
| 337 | // size_bytes. |
| 338 | encoded_frame_samples_.back().size_bytes += encoded_length; |
| 339 | encoded_frame_samples_.back().time_complete_ms = now_ms; |
| 340 | } else { |
| 341 | encoded_frame_samples_.push_back( |
| 342 | EncodedFrameSample(encoded_length, timestamp, now_ms)); |
| 343 | } |
| 344 | UpdateSentBitrate(now_ms); |
| 345 | UpdateSentFramerate(); |
| 346 | if (encoded_length > 0) { |
Peter Boström | 49e196a | 2015-10-23 15:58:18 +0200 | [diff] [blame] | 347 | const bool delta_frame = encoded_image._frameType != kVideoFrameKey; |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 348 | |
| 349 | frame_dropper_->Fill(encoded_length, delta_frame); |
| 350 | if (max_payload_size_ > 0 && encoded_length > 0) { |
| 351 | const float min_packets_per_frame = |
| 352 | encoded_length / static_cast<float>(max_payload_size_); |
| 353 | if (delta_frame) { |
| 354 | loss_prot_logic_->UpdatePacketsPerFrame(min_packets_per_frame, |
| 355 | clock_->TimeInMilliseconds()); |
| 356 | } else { |
| 357 | loss_prot_logic_->UpdatePacketsPerFrameKey( |
| 358 | min_packets_per_frame, clock_->TimeInMilliseconds()); |
| 359 | } |
| 360 | |
| 361 | if (enable_qm_) { |
| 362 | // Update quality select with encoded length. |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 363 | qm_resolution_->UpdateEncodedSize(encoded_length); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | if (!delta_frame && encoded_length > 0) { |
| 367 | loss_prot_logic_->UpdateKeyFrameSize(static_cast<float>(encoded_length)); |
| 368 | } |
| 369 | |
| 370 | // Updating counters. |
| 371 | if (delta_frame) { |
| 372 | delta_frame_cnt_++; |
| 373 | } else { |
| 374 | key_frame_cnt_++; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | return VCM_OK; |
| 379 | } |
| 380 | |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 381 | void MediaOptimization::EnableQM(bool enable) { |
| 382 | CriticalSectionScoped lock(crit_sect_.get()); |
| 383 | enable_qm_ = enable; |
| 384 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 385 | |
| 386 | void MediaOptimization::EnableFrameDropper(bool enable) { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 387 | CriticalSectionScoped lock(crit_sect_.get()); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 388 | frame_dropper_->Enable(enable); |
| 389 | } |
| 390 | |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 391 | void MediaOptimization::SuspendBelowMinBitrate(int threshold_bps, |
| 392 | int window_bps) { |
| 393 | CriticalSectionScoped lock(crit_sect_.get()); |
| 394 | assert(threshold_bps > 0 && window_bps >= 0); |
| 395 | suspension_threshold_bps_ = threshold_bps; |
| 396 | suspension_window_bps_ = window_bps; |
| 397 | suspension_enabled_ = true; |
| 398 | video_suspended_ = false; |
| 399 | } |
| 400 | |
| 401 | bool MediaOptimization::IsVideoSuspended() const { |
| 402 | CriticalSectionScoped lock(crit_sect_.get()); |
| 403 | return video_suspended_; |
| 404 | } |
| 405 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 406 | bool MediaOptimization::DropFrame() { |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 407 | CriticalSectionScoped lock(crit_sect_.get()); |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 408 | UpdateIncomingFrameRate(); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 409 | // Leak appropriate number of bytes. |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 410 | frame_dropper_->Leak((uint32_t)(InputFrameRateInternal() + 0.5f)); |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 411 | if (video_suspended_) { |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 412 | return true; // Drop all frames when muted. |
| 413 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 414 | return frame_dropper_->DropFrame(); |
| 415 | } |
| 416 | |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 417 | void MediaOptimization::UpdateContentData( |
| 418 | const VideoContentMetrics* content_metrics) { |
| 419 | CriticalSectionScoped lock(crit_sect_.get()); |
| 420 | // Updating content metrics. |
| 421 | if (content_metrics == NULL) { |
| 422 | // Disable QM if metrics are NULL. |
| 423 | enable_qm_ = false; |
| 424 | qm_resolution_->Reset(); |
| 425 | } else { |
| 426 | content_->UpdateContentData(content_metrics); |
| 427 | } |
| 428 | } |
| 429 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 430 | void MediaOptimization::UpdateIncomingFrameRate() { |
| 431 | int64_t now = clock_->TimeInMilliseconds(); |
| 432 | if (incoming_frame_times_[0] == 0) { |
| 433 | // No shifting if this is the first time. |
| 434 | } else { |
| 435 | // Shift all times one step. |
| 436 | for (int32_t i = (kFrameCountHistorySize - 2); i >= 0; i--) { |
| 437 | incoming_frame_times_[i + 1] = incoming_frame_times_[i]; |
| 438 | } |
| 439 | } |
| 440 | incoming_frame_times_[0] = now; |
| 441 | ProcessIncomingFrameRate(now); |
| 442 | } |
| 443 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 444 | int32_t MediaOptimization::SelectQuality( |
| 445 | VCMQMSettingsCallback* video_qmsettings_callback) { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 446 | // Reset quantities for QM select. |
| 447 | qm_resolution_->ResetQM(); |
| 448 | |
| 449 | // Update QM will long-term averaged content metrics. |
| 450 | qm_resolution_->UpdateContent(content_->LongTermAvgData()); |
| 451 | |
| 452 | // Select quality mode. |
| 453 | VCMResolutionScale* qm = NULL; |
| 454 | int32_t ret = qm_resolution_->SelectResolution(&qm); |
| 455 | if (ret < 0) { |
| 456 | return ret; |
| 457 | } |
| 458 | |
| 459 | // Check for updates to spatial/temporal modes. |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 460 | QMUpdate(qm, video_qmsettings_callback); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 461 | |
| 462 | // Reset all the rate and related frame counters quantities. |
| 463 | qm_resolution_->ResetRates(); |
| 464 | |
| 465 | // Reset counters. |
| 466 | last_qm_update_time_ = clock_->TimeInMilliseconds(); |
| 467 | |
| 468 | // Reset content metrics. |
| 469 | content_->Reset(); |
| 470 | |
| 471 | return VCM_OK; |
| 472 | } |
| 473 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 474 | void MediaOptimization::PurgeOldFrameSamples(int64_t now_ms) { |
| 475 | while (!encoded_frame_samples_.empty()) { |
| 476 | if (now_ms - encoded_frame_samples_.front().time_complete_ms > |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 477 | kBitrateAverageWinMs) { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 478 | encoded_frame_samples_.pop_front(); |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 479 | } else { |
| 480 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 481 | } |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 482 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 483 | } |
| 484 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 485 | void MediaOptimization::UpdateSentBitrate(int64_t now_ms) { |
| 486 | if (encoded_frame_samples_.empty()) { |
| 487 | avg_sent_bit_rate_bps_ = 0; |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 488 | return; |
| 489 | } |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 490 | size_t framesize_sum = 0; |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 491 | for (FrameSampleList::iterator it = encoded_frame_samples_.begin(); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 492 | it != encoded_frame_samples_.end(); ++it) { |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 493 | framesize_sum += it->size_bytes; |
| 494 | } |
| 495 | float denom = static_cast<float>( |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 496 | now_ms - encoded_frame_samples_.front().time_complete_ms); |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 497 | if (denom >= 1.0f) { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 498 | avg_sent_bit_rate_bps_ = |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 499 | static_cast<uint32_t>(framesize_sum * 8.0f * 1000.0f / denom + 0.5f); |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 500 | } else { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 501 | avg_sent_bit_rate_bps_ = framesize_sum * 8; |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 505 | void MediaOptimization::UpdateSentFramerate() { |
| 506 | if (encoded_frame_samples_.size() <= 1) { |
| 507 | avg_sent_framerate_ = encoded_frame_samples_.size(); |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 508 | return; |
| 509 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 510 | int denom = encoded_frame_samples_.back().timestamp - |
| 511 | encoded_frame_samples_.front().timestamp; |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 512 | if (denom > 0) { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 513 | avg_sent_framerate_ = |
| 514 | (90000 * (encoded_frame_samples_.size() - 1) + denom / 2) / denom; |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 515 | } else { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 516 | avg_sent_framerate_ = encoded_frame_samples_.size(); |
stefan@webrtc.org | f4944d4 | 2013-03-18 17:04:52 +0000 | [diff] [blame] | 517 | } |
| 518 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 519 | |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 520 | bool MediaOptimization::QMUpdate( |
| 521 | VCMResolutionScale* qm, |
| 522 | VCMQMSettingsCallback* video_qmsettings_callback) { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 523 | // Check for no change. |
marpan@webrtc.org | e22d81c | 2012-03-20 18:21:53 +0000 | [diff] [blame] | 524 | if (!qm->change_resolution_spatial && !qm->change_resolution_temporal) { |
marpan@webrtc.org | accf607 | 2012-03-07 17:16:10 +0000 | [diff] [blame] | 525 | return false; |
| 526 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 527 | |
marpan@webrtc.org | accf607 | 2012-03-07 17:16:10 +0000 | [diff] [blame] | 528 | // Check for change in frame rate. |
marpan@webrtc.org | e22d81c | 2012-03-20 18:21:53 +0000 | [diff] [blame] | 529 | if (qm->change_resolution_temporal) { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 530 | incoming_frame_rate_ = qm->frame_rate; |
marpan@webrtc.org | e22d81c | 2012-03-20 18:21:53 +0000 | [diff] [blame] | 531 | // Reset frame rate estimate. |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 532 | memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_)); |
marpan@webrtc.org | accf607 | 2012-03-07 17:16:10 +0000 | [diff] [blame] | 533 | } |
marpan@google.com | 86548c6 | 2011-07-12 17:12:57 +0000 | [diff] [blame] | 534 | |
marpan@webrtc.org | accf607 | 2012-03-07 17:16:10 +0000 | [diff] [blame] | 535 | // Check for change in frame size. |
marpan@webrtc.org | e22d81c | 2012-03-20 18:21:53 +0000 | [diff] [blame] | 536 | if (qm->change_resolution_spatial) { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 537 | codec_width_ = qm->codec_width; |
| 538 | codec_height_ = qm->codec_height; |
marpan@webrtc.org | accf607 | 2012-03-07 17:16:10 +0000 | [diff] [blame] | 539 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 540 | |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 541 | LOG(LS_INFO) << "Media optimizer requests the video resolution to be changed " |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 542 | "to " |
| 543 | << qm->codec_width << "x" << qm->codec_height << "@" |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 544 | << qm->frame_rate; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 545 | |
marpan@webrtc.org | e22d81c | 2012-03-20 18:21:53 +0000 | [diff] [blame] | 546 | // Update VPM with new target frame rate and frame size. |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 547 | // Note: use |qm->frame_rate| instead of |_incoming_frame_rate| for updating |
| 548 | // target frame rate in VPM frame dropper. The quantity |_incoming_frame_rate| |
marpan@webrtc.org | e22d81c | 2012-03-20 18:21:53 +0000 | [diff] [blame] | 549 | // will vary/fluctuate, and since we don't want to change the state of the |
| 550 | // VPM frame dropper, unless a temporal action was selected, we use the |
| 551 | // quantity |qm->frame_rate| for updating. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 552 | video_qmsettings_callback->SetVideoQMSettings(qm->frame_rate, codec_width_, |
| 553 | codec_height_); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 554 | content_->UpdateFrameRate(qm->frame_rate); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 555 | qm_resolution_->UpdateCodecParameters(qm->frame_rate, codec_width_, |
| 556 | codec_height_); |
marpan@webrtc.org | accf607 | 2012-03-07 17:16:10 +0000 | [diff] [blame] | 557 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 558 | } |
| 559 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 560 | // Check timing constraints and look for significant change in: |
| 561 | // (1) scene content, |
| 562 | // (2) target bit rate. |
| 563 | bool MediaOptimization::CheckStatusForQMchange() { |
| 564 | bool status = true; |
| 565 | |
| 566 | // Check that we do not call QMSelect too often, and that we waited some time |
| 567 | // (to sample the metrics) from the event last_change_time |
| 568 | // last_change_time is the time where user changed the size/rate/frame rate |
| 569 | // (via SetEncodingData). |
| 570 | int64_t now = clock_->TimeInMilliseconds(); |
| 571 | if ((now - last_qm_update_time_) < kQmMinIntervalMs || |
| 572 | (now - last_change_time_) < kQmMinIntervalMs) { |
| 573 | status = false; |
| 574 | } |
| 575 | |
| 576 | return status; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 577 | } |
| 578 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 579 | // Allowing VCM to keep track of incoming frame rate. |
| 580 | void MediaOptimization::ProcessIncomingFrameRate(int64_t now) { |
| 581 | int32_t num = 0; |
| 582 | int32_t nr_of_frames = 0; |
| 583 | for (num = 1; num < (kFrameCountHistorySize - 1); ++num) { |
| 584 | if (incoming_frame_times_[num] <= 0 || |
| 585 | // don't use data older than 2 s |
| 586 | now - incoming_frame_times_[num] > kFrameHistoryWinMs) { |
| 587 | break; |
| 588 | } else { |
| 589 | nr_of_frames++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 590 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 591 | } |
| 592 | if (num > 1) { |
Erik Språng | 66a641a | 2015-06-11 14:20:07 +0200 | [diff] [blame] | 593 | const int64_t diff = |
| 594 | incoming_frame_times_[0] - incoming_frame_times_[num - 1]; |
| 595 | incoming_frame_rate_ = 0.0; // No frame rate estimate available. |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 596 | if (diff > 0) { |
| 597 | incoming_frame_rate_ = nr_of_frames * 1000.0f / static_cast<float>(diff); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 598 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 599 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 600 | } |
| 601 | |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 602 | void MediaOptimization::CheckSuspendConditions() { |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 603 | // Check conditions for SuspendBelowMinBitrate. |video_target_bitrate_| is in |
| 604 | // bps. |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 605 | if (suspension_enabled_) { |
| 606 | if (!video_suspended_) { |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 607 | // Check if we just went below the threshold. |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 608 | if (video_target_bitrate_ < suspension_threshold_bps_) { |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 609 | video_suspended_ = true; |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 610 | } |
| 611 | } else { |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 612 | // Video is already suspended. Check if we just went over the threshold |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 613 | // with a margin. |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 614 | if (video_target_bitrate_ > |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 615 | suspension_threshold_bps_ + suspension_window_bps_) { |
| 616 | video_suspended_ = false; |
henrik.lundin@webrtc.org | 544b17c | 2013-09-26 12:05:15 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 622 | } // namespace media_optimization |
| 623 | } // namespace webrtc |