blob: 3b096329feb1738c1e46cf2827c815f2973cf24a [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
philipeld9cc8c02019-09-16 14:53:40 +020020#include "api/units/data_rate.h"
Jiawei Ou4206a0a2018-07-20 15:49:43 -070021#include "api/video/video_bitrate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/video/video_rotation.h"
Niels Möllerc6ce9c52018-05-11 11:15:30 +020023#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020024#include "api/video/video_stream_encoder_interface.h"
Niels Möller213618e2018-07-24 09:29:58 +020025#include "api/video/video_stream_encoder_observer.h"
26#include "api/video/video_stream_encoder_settings.h"
Erik Språng6a7baa72019-02-26 18:31:00 +010027#include "api/video_codecs/video_codec.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020028#include "api/video_codecs/video_encoder.h"
Niels Möller6bb5ab92019-01-11 11:11:10 +010029#include "modules/video_coding/utility/frame_dropper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/video_coding/utility/quality_scaler.h"
philipelda5aa4d2019-04-26 13:37:37 +020031#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/event.h"
Åsa Perssonf3d828e2019-05-06 12:22:49 +020033#include "rtc_base/experiments/balanced_degradation_settings.h"
Åsa Persson139f4dc2019-08-02 09:29:58 +020034#include "rtc_base/experiments/quality_scaler_settings.h"
Erik Språng7ca375c2019-02-06 16:20:17 +010035#include "rtc_base/experiments/rate_control_settings.h"
philipeld9cc8c02019-09-16 14:53:40 +020036#include "rtc_base/numerics/exp_filter.h"
Erik Språng6a7baa72019-02-26 18:31:00 +010037#include "rtc_base/race_checker.h"
Niels Möller6bb5ab92019-01-11 11:11:10 +010038#include "rtc_base/rate_statistics.h"
Sebastian Janssonb55015e2019-04-09 13:44:04 +020039#include "rtc_base/synchronization/sequence_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "rtc_base/task_queue.h"
Niels Möllerfe407b72019-09-10 10:48:48 +020041#include "system_wrappers/include/clock.h"
Erik Språng7ca375c2019-02-06 16:20:17 +010042#include "video/encoder_bitrate_adjuster.h"
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020043#include "video/frame_encode_metadata_writer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "video/overuse_frame_detector.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000045
46namespace webrtc {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000047
Sebastian Jansson652dc912018-04-19 17:09:15 +020048// VideoStreamEncoder represent a video encoder that accepts raw video frames as
49// input and produces an encoded bit stream.
50// Usage:
51// Instantiate.
52// Call SetSink.
53// Call SetSource.
54// Call ConfigureEncoder with the codec settings.
55// Call Stop() when done.
56class VideoStreamEncoder : public VideoStreamEncoderInterface,
57 private EncodedImageCallback,
58 // Protected only to provide access to tests.
59 protected AdaptationObserverInterface {
60 public:
Sebastian Jansson572c60f2019-03-04 18:30:41 +010061 VideoStreamEncoder(Clock* clock,
62 uint32_t number_of_cores,
Niels Möller213618e2018-07-24 09:29:58 +020063 VideoStreamEncoderObserver* encoder_stats_observer,
64 const VideoStreamEncoderSettings& settings,
Sebastian Jansson74682c12019-03-01 11:50:20 +010065 std::unique_ptr<OveruseFrameDetector> overuse_detector,
66 TaskQueueFactory* task_queue_factory);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020067 ~VideoStreamEncoder() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000068
Sebastian Jansson652dc912018-04-19 17:09:15 +020069 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source,
Taylor Brandstetter49fcc102018-05-16 14:20:41 -070070 const DegradationPreference& degradation_preference) override;
perkj803d97f2016-11-01 11:45:46 -070071
Sebastian Jansson652dc912018-04-19 17:09:15 +020072 void SetSink(EncoderSink* sink, bool rotation_applied) override;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000073
perkj26091b12016-09-01 01:17:40 -070074 // TODO(perkj): Can we remove VideoCodec.startBitrate ?
Sebastian Jansson652dc912018-04-19 17:09:15 +020075 void SetStartBitrate(int start_bitrate_bps) override;
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +000076
Niels Möller0327c2d2018-05-21 14:09:31 +020077 void SetBitrateAllocationObserver(
Sebastian Jansson652dc912018-04-19 17:09:15 +020078 VideoBitrateAllocationObserver* bitrate_observer) override;
sprang1a646ee2016-12-01 06:34:11 -080079
Elad Alon8f01c4e2019-06-28 15:19:43 +020080 void SetFecControllerOverride(
81 FecControllerOverride* fec_controller_override) override;
82
Per512ecb32016-09-23 15:52:06 +020083 void ConfigureEncoder(VideoEncoderConfig config,
Niels Möllerf1338562018-04-26 09:51:47 +020084 size_t max_data_payload_length) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000085
perkj26091b12016-09-01 01:17:40 -070086 // Permanently stop encoding. After this method has returned, it is
87 // guaranteed that no encoded frames will be delivered to the sink.
Sebastian Jansson652dc912018-04-19 17:09:15 +020088 void Stop() override;
perkj26091b12016-09-01 01:17:40 -070089
Sebastian Jansson652dc912018-04-19 17:09:15 +020090 void SendKeyFrame() override;
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +000091
Elad Alonb6ef99b2019-04-10 16:37:07 +020092 void OnLossNotification(
93 const VideoEncoder::LossNotification& loss_notification) override;
94
Erik Språng610c7632019-03-06 15:37:33 +010095 void OnBitrateUpdated(DataRate target_bitrate,
Florent Castellia8336d32019-09-09 13:36:55 +020096 DataRate stable_target_bitrate,
Erik Språng610c7632019-03-06 15:37:33 +010097 DataRate target_headroom,
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000098 uint8_t fraction_lost,
Sebastian Jansson652dc912018-04-19 17:09:15 +020099 int64_t round_trip_time_ms) override;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000100
perkj803d97f2016-11-01 11:45:46 -0700101 protected:
kthelgason876222f2016-11-29 01:44:11 -0800102 // Used for testing. For example the |ScalingObserverInterface| methods must
103 // be called on |encoder_queue_|.
perkj803d97f2016-11-01 11:45:46 -0700104 rtc::TaskQueue* encoder_queue() { return &encoder_queue_; }
105
Niels Möllerd692ef92017-10-04 15:28:55 +0200106 // AdaptationObserverInterface implementation.
perkj803d97f2016-11-01 11:45:46 -0700107 // These methods are protected for easier testing.
sprangb1ca0732017-02-01 08:38:12 -0800108 void AdaptUp(AdaptReason reason) override;
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200109 bool AdaptDown(AdaptReason reason) override;
perkj803d97f2016-11-01 11:45:46 -0700110
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000111 private:
perkja49cbd32016-09-16 07:53:41 -0700112 class VideoSourceProxy;
perkj26091b12016-09-01 01:17:40 -0700113
kthelgason93f16d72017-01-16 06:15:23 -0800114 class VideoFrameInfo {
115 public:
Yves Gerey665174f2018-06-19 15:03:05 +0200116 VideoFrameInfo(int width, int height, bool is_texture)
117 : width(width), height(height), is_texture(is_texture) {}
perkjfa10b552016-10-02 23:45:26 -0700118 int width;
119 int height;
perkjfa10b552016-10-02 23:45:26 -0700120 bool is_texture;
kthelgason93f16d72017-01-16 06:15:23 -0800121 int pixel_count() const { return width * height; }
perkjfa10b552016-10-02 23:45:26 -0700122 };
123
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200124 struct EncoderRateSettings {
Erik Språng4c6ca302019-04-08 15:14:01 +0200125 EncoderRateSettings();
126 EncoderRateSettings(const VideoBitrateAllocation& bitrate,
127 double framerate_fps,
128 DataRate bandwidth_allocation,
Florent Castellia8336d32019-09-09 13:36:55 +0200129 DataRate encoder_target,
130 DataRate stable_encoder_target);
Erik Språng4c6ca302019-04-08 15:14:01 +0200131 bool operator==(const EncoderRateSettings& rhs) const;
132 bool operator!=(const EncoderRateSettings& rhs) const;
133
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200134 VideoEncoder::RateControlParameters rate_control;
Erik Språng4c6ca302019-04-08 15:14:01 +0200135 // This is the scalar target bitrate before the VideoBitrateAllocator, i.e.
136 // the |target_bitrate| argument of the OnBitrateUpdated() method. This is
137 // needed because the bitrate allocator may truncate the total bitrate and a
138 // later call to the same allocator instance, e.g.
139 // |using last_encoder_rate_setings_->bitrate.get_sum_bps()|, may trick it
140 // into thinking the available bitrate has decreased since the last call.
141 DataRate encoder_target;
Florent Castellia8336d32019-09-09 13:36:55 +0200142 DataRate stable_encoder_target;
Erik Språng4c6ca302019-04-08 15:14:01 +0200143 };
144
Niels Möllera8b15082018-02-07 13:42:09 +0100145 void ReconfigureEncoder() RTC_RUN_ON(&encoder_queue_);
perkj26091b12016-09-01 01:17:40 -0700146
Erik Språng7ca375c2019-02-06 16:20:17 +0100147 void ConfigureQualityScaler(const VideoEncoder::EncoderInfo& encoder_info);
kthelgason2bc68642017-02-07 07:02:22 -0800148
perkja49cbd32016-09-16 07:53:41 -0700149 // Implements VideoSinkInterface.
150 void OnFrame(const VideoFrame& video_frame) override;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200151 void OnDiscardedFrame() override;
perkja49cbd32016-09-16 07:53:41 -0700152
Sebastian Janssona3177052018-04-10 13:05:49 +0200153 void MaybeEncodeVideoFrame(const VideoFrame& frame,
154 int64_t time_when_posted_in_ms);
155
perkjd52063f2016-09-07 06:32:18 -0700156 void EncodeVideoFrame(const VideoFrame& frame,
157 int64_t time_when_posted_in_ms);
Sebastian Janssona3177052018-04-10 13:05:49 +0200158 // Indicates wether frame should be dropped because the pixel count is too
159 // large for the current bitrate configuration.
160 bool DropDueToSize(uint32_t pixel_count) const RTC_RUN_ON(&encoder_queue_);
perkj26091b12016-09-01 01:17:40 -0700161
162 // Implements EncodedImageCallback.
163 EncodedImageCallback::Result OnEncodedImage(
164 const EncodedImage& encoded_image,
165 const CodecSpecificInfo* codec_specific_info,
166 const RTPFragmentationHeader* fragmentation) override;
167
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200168 void OnDroppedFrame(EncodedImageCallback::DropReason reason) override;
kthelgason876222f2016-11-29 01:44:11 -0800169
perkj26091b12016-09-01 01:17:40 -0700170 bool EncoderPaused() const;
171 void TraceFrameDropStart();
172 void TraceFrameDropEnd();
173
Erik Språng4c6ca302019-04-08 15:14:01 +0200174 // Returns a copy of |rate_settings| with the |bitrate| field updated using
175 // the current VideoBitrateAllocator, and notifies any listeners of the new
176 // allocation.
177 EncoderRateSettings UpdateBitrateAllocationAndNotifyObserver(
178 const EncoderRateSettings& rate_settings) RTC_RUN_ON(&encoder_queue_);
179
Niels Möller6bb5ab92019-01-11 11:11:10 +0100180 uint32_t GetInputFramerateFps() RTC_RUN_ON(&encoder_queue_);
Erik Språng4c6ca302019-04-08 15:14:01 +0200181 void SetEncoderRates(const EncoderRateSettings& rate_settings)
182 RTC_RUN_ON(&encoder_queue_);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100183
asapersson09f05612017-05-15 23:40:18 -0700184 // Class holding adaptation information.
185 class AdaptCounter final {
186 public:
187 AdaptCounter();
188 ~AdaptCounter();
189
190 // Get number of adaptation downscales for |reason|.
Niels Möller213618e2018-07-24 09:29:58 +0200191 VideoStreamEncoderObserver::AdaptationSteps Counts(int reason) const;
asapersson09f05612017-05-15 23:40:18 -0700192
193 std::string ToString() const;
194
asaperssonf7e294d2017-06-13 23:25:22 -0700195 void IncrementFramerate(int reason);
196 void IncrementResolution(int reason);
197 void DecrementFramerate(int reason);
198 void DecrementResolution(int reason);
199 void DecrementFramerate(int reason, int cur_fps);
asapersson09f05612017-05-15 23:40:18 -0700200
201 // Gets the total number of downgrades (for all adapt reasons).
202 int FramerateCount() const;
203 int ResolutionCount() const;
asapersson09f05612017-05-15 23:40:18 -0700204
205 // Gets the total number of downgrades for |reason|.
206 int FramerateCount(int reason) const;
207 int ResolutionCount(int reason) const;
208 int TotalCount(int reason) const;
209
210 private:
211 std::string ToString(const std::vector<int>& counters) const;
212 int Count(const std::vector<int>& counters) const;
asaperssonf7e294d2017-06-13 23:25:22 -0700213 void MoveCount(std::vector<int>* counters, int from_reason);
asapersson09f05612017-05-15 23:40:18 -0700214
215 // Degradation counters holding number of framerate/resolution reductions
216 // per adapt reason.
217 std::vector<int> fps_counters_;
218 std::vector<int> resolution_counters_;
219 };
220
danilchapa37de392017-09-09 04:17:22 -0700221 AdaptCounter& GetAdaptCounter() RTC_RUN_ON(&encoder_queue_);
222 const AdaptCounter& GetConstAdaptCounter() RTC_RUN_ON(&encoder_queue_);
223 void UpdateAdaptationStats(AdaptReason reason) RTC_RUN_ON(&encoder_queue_);
Niels Möller213618e2018-07-24 09:29:58 +0200224 VideoStreamEncoderObserver::AdaptationSteps GetActiveCounts(
225 AdaptReason reason) RTC_RUN_ON(&encoder_queue_);
Sergey Silkin41c650b2019-10-14 13:12:19 +0200226 bool CanAdaptUpResolution(int pixels, uint32_t bitrate_bps) const
227 RTC_RUN_ON(&encoder_queue_);
Erik Språng7ca375c2019-02-06 16:20:17 +0100228 void RunPostEncode(EncodedImage encoded_image,
Niels Möller6bb5ab92019-01-11 11:11:10 +0100229 int64_t time_sent_us,
Erik Språng7ca375c2019-02-06 16:20:17 +0100230 int temporal_index);
Erik Språngd7329ca2019-02-21 21:19:53 +0100231 bool HasInternalSource() const RTC_RUN_ON(&encoder_queue_);
Erik Språng6a7baa72019-02-26 18:31:00 +0100232 void ReleaseEncoder() RTC_RUN_ON(&encoder_queue_);
sprangc5d62e22017-04-02 23:53:04 -0700233
perkj26091b12016-09-01 01:17:40 -0700234 rtc::Event shutdown_event_;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000235
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000236 const uint32_t number_of_cores_;
Kári Tristan Helgason639602a2018-08-02 10:51:40 +0200237 // Counts how many frames we've dropped in the initial framedrop phase.
238 int initial_framedrop_;
239 const bool initial_framedrop_on_bwe_enabled_;
240 bool has_seen_first_significant_bwe_change_ = false;
perkja49cbd32016-09-16 07:53:41 -0700241
Åsa Perssona945aee2018-04-24 16:53:25 +0200242 const bool quality_scaling_experiment_enabled_;
243
perkja49cbd32016-09-16 07:53:41 -0700244 const std::unique_ptr<VideoSourceProxy> source_proxy_;
Per512ecb32016-09-23 15:52:06 +0200245 EncoderSink* sink_;
Niels Möller213618e2018-07-24 09:29:58 +0200246 const VideoStreamEncoderSettings settings_;
Erik Språng7ca375c2019-02-06 16:20:17 +0100247 const RateControlSettings rate_control_settings_;
Åsa Persson139f4dc2019-08-02 09:29:58 +0200248 const QualityScalerSettings quality_scaler_settings_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000249
Niels Möllera8b15082018-02-07 13:42:09 +0100250 const std::unique_ptr<OveruseFrameDetector> overuse_detector_
251 RTC_PT_GUARDED_BY(&encoder_queue_);
Yves Gerey665174f2018-06-19 15:03:05 +0200252 std::unique_ptr<QualityScaler> quality_scaler_ RTC_GUARDED_BY(&encoder_queue_)
Niels Möllera8b15082018-02-07 13:42:09 +0100253 RTC_PT_GUARDED_BY(&encoder_queue_);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000254
Niels Möller213618e2018-07-24 09:29:58 +0200255 VideoStreamEncoderObserver* const encoder_stats_observer_;
perkja49cbd32016-09-16 07:53:41 -0700256 // |thread_checker_| checks that public methods that are related to lifetime
mflodmancc3d4422017-08-03 08:27:51 -0700257 // of VideoStreamEncoder are called on the same thread.
perkja49cbd32016-09-16 07:53:41 -0700258 rtc::ThreadChecker thread_checker_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000259
Niels Möller1e062892018-02-07 10:18:32 +0100260 VideoEncoderConfig encoder_config_ RTC_GUARDED_BY(&encoder_queue_);
Niels Möller4db138e2018-04-19 09:04:13 +0200261 std::unique_ptr<VideoEncoder> encoder_ RTC_GUARDED_BY(&encoder_queue_)
262 RTC_PT_GUARDED_BY(&encoder_queue_);
Erik Språng6a7baa72019-02-26 18:31:00 +0100263 bool encoder_initialized_;
Erik Språng08127a92016-11-16 16:41:30 +0100264 std::unique_ptr<VideoBitrateAllocator> rate_allocator_
Yves Gerey665174f2018-06-19 15:03:05 +0200265 RTC_GUARDED_BY(&encoder_queue_) RTC_PT_GUARDED_BY(&encoder_queue_);
sprangfda496a2017-06-15 04:21:07 -0700266 // The maximum frame rate of the current codec configuration, as determined
267 // at the last ReconfigureEncoder() call.
Niels Möller1e062892018-02-07 10:18:32 +0100268 int max_framerate_ RTC_GUARDED_BY(&encoder_queue_);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000269
perkjfa10b552016-10-02 23:45:26 -0700270 // Set when ConfigureEncoder has been called in order to lazy reconfigure the
271 // encoder on the next frame.
Niels Möller1e062892018-02-07 10:18:32 +0100272 bool pending_encoder_reconfiguration_ RTC_GUARDED_BY(&encoder_queue_);
Mirta Dvornicic1ec2a162018-12-10 09:47:34 +0000273 // Set when configuration must create a new encoder object, e.g.,
274 // because of a codec change.
275 bool pending_encoder_creation_ RTC_GUARDED_BY(&encoder_queue_);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200276
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200277 absl::optional<VideoFrameInfo> last_frame_info_
Niels Möller1e062892018-02-07 10:18:32 +0100278 RTC_GUARDED_BY(&encoder_queue_);
279 int crop_width_ RTC_GUARDED_BY(&encoder_queue_);
280 int crop_height_ RTC_GUARDED_BY(&encoder_queue_);
281 uint32_t encoder_start_bitrate_bps_ RTC_GUARDED_BY(&encoder_queue_);
Åsa Persson139f4dc2019-08-02 09:29:58 +0200282 int set_start_bitrate_bps_ RTC_GUARDED_BY(&encoder_queue_);
283 int64_t set_start_bitrate_time_ms_ RTC_GUARDED_BY(&encoder_queue_);
284 bool has_seen_first_bwe_drop_ RTC_GUARDED_BY(&encoder_queue_);
Niels Möller1e062892018-02-07 10:18:32 +0100285 size_t max_data_payload_length_ RTC_GUARDED_BY(&encoder_queue_);
Erik Språng4c6ca302019-04-08 15:14:01 +0200286 absl::optional<EncoderRateSettings> last_encoder_rate_settings_
287 RTC_GUARDED_BY(&encoder_queue_);
Niels Möller1e062892018-02-07 10:18:32 +0100288 bool encoder_paused_and_dropped_frame_ RTC_GUARDED_BY(&encoder_queue_);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200289
290 // Set to true if at least one frame was sent to encoder since last encoder
291 // initialization.
292 bool was_encode_called_since_last_initialization_
293 RTC_GUARDED_BY(&encoder_queue_);
294
philipele8ed8302019-07-03 11:53:48 +0200295 bool encoder_failed_ RTC_GUARDED_BY(&encoder_queue_);
perkj26091b12016-09-01 01:17:40 -0700296 Clock* const clock_;
asapersson09f05612017-05-15 23:40:18 -0700297 // Counters used for deciding if the video resolution or framerate is
298 // currently restricted, and if so, why, on a per degradation preference
299 // basis.
sprangc5d62e22017-04-02 23:53:04 -0700300 // TODO(sprang): Replace this with a state holding a relative overuse measure
301 // instead, that can be translated into suitable down-scale or fps limit.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700302 std::map<const DegradationPreference, AdaptCounter> adapt_counters_
Niels Möller1e062892018-02-07 10:18:32 +0100303 RTC_GUARDED_BY(&encoder_queue_);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700304 // Set depending on degradation preferences.
305 DegradationPreference degradation_preference_ RTC_GUARDED_BY(&encoder_queue_);
perkj803d97f2016-11-01 11:45:46 -0700306
Åsa Persson12314192019-06-20 15:45:07 +0200307 const BalancedDegradationSettings balanced_settings_;
Åsa Perssonf3d828e2019-05-06 12:22:49 +0200308
sprang84a37592017-02-10 07:04:27 -0800309 struct AdaptationRequest {
310 // The pixel count produced by the source at the time of the adaptation.
311 int input_pixel_count_;
sprangc5d62e22017-04-02 23:53:04 -0700312 // Framerate received from the source at the time of the adaptation.
313 int framerate_fps_;
sprang84a37592017-02-10 07:04:27 -0800314 // Indicates if request was to adapt up or down.
315 enum class Mode { kAdaptUp, kAdaptDown } mode_;
316 };
317 // Stores a snapshot of the last adaptation request triggered by an AdaptUp
318 // or AdaptDown signal.
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200319 absl::optional<AdaptationRequest> last_adaptation_request_
Niels Möller1e062892018-02-07 10:18:32 +0100320 RTC_GUARDED_BY(&encoder_queue_);
perkj803d97f2016-11-01 11:45:46 -0700321
322 rtc::RaceChecker incoming_frame_race_checker_
danilchapa37de392017-09-09 04:17:22 -0700323 RTC_GUARDED_BY(incoming_frame_race_checker_);
Yuwei Huangd9f99c12017-10-24 15:40:52 -0700324 std::atomic<int> posted_frames_waiting_for_encode_;
perkj26091b12016-09-01 01:17:40 -0700325 // Used to make sure incoming time stamp is increasing for every frame.
danilchapa37de392017-09-09 04:17:22 -0700326 int64_t last_captured_timestamp_ RTC_GUARDED_BY(incoming_frame_race_checker_);
perkj26091b12016-09-01 01:17:40 -0700327 // Delta used for translating between NTP and internal timestamps.
danilchapa37de392017-09-09 04:17:22 -0700328 const int64_t delta_ntp_internal_ms_
329 RTC_GUARDED_BY(incoming_frame_race_checker_);
perkj26091b12016-09-01 01:17:40 -0700330
danilchapa37de392017-09-09 04:17:22 -0700331 int64_t last_frame_log_ms_ RTC_GUARDED_BY(incoming_frame_race_checker_);
Niels Möller1e062892018-02-07 10:18:32 +0100332 int captured_frame_count_ RTC_GUARDED_BY(&encoder_queue_);
333 int dropped_frame_count_ RTC_GUARDED_BY(&encoder_queue_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200334 absl::optional<VideoFrame> pending_frame_ RTC_GUARDED_BY(&encoder_queue_);
Sebastian Janssona3177052018-04-10 13:05:49 +0200335 int64_t pending_frame_post_time_us_ RTC_GUARDED_BY(&encoder_queue_);
asapersson6ffb67d2016-09-12 00:10:45 -0700336
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100337 VideoFrame::UpdateRect accumulated_update_rect_
338 RTC_GUARDED_BY(&encoder_queue_);
339
danilchapa37de392017-09-09 04:17:22 -0700340 VideoBitrateAllocationObserver* bitrate_observer_
Niels Möller1e062892018-02-07 10:18:32 +0100341 RTC_GUARDED_BY(&encoder_queue_);
Elad Alon8f01c4e2019-06-28 15:19:43 +0200342 FecControllerOverride* fec_controller_override_
343 RTC_GUARDED_BY(&encoder_queue_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200344 absl::optional<int64_t> last_parameters_update_ms_
Niels Möller1e062892018-02-07 10:18:32 +0100345 RTC_GUARDED_BY(&encoder_queue_);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100346 absl::optional<int64_t> last_encode_info_ms_ RTC_GUARDED_BY(&encoder_queue_);
sprang1a646ee2016-12-01 06:34:11 -0800347
Erik Språnge2fd86a2018-10-24 11:32:39 +0200348 VideoEncoder::EncoderInfo encoder_info_ RTC_GUARDED_BY(&encoder_queue_);
Sergey Silkin6456e352019-07-08 17:56:40 +0200349 absl::optional<VideoEncoder::ResolutionBitrateLimits> encoder_bitrate_limits_
350 RTC_GUARDED_BY(&encoder_queue_);
Erik Språngd7329ca2019-02-21 21:19:53 +0100351 VideoEncoderFactory::CodecInfo codec_info_ RTC_GUARDED_BY(&encoder_queue_);
Erik Språng6a7baa72019-02-26 18:31:00 +0100352 VideoCodec send_codec_ RTC_GUARDED_BY(&encoder_queue_);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100353
Erik Språng6a7baa72019-02-26 18:31:00 +0100354 FrameDropper frame_dropper_ RTC_GUARDED_BY(&encoder_queue_);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100355 // If frame dropper is not force disabled, frame dropping might still be
356 // disabled if VideoEncoder::GetEncoderInfo() indicates that the encoder has a
357 // trusted rate controller. This is determined on a per-frame basis, as the
358 // encoder behavior might dynamically change.
359 bool force_disable_frame_dropper_ RTC_GUARDED_BY(&encoder_queue_);
360 RateStatistics input_framerate_ RTC_GUARDED_BY(&encoder_queue_);
361 // Incremented on worker thread whenever |frame_dropper_| determines that a
362 // frame should be dropped. Decremented on whichever thread runs
363 // OnEncodedImage(), which is only called by one thread but not necessarily
364 // the worker thread.
365 std::atomic<int> pending_frame_drops_;
Erik Språnge2fd86a2018-10-24 11:32:39 +0200366
Erik Språng7ca375c2019-02-06 16:20:17 +0100367 std::unique_ptr<EncoderBitrateAdjuster> bitrate_adjuster_
368 RTC_GUARDED_BY(&encoder_queue_);
369
Erik Språngd7329ca2019-02-21 21:19:53 +0100370 // TODO(sprang): Change actually support keyframe per simulcast stream, or
371 // turn this into a simple bool |pending_keyframe_request_|.
Niels Möller87e2d782019-03-07 10:18:23 +0100372 std::vector<VideoFrameType> next_frame_types_ RTC_GUARDED_BY(&encoder_queue_);
Erik Språngd7329ca2019-02-21 21:19:53 +0100373
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +0200374 FrameEncodeMetadataWriter frame_encode_metadata_writer_;
Erik Språng6a7baa72019-02-26 18:31:00 +0100375
376 // Experiment groups parsed from field trials for realtime video ([0]) and
377 // screenshare ([1]). 0 means no group specified. Positive values are
378 // experiment group numbers incremented by 1.
379 const std::array<uint8_t, 2> experiment_groups_;
380
philipelda5aa4d2019-04-26 13:37:37 +0200381 // TODO(philipel): Remove this lock and run on |encoder_queue_| instead.
382 rtc::CriticalSection encoded_image_lock_;
383
384 int64_t next_frame_id_ RTC_GUARDED_BY(encoded_image_lock_);
385
386 // This array is used as a map from simulcast id to an encoder's buffer
387 // state. For every buffer of the encoder we keep track of the last frame id
388 // that updated that buffer.
389 std::array<std::array<int64_t, kMaxEncoderBuffers>, kMaxSimulcastStreams>
390 encoder_buffer_state_ RTC_GUARDED_BY(encoded_image_lock_);
391
philipeld9cc8c02019-09-16 14:53:40 +0200392 struct EncoderSwitchExperiment {
393 struct Thresholds {
394 absl::optional<DataRate> bitrate;
395 absl::optional<int> pixel_count;
396 };
397
398 // Codec --> switching thresholds
399 std::map<VideoCodecType, Thresholds> codec_thresholds;
400
401 // To smooth out the target bitrate so that we don't trigger a switch
402 // too easily.
403 rtc::ExpFilter bitrate_filter{1.0};
404
405 // Codec/implementation to switch to
406 std::string to_codec;
407 absl::optional<std::string> to_param;
408 absl::optional<std::string> to_value;
409
410 // Thresholds for the currently used codecs.
411 Thresholds current_thresholds;
412
413 // Updates the |bitrate_filter|, so not const.
414 bool IsBitrateBelowThreshold(const DataRate& target_bitrate);
415 bool IsPixelCountBelowThreshold(int pixel_count) const;
416 void SetCodec(VideoCodecType codec);
417 };
418
419 EncoderSwitchExperiment ParseEncoderSwitchFieldTrial() const;
420
421 EncoderSwitchExperiment encoder_switch_experiment_
422 RTC_GUARDED_BY(&encoder_queue_);
423
424 // An encoder switch is only requested once, this variable is used to keep
425 // track of whether a request has been made or not.
426 bool encoder_switch_requested_ RTC_GUARDED_BY(&encoder_queue_);
427
philipele5d0fe02019-10-15 11:02:53 +0200428 // All public methods are proxied to |encoder_queue_|. It must must be
429 // destroyed first to make sure no tasks are run that use other members.
430 rtc::TaskQueue encoder_queue_;
431
mflodmancc3d4422017-08-03 08:27:51 -0700432 RTC_DISALLOW_COPY_AND_ASSIGN(VideoStreamEncoder);
niklase@google.com470e71d2011-07-07 08:21:25 +0000433};
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000434
435} // namespace webrtc
436
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200437#endif // VIDEO_VIDEO_STREAM_ENCODER_H_