blob: a234a06f9b4ce137581f5811d86b26d408d59344 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Kjellander2557b862015-11-18 22:00:21 +010011#include "webrtc/modules/video_coding/media_optimization.h"
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000012
pbos854e84c2015-11-16 16:39:06 -080013#include "webrtc/base/logging.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010014#include "webrtc/modules/video_coding/content_metrics_processing.h"
15#include "webrtc/modules/video_coding/qm_select.h"
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010016#include "webrtc/modules/video_coding/utility/frame_dropper.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010017#include "webrtc/system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018
19namespace webrtc {
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000020namespace media_optimization {
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000021namespace {
22void 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.
philipel9d3ab612015-12-21 04:12:39 -080056 video_protection_callback->ProtectionRequest(
57 &delta_fec_params, &key_fec_params, video_rate_bps,
58 nack_overhead_rate_bps, fec_overhead_rate_bps);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000059}
60} // namespace
61
62struct MediaOptimization::EncodedFrameSample {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000063 EncodedFrameSample(size_t size_bytes,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000064 uint32_t timestamp,
65 int64_t time_complete_ms)
66 : size_bytes(size_bytes),
67 timestamp(timestamp),
68 time_complete_ms(time_complete_ms) {}
69
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000070 size_t size_bytes;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000071 uint32_t timestamp;
72 int64_t time_complete_ms;
73};
niklase@google.com470e71d2011-07-07 08:21:25 +000074
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000075MediaOptimization::MediaOptimization(Clock* clock)
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000076 : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
77 clock_(clock),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000078 max_bit_rate_(0),
79 send_codec_type_(kVideoCodecUnknown),
80 codec_width_(0),
81 codec_height_(0),
82 user_frame_rate_(0),
henrik.lundin@webrtc.orgb426c462013-09-24 07:41:53 +000083 frame_dropper_(new FrameDropper),
84 loss_prot_logic_(
85 new VCMLossProtectionLogic(clock_->TimeInMilliseconds())),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000086 fraction_lost_(0),
87 send_statistics_zero_encode_(0),
88 max_payload_size_(1460),
pbos73674632015-10-29 15:45:00 -070089 video_target_bitrate_(0),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000090 incoming_frame_rate_(0),
91 enable_qm_(false),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000092 encoded_frame_samples_(),
93 avg_sent_bit_rate_bps_(0),
94 avg_sent_framerate_(0),
95 key_frame_cnt_(0),
96 delta_frame_cnt_(0),
henrik.lundin@webrtc.orgb426c462013-09-24 07:41:53 +000097 content_(new VCMContentMetricsProcessing()),
98 qm_resolution_(new VCMQmResolution()),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000099 last_qm_update_time_(0),
100 last_change_time_(0),
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000101 num_layers_(0),
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000102 suspension_enabled_(false),
103 video_suspended_(false),
104 suspension_threshold_bps_(0),
105 suspension_window_bps_(0) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000106 memset(send_statistics_, 0, sizeof(send_statistics_));
107 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000108}
109
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000110MediaOptimization::~MediaOptimization(void) {
111 loss_prot_logic_->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +0000112}
113
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000114void MediaOptimization::Reset() {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000115 CriticalSectionScoped lock(crit_sect_.get());
philipel9d3ab612015-12-21 04:12:39 -0800116 SetEncodingDataInternal(kVideoCodecUnknown, 0, 0, 0, 0, 0, 0,
117 max_payload_size_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000118 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
119 incoming_frame_rate_ = 0.0;
120 frame_dropper_->Reset();
121 loss_prot_logic_->Reset(clock_->TimeInMilliseconds());
122 frame_dropper_->SetRates(0, 0);
123 content_->Reset();
124 qm_resolution_->Reset();
125 loss_prot_logic_->UpdateFrameRate(incoming_frame_rate_);
126 loss_prot_logic_->Reset(clock_->TimeInMilliseconds());
127 send_statistics_zero_encode_ = 0;
pbos73674632015-10-29 15:45:00 -0700128 video_target_bitrate_ = 0;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000129 codec_width_ = 0;
130 codec_height_ = 0;
131 user_frame_rate_ = 0;
132 key_frame_cnt_ = 0;
133 delta_frame_cnt_ = 0;
134 last_qm_update_time_ = 0;
135 last_change_time_ = 0;
136 encoded_frame_samples_.clear();
137 avg_sent_bit_rate_bps_ = 0;
138 num_layers_ = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000139}
140
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000141void MediaOptimization::SetEncodingData(VideoCodecType send_codec_type,
142 int32_t max_bit_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000143 uint32_t target_bitrate,
144 uint16_t width,
145 uint16_t height,
Peter Boströmdf664532015-05-12 12:22:14 +0200146 uint32_t frame_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000147 int num_layers,
148 int32_t mtu) {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000149 CriticalSectionScoped lock(crit_sect_.get());
philipel9d3ab612015-12-21 04:12:39 -0800150 SetEncodingDataInternal(send_codec_type, max_bit_rate, frame_rate,
151 target_bitrate, width, height, num_layers, mtu);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000152}
153
154void MediaOptimization::SetEncodingDataInternal(VideoCodecType send_codec_type,
155 int32_t max_bit_rate,
156 uint32_t frame_rate,
157 uint32_t target_bitrate,
158 uint16_t width,
159 uint16_t height,
160 int num_layers,
161 int32_t mtu) {
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000162 // Everything codec specific should be reset here since this means the codec
163 // has changed. If native dimension values have changed, then either user
164 // initiated change, or QM initiated change. Will be able to determine only
165 // after the processing of the first frame.
166 last_change_time_ = clock_->TimeInMilliseconds();
167 content_->Reset();
168 content_->UpdateFrameRate(frame_rate);
169
170 max_bit_rate_ = max_bit_rate;
171 send_codec_type_ = send_codec_type;
pbos73674632015-10-29 15:45:00 -0700172 video_target_bitrate_ = target_bitrate;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000173 float target_bitrate_kbps = static_cast<float>(target_bitrate) / 1000.0f;
174 loss_prot_logic_->UpdateBitRate(target_bitrate_kbps);
175 loss_prot_logic_->UpdateFrameRate(static_cast<float>(frame_rate));
176 loss_prot_logic_->UpdateFrameSize(width, height);
177 loss_prot_logic_->UpdateNumLayers(num_layers);
178 frame_dropper_->Reset();
179 frame_dropper_->SetRates(target_bitrate_kbps, static_cast<float>(frame_rate));
180 user_frame_rate_ = static_cast<float>(frame_rate);
181 codec_width_ = width;
182 codec_height_ = height;
183 num_layers_ = (num_layers <= 1) ? 1 : num_layers; // Can also be zero.
184 max_payload_size_ = mtu;
philipel9d3ab612015-12-21 04:12:39 -0800185 qm_resolution_->Initialize(target_bitrate_kbps, user_frame_rate_,
186 codec_width_, codec_height_, num_layers_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000187}
188
189uint32_t MediaOptimization::SetTargetRates(
190 uint32_t target_bitrate,
191 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000192 int64_t round_trip_time_ms,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000193 VCMProtectionCallback* protection_callback,
194 VCMQMSettingsCallback* qmsettings_callback) {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000195 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000196 VCMProtectionMethod* selected_method = loss_prot_logic_->SelectedMethod();
197 float target_bitrate_kbps = static_cast<float>(target_bitrate) / 1000.0f;
198 loss_prot_logic_->UpdateBitRate(target_bitrate_kbps);
199 loss_prot_logic_->UpdateRtt(round_trip_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000200
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000201 // Get frame rate for encoder: this is the actual/sent frame rate.
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000202 float actual_frame_rate = SentFrameRateInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000203
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000204 // Sanity check.
205 if (actual_frame_rate < 1.0) {
206 actual_frame_rate = 1.0;
207 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000208
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000209 // Update frame rate for the loss protection logic class: frame rate should
210 // be the actual/sent rate.
211 loss_prot_logic_->UpdateFrameRate(actual_frame_rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000212
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000213 fraction_lost_ = fraction_lost;
niklase@google.com470e71d2011-07-07 08:21:25 +0000214
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000215 // Returns the filtered packet loss, used for the protection setting.
216 // The filtered loss may be the received loss (no filter), or some
217 // filtered value (average or max window filter).
218 // Use max window filter for now.
219 FilterPacketLossMode filter_mode = kMaxFilter;
220 uint8_t packet_loss_enc = loss_prot_logic_->FilteredLoss(
221 clock_->TimeInMilliseconds(), filter_mode, fraction_lost);
niklase@google.com470e71d2011-07-07 08:21:25 +0000222
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000223 // For now use the filtered loss for computing the robustness settings.
224 loss_prot_logic_->UpdateFilteredLossPr(packet_loss_enc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000225
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000226 // Rate cost of the protection methods.
pbos73674632015-10-29 15:45:00 -0700227 float protection_overhead_rate = 0.0f;
niklase@google.com470e71d2011-07-07 08:21:25 +0000228
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000229 // Update protection settings, when applicable.
230 float sent_video_rate_kbps = 0.0f;
mflodmanfcf54bd2015-04-14 21:28:08 +0200231 if (loss_prot_logic_->SelectedType() != kNone) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000232 // Update protection method with content metrics.
233 selected_method->UpdateContentMetrics(content_->ShortTermAvgData());
marpan@google.com86548c62011-07-12 17:12:57 +0000234
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000235 // Update method will compute the robustness settings for the given
236 // protection method and the overhead cost
237 // the protection method is set by the user via SetVideoProtection.
238 loss_prot_logic_->UpdateMethod();
niklase@google.com470e71d2011-07-07 08:21:25 +0000239
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000240 // Update protection callback with protection settings.
241 uint32_t sent_video_rate_bps = 0;
242 uint32_t sent_nack_rate_bps = 0;
243 uint32_t sent_fec_rate_bps = 0;
244 // Get the bit cost of protection method, based on the amount of
245 // overhead data actually transmitted (including headers) the last
246 // second.
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000247 if (protection_callback) {
philipel9d3ab612015-12-21 04:12:39 -0800248 UpdateProtectionCallback(selected_method, &sent_video_rate_bps,
249 &sent_nack_rate_bps, &sent_fec_rate_bps,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000250 protection_callback);
251 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000252 uint32_t sent_total_rate_bps =
253 sent_video_rate_bps + sent_nack_rate_bps + sent_fec_rate_bps;
254 // Estimate the overhead costs of the next second as staying the same
255 // wrt the source bitrate.
256 if (sent_total_rate_bps > 0) {
pbos73674632015-10-29 15:45:00 -0700257 protection_overhead_rate =
258 static_cast<float>(sent_nack_rate_bps + sent_fec_rate_bps) /
259 sent_total_rate_bps;
niklase@google.com470e71d2011-07-07 08:21:25 +0000260 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000261 // Cap the overhead estimate to 50%.
pbos73674632015-10-29 15:45:00 -0700262 if (protection_overhead_rate > 0.5)
263 protection_overhead_rate = 0.5;
niklase@google.com470e71d2011-07-07 08:21:25 +0000264
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000265 // Get the effective packet loss for encoder ER when applicable. Should be
266 // passed to encoder via fraction_lost.
267 packet_loss_enc = selected_method->RequiredPacketLossER();
268 sent_video_rate_kbps = static_cast<float>(sent_video_rate_bps) / 1000.0f;
269 }
stefan@webrtc.orgf4c82862011-12-13 15:38:14 +0000270
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000271 // Source coding rate: total rate - protection overhead.
pbos73674632015-10-29 15:45:00 -0700272 video_target_bitrate_ = target_bitrate * (1.0 - protection_overhead_rate);
273
274 // Cap target video bitrate to codec maximum.
275 if (max_bit_rate_ > 0 && video_target_bitrate_ > max_bit_rate_) {
276 video_target_bitrate_ = max_bit_rate_;
277 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000278
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000279 // Update encoding rates following protection settings.
280 float target_video_bitrate_kbps =
pbos73674632015-10-29 15:45:00 -0700281 static_cast<float>(video_target_bitrate_) / 1000.0f;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000282 frame_dropper_->SetRates(target_video_bitrate_kbps, incoming_frame_rate_);
283
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000284 if (enable_qm_ && qmsettings_callback) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000285 // Update QM with rates.
philipel9d3ab612015-12-21 04:12:39 -0800286 qm_resolution_->UpdateRates(target_video_bitrate_kbps, sent_video_rate_kbps,
287 incoming_frame_rate_, fraction_lost_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000288 // Check for QM selection.
289 bool select_qm = CheckStatusForQMchange();
290 if (select_qm) {
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000291 SelectQuality(qmsettings_callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000292 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000293 // Reset the short-term averaged content data.
294 content_->ResetShortTermAvgData();
295 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000296
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000297 CheckSuspendConditions();
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000298
pbos73674632015-10-29 15:45:00 -0700299 return video_target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000300}
301
pbosba8c15b2015-07-14 09:36:34 -0700302void MediaOptimization::SetProtectionMethod(VCMProtectionMethodEnum method) {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000303 CriticalSectionScoped lock(crit_sect_.get());
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000304 loss_prot_logic_->SetMethod(method);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000305}
306
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000307uint32_t MediaOptimization::InputFrameRate() {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000308 CriticalSectionScoped lock(crit_sect_.get());
309 return InputFrameRateInternal();
310}
311
312uint32_t MediaOptimization::InputFrameRateInternal() {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000313 ProcessIncomingFrameRate(clock_->TimeInMilliseconds());
314 return uint32_t(incoming_frame_rate_ + 0.5f);
315}
316
317uint32_t MediaOptimization::SentFrameRate() {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000318 CriticalSectionScoped lock(crit_sect_.get());
319 return SentFrameRateInternal();
320}
321
322uint32_t MediaOptimization::SentFrameRateInternal() {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000323 PurgeOldFrameSamples(clock_->TimeInMilliseconds());
324 UpdateSentFramerate();
325 return avg_sent_framerate_;
326}
327
328uint32_t MediaOptimization::SentBitRate() {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000329 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000330 const int64_t now_ms = clock_->TimeInMilliseconds();
331 PurgeOldFrameSamples(now_ms);
332 UpdateSentBitrate(now_ms);
333 return avg_sent_bit_rate_bps_;
334}
335
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000336int32_t MediaOptimization::UpdateWithEncodedData(
337 const EncodedImage& encoded_image) {
338 size_t encoded_length = encoded_image._length;
339 uint32_t timestamp = encoded_image._timeStamp;
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000340 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000341 const int64_t now_ms = clock_->TimeInMilliseconds();
342 PurgeOldFrameSamples(now_ms);
343 if (encoded_frame_samples_.size() > 0 &&
344 encoded_frame_samples_.back().timestamp == timestamp) {
345 // Frames having the same timestamp are generated from the same input
346 // frame. We don't want to double count them, but only increment the
347 // size_bytes.
348 encoded_frame_samples_.back().size_bytes += encoded_length;
349 encoded_frame_samples_.back().time_complete_ms = now_ms;
350 } else {
351 encoded_frame_samples_.push_back(
352 EncodedFrameSample(encoded_length, timestamp, now_ms));
353 }
354 UpdateSentBitrate(now_ms);
355 UpdateSentFramerate();
356 if (encoded_length > 0) {
Peter Boström49e196a2015-10-23 15:58:18 +0200357 const bool delta_frame = encoded_image._frameType != kVideoFrameKey;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000358
359 frame_dropper_->Fill(encoded_length, delta_frame);
360 if (max_payload_size_ > 0 && encoded_length > 0) {
361 const float min_packets_per_frame =
362 encoded_length / static_cast<float>(max_payload_size_);
363 if (delta_frame) {
364 loss_prot_logic_->UpdatePacketsPerFrame(min_packets_per_frame,
365 clock_->TimeInMilliseconds());
366 } else {
367 loss_prot_logic_->UpdatePacketsPerFrameKey(
368 min_packets_per_frame, clock_->TimeInMilliseconds());
369 }
370
371 if (enable_qm_) {
372 // Update quality select with encoded length.
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000373 qm_resolution_->UpdateEncodedSize(encoded_length);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000374 }
375 }
376 if (!delta_frame && encoded_length > 0) {
377 loss_prot_logic_->UpdateKeyFrameSize(static_cast<float>(encoded_length));
378 }
379
380 // Updating counters.
381 if (delta_frame) {
382 delta_frame_cnt_++;
383 } else {
384 key_frame_cnt_++;
385 }
386 }
387
388 return VCM_OK;
389}
390
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000391void MediaOptimization::EnableQM(bool enable) {
392 CriticalSectionScoped lock(crit_sect_.get());
393 enable_qm_ = enable;
394}
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000395
396void MediaOptimization::EnableFrameDropper(bool enable) {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000397 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000398 frame_dropper_->Enable(enable);
399}
400
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000401void MediaOptimization::SuspendBelowMinBitrate(int threshold_bps,
402 int window_bps) {
403 CriticalSectionScoped lock(crit_sect_.get());
404 assert(threshold_bps > 0 && window_bps >= 0);
405 suspension_threshold_bps_ = threshold_bps;
406 suspension_window_bps_ = window_bps;
407 suspension_enabled_ = true;
408 video_suspended_ = false;
409}
410
411bool MediaOptimization::IsVideoSuspended() const {
412 CriticalSectionScoped lock(crit_sect_.get());
413 return video_suspended_;
414}
415
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000416bool MediaOptimization::DropFrame() {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000417 CriticalSectionScoped lock(crit_sect_.get());
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000418 UpdateIncomingFrameRate();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000419 // Leak appropriate number of bytes.
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000420 frame_dropper_->Leak((uint32_t)(InputFrameRateInternal() + 0.5f));
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000421 if (video_suspended_) {
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000422 return true; // Drop all frames when muted.
423 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000424 return frame_dropper_->DropFrame();
425}
426
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000427void MediaOptimization::UpdateContentData(
428 const VideoContentMetrics* content_metrics) {
429 CriticalSectionScoped lock(crit_sect_.get());
430 // Updating content metrics.
431 if (content_metrics == NULL) {
432 // Disable QM if metrics are NULL.
433 enable_qm_ = false;
434 qm_resolution_->Reset();
435 } else {
436 content_->UpdateContentData(content_metrics);
437 }
438}
439
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000440void MediaOptimization::UpdateIncomingFrameRate() {
441 int64_t now = clock_->TimeInMilliseconds();
442 if (incoming_frame_times_[0] == 0) {
443 // No shifting if this is the first time.
444 } else {
445 // Shift all times one step.
446 for (int32_t i = (kFrameCountHistorySize - 2); i >= 0; i--) {
447 incoming_frame_times_[i + 1] = incoming_frame_times_[i];
448 }
449 }
450 incoming_frame_times_[0] = now;
451 ProcessIncomingFrameRate(now);
452}
453
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000454int32_t MediaOptimization::SelectQuality(
455 VCMQMSettingsCallback* video_qmsettings_callback) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000456 // Reset quantities for QM select.
457 qm_resolution_->ResetQM();
458
459 // Update QM will long-term averaged content metrics.
460 qm_resolution_->UpdateContent(content_->LongTermAvgData());
461
462 // Select quality mode.
463 VCMResolutionScale* qm = NULL;
464 int32_t ret = qm_resolution_->SelectResolution(&qm);
465 if (ret < 0) {
466 return ret;
467 }
468
469 // Check for updates to spatial/temporal modes.
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000470 QMUpdate(qm, video_qmsettings_callback);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000471
472 // Reset all the rate and related frame counters quantities.
473 qm_resolution_->ResetRates();
474
475 // Reset counters.
476 last_qm_update_time_ = clock_->TimeInMilliseconds();
477
478 // Reset content metrics.
479 content_->Reset();
480
481 return VCM_OK;
482}
483
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000484void MediaOptimization::PurgeOldFrameSamples(int64_t now_ms) {
485 while (!encoded_frame_samples_.empty()) {
486 if (now_ms - encoded_frame_samples_.front().time_complete_ms >
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000487 kBitrateAverageWinMs) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000488 encoded_frame_samples_.pop_front();
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000489 } else {
490 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000491 }
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000492 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000493}
494
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000495void MediaOptimization::UpdateSentBitrate(int64_t now_ms) {
496 if (encoded_frame_samples_.empty()) {
497 avg_sent_bit_rate_bps_ = 0;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000498 return;
499 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000500 size_t framesize_sum = 0;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000501 for (FrameSampleList::iterator it = encoded_frame_samples_.begin();
philipel9d3ab612015-12-21 04:12:39 -0800502 it != encoded_frame_samples_.end(); ++it) {
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000503 framesize_sum += it->size_bytes;
504 }
505 float denom = static_cast<float>(
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000506 now_ms - encoded_frame_samples_.front().time_complete_ms);
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000507 if (denom >= 1.0f) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000508 avg_sent_bit_rate_bps_ =
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000509 static_cast<uint32_t>(framesize_sum * 8.0f * 1000.0f / denom + 0.5f);
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000510 } else {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000511 avg_sent_bit_rate_bps_ = framesize_sum * 8;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000512 }
513}
514
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000515void MediaOptimization::UpdateSentFramerate() {
516 if (encoded_frame_samples_.size() <= 1) {
517 avg_sent_framerate_ = encoded_frame_samples_.size();
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000518 return;
519 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000520 int denom = encoded_frame_samples_.back().timestamp -
521 encoded_frame_samples_.front().timestamp;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000522 if (denom > 0) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000523 avg_sent_framerate_ =
524 (90000 * (encoded_frame_samples_.size() - 1) + denom / 2) / denom;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000525 } else {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000526 avg_sent_framerate_ = encoded_frame_samples_.size();
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000527 }
528}
niklase@google.com470e71d2011-07-07 08:21:25 +0000529
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000530bool MediaOptimization::QMUpdate(
531 VCMResolutionScale* qm,
532 VCMQMSettingsCallback* video_qmsettings_callback) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000533 // Check for no change.
marpan@webrtc.orge22d81c2012-03-20 18:21:53 +0000534 if (!qm->change_resolution_spatial && !qm->change_resolution_temporal) {
marpan@webrtc.orgaccf6072012-03-07 17:16:10 +0000535 return false;
536 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000537
marpan@webrtc.orgaccf6072012-03-07 17:16:10 +0000538 // Check for change in frame rate.
marpan@webrtc.orge22d81c2012-03-20 18:21:53 +0000539 if (qm->change_resolution_temporal) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000540 incoming_frame_rate_ = qm->frame_rate;
marpan@webrtc.orge22d81c2012-03-20 18:21:53 +0000541 // Reset frame rate estimate.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000542 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
marpan@webrtc.orgaccf6072012-03-07 17:16:10 +0000543 }
marpan@google.com86548c62011-07-12 17:12:57 +0000544
marpan@webrtc.orgaccf6072012-03-07 17:16:10 +0000545 // Check for change in frame size.
marpan@webrtc.orge22d81c2012-03-20 18:21:53 +0000546 if (qm->change_resolution_spatial) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000547 codec_width_ = qm->codec_width;
548 codec_height_ = qm->codec_height;
marpan@webrtc.orgaccf6072012-03-07 17:16:10 +0000549 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000550
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000551 LOG(LS_INFO) << "Media optimizer requests the video resolution to be changed "
philipel9d3ab612015-12-21 04:12:39 -0800552 "to "
553 << qm->codec_width << "x" << qm->codec_height << "@"
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000554 << qm->frame_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000555
marpan@webrtc.orge22d81c2012-03-20 18:21:53 +0000556 // Update VPM with new target frame rate and frame size.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000557 // Note: use |qm->frame_rate| instead of |_incoming_frame_rate| for updating
558 // target frame rate in VPM frame dropper. The quantity |_incoming_frame_rate|
marpan@webrtc.orge22d81c2012-03-20 18:21:53 +0000559 // will vary/fluctuate, and since we don't want to change the state of the
560 // VPM frame dropper, unless a temporal action was selected, we use the
561 // quantity |qm->frame_rate| for updating.
philipel9d3ab612015-12-21 04:12:39 -0800562 video_qmsettings_callback->SetVideoQMSettings(qm->frame_rate, codec_width_,
563 codec_height_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000564 content_->UpdateFrameRate(qm->frame_rate);
philipel9d3ab612015-12-21 04:12:39 -0800565 qm_resolution_->UpdateCodecParameters(qm->frame_rate, codec_width_,
566 codec_height_);
marpan@webrtc.orgaccf6072012-03-07 17:16:10 +0000567 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000568}
569
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000570// Check timing constraints and look for significant change in:
571// (1) scene content,
572// (2) target bit rate.
573bool MediaOptimization::CheckStatusForQMchange() {
574 bool status = true;
575
576 // Check that we do not call QMSelect too often, and that we waited some time
577 // (to sample the metrics) from the event last_change_time
578 // last_change_time is the time where user changed the size/rate/frame rate
579 // (via SetEncodingData).
580 int64_t now = clock_->TimeInMilliseconds();
581 if ((now - last_qm_update_time_) < kQmMinIntervalMs ||
582 (now - last_change_time_) < kQmMinIntervalMs) {
583 status = false;
584 }
585
586 return status;
niklase@google.com470e71d2011-07-07 08:21:25 +0000587}
588
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000589// Allowing VCM to keep track of incoming frame rate.
590void MediaOptimization::ProcessIncomingFrameRate(int64_t now) {
591 int32_t num = 0;
592 int32_t nr_of_frames = 0;
593 for (num = 1; num < (kFrameCountHistorySize - 1); ++num) {
594 if (incoming_frame_times_[num] <= 0 ||
595 // don't use data older than 2 s
596 now - incoming_frame_times_[num] > kFrameHistoryWinMs) {
597 break;
598 } else {
599 nr_of_frames++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000600 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000601 }
602 if (num > 1) {
Erik Språng66a641a2015-06-11 14:20:07 +0200603 const int64_t diff =
604 incoming_frame_times_[0] - incoming_frame_times_[num - 1];
605 incoming_frame_rate_ = 0.0; // No frame rate estimate available.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000606 if (diff > 0) {
607 incoming_frame_rate_ = nr_of_frames * 1000.0f / static_cast<float>(diff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000608 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000609 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000610}
611
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000612void MediaOptimization::CheckSuspendConditions() {
pbos73674632015-10-29 15:45:00 -0700613 // Check conditions for SuspendBelowMinBitrate. |video_target_bitrate_| is in
614 // bps.
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000615 if (suspension_enabled_) {
616 if (!video_suspended_) {
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000617 // Check if we just went below the threshold.
pbos73674632015-10-29 15:45:00 -0700618 if (video_target_bitrate_ < suspension_threshold_bps_) {
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000619 video_suspended_ = true;
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000620 }
621 } else {
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000622 // Video is already suspended. Check if we just went over the threshold
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000623 // with a margin.
pbos73674632015-10-29 15:45:00 -0700624 if (video_target_bitrate_ >
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000625 suspension_threshold_bps_ + suspension_window_bps_) {
626 video_suspended_ = false;
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000627 }
628 }
629 }
630}
631
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000632} // namespace media_optimization
633} // namespace webrtc