blob: acaa6d82d7e764aa58eb8fad36a26c7ebf5de1a1 [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
Erik Språng4529fbc2018-10-12 10:30:31 +020016#include "api/video_codecs/vp8_temporal_layers.h"
Erik Språng59021ba2018-10-03 11:05:16 +020017#include "modules/video_coding/codecs/vp8/include/temporal_layers_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/video_coding/utility/frame_dropper.h"
19#include "rtc_base/rate_statistics.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/time_utils.h"
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000021
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000022namespace webrtc {
23
24struct CodecSpecificInfoVP8;
sprangb0fdfea2016-03-01 05:51:16 -080025class Clock;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000026
Erik Språng4529fbc2018-10-12 10:30:31 +020027class ScreenshareLayers : public Vp8TemporalLayers {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000028 public:
sprang@webrtc.org70f74f32014-12-17 10:57:10 +000029 static const double kMaxTL0FpsReduction;
30 static const double kAcceptableTargetOvershoot;
sprangafe1f742016-04-12 02:45:13 -070031 static const int kMaxFrameIntervalMs;
sprang@webrtc.org70f74f32014-12-17 10:57:10 +000032
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020033 ScreenshareLayers(int num_temporal_layers,
34 Clock* clock);
Erik Språngfb2a66a2018-09-10 10:48:01 +020035 ~ScreenshareLayers() override;
36
37 bool SupportsEncoderFrameDropping() const override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000038
39 // Returns the recommended VP8 encode flags needed. May refresh the decoder
40 // and/or update the reference buffers.
Erik Språng4529fbc2018-10-12 10:30:31 +020041 Vp8TemporalLayers::FrameConfig UpdateLayerConfig(
Erik Språngb75d6b82018-08-13 16:05:33 +020042 uint32_t rtp_timestamp) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000043
Erik Språngbb60a3a2018-03-19 18:25:10 +010044 // New target bitrate, per temporal layer.
45 void OnRatesUpdated(const std::vector<uint32_t>& bitrates_bps,
46 int framerate_fps) override;
Erik Språng08127a92016-11-16 16:41:30 +010047
48 // Update the encoder configuration with target bitrates or other parameters.
49 // Returns true iff the configuration was actually modified.
Anders Carlssonbeabdcb2018-01-24 10:25:15 +010050 bool UpdateConfiguration(Vp8EncoderConfig* cfg) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000051
Erik Språng59021ba2018-10-03 11:05:16 +020052 void OnEncodeDone(uint32_t rtp_timestamp,
53 size_t size_bytes,
54 bool is_keyframe,
55 int qp,
56 CodecSpecificInfoVP8* vp8_info) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000057
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000058 private:
sprang916170a2017-05-23 07:47:55 -070059 enum class TemporalLayerState : int { kDrop, kTl0, kTl1, kTl1Sync };
pbos51f083c2017-05-04 06:39:04 -070060
Erik Språng2c4c9142015-06-24 11:24:44 +020061 bool TimeToSync(int64_t timestamp) const;
sprangc7805db2016-11-25 08:09:43 -080062 uint32_t GetCodecTargetBitrateKbps() const;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000063
sprangb0fdfea2016-03-01 05:51:16 -080064 Clock* const clock_;
65
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000066 int number_of_temporal_layers_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000067 int active_layer_;
Erik Språng2c4c9142015-06-24 11:24:44 +020068 int64_t last_timestamp_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000069 int64_t last_sync_timestamp_;
sprangafe1f742016-04-12 02:45:13 -070070 int64_t last_emitted_tl0_timestamp_;
Erik Språng27a457d2018-01-12 11:00:21 +010071 int64_t last_frame_time_ms_;
Erik Språng2c4c9142015-06-24 11:24:44 +020072 rtc::TimestampWrapAroundHandler time_wrap_handler_;
73 int min_qp_;
74 int max_qp_;
75 uint32_t max_debt_bytes_;
sprangac4a90d2016-12-28 05:58:07 -080076
Erik Språng4529fbc2018-10-12 10:30:31 +020077 std::map<uint32_t, Vp8TemporalLayers::FrameConfig> pending_frame_configs_;
Erik Språng59021ba2018-10-03 11:05:16 +020078
sprangac4a90d2016-12-28 05:58:07 -080079 // Configured max framerate.
Danil Chapovalov0040b662018-06-18 10:48:16 +020080 absl::optional<uint32_t> target_framerate_;
sprangac4a90d2016-12-28 05:58:07 -080081 // Incoming framerate from capturer.
Danil Chapovalov0040b662018-06-18 10:48:16 +020082 absl::optional<uint32_t> capture_framerate_;
sprangac4a90d2016-12-28 05:58:07 -080083 // Tracks what framerate we actually encode, and drops frames on overshoot.
84 RateStatistics encode_framerate_;
Erik Språng08127a92016-11-16 16:41:30 +010085 bool bitrate_updated_;
Erik Språng2c4c9142015-06-24 11:24:44 +020086
sprang429600d2017-01-26 06:12:26 -080087 static constexpr int kMaxNumTemporalLayers = 2;
Erik Språng2c4c9142015-06-24 11:24:44 +020088 struct TemporalLayer {
89 TemporalLayer()
90 : state(State::kNormal),
91 enhanced_max_qp(-1),
92 last_qp(-1),
93 debt_bytes_(0),
94 target_rate_kbps_(0) {}
95
96 enum class State {
97 kNormal,
98 kDropped,
99 kReencoded,
100 kQualityBoost,
Erik Språngb75d6b82018-08-13 16:05:33 +0200101 kKeyFrame
Erik Språng2c4c9142015-06-24 11:24:44 +0200102 } state;
103
104 int enhanced_max_qp;
105 int last_qp;
106 uint32_t debt_bytes_;
107 uint32_t target_rate_kbps_;
108
109 void UpdateDebt(int64_t delta_ms);
110 } layers_[kMaxNumTemporalLayers];
sprangb0fdfea2016-03-01 05:51:16 -0800111
112 void UpdateHistograms();
113 // Data for histogram statistics.
114 struct Stats {
115 int64_t first_frame_time_ms_ = -1;
116 int64_t num_tl0_frames_ = 0;
117 int64_t num_tl1_frames_ = 0;
118 int64_t num_dropped_frames_ = 0;
119 int64_t num_overshoots_ = 0;
120 int64_t tl0_qp_sum_ = 0;
121 int64_t tl1_qp_sum_ = 0;
122 int64_t tl0_target_bitrate_sum_ = 0;
123 int64_t tl1_target_bitrate_sum_ = 0;
124 } stats_;
Erik Språng59021ba2018-10-03 11:05:16 +0200125
126 // Optional utility used to verify reference validity.
127 std::unique_ptr<TemporalLayersChecker> checker_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000128};
129} // namespace webrtc
130
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200131#endif // MODULES_VIDEO_CODING_CODECS_VP8_SCREENSHARE_LAYERS_H_