niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
stefan@webrtc.org | 07b45a5 | 2012-02-02 08:37:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef VIDEO_VIDEO_STREAM_ENCODER_H_ |
| 12 | #define VIDEO_VIDEO_STREAM_ENCODER_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
Yuwei Huang | d9f99c1 | 2017-10-24 15:40:52 -0700 | [diff] [blame] | 14 | #include <atomic> |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 15 | #include <map> |
kwiberg | 27f982b | 2016-03-01 11:52:33 -0800 | [diff] [blame] | 16 | #include <memory> |
perkj | 376b192 | 2016-05-02 11:35:24 -0700 | [diff] [blame] | 17 | #include <string> |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 18 | #include <vector> |
mflodman@webrtc.org | d6ec386 | 2012-10-25 11:30:29 +0000 | [diff] [blame] | 19 | |
Jiawei Ou | 4206a0a | 2018-07-20 15:49:43 -0700 | [diff] [blame] | 20 | #include "api/video/video_bitrate_allocator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "api/video/video_rotation.h" |
Niels Möller | c6ce9c5 | 2018-05-11 11:15:30 +0200 | [diff] [blame] | 22 | #include "api/video/video_sink_interface.h" |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 23 | #include "api/video/video_stream_encoder_interface.h" |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 24 | #include "api/video/video_stream_encoder_observer.h" |
| 25 | #include "api/video/video_stream_encoder_settings.h" |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 26 | #include "api/video_codecs/video_codec.h" |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 27 | #include "api/video_codecs/video_encoder.h" |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 28 | #include "modules/video_coding/utility/frame_dropper.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "modules/video_coding/utility/quality_scaler.h" |
| 30 | #include "modules/video_coding/video_coding_impl.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "rtc_base/event.h" |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 32 | #include "rtc_base/experiments/rate_control_settings.h" |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 33 | #include "rtc_base/race_checker.h" |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 34 | #include "rtc_base/rate_statistics.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 35 | #include "rtc_base/sequenced_task_checker.h" |
| 36 | #include "rtc_base/task_queue.h" |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 37 | #include "video/encoder_bitrate_adjuster.h" |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 38 | #include "video/frame_encode_timer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 39 | #include "video/overuse_frame_detector.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | |
| 41 | namespace webrtc { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 42 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 43 | // VideoStreamEncoder represent a video encoder that accepts raw video frames as |
| 44 | // input and produces an encoded bit stream. |
| 45 | // Usage: |
| 46 | // Instantiate. |
| 47 | // Call SetSink. |
| 48 | // Call SetSource. |
| 49 | // Call ConfigureEncoder with the codec settings. |
| 50 | // Call Stop() when done. |
| 51 | class VideoStreamEncoder : public VideoStreamEncoderInterface, |
| 52 | private EncodedImageCallback, |
| 53 | // Protected only to provide access to tests. |
| 54 | protected AdaptationObserverInterface { |
| 55 | public: |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 56 | VideoStreamEncoder(Clock* clock, |
| 57 | uint32_t number_of_cores, |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 58 | VideoStreamEncoderObserver* encoder_stats_observer, |
| 59 | const VideoStreamEncoderSettings& settings, |
Sebastian Jansson | 74682c1 | 2019-03-01 11:50:20 +0100 | [diff] [blame] | 60 | std::unique_ptr<OveruseFrameDetector> overuse_detector, |
| 61 | TaskQueueFactory* task_queue_factory); |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 62 | ~VideoStreamEncoder() override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 64 | void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 65 | const DegradationPreference& degradation_preference) override; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 66 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 67 | void SetSink(EncoderSink* sink, bool rotation_applied) override; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 68 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 69 | // TODO(perkj): Can we remove VideoCodec.startBitrate ? |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 70 | void SetStartBitrate(int start_bitrate_bps) override; |
mflodman@webrtc.org | 9ec883e | 2012-03-05 17:12:41 +0000 | [diff] [blame] | 71 | |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 72 | void SetBitrateAllocationObserver( |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 73 | VideoBitrateAllocationObserver* bitrate_observer) override; |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 74 | |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 75 | void ConfigureEncoder(VideoEncoderConfig config, |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 76 | size_t max_data_payload_length) override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 78 | // Permanently stop encoding. After this method has returned, it is |
| 79 | // guaranteed that no encoded frames will be delivered to the sink. |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 80 | void Stop() override; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 81 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 82 | void SendKeyFrame() override; |
mflodman@webrtc.org | d6ec386 | 2012-10-25 11:30:29 +0000 | [diff] [blame] | 83 | |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 84 | void OnBitrateUpdated(DataRate target_bitrate, |
| 85 | DataRate target_headroom, |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 86 | uint8_t fraction_lost, |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 87 | int64_t round_trip_time_ms) override; |
pwestin@webrtc.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 88 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 89 | protected: |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 90 | // Used for testing. For example the |ScalingObserverInterface| methods must |
| 91 | // be called on |encoder_queue_|. |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 92 | rtc::TaskQueue* encoder_queue() { return &encoder_queue_; } |
| 93 | |
Niels Möller | d692ef9 | 2017-10-04 15:28:55 +0200 | [diff] [blame] | 94 | // AdaptationObserverInterface implementation. |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 95 | // These methods are protected for easier testing. |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 96 | void AdaptUp(AdaptReason reason) override; |
| 97 | void AdaptDown(AdaptReason reason) override; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 98 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 99 | private: |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 100 | class VideoSourceProxy; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 101 | |
kthelgason | 93f16d7 | 2017-01-16 06:15:23 -0800 | [diff] [blame] | 102 | class VideoFrameInfo { |
| 103 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 104 | VideoFrameInfo(int width, int height, bool is_texture) |
| 105 | : width(width), height(height), is_texture(is_texture) {} |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 106 | int width; |
| 107 | int height; |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 108 | bool is_texture; |
kthelgason | 93f16d7 | 2017-01-16 06:15:23 -0800 | [diff] [blame] | 109 | int pixel_count() const { return width * height; } |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame^] | 112 | struct EncoderRateSettings : public VideoEncoder::RateControlParameters { |
| 113 | EncoderRateSettings(); |
| 114 | EncoderRateSettings(const VideoBitrateAllocation& bitrate, |
| 115 | double framerate_fps, |
| 116 | DataRate bandwidth_allocation, |
| 117 | DataRate encoder_target); |
| 118 | bool operator==(const EncoderRateSettings& rhs) const; |
| 119 | bool operator!=(const EncoderRateSettings& rhs) const; |
| 120 | |
| 121 | // This is the scalar target bitrate before the VideoBitrateAllocator, i.e. |
| 122 | // the |target_bitrate| argument of the OnBitrateUpdated() method. This is |
| 123 | // needed because the bitrate allocator may truncate the total bitrate and a |
| 124 | // later call to the same allocator instance, e.g. |
| 125 | // |using last_encoder_rate_setings_->bitrate.get_sum_bps()|, may trick it |
| 126 | // into thinking the available bitrate has decreased since the last call. |
| 127 | DataRate encoder_target; |
| 128 | }; |
| 129 | |
Per | a48ddb7 | 2016-09-29 11:48:50 +0200 | [diff] [blame] | 130 | void ConfigureEncoderOnTaskQueue(VideoEncoderConfig config, |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 131 | size_t max_data_payload_length); |
Niels Möller | a8b1508 | 2018-02-07 13:42:09 +0100 | [diff] [blame] | 132 | void ReconfigureEncoder() RTC_RUN_ON(&encoder_queue_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 133 | |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 134 | void ConfigureQualityScaler(const VideoEncoder::EncoderInfo& encoder_info); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 135 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 136 | // Implements VideoSinkInterface. |
| 137 | void OnFrame(const VideoFrame& video_frame) override; |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 138 | void OnDiscardedFrame() override; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 139 | |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 140 | void MaybeEncodeVideoFrame(const VideoFrame& frame, |
| 141 | int64_t time_when_posted_in_ms); |
| 142 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 143 | void EncodeVideoFrame(const VideoFrame& frame, |
| 144 | int64_t time_when_posted_in_ms); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 145 | // Indicates wether frame should be dropped because the pixel count is too |
| 146 | // large for the current bitrate configuration. |
| 147 | bool DropDueToSize(uint32_t pixel_count) const RTC_RUN_ON(&encoder_queue_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 148 | |
| 149 | // Implements EncodedImageCallback. |
| 150 | EncodedImageCallback::Result OnEncodedImage( |
| 151 | const EncodedImage& encoded_image, |
| 152 | const CodecSpecificInfo* codec_specific_info, |
| 153 | const RTPFragmentationHeader* fragmentation) override; |
| 154 | |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 155 | void OnDroppedFrame(EncodedImageCallback::DropReason reason) override; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 156 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 157 | bool EncoderPaused() const; |
| 158 | void TraceFrameDropStart(); |
| 159 | void TraceFrameDropEnd(); |
| 160 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame^] | 161 | // Returns a copy of |rate_settings| with the |bitrate| field updated using |
| 162 | // the current VideoBitrateAllocator, and notifies any listeners of the new |
| 163 | // allocation. |
| 164 | EncoderRateSettings UpdateBitrateAllocationAndNotifyObserver( |
| 165 | const EncoderRateSettings& rate_settings) RTC_RUN_ON(&encoder_queue_); |
| 166 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 167 | uint32_t GetInputFramerateFps() RTC_RUN_ON(&encoder_queue_); |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame^] | 168 | void SetEncoderRates(const EncoderRateSettings& rate_settings) |
| 169 | RTC_RUN_ON(&encoder_queue_); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 170 | |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 171 | // Class holding adaptation information. |
| 172 | class AdaptCounter final { |
| 173 | public: |
| 174 | AdaptCounter(); |
| 175 | ~AdaptCounter(); |
| 176 | |
| 177 | // Get number of adaptation downscales for |reason|. |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 178 | VideoStreamEncoderObserver::AdaptationSteps Counts(int reason) const; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 179 | |
| 180 | std::string ToString() const; |
| 181 | |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 182 | void IncrementFramerate(int reason); |
| 183 | void IncrementResolution(int reason); |
| 184 | void DecrementFramerate(int reason); |
| 185 | void DecrementResolution(int reason); |
| 186 | void DecrementFramerate(int reason, int cur_fps); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 187 | |
| 188 | // Gets the total number of downgrades (for all adapt reasons). |
| 189 | int FramerateCount() const; |
| 190 | int ResolutionCount() const; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 191 | |
| 192 | // Gets the total number of downgrades for |reason|. |
| 193 | int FramerateCount(int reason) const; |
| 194 | int ResolutionCount(int reason) const; |
| 195 | int TotalCount(int reason) const; |
| 196 | |
| 197 | private: |
| 198 | std::string ToString(const std::vector<int>& counters) const; |
| 199 | int Count(const std::vector<int>& counters) const; |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 200 | void MoveCount(std::vector<int>* counters, int from_reason); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 201 | |
| 202 | // Degradation counters holding number of framerate/resolution reductions |
| 203 | // per adapt reason. |
| 204 | std::vector<int> fps_counters_; |
| 205 | std::vector<int> resolution_counters_; |
| 206 | }; |
| 207 | |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 208 | AdaptCounter& GetAdaptCounter() RTC_RUN_ON(&encoder_queue_); |
| 209 | const AdaptCounter& GetConstAdaptCounter() RTC_RUN_ON(&encoder_queue_); |
| 210 | void UpdateAdaptationStats(AdaptReason reason) RTC_RUN_ON(&encoder_queue_); |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 211 | VideoStreamEncoderObserver::AdaptationSteps GetActiveCounts( |
| 212 | AdaptReason reason) RTC_RUN_ON(&encoder_queue_); |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 213 | void RunPostEncode(EncodedImage encoded_image, |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 214 | int64_t time_sent_us, |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 215 | int temporal_index); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 216 | bool HasInternalSource() const RTC_RUN_ON(&encoder_queue_); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 217 | void ReleaseEncoder() RTC_RUN_ON(&encoder_queue_); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 218 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 219 | rtc::Event shutdown_event_; |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 220 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 221 | const uint32_t number_of_cores_; |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 222 | // Counts how many frames we've dropped in the initial framedrop phase. |
| 223 | int initial_framedrop_; |
| 224 | const bool initial_framedrop_on_bwe_enabled_; |
| 225 | bool has_seen_first_significant_bwe_change_ = false; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 226 | |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 227 | const bool quality_scaling_experiment_enabled_; |
| 228 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 229 | const std::unique_ptr<VideoSourceProxy> source_proxy_; |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 230 | EncoderSink* sink_; |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 231 | const VideoStreamEncoderSettings settings_; |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 232 | const RateControlSettings rate_control_settings_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 233 | |
Niels Möller | a8b1508 | 2018-02-07 13:42:09 +0100 | [diff] [blame] | 234 | const std::unique_ptr<OveruseFrameDetector> overuse_detector_ |
| 235 | RTC_PT_GUARDED_BY(&encoder_queue_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 236 | std::unique_ptr<QualityScaler> quality_scaler_ RTC_GUARDED_BY(&encoder_queue_) |
Niels Möller | a8b1508 | 2018-02-07 13:42:09 +0100 | [diff] [blame] | 237 | RTC_PT_GUARDED_BY(&encoder_queue_); |
pwestin@webrtc.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 238 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 239 | VideoStreamEncoderObserver* const encoder_stats_observer_; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 240 | // |thread_checker_| checks that public methods that are related to lifetime |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 241 | // of VideoStreamEncoder are called on the same thread. |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 242 | rtc::ThreadChecker thread_checker_; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 243 | |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 244 | VideoEncoderConfig encoder_config_ RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 245 | std::unique_ptr<VideoEncoder> encoder_ RTC_GUARDED_BY(&encoder_queue_) |
| 246 | RTC_PT_GUARDED_BY(&encoder_queue_); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 247 | bool encoder_initialized_; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 248 | std::unique_ptr<VideoBitrateAllocator> rate_allocator_ |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 249 | RTC_GUARDED_BY(&encoder_queue_) RTC_PT_GUARDED_BY(&encoder_queue_); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 250 | // The maximum frame rate of the current codec configuration, as determined |
| 251 | // at the last ReconfigureEncoder() call. |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 252 | int max_framerate_ RTC_GUARDED_BY(&encoder_queue_); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 253 | |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 254 | // Set when ConfigureEncoder has been called in order to lazy reconfigure the |
| 255 | // encoder on the next frame. |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 256 | bool pending_encoder_reconfiguration_ RTC_GUARDED_BY(&encoder_queue_); |
Mirta Dvornicic | 1ec2a16 | 2018-12-10 09:47:34 +0000 | [diff] [blame] | 257 | // Set when configuration must create a new encoder object, e.g., |
| 258 | // because of a codec change. |
| 259 | bool pending_encoder_creation_ RTC_GUARDED_BY(&encoder_queue_); |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 260 | absl::optional<VideoFrameInfo> last_frame_info_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 261 | RTC_GUARDED_BY(&encoder_queue_); |
| 262 | int crop_width_ RTC_GUARDED_BY(&encoder_queue_); |
| 263 | int crop_height_ RTC_GUARDED_BY(&encoder_queue_); |
| 264 | uint32_t encoder_start_bitrate_bps_ RTC_GUARDED_BY(&encoder_queue_); |
| 265 | size_t max_data_payload_length_ RTC_GUARDED_BY(&encoder_queue_); |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame^] | 266 | absl::optional<EncoderRateSettings> last_encoder_rate_settings_ |
| 267 | RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 268 | bool encoder_paused_and_dropped_frame_ RTC_GUARDED_BY(&encoder_queue_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 269 | Clock* const clock_; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 270 | // Counters used for deciding if the video resolution or framerate is |
| 271 | // currently restricted, and if so, why, on a per degradation preference |
| 272 | // basis. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 273 | // TODO(sprang): Replace this with a state holding a relative overuse measure |
| 274 | // instead, that can be translated into suitable down-scale or fps limit. |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 275 | std::map<const DegradationPreference, AdaptCounter> adapt_counters_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 276 | RTC_GUARDED_BY(&encoder_queue_); |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 277 | // Set depending on degradation preferences. |
| 278 | DegradationPreference degradation_preference_ RTC_GUARDED_BY(&encoder_queue_); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 279 | |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 280 | struct AdaptationRequest { |
| 281 | // The pixel count produced by the source at the time of the adaptation. |
| 282 | int input_pixel_count_; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 283 | // Framerate received from the source at the time of the adaptation. |
| 284 | int framerate_fps_; |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 285 | // Indicates if request was to adapt up or down. |
| 286 | enum class Mode { kAdaptUp, kAdaptDown } mode_; |
| 287 | }; |
| 288 | // Stores a snapshot of the last adaptation request triggered by an AdaptUp |
| 289 | // or AdaptDown signal. |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 290 | absl::optional<AdaptationRequest> last_adaptation_request_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 291 | RTC_GUARDED_BY(&encoder_queue_); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 292 | |
| 293 | rtc::RaceChecker incoming_frame_race_checker_ |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 294 | RTC_GUARDED_BY(incoming_frame_race_checker_); |
Yuwei Huang | d9f99c1 | 2017-10-24 15:40:52 -0700 | [diff] [blame] | 295 | std::atomic<int> posted_frames_waiting_for_encode_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 296 | // Used to make sure incoming time stamp is increasing for every frame. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 297 | int64_t last_captured_timestamp_ RTC_GUARDED_BY(incoming_frame_race_checker_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 298 | // Delta used for translating between NTP and internal timestamps. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 299 | const int64_t delta_ntp_internal_ms_ |
| 300 | RTC_GUARDED_BY(incoming_frame_race_checker_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 301 | |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 302 | int64_t last_frame_log_ms_ RTC_GUARDED_BY(incoming_frame_race_checker_); |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 303 | int captured_frame_count_ RTC_GUARDED_BY(&encoder_queue_); |
| 304 | int dropped_frame_count_ RTC_GUARDED_BY(&encoder_queue_); |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 305 | absl::optional<VideoFrame> pending_frame_ RTC_GUARDED_BY(&encoder_queue_); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 306 | int64_t pending_frame_post_time_us_ RTC_GUARDED_BY(&encoder_queue_); |
asapersson | 6ffb67d | 2016-09-12 00:10:45 -0700 | [diff] [blame] | 307 | |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 13:01:26 +0100 | [diff] [blame] | 308 | VideoFrame::UpdateRect accumulated_update_rect_ |
| 309 | RTC_GUARDED_BY(&encoder_queue_); |
| 310 | |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 311 | VideoBitrateAllocationObserver* bitrate_observer_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 312 | RTC_GUARDED_BY(&encoder_queue_); |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 313 | absl::optional<int64_t> last_parameters_update_ms_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 314 | RTC_GUARDED_BY(&encoder_queue_); |
Åsa Persson | c29cb2c | 2019-03-25 12:06:59 +0100 | [diff] [blame] | 315 | absl::optional<int64_t> last_encode_info_ms_ RTC_GUARDED_BY(&encoder_queue_); |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 316 | |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 317 | VideoEncoder::EncoderInfo encoder_info_ RTC_GUARDED_BY(&encoder_queue_); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 318 | VideoEncoderFactory::CodecInfo codec_info_ RTC_GUARDED_BY(&encoder_queue_); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 319 | VideoCodec send_codec_ RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 320 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 321 | FrameDropper frame_dropper_ RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 322 | // If frame dropper is not force disabled, frame dropping might still be |
| 323 | // disabled if VideoEncoder::GetEncoderInfo() indicates that the encoder has a |
| 324 | // trusted rate controller. This is determined on a per-frame basis, as the |
| 325 | // encoder behavior might dynamically change. |
| 326 | bool force_disable_frame_dropper_ RTC_GUARDED_BY(&encoder_queue_); |
| 327 | RateStatistics input_framerate_ RTC_GUARDED_BY(&encoder_queue_); |
| 328 | // Incremented on worker thread whenever |frame_dropper_| determines that a |
| 329 | // frame should be dropped. Decremented on whichever thread runs |
| 330 | // OnEncodedImage(), which is only called by one thread but not necessarily |
| 331 | // the worker thread. |
| 332 | std::atomic<int> pending_frame_drops_; |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 333 | |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 334 | std::unique_ptr<EncoderBitrateAdjuster> bitrate_adjuster_ |
| 335 | RTC_GUARDED_BY(&encoder_queue_); |
| 336 | |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 337 | // TODO(sprang): Change actually support keyframe per simulcast stream, or |
| 338 | // turn this into a simple bool |pending_keyframe_request_|. |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 339 | std::vector<VideoFrameType> next_frame_types_ RTC_GUARDED_BY(&encoder_queue_); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 340 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 341 | FrameEncodeTimer frame_encoder_timer_; |
| 342 | |
| 343 | // Experiment groups parsed from field trials for realtime video ([0]) and |
| 344 | // screenshare ([1]). 0 means no group specified. Positive values are |
| 345 | // experiment group numbers incremented by 1. |
| 346 | const std::array<uint8_t, 2> experiment_groups_; |
| 347 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 348 | // All public methods are proxied to |encoder_queue_|. It must must be |
| 349 | // destroyed first to make sure no tasks are run that use other members. |
| 350 | rtc::TaskQueue encoder_queue_; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 351 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 352 | RTC_DISALLOW_COPY_AND_ASSIGN(VideoStreamEncoder); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 353 | }; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 354 | |
| 355 | } // namespace webrtc |
| 356 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 357 | #endif // VIDEO_VIDEO_STREAM_ENCODER_H_ |