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 | |
philipel | d9cc8c0 | 2019-09-16 14:53:40 +0200 | [diff] [blame] | 20 | #include "api/units/data_rate.h" |
Jiawei Ou | 4206a0a | 2018-07-20 15:49:43 -0700 | [diff] [blame] | 21 | #include "api/video/video_bitrate_allocator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "api/video/video_rotation.h" |
Niels Möller | c6ce9c5 | 2018-05-11 11:15:30 +0200 | [diff] [blame] | 23 | #include "api/video/video_sink_interface.h" |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 24 | #include "api/video/video_stream_encoder_interface.h" |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 25 | #include "api/video/video_stream_encoder_observer.h" |
| 26 | #include "api/video/video_stream_encoder_settings.h" |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 27 | #include "api/video_codecs/video_codec.h" |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 28 | #include "api/video_codecs/video_encoder.h" |
Henrik Boström | 6205762 | 2020-03-10 19:08:05 +0100 | [diff] [blame] | 29 | #include "call/adaptation/resource_adaptation_processor_interface.h" |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 30 | #include "call/adaptation/video_source_restrictions.h" |
Henrik Boström | d516b25 | 2020-04-17 12:10:59 +0200 | [diff] [blame^] | 31 | #include "call/adaptation/video_stream_input_state_provider.h" |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 32 | #include "modules/video_coding/utility/frame_dropper.h" |
philipel | da5aa4d | 2019-04-26 13:37:37 +0200 | [diff] [blame] | 33 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 34 | #include "rtc_base/event.h" |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 35 | #include "rtc_base/experiments/rate_control_settings.h" |
philipel | d9cc8c0 | 2019-09-16 14:53:40 +0200 | [diff] [blame] | 36 | #include "rtc_base/numerics/exp_filter.h" |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 37 | #include "rtc_base/race_checker.h" |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 38 | #include "rtc_base/rate_statistics.h" |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 39 | #include "rtc_base/synchronization/sequence_checker.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 40 | #include "rtc_base/task_queue.h" |
Niels Möller | fe407b7 | 2019-09-10 10:48:48 +0200 | [diff] [blame] | 41 | #include "system_wrappers/include/clock.h" |
Henrik Boström | 6205762 | 2020-03-10 19:08:05 +0100 | [diff] [blame] | 42 | #include "video/adaptation/resource_adaptation_processor.h" |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 43 | #include "video/encoder_bitrate_adjuster.h" |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 44 | #include "video/frame_encode_metadata_writer.h" |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 45 | #include "video/video_source_sink_controller.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | |
| 47 | namespace webrtc { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 48 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 49 | // VideoStreamEncoder represent a video encoder that accepts raw video frames as |
| 50 | // input and produces an encoded bit stream. |
| 51 | // Usage: |
| 52 | // Instantiate. |
| 53 | // Call SetSink. |
| 54 | // Call SetSource. |
| 55 | // Call ConfigureEncoder with the codec settings. |
| 56 | // Call Stop() when done. |
| 57 | class VideoStreamEncoder : public VideoStreamEncoderInterface, |
Henrik Boström | d238200 | 2020-01-10 15:44:01 +0100 | [diff] [blame] | 58 | private EncodedImageCallback, |
Henrik Boström | 6205762 | 2020-03-10 19:08:05 +0100 | [diff] [blame] | 59 | public ResourceAdaptationProcessorListener { |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 60 | public: |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 61 | VideoStreamEncoder(Clock* clock, |
| 62 | uint32_t number_of_cores, |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 63 | VideoStreamEncoderObserver* encoder_stats_observer, |
| 64 | const VideoStreamEncoderSettings& settings, |
Sebastian Jansson | 74682c1 | 2019-03-01 11:50:20 +0100 | [diff] [blame] | 65 | std::unique_ptr<OveruseFrameDetector> overuse_detector, |
| 66 | TaskQueueFactory* task_queue_factory); |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 67 | ~VideoStreamEncoder() override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 69 | void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 70 | const DegradationPreference& degradation_preference) override; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 71 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 72 | void SetSink(EncoderSink* sink, bool rotation_applied) override; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 73 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 74 | // TODO(perkj): Can we remove VideoCodec.startBitrate ? |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 75 | void SetStartBitrate(int start_bitrate_bps) override; |
mflodman@webrtc.org | 9ec883e | 2012-03-05 17:12:41 +0000 | [diff] [blame] | 76 | |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 77 | void SetBitrateAllocationObserver( |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 78 | VideoBitrateAllocationObserver* bitrate_observer) override; |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 79 | |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 80 | void SetFecControllerOverride( |
| 81 | FecControllerOverride* fec_controller_override) override; |
| 82 | |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 83 | void ConfigureEncoder(VideoEncoderConfig config, |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 84 | size_t max_data_payload_length) override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 86 | // Permanently stop encoding. After this method has returned, it is |
| 87 | // guaranteed that no encoded frames will be delivered to the sink. |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 88 | void Stop() override; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 89 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 90 | void SendKeyFrame() override; |
mflodman@webrtc.org | d6ec386 | 2012-10-25 11:30:29 +0000 | [diff] [blame] | 91 | |
Elad Alon | b6ef99b | 2019-04-10 16:37:07 +0200 | [diff] [blame] | 92 | void OnLossNotification( |
| 93 | const VideoEncoder::LossNotification& loss_notification) override; |
| 94 | |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 95 | void OnBitrateUpdated(DataRate target_bitrate, |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 96 | DataRate stable_target_bitrate, |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 97 | DataRate target_headroom, |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 98 | uint8_t fraction_lost, |
Ying Wang | 9b881ab | 2020-02-07 14:29:32 +0100 | [diff] [blame] | 99 | int64_t round_trip_time_ms, |
| 100 | double cwnd_reduce_ratio) override; |
| 101 | |
| 102 | DataRate UpdateTargetBitrate(DataRate target_bitrate, |
| 103 | double cwnd_reduce_ratio); |
pwestin@webrtc.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 104 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 105 | protected: |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 106 | // Used for testing. For example the |ScalingObserverInterface| methods must |
| 107 | // be called on |encoder_queue_|. |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 108 | rtc::TaskQueue* encoder_queue() { return &encoder_queue_; } |
| 109 | |
Henrik Boström | d238200 | 2020-01-10 15:44:01 +0100 | [diff] [blame] | 110 | void OnVideoSourceRestrictionsUpdated( |
| 111 | VideoSourceRestrictions restrictions) override; |
| 112 | |
Evan Shrubsole | aa6fbc1 | 2020-02-25 16:26:01 +0100 | [diff] [blame] | 113 | // Used for injected test resources. |
| 114 | // TODO(eshr): Move all adaptation tests out of VideoStreamEncoder tests. |
| 115 | void InjectAdaptationResource(Resource* resource, |
Evan Shrubsole | ce0a11d | 2020-04-16 11:36:55 +0200 | [diff] [blame] | 116 | VideoAdaptationReason reason) |
Evan Shrubsole | aa6fbc1 | 2020-02-25 16:26:01 +0100 | [diff] [blame] | 117 | RTC_RUN_ON(&encoder_queue_); |
| 118 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 119 | private: |
kthelgason | 93f16d7 | 2017-01-16 06:15:23 -0800 | [diff] [blame] | 120 | class VideoFrameInfo { |
| 121 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 122 | VideoFrameInfo(int width, int height, bool is_texture) |
| 123 | : width(width), height(height), is_texture(is_texture) {} |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 124 | int width; |
| 125 | int height; |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 126 | bool is_texture; |
kthelgason | 93f16d7 | 2017-01-16 06:15:23 -0800 | [diff] [blame] | 127 | int pixel_count() const { return width * height; } |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
Evan Shrubsole | 7c079f6 | 2019-09-26 09:55:03 +0200 | [diff] [blame] | 130 | struct EncoderRateSettings { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 131 | EncoderRateSettings(); |
| 132 | EncoderRateSettings(const VideoBitrateAllocation& bitrate, |
| 133 | double framerate_fps, |
| 134 | DataRate bandwidth_allocation, |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 135 | DataRate encoder_target, |
| 136 | DataRate stable_encoder_target); |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 137 | bool operator==(const EncoderRateSettings& rhs) const; |
| 138 | bool operator!=(const EncoderRateSettings& rhs) const; |
| 139 | |
Evan Shrubsole | 7c079f6 | 2019-09-26 09:55:03 +0200 | [diff] [blame] | 140 | VideoEncoder::RateControlParameters rate_control; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 141 | // This is the scalar target bitrate before the VideoBitrateAllocator, i.e. |
| 142 | // the |target_bitrate| argument of the OnBitrateUpdated() method. This is |
| 143 | // needed because the bitrate allocator may truncate the total bitrate and a |
| 144 | // later call to the same allocator instance, e.g. |
| 145 | // |using last_encoder_rate_setings_->bitrate.get_sum_bps()|, may trick it |
| 146 | // into thinking the available bitrate has decreased since the last call. |
| 147 | DataRate encoder_target; |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 148 | DataRate stable_encoder_target; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 149 | }; |
| 150 | |
Niels Möller | a8b1508 | 2018-02-07 13:42:09 +0100 | [diff] [blame] | 151 | void ReconfigureEncoder() RTC_RUN_ON(&encoder_queue_); |
Henrik Boström | d516b25 | 2020-04-17 12:10:59 +0200 | [diff] [blame^] | 152 | void OnEncoderSettingsChanged() RTC_RUN_ON(&encoder_queue_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 153 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 154 | // Implements VideoSinkInterface. |
| 155 | void OnFrame(const VideoFrame& video_frame) override; |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 156 | void OnDiscardedFrame() override; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 157 | |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 158 | void MaybeEncodeVideoFrame(const VideoFrame& frame, |
| 159 | int64_t time_when_posted_in_ms); |
| 160 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 161 | void EncodeVideoFrame(const VideoFrame& frame, |
| 162 | int64_t time_when_posted_in_ms); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 163 | // Indicates wether frame should be dropped because the pixel count is too |
| 164 | // large for the current bitrate configuration. |
| 165 | bool DropDueToSize(uint32_t pixel_count) const RTC_RUN_ON(&encoder_queue_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 166 | |
| 167 | // Implements EncodedImageCallback. |
| 168 | EncodedImageCallback::Result OnEncodedImage( |
| 169 | const EncodedImage& encoded_image, |
| 170 | const CodecSpecificInfo* codec_specific_info, |
| 171 | const RTPFragmentationHeader* fragmentation) override; |
| 172 | |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 173 | void OnDroppedFrame(EncodedImageCallback::DropReason reason) override; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 174 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 175 | bool EncoderPaused() const; |
| 176 | void TraceFrameDropStart(); |
| 177 | void TraceFrameDropEnd(); |
| 178 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 179 | // Returns a copy of |rate_settings| with the |bitrate| field updated using |
| 180 | // the current VideoBitrateAllocator, and notifies any listeners of the new |
| 181 | // allocation. |
| 182 | EncoderRateSettings UpdateBitrateAllocationAndNotifyObserver( |
| 183 | const EncoderRateSettings& rate_settings) RTC_RUN_ON(&encoder_queue_); |
| 184 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 185 | uint32_t GetInputFramerateFps() RTC_RUN_ON(&encoder_queue_); |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 186 | void SetEncoderRates(const EncoderRateSettings& rate_settings) |
| 187 | RTC_RUN_ON(&encoder_queue_); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 188 | |
Evan Shrubsole | 2bc91e8 | 2020-01-17 11:44:24 +0100 | [diff] [blame] | 189 | void RunPostEncode(const EncodedImage& encoded_image, |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 190 | int64_t time_sent_us, |
philipel | c7a46c4 | 2019-11-25 15:01:09 +0100 | [diff] [blame] | 191 | int temporal_index, |
| 192 | DataSize frame_size); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 193 | bool HasInternalSource() const RTC_RUN_ON(&encoder_queue_); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 194 | void ReleaseEncoder() RTC_RUN_ON(&encoder_queue_); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 195 | |
Ilya Nikolaevskiy | 648b9d7 | 2019-12-03 16:54:17 +0100 | [diff] [blame] | 196 | void CheckForAnimatedContent(const VideoFrame& frame, |
| 197 | int64_t time_when_posted_in_ms) |
| 198 | RTC_RUN_ON(&encoder_queue_); |
| 199 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 200 | rtc::Event shutdown_event_; |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 201 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 202 | const uint32_t number_of_cores_; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 203 | |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 204 | const bool quality_scaling_experiment_enabled_; |
| 205 | |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 206 | EncoderSink* sink_; |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 207 | const VideoStreamEncoderSettings settings_; |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 208 | const RateControlSettings rate_control_settings_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 209 | |
philipel | 9b05803 | 2020-02-10 11:30:00 +0100 | [diff] [blame] | 210 | std::unique_ptr<VideoEncoderFactory::EncoderSelectorInterface> const |
| 211 | encoder_selector_; |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 212 | VideoStreamEncoderObserver* const encoder_stats_observer_; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 213 | // |thread_checker_| checks that public methods that are related to lifetime |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 214 | // of VideoStreamEncoder are called on the same thread. |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 215 | rtc::ThreadChecker thread_checker_; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 216 | |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 217 | VideoEncoderConfig encoder_config_ RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 218 | std::unique_ptr<VideoEncoder> encoder_ RTC_GUARDED_BY(&encoder_queue_) |
| 219 | RTC_PT_GUARDED_BY(&encoder_queue_); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 220 | bool encoder_initialized_; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 221 | std::unique_ptr<VideoBitrateAllocator> rate_allocator_ |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 222 | RTC_GUARDED_BY(&encoder_queue_) RTC_PT_GUARDED_BY(&encoder_queue_); |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 223 | int max_framerate_ RTC_GUARDED_BY(&encoder_queue_); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 224 | |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 225 | // Set when ConfigureEncoder has been called in order to lazy reconfigure the |
| 226 | // encoder on the next frame. |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 227 | bool pending_encoder_reconfiguration_ RTC_GUARDED_BY(&encoder_queue_); |
Mirta Dvornicic | 1ec2a16 | 2018-12-10 09:47:34 +0000 | [diff] [blame] | 228 | // Set when configuration must create a new encoder object, e.g., |
| 229 | // because of a codec change. |
| 230 | bool pending_encoder_creation_ RTC_GUARDED_BY(&encoder_queue_); |
Sergey Silkin | 5ee6967 | 2019-07-02 14:18:34 +0200 | [diff] [blame] | 231 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 232 | absl::optional<VideoFrameInfo> last_frame_info_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 233 | RTC_GUARDED_BY(&encoder_queue_); |
| 234 | int crop_width_ RTC_GUARDED_BY(&encoder_queue_); |
| 235 | int crop_height_ RTC_GUARDED_BY(&encoder_queue_); |
Henrik Boström | ede69c0 | 2020-01-21 17:45:35 +0100 | [diff] [blame] | 236 | absl::optional<uint32_t> encoder_target_bitrate_bps_ |
| 237 | RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 238 | size_t max_data_payload_length_ RTC_GUARDED_BY(&encoder_queue_); |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 239 | absl::optional<EncoderRateSettings> last_encoder_rate_settings_ |
| 240 | RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 241 | bool encoder_paused_and_dropped_frame_ RTC_GUARDED_BY(&encoder_queue_); |
Sergey Silkin | 5ee6967 | 2019-07-02 14:18:34 +0200 | [diff] [blame] | 242 | |
| 243 | // Set to true if at least one frame was sent to encoder since last encoder |
| 244 | // initialization. |
| 245 | bool was_encode_called_since_last_initialization_ |
| 246 | RTC_GUARDED_BY(&encoder_queue_); |
| 247 | |
philipel | e8ed830 | 2019-07-03 11:53:48 +0200 | [diff] [blame] | 248 | bool encoder_failed_ RTC_GUARDED_BY(&encoder_queue_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 249 | Clock* const clock_; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 250 | |
| 251 | rtc::RaceChecker incoming_frame_race_checker_ |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 252 | RTC_GUARDED_BY(incoming_frame_race_checker_); |
Yuwei Huang | d9f99c1 | 2017-10-24 15:40:52 -0700 | [diff] [blame] | 253 | std::atomic<int> posted_frames_waiting_for_encode_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 254 | // Used to make sure incoming time stamp is increasing for every frame. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 255 | int64_t last_captured_timestamp_ RTC_GUARDED_BY(incoming_frame_race_checker_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 256 | // Delta used for translating between NTP and internal timestamps. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 257 | const int64_t delta_ntp_internal_ms_ |
| 258 | RTC_GUARDED_BY(incoming_frame_race_checker_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 259 | |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 260 | 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] | 261 | int captured_frame_count_ RTC_GUARDED_BY(&encoder_queue_); |
Ying Wang | 9b881ab | 2020-02-07 14:29:32 +0100 | [diff] [blame] | 262 | int dropped_frame_cwnd_pushback_count_ RTC_GUARDED_BY(&encoder_queue_); |
| 263 | int dropped_frame_encoder_block_count_ RTC_GUARDED_BY(&encoder_queue_); |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 264 | absl::optional<VideoFrame> pending_frame_ RTC_GUARDED_BY(&encoder_queue_); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 265 | int64_t pending_frame_post_time_us_ RTC_GUARDED_BY(&encoder_queue_); |
asapersson | 6ffb67d | 2016-09-12 00:10:45 -0700 | [diff] [blame] | 266 | |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 13:01:26 +0100 | [diff] [blame] | 267 | VideoFrame::UpdateRect accumulated_update_rect_ |
| 268 | RTC_GUARDED_BY(&encoder_queue_); |
Ilya Nikolaevskiy | 9560d7d | 2019-10-30 11:19:47 +0100 | [diff] [blame] | 269 | bool accumulated_update_rect_is_valid_ RTC_GUARDED_BY(&encoder_queue_); |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 13:01:26 +0100 | [diff] [blame] | 270 | |
Ilya Nikolaevskiy | 648b9d7 | 2019-12-03 16:54:17 +0100 | [diff] [blame] | 271 | // Used for automatic content type detection. |
| 272 | absl::optional<VideoFrame::UpdateRect> last_update_rect_ |
| 273 | RTC_GUARDED_BY(&encoder_queue_); |
| 274 | Timestamp animation_start_time_ RTC_GUARDED_BY(&encoder_queue_); |
| 275 | bool cap_resolution_due_to_video_content_ RTC_GUARDED_BY(&encoder_queue_); |
| 276 | // Used to correctly ignore changes in update_rect introduced by |
| 277 | // resize triggered by animation detection. |
| 278 | enum class ExpectResizeState { |
| 279 | kNoResize, // Normal operation. |
| 280 | kResize, // Resize was triggered by the animation detection. |
| 281 | kFirstFrameAfterResize // Resize observed. |
| 282 | } expect_resize_state_ RTC_GUARDED_BY(&encoder_queue_); |
| 283 | |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 284 | VideoBitrateAllocationObserver* bitrate_observer_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 285 | RTC_GUARDED_BY(&encoder_queue_); |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 286 | FecControllerOverride* fec_controller_override_ |
| 287 | RTC_GUARDED_BY(&encoder_queue_); |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 288 | absl::optional<int64_t> last_parameters_update_ms_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 289 | RTC_GUARDED_BY(&encoder_queue_); |
Åsa Persson | c29cb2c | 2019-03-25 12:06:59 +0100 | [diff] [blame] | 290 | absl::optional<int64_t> last_encode_info_ms_ RTC_GUARDED_BY(&encoder_queue_); |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 291 | |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 292 | VideoEncoder::EncoderInfo encoder_info_ RTC_GUARDED_BY(&encoder_queue_); |
Sergey Silkin | 6456e35 | 2019-07-08 17:56:40 +0200 | [diff] [blame] | 293 | absl::optional<VideoEncoder::ResolutionBitrateLimits> encoder_bitrate_limits_ |
| 294 | RTC_GUARDED_BY(&encoder_queue_); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 295 | VideoEncoderFactory::CodecInfo codec_info_ RTC_GUARDED_BY(&encoder_queue_); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 296 | VideoCodec send_codec_ RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 297 | |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 298 | FrameDropper frame_dropper_ RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 299 | // If frame dropper is not force disabled, frame dropping might still be |
| 300 | // disabled if VideoEncoder::GetEncoderInfo() indicates that the encoder has a |
| 301 | // trusted rate controller. This is determined on a per-frame basis, as the |
| 302 | // encoder behavior might dynamically change. |
| 303 | bool force_disable_frame_dropper_ RTC_GUARDED_BY(&encoder_queue_); |
| 304 | RateStatistics input_framerate_ RTC_GUARDED_BY(&encoder_queue_); |
| 305 | // Incremented on worker thread whenever |frame_dropper_| determines that a |
| 306 | // frame should be dropped. Decremented on whichever thread runs |
| 307 | // OnEncodedImage(), which is only called by one thread but not necessarily |
| 308 | // the worker thread. |
| 309 | std::atomic<int> pending_frame_drops_; |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 310 | |
Ying Wang | 9b881ab | 2020-02-07 14:29:32 +0100 | [diff] [blame] | 311 | // Congestion window frame drop ratio (drop 1 in every |
| 312 | // cwnd_frame_drop_interval_ frames). |
| 313 | absl::optional<int> cwnd_frame_drop_interval_ RTC_GUARDED_BY(&encoder_queue_); |
| 314 | // Frame counter for congestion window frame drop. |
| 315 | int cwnd_frame_counter_ RTC_GUARDED_BY(&encoder_queue_); |
| 316 | |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 317 | std::unique_ptr<EncoderBitrateAdjuster> bitrate_adjuster_ |
| 318 | RTC_GUARDED_BY(&encoder_queue_); |
| 319 | |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 320 | // TODO(sprang): Change actually support keyframe per simulcast stream, or |
| 321 | // turn this into a simple bool |pending_keyframe_request_|. |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 322 | std::vector<VideoFrameType> next_frame_types_ RTC_GUARDED_BY(&encoder_queue_); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 323 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 324 | FrameEncodeMetadataWriter frame_encode_metadata_writer_; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 325 | |
| 326 | // Experiment groups parsed from field trials for realtime video ([0]) and |
| 327 | // screenshare ([1]). 0 means no group specified. Positive values are |
| 328 | // experiment group numbers incremented by 1. |
| 329 | const std::array<uint8_t, 2> experiment_groups_; |
| 330 | |
philipel | da5aa4d | 2019-04-26 13:37:37 +0200 | [diff] [blame] | 331 | // TODO(philipel): Remove this lock and run on |encoder_queue_| instead. |
| 332 | rtc::CriticalSection encoded_image_lock_; |
| 333 | |
| 334 | int64_t next_frame_id_ RTC_GUARDED_BY(encoded_image_lock_); |
| 335 | |
| 336 | // This array is used as a map from simulcast id to an encoder's buffer |
| 337 | // state. For every buffer of the encoder we keep track of the last frame id |
| 338 | // that updated that buffer. |
| 339 | std::array<std::array<int64_t, kMaxEncoderBuffers>, kMaxSimulcastStreams> |
| 340 | encoder_buffer_state_ RTC_GUARDED_BY(encoded_image_lock_); |
| 341 | |
philipel | d9cc8c0 | 2019-09-16 14:53:40 +0200 | [diff] [blame] | 342 | struct EncoderSwitchExperiment { |
| 343 | struct Thresholds { |
| 344 | absl::optional<DataRate> bitrate; |
| 345 | absl::optional<int> pixel_count; |
| 346 | }; |
| 347 | |
| 348 | // Codec --> switching thresholds |
| 349 | std::map<VideoCodecType, Thresholds> codec_thresholds; |
| 350 | |
| 351 | // To smooth out the target bitrate so that we don't trigger a switch |
| 352 | // too easily. |
| 353 | rtc::ExpFilter bitrate_filter{1.0}; |
| 354 | |
| 355 | // Codec/implementation to switch to |
| 356 | std::string to_codec; |
| 357 | absl::optional<std::string> to_param; |
| 358 | absl::optional<std::string> to_value; |
| 359 | |
| 360 | // Thresholds for the currently used codecs. |
| 361 | Thresholds current_thresholds; |
| 362 | |
| 363 | // Updates the |bitrate_filter|, so not const. |
| 364 | bool IsBitrateBelowThreshold(const DataRate& target_bitrate); |
| 365 | bool IsPixelCountBelowThreshold(int pixel_count) const; |
| 366 | void SetCodec(VideoCodecType codec); |
| 367 | }; |
| 368 | |
| 369 | EncoderSwitchExperiment ParseEncoderSwitchFieldTrial() const; |
| 370 | |
| 371 | EncoderSwitchExperiment encoder_switch_experiment_ |
| 372 | RTC_GUARDED_BY(&encoder_queue_); |
| 373 | |
Ilya Nikolaevskiy | 648b9d7 | 2019-12-03 16:54:17 +0100 | [diff] [blame] | 374 | struct AutomaticAnimationDetectionExperiment { |
| 375 | bool enabled = false; |
| 376 | int min_duration_ms = 2000; |
| 377 | double min_area_ratio = 0.8; |
| 378 | int min_fps = 10; |
| 379 | std::unique_ptr<StructParametersParser> Parser() { |
| 380 | return StructParametersParser::Create( |
| 381 | "enabled", &enabled, // |
| 382 | "min_duration_ms", &min_duration_ms, // |
| 383 | "min_area_ratio", &min_area_ratio, // |
| 384 | "min_fps", &min_fps); |
| 385 | } |
| 386 | }; |
| 387 | |
| 388 | AutomaticAnimationDetectionExperiment |
| 389 | ParseAutomatincAnimationDetectionFieldTrial() const; |
| 390 | |
| 391 | AutomaticAnimationDetectionExperiment |
| 392 | automatic_animation_detection_experiment_ RTC_GUARDED_BY(&encoder_queue_); |
| 393 | |
philipel | d9cc8c0 | 2019-09-16 14:53:40 +0200 | [diff] [blame] | 394 | // An encoder switch is only requested once, this variable is used to keep |
| 395 | // track of whether a request has been made or not. |
| 396 | bool encoder_switch_requested_ RTC_GUARDED_BY(&encoder_queue_); |
| 397 | |
Henrik Boström | 07b17df | 2020-01-15 11:42:12 +0100 | [diff] [blame] | 398 | // The controller updates the sink wants based on restrictions that come from |
Henrik Boström | 6205762 | 2020-03-10 19:08:05 +0100 | [diff] [blame] | 399 | // the resource adaptation processor or adaptation due to bandwidth |
| 400 | // adaptation. |
Henrik Boström | 07b17df | 2020-01-15 11:42:12 +0100 | [diff] [blame] | 401 | // |
| 402 | // This is used on the encoder queue, with a few exceptions: |
| 403 | // - VideoStreamEncoder::SetSource() invokes SetSource(). |
| 404 | // - VideoStreamEncoder::SetSink() invokes SetRotationApplied() and |
| 405 | // PushSourceSinkSettings(). |
| 406 | // - VideoStreamEncoder::Stop() invokes SetSource(). |
| 407 | // TODO(hbos): If these can be moved to the encoder queue, |
| 408 | // VideoSourceSinkController can be made single-threaded, and its lock can be |
| 409 | // replaced with a sequence checker. |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 410 | std::unique_ptr<VideoSourceSinkController> video_source_sink_controller_; |
Henrik Boström | d516b25 | 2020-04-17 12:10:59 +0200 | [diff] [blame^] | 411 | std::unique_ptr<VideoStreamInputStateProvider> input_state_provider_ |
| 412 | RTC_GUARDED_BY(&encoder_queue_); |
Henrik Boström | 6205762 | 2020-03-10 19:08:05 +0100 | [diff] [blame] | 413 | std::unique_ptr<ResourceAdaptationProcessor> resource_adaptation_processor_ |
| 414 | RTC_GUARDED_BY(&encoder_queue_); |
Henrik Boström | b08882b | 2020-01-07 10:11:17 +0100 | [diff] [blame] | 415 | |
philipel | e5d0fe0 | 2019-10-15 11:02:53 +0200 | [diff] [blame] | 416 | // All public methods are proxied to |encoder_queue_|. It must must be |
| 417 | // destroyed first to make sure no tasks are run that use other members. |
| 418 | rtc::TaskQueue encoder_queue_; |
| 419 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 420 | RTC_DISALLOW_COPY_AND_ASSIGN(VideoStreamEncoder); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 421 | }; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 422 | |
| 423 | } // namespace webrtc |
| 424 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 425 | #endif // VIDEO_VIDEO_STREAM_ENCODER_H_ |