blob: f7f1019820f4eb506032797627b842dd9a085a6a [file] [log] [blame]
pbos@webrtc.org9115cde2014-12-09 10:36:40 +00001/* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
Ilya Nikolaevskiybf352982017-10-02 10:08:25 +02002 *
3 * Use of this source code is governed by a BSD-style license
4 * that can be found in the LICENSE file in the root of the source
5 * tree. An additional intellectual property rights grant can be found
6 * in the file PATENTS. All contributing project authors may
7 * be found in the AUTHORS file in the root of the source tree.
8 */
pbos@webrtc.org9115cde2014-12-09 10:36:40 +00009
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "modules/video_coding/codecs/vp8/screenshare_layers.h"
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000011
12#include <stdlib.h>
13
philipelcce46fc2015-12-21 03:04:49 -080014#include <algorithm>
Ilya Nikolaevskiybf352982017-10-02 10:08:25 +020015#include <memory>
philipelcce46fc2015-12-21 03:04:49 -080016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/video_coding/include/video_codec_interface.h"
18#include "rtc_base/checks.h"
Ilya Nikolaevskiy58662912017-10-04 15:07:09 +020019#include "rtc_base/logging.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "system_wrappers/include/clock.h"
21#include "system_wrappers/include/metrics.h"
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000022
23namespace webrtc {
24
Erik Språng2c4c9142015-06-24 11:24:44 +020025static const int kOneSecond90Khz = 90000;
Erik Språng13044c12017-10-05 12:20:36 +020026static const int kMinTimeBetweenSyncs = kOneSecond90Khz * 2;
27static const int kMaxTimeBetweenSyncs = kOneSecond90Khz * 4;
Erik Språng2c4c9142015-06-24 11:24:44 +020028static const int kQpDeltaThresholdForSync = 8;
sprang916170a2017-05-23 07:47:55 -070029static const int kMinBitrateKbpsForQpBoost = 500;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000030
sprang@webrtc.org70f74f32014-12-17 10:57:10 +000031const double ScreenshareLayers::kMaxTL0FpsReduction = 2.5;
32const double ScreenshareLayers::kAcceptableTargetOvershoot = 2.0;
33
sprang429600d2017-01-26 06:12:26 -080034constexpr int ScreenshareLayers::kMaxNumTemporalLayers;
35
sprangafe1f742016-04-12 02:45:13 -070036// Always emit a frame with certain interval, even if bitrate targets have
sprang916170a2017-05-23 07:47:55 -070037// been exceeded. This prevents needless keyframe requests.
sprang6c4bbfa2017-05-30 10:08:23 -070038const int ScreenshareLayers::kMaxFrameIntervalMs = 2750;
sprangafe1f742016-04-12 02:45:13 -070039
Mirko Bonadei6f440ed2018-06-21 13:41:01 +000040ScreenshareLayers::ScreenshareLayers(int num_temporal_layers, Clock* clock)
sprangb0fdfea2016-03-01 05:51:16 -080041 : clock_(clock),
sprang429600d2017-01-26 06:12:26 -080042 number_of_temporal_layers_(
43 std::min(kMaxNumTemporalLayers, num_temporal_layers)),
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000044 last_base_layer_sync_(false),
Erik Språng2c4c9142015-06-24 11:24:44 +020045 active_layer_(-1),
46 last_timestamp_(-1),
47 last_sync_timestamp_(-1),
sprangafe1f742016-04-12 02:45:13 -070048 last_emitted_tl0_timestamp_(-1),
Erik Språng27a457d2018-01-12 11:00:21 +010049 last_frame_time_ms_(-1),
Erik Språng2c4c9142015-06-24 11:24:44 +020050 min_qp_(-1),
51 max_qp_(-1),
52 max_debt_bytes_(0),
sprangac4a90d2016-12-28 05:58:07 -080053 encode_framerate_(1000.0f, 1000.0f), // 1 second window, second scale.
Erik Språng08127a92016-11-16 16:41:30 +010054 bitrate_updated_(false) {
sprang429600d2017-01-26 06:12:26 -080055 RTC_CHECK_GT(number_of_temporal_layers_, 0);
56 RTC_CHECK_LE(number_of_temporal_layers_, kMaxNumTemporalLayers);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000057}
58
sprangb0fdfea2016-03-01 05:51:16 -080059ScreenshareLayers::~ScreenshareLayers() {
60 UpdateHistograms();
61}
62
pbos18ad1d42017-05-04 05:04:46 -070063TemporalLayers::FrameConfig ScreenshareLayers::UpdateLayerConfig(
64 uint32_t timestamp) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000065 if (number_of_temporal_layers_ <= 1) {
66 // No flags needed for 1 layer screenshare.
Peter Boström1436c832017-03-27 15:01:49 -040067 // TODO(pbos): Consider updating only last, and not all buffers.
pbos51f083c2017-05-04 06:39:04 -070068 TemporalLayers::FrameConfig tl_config(
69 kReferenceAndUpdate, kReferenceAndUpdate, kReferenceAndUpdate);
pbos51f083c2017-05-04 06:39:04 -070070 return tl_config;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000071 }
Erik Språng2c4c9142015-06-24 11:24:44 +020072
sprangac4a90d2016-12-28 05:58:07 -080073 const int64_t now_ms = clock_->TimeInMilliseconds();
sprangb0fdfea2016-03-01 05:51:16 -080074
Erik Språng2c4c9142015-06-24 11:24:44 +020075 int64_t unwrapped_timestamp = time_wrap_handler_.Unwrap(timestamp);
sprang916170a2017-05-23 07:47:55 -070076 int64_t ts_diff;
77 if (last_timestamp_ == -1) {
78 ts_diff = kOneSecond90Khz / capture_framerate_.value_or(*target_framerate_);
79 } else {
80 ts_diff = unwrapped_timestamp - last_timestamp_;
81 }
Erik Språng27a457d2018-01-12 11:00:21 +010082
83 if (target_framerate_) {
84 // If input frame rate exceeds target frame rate, either over a one second
85 // averaging window, or if frame interval is below 90% of desired value,
86 // drop frame.
Erik Språng27a457d2018-01-12 11:00:21 +010087 if (encode_framerate_.Rate(now_ms).value_or(0) > *target_framerate_)
88 return TemporalLayers::FrameConfig(kNone, kNone, kNone);
89
Erik Språngdb9e9d52018-01-22 09:23:11 -080090 // Primarily check if frame interval is too short using frame timestamps,
91 // as if they are correct they won't be affected by queuing in webrtc.
92 const int64_t expected_frame_interval_90khz =
93 kOneSecond90Khz / *target_framerate_;
94 if (last_timestamp_ != -1 && ts_diff > 0) {
95 if (ts_diff < 85 * expected_frame_interval_90khz / 100) {
96 return TemporalLayers::FrameConfig(kNone, kNone, kNone);
97 }
98 } else {
99 // Timestamps looks off, use realtime clock here instead.
100 const int64_t expected_frame_interval_ms = 1000 / *target_framerate_;
101 if (last_frame_time_ms_ != -1 &&
102 now_ms - last_frame_time_ms_ <
103 (85 * expected_frame_interval_ms) / 100) {
104 return TemporalLayers::FrameConfig(kNone, kNone, kNone);
105 }
Erik Språng27a457d2018-01-12 11:00:21 +0100106 }
107 }
108
109 if (stats_.first_frame_time_ms_ == -1)
110 stats_.first_frame_time_ms_ = now_ms;
111
sprang916170a2017-05-23 07:47:55 -0700112 // Make sure both frame droppers leak out bits.
113 layers_[0].UpdateDebt(ts_diff / 90);
114 layers_[1].UpdateDebt(ts_diff / 90);
115 last_timestamp_ = timestamp;
Erik Språng27a457d2018-01-12 11:00:21 +0100116 last_frame_time_ms_ = now_ms;
sprang916170a2017-05-23 07:47:55 -0700117
118 TemporalLayerState layer_state = TemporalLayerState::kDrop;
119
Erik Språng2c4c9142015-06-24 11:24:44 +0200120 if (active_layer_ == -1 ||
121 layers_[active_layer_].state != TemporalLayer::State::kDropped) {
sprangafe1f742016-04-12 02:45:13 -0700122 if (last_emitted_tl0_timestamp_ != -1 &&
123 (unwrapped_timestamp - last_emitted_tl0_timestamp_) / 90 >
124 kMaxFrameIntervalMs) {
125 // Too long time has passed since the last frame was emitted, cancel
126 // enough debt to allow a single frame.
127 layers_[0].debt_bytes_ = max_debt_bytes_ - 1;
128 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200129 if (layers_[0].debt_bytes_ > max_debt_bytes_) {
130 // Must drop TL0, encode TL1 instead.
131 if (layers_[1].debt_bytes_ > max_debt_bytes_) {
132 // Must drop both TL0 and TL1.
133 active_layer_ = -1;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000134 } else {
Erik Språng2c4c9142015-06-24 11:24:44 +0200135 active_layer_ = 1;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000136 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200137 } else {
138 active_layer_ = 0;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000139 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000140 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200141
142 switch (active_layer_) {
143 case 0:
sprang916170a2017-05-23 07:47:55 -0700144 layer_state = TemporalLayerState::kTl0;
sprangafe1f742016-04-12 02:45:13 -0700145 last_emitted_tl0_timestamp_ = unwrapped_timestamp;
Erik Språng2c4c9142015-06-24 11:24:44 +0200146 break;
147 case 1:
sprangf03ea042017-07-13 03:53:51 -0700148 if (layers_[1].state != TemporalLayer::State::kDropped) {
149 if (TimeToSync(unwrapped_timestamp)) {
150 last_sync_timestamp_ = unwrapped_timestamp;
151 layer_state = TemporalLayerState::kTl1Sync;
152 } else {
153 layer_state = TemporalLayerState::kTl1;
154 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200155 } else {
sprangf03ea042017-07-13 03:53:51 -0700156 layer_state = last_sync_timestamp_ == unwrapped_timestamp
157 ? TemporalLayerState::kTl1Sync
158 : TemporalLayerState::kTl1;
Erik Språng2c4c9142015-06-24 11:24:44 +0200159 }
160 break;
161 case -1:
sprang916170a2017-05-23 07:47:55 -0700162 layer_state = TemporalLayerState::kDrop;
sprangb0fdfea2016-03-01 05:51:16 -0800163 ++stats_.num_dropped_frames_;
Erik Språng2c4c9142015-06-24 11:24:44 +0200164 break;
165 default:
Erik Språng2c4c9142015-06-24 11:24:44 +0200166 RTC_NOTREACHED();
167 }
168
pbos51f083c2017-05-04 06:39:04 -0700169 TemporalLayers::FrameConfig tl_config;
Peter Boström1436c832017-03-27 15:01:49 -0400170 // TODO(pbos): Consider referencing but not updating the 'alt' buffer for all
171 // layers.
172 switch (layer_state) {
sprang916170a2017-05-23 07:47:55 -0700173 case TemporalLayerState::kDrop:
pbos51f083c2017-05-04 06:39:04 -0700174 tl_config = TemporalLayers::FrameConfig(kNone, kNone, kNone);
175 break;
sprang916170a2017-05-23 07:47:55 -0700176 case TemporalLayerState::kTl0:
Peter Boström1436c832017-03-27 15:01:49 -0400177 // TL0 only references and updates 'last'.
pbos51f083c2017-05-04 06:39:04 -0700178 tl_config =
179 TemporalLayers::FrameConfig(kReferenceAndUpdate, kNone, kNone);
pbos1777c5f2017-07-19 17:04:02 -0700180 tl_config.packetizer_temporal_idx = 0;
pbos51f083c2017-05-04 06:39:04 -0700181 break;
sprang916170a2017-05-23 07:47:55 -0700182 case TemporalLayerState::kTl1:
Peter Boström1436c832017-03-27 15:01:49 -0400183 // TL1 references both 'last' and 'golden' but only updates 'golden'.
pbos51f083c2017-05-04 06:39:04 -0700184 tl_config =
185 TemporalLayers::FrameConfig(kReference, kReferenceAndUpdate, kNone);
pbos1777c5f2017-07-19 17:04:02 -0700186 tl_config.packetizer_temporal_idx = 1;
pbos51f083c2017-05-04 06:39:04 -0700187 break;
sprang916170a2017-05-23 07:47:55 -0700188 case TemporalLayerState::kTl1Sync:
Peter Boström1436c832017-03-27 15:01:49 -0400189 // Predict from only TL0 to allow participants to switch to the high
190 // bitrate stream. Updates 'golden' so that TL1 can continue to refer to
191 // and update 'golden' from this point on.
pbos51f083c2017-05-04 06:39:04 -0700192 tl_config = TemporalLayers::FrameConfig(kReference, kUpdate, kNone);
pbos1777c5f2017-07-19 17:04:02 -0700193 tl_config.packetizer_temporal_idx = 1;
pbos51f083c2017-05-04 06:39:04 -0700194 break;
Peter Boström1436c832017-03-27 15:01:49 -0400195 }
pbos51f083c2017-05-04 06:39:04 -0700196
pbos1777c5f2017-07-19 17:04:02 -0700197 tl_config.layer_sync = layer_state == TemporalLayerState::kTl1Sync;
pbos51f083c2017-05-04 06:39:04 -0700198 return tl_config;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000199}
200
Erik Språngbb60a3a2018-03-19 18:25:10 +0100201void ScreenshareLayers::OnRatesUpdated(
202 const std::vector<uint32_t>& bitrates_bps,
203 int framerate_fps) {
204 RTC_DCHECK_GT(framerate_fps, 0);
205 RTC_DCHECK_GE(bitrates_bps.size(), 1);
206 RTC_DCHECK_LE(bitrates_bps.size(), 2);
207
208 // |bitrates_bps| uses individual rates per layer, but we want to use the
209 // accumulated rate here.
210 uint32_t tl0_kbps = bitrates_bps[0] / 1000;
211 uint32_t tl1_kbps = tl0_kbps;
212 if (bitrates_bps.size() > 1) {
213 tl1_kbps += bitrates_bps[1] / 1000;
214 }
215
sprangac4a90d2016-12-28 05:58:07 -0800216 if (!target_framerate_) {
Erik Språngbb60a3a2018-03-19 18:25:10 +0100217 // First OnRatesUpdated() is called during construction, with the
218 // configured targets as parameters.
219 target_framerate_ = framerate_fps;
sprang0ad0de62017-01-11 05:01:32 -0800220 capture_framerate_ = target_framerate_;
sprangac4a90d2016-12-28 05:58:07 -0800221 bitrate_updated_ = true;
222 } else {
Erik Språnge624d072018-04-11 16:47:27 +0200223 if ((capture_framerate_ &&
224 framerate_fps != static_cast<int>(*capture_framerate_)) ||
225 (tl0_kbps != layers_[0].target_rate_kbps_) ||
226 (tl1_kbps != layers_[1].target_rate_kbps_)) {
227 bitrate_updated_ = true;
228 }
Erik Språngbb60a3a2018-03-19 18:25:10 +0100229
230 if (framerate_fps < 0) {
sprang0ad0de62017-01-11 05:01:32 -0800231 capture_framerate_.reset();
sprangac4a90d2016-12-28 05:58:07 -0800232 } else {
Erik Språngbb60a3a2018-03-19 18:25:10 +0100233 capture_framerate_ = framerate_fps;
sprangac4a90d2016-12-28 05:58:07 -0800234 }
235 }
236
Erik Språngbb60a3a2018-03-19 18:25:10 +0100237 layers_[0].target_rate_kbps_ = tl0_kbps;
238 layers_[1].target_rate_kbps_ = tl1_kbps;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000239}
240
Peter Boström1436c832017-03-27 15:01:49 -0400241void ScreenshareLayers::FrameEncoded(unsigned int size, int qp) {
sprangac4a90d2016-12-28 05:58:07 -0800242 if (size > 0)
243 encode_framerate_.Update(1, clock_->TimeInMilliseconds());
244
sprang2ddb8bd2016-02-04 03:59:52 -0800245 if (number_of_temporal_layers_ == 1)
246 return;
247
248 RTC_DCHECK_NE(-1, active_layer_);
Erik Språng2c4c9142015-06-24 11:24:44 +0200249 if (size == 0) {
250 layers_[active_layer_].state = TemporalLayer::State::kDropped;
sprangb0fdfea2016-03-01 05:51:16 -0800251 ++stats_.num_overshoots_;
Erik Språng2c4c9142015-06-24 11:24:44 +0200252 return;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000253 }
sprangef7228c2015-08-05 02:01:29 -0700254
Erik Språng2c4c9142015-06-24 11:24:44 +0200255 if (layers_[active_layer_].state == TemporalLayer::State::kDropped) {
256 layers_[active_layer_].state = TemporalLayer::State::kQualityBoost;
257 }
258
259 if (qp != -1)
260 layers_[active_layer_].last_qp = qp;
261
262 if (active_layer_ == 0) {
263 layers_[0].debt_bytes_ += size;
264 layers_[1].debt_bytes_ += size;
sprangb0fdfea2016-03-01 05:51:16 -0800265 ++stats_.num_tl0_frames_;
266 stats_.tl0_target_bitrate_sum_ += layers_[0].target_rate_kbps_;
267 stats_.tl0_qp_sum_ += qp;
Erik Språng2c4c9142015-06-24 11:24:44 +0200268 } else if (active_layer_ == 1) {
269 layers_[1].debt_bytes_ += size;
sprangb0fdfea2016-03-01 05:51:16 -0800270 ++stats_.num_tl1_frames_;
271 stats_.tl1_target_bitrate_sum_ += layers_[1].target_rate_kbps_;
272 stats_.tl1_qp_sum_ += qp;
Erik Språng2c4c9142015-06-24 11:24:44 +0200273 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000274}
275
pbos18ad1d42017-05-04 05:04:46 -0700276void ScreenshareLayers::PopulateCodecSpecific(
277 bool frame_is_keyframe,
278 const TemporalLayers::FrameConfig& tl_config,
279 CodecSpecificInfoVP8* vp8_info,
280 uint32_t timestamp) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000281 if (number_of_temporal_layers_ == 1) {
282 vp8_info->temporalIdx = kNoTemporalIdx;
283 vp8_info->layerSync = false;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000284 } else {
pbos1777c5f2017-07-19 17:04:02 -0700285 int64_t unwrapped_timestamp = time_wrap_handler_.Unwrap(timestamp);
286 vp8_info->temporalIdx = tl_config.packetizer_temporal_idx;
287 vp8_info->layerSync = tl_config.layer_sync;
Peter Boström1436c832017-03-27 15:01:49 -0400288 if (frame_is_keyframe) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000289 vp8_info->temporalIdx = 0;
Erik Språng2c4c9142015-06-24 11:24:44 +0200290 last_sync_timestamp_ = unwrapped_timestamp;
pbos1777c5f2017-07-19 17:04:02 -0700291 vp8_info->layerSync = true;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000292 } else if (last_base_layer_sync_ && vp8_info->temporalIdx != 0) {
293 // Regardless of pattern the frame after a base layer sync will always
294 // be a layer sync.
Erik Språng2c4c9142015-06-24 11:24:44 +0200295 last_sync_timestamp_ = unwrapped_timestamp;
pbos1777c5f2017-07-19 17:04:02 -0700296 vp8_info->layerSync = true;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000297 }
Peter Boström1436c832017-03-27 15:01:49 -0400298 last_base_layer_sync_ = frame_is_keyframe;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000299 }
300}
301
Erik Språng2c4c9142015-06-24 11:24:44 +0200302bool ScreenshareLayers::TimeToSync(int64_t timestamp) const {
sprang2ddb8bd2016-02-04 03:59:52 -0800303 RTC_DCHECK_EQ(1, active_layer_);
henrikg91d6ede2015-09-17 00:24:34 -0700304 RTC_DCHECK_NE(-1, layers_[0].last_qp);
Erik Språng2c4c9142015-06-24 11:24:44 +0200305 if (layers_[1].last_qp == -1) {
306 // First frame in TL1 should only depend on TL0 since there are no
307 // previous frames in TL1.
308 return true;
309 }
310
henrikg91d6ede2015-09-17 00:24:34 -0700311 RTC_DCHECK_NE(-1, last_sync_timestamp_);
Erik Språng2c4c9142015-06-24 11:24:44 +0200312 int64_t timestamp_diff = timestamp - last_sync_timestamp_;
313 if (timestamp_diff > kMaxTimeBetweenSyncs) {
314 // After a certain time, force a sync frame.
315 return true;
316 } else if (timestamp_diff < kMinTimeBetweenSyncs) {
317 // If too soon from previous sync frame, don't issue a new one.
318 return false;
319 }
320 // Issue a sync frame if difference in quality between TL0 and TL1 isn't too
321 // large.
322 if (layers_[0].last_qp - layers_[1].last_qp < kQpDeltaThresholdForSync)
323 return true;
324 return false;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000325}
326
sprangc7805db2016-11-25 08:09:43 -0800327uint32_t ScreenshareLayers::GetCodecTargetBitrateKbps() const {
328 uint32_t target_bitrate_kbps = layers_[0].target_rate_kbps_;
329
330 if (number_of_temporal_layers_ > 1) {
331 // Calculate a codec target bitrate. This may be higher than TL0, gaining
332 // quality at the expense of frame rate at TL0. Constraints:
333 // - TL0 frame rate no less than framerate / kMaxTL0FpsReduction.
334 // - Target rate * kAcceptableTargetOvershoot should not exceed TL1 rate.
335 target_bitrate_kbps =
336 std::min(layers_[0].target_rate_kbps_ * kMaxTL0FpsReduction,
337 layers_[1].target_rate_kbps_ / kAcceptableTargetOvershoot);
338 }
339
340 return std::max(layers_[0].target_rate_kbps_, target_bitrate_kbps);
341}
342
Anders Carlssonbeabdcb2018-01-24 10:25:15 +0100343bool ScreenshareLayers::UpdateConfiguration(Vp8EncoderConfig* cfg) {
Erik Språng08127a92016-11-16 16:41:30 +0100344 bool cfg_updated = false;
sprangc7805db2016-11-25 08:09:43 -0800345 uint32_t target_bitrate_kbps = GetCodecTargetBitrateKbps();
Erik Språng27a457d2018-01-12 11:00:21 +0100346
347 // TODO(sprang): We _really_ need to make an overhaul of this class. :(
348 // If we're dropping frames in order to meet a target framerate, adjust the
349 // bitrate assigned to the encoder so the total average bitrate is correct.
350 float encoder_config_bitrate_kbps = target_bitrate_kbps;
351 if (target_framerate_ && capture_framerate_ &&
352 *target_framerate_ < *capture_framerate_) {
353 encoder_config_bitrate_kbps *=
354 static_cast<float>(*capture_framerate_) / *target_framerate_;
355 }
356
357 if (bitrate_updated_ ||
358 cfg->rc_target_bitrate != encoder_config_bitrate_kbps) {
359 cfg->rc_target_bitrate = encoder_config_bitrate_kbps;
Erik Språng08127a92016-11-16 16:41:30 +0100360
361 // Don't reconfigure qp limits during quality boost frames.
362 if (active_layer_ == -1 ||
363 layers_[active_layer_].state != TemporalLayer::State::kQualityBoost) {
364 min_qp_ = cfg->rc_min_quantizer;
365 max_qp_ = cfg->rc_max_quantizer;
366 // After a dropped frame, a frame with max qp will be encoded and the
367 // quality will then ramp up from there. To boost the speed of recovery,
sprang916170a2017-05-23 07:47:55 -0700368 // encode the next frame with lower max qp, if there is sufficient
369 // bandwidth to do so without causing excessive delay.
370 // TL0 is the most important to improve since the errors in this layer
371 // will propagate to TL1.
Erik Språng08127a92016-11-16 16:41:30 +0100372 // Currently, reduce max qp by 20% for TL0 and 15% for TL1.
sprang916170a2017-05-23 07:47:55 -0700373 if (layers_[1].target_rate_kbps_ >= kMinBitrateKbpsForQpBoost) {
374 layers_[0].enhanced_max_qp =
375 min_qp_ + (((max_qp_ - min_qp_) * 80) / 100);
376 layers_[1].enhanced_max_qp =
377 min_qp_ + (((max_qp_ - min_qp_) * 85) / 100);
378 } else {
379 layers_[0].enhanced_max_qp = -1;
380 layers_[1].enhanced_max_qp = -1;
381 }
Erik Språng08127a92016-11-16 16:41:30 +0100382 }
383
sprang0ad0de62017-01-11 05:01:32 -0800384 if (capture_framerate_) {
sprangac4a90d2016-12-28 05:58:07 -0800385 int avg_frame_size =
sprang0ad0de62017-01-11 05:01:32 -0800386 (target_bitrate_kbps * 1000) / (8 * *capture_framerate_);
sprang916170a2017-05-23 07:47:55 -0700387 // Allow max debt to be the size of a single optimal frame.
388 // TODO(sprang): Determine if this needs to be adjusted by some factor.
389 // (Lower values may cause more frame drops, higher may lead to queuing
390 // delays.)
391 max_debt_bytes_ = avg_frame_size;
Erik Språng08127a92016-11-16 16:41:30 +0100392 }
393
394 bitrate_updated_ = false;
395 cfg_updated = true;
396 }
397
398 // Don't try to update boosts state if not active yet.
399 if (active_layer_ == -1)
400 return cfg_updated;
401
sprangef7228c2015-08-05 02:01:29 -0700402 if (max_qp_ == -1 || number_of_temporal_layers_ <= 1)
Erik Språng08127a92016-11-16 16:41:30 +0100403 return cfg_updated;
Erik Språng2c4c9142015-06-24 11:24:44 +0200404
405 // If layer is in the quality boost state (following a dropped frame), update
406 // the configuration with the adjusted (lower) qp and set the state back to
407 // normal.
408 unsigned int adjusted_max_qp;
409 if (layers_[active_layer_].state == TemporalLayer::State::kQualityBoost &&
410 layers_[active_layer_].enhanced_max_qp != -1) {
411 adjusted_max_qp = layers_[active_layer_].enhanced_max_qp;
412 layers_[active_layer_].state = TemporalLayer::State::kNormal;
413 } else {
Erik Språng2c4c9142015-06-24 11:24:44 +0200414 adjusted_max_qp = max_qp_; // Set the normal max qp.
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000415 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200416
417 if (adjusted_max_qp == cfg->rc_max_quantizer)
Erik Språng08127a92016-11-16 16:41:30 +0100418 return cfg_updated;
Erik Språng2c4c9142015-06-24 11:24:44 +0200419
420 cfg->rc_max_quantizer = adjusted_max_qp;
Erik Språng08127a92016-11-16 16:41:30 +0100421 cfg_updated = true;
sprangc7805db2016-11-25 08:09:43 -0800422
Erik Språng08127a92016-11-16 16:41:30 +0100423 return cfg_updated;
Erik Språng2c4c9142015-06-24 11:24:44 +0200424}
425
426void ScreenshareLayers::TemporalLayer::UpdateDebt(int64_t delta_ms) {
427 uint32_t debt_reduction_bytes = target_rate_kbps_ * delta_ms / 8;
428 if (debt_reduction_bytes >= debt_bytes_) {
429 debt_bytes_ = 0;
430 } else {
431 debt_bytes_ -= debt_reduction_bytes;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000432 }
433}
sprang@webrtc.org70f74f32014-12-17 10:57:10 +0000434
sprangb0fdfea2016-03-01 05:51:16 -0800435void ScreenshareLayers::UpdateHistograms() {
436 if (stats_.first_frame_time_ms_ == -1)
437 return;
438 int64_t duration_sec =
439 (clock_->TimeInMilliseconds() - stats_.first_frame_time_ms_ + 500) / 1000;
440 if (duration_sec >= metrics::kMinRunTimeInSeconds) {
441 RTC_HISTOGRAM_COUNTS_10000(
442 "WebRTC.Video.Screenshare.Layer0.FrameRate",
443 (stats_.num_tl0_frames_ + (duration_sec / 2)) / duration_sec);
444 RTC_HISTOGRAM_COUNTS_10000(
445 "WebRTC.Video.Screenshare.Layer1.FrameRate",
446 (stats_.num_tl1_frames_ + (duration_sec / 2)) / duration_sec);
447 int total_frames = stats_.num_tl0_frames_ + stats_.num_tl1_frames_;
asapersson58d992e2016-03-29 02:15:06 -0700448 RTC_HISTOGRAM_COUNTS_10000(
449 "WebRTC.Video.Screenshare.FramesPerDrop",
Ilya Nikolaevskiybf352982017-10-02 10:08:25 +0200450 (stats_.num_dropped_frames_ == 0
451 ? 0
452 : total_frames / stats_.num_dropped_frames_));
asapersson58d992e2016-03-29 02:15:06 -0700453 RTC_HISTOGRAM_COUNTS_10000(
454 "WebRTC.Video.Screenshare.FramesPerOvershoot",
455 (stats_.num_overshoots_ == 0 ? 0
456 : total_frames / stats_.num_overshoots_));
sprangb0fdfea2016-03-01 05:51:16 -0800457 if (stats_.num_tl0_frames_ > 0) {
458 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.Screenshare.Layer0.Qp",
459 stats_.tl0_qp_sum_ / stats_.num_tl0_frames_);
460 RTC_HISTOGRAM_COUNTS_10000(
461 "WebRTC.Video.Screenshare.Layer0.TargetBitrate",
462 stats_.tl0_target_bitrate_sum_ / stats_.num_tl0_frames_);
463 }
464 if (stats_.num_tl1_frames_ > 0) {
465 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.Screenshare.Layer1.Qp",
466 stats_.tl1_qp_sum_ / stats_.num_tl1_frames_);
467 RTC_HISTOGRAM_COUNTS_10000(
468 "WebRTC.Video.Screenshare.Layer1.TargetBitrate",
469 stats_.tl1_target_bitrate_sum_ / stats_.num_tl1_frames_);
470 }
471 }
472}
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000473} // namespace webrtc