| 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 |
| 11 | #ifndef VIDEO_VIDEO_STREAM_ENCODER_H_ |
| 12 | #define VIDEO_VIDEO_STREAM_ENCODER_H_ |
| 13 |
| 14 | #include <atomic> |
| 15 | #include <map> |
| 16 | #include <memory> |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 |
| 20 | #include "absl/container/inlined_vector.h" |
| 21 | #include "api/adaptation/resource.h" |
| 22 | #include "api/field_trials_view.h" |
| 23 | #include "api/rtp_sender_interface.h" |
| 24 | #include "api/sequence_checker.h" |
| 25 | #include "api/task_queue/pending_task_safety_flag.h" |
| 26 | #include "api/units/data_rate.h" |
| 27 | #include "api/video/encoded_image.h" |
| 28 | #include "api/video/video_bitrate_allocator.h" |
| 29 | #include "api/video/video_rotation.h" |
| 30 | #include "api/video/video_sink_interface.h" |
| 31 | #include "api/video/video_stream_encoder_settings.h" |
| 32 | #include "api/video_codecs/video_codec.h" |
| 33 | #include "api/video_codecs/video_encoder.h" |
| 34 | #include "call/adaptation/adaptation_constraint.h" |
| 35 | #include "call/adaptation/resource_adaptation_processor.h" |
| 36 | #include "call/adaptation/resource_adaptation_processor_interface.h" |
| 37 | #include "call/adaptation/video_source_restrictions.h" |
| 38 | #include "call/adaptation/video_stream_input_state_provider.h" |
| 39 | #include "modules/video_coding/utility/frame_dropper.h" |
| 40 | #include "modules/video_coding/utility/qp_parser.h" |
| 41 | #include "rtc_base/experiments/rate_control_settings.h" |
| 42 | #include "rtc_base/numerics/exp_filter.h" |
| 43 | #include "rtc_base/race_checker.h" |
| 44 | #include "rtc_base/rate_statistics.h" |
| 45 | #include "rtc_base/thread_annotations.h" |
| 46 | #include "system_wrappers/include/clock.h" |
| 47 | #include "video/adaptation/video_stream_encoder_resource_manager.h" |
| 48 | #include "video/encoder_bitrate_adjuster.h" |
| 49 | #include "video/frame_cadence_adapter.h" |
| 50 | #include "video/frame_encode_metadata_writer.h" |
| 51 | #include "video/video_source_sink_controller.h" |
| 52 | #include "video/video_stream_encoder_interface.h" |
| 53 | #include "video/video_stream_encoder_observer.h" |
| 54 |
| 55 | namespace webrtc { |
| 56 |
| 57 | // VideoStreamEncoder represent a video encoder that accepts raw video frames as |
| 58 | // input and produces an encoded bit stream. |
| 59 | // Usage: |
| 60 | // Instantiate. |
| 61 | // Call SetSink. |
| 62 | // Call SetSource. |
| 63 | // Call ConfigureEncoder with the codec settings. |
| 64 | // Call Stop() when done. |
| 65 | class VideoStreamEncoder : public VideoStreamEncoderInterface, |
| 66 | private EncodedImageCallback, |
| 67 | public VideoSourceRestrictionsListener { |
| 68 | public: |
| 69 | // TODO(bugs.webrtc.org/12000): Reporting of VideoBitrateAllocation is being |
| 70 | // deprecated. Instead VideoLayersAllocation should be reported. |
| 71 | enum class BitrateAllocationCallbackType { |
| 72 | kVideoBitrateAllocation, |
| 73 | kVideoBitrateAllocationWhenScreenSharing, |
| 74 | kVideoLayersAllocation |
| 75 | }; |
| 76 | VideoStreamEncoder( |
| 77 | Clock* clock, |
| 78 | uint32_t number_of_cores, |
| 79 | VideoStreamEncoderObserver* encoder_stats_observer, |
| 80 | const VideoStreamEncoderSettings& settings, |
| 81 | std::unique_ptr<OveruseFrameDetector> overuse_detector, |
| 82 | std::unique_ptr<FrameCadenceAdapterInterface> frame_cadence_adapter, |
| 83 | std::unique_ptr<webrtc::TaskQueueBase, webrtc::TaskQueueDeleter> |
| 84 | encoder_queue, |
| 85 | BitrateAllocationCallbackType allocation_cb_type, |
| 86 | const FieldTrialsView& field_trials, |
| 87 | webrtc::VideoEncoderFactory::EncoderSelectorInterface* encoder_selector = |
| 88 | nullptr); |
| 89 | ~VideoStreamEncoder() override; |
| 90 |
| 91 | VideoStreamEncoder(const VideoStreamEncoder&) = delete; |
| 92 | VideoStreamEncoder& operator=(const VideoStreamEncoder&) = delete; |
| 93 |
| 94 | void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) override; |
| 95 | std::vector<rtc::scoped_refptr<Resource>> GetAdaptationResources() override; |
| 96 |
| 97 | void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, |
| 98 | const DegradationPreference& degradation_preference) override; |
| 99 |
| 100 | void SetSink(EncoderSink* sink, bool rotation_applied) override; |
| 101 |
| 102 | // TODO(perkj): Can we remove VideoCodec.startBitrate ? |
| 103 | void SetStartBitrate(int start_bitrate_bps) override; |
| 104 |
| 105 | void SetFecControllerOverride( |
| 106 | FecControllerOverride* fec_controller_override) override; |
| 107 |
| 108 | void ConfigureEncoder(VideoEncoderConfig config, |
| 109 | size_t max_data_payload_length) override; |
| 110 | void ConfigureEncoder(VideoEncoderConfig config, |
| 111 | size_t max_data_payload_length, |
| 112 | SetParametersCallback callback) override; |
| 113 |
| 114 | // Permanently stop encoding. After this method has returned, it is |
| 115 | // guaranteed that no encoded frames will be delivered to the sink. |
| 116 | void Stop() override; |
| 117 |
| 118 | void SendKeyFrame(const std::vector<VideoFrameType>& layers = {}) override; |
| 119 |
| 120 | void OnLossNotification( |
| 121 | const VideoEncoder::LossNotification& loss_notification) override; |
| 122 |
| 123 | void OnBitrateUpdated(DataRate target_bitrate, |
| 124 | DataRate stable_target_bitrate, |
| 125 | DataRate target_headroom, |
| 126 | uint8_t fraction_lost, |
| 127 | int64_t round_trip_time_ms, |
| 128 | double cwnd_reduce_ratio) override; |
| 129 |
| 130 | DataRate UpdateTargetBitrate(DataRate target_bitrate, |
| 131 | double cwnd_reduce_ratio); |
| 132 |
| 133 | protected: |
| 134 | friend class VideoStreamEncoderFrameCadenceRestrictionTest; |
| 135 |
| 136 | // Used for testing. For example the `ScalingObserverInterface` methods must |
| 137 | // be called on `encoder_queue_`. |
| 138 | TaskQueueBase* encoder_queue() { return encoder_queue_.get(); } |
| 139 |
| 140 | void OnVideoSourceRestrictionsUpdated( |
| 141 | VideoSourceRestrictions restrictions, |
| 142 | const VideoAdaptationCounters& adaptation_counters, |
| 143 | rtc::scoped_refptr<Resource> reason, |
| 144 | const VideoSourceRestrictions& unfiltered_restrictions) override; |
| 145 |
| 146 | // Used for injected test resources. |
| 147 | // TODO(eshr): Move all adaptation tests out of VideoStreamEncoder tests. |
| 148 | void InjectAdaptationResource(rtc::scoped_refptr<Resource> resource, |
| 149 | VideoAdaptationReason reason); |
| 150 | void InjectAdaptationConstraint(AdaptationConstraint* adaptation_constraint); |
| 151 |
| 152 | void AddRestrictionsListenerForTesting( |
| 153 | VideoSourceRestrictionsListener* restrictions_listener); |
| 154 | void RemoveRestrictionsListenerForTesting( |
| 155 | VideoSourceRestrictionsListener* restrictions_listener); |
| 156 |
| 157 | private: |
| 158 | class CadenceCallback : public FrameCadenceAdapterInterface::Callback { |
| 159 | public: |
| 160 | explicit CadenceCallback(VideoStreamEncoder& video_stream_encoder) |
| 161 | : video_stream_encoder_(video_stream_encoder) {} |
| 162 | // FrameCadenceAdapterInterface::Callback overrides. |
| 163 | void OnFrame(Timestamp post_time, |
| 164 | bool queue_overload, |
| 165 | const VideoFrame& frame) override { |
| 166 | video_stream_encoder_.OnFrame(post_time, queue_overload, frame); |
| 167 | } |
| 168 | void OnDiscardedFrame() override { |
| 169 | video_stream_encoder_.OnDiscardedFrame(); |
| 170 | } |
| 171 | void RequestRefreshFrame() override { |
| 172 | video_stream_encoder_.RequestRefreshFrame(); |
| 173 | } |
| 174 |
| 175 | private: |
| 176 | VideoStreamEncoder& video_stream_encoder_; |
| 177 | }; |
| 178 |
| 179 | class VideoFrameInfo { |
| 180 | public: |
| 181 | VideoFrameInfo(int width, int height, bool is_texture) |
| 182 | : width(width), height(height), is_texture(is_texture) {} |
| 183 | int width; |
| 184 | int height; |
| 185 | bool is_texture; |
| 186 | int pixel_count() const { return width * height; } |
| 187 | }; |
| 188 |
| 189 | struct EncoderRateSettings { |
| 190 | EncoderRateSettings(); |
| 191 | EncoderRateSettings(const VideoBitrateAllocation& bitrate, |
| 192 | double framerate_fps, |
| 193 | DataRate bandwidth_allocation, |
| 194 | DataRate encoder_target, |
| 195 | DataRate stable_encoder_target); |
| 196 | bool operator==(const EncoderRateSettings& rhs) const; |
| 197 | bool operator!=(const EncoderRateSettings& rhs) const; |
| 198 |
| 199 | VideoEncoder::RateControlParameters rate_control; |
| 200 | // This is the scalar target bitrate before the VideoBitrateAllocator, i.e. |
| 201 | // the `target_bitrate` argument of the OnBitrateUpdated() method. This is |
| 202 | // needed because the bitrate allocator may truncate the total bitrate and a |
| 203 | // later call to the same allocator instance, e.g. |
| 204 | // |using last_encoder_rate_setings_->bitrate.get_sum_bps()|, may trick it |
| 205 | // into thinking the available bitrate has decreased since the last call. |
| 206 | DataRate encoder_target; |
| 207 | DataRate stable_encoder_target; |
| 208 | }; |
| 209 |
| 210 | class DegradationPreferenceManager; |
| 211 |
| 212 | void ReconfigureEncoder() RTC_RUN_ON(encoder_queue_); |
| 213 | void OnEncoderSettingsChanged() RTC_RUN_ON(encoder_queue_); |
| 214 | void OnFrame(Timestamp post_time, |
| 215 | bool queue_overload, |
| 216 | const VideoFrame& video_frame); |
| 217 | void OnDiscardedFrame(); |
| 218 | void RequestRefreshFrame(); |
| 219 |
| 220 | void MaybeEncodeVideoFrame(const VideoFrame& frame, |
| 221 | int64_t time_when_posted_in_ms); |
| 222 |
| 223 | void EncodeVideoFrame(const VideoFrame& frame, |
| 224 | int64_t time_when_posted_in_ms); |
| 225 | // Indicates whether frame should be dropped because the pixel count is too |
| 226 | // large for the current bitrate configuration. |
| 227 | bool DropDueToSize(uint32_t pixel_count) const RTC_RUN_ON(encoder_queue_); |
| 228 |
| 229 | // Implements EncodedImageCallback. |
| 230 | EncodedImageCallback::Result OnEncodedImage( |
| 231 | const EncodedImage& encoded_image, |
| 232 | const CodecSpecificInfo* codec_specific_info) override; |
| 233 |
| 234 | void OnDroppedFrame(EncodedImageCallback::DropReason reason) override; |
| 235 |
| 236 | bool EncoderPaused() const; |
| 237 | void TraceFrameDropStart(); |
| 238 | void TraceFrameDropEnd(); |
| 239 |
| 240 | // Returns a copy of `rate_settings` with the `bitrate` field updated using |
| 241 | // the current VideoBitrateAllocator. |
| 242 | EncoderRateSettings UpdateBitrateAllocation( |
| 243 | const EncoderRateSettings& rate_settings) RTC_RUN_ON(encoder_queue_); |
| 244 |
| 245 | uint32_t GetInputFramerateFps() RTC_RUN_ON(encoder_queue_); |
| 246 | void SetEncoderRates(const EncoderRateSettings& rate_settings) |
| 247 | RTC_RUN_ON(encoder_queue_); |
| 248 |
| 249 | void RunPostEncode(const EncodedImage& encoded_image, |
| 250 | int64_t time_sent_us, |
| 251 | int temporal_index, |
| 252 | DataSize frame_size); |
| 253 | void ReleaseEncoder() RTC_RUN_ON(encoder_queue_); |
| 254 | // After calling this function `resource_adaptation_processor_` will be null. |
| 255 | void ShutdownResourceAdaptationQueue(); |
| 256 |
| 257 | void CheckForAnimatedContent(const VideoFrame& frame, |
| 258 | int64_t time_when_posted_in_ms) |
| 259 | RTC_RUN_ON(encoder_queue_); |
| 260 |
| 261 | void RequestEncoderSwitch() RTC_RUN_ON(encoder_queue_); |
| 262 |
| 263 | // Augments an EncodedImage received from an encoder with parsable |
| 264 | // information. |
| 265 | EncodedImage AugmentEncodedImage( |
| 266 | const EncodedImage& encoded_image, |
| 267 | const CodecSpecificInfo* codec_specific_info); |
| 268 |
| 269 | void ProcessDroppedFrame(const VideoFrame& frame, |
| 270 | VideoStreamEncoderObserver::DropReason reason) |
| 271 | RTC_RUN_ON(encoder_queue_); |
| 272 | int b; |
| 273 | const FieldTrialsView& field_trials_; |
| 274 | TaskQueueBase* const worker_queue_; |
| 275 |
| 276 | const int number_of_cores_; |
| 277 |
| 278 | EncoderSink* sink_ = nullptr; |
| 279 | const VideoStreamEncoderSettings settings_; |
| 280 | const BitrateAllocationCallbackType allocation_cb_type_; |
| 281 | const RateControlSettings rate_control_settings_; |
| 282 |
| 283 | webrtc::VideoEncoderFactory::EncoderSelectorInterface* const |
| 284 | encoder_selector_from_constructor_; |
| 285 | std::unique_ptr<VideoEncoderFactory::EncoderSelectorInterface> const |
| 286 | encoder_selector_from_factory_; |
| 287 | // Pointing to either encoder_selector_from_constructor_ or |
| 288 | // encoder_selector_from_factory_ but can be nullptr. |
| 289 | VideoEncoderFactory::EncoderSelectorInterface* const encoder_selector_; |
| 290 |
| 291 | VideoStreamEncoderObserver* const encoder_stats_observer_; |
| 292 | // Adapter that avoids public inheritance of the cadence adapter's callback |
| 293 | // interface. |
| 294 | CadenceCallback cadence_callback_{*this}; |
| 295 | // Frame cadence encoder adapter. Frames enter this adapter first, and it then |
| 296 | // forwards them to our OnFrame method. |
| 297 | std::unique_ptr<FrameCadenceAdapterInterface> frame_cadence_adapter_ |
| 298 | RTC_GUARDED_BY(encoder_queue_) RTC_PT_GUARDED_BY(encoder_queue_); |
| 299 |
| 300 | VideoEncoderConfig encoder_config_ RTC_GUARDED_BY(encoder_queue_); |
| 301 | std::unique_ptr<VideoEncoder> encoder_ RTC_GUARDED_BY(encoder_queue_) |
| 302 | RTC_PT_GUARDED_BY(encoder_queue_); |
| 303 | bool encoder_initialized_ = false; |
| 304 | std::unique_ptr<VideoBitrateAllocator> rate_allocator_ |
| 305 | RTC_GUARDED_BY(encoder_queue_) RTC_PT_GUARDED_BY(encoder_queue_); |
| 306 | int max_framerate_ RTC_GUARDED_BY(encoder_queue_) = -1; |
| 307 |
| 308 | // Set when ConfigureEncoder has been called in order to lazy reconfigure the |
| 309 | // encoder on the next frame. |
| 310 | bool pending_encoder_reconfiguration_ RTC_GUARDED_BY(encoder_queue_) = false; |
| 311 | // Set when configuration must create a new encoder object, e.g., |
| 312 | // because of a codec change. |
| 313 | bool pending_encoder_creation_ RTC_GUARDED_BY(encoder_queue_) = false; |
| 314 | absl::InlinedVector<SetParametersCallback, 2> encoder_configuration_callbacks_ |
| 315 | RTC_GUARDED_BY(encoder_queue_); |
| 316 |
| 317 | absl::optional<VideoFrameInfo> last_frame_info_ |
| 318 | RTC_GUARDED_BY(encoder_queue_); |
| 319 | int crop_width_ RTC_GUARDED_BY(encoder_queue_) = 0; |
| 320 | int crop_height_ RTC_GUARDED_BY(encoder_queue_) = 0; |
| 321 | absl::optional<uint32_t> encoder_target_bitrate_bps_ |
| 322 | RTC_GUARDED_BY(encoder_queue_); |
| 323 | size_t max_data_payload_length_ RTC_GUARDED_BY(encoder_queue_) = 0; |
| 324 | absl::optional<EncoderRateSettings> last_encoder_rate_settings_ |
| 325 | RTC_GUARDED_BY(encoder_queue_); |
| 326 | bool encoder_paused_and_dropped_frame_ RTC_GUARDED_BY(encoder_queue_) = false; |
| 327 |
| 328 | // Set to true if at least one frame was sent to encoder since last encoder |
| 329 | // initialization. |
| 330 | bool was_encode_called_since_last_initialization_ |
| 331 | RTC_GUARDED_BY(encoder_queue_) = false; |
| 332 |
| 333 | bool encoder_failed_ RTC_GUARDED_BY(encoder_queue_) = false; |
| 334 | Clock* const clock_; |
| 335 |
| 336 | // Used to make sure incoming time stamp is increasing for every frame. |
| 337 | int64_t last_captured_timestamp_ RTC_GUARDED_BY(encoder_queue_) = 0; |
| 338 | // Delta used for translating between NTP and internal timestamps. |
| 339 | const int64_t delta_ntp_internal_ms_ RTC_GUARDED_BY(encoder_queue_); |
| 340 |
| 341 | int64_t last_frame_log_ms_ RTC_GUARDED_BY(encoder_queue_); |
| 342 | int captured_frame_count_ RTC_GUARDED_BY(encoder_queue_) = 0; |
| 343 | int dropped_frame_cwnd_pushback_count_ RTC_GUARDED_BY(encoder_queue_) = 0; |
| 344 | int dropped_frame_encoder_block_count_ RTC_GUARDED_BY(encoder_queue_) = 0; |
| 345 | absl::optional<VideoFrame> pending_frame_ RTC_GUARDED_BY(encoder_queue_); |
| 346 | int64_t pending_frame_post_time_us_ RTC_GUARDED_BY(encoder_queue_) = 0; |
| 347 |
| 348 | VideoFrame::UpdateRect accumulated_update_rect_ |
| 349 | RTC_GUARDED_BY(encoder_queue_); |
| 350 | bool accumulated_update_rect_is_valid_ RTC_GUARDED_BY(encoder_queue_) = true; |
| 351 |
| 352 | // Used for automatic content type detection. |
| 353 | absl::optional<VideoFrame::UpdateRect> last_update_rect_ |
| 354 | RTC_GUARDED_BY(encoder_queue_); |
| 355 | Timestamp animation_start_time_ RTC_GUARDED_BY(encoder_queue_) = |
| 356 | Timestamp::PlusInfinity(); |
| 357 | bool cap_resolution_due_to_video_content_ RTC_GUARDED_BY(encoder_queue_) = |
| 358 | false; |
| 359 | // Used to correctly ignore changes in update_rect introduced by |
| 360 | // resize triggered by animation detection. |
| 361 | enum class ExpectResizeState { |
| 362 | kNoResize, // Normal operation. |
| 363 | kResize, // Resize was triggered by the animation detection. |
| 364 | kFirstFrameAfterResize // Resize observed. |
| 365 | } expect_resize_state_ RTC_GUARDED_BY(encoder_queue_) = |
| 366 | ExpectResizeState::kNoResize; |
| 367 |
| 368 | FecControllerOverride* fec_controller_override_ |
| 369 | RTC_GUARDED_BY(encoder_queue_) = nullptr; |
| 370 | absl::optional<int64_t> last_parameters_update_ms_ |
| 371 | RTC_GUARDED_BY(encoder_queue_); |
| 372 | absl::optional<int64_t> last_encode_info_ms_ RTC_GUARDED_BY(encoder_queue_); |
| 373 |
| 374 | VideoEncoder::EncoderInfo encoder_info_ RTC_GUARDED_BY(encoder_queue_); |
| 375 | VideoCodec send_codec_ RTC_GUARDED_BY(encoder_queue_); |
| 376 |
| 377 | FrameDropper frame_dropper_ RTC_GUARDED_BY(encoder_queue_); |
| 378 | // If frame dropper is not force disabled, frame dropping might still be |
| 379 | // disabled if VideoEncoder::GetEncoderInfo() indicates that the encoder has a |
| 380 | // trusted rate controller. This is determined on a per-frame basis, as the |
| 381 | // encoder behavior might dynamically change. |
| 382 | bool force_disable_frame_dropper_ RTC_GUARDED_BY(encoder_queue_) = false; |
| 383 | // Incremented on worker thread whenever `frame_dropper_` determines that a |
| 384 | // frame should be dropped. Decremented on whichever thread runs |
| 385 | // OnEncodedImage(), which is only called by one thread but not necessarily |
| 386 | // the worker thread. |
| 387 | std::atomic<int> pending_frame_drops_{0}; |
| 388 |
| 389 | // Congestion window frame drop ratio (drop 1 in every |
| 390 | // cwnd_frame_drop_interval_ frames). |
| 391 | absl::optional<int> cwnd_frame_drop_interval_ RTC_GUARDED_BY(encoder_queue_); |
| 392 | // Frame counter for congestion window frame drop. |
| 393 | int cwnd_frame_counter_ RTC_GUARDED_BY(encoder_queue_) = 0; |
| 394 |
| 395 | std::unique_ptr<EncoderBitrateAdjuster> bitrate_adjuster_ |
| 396 | RTC_GUARDED_BY(encoder_queue_); |
| 397 |
| 398 | // TODO(sprang): Change actually support keyframe per simulcast stream, or |
| 399 | // turn this into a simple bool `pending_keyframe_request_`. |
| 400 | std::vector<VideoFrameType> next_frame_types_ RTC_GUARDED_BY(encoder_queue_); |
| 401 |
| 402 | FrameEncodeMetadataWriter frame_encode_metadata_writer_{this}; |
| 403 |
| 404 | struct AutomaticAnimationDetectionExperiment { |
| 405 | bool enabled = false; |
| 406 | int min_duration_ms = 2000; |
| 407 | double min_area_ratio = 0.8; |
| 408 | int min_fps = 10; |
| 409 | std::unique_ptr<StructParametersParser> Parser() { |
| 410 | return StructParametersParser::Create( |
| 411 | "enabled", &enabled, // |
| 412 | "min_duration_ms", &min_duration_ms, // |
| 413 | "min_area_ratio", &min_area_ratio, // |
| 414 | "min_fps", &min_fps); |
| 415 | } |
| 416 | }; |
| 417 |
| 418 | AutomaticAnimationDetectionExperiment |
| 419 | ParseAutomatincAnimationDetectionFieldTrial() const; |
| 420 |
| 421 | AutomaticAnimationDetectionExperiment |
| 422 | automatic_animation_detection_experiment_ RTC_GUARDED_BY(encoder_queue_); |
| 423 |
| 424 | // Provides video stream input states: current resolution and frame rate. |
| 425 | VideoStreamInputStateProvider input_state_provider_; |
| 426 |
| 427 | const std::unique_ptr<VideoStreamAdapter> video_stream_adapter_ |
| 428 | RTC_GUARDED_BY(encoder_queue_); |
| 429 | // Responsible for adapting input resolution or frame rate to ensure resources |
| 430 | // (e.g. CPU or bandwidth) are not overused. Adding resources can occur on any |
| 431 | // thread. |
| 432 | std::unique_ptr<ResourceAdaptationProcessorInterface> |
| 433 | resource_adaptation_processor_ RTC_GUARDED_BY(encoder_queue_); |
| 434 | std::unique_ptr<DegradationPreferenceManager> degradation_preference_manager_ |
| 435 | RTC_GUARDED_BY(encoder_queue_); |
| 436 | std::vector<AdaptationConstraint*> adaptation_constraints_ |
| 437 | RTC_GUARDED_BY(encoder_queue_); |
| 438 | // Handles input, output and stats reporting related to VideoStreamEncoder |
| 439 | // specific resources, such as "encode usage percent" measurements and "QP |
| 440 | // scaling". Also involved with various mitigations such as initial frame |
| 441 | // dropping. |
| 442 | // The manager primarily operates on the `encoder_queue_` but its lifetime is |
| 443 | // tied to the VideoStreamEncoder (which is destroyed off the encoder queue) |
| 444 | // and its resource list is accessible from any thread. |
| 445 | VideoStreamEncoderResourceManager stream_resource_manager_ |
| 446 | RTC_GUARDED_BY(encoder_queue_); |
| 447 | std::vector<rtc::scoped_refptr<Resource>> additional_resources_ |
| 448 | RTC_GUARDED_BY(encoder_queue_); |
| 449 | // Carries out the VideoSourceRestrictions provided by the |
| 450 | // ResourceAdaptationProcessor, i.e. reconfigures the source of video frames |
| 451 | // to provide us with different resolution or frame rate. |
| 452 | // This class is thread-safe. |
| 453 | VideoSourceSinkController video_source_sink_controller_ |
| 454 | RTC_GUARDED_BY(worker_queue_); |
| 455 |
| 456 | // Default bitrate limits in EncoderInfoSettings allowed. |
| 457 | const bool default_limits_allowed_; |
| 458 |
| 459 | // QP parser is used to extract QP value from encoded frame when that is not |
| 460 | // provided by encoder. |
| 461 | QpParser qp_parser_; |
| 462 | const bool qp_parsing_allowed_; |
| 463 |
| 464 | // Enables encoder switching on initialization failures. |
| 465 | bool switch_encoder_on_init_failures_; |
| 466 |
| 467 | const absl::optional<int> vp9_low_tier_core_threshold_; |
| 468 | const absl::optional<int> experimental_encoder_thread_limit_; |
| 469 |
| 470 | // These are copies of restrictions (glorified max_pixel_count) set by |
| 471 | // a) OnVideoSourceRestrictionsUpdated |
| 472 | // b) CheckForAnimatedContent |
| 473 | // They are used to scale down encoding resolution if needed when using |
| 474 | // requested_resolution. |
| 475 | // |
| 476 | // TODO(webrtc:14451) Split video_source_sink_controller_ |
| 477 | // so that ownership on restrictions/wants is kept on &encoder_queue_, that |
| 478 | // these extra copies would not be needed. |
| 479 | absl::optional<VideoSourceRestrictions> latest_restrictions_ |
| 480 | RTC_GUARDED_BY(encoder_queue_); |
| 481 | absl::optional<VideoSourceRestrictions> animate_restrictions_ |
| 482 | RTC_GUARDED_BY(encoder_queue_); |
| 483 |
| 484 | // Used to cancel any potentially pending tasks to the worker thread. |
| 485 | // Refrenced by tasks running on `encoder_queue_` so need to be destroyed |
| 486 | // after stopping that queue. Must be created and destroyed on |
| 487 | // `worker_queue_`. |
| 488 | ScopedTaskSafety task_safety_; |
| 489 |
| 490 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> encoder_queue_; |
| 491 | }; |
| 492 |
| 493 | } // namespace webrtc |
| 494 |
| 495 | #endif // VIDEO_VIDEO_STREAM_ENCODER_H_ |
| 496 |