blob: 1e8427b7e189920faeef8b5b395137f9f29a5d33 [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
Erik Språng08127a92016-11-16 16:41:30 +010040webrtc::TemporalLayers* ScreenshareTemporalLayersFactory::Create(
41 int simulcast_id,
42 int num_temporal_layers,
43 uint8_t initial_tl0_pic_idx) const {
sprang429600d2017-01-26 06:12:26 -080044 webrtc::TemporalLayers* tl;
45 if (simulcast_id == 0) {
46 tl = new webrtc::ScreenshareLayers(num_temporal_layers, rand(),
47 webrtc::Clock::GetRealTimeClock());
48 } else {
sprang5271ea62017-03-01 01:58:17 -080049 TemporalLayersFactory rt_tl_factory;
sprang429600d2017-01-26 06:12:26 -080050 tl = rt_tl_factory.Create(simulcast_id, num_temporal_layers, rand());
51 }
Erik Språng08127a92016-11-16 16:41:30 +010052 if (listener_)
53 listener_->OnTemporalLayersCreated(simulcast_id, tl);
54 return tl;
55}
56
Ilya Nikolaevskiybf352982017-10-02 10:08:25 +020057std::unique_ptr<webrtc::TemporalLayersChecker>
58ScreenshareTemporalLayersFactory::CreateChecker(
59 int simulcast_id,
60 int temporal_layers,
61 uint8_t initial_tl0_pic_idx) const {
62 webrtc::TemporalLayersChecker* tlc;
63 if (simulcast_id == 0) {
Ilya Nikolaevskiy58662912017-10-04 15:07:09 +020064 tlc =
65 new webrtc::TemporalLayersChecker(temporal_layers, initial_tl0_pic_idx);
Ilya Nikolaevskiybf352982017-10-02 10:08:25 +020066 } else {
67 TemporalLayersFactory rt_tl_factory;
68 return rt_tl_factory.CreateChecker(simulcast_id, temporal_layers,
69 initial_tl0_pic_idx);
70 }
71 return std::unique_ptr<webrtc::TemporalLayersChecker>(tlc);
72}
73
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000074ScreenshareLayers::ScreenshareLayers(int num_temporal_layers,
sprangb0fdfea2016-03-01 05:51:16 -080075 uint8_t initial_tl0_pic_idx,
76 Clock* clock)
77 : clock_(clock),
sprang429600d2017-01-26 06:12:26 -080078 number_of_temporal_layers_(
79 std::min(kMaxNumTemporalLayers, num_temporal_layers)),
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000080 last_base_layer_sync_(false),
81 tl0_pic_idx_(initial_tl0_pic_idx),
Erik Språng2c4c9142015-06-24 11:24:44 +020082 active_layer_(-1),
83 last_timestamp_(-1),
84 last_sync_timestamp_(-1),
sprangafe1f742016-04-12 02:45:13 -070085 last_emitted_tl0_timestamp_(-1),
Erik Språng27a457d2018-01-12 11:00:21 +010086 last_frame_time_ms_(-1),
Erik Språng2c4c9142015-06-24 11:24:44 +020087 min_qp_(-1),
88 max_qp_(-1),
89 max_debt_bytes_(0),
sprangac4a90d2016-12-28 05:58:07 -080090 encode_framerate_(1000.0f, 1000.0f), // 1 second window, second scale.
Erik Språng08127a92016-11-16 16:41:30 +010091 bitrate_updated_(false) {
sprang429600d2017-01-26 06:12:26 -080092 RTC_CHECK_GT(number_of_temporal_layers_, 0);
93 RTC_CHECK_LE(number_of_temporal_layers_, kMaxNumTemporalLayers);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000094}
95
sprangb0fdfea2016-03-01 05:51:16 -080096ScreenshareLayers::~ScreenshareLayers() {
97 UpdateHistograms();
98}
99
brandtr080830c2017-05-03 03:25:53 -0700100uint8_t ScreenshareLayers::Tl0PicIdx() const {
101 return tl0_pic_idx_;
102}
103
pbos18ad1d42017-05-04 05:04:46 -0700104TemporalLayers::FrameConfig ScreenshareLayers::UpdateLayerConfig(
105 uint32_t timestamp) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000106 if (number_of_temporal_layers_ <= 1) {
107 // No flags needed for 1 layer screenshare.
Peter Boström1436c832017-03-27 15:01:49 -0400108 // TODO(pbos): Consider updating only last, and not all buffers.
pbos51f083c2017-05-04 06:39:04 -0700109 TemporalLayers::FrameConfig tl_config(
110 kReferenceAndUpdate, kReferenceAndUpdate, kReferenceAndUpdate);
pbos51f083c2017-05-04 06:39:04 -0700111 return tl_config;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000112 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200113
sprangac4a90d2016-12-28 05:58:07 -0800114 const int64_t now_ms = clock_->TimeInMilliseconds();
sprangb0fdfea2016-03-01 05:51:16 -0800115
Erik Språng2c4c9142015-06-24 11:24:44 +0200116 int64_t unwrapped_timestamp = time_wrap_handler_.Unwrap(timestamp);
sprang916170a2017-05-23 07:47:55 -0700117 int64_t ts_diff;
118 if (last_timestamp_ == -1) {
119 ts_diff = kOneSecond90Khz / capture_framerate_.value_or(*target_framerate_);
120 } else {
121 ts_diff = unwrapped_timestamp - last_timestamp_;
122 }
Erik Språng27a457d2018-01-12 11:00:21 +0100123
124 if (target_framerate_) {
125 // If input frame rate exceeds target frame rate, either over a one second
126 // averaging window, or if frame interval is below 90% of desired value,
127 // drop frame.
Erik Språng27a457d2018-01-12 11:00:21 +0100128 if (encode_framerate_.Rate(now_ms).value_or(0) > *target_framerate_)
129 return TemporalLayers::FrameConfig(kNone, kNone, kNone);
130
Erik Språngdb9e9d52018-01-22 09:23:11 -0800131 // Primarily check if frame interval is too short using frame timestamps,
132 // as if they are correct they won't be affected by queuing in webrtc.
133 const int64_t expected_frame_interval_90khz =
134 kOneSecond90Khz / *target_framerate_;
135 if (last_timestamp_ != -1 && ts_diff > 0) {
136 if (ts_diff < 85 * expected_frame_interval_90khz / 100) {
137 return TemporalLayers::FrameConfig(kNone, kNone, kNone);
138 }
139 } else {
140 // Timestamps looks off, use realtime clock here instead.
141 const int64_t expected_frame_interval_ms = 1000 / *target_framerate_;
142 if (last_frame_time_ms_ != -1 &&
143 now_ms - last_frame_time_ms_ <
144 (85 * expected_frame_interval_ms) / 100) {
145 return TemporalLayers::FrameConfig(kNone, kNone, kNone);
146 }
Erik Språng27a457d2018-01-12 11:00:21 +0100147 }
148 }
149
150 if (stats_.first_frame_time_ms_ == -1)
151 stats_.first_frame_time_ms_ = now_ms;
152
sprang916170a2017-05-23 07:47:55 -0700153 // Make sure both frame droppers leak out bits.
154 layers_[0].UpdateDebt(ts_diff / 90);
155 layers_[1].UpdateDebt(ts_diff / 90);
156 last_timestamp_ = timestamp;
Erik Språng27a457d2018-01-12 11:00:21 +0100157 last_frame_time_ms_ = now_ms;
sprang916170a2017-05-23 07:47:55 -0700158
159 TemporalLayerState layer_state = TemporalLayerState::kDrop;
160
Erik Språng2c4c9142015-06-24 11:24:44 +0200161 if (active_layer_ == -1 ||
162 layers_[active_layer_].state != TemporalLayer::State::kDropped) {
sprangafe1f742016-04-12 02:45:13 -0700163 if (last_emitted_tl0_timestamp_ != -1 &&
164 (unwrapped_timestamp - last_emitted_tl0_timestamp_) / 90 >
165 kMaxFrameIntervalMs) {
166 // Too long time has passed since the last frame was emitted, cancel
167 // enough debt to allow a single frame.
168 layers_[0].debt_bytes_ = max_debt_bytes_ - 1;
169 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200170 if (layers_[0].debt_bytes_ > max_debt_bytes_) {
171 // Must drop TL0, encode TL1 instead.
172 if (layers_[1].debt_bytes_ > max_debt_bytes_) {
173 // Must drop both TL0 and TL1.
174 active_layer_ = -1;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000175 } else {
Erik Språng2c4c9142015-06-24 11:24:44 +0200176 active_layer_ = 1;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000177 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200178 } else {
179 active_layer_ = 0;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000180 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000181 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200182
183 switch (active_layer_) {
184 case 0:
sprang916170a2017-05-23 07:47:55 -0700185 layer_state = TemporalLayerState::kTl0;
sprangafe1f742016-04-12 02:45:13 -0700186 last_emitted_tl0_timestamp_ = unwrapped_timestamp;
Erik Språng2c4c9142015-06-24 11:24:44 +0200187 break;
188 case 1:
sprangf03ea042017-07-13 03:53:51 -0700189 if (layers_[1].state != TemporalLayer::State::kDropped) {
190 if (TimeToSync(unwrapped_timestamp)) {
191 last_sync_timestamp_ = unwrapped_timestamp;
192 layer_state = TemporalLayerState::kTl1Sync;
193 } else {
194 layer_state = TemporalLayerState::kTl1;
195 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200196 } else {
sprangf03ea042017-07-13 03:53:51 -0700197 layer_state = last_sync_timestamp_ == unwrapped_timestamp
198 ? TemporalLayerState::kTl1Sync
199 : TemporalLayerState::kTl1;
Erik Språng2c4c9142015-06-24 11:24:44 +0200200 }
201 break;
202 case -1:
sprang916170a2017-05-23 07:47:55 -0700203 layer_state = TemporalLayerState::kDrop;
sprangb0fdfea2016-03-01 05:51:16 -0800204 ++stats_.num_dropped_frames_;
Erik Språng2c4c9142015-06-24 11:24:44 +0200205 break;
206 default:
Erik Språng2c4c9142015-06-24 11:24:44 +0200207 RTC_NOTREACHED();
208 }
209
pbos51f083c2017-05-04 06:39:04 -0700210 TemporalLayers::FrameConfig tl_config;
Peter Boström1436c832017-03-27 15:01:49 -0400211 // TODO(pbos): Consider referencing but not updating the 'alt' buffer for all
212 // layers.
213 switch (layer_state) {
sprang916170a2017-05-23 07:47:55 -0700214 case TemporalLayerState::kDrop:
pbos51f083c2017-05-04 06:39:04 -0700215 tl_config = TemporalLayers::FrameConfig(kNone, kNone, kNone);
216 break;
sprang916170a2017-05-23 07:47:55 -0700217 case TemporalLayerState::kTl0:
Peter Boström1436c832017-03-27 15:01:49 -0400218 // TL0 only references and updates 'last'.
pbos51f083c2017-05-04 06:39:04 -0700219 tl_config =
220 TemporalLayers::FrameConfig(kReferenceAndUpdate, kNone, kNone);
pbos1777c5f2017-07-19 17:04:02 -0700221 tl_config.packetizer_temporal_idx = 0;
pbos51f083c2017-05-04 06:39:04 -0700222 break;
sprang916170a2017-05-23 07:47:55 -0700223 case TemporalLayerState::kTl1:
Peter Boström1436c832017-03-27 15:01:49 -0400224 // TL1 references both 'last' and 'golden' but only updates 'golden'.
pbos51f083c2017-05-04 06:39:04 -0700225 tl_config =
226 TemporalLayers::FrameConfig(kReference, kReferenceAndUpdate, kNone);
pbos1777c5f2017-07-19 17:04:02 -0700227 tl_config.packetizer_temporal_idx = 1;
pbos51f083c2017-05-04 06:39:04 -0700228 break;
sprang916170a2017-05-23 07:47:55 -0700229 case TemporalLayerState::kTl1Sync:
Peter Boström1436c832017-03-27 15:01:49 -0400230 // Predict from only TL0 to allow participants to switch to the high
231 // bitrate stream. Updates 'golden' so that TL1 can continue to refer to
232 // and update 'golden' from this point on.
pbos51f083c2017-05-04 06:39:04 -0700233 tl_config = TemporalLayers::FrameConfig(kReference, kUpdate, kNone);
pbos1777c5f2017-07-19 17:04:02 -0700234 tl_config.packetizer_temporal_idx = 1;
pbos51f083c2017-05-04 06:39:04 -0700235 break;
Peter Boström1436c832017-03-27 15:01:49 -0400236 }
pbos51f083c2017-05-04 06:39:04 -0700237
pbos1777c5f2017-07-19 17:04:02 -0700238 tl_config.layer_sync = layer_state == TemporalLayerState::kTl1Sync;
pbos51f083c2017-05-04 06:39:04 -0700239 return tl_config;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000240}
241
Erik Språngbb60a3a2018-03-19 18:25:10 +0100242void ScreenshareLayers::OnRatesUpdated(
243 const std::vector<uint32_t>& bitrates_bps,
244 int framerate_fps) {
245 RTC_DCHECK_GT(framerate_fps, 0);
246 RTC_DCHECK_GE(bitrates_bps.size(), 1);
247 RTC_DCHECK_LE(bitrates_bps.size(), 2);
248
249 // |bitrates_bps| uses individual rates per layer, but we want to use the
250 // accumulated rate here.
251 uint32_t tl0_kbps = bitrates_bps[0] / 1000;
252 uint32_t tl1_kbps = tl0_kbps;
253 if (bitrates_bps.size() > 1) {
254 tl1_kbps += bitrates_bps[1] / 1000;
255 }
256
sprangac4a90d2016-12-28 05:58:07 -0800257 if (!target_framerate_) {
Erik Språngbb60a3a2018-03-19 18:25:10 +0100258 // First OnRatesUpdated() is called during construction, with the
259 // configured targets as parameters.
260 target_framerate_ = framerate_fps;
sprang0ad0de62017-01-11 05:01:32 -0800261 capture_framerate_ = target_framerate_;
sprangac4a90d2016-12-28 05:58:07 -0800262 bitrate_updated_ = true;
263 } else {
Erik Språngbb60a3a2018-03-19 18:25:10 +0100264 bitrate_updated_ = capture_framerate_ &&
265 framerate_fps != static_cast<int>(*capture_framerate_);
266 bitrate_updated_ |= tl0_kbps != layers_[0].target_rate_kbps_;
267 bitrate_updated_ |= tl1_kbps != layers_[1].target_rate_kbps_;
268
269 if (framerate_fps < 0) {
sprang0ad0de62017-01-11 05:01:32 -0800270 capture_framerate_.reset();
sprangac4a90d2016-12-28 05:58:07 -0800271 } else {
Erik Språngbb60a3a2018-03-19 18:25:10 +0100272 capture_framerate_ = framerate_fps;
sprangac4a90d2016-12-28 05:58:07 -0800273 }
274 }
275
Erik Språngbb60a3a2018-03-19 18:25:10 +0100276 layers_[0].target_rate_kbps_ = tl0_kbps;
277 layers_[1].target_rate_kbps_ = tl1_kbps;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000278}
279
Peter Boström1436c832017-03-27 15:01:49 -0400280void ScreenshareLayers::FrameEncoded(unsigned int size, int qp) {
sprangac4a90d2016-12-28 05:58:07 -0800281 if (size > 0)
282 encode_framerate_.Update(1, clock_->TimeInMilliseconds());
283
sprang2ddb8bd2016-02-04 03:59:52 -0800284 if (number_of_temporal_layers_ == 1)
285 return;
286
287 RTC_DCHECK_NE(-1, active_layer_);
Erik Språng2c4c9142015-06-24 11:24:44 +0200288 if (size == 0) {
289 layers_[active_layer_].state = TemporalLayer::State::kDropped;
sprangb0fdfea2016-03-01 05:51:16 -0800290 ++stats_.num_overshoots_;
Erik Språng2c4c9142015-06-24 11:24:44 +0200291 return;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000292 }
sprangef7228c2015-08-05 02:01:29 -0700293
Erik Språng2c4c9142015-06-24 11:24:44 +0200294 if (layers_[active_layer_].state == TemporalLayer::State::kDropped) {
295 layers_[active_layer_].state = TemporalLayer::State::kQualityBoost;
296 }
297
298 if (qp != -1)
299 layers_[active_layer_].last_qp = qp;
300
301 if (active_layer_ == 0) {
302 layers_[0].debt_bytes_ += size;
303 layers_[1].debt_bytes_ += size;
sprangb0fdfea2016-03-01 05:51:16 -0800304 ++stats_.num_tl0_frames_;
305 stats_.tl0_target_bitrate_sum_ += layers_[0].target_rate_kbps_;
306 stats_.tl0_qp_sum_ += qp;
Erik Språng2c4c9142015-06-24 11:24:44 +0200307 } else if (active_layer_ == 1) {
308 layers_[1].debt_bytes_ += size;
sprangb0fdfea2016-03-01 05:51:16 -0800309 ++stats_.num_tl1_frames_;
310 stats_.tl1_target_bitrate_sum_ += layers_[1].target_rate_kbps_;
311 stats_.tl1_qp_sum_ += qp;
Erik Språng2c4c9142015-06-24 11:24:44 +0200312 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000313}
314
pbos18ad1d42017-05-04 05:04:46 -0700315void ScreenshareLayers::PopulateCodecSpecific(
316 bool frame_is_keyframe,
317 const TemporalLayers::FrameConfig& tl_config,
318 CodecSpecificInfoVP8* vp8_info,
319 uint32_t timestamp) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000320 if (number_of_temporal_layers_ == 1) {
321 vp8_info->temporalIdx = kNoTemporalIdx;
322 vp8_info->layerSync = false;
323 vp8_info->tl0PicIdx = kNoTl0PicIdx;
324 } else {
pbos1777c5f2017-07-19 17:04:02 -0700325 int64_t unwrapped_timestamp = time_wrap_handler_.Unwrap(timestamp);
326 vp8_info->temporalIdx = tl_config.packetizer_temporal_idx;
327 vp8_info->layerSync = tl_config.layer_sync;
Peter Boström1436c832017-03-27 15:01:49 -0400328 if (frame_is_keyframe) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000329 vp8_info->temporalIdx = 0;
Erik Språng2c4c9142015-06-24 11:24:44 +0200330 last_sync_timestamp_ = unwrapped_timestamp;
pbos1777c5f2017-07-19 17:04:02 -0700331 vp8_info->layerSync = true;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000332 } else if (last_base_layer_sync_ && vp8_info->temporalIdx != 0) {
333 // Regardless of pattern the frame after a base layer sync will always
334 // be a layer sync.
Erik Språng2c4c9142015-06-24 11:24:44 +0200335 last_sync_timestamp_ = unwrapped_timestamp;
pbos1777c5f2017-07-19 17:04:02 -0700336 vp8_info->layerSync = true;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000337 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000338 if (vp8_info->temporalIdx == 0) {
339 tl0_pic_idx_++;
340 }
Peter Boström1436c832017-03-27 15:01:49 -0400341 last_base_layer_sync_ = frame_is_keyframe;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000342 vp8_info->tl0PicIdx = tl0_pic_idx_;
343 }
344}
345
Erik Språng2c4c9142015-06-24 11:24:44 +0200346bool ScreenshareLayers::TimeToSync(int64_t timestamp) const {
sprang2ddb8bd2016-02-04 03:59:52 -0800347 RTC_DCHECK_EQ(1, active_layer_);
henrikg91d6ede2015-09-17 00:24:34 -0700348 RTC_DCHECK_NE(-1, layers_[0].last_qp);
Erik Språng2c4c9142015-06-24 11:24:44 +0200349 if (layers_[1].last_qp == -1) {
350 // First frame in TL1 should only depend on TL0 since there are no
351 // previous frames in TL1.
352 return true;
353 }
354
henrikg91d6ede2015-09-17 00:24:34 -0700355 RTC_DCHECK_NE(-1, last_sync_timestamp_);
Erik Språng2c4c9142015-06-24 11:24:44 +0200356 int64_t timestamp_diff = timestamp - last_sync_timestamp_;
357 if (timestamp_diff > kMaxTimeBetweenSyncs) {
358 // After a certain time, force a sync frame.
359 return true;
360 } else if (timestamp_diff < kMinTimeBetweenSyncs) {
361 // If too soon from previous sync frame, don't issue a new one.
362 return false;
363 }
364 // Issue a sync frame if difference in quality between TL0 and TL1 isn't too
365 // large.
366 if (layers_[0].last_qp - layers_[1].last_qp < kQpDeltaThresholdForSync)
367 return true;
368 return false;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000369}
370
sprangc7805db2016-11-25 08:09:43 -0800371uint32_t ScreenshareLayers::GetCodecTargetBitrateKbps() const {
372 uint32_t target_bitrate_kbps = layers_[0].target_rate_kbps_;
373
374 if (number_of_temporal_layers_ > 1) {
375 // Calculate a codec target bitrate. This may be higher than TL0, gaining
376 // quality at the expense of frame rate at TL0. Constraints:
377 // - TL0 frame rate no less than framerate / kMaxTL0FpsReduction.
378 // - Target rate * kAcceptableTargetOvershoot should not exceed TL1 rate.
379 target_bitrate_kbps =
380 std::min(layers_[0].target_rate_kbps_ * kMaxTL0FpsReduction,
381 layers_[1].target_rate_kbps_ / kAcceptableTargetOvershoot);
382 }
383
384 return std::max(layers_[0].target_rate_kbps_, target_bitrate_kbps);
385}
386
Anders Carlssonbeabdcb2018-01-24 10:25:15 +0100387bool ScreenshareLayers::UpdateConfiguration(Vp8EncoderConfig* cfg) {
Erik Språng08127a92016-11-16 16:41:30 +0100388 bool cfg_updated = false;
sprangc7805db2016-11-25 08:09:43 -0800389 uint32_t target_bitrate_kbps = GetCodecTargetBitrateKbps();
Erik Språng27a457d2018-01-12 11:00:21 +0100390
391 // TODO(sprang): We _really_ need to make an overhaul of this class. :(
392 // If we're dropping frames in order to meet a target framerate, adjust the
393 // bitrate assigned to the encoder so the total average bitrate is correct.
394 float encoder_config_bitrate_kbps = target_bitrate_kbps;
395 if (target_framerate_ && capture_framerate_ &&
396 *target_framerate_ < *capture_framerate_) {
397 encoder_config_bitrate_kbps *=
398 static_cast<float>(*capture_framerate_) / *target_framerate_;
399 }
400
401 if (bitrate_updated_ ||
402 cfg->rc_target_bitrate != encoder_config_bitrate_kbps) {
403 cfg->rc_target_bitrate = encoder_config_bitrate_kbps;
Erik Språng08127a92016-11-16 16:41:30 +0100404
405 // Don't reconfigure qp limits during quality boost frames.
406 if (active_layer_ == -1 ||
407 layers_[active_layer_].state != TemporalLayer::State::kQualityBoost) {
408 min_qp_ = cfg->rc_min_quantizer;
409 max_qp_ = cfg->rc_max_quantizer;
410 // After a dropped frame, a frame with max qp will be encoded and the
411 // quality will then ramp up from there. To boost the speed of recovery,
sprang916170a2017-05-23 07:47:55 -0700412 // encode the next frame with lower max qp, if there is sufficient
413 // bandwidth to do so without causing excessive delay.
414 // TL0 is the most important to improve since the errors in this layer
415 // will propagate to TL1.
Erik Språng08127a92016-11-16 16:41:30 +0100416 // Currently, reduce max qp by 20% for TL0 and 15% for TL1.
sprang916170a2017-05-23 07:47:55 -0700417 if (layers_[1].target_rate_kbps_ >= kMinBitrateKbpsForQpBoost) {
418 layers_[0].enhanced_max_qp =
419 min_qp_ + (((max_qp_ - min_qp_) * 80) / 100);
420 layers_[1].enhanced_max_qp =
421 min_qp_ + (((max_qp_ - min_qp_) * 85) / 100);
422 } else {
423 layers_[0].enhanced_max_qp = -1;
424 layers_[1].enhanced_max_qp = -1;
425 }
Erik Språng08127a92016-11-16 16:41:30 +0100426 }
427
sprang0ad0de62017-01-11 05:01:32 -0800428 if (capture_framerate_) {
sprangac4a90d2016-12-28 05:58:07 -0800429 int avg_frame_size =
sprang0ad0de62017-01-11 05:01:32 -0800430 (target_bitrate_kbps * 1000) / (8 * *capture_framerate_);
sprang916170a2017-05-23 07:47:55 -0700431 // Allow max debt to be the size of a single optimal frame.
432 // TODO(sprang): Determine if this needs to be adjusted by some factor.
433 // (Lower values may cause more frame drops, higher may lead to queuing
434 // delays.)
435 max_debt_bytes_ = avg_frame_size;
Erik Språng08127a92016-11-16 16:41:30 +0100436 }
437
438 bitrate_updated_ = false;
439 cfg_updated = true;
440 }
441
442 // Don't try to update boosts state if not active yet.
443 if (active_layer_ == -1)
444 return cfg_updated;
445
sprangef7228c2015-08-05 02:01:29 -0700446 if (max_qp_ == -1 || number_of_temporal_layers_ <= 1)
Erik Språng08127a92016-11-16 16:41:30 +0100447 return cfg_updated;
Erik Språng2c4c9142015-06-24 11:24:44 +0200448
449 // If layer is in the quality boost state (following a dropped frame), update
450 // the configuration with the adjusted (lower) qp and set the state back to
451 // normal.
452 unsigned int adjusted_max_qp;
453 if (layers_[active_layer_].state == TemporalLayer::State::kQualityBoost &&
454 layers_[active_layer_].enhanced_max_qp != -1) {
455 adjusted_max_qp = layers_[active_layer_].enhanced_max_qp;
456 layers_[active_layer_].state = TemporalLayer::State::kNormal;
457 } else {
Erik Språng2c4c9142015-06-24 11:24:44 +0200458 adjusted_max_qp = max_qp_; // Set the normal max qp.
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000459 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200460
461 if (adjusted_max_qp == cfg->rc_max_quantizer)
Erik Språng08127a92016-11-16 16:41:30 +0100462 return cfg_updated;
Erik Språng2c4c9142015-06-24 11:24:44 +0200463
464 cfg->rc_max_quantizer = adjusted_max_qp;
Erik Språng08127a92016-11-16 16:41:30 +0100465 cfg_updated = true;
sprangc7805db2016-11-25 08:09:43 -0800466
Erik Språng08127a92016-11-16 16:41:30 +0100467 return cfg_updated;
Erik Språng2c4c9142015-06-24 11:24:44 +0200468}
469
470void ScreenshareLayers::TemporalLayer::UpdateDebt(int64_t delta_ms) {
471 uint32_t debt_reduction_bytes = target_rate_kbps_ * delta_ms / 8;
472 if (debt_reduction_bytes >= debt_bytes_) {
473 debt_bytes_ = 0;
474 } else {
475 debt_bytes_ -= debt_reduction_bytes;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000476 }
477}
sprang@webrtc.org70f74f32014-12-17 10:57:10 +0000478
sprangb0fdfea2016-03-01 05:51:16 -0800479void ScreenshareLayers::UpdateHistograms() {
480 if (stats_.first_frame_time_ms_ == -1)
481 return;
482 int64_t duration_sec =
483 (clock_->TimeInMilliseconds() - stats_.first_frame_time_ms_ + 500) / 1000;
484 if (duration_sec >= metrics::kMinRunTimeInSeconds) {
485 RTC_HISTOGRAM_COUNTS_10000(
486 "WebRTC.Video.Screenshare.Layer0.FrameRate",
487 (stats_.num_tl0_frames_ + (duration_sec / 2)) / duration_sec);
488 RTC_HISTOGRAM_COUNTS_10000(
489 "WebRTC.Video.Screenshare.Layer1.FrameRate",
490 (stats_.num_tl1_frames_ + (duration_sec / 2)) / duration_sec);
491 int total_frames = stats_.num_tl0_frames_ + stats_.num_tl1_frames_;
asapersson58d992e2016-03-29 02:15:06 -0700492 RTC_HISTOGRAM_COUNTS_10000(
493 "WebRTC.Video.Screenshare.FramesPerDrop",
Ilya Nikolaevskiybf352982017-10-02 10:08:25 +0200494 (stats_.num_dropped_frames_ == 0
495 ? 0
496 : total_frames / stats_.num_dropped_frames_));
asapersson58d992e2016-03-29 02:15:06 -0700497 RTC_HISTOGRAM_COUNTS_10000(
498 "WebRTC.Video.Screenshare.FramesPerOvershoot",
499 (stats_.num_overshoots_ == 0 ? 0
500 : total_frames / stats_.num_overshoots_));
sprangb0fdfea2016-03-01 05:51:16 -0800501 if (stats_.num_tl0_frames_ > 0) {
502 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.Screenshare.Layer0.Qp",
503 stats_.tl0_qp_sum_ / stats_.num_tl0_frames_);
504 RTC_HISTOGRAM_COUNTS_10000(
505 "WebRTC.Video.Screenshare.Layer0.TargetBitrate",
506 stats_.tl0_target_bitrate_sum_ / stats_.num_tl0_frames_);
507 }
508 if (stats_.num_tl1_frames_ > 0) {
509 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.Screenshare.Layer1.Qp",
510 stats_.tl1_qp_sum_ / stats_.num_tl1_frames_);
511 RTC_HISTOGRAM_COUNTS_10000(
512 "WebRTC.Video.Screenshare.Layer1.TargetBitrate",
513 stats_.tl1_target_bitrate_sum_ / stats_.num_tl1_frames_);
514 }
515 }
516}
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000517} // namespace webrtc