blob: d5fbadc122fc34c92ea71debad7aa5ee04bc2742 [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"
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010014#include "webrtc/modules/video_coding/utility/frame_dropper.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010015#include "webrtc/system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000016
17namespace webrtc {
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000018namespace media_optimization {
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000019namespace {
20void UpdateProtectionCallback(
21 VCMProtectionMethod* selected_method,
22 uint32_t* video_rate_bps,
23 uint32_t* nack_overhead_rate_bps,
24 uint32_t* fec_overhead_rate_bps,
25 VCMProtectionCallback* video_protection_callback) {
26 FecProtectionParams delta_fec_params;
27 FecProtectionParams key_fec_params;
28 // Get the FEC code rate for Key frames (set to 0 when NA).
29 key_fec_params.fec_rate = selected_method->RequiredProtectionFactorK();
30
31 // Get the FEC code rate for Delta frames (set to 0 when NA).
32 delta_fec_params.fec_rate = selected_method->RequiredProtectionFactorD();
33
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000034 // The RTP module currently requires the same |max_fec_frames| for both
35 // key and delta frames.
36 delta_fec_params.max_fec_frames = selected_method->MaxFramesFec();
37 key_fec_params.max_fec_frames = selected_method->MaxFramesFec();
38
39 // Set the FEC packet mask type. |kFecMaskBursty| is more effective for
40 // consecutive losses and little/no packet re-ordering. As we currently
41 // do not have feedback data on the degree of correlated losses and packet
42 // re-ordering, we keep default setting to |kFecMaskRandom| for now.
43 delta_fec_params.fec_mask_type = kFecMaskRandom;
44 key_fec_params.fec_mask_type = kFecMaskRandom;
45
46 // TODO(Marco): Pass FEC protection values per layer.
philipel9d3ab612015-12-21 04:12:39 -080047 video_protection_callback->ProtectionRequest(
48 &delta_fec_params, &key_fec_params, video_rate_bps,
49 nack_overhead_rate_bps, fec_overhead_rate_bps);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000050}
51} // namespace
52
53struct MediaOptimization::EncodedFrameSample {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000054 EncodedFrameSample(size_t size_bytes,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000055 uint32_t timestamp,
56 int64_t time_complete_ms)
57 : size_bytes(size_bytes),
58 timestamp(timestamp),
59 time_complete_ms(time_complete_ms) {}
60
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000061 size_t size_bytes;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000062 uint32_t timestamp;
63 int64_t time_complete_ms;
64};
niklase@google.com470e71d2011-07-07 08:21:25 +000065
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000066MediaOptimization::MediaOptimization(Clock* clock)
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000067 : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
68 clock_(clock),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000069 max_bit_rate_(0),
70 send_codec_type_(kVideoCodecUnknown),
71 codec_width_(0),
72 codec_height_(0),
73 user_frame_rate_(0),
henrik.lundin@webrtc.orgb426c462013-09-24 07:41:53 +000074 frame_dropper_(new FrameDropper),
75 loss_prot_logic_(
76 new VCMLossProtectionLogic(clock_->TimeInMilliseconds())),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000077 fraction_lost_(0),
78 send_statistics_zero_encode_(0),
79 max_payload_size_(1460),
pbos73674632015-10-29 15:45:00 -070080 video_target_bitrate_(0),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000081 incoming_frame_rate_(0),
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000082 encoded_frame_samples_(),
83 avg_sent_bit_rate_bps_(0),
84 avg_sent_framerate_(0),
85 key_frame_cnt_(0),
86 delta_frame_cnt_(0),
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +000087 num_layers_(0),
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +000088 suspension_enabled_(false),
89 video_suspended_(false),
90 suspension_threshold_bps_(0),
91 suspension_window_bps_(0) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000092 memset(send_statistics_, 0, sizeof(send_statistics_));
93 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
niklase@google.com470e71d2011-07-07 08:21:25 +000094}
95
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000096MediaOptimization::~MediaOptimization(void) {
97 loss_prot_logic_->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +000098}
99
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000100void MediaOptimization::Reset() {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000101 CriticalSectionScoped lock(crit_sect_.get());
philipel9d3ab612015-12-21 04:12:39 -0800102 SetEncodingDataInternal(kVideoCodecUnknown, 0, 0, 0, 0, 0, 0,
103 max_payload_size_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000104 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
105 incoming_frame_rate_ = 0.0;
106 frame_dropper_->Reset();
107 loss_prot_logic_->Reset(clock_->TimeInMilliseconds());
108 frame_dropper_->SetRates(0, 0);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000109 loss_prot_logic_->UpdateFrameRate(incoming_frame_rate_);
110 loss_prot_logic_->Reset(clock_->TimeInMilliseconds());
111 send_statistics_zero_encode_ = 0;
pbos73674632015-10-29 15:45:00 -0700112 video_target_bitrate_ = 0;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000113 codec_width_ = 0;
114 codec_height_ = 0;
115 user_frame_rate_ = 0;
116 key_frame_cnt_ = 0;
117 delta_frame_cnt_ = 0;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000118 encoded_frame_samples_.clear();
119 avg_sent_bit_rate_bps_ = 0;
120 num_layers_ = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000121}
122
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000123void MediaOptimization::SetEncodingData(VideoCodecType send_codec_type,
124 int32_t max_bit_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000125 uint32_t target_bitrate,
126 uint16_t width,
127 uint16_t height,
Peter Boströmdf664532015-05-12 12:22:14 +0200128 uint32_t frame_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000129 int num_layers,
130 int32_t mtu) {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000131 CriticalSectionScoped lock(crit_sect_.get());
philipel9d3ab612015-12-21 04:12:39 -0800132 SetEncodingDataInternal(send_codec_type, max_bit_rate, frame_rate,
133 target_bitrate, width, height, num_layers, mtu);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000134}
135
136void MediaOptimization::SetEncodingDataInternal(VideoCodecType send_codec_type,
137 int32_t max_bit_rate,
138 uint32_t frame_rate,
139 uint32_t target_bitrate,
140 uint16_t width,
141 uint16_t height,
142 int num_layers,
143 int32_t mtu) {
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000144 // Everything codec specific should be reset here since this means the codec
Peter Boströmad6fc5a2016-05-12 03:01:31 +0200145 // has changed.
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000146
147 max_bit_rate_ = max_bit_rate;
148 send_codec_type_ = send_codec_type;
pbos73674632015-10-29 15:45:00 -0700149 video_target_bitrate_ = target_bitrate;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000150 float target_bitrate_kbps = static_cast<float>(target_bitrate) / 1000.0f;
151 loss_prot_logic_->UpdateBitRate(target_bitrate_kbps);
152 loss_prot_logic_->UpdateFrameRate(static_cast<float>(frame_rate));
153 loss_prot_logic_->UpdateFrameSize(width, height);
154 loss_prot_logic_->UpdateNumLayers(num_layers);
155 frame_dropper_->Reset();
156 frame_dropper_->SetRates(target_bitrate_kbps, static_cast<float>(frame_rate));
157 user_frame_rate_ = static_cast<float>(frame_rate);
158 codec_width_ = width;
159 codec_height_ = height;
160 num_layers_ = (num_layers <= 1) ? 1 : num_layers; // Can also be zero.
161 max_payload_size_ = mtu;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000162}
163
164uint32_t MediaOptimization::SetTargetRates(
165 uint32_t target_bitrate,
166 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000167 int64_t round_trip_time_ms,
Peter Boströmad6fc5a2016-05-12 03:01:31 +0200168 VCMProtectionCallback* protection_callback) {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000169 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000170 VCMProtectionMethod* selected_method = loss_prot_logic_->SelectedMethod();
171 float target_bitrate_kbps = static_cast<float>(target_bitrate) / 1000.0f;
172 loss_prot_logic_->UpdateBitRate(target_bitrate_kbps);
173 loss_prot_logic_->UpdateRtt(round_trip_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000174
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000175 // Get frame rate for encoder: this is the actual/sent frame rate.
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000176 float actual_frame_rate = SentFrameRateInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000177
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000178 // Sanity check.
179 if (actual_frame_rate < 1.0) {
180 actual_frame_rate = 1.0;
181 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000182
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000183 // Update frame rate for the loss protection logic class: frame rate should
184 // be the actual/sent rate.
185 loss_prot_logic_->UpdateFrameRate(actual_frame_rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000186
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000187 fraction_lost_ = fraction_lost;
niklase@google.com470e71d2011-07-07 08:21:25 +0000188
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000189 // Returns the filtered packet loss, used for the protection setting.
190 // The filtered loss may be the received loss (no filter), or some
191 // filtered value (average or max window filter).
192 // Use max window filter for now.
193 FilterPacketLossMode filter_mode = kMaxFilter;
194 uint8_t packet_loss_enc = loss_prot_logic_->FilteredLoss(
195 clock_->TimeInMilliseconds(), filter_mode, fraction_lost);
niklase@google.com470e71d2011-07-07 08:21:25 +0000196
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000197 // For now use the filtered loss for computing the robustness settings.
198 loss_prot_logic_->UpdateFilteredLossPr(packet_loss_enc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000200 // Rate cost of the protection methods.
pbos73674632015-10-29 15:45:00 -0700201 float protection_overhead_rate = 0.0f;
niklase@google.com470e71d2011-07-07 08:21:25 +0000202
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000203 // Update protection settings, when applicable.
mflodmanfcf54bd2015-04-14 21:28:08 +0200204 if (loss_prot_logic_->SelectedType() != kNone) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000205 // Update method will compute the robustness settings for the given
206 // protection method and the overhead cost
207 // the protection method is set by the user via SetVideoProtection.
208 loss_prot_logic_->UpdateMethod();
niklase@google.com470e71d2011-07-07 08:21:25 +0000209
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000210 // Update protection callback with protection settings.
211 uint32_t sent_video_rate_bps = 0;
212 uint32_t sent_nack_rate_bps = 0;
213 uint32_t sent_fec_rate_bps = 0;
214 // Get the bit cost of protection method, based on the amount of
215 // overhead data actually transmitted (including headers) the last
216 // second.
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000217 if (protection_callback) {
philipel9d3ab612015-12-21 04:12:39 -0800218 UpdateProtectionCallback(selected_method, &sent_video_rate_bps,
219 &sent_nack_rate_bps, &sent_fec_rate_bps,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000220 protection_callback);
221 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000222 uint32_t sent_total_rate_bps =
223 sent_video_rate_bps + sent_nack_rate_bps + sent_fec_rate_bps;
224 // Estimate the overhead costs of the next second as staying the same
225 // wrt the source bitrate.
226 if (sent_total_rate_bps > 0) {
pbos73674632015-10-29 15:45:00 -0700227 protection_overhead_rate =
228 static_cast<float>(sent_nack_rate_bps + sent_fec_rate_bps) /
229 sent_total_rate_bps;
niklase@google.com470e71d2011-07-07 08:21:25 +0000230 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000231 // Cap the overhead estimate to 50%.
pbos73674632015-10-29 15:45:00 -0700232 if (protection_overhead_rate > 0.5)
233 protection_overhead_rate = 0.5;
niklase@google.com470e71d2011-07-07 08:21:25 +0000234
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000235 // Get the effective packet loss for encoder ER when applicable. Should be
236 // passed to encoder via fraction_lost.
237 packet_loss_enc = selected_method->RequiredPacketLossER();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000238 }
stefan@webrtc.orgf4c82862011-12-13 15:38:14 +0000239
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000240 // Source coding rate: total rate - protection overhead.
pbos73674632015-10-29 15:45:00 -0700241 video_target_bitrate_ = target_bitrate * (1.0 - protection_overhead_rate);
242
243 // Cap target video bitrate to codec maximum.
244 if (max_bit_rate_ > 0 && video_target_bitrate_ > max_bit_rate_) {
245 video_target_bitrate_ = max_bit_rate_;
246 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000247
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000248 // Update encoding rates following protection settings.
249 float target_video_bitrate_kbps =
pbos73674632015-10-29 15:45:00 -0700250 static_cast<float>(video_target_bitrate_) / 1000.0f;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000251 frame_dropper_->SetRates(target_video_bitrate_kbps, incoming_frame_rate_);
252
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000253 CheckSuspendConditions();
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000254
pbos73674632015-10-29 15:45:00 -0700255 return video_target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000256}
257
pbosba8c15b2015-07-14 09:36:34 -0700258void MediaOptimization::SetProtectionMethod(VCMProtectionMethodEnum method) {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000259 CriticalSectionScoped lock(crit_sect_.get());
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000260 loss_prot_logic_->SetMethod(method);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000261}
262
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000263uint32_t MediaOptimization::InputFrameRate() {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000264 CriticalSectionScoped lock(crit_sect_.get());
265 return InputFrameRateInternal();
266}
267
268uint32_t MediaOptimization::InputFrameRateInternal() {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000269 ProcessIncomingFrameRate(clock_->TimeInMilliseconds());
270 return uint32_t(incoming_frame_rate_ + 0.5f);
271}
272
273uint32_t MediaOptimization::SentFrameRate() {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000274 CriticalSectionScoped lock(crit_sect_.get());
275 return SentFrameRateInternal();
276}
277
278uint32_t MediaOptimization::SentFrameRateInternal() {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000279 PurgeOldFrameSamples(clock_->TimeInMilliseconds());
280 UpdateSentFramerate();
281 return avg_sent_framerate_;
282}
283
284uint32_t MediaOptimization::SentBitRate() {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000285 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000286 const int64_t now_ms = clock_->TimeInMilliseconds();
287 PurgeOldFrameSamples(now_ms);
288 UpdateSentBitrate(now_ms);
289 return avg_sent_bit_rate_bps_;
290}
291
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000292int32_t MediaOptimization::UpdateWithEncodedData(
293 const EncodedImage& encoded_image) {
294 size_t encoded_length = encoded_image._length;
295 uint32_t timestamp = encoded_image._timeStamp;
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000296 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000297 const int64_t now_ms = clock_->TimeInMilliseconds();
298 PurgeOldFrameSamples(now_ms);
299 if (encoded_frame_samples_.size() > 0 &&
300 encoded_frame_samples_.back().timestamp == timestamp) {
301 // Frames having the same timestamp are generated from the same input
302 // frame. We don't want to double count them, but only increment the
303 // size_bytes.
304 encoded_frame_samples_.back().size_bytes += encoded_length;
305 encoded_frame_samples_.back().time_complete_ms = now_ms;
306 } else {
307 encoded_frame_samples_.push_back(
308 EncodedFrameSample(encoded_length, timestamp, now_ms));
309 }
310 UpdateSentBitrate(now_ms);
311 UpdateSentFramerate();
312 if (encoded_length > 0) {
Peter Boström49e196a2015-10-23 15:58:18 +0200313 const bool delta_frame = encoded_image._frameType != kVideoFrameKey;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000314
315 frame_dropper_->Fill(encoded_length, delta_frame);
316 if (max_payload_size_ > 0 && encoded_length > 0) {
317 const float min_packets_per_frame =
318 encoded_length / static_cast<float>(max_payload_size_);
319 if (delta_frame) {
320 loss_prot_logic_->UpdatePacketsPerFrame(min_packets_per_frame,
321 clock_->TimeInMilliseconds());
322 } else {
323 loss_prot_logic_->UpdatePacketsPerFrameKey(
324 min_packets_per_frame, clock_->TimeInMilliseconds());
325 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000326 }
327 if (!delta_frame && encoded_length > 0) {
328 loss_prot_logic_->UpdateKeyFrameSize(static_cast<float>(encoded_length));
329 }
330
331 // Updating counters.
332 if (delta_frame) {
333 delta_frame_cnt_++;
334 } else {
335 key_frame_cnt_++;
336 }
337 }
338
339 return VCM_OK;
340}
341
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000342void MediaOptimization::EnableFrameDropper(bool enable) {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000343 CriticalSectionScoped lock(crit_sect_.get());
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000344 frame_dropper_->Enable(enable);
345}
346
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000347void MediaOptimization::SuspendBelowMinBitrate(int threshold_bps,
348 int window_bps) {
349 CriticalSectionScoped lock(crit_sect_.get());
350 assert(threshold_bps > 0 && window_bps >= 0);
351 suspension_threshold_bps_ = threshold_bps;
352 suspension_window_bps_ = window_bps;
353 suspension_enabled_ = true;
354 video_suspended_ = false;
355}
356
357bool MediaOptimization::IsVideoSuspended() const {
358 CriticalSectionScoped lock(crit_sect_.get());
359 return video_suspended_;
360}
361
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000362bool MediaOptimization::DropFrame() {
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000363 CriticalSectionScoped lock(crit_sect_.get());
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000364 UpdateIncomingFrameRate();
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000365 // Leak appropriate number of bytes.
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000366 frame_dropper_->Leak((uint32_t)(InputFrameRateInternal() + 0.5f));
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000367 if (video_suspended_) {
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000368 return true; // Drop all frames when muted.
369 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000370 return frame_dropper_->DropFrame();
371}
372
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000373void MediaOptimization::UpdateIncomingFrameRate() {
374 int64_t now = clock_->TimeInMilliseconds();
375 if (incoming_frame_times_[0] == 0) {
376 // No shifting if this is the first time.
377 } else {
378 // Shift all times one step.
379 for (int32_t i = (kFrameCountHistorySize - 2); i >= 0; i--) {
380 incoming_frame_times_[i + 1] = incoming_frame_times_[i];
381 }
382 }
383 incoming_frame_times_[0] = now;
384 ProcessIncomingFrameRate(now);
385}
386
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000387void MediaOptimization::PurgeOldFrameSamples(int64_t now_ms) {
388 while (!encoded_frame_samples_.empty()) {
389 if (now_ms - encoded_frame_samples_.front().time_complete_ms >
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000390 kBitrateAverageWinMs) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000391 encoded_frame_samples_.pop_front();
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000392 } else {
393 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000394 }
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000395 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000396}
397
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000398void MediaOptimization::UpdateSentBitrate(int64_t now_ms) {
399 if (encoded_frame_samples_.empty()) {
400 avg_sent_bit_rate_bps_ = 0;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000401 return;
402 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000403 size_t framesize_sum = 0;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000404 for (FrameSampleList::iterator it = encoded_frame_samples_.begin();
philipel9d3ab612015-12-21 04:12:39 -0800405 it != encoded_frame_samples_.end(); ++it) {
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000406 framesize_sum += it->size_bytes;
407 }
408 float denom = static_cast<float>(
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000409 now_ms - encoded_frame_samples_.front().time_complete_ms);
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000410 if (denom >= 1.0f) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000411 avg_sent_bit_rate_bps_ =
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000412 static_cast<uint32_t>(framesize_sum * 8.0f * 1000.0f / denom + 0.5f);
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000413 } else {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000414 avg_sent_bit_rate_bps_ = framesize_sum * 8;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000415 }
416}
417
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000418void MediaOptimization::UpdateSentFramerate() {
419 if (encoded_frame_samples_.size() <= 1) {
420 avg_sent_framerate_ = encoded_frame_samples_.size();
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000421 return;
422 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000423 int denom = encoded_frame_samples_.back().timestamp -
424 encoded_frame_samples_.front().timestamp;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000425 if (denom > 0) {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000426 avg_sent_framerate_ =
427 (90000 * (encoded_frame_samples_.size() - 1) + denom / 2) / denom;
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000428 } else {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000429 avg_sent_framerate_ = encoded_frame_samples_.size();
stefan@webrtc.orgf4944d42013-03-18 17:04:52 +0000430 }
431}
niklase@google.com470e71d2011-07-07 08:21:25 +0000432
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000433// Allowing VCM to keep track of incoming frame rate.
434void MediaOptimization::ProcessIncomingFrameRate(int64_t now) {
435 int32_t num = 0;
436 int32_t nr_of_frames = 0;
437 for (num = 1; num < (kFrameCountHistorySize - 1); ++num) {
438 if (incoming_frame_times_[num] <= 0 ||
439 // don't use data older than 2 s
440 now - incoming_frame_times_[num] > kFrameHistoryWinMs) {
441 break;
442 } else {
443 nr_of_frames++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000444 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000445 }
446 if (num > 1) {
Erik Språng66a641a2015-06-11 14:20:07 +0200447 const int64_t diff =
448 incoming_frame_times_[0] - incoming_frame_times_[num - 1];
449 incoming_frame_rate_ = 0.0; // No frame rate estimate available.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000450 if (diff > 0) {
451 incoming_frame_rate_ = nr_of_frames * 1000.0f / static_cast<float>(diff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000452 }
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000453 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000454}
455
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000456void MediaOptimization::CheckSuspendConditions() {
pbos73674632015-10-29 15:45:00 -0700457 // Check conditions for SuspendBelowMinBitrate. |video_target_bitrate_| is in
458 // bps.
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000459 if (suspension_enabled_) {
460 if (!video_suspended_) {
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000461 // Check if we just went below the threshold.
pbos73674632015-10-29 15:45:00 -0700462 if (video_target_bitrate_ < suspension_threshold_bps_) {
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000463 video_suspended_ = true;
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000464 }
465 } else {
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000466 // Video is already suspended. Check if we just went over the threshold
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000467 // with a margin.
pbos73674632015-10-29 15:45:00 -0700468 if (video_target_bitrate_ >
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000469 suspension_threshold_bps_ + suspension_window_bps_) {
470 video_suspended_ = false;
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000471 }
472 }
473 }
474}
475
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000476} // namespace media_optimization
477} // namespace webrtc