blob: 68b264deac3446649a11bf3a5cff92dce49bc2a7 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.org07b45a52012-02-02 08:37:48 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef VIDEO_VIDEO_STREAM_ENCODER_H_
12#define VIDEO_VIDEO_STREAM_ENCODER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Yuwei Huangd9f99c12017-10-24 15:40:52 -070014#include <atomic>
sprangc5d62e22017-04-02 23:53:04 -070015#include <map>
kwiberg27f982b2016-03-01 11:52:33 -080016#include <memory>
perkj376b1922016-05-02 11:35:24 -070017#include <string>
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000018#include <vector>
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +000019
Henrik Boströme2e8c172020-06-03 09:24:06 +020020#include "api/adaptation/resource.h"
philipeld9cc8c02019-09-16 14:53:40 +020021#include "api/units/data_rate.h"
Jiawei Ou4206a0a2018-07-20 15:49:43 -070022#include "api/video/video_bitrate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "api/video/video_rotation.h"
Niels Möllerc6ce9c52018-05-11 11:15:30 +020024#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020025#include "api/video/video_stream_encoder_interface.h"
Niels Möller213618e2018-07-24 09:29:58 +020026#include "api/video/video_stream_encoder_observer.h"
27#include "api/video/video_stream_encoder_settings.h"
Erik Språng6a7baa72019-02-26 18:31:00 +010028#include "api/video_codecs/video_codec.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020029#include "api/video_codecs/video_encoder.h"
Henrik Boström0f0aa9c2020-06-02 13:02:36 +020030#include "call/adaptation/adaptation_constraint.h"
31#include "call/adaptation/adaptation_listener.h"
Henrik Boström62057622020-03-10 19:08:05 +010032#include "call/adaptation/resource_adaptation_processor_interface.h"
Henrik Boströmce0ea492020-01-13 11:27:18 +010033#include "call/adaptation/video_source_restrictions.h"
Henrik Boströmd516b252020-04-17 12:10:59 +020034#include "call/adaptation/video_stream_input_state_provider.h"
Niels Möller6bb5ab92019-01-11 11:11:10 +010035#include "modules/video_coding/utility/frame_dropper.h"
philipelda5aa4d2019-04-26 13:37:37 +020036#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "rtc_base/event.h"
Erik Språng7ca375c2019-02-06 16:20:17 +010038#include "rtc_base/experiments/rate_control_settings.h"
philipeld9cc8c02019-09-16 14:53:40 +020039#include "rtc_base/numerics/exp_filter.h"
Erik Språng6a7baa72019-02-26 18:31:00 +010040#include "rtc_base/race_checker.h"
Niels Möller6bb5ab92019-01-11 11:11:10 +010041#include "rtc_base/rate_statistics.h"
Sebastian Janssonb55015e2019-04-09 13:44:04 +020042#include "rtc_base/synchronization/sequence_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "rtc_base/task_queue.h"
Tommia0a44802020-05-13 18:27:26 +020044#include "rtc_base/thread_checker.h"
Niels Möllerfe407b72019-09-10 10:48:48 +020045#include "system_wrappers/include/clock.h"
Henrik Boströmde8d5512020-04-17 17:15:26 +020046#include "video/adaptation/video_stream_encoder_resource_manager.h"
Erik Språng7ca375c2019-02-06 16:20:17 +010047#include "video/encoder_bitrate_adjuster.h"
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020048#include "video/frame_encode_metadata_writer.h"
Henrik Boströmce0ea492020-01-13 11:27:18 +010049#include "video/video_source_sink_controller.h"
Henrik Boström0f0aa9c2020-06-02 13:02:36 +020050
niklase@google.com470e71d2011-07-07 08:21:25 +000051namespace webrtc {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000052
Sebastian Jansson652dc912018-04-19 17:09:15 +020053// VideoStreamEncoder represent a video encoder that accepts raw video frames as
54// input and produces an encoded bit stream.
55// Usage:
56// Instantiate.
57// Call SetSink.
58// Call SetSource.
59// Call ConfigureEncoder with the codec settings.
60// Call Stop() when done.
61class VideoStreamEncoder : public VideoStreamEncoderInterface,
Henrik Boströmd2382002020-01-10 15:44:01 +010062 private EncodedImageCallback,
Henrik Boström0f0aa9c2020-06-02 13:02:36 +020063 public VideoSourceRestrictionsListener {
Sebastian Jansson652dc912018-04-19 17:09:15 +020064 public:
Sebastian Jansson572c60f2019-03-04 18:30:41 +010065 VideoStreamEncoder(Clock* clock,
66 uint32_t number_of_cores,
Niels Möller213618e2018-07-24 09:29:58 +020067 VideoStreamEncoderObserver* encoder_stats_observer,
68 const VideoStreamEncoderSettings& settings,
Sebastian Jansson74682c12019-03-01 11:50:20 +010069 std::unique_ptr<OveruseFrameDetector> overuse_detector,
70 TaskQueueFactory* task_queue_factory);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020071 ~VideoStreamEncoder() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000072
Henrik Boströmf4a99912020-06-11 12:07:14 +020073 void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) override;
74 std::vector<rtc::scoped_refptr<Resource>> GetAdaptationResources() override;
75
Sebastian Jansson652dc912018-04-19 17:09:15 +020076 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source,
Taylor Brandstetter49fcc102018-05-16 14:20:41 -070077 const DegradationPreference& degradation_preference) override;
perkj803d97f2016-11-01 11:45:46 -070078
Sebastian Jansson652dc912018-04-19 17:09:15 +020079 void SetSink(EncoderSink* sink, bool rotation_applied) override;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000080
perkj26091b12016-09-01 01:17:40 -070081 // TODO(perkj): Can we remove VideoCodec.startBitrate ?
Sebastian Jansson652dc912018-04-19 17:09:15 +020082 void SetStartBitrate(int start_bitrate_bps) override;
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +000083
Niels Möller0327c2d2018-05-21 14:09:31 +020084 void SetBitrateAllocationObserver(
Sebastian Jansson652dc912018-04-19 17:09:15 +020085 VideoBitrateAllocationObserver* bitrate_observer) override;
sprang1a646ee2016-12-01 06:34:11 -080086
Elad Alon8f01c4e2019-06-28 15:19:43 +020087 void SetFecControllerOverride(
88 FecControllerOverride* fec_controller_override) override;
89
Per512ecb32016-09-23 15:52:06 +020090 void ConfigureEncoder(VideoEncoderConfig config,
Niels Möllerf1338562018-04-26 09:51:47 +020091 size_t max_data_payload_length) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000092
perkj26091b12016-09-01 01:17:40 -070093 // Permanently stop encoding. After this method has returned, it is
94 // guaranteed that no encoded frames will be delivered to the sink.
Sebastian Jansson652dc912018-04-19 17:09:15 +020095 void Stop() override;
perkj26091b12016-09-01 01:17:40 -070096
Sebastian Jansson652dc912018-04-19 17:09:15 +020097 void SendKeyFrame() override;
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +000098
Elad Alonb6ef99b2019-04-10 16:37:07 +020099 void OnLossNotification(
100 const VideoEncoder::LossNotification& loss_notification) override;
101
Erik Språng610c7632019-03-06 15:37:33 +0100102 void OnBitrateUpdated(DataRate target_bitrate,
Florent Castellia8336d32019-09-09 13:36:55 +0200103 DataRate stable_target_bitrate,
Erik Språng610c7632019-03-06 15:37:33 +0100104 DataRate target_headroom,
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000105 uint8_t fraction_lost,
Ying Wang9b881ab2020-02-07 14:29:32 +0100106 int64_t round_trip_time_ms,
107 double cwnd_reduce_ratio) override;
108
109 DataRate UpdateTargetBitrate(DataRate target_bitrate,
110 double cwnd_reduce_ratio);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000111
perkj803d97f2016-11-01 11:45:46 -0700112 protected:
kthelgason876222f2016-11-29 01:44:11 -0800113 // Used for testing. For example the |ScalingObserverInterface| methods must
114 // be called on |encoder_queue_|.
perkj803d97f2016-11-01 11:45:46 -0700115 rtc::TaskQueue* encoder_queue() { return &encoder_queue_; }
Henrik Boström381d1092020-05-12 18:49:07 +0200116 rtc::TaskQueue* resource_adaptation_queue() {
117 return &resource_adaptation_queue_;
118 }
perkj803d97f2016-11-01 11:45:46 -0700119
Henrik Boströmd2382002020-01-10 15:44:01 +0100120 void OnVideoSourceRestrictionsUpdated(
Henrik Boströmd2930c62020-04-17 15:31:48 +0200121 VideoSourceRestrictions restrictions,
122 const VideoAdaptationCounters& adaptation_counters,
Henrik Boströmc55516d2020-05-11 16:29:22 +0200123 rtc::scoped_refptr<Resource> reason) override;
Henrik Boströmd2382002020-01-10 15:44:01 +0100124
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +0100125 // Used for injected test resources.
126 // TODO(eshr): Move all adaptation tests out of VideoStreamEncoder tests.
Henrik Boströmc55516d2020-05-11 16:29:22 +0200127 void InjectAdaptationResource(rtc::scoped_refptr<Resource> resource,
Henrik Boström0f0aa9c2020-06-02 13:02:36 +0200128 VideoAdaptationReason reason);
129 void InjectAdaptationConstraint(AdaptationConstraint* adaptation_constraint);
130 void InjectAdaptationListener(AdaptationListener* adaptation_listener);
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +0100131
Henrik Boströmc55516d2020-05-11 16:29:22 +0200132 rtc::scoped_refptr<QualityScalerResource>
133 quality_scaler_resource_for_testing();
Henrik Boström91aa7322020-04-28 12:24:33 +0200134
Henrik Boström0f0aa9c2020-06-02 13:02:36 +0200135 void AddRestrictionsListenerForTesting(
136 VideoSourceRestrictionsListener* restrictions_listener);
137 void RemoveRestrictionsListenerForTesting(
138 VideoSourceRestrictionsListener* restrictions_listener);
Henrik Boström381d1092020-05-12 18:49:07 +0200139
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000140 private:
kthelgason93f16d72017-01-16 06:15:23 -0800141 class VideoFrameInfo {
142 public:
Yves Gerey665174f2018-06-19 15:03:05 +0200143 VideoFrameInfo(int width, int height, bool is_texture)
144 : width(width), height(height), is_texture(is_texture) {}
perkjfa10b552016-10-02 23:45:26 -0700145 int width;
146 int height;
perkjfa10b552016-10-02 23:45:26 -0700147 bool is_texture;
kthelgason93f16d72017-01-16 06:15:23 -0800148 int pixel_count() const { return width * height; }
perkjfa10b552016-10-02 23:45:26 -0700149 };
150
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200151 struct EncoderRateSettings {
Erik Språng4c6ca302019-04-08 15:14:01 +0200152 EncoderRateSettings();
153 EncoderRateSettings(const VideoBitrateAllocation& bitrate,
154 double framerate_fps,
155 DataRate bandwidth_allocation,
Florent Castellia8336d32019-09-09 13:36:55 +0200156 DataRate encoder_target,
157 DataRate stable_encoder_target);
Erik Språng4c6ca302019-04-08 15:14:01 +0200158 bool operator==(const EncoderRateSettings& rhs) const;
159 bool operator!=(const EncoderRateSettings& rhs) const;
160
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200161 VideoEncoder::RateControlParameters rate_control;
Erik Språng4c6ca302019-04-08 15:14:01 +0200162 // This is the scalar target bitrate before the VideoBitrateAllocator, i.e.
163 // the |target_bitrate| argument of the OnBitrateUpdated() method. This is
164 // needed because the bitrate allocator may truncate the total bitrate and a
165 // later call to the same allocator instance, e.g.
166 // |using last_encoder_rate_setings_->bitrate.get_sum_bps()|, may trick it
167 // into thinking the available bitrate has decreased since the last call.
168 DataRate encoder_target;
Florent Castellia8336d32019-09-09 13:36:55 +0200169 DataRate stable_encoder_target;
Erik Språng4c6ca302019-04-08 15:14:01 +0200170 };
171
Niels Möllera8b15082018-02-07 13:42:09 +0100172 void ReconfigureEncoder() RTC_RUN_ON(&encoder_queue_);
Henrik Boströmd516b252020-04-17 12:10:59 +0200173 void OnEncoderSettingsChanged() RTC_RUN_ON(&encoder_queue_);
perkj26091b12016-09-01 01:17:40 -0700174
perkja49cbd32016-09-16 07:53:41 -0700175 // Implements VideoSinkInterface.
176 void OnFrame(const VideoFrame& video_frame) override;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200177 void OnDiscardedFrame() override;
perkja49cbd32016-09-16 07:53:41 -0700178
Sebastian Janssona3177052018-04-10 13:05:49 +0200179 void MaybeEncodeVideoFrame(const VideoFrame& frame,
180 int64_t time_when_posted_in_ms);
181
perkjd52063f2016-09-07 06:32:18 -0700182 void EncodeVideoFrame(const VideoFrame& frame,
183 int64_t time_when_posted_in_ms);
Sebastian Janssona3177052018-04-10 13:05:49 +0200184 // Indicates wether frame should be dropped because the pixel count is too
185 // large for the current bitrate configuration.
186 bool DropDueToSize(uint32_t pixel_count) const RTC_RUN_ON(&encoder_queue_);
perkj26091b12016-09-01 01:17:40 -0700187
188 // Implements EncodedImageCallback.
189 EncodedImageCallback::Result OnEncodedImage(
190 const EncodedImage& encoded_image,
191 const CodecSpecificInfo* codec_specific_info,
192 const RTPFragmentationHeader* fragmentation) override;
193
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200194 void OnDroppedFrame(EncodedImageCallback::DropReason reason) override;
kthelgason876222f2016-11-29 01:44:11 -0800195
perkj26091b12016-09-01 01:17:40 -0700196 bool EncoderPaused() const;
197 void TraceFrameDropStart();
198 void TraceFrameDropEnd();
199
Erik Språng4c6ca302019-04-08 15:14:01 +0200200 // Returns a copy of |rate_settings| with the |bitrate| field updated using
201 // the current VideoBitrateAllocator, and notifies any listeners of the new
202 // allocation.
203 EncoderRateSettings UpdateBitrateAllocationAndNotifyObserver(
204 const EncoderRateSettings& rate_settings) RTC_RUN_ON(&encoder_queue_);
205
Niels Möller6bb5ab92019-01-11 11:11:10 +0100206 uint32_t GetInputFramerateFps() RTC_RUN_ON(&encoder_queue_);
Erik Språng4c6ca302019-04-08 15:14:01 +0200207 void SetEncoderRates(const EncoderRateSettings& rate_settings)
208 RTC_RUN_ON(&encoder_queue_);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100209
Evan Shrubsole2bc91e82020-01-17 11:44:24 +0100210 void RunPostEncode(const EncodedImage& encoded_image,
Niels Möller6bb5ab92019-01-11 11:11:10 +0100211 int64_t time_sent_us,
philipelc7a46c42019-11-25 15:01:09 +0100212 int temporal_index,
213 DataSize frame_size);
Erik Språngd7329ca2019-02-21 21:19:53 +0100214 bool HasInternalSource() const RTC_RUN_ON(&encoder_queue_);
Erik Språng6a7baa72019-02-26 18:31:00 +0100215 void ReleaseEncoder() RTC_RUN_ON(&encoder_queue_);
sprangc5d62e22017-04-02 23:53:04 -0700216
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +0100217 void CheckForAnimatedContent(const VideoFrame& frame,
218 int64_t time_when_posted_in_ms)
219 RTC_RUN_ON(&encoder_queue_);
220
perkj26091b12016-09-01 01:17:40 -0700221 rtc::Event shutdown_event_;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000222
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000223 const uint32_t number_of_cores_;
perkja49cbd32016-09-16 07:53:41 -0700224
Åsa Perssona945aee2018-04-24 16:53:25 +0200225 const bool quality_scaling_experiment_enabled_;
226
Per512ecb32016-09-23 15:52:06 +0200227 EncoderSink* sink_;
Niels Möller213618e2018-07-24 09:29:58 +0200228 const VideoStreamEncoderSettings settings_;
Erik Språng7ca375c2019-02-06 16:20:17 +0100229 const RateControlSettings rate_control_settings_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000230
philipel9b058032020-02-10 11:30:00 +0100231 std::unique_ptr<VideoEncoderFactory::EncoderSelectorInterface> const
232 encoder_selector_;
Niels Möller213618e2018-07-24 09:29:58 +0200233 VideoStreamEncoderObserver* const encoder_stats_observer_;
perkja49cbd32016-09-16 07:53:41 -0700234 // |thread_checker_| checks that public methods that are related to lifetime
mflodmancc3d4422017-08-03 08:27:51 -0700235 // of VideoStreamEncoder are called on the same thread.
perkja49cbd32016-09-16 07:53:41 -0700236 rtc::ThreadChecker thread_checker_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000237
Niels Möller1e062892018-02-07 10:18:32 +0100238 VideoEncoderConfig encoder_config_ RTC_GUARDED_BY(&encoder_queue_);
Niels Möller4db138e2018-04-19 09:04:13 +0200239 std::unique_ptr<VideoEncoder> encoder_ RTC_GUARDED_BY(&encoder_queue_)
240 RTC_PT_GUARDED_BY(&encoder_queue_);
Erik Språng6a7baa72019-02-26 18:31:00 +0100241 bool encoder_initialized_;
Erik Språng08127a92016-11-16 16:41:30 +0100242 std::unique_ptr<VideoBitrateAllocator> rate_allocator_
Yves Gerey665174f2018-06-19 15:03:05 +0200243 RTC_GUARDED_BY(&encoder_queue_) RTC_PT_GUARDED_BY(&encoder_queue_);
Niels Möller1e062892018-02-07 10:18:32 +0100244 int max_framerate_ RTC_GUARDED_BY(&encoder_queue_);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000245
perkjfa10b552016-10-02 23:45:26 -0700246 // Set when ConfigureEncoder has been called in order to lazy reconfigure the
247 // encoder on the next frame.
Niels Möller1e062892018-02-07 10:18:32 +0100248 bool pending_encoder_reconfiguration_ RTC_GUARDED_BY(&encoder_queue_);
Mirta Dvornicic1ec2a162018-12-10 09:47:34 +0000249 // Set when configuration must create a new encoder object, e.g.,
250 // because of a codec change.
251 bool pending_encoder_creation_ RTC_GUARDED_BY(&encoder_queue_);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200252
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200253 absl::optional<VideoFrameInfo> last_frame_info_
Niels Möller1e062892018-02-07 10:18:32 +0100254 RTC_GUARDED_BY(&encoder_queue_);
255 int crop_width_ RTC_GUARDED_BY(&encoder_queue_);
256 int crop_height_ RTC_GUARDED_BY(&encoder_queue_);
Henrik Boströmede69c02020-01-21 17:45:35 +0100257 absl::optional<uint32_t> encoder_target_bitrate_bps_
258 RTC_GUARDED_BY(&encoder_queue_);
Niels Möller1e062892018-02-07 10:18:32 +0100259 size_t max_data_payload_length_ RTC_GUARDED_BY(&encoder_queue_);
Erik Språng4c6ca302019-04-08 15:14:01 +0200260 absl::optional<EncoderRateSettings> last_encoder_rate_settings_
261 RTC_GUARDED_BY(&encoder_queue_);
Niels Möller1e062892018-02-07 10:18:32 +0100262 bool encoder_paused_and_dropped_frame_ RTC_GUARDED_BY(&encoder_queue_);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200263
264 // Set to true if at least one frame was sent to encoder since last encoder
265 // initialization.
266 bool was_encode_called_since_last_initialization_
267 RTC_GUARDED_BY(&encoder_queue_);
268
philipele8ed8302019-07-03 11:53:48 +0200269 bool encoder_failed_ RTC_GUARDED_BY(&encoder_queue_);
perkj26091b12016-09-01 01:17:40 -0700270 Clock* const clock_;
perkj803d97f2016-11-01 11:45:46 -0700271
272 rtc::RaceChecker incoming_frame_race_checker_
danilchapa37de392017-09-09 04:17:22 -0700273 RTC_GUARDED_BY(incoming_frame_race_checker_);
Yuwei Huangd9f99c12017-10-24 15:40:52 -0700274 std::atomic<int> posted_frames_waiting_for_encode_;
perkj26091b12016-09-01 01:17:40 -0700275 // Used to make sure incoming time stamp is increasing for every frame.
danilchapa37de392017-09-09 04:17:22 -0700276 int64_t last_captured_timestamp_ RTC_GUARDED_BY(incoming_frame_race_checker_);
perkj26091b12016-09-01 01:17:40 -0700277 // Delta used for translating between NTP and internal timestamps.
danilchapa37de392017-09-09 04:17:22 -0700278 const int64_t delta_ntp_internal_ms_
279 RTC_GUARDED_BY(incoming_frame_race_checker_);
perkj26091b12016-09-01 01:17:40 -0700280
danilchapa37de392017-09-09 04:17:22 -0700281 int64_t last_frame_log_ms_ RTC_GUARDED_BY(incoming_frame_race_checker_);
Niels Möller1e062892018-02-07 10:18:32 +0100282 int captured_frame_count_ RTC_GUARDED_BY(&encoder_queue_);
Ying Wang9b881ab2020-02-07 14:29:32 +0100283 int dropped_frame_cwnd_pushback_count_ RTC_GUARDED_BY(&encoder_queue_);
284 int dropped_frame_encoder_block_count_ RTC_GUARDED_BY(&encoder_queue_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200285 absl::optional<VideoFrame> pending_frame_ RTC_GUARDED_BY(&encoder_queue_);
Sebastian Janssona3177052018-04-10 13:05:49 +0200286 int64_t pending_frame_post_time_us_ RTC_GUARDED_BY(&encoder_queue_);
asapersson6ffb67d2016-09-12 00:10:45 -0700287
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100288 VideoFrame::UpdateRect accumulated_update_rect_
289 RTC_GUARDED_BY(&encoder_queue_);
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100290 bool accumulated_update_rect_is_valid_ RTC_GUARDED_BY(&encoder_queue_);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100291
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +0100292 // Used for automatic content type detection.
293 absl::optional<VideoFrame::UpdateRect> last_update_rect_
294 RTC_GUARDED_BY(&encoder_queue_);
295 Timestamp animation_start_time_ RTC_GUARDED_BY(&encoder_queue_);
296 bool cap_resolution_due_to_video_content_ RTC_GUARDED_BY(&encoder_queue_);
297 // Used to correctly ignore changes in update_rect introduced by
298 // resize triggered by animation detection.
299 enum class ExpectResizeState {
300 kNoResize, // Normal operation.
301 kResize, // Resize was triggered by the animation detection.
302 kFirstFrameAfterResize // Resize observed.
303 } expect_resize_state_ RTC_GUARDED_BY(&encoder_queue_);
304
danilchapa37de392017-09-09 04:17:22 -0700305 VideoBitrateAllocationObserver* bitrate_observer_
Niels Möller1e062892018-02-07 10:18:32 +0100306 RTC_GUARDED_BY(&encoder_queue_);
Elad Alon8f01c4e2019-06-28 15:19:43 +0200307 FecControllerOverride* fec_controller_override_
308 RTC_GUARDED_BY(&encoder_queue_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200309 absl::optional<int64_t> last_parameters_update_ms_
Niels Möller1e062892018-02-07 10:18:32 +0100310 RTC_GUARDED_BY(&encoder_queue_);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100311 absl::optional<int64_t> last_encode_info_ms_ RTC_GUARDED_BY(&encoder_queue_);
sprang1a646ee2016-12-01 06:34:11 -0800312
Erik Språnge2fd86a2018-10-24 11:32:39 +0200313 VideoEncoder::EncoderInfo encoder_info_ RTC_GUARDED_BY(&encoder_queue_);
Sergey Silkin6456e352019-07-08 17:56:40 +0200314 absl::optional<VideoEncoder::ResolutionBitrateLimits> encoder_bitrate_limits_
315 RTC_GUARDED_BY(&encoder_queue_);
Erik Språngd7329ca2019-02-21 21:19:53 +0100316 VideoEncoderFactory::CodecInfo codec_info_ RTC_GUARDED_BY(&encoder_queue_);
Erik Språng6a7baa72019-02-26 18:31:00 +0100317 VideoCodec send_codec_ RTC_GUARDED_BY(&encoder_queue_);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100318
Erik Språng6a7baa72019-02-26 18:31:00 +0100319 FrameDropper frame_dropper_ RTC_GUARDED_BY(&encoder_queue_);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100320 // If frame dropper is not force disabled, frame dropping might still be
321 // disabled if VideoEncoder::GetEncoderInfo() indicates that the encoder has a
322 // trusted rate controller. This is determined on a per-frame basis, as the
323 // encoder behavior might dynamically change.
324 bool force_disable_frame_dropper_ RTC_GUARDED_BY(&encoder_queue_);
325 RateStatistics input_framerate_ RTC_GUARDED_BY(&encoder_queue_);
326 // Incremented on worker thread whenever |frame_dropper_| determines that a
327 // frame should be dropped. Decremented on whichever thread runs
328 // OnEncodedImage(), which is only called by one thread but not necessarily
329 // the worker thread.
330 std::atomic<int> pending_frame_drops_;
Erik Språnge2fd86a2018-10-24 11:32:39 +0200331
Ying Wang9b881ab2020-02-07 14:29:32 +0100332 // Congestion window frame drop ratio (drop 1 in every
333 // cwnd_frame_drop_interval_ frames).
334 absl::optional<int> cwnd_frame_drop_interval_ RTC_GUARDED_BY(&encoder_queue_);
335 // Frame counter for congestion window frame drop.
336 int cwnd_frame_counter_ RTC_GUARDED_BY(&encoder_queue_);
337
Erik Språng7ca375c2019-02-06 16:20:17 +0100338 std::unique_ptr<EncoderBitrateAdjuster> bitrate_adjuster_
339 RTC_GUARDED_BY(&encoder_queue_);
340
Erik Språngd7329ca2019-02-21 21:19:53 +0100341 // TODO(sprang): Change actually support keyframe per simulcast stream, or
342 // turn this into a simple bool |pending_keyframe_request_|.
Niels Möller87e2d782019-03-07 10:18:23 +0100343 std::vector<VideoFrameType> next_frame_types_ RTC_GUARDED_BY(&encoder_queue_);
Erik Språngd7329ca2019-02-21 21:19:53 +0100344
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +0200345 FrameEncodeMetadataWriter frame_encode_metadata_writer_;
Erik Språng6a7baa72019-02-26 18:31:00 +0100346
347 // Experiment groups parsed from field trials for realtime video ([0]) and
348 // screenshare ([1]). 0 means no group specified. Positive values are
349 // experiment group numbers incremented by 1.
350 const std::array<uint8_t, 2> experiment_groups_;
351
philipeld9cc8c02019-09-16 14:53:40 +0200352 struct EncoderSwitchExperiment {
353 struct Thresholds {
354 absl::optional<DataRate> bitrate;
355 absl::optional<int> pixel_count;
356 };
357
358 // Codec --> switching thresholds
359 std::map<VideoCodecType, Thresholds> codec_thresholds;
360
361 // To smooth out the target bitrate so that we don't trigger a switch
362 // too easily.
363 rtc::ExpFilter bitrate_filter{1.0};
364
365 // Codec/implementation to switch to
366 std::string to_codec;
367 absl::optional<std::string> to_param;
368 absl::optional<std::string> to_value;
369
370 // Thresholds for the currently used codecs.
371 Thresholds current_thresholds;
372
373 // Updates the |bitrate_filter|, so not const.
374 bool IsBitrateBelowThreshold(const DataRate& target_bitrate);
375 bool IsPixelCountBelowThreshold(int pixel_count) const;
376 void SetCodec(VideoCodecType codec);
377 };
378
379 EncoderSwitchExperiment ParseEncoderSwitchFieldTrial() const;
380
381 EncoderSwitchExperiment encoder_switch_experiment_
382 RTC_GUARDED_BY(&encoder_queue_);
383
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +0100384 struct AutomaticAnimationDetectionExperiment {
385 bool enabled = false;
386 int min_duration_ms = 2000;
387 double min_area_ratio = 0.8;
388 int min_fps = 10;
389 std::unique_ptr<StructParametersParser> Parser() {
390 return StructParametersParser::Create(
391 "enabled", &enabled, //
392 "min_duration_ms", &min_duration_ms, //
393 "min_area_ratio", &min_area_ratio, //
394 "min_fps", &min_fps);
395 }
396 };
397
398 AutomaticAnimationDetectionExperiment
399 ParseAutomatincAnimationDetectionFieldTrial() const;
400
401 AutomaticAnimationDetectionExperiment
402 automatic_animation_detection_experiment_ RTC_GUARDED_BY(&encoder_queue_);
403
philipeld9cc8c02019-09-16 14:53:40 +0200404 // An encoder switch is only requested once, this variable is used to keep
405 // track of whether a request has been made or not.
406 bool encoder_switch_requested_ RTC_GUARDED_BY(&encoder_queue_);
407
Henrik Boström87eece92020-04-17 18:36:19 +0200408 // Provies video stream input states: current resolution and frame rate.
Henrik Boströmc55516d2020-05-11 16:29:22 +0200409 // This class is thread-safe.
410 VideoStreamInputStateProvider input_state_provider_;
Henrik Boström87eece92020-04-17 18:36:19 +0200411 // Responsible for adapting input resolution or frame rate to ensure resources
412 // (e.g. CPU or bandwidth) are not overused.
Henrik Boström381d1092020-05-12 18:49:07 +0200413 // This class is single-threaded on the resource adaptation queue.
Henrik Boström3745d3f2020-04-30 10:13:29 +0200414 std::unique_ptr<ResourceAdaptationProcessorInterface>
Henrik Boström381d1092020-05-12 18:49:07 +0200415 resource_adaptation_processor_
416 RTC_GUARDED_BY(&resource_adaptation_queue_);
Henrik Boström0f0aa9c2020-06-02 13:02:36 +0200417 std::vector<AdaptationConstraint*> adaptation_constraints_
418 RTC_GUARDED_BY(&resource_adaptation_queue_);
419 std::vector<AdaptationListener*> adaptation_listeners_
420 RTC_GUARDED_BY(&resource_adaptation_queue_);
Henrik Boström87eece92020-04-17 18:36:19 +0200421 // Handles input, output and stats reporting related to VideoStreamEncoder
422 // specific resources, such as "encode usage percent" measurements and "QP
423 // scaling". Also involved with various mitigations such as inital frame
424 // dropping.
Henrik Boströmc55516d2020-05-11 16:29:22 +0200425 // The manager primarily operates on the |encoder_queue_| but its lifetime is
426 // tied to the VideoStreamEncoder (which is destroyed off the encoder queue)
427 // and its resource list is accessible from any thread.
Henrik Boström381d1092020-05-12 18:49:07 +0200428 VideoStreamEncoderResourceManager stream_resource_manager_;
Henrik Boström87eece92020-04-17 18:36:19 +0200429 // Carries out the VideoSourceRestrictions provided by the
430 // ResourceAdaptationProcessor, i.e. reconfigures the source of video frames
431 // to provide us with different resolution or frame rate.
Henrik Boströmc55516d2020-05-11 16:29:22 +0200432 // This class is thread-safe.
Henrik Boström87eece92020-04-17 18:36:19 +0200433 VideoSourceSinkController video_source_sink_controller_;
Henrik Boströmb08882b2020-01-07 10:11:17 +0100434
Henrik Boström381d1092020-05-12 18:49:07 +0200435 // Public methods are proxied to the task queues. The queues must be destroyed
436 // first to make sure no tasks run that use other members.
437 // TODO(https://crbug.com/webrtc/11172): Move ownership of the
438 // ResourceAdaptationProcessor and its task queue to Call when processors are
439 // multi-stream aware.
440 rtc::TaskQueue resource_adaptation_queue_;
philipele5d0fe02019-10-15 11:02:53 +0200441 rtc::TaskQueue encoder_queue_;
442
mflodmancc3d4422017-08-03 08:27:51 -0700443 RTC_DISALLOW_COPY_AND_ASSIGN(VideoStreamEncoder);
niklase@google.com470e71d2011-07-07 08:21:25 +0000444};
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000445
446} // namespace webrtc
447
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200448#endif // VIDEO_VIDEO_STREAM_ENCODER_H_