blob: 6b1bc7ec2515e232b31d49248537014c72e8e91b [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ång59021ba2018-10-03 11:05:16 +020012#include <map>
13#include <memory>
Erik Språng08127a92016-11-16 16:41:30 +010014#include <vector>
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000015
Elad Alon411b49b2019-01-29 14:05:55 +010016#include "api/video_codecs/vp8_frame_config.h"
Erik Språng4529fbc2018-10-12 10:30:31 +020017#include "api/video_codecs/vp8_temporal_layers.h"
Erik Språng59021ba2018-10-03 11:05:16 +020018#include "modules/video_coding/codecs/vp8/include/temporal_layers_checker.h"
philipel9df33532019-03-04 16:37:50 +010019#include "modules/video_coding/include/video_codec_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/video_coding/utility/frame_dropper.h"
21#include "rtc_base/rate_statistics.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/time_utils.h"
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000023
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000024namespace webrtc {
25
26struct CodecSpecificInfoVP8;
sprangb0fdfea2016-03-01 05:51:16 -080027class Clock;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000028
Elad Aloncde8ab22019-03-20 11:56:20 +010029class ScreenshareLayers final : public Vp8FrameBufferController {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000030 public:
sprang@webrtc.org70f74f32014-12-17 10:57:10 +000031 static const double kMaxTL0FpsReduction;
32 static const double kAcceptableTargetOvershoot;
sprangafe1f742016-04-12 02:45:13 -070033 static const int kMaxFrameIntervalMs;
sprang@webrtc.org70f74f32014-12-17 10:57:10 +000034
Sebastian Janssone64a6882019-03-01 18:04:07 +010035 explicit ScreenshareLayers(int num_temporal_layers);
Erik Språngfb2a66a2018-09-10 10:48:01 +020036 ~ScreenshareLayers() override;
37
Elad Aloncde8ab22019-03-20 11:56:20 +010038 size_t StreamCount() const override;
39
40 bool SupportsEncoderFrameDropping(size_t stream_index) const override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000041
42 // Returns the recommended VP8 encode flags needed. May refresh the decoder
43 // and/or update the reference buffers.
Elad Aloncde8ab22019-03-20 11:56:20 +010044 Vp8FrameConfig UpdateLayerConfig(size_t stream_index,
45 uint32_t rtp_timestamp) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000046
Erik Språngbb60a3a2018-03-19 18:25:10 +010047 // New target bitrate, per temporal layer.
Elad Aloncde8ab22019-03-20 11:56:20 +010048 void OnRatesUpdated(size_t stream_index,
49 const std::vector<uint32_t>& bitrates_bps,
Erik Språngbb60a3a2018-03-19 18:25:10 +010050 int framerate_fps) override;
Erik Språng08127a92016-11-16 16:41:30 +010051
52 // Update the encoder configuration with target bitrates or other parameters.
53 // Returns true iff the configuration was actually modified.
Elad Aloncde8ab22019-03-20 11:56:20 +010054 bool UpdateConfiguration(size_t stream_index, Vp8EncoderConfig* cfg) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000055
Elad Aloncde8ab22019-03-20 11:56:20 +010056 void OnEncodeDone(size_t stream_index,
57 uint32_t rtp_timestamp,
Erik Språng59021ba2018-10-03 11:05:16 +020058 size_t size_bytes,
59 bool is_keyframe,
60 int qp,
philipel9df33532019-03-04 16:37:50 +010061 CodecSpecificInfo* info) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000062
Elad Aloncde8ab22019-03-20 11:56:20 +010063 void OnPacketLossRateUpdate(float packet_loss_rate) override;
64
65 void OnRttUpdate(int64_t rtt_ms) override;
66
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000067 private:
sprang916170a2017-05-23 07:47:55 -070068 enum class TemporalLayerState : int { kDrop, kTl0, kTl1, kTl1Sync };
pbos51f083c2017-05-04 06:39:04 -070069
Erik Språng2c4c9142015-06-24 11:24:44 +020070 bool TimeToSync(int64_t timestamp) const;
sprangc7805db2016-11-25 08:09:43 -080071 uint32_t GetCodecTargetBitrateKbps() const;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000072
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000073 int number_of_temporal_layers_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000074 int active_layer_;
Erik Språng2c4c9142015-06-24 11:24:44 +020075 int64_t last_timestamp_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000076 int64_t last_sync_timestamp_;
sprangafe1f742016-04-12 02:45:13 -070077 int64_t last_emitted_tl0_timestamp_;
Erik Språng27a457d2018-01-12 11:00:21 +010078 int64_t last_frame_time_ms_;
Erik Språng2c4c9142015-06-24 11:24:44 +020079 rtc::TimestampWrapAroundHandler time_wrap_handler_;
80 int min_qp_;
81 int max_qp_;
82 uint32_t max_debt_bytes_;
sprangac4a90d2016-12-28 05:58:07 -080083
Elad Alon411b49b2019-01-29 14:05:55 +010084 std::map<uint32_t, Vp8FrameConfig> pending_frame_configs_;
Erik Språng59021ba2018-10-03 11:05:16 +020085
sprangac4a90d2016-12-28 05:58:07 -080086 // Configured max framerate.
Danil Chapovalov0040b662018-06-18 10:48:16 +020087 absl::optional<uint32_t> target_framerate_;
sprangac4a90d2016-12-28 05:58:07 -080088 // Incoming framerate from capturer.
Danil Chapovalov0040b662018-06-18 10:48:16 +020089 absl::optional<uint32_t> capture_framerate_;
philipel9df33532019-03-04 16:37:50 +010090
sprangac4a90d2016-12-28 05:58:07 -080091 // Tracks what framerate we actually encode, and drops frames on overshoot.
92 RateStatistics encode_framerate_;
Erik Språng08127a92016-11-16 16:41:30 +010093 bool bitrate_updated_;
Erik Språng2c4c9142015-06-24 11:24:44 +020094
sprang429600d2017-01-26 06:12:26 -080095 static constexpr int kMaxNumTemporalLayers = 2;
Erik Språng2c4c9142015-06-24 11:24:44 +020096 struct TemporalLayer {
97 TemporalLayer()
98 : state(State::kNormal),
99 enhanced_max_qp(-1),
100 last_qp(-1),
101 debt_bytes_(0),
102 target_rate_kbps_(0) {}
103
104 enum class State {
105 kNormal,
106 kDropped,
107 kReencoded,
108 kQualityBoost,
Erik Språngb75d6b82018-08-13 16:05:33 +0200109 kKeyFrame
Erik Språng2c4c9142015-06-24 11:24:44 +0200110 } state;
111
112 int enhanced_max_qp;
113 int last_qp;
114 uint32_t debt_bytes_;
115 uint32_t target_rate_kbps_;
116
117 void UpdateDebt(int64_t delta_ms);
118 } layers_[kMaxNumTemporalLayers];
sprangb0fdfea2016-03-01 05:51:16 -0800119
120 void UpdateHistograms();
philipel9df33532019-03-04 16:37:50 +0100121 TemplateStructure GetTemplateStructure(int num_layers) const;
122
sprangb0fdfea2016-03-01 05:51:16 -0800123 // Data for histogram statistics.
124 struct Stats {
125 int64_t first_frame_time_ms_ = -1;
126 int64_t num_tl0_frames_ = 0;
127 int64_t num_tl1_frames_ = 0;
128 int64_t num_dropped_frames_ = 0;
129 int64_t num_overshoots_ = 0;
130 int64_t tl0_qp_sum_ = 0;
131 int64_t tl1_qp_sum_ = 0;
132 int64_t tl0_target_bitrate_sum_ = 0;
133 int64_t tl1_target_bitrate_sum_ = 0;
134 } stats_;
Erik Språng59021ba2018-10-03 11:05:16 +0200135
136 // Optional utility used to verify reference validity.
137 std::unique_ptr<TemporalLayersChecker> checker_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000138};
139} // namespace webrtc
140
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200141#endif // MODULES_VIDEO_CODING_CODECS_VP8_SCREENSHARE_LAYERS_H_