blob: 953b00d60d5f34d80038f7d5e12b554bdc64e73e [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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +02009#ifndef MODULES_VIDEO_CODING_CODECS_VP8_SCREENSHARE_LAYERS_H_
10#define MODULES_VIDEO_CODING_CODECS_VP8_SCREENSHARE_LAYERS_H_
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000011
Erik Språng08127a92016-11-16 16:41:30 +010012#include <vector>
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/video_coding/codecs/vp8/temporal_layers.h"
15#include "modules/video_coding/utility/frame_dropper.h"
16#include "rtc_base/rate_statistics.h"
17#include "rtc_base/timeutils.h"
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000018
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000019namespace webrtc {
20
21struct CodecSpecificInfoVP8;
sprangb0fdfea2016-03-01 05:51:16 -080022class Clock;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000023
24class ScreenshareLayers : public TemporalLayers {
25 public:
sprang@webrtc.org70f74f32014-12-17 10:57:10 +000026 static const double kMaxTL0FpsReduction;
27 static const double kAcceptableTargetOvershoot;
sprangafe1f742016-04-12 02:45:13 -070028 static const int kMaxFrameIntervalMs;
sprang@webrtc.org70f74f32014-12-17 10:57:10 +000029
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020030 ScreenshareLayers(int num_temporal_layers,
31 Clock* clock);
sprangb0fdfea2016-03-01 05:51:16 -080032 virtual ~ScreenshareLayers();
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000033
34 // Returns the recommended VP8 encode flags needed. May refresh the decoder
35 // and/or update the reference buffers.
pbos18ad1d42017-05-04 05:04:46 -070036 TemporalLayers::FrameConfig UpdateLayerConfig(uint32_t timestamp) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000037
Erik Språngbb60a3a2018-03-19 18:25:10 +010038 // New target bitrate, per temporal layer.
39 void OnRatesUpdated(const std::vector<uint32_t>& bitrates_bps,
40 int framerate_fps) override;
Erik Språng08127a92016-11-16 16:41:30 +010041
42 // Update the encoder configuration with target bitrates or other parameters.
43 // Returns true iff the configuration was actually modified.
Anders Carlssonbeabdcb2018-01-24 10:25:15 +010044 bool UpdateConfiguration(Vp8EncoderConfig* cfg) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000045
Erik Språng2c4c9142015-06-24 11:24:44 +020046 void PopulateCodecSpecific(bool base_layer_sync,
pbos18ad1d42017-05-04 05:04:46 -070047 const TemporalLayers::FrameConfig& tl_config,
Erik Språng2c4c9142015-06-24 11:24:44 +020048 CodecSpecificInfoVP8* vp8_info,
49 uint32_t timestamp) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000050
Peter Boström1436c832017-03-27 15:01:49 -040051 void FrameEncoded(unsigned int size, int qp) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000052
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000053 private:
sprang916170a2017-05-23 07:47:55 -070054 enum class TemporalLayerState : int { kDrop, kTl0, kTl1, kTl1Sync };
pbos51f083c2017-05-04 06:39:04 -070055
Erik Språng2c4c9142015-06-24 11:24:44 +020056 bool TimeToSync(int64_t timestamp) const;
sprangc7805db2016-11-25 08:09:43 -080057 uint32_t GetCodecTargetBitrateKbps() const;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000058
sprangb0fdfea2016-03-01 05:51:16 -080059 Clock* const clock_;
60
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000061 int number_of_temporal_layers_;
62 bool last_base_layer_sync_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000063 int active_layer_;
Erik Språng2c4c9142015-06-24 11:24:44 +020064 int64_t last_timestamp_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000065 int64_t last_sync_timestamp_;
sprangafe1f742016-04-12 02:45:13 -070066 int64_t last_emitted_tl0_timestamp_;
Erik Språng27a457d2018-01-12 11:00:21 +010067 int64_t last_frame_time_ms_;
Erik Språng2c4c9142015-06-24 11:24:44 +020068 rtc::TimestampWrapAroundHandler time_wrap_handler_;
69 int min_qp_;
70 int max_qp_;
71 uint32_t max_debt_bytes_;
sprangac4a90d2016-12-28 05:58:07 -080072
73 // Configured max framerate.
Danil Chapovalov0040b662018-06-18 10:48:16 +020074 absl::optional<uint32_t> target_framerate_;
sprangac4a90d2016-12-28 05:58:07 -080075 // Incoming framerate from capturer.
Danil Chapovalov0040b662018-06-18 10:48:16 +020076 absl::optional<uint32_t> capture_framerate_;
sprangac4a90d2016-12-28 05:58:07 -080077 // Tracks what framerate we actually encode, and drops frames on overshoot.
78 RateStatistics encode_framerate_;
Erik Språng08127a92016-11-16 16:41:30 +010079 bool bitrate_updated_;
Erik Språng2c4c9142015-06-24 11:24:44 +020080
sprang429600d2017-01-26 06:12:26 -080081 static constexpr int kMaxNumTemporalLayers = 2;
Erik Språng2c4c9142015-06-24 11:24:44 +020082 struct TemporalLayer {
83 TemporalLayer()
84 : state(State::kNormal),
85 enhanced_max_qp(-1),
86 last_qp(-1),
87 debt_bytes_(0),
88 target_rate_kbps_(0) {}
89
90 enum class State {
91 kNormal,
92 kDropped,
93 kReencoded,
94 kQualityBoost,
95 } state;
96
97 int enhanced_max_qp;
98 int last_qp;
99 uint32_t debt_bytes_;
100 uint32_t target_rate_kbps_;
101
102 void UpdateDebt(int64_t delta_ms);
103 } layers_[kMaxNumTemporalLayers];
sprangb0fdfea2016-03-01 05:51:16 -0800104
105 void UpdateHistograms();
106 // Data for histogram statistics.
107 struct Stats {
108 int64_t first_frame_time_ms_ = -1;
109 int64_t num_tl0_frames_ = 0;
110 int64_t num_tl1_frames_ = 0;
111 int64_t num_dropped_frames_ = 0;
112 int64_t num_overshoots_ = 0;
113 int64_t tl0_qp_sum_ = 0;
114 int64_t tl1_qp_sum_ = 0;
115 int64_t tl0_target_bitrate_sum_ = 0;
116 int64_t tl1_target_bitrate_sum_ = 0;
117 } stats_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000118};
119} // namespace webrtc
120
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200121#endif // MODULES_VIDEO_CODING_CODECS_VP8_SCREENSHARE_LAYERS_H_