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