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 | |
Henrik Boström | e2e8c17 | 2020-06-03 09:24:06 +0200 | [diff] [blame] | 20 | #include "api/adaptation/resource.h" |
philipel | d9cc8c0 | 2019-09-16 14:53:40 +0200 | [diff] [blame] | 21 | #include "api/units/data_rate.h" |
Jiawei Ou | 4206a0a | 2018-07-20 15:49:43 -0700 | [diff] [blame] | 22 | #include "api/video/video_bitrate_allocator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "api/video/video_rotation.h" |
Niels Möller | c6ce9c5 | 2018-05-11 11:15:30 +0200 | [diff] [blame] | 24 | #include "api/video/video_sink_interface.h" |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 25 | #include "api/video/video_stream_encoder_interface.h" |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 26 | #include "api/video/video_stream_encoder_observer.h" |
| 27 | #include "api/video/video_stream_encoder_settings.h" |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 28 | #include "api/video_codecs/video_codec.h" |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 29 | #include "api/video_codecs/video_encoder.h" |
Henrik Boström | 0f0aa9c | 2020-06-02 13:02:36 +0200 | [diff] [blame] | 30 | #include "call/adaptation/adaptation_constraint.h" |
| 31 | #include "call/adaptation/adaptation_listener.h" |
Henrik Boström | 6205762 | 2020-03-10 19:08:05 +0100 | [diff] [blame] | 32 | #include "call/adaptation/resource_adaptation_processor_interface.h" |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 33 | #include "call/adaptation/video_source_restrictions.h" |
Henrik Boström | d516b25 | 2020-04-17 12:10:59 +0200 | [diff] [blame] | 34 | #include "call/adaptation/video_stream_input_state_provider.h" |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 35 | #include "modules/video_coding/utility/frame_dropper.h" |
philipel | da5aa4d | 2019-04-26 13:37:37 +0200 | [diff] [blame] | 36 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 37 | #include "rtc_base/event.h" |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 38 | #include "rtc_base/experiments/rate_control_settings.h" |
philipel | d9cc8c0 | 2019-09-16 14:53:40 +0200 | [diff] [blame] | 39 | #include "rtc_base/numerics/exp_filter.h" |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 40 | #include "rtc_base/race_checker.h" |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 41 | #include "rtc_base/rate_statistics.h" |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 42 | #include "rtc_base/synchronization/sequence_checker.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 43 | #include "rtc_base/task_queue.h" |
Tommi | a0a4480 | 2020-05-13 18:27:26 +0200 | [diff] [blame] | 44 | #include "rtc_base/thread_checker.h" |
Niels Möller | fe407b7 | 2019-09-10 10:48:48 +0200 | [diff] [blame] | 45 | #include "system_wrappers/include/clock.h" |
Henrik Boström | de8d551 | 2020-04-17 17:15:26 +0200 | [diff] [blame] | 46 | #include "video/adaptation/video_stream_encoder_resource_manager.h" |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 47 | #include "video/encoder_bitrate_adjuster.h" |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 48 | #include "video/frame_encode_metadata_writer.h" |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 49 | #include "video/video_source_sink_controller.h" |
Henrik Boström | 0f0aa9c | 2020-06-02 13:02:36 +0200 | [diff] [blame] | 50 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | namespace webrtc { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 52 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 53 | // 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. |
| 61 | class VideoStreamEncoder : public VideoStreamEncoderInterface, |
Henrik Boström | d238200 | 2020-01-10 15:44:01 +0100 | [diff] [blame] | 62 | private EncodedImageCallback, |
Henrik Boström | 0f0aa9c | 2020-06-02 13:02:36 +0200 | [diff] [blame] | 63 | public VideoSourceRestrictionsListener { |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 64 | public: |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 65 | VideoStreamEncoder(Clock* clock, |
| 66 | uint32_t number_of_cores, |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 67 | VideoStreamEncoderObserver* encoder_stats_observer, |
| 68 | const VideoStreamEncoderSettings& settings, |
Sebastian Jansson | 74682c1 | 2019-03-01 11:50:20 +0100 | [diff] [blame] | 69 | std::unique_ptr<OveruseFrameDetector> overuse_detector, |
| 70 | TaskQueueFactory* task_queue_factory); |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 71 | ~VideoStreamEncoder() override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 72 | |
Henrik Boström | f4a9991 | 2020-06-11 12:07:14 +0200 | [diff] [blame] | 73 | void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) override; |
| 74 | std::vector<rtc::scoped_refptr<Resource>> GetAdaptationResources() override; |
| 75 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 76 | void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 77 | const DegradationPreference& degradation_preference) override; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 78 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 79 | void SetSink(EncoderSink* sink, bool rotation_applied) override; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 80 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 81 | // TODO(perkj): Can we remove VideoCodec.startBitrate ? |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 82 | void SetStartBitrate(int start_bitrate_bps) override; |
mflodman@webrtc.org | 9ec883e | 2012-03-05 17:12:41 +0000 | [diff] [blame] | 83 | |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 84 | void SetBitrateAllocationObserver( |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 85 | VideoBitrateAllocationObserver* bitrate_observer) override; |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 86 | |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 87 | void SetFecControllerOverride( |
| 88 | FecControllerOverride* fec_controller_override) override; |
| 89 | |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 90 | void ConfigureEncoder(VideoEncoderConfig config, |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 91 | size_t max_data_payload_length) override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 92 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 93 | // Permanently stop encoding. After this method has returned, it is |
| 94 | // guaranteed that no encoded frames will be delivered to the sink. |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 95 | void Stop() override; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 96 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 97 | void SendKeyFrame() override; |
mflodman@webrtc.org | d6ec386 | 2012-10-25 11:30:29 +0000 | [diff] [blame] | 98 | |
Elad Alon | b6ef99b | 2019-04-10 16:37:07 +0200 | [diff] [blame] | 99 | void OnLossNotification( |
| 100 | const VideoEncoder::LossNotification& loss_notification) override; |
| 101 | |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 102 | void OnBitrateUpdated(DataRate target_bitrate, |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 103 | DataRate stable_target_bitrate, |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 104 | DataRate target_headroom, |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 105 | uint8_t fraction_lost, |
Ying Wang | 9b881ab | 2020-02-07 14:29:32 +0100 | [diff] [blame] | 106 | 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.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 111 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 112 | protected: |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 113 | // Used for testing. For example the |ScalingObserverInterface| methods must |
| 114 | // be called on |encoder_queue_|. |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 115 | rtc::TaskQueue* encoder_queue() { return &encoder_queue_; } |
Henrik Boström | 381d109 | 2020-05-12 18:49:07 +0200 | [diff] [blame] | 116 | rtc::TaskQueue* resource_adaptation_queue() { |
| 117 | return &resource_adaptation_queue_; |
| 118 | } |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 119 | |
Henrik Boström | d238200 | 2020-01-10 15:44:01 +0100 | [diff] [blame] | 120 | void OnVideoSourceRestrictionsUpdated( |
Henrik Boström | d2930c6 | 2020-04-17 15:31:48 +0200 | [diff] [blame] | 121 | VideoSourceRestrictions restrictions, |
| 122 | const VideoAdaptationCounters& adaptation_counters, |
Henrik Boström | c55516d | 2020-05-11 16:29:22 +0200 | [diff] [blame] | 123 | rtc::scoped_refptr<Resource> reason) override; |
Henrik Boström | d238200 | 2020-01-10 15:44:01 +0100 | [diff] [blame] | 124 | |
Evan Shrubsole | aa6fbc1 | 2020-02-25 16:26:01 +0100 | [diff] [blame] | 125 | // Used for injected test resources. |
| 126 | // TODO(eshr): Move all adaptation tests out of VideoStreamEncoder tests. |
Henrik Boström | c55516d | 2020-05-11 16:29:22 +0200 | [diff] [blame] | 127 | void InjectAdaptationResource(rtc::scoped_refptr<Resource> resource, |
Henrik Boström | 0f0aa9c | 2020-06-02 13:02:36 +0200 | [diff] [blame] | 128 | VideoAdaptationReason reason); |
| 129 | void InjectAdaptationConstraint(AdaptationConstraint* adaptation_constraint); |
| 130 | void InjectAdaptationListener(AdaptationListener* adaptation_listener); |
Evan Shrubsole | aa6fbc1 | 2020-02-25 16:26:01 +0100 | [diff] [blame] | 131 | |
Henrik Boström | c55516d | 2020-05-11 16:29:22 +0200 | [diff] [blame] | 132 | rtc::scoped_refptr<QualityScalerResource> |
| 133 | quality_scaler_resource_for_testing(); |
Henrik Boström | 91aa732 | 2020-04-28 12:24:33 +0200 | [diff] [blame] | 134 | |
Henrik Boström | 0f0aa9c | 2020-06-02 13:02:36 +0200 | [diff] [blame] | 135 | void AddRestrictionsListenerForTesting( |
| 136 | VideoSourceRestrictionsListener* restrictions_listener); |
| 137 | void RemoveRestrictionsListenerForTesting( |
| 138 | VideoSourceRestrictionsListener* restrictions_listener); |
Henrik Boström | 381d109 | 2020-05-12 18:49:07 +0200 | [diff] [blame] | 139 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 140 | private: |
kthelgason | 93f16d7 | 2017-01-16 06:15:23 -0800 | [diff] [blame] | 141 | class VideoFrameInfo { |
| 142 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 143 | VideoFrameInfo(int width, int height, bool is_texture) |
| 144 | : width(width), height(height), is_texture(is_texture) {} |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 145 | int width; |
| 146 | int height; |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 147 | bool is_texture; |
kthelgason | 93f16d7 | 2017-01-16 06:15:23 -0800 | [diff] [blame] | 148 | int pixel_count() const { return width * height; } |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
Evan Shrubsole | 7c079f6 | 2019-09-26 09:55:03 +0200 | [diff] [blame] | 151 | struct EncoderRateSettings { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 152 | EncoderRateSettings(); |
| 153 | EncoderRateSettings(const VideoBitrateAllocation& bitrate, |
| 154 | double framerate_fps, |
| 155 | DataRate bandwidth_allocation, |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 156 | DataRate encoder_target, |
| 157 | DataRate stable_encoder_target); |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 158 | bool operator==(const EncoderRateSettings& rhs) const; |
| 159 | bool operator!=(const EncoderRateSettings& rhs) const; |
| 160 | |
Evan Shrubsole | 7c079f6 | 2019-09-26 09:55:03 +0200 | [diff] [blame] | 161 | VideoEncoder::RateControlParameters rate_control; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 162 | // 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 Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 169 | DataRate stable_encoder_target; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 170 | }; |
| 171 | |
Niels Möller | a8b1508 | 2018-02-07 13:42:09 +0100 | [diff] [blame] | 172 | void ReconfigureEncoder() RTC_RUN_ON(&encoder_queue_); |
Henrik Boström | d516b25 | 2020-04-17 12:10:59 +0200 | [diff] [blame] | 173 | void OnEncoderSettingsChanged() RTC_RUN_ON(&encoder_queue_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 174 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 175 | // Implements VideoSinkInterface. |
| 176 | void OnFrame(const VideoFrame& video_frame) override; |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 177 | void OnDiscardedFrame() override; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 178 | |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 179 | void MaybeEncodeVideoFrame(const VideoFrame& frame, |
| 180 | int64_t time_when_posted_in_ms); |
| 181 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 182 | void EncodeVideoFrame(const VideoFrame& frame, |
| 183 | int64_t time_when_posted_in_ms); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 184 | // 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_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 187 | |
| 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 Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 194 | void OnDroppedFrame(EncodedImageCallback::DropReason reason) override; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 195 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 196 | bool EncoderPaused() const; |
| 197 | void TraceFrameDropStart(); |
| 198 | void TraceFrameDropEnd(); |
| 199 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 200 | // 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öller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 206 | uint32_t GetInputFramerateFps() RTC_RUN_ON(&encoder_queue_); |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 207 | void SetEncoderRates(const EncoderRateSettings& rate_settings) |
| 208 | RTC_RUN_ON(&encoder_queue_); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 209 | |
Evan Shrubsole | 2bc91e8 | 2020-01-17 11:44:24 +0100 | [diff] [blame] | 210 | void RunPostEncode(const EncodedImage& encoded_image, |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 211 | int64_t time_sent_us, |
philipel | c7a46c4 | 2019-11-25 15:01:09 +0100 | [diff] [blame] | 212 | int temporal_index, |
| 213 | DataSize frame_size); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 214 | bool HasInternalSource() const RTC_RUN_ON(&encoder_queue_); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 215 | void ReleaseEncoder() RTC_RUN_ON(&encoder_queue_); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 216 | |
Ilya Nikolaevskiy | 648b9d7 | 2019-12-03 16:54:17 +0100 | [diff] [blame] | 217 | void CheckForAnimatedContent(const VideoFrame& frame, |
| 218 | int64_t time_when_posted_in_ms) |
| 219 | RTC_RUN_ON(&encoder_queue_); |
| 220 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 221 | rtc::Event shutdown_event_; |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 222 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 223 | const uint32_t number_of_cores_; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 224 | |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 225 | const bool quality_scaling_experiment_enabled_; |
| 226 | |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 227 | EncoderSink* sink_; |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 228 | const VideoStreamEncoderSettings settings_; |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 229 | const RateControlSettings rate_control_settings_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 230 | |
philipel | 9b05803 | 2020-02-10 11:30:00 +0100 | [diff] [blame] | 231 | std::unique_ptr<VideoEncoderFactory::EncoderSelectorInterface> const |
| 232 | encoder_selector_; |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 233 | VideoStreamEncoderObserver* const encoder_stats_observer_; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 234 | // |thread_checker_| checks that public methods that are related to lifetime |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 235 | // of VideoStreamEncoder are called on the same thread. |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 236 | rtc::ThreadChecker thread_checker_; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 237 | |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 238 | VideoEncoderConfig encoder_config_ RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 239 | std::unique_ptr<VideoEncoder> encoder_ RTC_GUARDED_BY(&encoder_queue_) |
| 240 | RTC_PT_GUARDED_BY(&encoder_queue_); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 241 | bool encoder_initialized_; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 242 | std::unique_ptr<VideoBitrateAllocator> rate_allocator_ |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 243 | RTC_GUARDED_BY(&encoder_queue_) RTC_PT_GUARDED_BY(&encoder_queue_); |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 244 | int max_framerate_ RTC_GUARDED_BY(&encoder_queue_); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 245 | |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 246 | // Set when ConfigureEncoder has been called in order to lazy reconfigure the |
| 247 | // encoder on the next frame. |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 248 | bool pending_encoder_reconfiguration_ RTC_GUARDED_BY(&encoder_queue_); |
Mirta Dvornicic | 1ec2a16 | 2018-12-10 09:47:34 +0000 | [diff] [blame] | 249 | // 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 Silkin | 5ee6967 | 2019-07-02 14:18:34 +0200 | [diff] [blame] | 252 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 253 | absl::optional<VideoFrameInfo> last_frame_info_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 254 | 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öm | ede69c0 | 2020-01-21 17:45:35 +0100 | [diff] [blame] | 257 | absl::optional<uint32_t> encoder_target_bitrate_bps_ |
| 258 | RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 259 | size_t max_data_payload_length_ RTC_GUARDED_BY(&encoder_queue_); |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 260 | absl::optional<EncoderRateSettings> last_encoder_rate_settings_ |
| 261 | RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 262 | bool encoder_paused_and_dropped_frame_ RTC_GUARDED_BY(&encoder_queue_); |
Sergey Silkin | 5ee6967 | 2019-07-02 14:18:34 +0200 | [diff] [blame] | 263 | |
| 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 | |
philipel | e8ed830 | 2019-07-03 11:53:48 +0200 | [diff] [blame] | 269 | bool encoder_failed_ RTC_GUARDED_BY(&encoder_queue_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 270 | Clock* const clock_; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 271 | |
| 272 | rtc::RaceChecker incoming_frame_race_checker_ |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 273 | RTC_GUARDED_BY(incoming_frame_race_checker_); |
Yuwei Huang | d9f99c1 | 2017-10-24 15:40:52 -0700 | [diff] [blame] | 274 | std::atomic<int> posted_frames_waiting_for_encode_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 275 | // Used to make sure incoming time stamp is increasing for every frame. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 276 | int64_t last_captured_timestamp_ RTC_GUARDED_BY(incoming_frame_race_checker_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 277 | // Delta used for translating between NTP and internal timestamps. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 278 | const int64_t delta_ntp_internal_ms_ |
| 279 | RTC_GUARDED_BY(incoming_frame_race_checker_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 280 | |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 281 | 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] | 282 | int captured_frame_count_ RTC_GUARDED_BY(&encoder_queue_); |
Ying Wang | 9b881ab | 2020-02-07 14:29:32 +0100 | [diff] [blame] | 283 | int dropped_frame_cwnd_pushback_count_ RTC_GUARDED_BY(&encoder_queue_); |
| 284 | int dropped_frame_encoder_block_count_ RTC_GUARDED_BY(&encoder_queue_); |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 285 | absl::optional<VideoFrame> pending_frame_ RTC_GUARDED_BY(&encoder_queue_); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 286 | int64_t pending_frame_post_time_us_ RTC_GUARDED_BY(&encoder_queue_); |
asapersson | 6ffb67d | 2016-09-12 00:10:45 -0700 | [diff] [blame] | 287 | |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 13:01:26 +0100 | [diff] [blame] | 288 | VideoFrame::UpdateRect accumulated_update_rect_ |
| 289 | RTC_GUARDED_BY(&encoder_queue_); |
Ilya Nikolaevskiy | 9560d7d | 2019-10-30 11:19:47 +0100 | [diff] [blame] | 290 | bool accumulated_update_rect_is_valid_ RTC_GUARDED_BY(&encoder_queue_); |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 13:01:26 +0100 | [diff] [blame] | 291 | |
Ilya Nikolaevskiy | 648b9d7 | 2019-12-03 16:54:17 +0100 | [diff] [blame] | 292 | // 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 | |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 305 | VideoBitrateAllocationObserver* bitrate_observer_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 306 | RTC_GUARDED_BY(&encoder_queue_); |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 307 | FecControllerOverride* fec_controller_override_ |
| 308 | RTC_GUARDED_BY(&encoder_queue_); |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 309 | absl::optional<int64_t> last_parameters_update_ms_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 310 | RTC_GUARDED_BY(&encoder_queue_); |
Åsa Persson | c29cb2c | 2019-03-25 12:06:59 +0100 | [diff] [blame] | 311 | absl::optional<int64_t> last_encode_info_ms_ RTC_GUARDED_BY(&encoder_queue_); |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 312 | |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 313 | VideoEncoder::EncoderInfo encoder_info_ RTC_GUARDED_BY(&encoder_queue_); |
Sergey Silkin | 6456e35 | 2019-07-08 17:56:40 +0200 | [diff] [blame] | 314 | absl::optional<VideoEncoder::ResolutionBitrateLimits> encoder_bitrate_limits_ |
| 315 | RTC_GUARDED_BY(&encoder_queue_); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 316 | VideoEncoderFactory::CodecInfo codec_info_ RTC_GUARDED_BY(&encoder_queue_); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 317 | VideoCodec send_codec_ RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 318 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 319 | FrameDropper frame_dropper_ RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 320 | // 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ång | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 331 | |
Ying Wang | 9b881ab | 2020-02-07 14:29:32 +0100 | [diff] [blame] | 332 | // 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ång | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 338 | std::unique_ptr<EncoderBitrateAdjuster> bitrate_adjuster_ |
| 339 | RTC_GUARDED_BY(&encoder_queue_); |
| 340 | |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 341 | // TODO(sprang): Change actually support keyframe per simulcast stream, or |
| 342 | // turn this into a simple bool |pending_keyframe_request_|. |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 343 | std::vector<VideoFrameType> next_frame_types_ RTC_GUARDED_BY(&encoder_queue_); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 344 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 345 | FrameEncodeMetadataWriter frame_encode_metadata_writer_; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 346 | |
| 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 | |
philipel | d9cc8c0 | 2019-09-16 14:53:40 +0200 | [diff] [blame] | 352 | 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 Nikolaevskiy | 648b9d7 | 2019-12-03 16:54:17 +0100 | [diff] [blame] | 384 | 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 | |
philipel | d9cc8c0 | 2019-09-16 14:53:40 +0200 | [diff] [blame] | 404 | // 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öm | 87eece9 | 2020-04-17 18:36:19 +0200 | [diff] [blame] | 408 | // Provies video stream input states: current resolution and frame rate. |
Henrik Boström | c55516d | 2020-05-11 16:29:22 +0200 | [diff] [blame] | 409 | // This class is thread-safe. |
| 410 | VideoStreamInputStateProvider input_state_provider_; |
Henrik Boström | 87eece9 | 2020-04-17 18:36:19 +0200 | [diff] [blame] | 411 | // Responsible for adapting input resolution or frame rate to ensure resources |
| 412 | // (e.g. CPU or bandwidth) are not overused. |
Henrik Boström | 381d109 | 2020-05-12 18:49:07 +0200 | [diff] [blame] | 413 | // This class is single-threaded on the resource adaptation queue. |
Henrik Boström | 3745d3f | 2020-04-30 10:13:29 +0200 | [diff] [blame] | 414 | std::unique_ptr<ResourceAdaptationProcessorInterface> |
Henrik Boström | 381d109 | 2020-05-12 18:49:07 +0200 | [diff] [blame] | 415 | resource_adaptation_processor_ |
| 416 | RTC_GUARDED_BY(&resource_adaptation_queue_); |
Henrik Boström | 0f0aa9c | 2020-06-02 13:02:36 +0200 | [diff] [blame] | 417 | 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öm | 87eece9 | 2020-04-17 18:36:19 +0200 | [diff] [blame] | 421 | // 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öm | c55516d | 2020-05-11 16:29:22 +0200 | [diff] [blame] | 425 | // 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öm | 381d109 | 2020-05-12 18:49:07 +0200 | [diff] [blame] | 428 | VideoStreamEncoderResourceManager stream_resource_manager_; |
Henrik Boström | 87eece9 | 2020-04-17 18:36:19 +0200 | [diff] [blame] | 429 | // 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öm | c55516d | 2020-05-11 16:29:22 +0200 | [diff] [blame] | 432 | // This class is thread-safe. |
Henrik Boström | 87eece9 | 2020-04-17 18:36:19 +0200 | [diff] [blame] | 433 | VideoSourceSinkController video_source_sink_controller_; |
Henrik Boström | b08882b | 2020-01-07 10:11:17 +0100 | [diff] [blame] | 434 | |
Henrik Boström | 381d109 | 2020-05-12 18:49:07 +0200 | [diff] [blame] | 435 | // 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_; |
philipel | e5d0fe0 | 2019-10-15 11:02:53 +0200 | [diff] [blame] | 441 | rtc::TaskQueue encoder_queue_; |
| 442 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 443 | RTC_DISALLOW_COPY_AND_ASSIGN(VideoStreamEncoder); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 444 | }; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 445 | |
| 446 | } // namespace webrtc |
| 447 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 448 | #endif // VIDEO_VIDEO_STREAM_ENCODER_H_ |