Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(476)

Side by Side Diff: webrtc/modules/video_coding/protection_bitrate_calculator.cc

Issue 1972083002: Move logic for calculating needed bitrate overhead used by NACK and FEC to VideoSender. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Addressed Åsas comments. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/video_coding/media_optimization.h" 11 #include <webrtc/modules/video_coding/protection_bitrate_calculator.h>
12
13 #include "webrtc/base/logging.h"
14 #include "webrtc/modules/video_coding/utility/frame_dropper.h"
15 #include "webrtc/system_wrappers/include/clock.h"
16 12
17 namespace webrtc { 13 namespace webrtc {
18 namespace media_optimization { 14
19 namespace { 15 using rtc::CritScope;
20 void UpdateProtectionCallback( 16
21 VCMProtectionMethod* selected_method, 17 struct ProtectionBitrateCalculator::EncodedFrameSample {
22 uint32_t* video_rate_bps, 18 EncodedFrameSample(size_t size_bytes,
23 uint32_t* nack_overhead_rate_bps, 19 uint32_t timestamp,
24 uint32_t* fec_overhead_rate_bps, 20 int64_t time_complete_ms)
25 VCMProtectionCallback* video_protection_callback) { 21 : size_bytes(size_bytes),
22 timestamp(timestamp),
23 time_complete_ms(time_complete_ms) {}
24 size_t size_bytes;
25 uint32_t timestamp;
26 int64_t time_complete_ms;
27 };
28
29 ProtectionBitrateCalculator::ProtectionBitrateCalculator(
30 Clock* clock,
31 VCMProtectionCallback* protection_callback)
32 : clock_(clock),
33 protection_callback_(protection_callback),
34 loss_prot_logic_(new media_optimization::VCMLossProtectionLogic(
35 clock_->TimeInMilliseconds())),
36 max_payload_size_(1460) {}
37
38 ProtectionBitrateCalculator::~ProtectionBitrateCalculator(void) {
39 loss_prot_logic_->Release();
40 }
41
42 void ProtectionBitrateCalculator::SetEncodingData(uint32_t target_bitrate,
43 uint16_t width,
44 uint16_t height,
45 uint32_t frame_rate,
46 size_t num_temporal_layers,
47 size_t max_payload_size) {
48 CritScope lock(&crit_sect_);
49 // Everything codec specific should be reset here since this means the codec
50 // has changed.
51 float target_bitrate_kbps = static_cast<float>(target_bitrate) / 1000.0f;
52 loss_prot_logic_->UpdateBitRate(target_bitrate_kbps);
53 loss_prot_logic_->UpdateFrameRate(static_cast<float>(frame_rate));
54 loss_prot_logic_->UpdateFrameSize(width, height);
55 loss_prot_logic_->UpdateNumLayers(num_temporal_layers);
56 max_payload_size_ = max_payload_size;
57 }
58
59 uint32_t ProtectionBitrateCalculator::SetTargetRates(
60 uint32_t estimated_bitrate_bps,
61 int actual_framerate_fps,
62 uint8_t fraction_lost,
63 int64_t round_trip_time_ms) {
64 float target_bitrate_kbps =
65 static_cast<float>(estimated_bitrate_bps) / 1000.0f;
66 // Sanity check.
67 if (actual_framerate_fps < 1.0) {
68 actual_framerate_fps = 1.0;
69 }
70
26 FecProtectionParams delta_fec_params; 71 FecProtectionParams delta_fec_params;
27 FecProtectionParams key_fec_params; 72 FecProtectionParams key_fec_params;
28 // Get the FEC code rate for Key frames (set to 0 when NA). 73 {
29 key_fec_params.fec_rate = selected_method->RequiredProtectionFactorK(); 74 CritScope lock(&crit_sect_);
30 75
31 // Get the FEC code rate for Delta frames (set to 0 when NA). 76 loss_prot_logic_->UpdateBitRate(target_bitrate_kbps);
32 delta_fec_params.fec_rate = selected_method->RequiredProtectionFactorD(); 77 loss_prot_logic_->UpdateRtt(round_trip_time_ms);
33 78
34 // The RTP module currently requires the same |max_fec_frames| for both 79 // Update frame rate for the loss protection logic class: frame rate should
35 // key and delta frames. 80 // be the actual/sent rate.
36 delta_fec_params.max_fec_frames = selected_method->MaxFramesFec(); 81 loss_prot_logic_->UpdateFrameRate(actual_framerate_fps);
37 key_fec_params.max_fec_frames = selected_method->MaxFramesFec(); 82
83 // Returns the filtered packet loss, used for the protection setting.
84 // The filtered loss may be the received loss (no filter), or some
85 // filtered value (average or max window filter).
86 // Use max window filter for now.
87 media_optimization::FilterPacketLossMode filter_mode =
88 media_optimization::kMaxFilter;
89 uint8_t packet_loss_enc = loss_prot_logic_->FilteredLoss(
90 clock_->TimeInMilliseconds(), filter_mode, fraction_lost);
91
92 // For now use the filtered loss for computing the robustness settings.
93 loss_prot_logic_->UpdateFilteredLossPr(packet_loss_enc);
94
95 if (loss_prot_logic_->SelectedType() == media_optimization::kNone) {
96 return estimated_bitrate_bps;
97 }
98
99 // Update method will compute the robustness settings for the given
100 // protection method and the overhead cost
101 // the protection method is set by the user via SetVideoProtection.
102 loss_prot_logic_->UpdateMethod();
103
104 // Get the bit cost of protection method, based on the amount of
105 // overhead data actually transmitted (including headers) the last
106 // second.
107
108 // Get the FEC code rate for Key frames (set to 0 when NA).
109 key_fec_params.fec_rate =
110 loss_prot_logic_->SelectedMethod()->RequiredProtectionFactorK();
111
112 // Get the FEC code rate for Delta frames (set to 0 when NA).
113 delta_fec_params.fec_rate =
114 loss_prot_logic_->SelectedMethod()->RequiredProtectionFactorD();
115
116 // The RTP module currently requires the same |max_fec_frames| for both
117 // key and delta frames.
118 delta_fec_params.max_fec_frames =
119 loss_prot_logic_->SelectedMethod()->MaxFramesFec();
120 key_fec_params.max_fec_frames =
121 loss_prot_logic_->SelectedMethod()->MaxFramesFec();
122 }
38 123
39 // Set the FEC packet mask type. |kFecMaskBursty| is more effective for 124 // Set the FEC packet mask type. |kFecMaskBursty| is more effective for
40 // consecutive losses and little/no packet re-ordering. As we currently 125 // 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 126 // do not have feedback data on the degree of correlated losses and packet
42 // re-ordering, we keep default setting to |kFecMaskRandom| for now. 127 // re-ordering, we keep default setting to |kFecMaskRandom| for now.
43 delta_fec_params.fec_mask_type = kFecMaskRandom; 128 delta_fec_params.fec_mask_type = kFecMaskRandom;
44 key_fec_params.fec_mask_type = kFecMaskRandom; 129 key_fec_params.fec_mask_type = kFecMaskRandom;
45 130
46 // TODO(Marco): Pass FEC protection values per layer. 131 // Update protection callback with protection settings.
47 video_protection_callback->ProtectionRequest( 132 uint32_t sent_video_rate_bps = 0;
48 &delta_fec_params, &key_fec_params, video_rate_bps, 133 uint32_t sent_nack_rate_bps = 0;
49 nack_overhead_rate_bps, fec_overhead_rate_bps); 134 uint32_t sent_fec_rate_bps = 0;
50 }
51 } // namespace
52
53 struct MediaOptimization::EncodedFrameSample {
54 EncodedFrameSample(size_t size_bytes,
55 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
61 size_t size_bytes;
62 uint32_t timestamp;
63 int64_t time_complete_ms;
64 };
65
66 MediaOptimization::MediaOptimization(Clock* clock)
67 : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
68 clock_(clock),
69 max_bit_rate_(0),
70 send_codec_type_(kVideoCodecUnknown),
71 codec_width_(0),
72 codec_height_(0),
73 user_frame_rate_(0),
74 frame_dropper_(new FrameDropper),
75 loss_prot_logic_(
76 new VCMLossProtectionLogic(clock_->TimeInMilliseconds())),
77 fraction_lost_(0),
78 send_statistics_zero_encode_(0),
79 max_payload_size_(1460),
80 video_target_bitrate_(0),
81 incoming_frame_rate_(0),
82 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),
87 num_layers_(0),
88 suspension_enabled_(false),
89 video_suspended_(false),
90 suspension_threshold_bps_(0),
91 suspension_window_bps_(0) {
92 memset(send_statistics_, 0, sizeof(send_statistics_));
93 memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
94 }
95
96 MediaOptimization::~MediaOptimization(void) {
97 loss_prot_logic_->Release();
98 }
99
100 void MediaOptimization::Reset() {
101 CriticalSectionScoped lock(crit_sect_.get());
102 SetEncodingDataInternal(kVideoCodecUnknown, 0, 0, 0, 0, 0, 0,
103 max_payload_size_);
104 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);
109 loss_prot_logic_->UpdateFrameRate(incoming_frame_rate_);
110 loss_prot_logic_->Reset(clock_->TimeInMilliseconds());
111 send_statistics_zero_encode_ = 0;
112 video_target_bitrate_ = 0;
113 codec_width_ = 0;
114 codec_height_ = 0;
115 user_frame_rate_ = 0;
116 key_frame_cnt_ = 0;
117 delta_frame_cnt_ = 0;
118 encoded_frame_samples_.clear();
119 avg_sent_bit_rate_bps_ = 0;
120 num_layers_ = 1;
121 }
122
123 void MediaOptimization::SetEncodingData(VideoCodecType send_codec_type,
124 int32_t max_bit_rate,
125 uint32_t target_bitrate,
126 uint16_t width,
127 uint16_t height,
128 uint32_t frame_rate,
129 int num_layers,
130 int32_t mtu) {
131 CriticalSectionScoped lock(crit_sect_.get());
132 SetEncodingDataInternal(send_codec_type, max_bit_rate, frame_rate,
133 target_bitrate, width, height, num_layers, mtu);
134 }
135
136 void 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) {
144 // Everything codec specific should be reset here since this means the codec
145 // has changed.
146
147 max_bit_rate_ = max_bit_rate;
148 send_codec_type_ = send_codec_type;
149 video_target_bitrate_ = target_bitrate;
150 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;
162 }
163
164 uint32_t MediaOptimization::SetTargetRates(
165 uint32_t target_bitrate,
166 uint8_t fraction_lost,
167 int64_t round_trip_time_ms,
168 VCMProtectionCallback* protection_callback) {
169 CriticalSectionScoped lock(crit_sect_.get());
170 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);
174
175 // Get frame rate for encoder: this is the actual/sent frame rate.
176 float actual_frame_rate = SentFrameRateInternal();
177
178 // Sanity check.
179 if (actual_frame_rate < 1.0) {
180 actual_frame_rate = 1.0;
181 }
182
183 // 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);
186
187 fraction_lost_ = fraction_lost;
188
189 // 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);
196
197 // For now use the filtered loss for computing the robustness settings.
198 loss_prot_logic_->UpdateFilteredLossPr(packet_loss_enc);
199
200 // Rate cost of the protection methods. 135 // Rate cost of the protection methods.
201 float protection_overhead_rate = 0.0f; 136 float protection_overhead_rate = 0.0f;
202 137
203 // Update protection settings, when applicable. 138 // TODO(Marco): Pass FEC protection values per layer.
204 if (loss_prot_logic_->SelectedType() != kNone) { 139 protection_callback_->ProtectionRequest(
205 // Update method will compute the robustness settings for the given 140 &delta_fec_params, &key_fec_params, &sent_video_rate_bps,
206 // protection method and the overhead cost 141 &sent_nack_rate_bps, &sent_fec_rate_bps);
207 // the protection method is set by the user via SetVideoProtection.
208 loss_prot_logic_->UpdateMethod();
209 142
210 // Update protection callback with protection settings. 143 uint32_t sent_total_rate_bps =
211 uint32_t sent_video_rate_bps = 0; 144 sent_video_rate_bps + sent_nack_rate_bps + sent_fec_rate_bps;
212 uint32_t sent_nack_rate_bps = 0; 145 // Estimate the overhead costs of the next second as staying the same
213 uint32_t sent_fec_rate_bps = 0; 146 // wrt the source bitrate.
214 // Get the bit cost of protection method, based on the amount of 147 if (sent_total_rate_bps > 0) {
215 // overhead data actually transmitted (including headers) the last 148 protection_overhead_rate =
216 // second. 149 static_cast<float>(sent_nack_rate_bps + sent_fec_rate_bps) /
217 if (protection_callback) { 150 sent_total_rate_bps;
218 UpdateProtectionCallback(selected_method, &sent_video_rate_bps,
219 &sent_nack_rate_bps, &sent_fec_rate_bps,
220 protection_callback);
221 }
222 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) {
227 protection_overhead_rate =
228 static_cast<float>(sent_nack_rate_bps + sent_fec_rate_bps) /
229 sent_total_rate_bps;
230 }
231 // Cap the overhead estimate to 50%.
232 if (protection_overhead_rate > 0.5)
233 protection_overhead_rate = 0.5;
234
235 // 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();
238 } 151 }
152 // Cap the overhead estimate to 50%.
153 if (protection_overhead_rate > 0.5)
154 protection_overhead_rate = 0.5;
239 155
240 // Source coding rate: total rate - protection overhead. 156 // Source coding rate: total rate - protection overhead.
241 video_target_bitrate_ = target_bitrate * (1.0 - protection_overhead_rate); 157 return estimated_bitrate_bps * (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 }
247
248 // Update encoding rates following protection settings.
249 float target_video_bitrate_kbps =
250 static_cast<float>(video_target_bitrate_) / 1000.0f;
251 frame_dropper_->SetRates(target_video_bitrate_kbps, incoming_frame_rate_);
252
253 CheckSuspendConditions();
254
255 return video_target_bitrate_;
256 } 158 }
257 159
258 void MediaOptimization::SetProtectionMethod(VCMProtectionMethodEnum method) { 160 void ProtectionBitrateCalculator::SetProtectionMethod(bool enable_fec,
259 CriticalSectionScoped lock(crit_sect_.get()); 161 bool enable_nack) {
162 media_optimization::VCMProtectionMethodEnum method(media_optimization::kNone);
163 if (enable_fec && enable_nack) {
164 method = media_optimization::kNackFec;
165 } else if (enable_nack) {
166 method = media_optimization::kNack;
167 } else if (enable_fec) {
168 method = media_optimization::kFec;
169 }
170 CritScope lock(&crit_sect_);
260 loss_prot_logic_->SetMethod(method); 171 loss_prot_logic_->SetMethod(method);
261 } 172 }
262 173
263 uint32_t MediaOptimization::InputFrameRate() { 174 void ProtectionBitrateCalculator::UpdateWithEncodedData(
264 CriticalSectionScoped lock(crit_sect_.get());
265 return InputFrameRateInternal();
266 }
267
268 uint32_t MediaOptimization::InputFrameRateInternal() {
269 ProcessIncomingFrameRate(clock_->TimeInMilliseconds());
270 return uint32_t(incoming_frame_rate_ + 0.5f);
271 }
272
273 uint32_t MediaOptimization::SentFrameRate() {
274 CriticalSectionScoped lock(crit_sect_.get());
275 return SentFrameRateInternal();
276 }
277
278 uint32_t MediaOptimization::SentFrameRateInternal() {
279 PurgeOldFrameSamples(clock_->TimeInMilliseconds());
280 UpdateSentFramerate();
281 return avg_sent_framerate_;
282 }
283
284 uint32_t MediaOptimization::SentBitRate() {
285 CriticalSectionScoped lock(crit_sect_.get());
286 const int64_t now_ms = clock_->TimeInMilliseconds();
287 PurgeOldFrameSamples(now_ms);
288 UpdateSentBitrate(now_ms);
289 return avg_sent_bit_rate_bps_;
290 }
291
292 int32_t MediaOptimization::UpdateWithEncodedData(
293 const EncodedImage& encoded_image) { 175 const EncodedImage& encoded_image) {
294 size_t encoded_length = encoded_image._length; 176 const size_t encoded_length = encoded_image._length;
295 uint32_t timestamp = encoded_image._timeStamp; 177 CritScope lock(&crit_sect_);
296 CriticalSectionScoped lock(crit_sect_.get());
297 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) { 178 if (encoded_length > 0) {
313 const bool delta_frame = encoded_image._frameType != kVideoFrameKey; 179 const bool delta_frame = encoded_image._frameType != kVideoFrameKey;
314 180
315 frame_dropper_->Fill(encoded_length, delta_frame);
316 if (max_payload_size_ > 0 && encoded_length > 0) { 181 if (max_payload_size_ > 0 && encoded_length > 0) {
317 const float min_packets_per_frame = 182 const float min_packets_per_frame =
318 encoded_length / static_cast<float>(max_payload_size_); 183 encoded_length / static_cast<float>(max_payload_size_);
319 if (delta_frame) { 184 if (delta_frame) {
320 loss_prot_logic_->UpdatePacketsPerFrame(min_packets_per_frame, 185 loss_prot_logic_->UpdatePacketsPerFrame(min_packets_per_frame,
321 clock_->TimeInMilliseconds()); 186 clock_->TimeInMilliseconds());
322 } else { 187 } else {
323 loss_prot_logic_->UpdatePacketsPerFrameKey( 188 loss_prot_logic_->UpdatePacketsPerFrameKey(
324 min_packets_per_frame, clock_->TimeInMilliseconds()); 189 min_packets_per_frame, clock_->TimeInMilliseconds());
325 } 190 }
326 } 191 }
327 if (!delta_frame && encoded_length > 0) { 192 if (!delta_frame && encoded_length > 0) {
328 loss_prot_logic_->UpdateKeyFrameSize(static_cast<float>(encoded_length)); 193 loss_prot_logic_->UpdateKeyFrameSize(static_cast<float>(encoded_length));
329 } 194 }
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
342 void MediaOptimization::EnableFrameDropper(bool enable) {
343 CriticalSectionScoped lock(crit_sect_.get());
344 frame_dropper_->Enable(enable);
345 }
346
347 void 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
357 bool MediaOptimization::IsVideoSuspended() const {
358 CriticalSectionScoped lock(crit_sect_.get());
359 return video_suspended_;
360 }
361
362 bool MediaOptimization::DropFrame() {
363 CriticalSectionScoped lock(crit_sect_.get());
364 UpdateIncomingFrameRate();
365 // Leak appropriate number of bytes.
366 frame_dropper_->Leak((uint32_t)(InputFrameRateInternal() + 0.5f));
367 if (video_suspended_) {
368 return true; // Drop all frames when muted.
369 }
370 return frame_dropper_->DropFrame();
371 }
372
373 void 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
387 void MediaOptimization::PurgeOldFrameSamples(int64_t now_ms) {
388 while (!encoded_frame_samples_.empty()) {
389 if (now_ms - encoded_frame_samples_.front().time_complete_ms >
390 kBitrateAverageWinMs) {
391 encoded_frame_samples_.pop_front();
392 } else {
393 break;
394 }
395 } 195 }
396 } 196 }
397 197
398 void MediaOptimization::UpdateSentBitrate(int64_t now_ms) {
399 if (encoded_frame_samples_.empty()) {
400 avg_sent_bit_rate_bps_ = 0;
401 return;
402 }
403 size_t framesize_sum = 0;
404 for (FrameSampleList::iterator it = encoded_frame_samples_.begin();
405 it != encoded_frame_samples_.end(); ++it) {
406 framesize_sum += it->size_bytes;
407 }
408 float denom = static_cast<float>(
409 now_ms - encoded_frame_samples_.front().time_complete_ms);
410 if (denom >= 1.0f) {
411 avg_sent_bit_rate_bps_ =
412 static_cast<uint32_t>(framesize_sum * 8.0f * 1000.0f / denom + 0.5f);
413 } else {
414 avg_sent_bit_rate_bps_ = framesize_sum * 8;
415 }
416 }
417
418 void MediaOptimization::UpdateSentFramerate() {
419 if (encoded_frame_samples_.size() <= 1) {
420 avg_sent_framerate_ = encoded_frame_samples_.size();
421 return;
422 }
423 int denom = encoded_frame_samples_.back().timestamp -
424 encoded_frame_samples_.front().timestamp;
425 if (denom > 0) {
426 avg_sent_framerate_ =
427 (90000 * (encoded_frame_samples_.size() - 1) + denom / 2) / denom;
428 } else {
429 avg_sent_framerate_ = encoded_frame_samples_.size();
430 }
431 }
432
433 // Allowing VCM to keep track of incoming frame rate.
434 void 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++;
444 }
445 }
446 if (num > 1) {
447 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.
450 if (diff > 0) {
451 incoming_frame_rate_ = nr_of_frames * 1000.0f / static_cast<float>(diff);
452 }
453 }
454 }
455
456 void MediaOptimization::CheckSuspendConditions() {
457 // Check conditions for SuspendBelowMinBitrate. |video_target_bitrate_| is in
458 // bps.
459 if (suspension_enabled_) {
460 if (!video_suspended_) {
461 // Check if we just went below the threshold.
462 if (video_target_bitrate_ < suspension_threshold_bps_) {
463 video_suspended_ = true;
464 }
465 } else {
466 // Video is already suspended. Check if we just went over the threshold
467 // with a margin.
468 if (video_target_bitrate_ >
469 suspension_threshold_bps_ + suspension_window_bps_) {
470 video_suspended_ = false;
471 }
472 }
473 }
474 }
475
476 } // namespace media_optimization
477 } // namespace webrtc 198 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698