blob: f24637e0b29408f40914ba0013b201fe765ecf67 [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
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000036 // 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.
philipel9d3ab612015-12-21 04:12:39 -080049 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.orge682aa52013-12-19 10:59:48 +000052}
53} // namespace
54
55struct MediaOptimization::EncodedFrameSample {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000056 EncodedFrameSample(size_t size_bytes,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000057 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.org4591fbd2014-11-20 22:28:14 +000063 size_t size_bytes;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000064 uint32_t timestamp;
65 int64_t time_complete_ms;
66};
niklase@google.com470e71d2011-07-07 08:21:25 +000067
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000068MediaOptimization::MediaOptimization(Clock* clock)
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000069 : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
70 clock_(clock),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000071 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.orgb426c462013-09-24 07:41:53 +000076 frame_dropper_(new FrameDropper),
77 loss_prot_logic_(
78 new VCMLossProtectionLogic(clock_->TimeInMilliseconds())),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000079 fraction_lost_(0),
80 send_statistics_zero_encode_(0),
81 max_payload_size_(1460),
pbos73674632015-10-29 15:45:00 -070082 video_target_bitrate_(0),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000083 incoming_frame_rate_(0),
84 enable_qm_(false),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000085 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.orgb426c462013-09-24 07:41:53 +000090 content_(new VCMContentMetricsProcessing()),
91 qm_resolution_(new VCMQmResolution()),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000092 last_qm_update_time_(0),
93 last_change_time_(0),
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +000094 num_layers_(0),
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +000095 suspension_enabled_(false),
96 video_suspended_(false),
97 suspension_threshold_bps_(0),
98 suspension_window_bps_(0) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000099 memset(send_statistics_, 0, sizeof(send_statistics_));
100 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000101}
102
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000103MediaOptimization::~MediaOptimization(void) {
104 loss_prot_logic_->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +0000105}
106
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000107void MediaOptimization::Reset() {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000108 CriticalSectionScoped lock(crit_sect_.get());
philipel9d3ab612015-12-21 04:12:39 -0800109 SetEncodingDataInternal(kVideoCodecUnknown, 0, 0, 0, 0, 0, 0,
110 max_payload_size_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000111 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;
pbos73674632015-10-29 15:45:00 -0700121 video_target_bitrate_ = 0;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000122 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.com470e71d2011-07-07 08:21:25 +0000132}
133
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000134void MediaOptimization::SetEncodingData(VideoCodecType send_codec_type,
135 int32_t max_bit_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000136 uint32_t target_bitrate,
137 uint16_t width,
138 uint16_t height,
Peter Boströmdf664532015-05-12 12:22:14 +0200139 uint32_t frame_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000140 int num_layers,
141 int32_t mtu) {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000142 CriticalSectionScoped lock(crit_sect_.get());
philipel9d3ab612015-12-21 04:12:39 -0800143 SetEncodingDataInternal(send_codec_type, max_bit_rate, frame_rate,
144 target_bitrate, width, height, num_layers, mtu);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000145}
146
147void 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.orge682aa52013-12-19 10:59:48 +0000155 // 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;
pbos73674632015-10-29 15:45:00 -0700165 video_target_bitrate_ = target_bitrate;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000166 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;
philipel9d3ab612015-12-21 04:12:39 -0800178 qm_resolution_->Initialize(target_bitrate_kbps, user_frame_rate_,
179 codec_width_, codec_height_, num_layers_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000180}
181
182uint32_t MediaOptimization::SetTargetRates(
183 uint32_t target_bitrate,
184 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000185 int64_t round_trip_time_ms,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000186 VCMProtectionCallback* protection_callback,
187 VCMQMSettingsCallback* qmsettings_callback) {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000188 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000189 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.com470e71d2011-07-07 08:21:25 +0000193
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000194 // Get frame rate for encoder: this is the actual/sent frame rate.
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000195 float actual_frame_rate = SentFrameRateInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000196
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000197 // Sanity check.
198 if (actual_frame_rate < 1.0) {
199 actual_frame_rate = 1.0;
200 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000201
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000202 // 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.com470e71d2011-07-07 08:21:25 +0000205
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000206 fraction_lost_ = fraction_lost;
niklase@google.com470e71d2011-07-07 08:21:25 +0000207
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000208 // 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.com470e71d2011-07-07 08:21:25 +0000215
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000216 // For now use the filtered loss for computing the robustness settings.
217 loss_prot_logic_->UpdateFilteredLossPr(packet_loss_enc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000218
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000219 // Rate cost of the protection methods.
pbos73674632015-10-29 15:45:00 -0700220 float protection_overhead_rate = 0.0f;
niklase@google.com470e71d2011-07-07 08:21:25 +0000221
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000222 // Update protection settings, when applicable.
223 float sent_video_rate_kbps = 0.0f;
mflodmanfcf54bd2015-04-14 21:28:08 +0200224 if (loss_prot_logic_->SelectedType() != kNone) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000225 // 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.com470e71d2011-07-07 08:21:25 +0000229
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000230 // 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.orge682aa52013-12-19 10:59:48 +0000237 if (protection_callback) {
philipel9d3ab612015-12-21 04:12:39 -0800238 UpdateProtectionCallback(selected_method, &sent_video_rate_bps,
239 &sent_nack_rate_bps, &sent_fec_rate_bps,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000240 protection_callback);
241 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000242 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) {
pbos73674632015-10-29 15:45:00 -0700247 protection_overhead_rate =
248 static_cast<float>(sent_nack_rate_bps + sent_fec_rate_bps) /
249 sent_total_rate_bps;
niklase@google.com470e71d2011-07-07 08:21:25 +0000250 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000251 // Cap the overhead estimate to 50%.
pbos73674632015-10-29 15:45:00 -0700252 if (protection_overhead_rate > 0.5)
253 protection_overhead_rate = 0.5;
niklase@google.com470e71d2011-07-07 08:21:25 +0000254
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000255 // 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.orgf4c82862011-12-13 15:38:14 +0000260
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000261 // Source coding rate: total rate - protection overhead.
pbos73674632015-10-29 15:45:00 -0700262 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.com470e71d2011-07-07 08:21:25 +0000268
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000269 // Update encoding rates following protection settings.
270 float target_video_bitrate_kbps =
pbos73674632015-10-29 15:45:00 -0700271 static_cast<float>(video_target_bitrate_) / 1000.0f;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000272 frame_dropper_->SetRates(target_video_bitrate_kbps, incoming_frame_rate_);
273
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000274 if (enable_qm_ && qmsettings_callback) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000275 // Update QM with rates.
philipel9d3ab612015-12-21 04:12:39 -0800276 qm_resolution_->UpdateRates(target_video_bitrate_kbps, sent_video_rate_kbps,
277 incoming_frame_rate_, fraction_lost_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000278 // Check for QM selection.
279 bool select_qm = CheckStatusForQMchange();
280 if (select_qm) {
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000281 SelectQuality(qmsettings_callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000282 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000283 // Reset the short-term averaged content data.
284 content_->ResetShortTermAvgData();
285 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000286
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000287 CheckSuspendConditions();
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000288
pbos73674632015-10-29 15:45:00 -0700289 return video_target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000290}
291
pbosba8c15b2015-07-14 09:36:34 -0700292void MediaOptimization::SetProtectionMethod(VCMProtectionMethodEnum method) {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000293 CriticalSectionScoped lock(crit_sect_.get());
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000294 loss_prot_logic_->SetMethod(method);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000295}
296
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000297uint32_t MediaOptimization::InputFrameRate() {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000298 CriticalSectionScoped lock(crit_sect_.get());
299 return InputFrameRateInternal();
300}
301
302uint32_t MediaOptimization::InputFrameRateInternal() {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000303 ProcessIncomingFrameRate(clock_->TimeInMilliseconds());
304 return uint32_t(incoming_frame_rate_ + 0.5f);
305}
306
307uint32_t MediaOptimization::SentFrameRate() {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000308 CriticalSectionScoped lock(crit_sect_.get());
309 return SentFrameRateInternal();
310}
311
312uint32_t MediaOptimization::SentFrameRateInternal() {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000313 PurgeOldFrameSamples(clock_->TimeInMilliseconds());
314 UpdateSentFramerate();
315 return avg_sent_framerate_;
316}
317
318uint32_t MediaOptimization::SentBitRate() {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000319 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000320 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.org273a4142014-12-01 15:23:21 +0000326int32_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.orgae7cfd72014-06-30 08:01:47 +0000330 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000331 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öm49e196a2015-10-23 15:58:18 +0200347 const bool delta_frame = encoded_image._frameType != kVideoFrameKey;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000348
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.org273a4142014-12-01 15:23:21 +0000363 qm_resolution_->UpdateEncodedSize(encoded_length);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000364 }
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.orgae7cfd72014-06-30 08:01:47 +0000381void MediaOptimization::EnableQM(bool enable) {
382 CriticalSectionScoped lock(crit_sect_.get());
383 enable_qm_ = enable;
384}
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000385
386void MediaOptimization::EnableFrameDropper(bool enable) {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000387 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000388 frame_dropper_->Enable(enable);
389}
390
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000391void 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
401bool MediaOptimization::IsVideoSuspended() const {
402 CriticalSectionScoped lock(crit_sect_.get());
403 return video_suspended_;
404}
405
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000406bool MediaOptimization::DropFrame() {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000407 CriticalSectionScoped lock(crit_sect_.get());
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000408 UpdateIncomingFrameRate();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000409 // Leak appropriate number of bytes.
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000410 frame_dropper_->Leak((uint32_t)(InputFrameRateInternal() + 0.5f));
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000411 if (video_suspended_) {
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000412 return true; // Drop all frames when muted.
413 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000414 return frame_dropper_->DropFrame();
415}
416
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000417void 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.orgbec11ef2013-09-23 19:54:25 +0000430void 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.orge682aa52013-12-19 10:59:48 +0000444int32_t MediaOptimization::SelectQuality(
445 VCMQMSettingsCallback* video_qmsettings_callback) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000446 // 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.orge682aa52013-12-19 10:59:48 +0000460 QMUpdate(qm, video_qmsettings_callback);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000461
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.orgbec11ef2013-09-23 19:54:25 +0000474void 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.orgf4944d42013-03-18 17:04:52 +0000477 kBitrateAverageWinMs) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000478 encoded_frame_samples_.pop_front();
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000479 } else {
480 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000481 }
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000482 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000483}
484
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000485void MediaOptimization::UpdateSentBitrate(int64_t now_ms) {
486 if (encoded_frame_samples_.empty()) {
487 avg_sent_bit_rate_bps_ = 0;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000488 return;
489 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000490 size_t framesize_sum = 0;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000491 for (FrameSampleList::iterator it = encoded_frame_samples_.begin();
philipel9d3ab612015-12-21 04:12:39 -0800492 it != encoded_frame_samples_.end(); ++it) {
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000493 framesize_sum += it->size_bytes;
494 }
495 float denom = static_cast<float>(
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000496 now_ms - encoded_frame_samples_.front().time_complete_ms);
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000497 if (denom >= 1.0f) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000498 avg_sent_bit_rate_bps_ =
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000499 static_cast<uint32_t>(framesize_sum * 8.0f * 1000.0f / denom + 0.5f);
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000500 } else {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000501 avg_sent_bit_rate_bps_ = framesize_sum * 8;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000502 }
503}
504
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000505void MediaOptimization::UpdateSentFramerate() {
506 if (encoded_frame_samples_.size() <= 1) {
507 avg_sent_framerate_ = encoded_frame_samples_.size();
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000508 return;
509 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000510 int denom = encoded_frame_samples_.back().timestamp -
511 encoded_frame_samples_.front().timestamp;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000512 if (denom > 0) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000513 avg_sent_framerate_ =
514 (90000 * (encoded_frame_samples_.size() - 1) + denom / 2) / denom;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000515 } else {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000516 avg_sent_framerate_ = encoded_frame_samples_.size();
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000517 }
518}
niklase@google.com470e71d2011-07-07 08:21:25 +0000519
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000520bool MediaOptimization::QMUpdate(
521 VCMResolutionScale* qm,
522 VCMQMSettingsCallback* video_qmsettings_callback) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000523 // Check for no change.
marpan@webrtc.orge22d81c2012-03-20 18:21:53 +0000524 if (!qm->change_resolution_spatial && !qm->change_resolution_temporal) {
marpan@webrtc.orgaccf6072012-03-07 17:16:10 +0000525 return false;
526 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000527
marpan@webrtc.orgaccf6072012-03-07 17:16:10 +0000528 // Check for change in frame rate.
marpan@webrtc.orge22d81c2012-03-20 18:21:53 +0000529 if (qm->change_resolution_temporal) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000530 incoming_frame_rate_ = qm->frame_rate;
marpan@webrtc.orge22d81c2012-03-20 18:21:53 +0000531 // Reset frame rate estimate.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000532 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
marpan@webrtc.orgaccf6072012-03-07 17:16:10 +0000533 }
marpan@google.com86548c62011-07-12 17:12:57 +0000534
marpan@webrtc.orgaccf6072012-03-07 17:16:10 +0000535 // Check for change in frame size.
marpan@webrtc.orge22d81c2012-03-20 18:21:53 +0000536 if (qm->change_resolution_spatial) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000537 codec_width_ = qm->codec_width;
538 codec_height_ = qm->codec_height;
marpan@webrtc.orgaccf6072012-03-07 17:16:10 +0000539 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000540
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000541 LOG(LS_INFO) << "Media optimizer requests the video resolution to be changed "
philipel9d3ab612015-12-21 04:12:39 -0800542 "to "
543 << qm->codec_width << "x" << qm->codec_height << "@"
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000544 << qm->frame_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000545
marpan@webrtc.orge22d81c2012-03-20 18:21:53 +0000546 // Update VPM with new target frame rate and frame size.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000547 // 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.orge22d81c2012-03-20 18:21:53 +0000549 // 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.
philipel9d3ab612015-12-21 04:12:39 -0800552 video_qmsettings_callback->SetVideoQMSettings(qm->frame_rate, codec_width_,
553 codec_height_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000554 content_->UpdateFrameRate(qm->frame_rate);
philipel9d3ab612015-12-21 04:12:39 -0800555 qm_resolution_->UpdateCodecParameters(qm->frame_rate, codec_width_,
556 codec_height_);
marpan@webrtc.orgaccf6072012-03-07 17:16:10 +0000557 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000558}
559
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000560// Check timing constraints and look for significant change in:
561// (1) scene content,
562// (2) target bit rate.
563bool 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.com470e71d2011-07-07 08:21:25 +0000577}
578
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000579// Allowing VCM to keep track of incoming frame rate.
580void 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.com470e71d2011-07-07 08:21:25 +0000590 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000591 }
592 if (num > 1) {
Erik Språng66a641a2015-06-11 14:20:07 +0200593 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.orgbec11ef2013-09-23 19:54:25 +0000596 if (diff > 0) {
597 incoming_frame_rate_ = nr_of_frames * 1000.0f / static_cast<float>(diff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000598 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000599 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000600}
601
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000602void MediaOptimization::CheckSuspendConditions() {
pbos73674632015-10-29 15:45:00 -0700603 // Check conditions for SuspendBelowMinBitrate. |video_target_bitrate_| is in
604 // bps.
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000605 if (suspension_enabled_) {
606 if (!video_suspended_) {
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000607 // Check if we just went below the threshold.
pbos73674632015-10-29 15:45:00 -0700608 if (video_target_bitrate_ < suspension_threshold_bps_) {
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000609 video_suspended_ = true;
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000610 }
611 } else {
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000612 // Video is already suspended. Check if we just went over the threshold
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000613 // with a margin.
pbos73674632015-10-29 15:45:00 -0700614 if (video_target_bitrate_ >
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000615 suspension_threshold_bps_ + suspension_window_bps_) {
616 video_suspended_ = false;
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000617 }
618 }
619 }
620}
621
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000622} // namespace media_optimization
623} // namespace webrtc