blob: 8c63cf1a0800ad6e8bf55e23e36d5be64697d31a [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"
Ilya Nikolaevskiybf352982017-10-02 10:08:25 +020022#include "vpx/vp8cx.h"
23#include "vpx/vpx_encoder.h"
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000024
25namespace webrtc {
26
Erik Språng2c4c9142015-06-24 11:24:44 +020027static const int kOneSecond90Khz = 90000;
Erik Språng13044c12017-10-05 12:20:36 +020028static const int kMinTimeBetweenSyncs = kOneSecond90Khz * 2;
29static const int kMaxTimeBetweenSyncs = kOneSecond90Khz * 4;
Erik Språng2c4c9142015-06-24 11:24:44 +020030static const int kQpDeltaThresholdForSync = 8;
sprang916170a2017-05-23 07:47:55 -070031static const int kMinBitrateKbpsForQpBoost = 500;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000032
sprang@webrtc.org70f74f32014-12-17 10:57:10 +000033const double ScreenshareLayers::kMaxTL0FpsReduction = 2.5;
34const double ScreenshareLayers::kAcceptableTargetOvershoot = 2.0;
35
sprang429600d2017-01-26 06:12:26 -080036constexpr int ScreenshareLayers::kMaxNumTemporalLayers;
37
sprangafe1f742016-04-12 02:45:13 -070038// Always emit a frame with certain interval, even if bitrate targets have
sprang916170a2017-05-23 07:47:55 -070039// been exceeded. This prevents needless keyframe requests.
sprang6c4bbfa2017-05-30 10:08:23 -070040const int ScreenshareLayers::kMaxFrameIntervalMs = 2750;
sprangafe1f742016-04-12 02:45:13 -070041
Erik Språng08127a92016-11-16 16:41:30 +010042webrtc::TemporalLayers* ScreenshareTemporalLayersFactory::Create(
43 int simulcast_id,
44 int num_temporal_layers,
45 uint8_t initial_tl0_pic_idx) const {
sprang429600d2017-01-26 06:12:26 -080046 webrtc::TemporalLayers* tl;
47 if (simulcast_id == 0) {
48 tl = new webrtc::ScreenshareLayers(num_temporal_layers, rand(),
49 webrtc::Clock::GetRealTimeClock());
50 } else {
sprang5271ea62017-03-01 01:58:17 -080051 TemporalLayersFactory rt_tl_factory;
sprang429600d2017-01-26 06:12:26 -080052 tl = rt_tl_factory.Create(simulcast_id, num_temporal_layers, rand());
53 }
Erik Språng08127a92016-11-16 16:41:30 +010054 if (listener_)
55 listener_->OnTemporalLayersCreated(simulcast_id, tl);
56 return tl;
57}
58
Ilya Nikolaevskiybf352982017-10-02 10:08:25 +020059std::unique_ptr<webrtc::TemporalLayersChecker>
60ScreenshareTemporalLayersFactory::CreateChecker(
61 int simulcast_id,
62 int temporal_layers,
63 uint8_t initial_tl0_pic_idx) const {
64 webrtc::TemporalLayersChecker* tlc;
65 if (simulcast_id == 0) {
Ilya Nikolaevskiy58662912017-10-04 15:07:09 +020066 tlc =
67 new webrtc::TemporalLayersChecker(temporal_layers, initial_tl0_pic_idx);
Ilya Nikolaevskiybf352982017-10-02 10:08:25 +020068 } else {
69 TemporalLayersFactory rt_tl_factory;
70 return rt_tl_factory.CreateChecker(simulcast_id, temporal_layers,
71 initial_tl0_pic_idx);
72 }
73 return std::unique_ptr<webrtc::TemporalLayersChecker>(tlc);
74}
75
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000076ScreenshareLayers::ScreenshareLayers(int num_temporal_layers,
sprangb0fdfea2016-03-01 05:51:16 -080077 uint8_t initial_tl0_pic_idx,
78 Clock* clock)
79 : clock_(clock),
sprang429600d2017-01-26 06:12:26 -080080 number_of_temporal_layers_(
81 std::min(kMaxNumTemporalLayers, num_temporal_layers)),
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000082 last_base_layer_sync_(false),
83 tl0_pic_idx_(initial_tl0_pic_idx),
Erik Språng2c4c9142015-06-24 11:24:44 +020084 active_layer_(-1),
85 last_timestamp_(-1),
86 last_sync_timestamp_(-1),
sprangafe1f742016-04-12 02:45:13 -070087 last_emitted_tl0_timestamp_(-1),
Erik Språng27a457d2018-01-12 11:00:21 +010088 last_frame_time_ms_(-1),
Erik Språng2c4c9142015-06-24 11:24:44 +020089 min_qp_(-1),
90 max_qp_(-1),
91 max_debt_bytes_(0),
sprangac4a90d2016-12-28 05:58:07 -080092 encode_framerate_(1000.0f, 1000.0f), // 1 second window, second scale.
Erik Språng08127a92016-11-16 16:41:30 +010093 bitrate_updated_(false) {
sprang429600d2017-01-26 06:12:26 -080094 RTC_CHECK_GT(number_of_temporal_layers_, 0);
95 RTC_CHECK_LE(number_of_temporal_layers_, kMaxNumTemporalLayers);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000096}
97
sprangb0fdfea2016-03-01 05:51:16 -080098ScreenshareLayers::~ScreenshareLayers() {
99 UpdateHistograms();
100}
101
brandtr080830c2017-05-03 03:25:53 -0700102uint8_t ScreenshareLayers::Tl0PicIdx() const {
103 return tl0_pic_idx_;
104}
105
pbos18ad1d42017-05-04 05:04:46 -0700106TemporalLayers::FrameConfig ScreenshareLayers::UpdateLayerConfig(
107 uint32_t timestamp) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000108 if (number_of_temporal_layers_ <= 1) {
109 // No flags needed for 1 layer screenshare.
Peter Boström1436c832017-03-27 15:01:49 -0400110 // TODO(pbos): Consider updating only last, and not all buffers.
pbos51f083c2017-05-04 06:39:04 -0700111 TemporalLayers::FrameConfig tl_config(
112 kReferenceAndUpdate, kReferenceAndUpdate, kReferenceAndUpdate);
pbos51f083c2017-05-04 06:39:04 -0700113 return tl_config;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000114 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200115
sprangac4a90d2016-12-28 05:58:07 -0800116 const int64_t now_ms = clock_->TimeInMilliseconds();
sprangb0fdfea2016-03-01 05:51:16 -0800117
Erik Språng2c4c9142015-06-24 11:24:44 +0200118 int64_t unwrapped_timestamp = time_wrap_handler_.Unwrap(timestamp);
sprang916170a2017-05-23 07:47:55 -0700119 int64_t ts_diff;
120 if (last_timestamp_ == -1) {
121 ts_diff = kOneSecond90Khz / capture_framerate_.value_or(*target_framerate_);
122 } else {
123 ts_diff = unwrapped_timestamp - last_timestamp_;
124 }
Erik Språng27a457d2018-01-12 11:00:21 +0100125
126 if (target_framerate_) {
127 // If input frame rate exceeds target frame rate, either over a one second
128 // averaging window, or if frame interval is below 90% of desired value,
129 // drop frame.
Erik Språng27a457d2018-01-12 11:00:21 +0100130 if (encode_framerate_.Rate(now_ms).value_or(0) > *target_framerate_)
131 return TemporalLayers::FrameConfig(kNone, kNone, kNone);
132
Erik Språngdb9e9d52018-01-22 09:23:11 -0800133 // Primarily check if frame interval is too short using frame timestamps,
134 // as if they are correct they won't be affected by queuing in webrtc.
135 const int64_t expected_frame_interval_90khz =
136 kOneSecond90Khz / *target_framerate_;
137 if (last_timestamp_ != -1 && ts_diff > 0) {
138 if (ts_diff < 85 * expected_frame_interval_90khz / 100) {
139 return TemporalLayers::FrameConfig(kNone, kNone, kNone);
140 }
141 } else {
142 // Timestamps looks off, use realtime clock here instead.
143 const int64_t expected_frame_interval_ms = 1000 / *target_framerate_;
144 if (last_frame_time_ms_ != -1 &&
145 now_ms - last_frame_time_ms_ <
146 (85 * expected_frame_interval_ms) / 100) {
147 return TemporalLayers::FrameConfig(kNone, kNone, kNone);
148 }
Erik Språng27a457d2018-01-12 11:00:21 +0100149 }
150 }
151
152 if (stats_.first_frame_time_ms_ == -1)
153 stats_.first_frame_time_ms_ = now_ms;
154
sprang916170a2017-05-23 07:47:55 -0700155 // Make sure both frame droppers leak out bits.
156 layers_[0].UpdateDebt(ts_diff / 90);
157 layers_[1].UpdateDebt(ts_diff / 90);
158 last_timestamp_ = timestamp;
Erik Språng27a457d2018-01-12 11:00:21 +0100159 last_frame_time_ms_ = now_ms;
sprang916170a2017-05-23 07:47:55 -0700160
161 TemporalLayerState layer_state = TemporalLayerState::kDrop;
162
Erik Språng2c4c9142015-06-24 11:24:44 +0200163 if (active_layer_ == -1 ||
164 layers_[active_layer_].state != TemporalLayer::State::kDropped) {
sprangafe1f742016-04-12 02:45:13 -0700165 if (last_emitted_tl0_timestamp_ != -1 &&
166 (unwrapped_timestamp - last_emitted_tl0_timestamp_) / 90 >
167 kMaxFrameIntervalMs) {
168 // Too long time has passed since the last frame was emitted, cancel
169 // enough debt to allow a single frame.
170 layers_[0].debt_bytes_ = max_debt_bytes_ - 1;
171 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200172 if (layers_[0].debt_bytes_ > max_debt_bytes_) {
173 // Must drop TL0, encode TL1 instead.
174 if (layers_[1].debt_bytes_ > max_debt_bytes_) {
175 // Must drop both TL0 and TL1.
176 active_layer_ = -1;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000177 } else {
Erik Språng2c4c9142015-06-24 11:24:44 +0200178 active_layer_ = 1;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000179 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200180 } else {
181 active_layer_ = 0;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000182 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000183 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200184
185 switch (active_layer_) {
186 case 0:
sprang916170a2017-05-23 07:47:55 -0700187 layer_state = TemporalLayerState::kTl0;
sprangafe1f742016-04-12 02:45:13 -0700188 last_emitted_tl0_timestamp_ = unwrapped_timestamp;
Erik Språng2c4c9142015-06-24 11:24:44 +0200189 break;
190 case 1:
sprangf03ea042017-07-13 03:53:51 -0700191 if (layers_[1].state != TemporalLayer::State::kDropped) {
192 if (TimeToSync(unwrapped_timestamp)) {
193 last_sync_timestamp_ = unwrapped_timestamp;
194 layer_state = TemporalLayerState::kTl1Sync;
195 } else {
196 layer_state = TemporalLayerState::kTl1;
197 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200198 } else {
sprangf03ea042017-07-13 03:53:51 -0700199 layer_state = last_sync_timestamp_ == unwrapped_timestamp
200 ? TemporalLayerState::kTl1Sync
201 : TemporalLayerState::kTl1;
Erik Språng2c4c9142015-06-24 11:24:44 +0200202 }
203 break;
204 case -1:
sprang916170a2017-05-23 07:47:55 -0700205 layer_state = TemporalLayerState::kDrop;
sprangb0fdfea2016-03-01 05:51:16 -0800206 ++stats_.num_dropped_frames_;
Erik Språng2c4c9142015-06-24 11:24:44 +0200207 break;
208 default:
Erik Språng2c4c9142015-06-24 11:24:44 +0200209 RTC_NOTREACHED();
210 }
211
pbos51f083c2017-05-04 06:39:04 -0700212 TemporalLayers::FrameConfig tl_config;
Peter Boström1436c832017-03-27 15:01:49 -0400213 // TODO(pbos): Consider referencing but not updating the 'alt' buffer for all
214 // layers.
215 switch (layer_state) {
sprang916170a2017-05-23 07:47:55 -0700216 case TemporalLayerState::kDrop:
pbos51f083c2017-05-04 06:39:04 -0700217 tl_config = TemporalLayers::FrameConfig(kNone, kNone, kNone);
218 break;
sprang916170a2017-05-23 07:47:55 -0700219 case TemporalLayerState::kTl0:
Peter Boström1436c832017-03-27 15:01:49 -0400220 // TL0 only references and updates 'last'.
pbos51f083c2017-05-04 06:39:04 -0700221 tl_config =
222 TemporalLayers::FrameConfig(kReferenceAndUpdate, kNone, kNone);
pbos1777c5f2017-07-19 17:04:02 -0700223 tl_config.packetizer_temporal_idx = 0;
pbos51f083c2017-05-04 06:39:04 -0700224 break;
sprang916170a2017-05-23 07:47:55 -0700225 case TemporalLayerState::kTl1:
Peter Boström1436c832017-03-27 15:01:49 -0400226 // TL1 references both 'last' and 'golden' but only updates 'golden'.
pbos51f083c2017-05-04 06:39:04 -0700227 tl_config =
228 TemporalLayers::FrameConfig(kReference, kReferenceAndUpdate, kNone);
pbos1777c5f2017-07-19 17:04:02 -0700229 tl_config.packetizer_temporal_idx = 1;
pbos51f083c2017-05-04 06:39:04 -0700230 break;
sprang916170a2017-05-23 07:47:55 -0700231 case TemporalLayerState::kTl1Sync:
Peter Boström1436c832017-03-27 15:01:49 -0400232 // Predict from only TL0 to allow participants to switch to the high
233 // bitrate stream. Updates 'golden' so that TL1 can continue to refer to
234 // and update 'golden' from this point on.
pbos51f083c2017-05-04 06:39:04 -0700235 tl_config = TemporalLayers::FrameConfig(kReference, kUpdate, kNone);
pbos1777c5f2017-07-19 17:04:02 -0700236 tl_config.packetizer_temporal_idx = 1;
pbos51f083c2017-05-04 06:39:04 -0700237 break;
Peter Boström1436c832017-03-27 15:01:49 -0400238 }
pbos51f083c2017-05-04 06:39:04 -0700239
pbos1777c5f2017-07-19 17:04:02 -0700240 tl_config.layer_sync = layer_state == TemporalLayerState::kTl1Sync;
pbos51f083c2017-05-04 06:39:04 -0700241 return tl_config;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000242}
243
Erik Språng08127a92016-11-16 16:41:30 +0100244std::vector<uint32_t> ScreenshareLayers::OnRatesUpdated(int bitrate_kbps,
245 int max_bitrate_kbps,
246 int framerate) {
sprangac4a90d2016-12-28 05:58:07 -0800247 RTC_DCHECK_GT(framerate, 0);
248 if (!target_framerate_) {
249 // First OnRatesUpdated() is called during construction, with the configured
250 // targets as parameters.
251 target_framerate_.emplace(framerate);
sprang0ad0de62017-01-11 05:01:32 -0800252 capture_framerate_ = target_framerate_;
sprangac4a90d2016-12-28 05:58:07 -0800253 bitrate_updated_ = true;
254 } else {
255 bitrate_updated_ =
256 bitrate_kbps != static_cast<int>(layers_[0].target_rate_kbps_) ||
257 max_bitrate_kbps != static_cast<int>(layers_[1].target_rate_kbps_) ||
sprang0ad0de62017-01-11 05:01:32 -0800258 (capture_framerate_ &&
259 framerate != static_cast<int>(*capture_framerate_));
sprangac4a90d2016-12-28 05:58:07 -0800260 if (framerate < 0) {
sprang0ad0de62017-01-11 05:01:32 -0800261 capture_framerate_.reset();
sprangac4a90d2016-12-28 05:58:07 -0800262 } else {
sprang0ad0de62017-01-11 05:01:32 -0800263 capture_framerate_.emplace(framerate);
sprangac4a90d2016-12-28 05:58:07 -0800264 }
265 }
266
Erik Språng2c4c9142015-06-24 11:24:44 +0200267 layers_[0].target_rate_kbps_ = bitrate_kbps;
268 layers_[1].target_rate_kbps_ = max_bitrate_kbps;
pbos@webrtc.org89042902015-03-20 12:49:38 +0000269
Erik Språng08127a92016-11-16 16:41:30 +0100270 std::vector<uint32_t> allocation;
271 allocation.push_back(bitrate_kbps);
272 if (max_bitrate_kbps > bitrate_kbps)
273 allocation.push_back(max_bitrate_kbps - bitrate_kbps);
274 return allocation;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000275}
276
Peter Boström1436c832017-03-27 15:01:49 -0400277void ScreenshareLayers::FrameEncoded(unsigned int size, int qp) {
sprangac4a90d2016-12-28 05:58:07 -0800278 if (size > 0)
279 encode_framerate_.Update(1, clock_->TimeInMilliseconds());
280
sprang2ddb8bd2016-02-04 03:59:52 -0800281 if (number_of_temporal_layers_ == 1)
282 return;
283
284 RTC_DCHECK_NE(-1, active_layer_);
Erik Språng2c4c9142015-06-24 11:24:44 +0200285 if (size == 0) {
286 layers_[active_layer_].state = TemporalLayer::State::kDropped;
sprangb0fdfea2016-03-01 05:51:16 -0800287 ++stats_.num_overshoots_;
Erik Språng2c4c9142015-06-24 11:24:44 +0200288 return;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000289 }
sprangef7228c2015-08-05 02:01:29 -0700290
Erik Språng2c4c9142015-06-24 11:24:44 +0200291 if (layers_[active_layer_].state == TemporalLayer::State::kDropped) {
292 layers_[active_layer_].state = TemporalLayer::State::kQualityBoost;
293 }
294
295 if (qp != -1)
296 layers_[active_layer_].last_qp = qp;
297
298 if (active_layer_ == 0) {
299 layers_[0].debt_bytes_ += size;
300 layers_[1].debt_bytes_ += size;
sprangb0fdfea2016-03-01 05:51:16 -0800301 ++stats_.num_tl0_frames_;
302 stats_.tl0_target_bitrate_sum_ += layers_[0].target_rate_kbps_;
303 stats_.tl0_qp_sum_ += qp;
Erik Språng2c4c9142015-06-24 11:24:44 +0200304 } else if (active_layer_ == 1) {
305 layers_[1].debt_bytes_ += size;
sprangb0fdfea2016-03-01 05:51:16 -0800306 ++stats_.num_tl1_frames_;
307 stats_.tl1_target_bitrate_sum_ += layers_[1].target_rate_kbps_;
308 stats_.tl1_qp_sum_ += qp;
Erik Språng2c4c9142015-06-24 11:24:44 +0200309 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000310}
311
pbos18ad1d42017-05-04 05:04:46 -0700312void ScreenshareLayers::PopulateCodecSpecific(
313 bool frame_is_keyframe,
314 const TemporalLayers::FrameConfig& tl_config,
315 CodecSpecificInfoVP8* vp8_info,
316 uint32_t timestamp) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000317 if (number_of_temporal_layers_ == 1) {
318 vp8_info->temporalIdx = kNoTemporalIdx;
319 vp8_info->layerSync = false;
320 vp8_info->tl0PicIdx = kNoTl0PicIdx;
321 } else {
pbos1777c5f2017-07-19 17:04:02 -0700322 int64_t unwrapped_timestamp = time_wrap_handler_.Unwrap(timestamp);
323 vp8_info->temporalIdx = tl_config.packetizer_temporal_idx;
324 vp8_info->layerSync = tl_config.layer_sync;
Peter Boström1436c832017-03-27 15:01:49 -0400325 if (frame_is_keyframe) {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000326 vp8_info->temporalIdx = 0;
Erik Språng2c4c9142015-06-24 11:24:44 +0200327 last_sync_timestamp_ = unwrapped_timestamp;
pbos1777c5f2017-07-19 17:04:02 -0700328 vp8_info->layerSync = true;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000329 } else if (last_base_layer_sync_ && vp8_info->temporalIdx != 0) {
330 // Regardless of pattern the frame after a base layer sync will always
331 // be a layer sync.
Erik Språng2c4c9142015-06-24 11:24:44 +0200332 last_sync_timestamp_ = unwrapped_timestamp;
pbos1777c5f2017-07-19 17:04:02 -0700333 vp8_info->layerSync = true;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000334 }
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000335 if (vp8_info->temporalIdx == 0) {
336 tl0_pic_idx_++;
337 }
Peter Boström1436c832017-03-27 15:01:49 -0400338 last_base_layer_sync_ = frame_is_keyframe;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000339 vp8_info->tl0PicIdx = tl0_pic_idx_;
340 }
341}
342
Erik Språng2c4c9142015-06-24 11:24:44 +0200343bool ScreenshareLayers::TimeToSync(int64_t timestamp) const {
sprang2ddb8bd2016-02-04 03:59:52 -0800344 RTC_DCHECK_EQ(1, active_layer_);
henrikg91d6ede2015-09-17 00:24:34 -0700345 RTC_DCHECK_NE(-1, layers_[0].last_qp);
Erik Språng2c4c9142015-06-24 11:24:44 +0200346 if (layers_[1].last_qp == -1) {
347 // First frame in TL1 should only depend on TL0 since there are no
348 // previous frames in TL1.
349 return true;
350 }
351
henrikg91d6ede2015-09-17 00:24:34 -0700352 RTC_DCHECK_NE(-1, last_sync_timestamp_);
Erik Språng2c4c9142015-06-24 11:24:44 +0200353 int64_t timestamp_diff = timestamp - last_sync_timestamp_;
354 if (timestamp_diff > kMaxTimeBetweenSyncs) {
355 // After a certain time, force a sync frame.
356 return true;
357 } else if (timestamp_diff < kMinTimeBetweenSyncs) {
358 // If too soon from previous sync frame, don't issue a new one.
359 return false;
360 }
361 // Issue a sync frame if difference in quality between TL0 and TL1 isn't too
362 // large.
363 if (layers_[0].last_qp - layers_[1].last_qp < kQpDeltaThresholdForSync)
364 return true;
365 return false;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000366}
367
sprangc7805db2016-11-25 08:09:43 -0800368uint32_t ScreenshareLayers::GetCodecTargetBitrateKbps() const {
369 uint32_t target_bitrate_kbps = layers_[0].target_rate_kbps_;
370
371 if (number_of_temporal_layers_ > 1) {
372 // Calculate a codec target bitrate. This may be higher than TL0, gaining
373 // quality at the expense of frame rate at TL0. Constraints:
374 // - TL0 frame rate no less than framerate / kMaxTL0FpsReduction.
375 // - Target rate * kAcceptableTargetOvershoot should not exceed TL1 rate.
376 target_bitrate_kbps =
377 std::min(layers_[0].target_rate_kbps_ * kMaxTL0FpsReduction,
378 layers_[1].target_rate_kbps_ / kAcceptableTargetOvershoot);
379 }
380
381 return std::max(layers_[0].target_rate_kbps_, target_bitrate_kbps);
382}
383
Erik Språng2c4c9142015-06-24 11:24:44 +0200384bool ScreenshareLayers::UpdateConfiguration(vpx_codec_enc_cfg_t* cfg) {
Erik Språng08127a92016-11-16 16:41:30 +0100385 bool cfg_updated = false;
sprangc7805db2016-11-25 08:09:43 -0800386 uint32_t target_bitrate_kbps = GetCodecTargetBitrateKbps();
Erik Språng27a457d2018-01-12 11:00:21 +0100387
388 // TODO(sprang): We _really_ need to make an overhaul of this class. :(
389 // If we're dropping frames in order to meet a target framerate, adjust the
390 // bitrate assigned to the encoder so the total average bitrate is correct.
391 float encoder_config_bitrate_kbps = target_bitrate_kbps;
392 if (target_framerate_ && capture_framerate_ &&
393 *target_framerate_ < *capture_framerate_) {
394 encoder_config_bitrate_kbps *=
395 static_cast<float>(*capture_framerate_) / *target_framerate_;
396 }
397
398 if (bitrate_updated_ ||
399 cfg->rc_target_bitrate != encoder_config_bitrate_kbps) {
400 cfg->rc_target_bitrate = encoder_config_bitrate_kbps;
Erik Språng08127a92016-11-16 16:41:30 +0100401
402 // Don't reconfigure qp limits during quality boost frames.
403 if (active_layer_ == -1 ||
404 layers_[active_layer_].state != TemporalLayer::State::kQualityBoost) {
405 min_qp_ = cfg->rc_min_quantizer;
406 max_qp_ = cfg->rc_max_quantizer;
407 // After a dropped frame, a frame with max qp will be encoded and the
408 // quality will then ramp up from there. To boost the speed of recovery,
sprang916170a2017-05-23 07:47:55 -0700409 // encode the next frame with lower max qp, if there is sufficient
410 // bandwidth to do so without causing excessive delay.
411 // TL0 is the most important to improve since the errors in this layer
412 // will propagate to TL1.
Erik Språng08127a92016-11-16 16:41:30 +0100413 // Currently, reduce max qp by 20% for TL0 and 15% for TL1.
sprang916170a2017-05-23 07:47:55 -0700414 if (layers_[1].target_rate_kbps_ >= kMinBitrateKbpsForQpBoost) {
415 layers_[0].enhanced_max_qp =
416 min_qp_ + (((max_qp_ - min_qp_) * 80) / 100);
417 layers_[1].enhanced_max_qp =
418 min_qp_ + (((max_qp_ - min_qp_) * 85) / 100);
419 } else {
420 layers_[0].enhanced_max_qp = -1;
421 layers_[1].enhanced_max_qp = -1;
422 }
Erik Språng08127a92016-11-16 16:41:30 +0100423 }
424
sprang0ad0de62017-01-11 05:01:32 -0800425 if (capture_framerate_) {
sprangac4a90d2016-12-28 05:58:07 -0800426 int avg_frame_size =
sprang0ad0de62017-01-11 05:01:32 -0800427 (target_bitrate_kbps * 1000) / (8 * *capture_framerate_);
sprang916170a2017-05-23 07:47:55 -0700428 // Allow max debt to be the size of a single optimal frame.
429 // TODO(sprang): Determine if this needs to be adjusted by some factor.
430 // (Lower values may cause more frame drops, higher may lead to queuing
431 // delays.)
432 max_debt_bytes_ = avg_frame_size;
Erik Språng08127a92016-11-16 16:41:30 +0100433 }
434
435 bitrate_updated_ = false;
436 cfg_updated = true;
437 }
438
439 // Don't try to update boosts state if not active yet.
440 if (active_layer_ == -1)
441 return cfg_updated;
442
sprangef7228c2015-08-05 02:01:29 -0700443 if (max_qp_ == -1 || number_of_temporal_layers_ <= 1)
Erik Språng08127a92016-11-16 16:41:30 +0100444 return cfg_updated;
Erik Språng2c4c9142015-06-24 11:24:44 +0200445
446 // If layer is in the quality boost state (following a dropped frame), update
447 // the configuration with the adjusted (lower) qp and set the state back to
448 // normal.
449 unsigned int adjusted_max_qp;
450 if (layers_[active_layer_].state == TemporalLayer::State::kQualityBoost &&
451 layers_[active_layer_].enhanced_max_qp != -1) {
452 adjusted_max_qp = layers_[active_layer_].enhanced_max_qp;
453 layers_[active_layer_].state = TemporalLayer::State::kNormal;
454 } else {
Erik Språng2c4c9142015-06-24 11:24:44 +0200455 adjusted_max_qp = max_qp_; // Set the normal max qp.
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000456 }
Erik Språng2c4c9142015-06-24 11:24:44 +0200457
458 if (adjusted_max_qp == cfg->rc_max_quantizer)
Erik Språng08127a92016-11-16 16:41:30 +0100459 return cfg_updated;
Erik Språng2c4c9142015-06-24 11:24:44 +0200460
461 cfg->rc_max_quantizer = adjusted_max_qp;
Erik Språng08127a92016-11-16 16:41:30 +0100462 cfg_updated = true;
sprangc7805db2016-11-25 08:09:43 -0800463
Erik Språng08127a92016-11-16 16:41:30 +0100464 return cfg_updated;
Erik Språng2c4c9142015-06-24 11:24:44 +0200465}
466
467void ScreenshareLayers::TemporalLayer::UpdateDebt(int64_t delta_ms) {
468 uint32_t debt_reduction_bytes = target_rate_kbps_ * delta_ms / 8;
469 if (debt_reduction_bytes >= debt_bytes_) {
470 debt_bytes_ = 0;
471 } else {
472 debt_bytes_ -= debt_reduction_bytes;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000473 }
474}
sprang@webrtc.org70f74f32014-12-17 10:57:10 +0000475
sprangb0fdfea2016-03-01 05:51:16 -0800476void ScreenshareLayers::UpdateHistograms() {
477 if (stats_.first_frame_time_ms_ == -1)
478 return;
479 int64_t duration_sec =
480 (clock_->TimeInMilliseconds() - stats_.first_frame_time_ms_ + 500) / 1000;
481 if (duration_sec >= metrics::kMinRunTimeInSeconds) {
482 RTC_HISTOGRAM_COUNTS_10000(
483 "WebRTC.Video.Screenshare.Layer0.FrameRate",
484 (stats_.num_tl0_frames_ + (duration_sec / 2)) / duration_sec);
485 RTC_HISTOGRAM_COUNTS_10000(
486 "WebRTC.Video.Screenshare.Layer1.FrameRate",
487 (stats_.num_tl1_frames_ + (duration_sec / 2)) / duration_sec);
488 int total_frames = stats_.num_tl0_frames_ + stats_.num_tl1_frames_;
asapersson58d992e2016-03-29 02:15:06 -0700489 RTC_HISTOGRAM_COUNTS_10000(
490 "WebRTC.Video.Screenshare.FramesPerDrop",
Ilya Nikolaevskiybf352982017-10-02 10:08:25 +0200491 (stats_.num_dropped_frames_ == 0
492 ? 0
493 : total_frames / stats_.num_dropped_frames_));
asapersson58d992e2016-03-29 02:15:06 -0700494 RTC_HISTOGRAM_COUNTS_10000(
495 "WebRTC.Video.Screenshare.FramesPerOvershoot",
496 (stats_.num_overshoots_ == 0 ? 0
497 : total_frames / stats_.num_overshoots_));
sprangb0fdfea2016-03-01 05:51:16 -0800498 if (stats_.num_tl0_frames_ > 0) {
499 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.Screenshare.Layer0.Qp",
500 stats_.tl0_qp_sum_ / stats_.num_tl0_frames_);
501 RTC_HISTOGRAM_COUNTS_10000(
502 "WebRTC.Video.Screenshare.Layer0.TargetBitrate",
503 stats_.tl0_target_bitrate_sum_ / stats_.num_tl0_frames_);
504 }
505 if (stats_.num_tl1_frames_ > 0) {
506 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.Screenshare.Layer1.Qp",
507 stats_.tl1_qp_sum_ / stats_.num_tl1_frames_);
508 RTC_HISTOGRAM_COUNTS_10000(
509 "WebRTC.Video.Screenshare.Layer1.TargetBitrate",
510 stats_.tl1_target_bitrate_sum_ / stats_.num_tl1_frames_);
511 }
512 }
513}
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000514} // namespace webrtc