1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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 
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 "api/adaptation/resource.h"
21 #include "api/units/data_rate.h"
22 #include "api/video/video_bitrate_allocator.h"
23 #include "api/video/video_rotation.h"
24 #include "api/video/video_sink_interface.h"
25 #include "api/video/video_stream_encoder_interface.h"
26 #include "api/video/video_stream_encoder_observer.h"
27 #include "api/video/video_stream_encoder_settings.h"
28 #include "api/video_codecs/video_codec.h"
29 #include "api/video_codecs/video_encoder.h"
30 #include "call/adaptation/adaptation_constraint.h"
31 #include "call/adaptation/resource_adaptation_processor.h"
32 #include "call/adaptation/resource_adaptation_processor_interface.h"
33 #include "call/adaptation/video_source_restrictions.h"
34 #include "call/adaptation/video_stream_input_state_provider.h"
35 #include "modules/video_coding/utility/frame_dropper.h"
36 #include "rtc_base/experiments/rate_control_settings.h"
37 #include "rtc_base/numerics/exp_filter.h"
38 #include "rtc_base/race_checker.h"
39 #include "rtc_base/rate_statistics.h"
40 #include "rtc_base/task_queue.h"
41 #include "rtc_base/task_utils/pending_task_safety_flag.h"
42 #include "rtc_base/thread_annotations.h"
43 #include "rtc_base/thread_checker.h"
44 #include "system_wrappers/include/clock.h"
45 #include "video/adaptation/video_stream_encoder_resource_manager.h"
46 #include "video/encoder_bitrate_adjuster.h"
47 #include "video/frame_encode_metadata_writer.h"
48 #include "video/video_source_sink_controller.h"
49 
50 namespace webrtc {
51 
52 // VideoStreamEncoder represent a video encoder that accepts raw video frames as
53 // input and produces an encoded bit stream.
54 // Usage:
55 //  Instantiate.
56 //  Call SetSink.
57 //  Call SetSource.
58 //  Call ConfigureEncoder with the codec settings.
59 //  Call Stop() when done.
60 class VideoStreamEncoder : public VideoStreamEncoderInterface,
61                            private EncodedImageCallback,
62                            public VideoSourceRestrictionsListener {
63  public:
64   VideoStreamEncoder(Clock* clock,
65                      uint32_t number_of_cores,
66                      VideoStreamEncoderObserver* encoder_stats_observer,
67                      const VideoStreamEncoderSettings& settings,
68                      std::unique_ptr<OveruseFrameDetector> overuse_detector,
69                      TaskQueueFactory* task_queue_factory);
70   ~VideoStreamEncoder() override;
71 
72   void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) override;
73   std::vector<rtc::scoped_refptr<Resource>> GetAdaptationResources() override;
74 
75   void SetSource(rtc::VideoSourceInterface<VideoFrame>* source,
76                  const DegradationPreference& degradation_preference) override;
77 
78   void SetSink(EncoderSink* sink, bool rotation_applied) override;
79 
80   // TODO(perkj): Can we remove VideoCodec.startBitrate ?
81   void SetStartBitrate(int start_bitrate_bps) override;
82 
83   void SetBitrateAllocationObserver(
84       VideoBitrateAllocationObserver* bitrate_observer) override;
85 
86   void SetFecControllerOverride(
87       FecControllerOverride* fec_controller_override) override;
88 
89   void ConfigureEncoder(VideoEncoderConfig config,
90                         size_t max_data_payload_length) override;
91 
92   // Permanently stop encoding. After this method has returned, it is
93   // guaranteed that no encoded frames will be delivered to the sink.
94   void Stop() override;
95 
96   void SendKeyFrame() override;
97 
98   void OnLossNotification(
99       const VideoEncoder::LossNotification& loss_notification) override;
100 
101   void OnBitrateUpdated(DataRate target_bitrate,
102                         DataRate stable_target_bitrate,
103                         DataRate target_headroom,
104                         uint8_t fraction_lost,
105                         int64_t round_trip_time_ms,
106                         double cwnd_reduce_ratio) override;
107 
108   DataRate UpdateTargetBitrate(DataRate target_bitrate,
109                                double cwnd_reduce_ratio);
110 
111  protected:
112   // Used for testing. For example the |ScalingObserverInterface| methods must
113   // be called on |encoder_queue_|.
encoder_queue()114   rtc::TaskQueue* encoder_queue() { return &encoder_queue_; }
115 
116   void OnVideoSourceRestrictionsUpdated(
117       VideoSourceRestrictions restrictions,
118       const VideoAdaptationCounters& adaptation_counters,
119       rtc::scoped_refptr<Resource> reason,
120       const VideoSourceRestrictions& unfiltered_restrictions) override;
121 
122   // Used for injected test resources.
123   // TODO(eshr): Move all adaptation tests out of VideoStreamEncoder tests.
124   void InjectAdaptationResource(rtc::scoped_refptr<Resource> resource,
125                                 VideoAdaptationReason reason);
126   void InjectAdaptationConstraint(AdaptationConstraint* adaptation_constraint);
127 
128   void AddRestrictionsListenerForTesting(
129       VideoSourceRestrictionsListener* restrictions_listener);
130   void RemoveRestrictionsListenerForTesting(
131       VideoSourceRestrictionsListener* restrictions_listener);
132 
133  private:
134   class VideoFrameInfo {
135    public:
VideoFrameInfo(int width,int height,bool is_texture)136     VideoFrameInfo(int width, int height, bool is_texture)
137         : width(width), height(height), is_texture(is_texture) {}
138     int width;
139     int height;
140     bool is_texture;
pixel_count()141     int pixel_count() const { return width * height; }
142   };
143 
144   struct EncoderRateSettings {
145     EncoderRateSettings();
146     EncoderRateSettings(const VideoBitrateAllocation& bitrate,
147                         double framerate_fps,
148                         DataRate bandwidth_allocation,
149                         DataRate encoder_target,
150                         DataRate stable_encoder_target);
151     bool operator==(const EncoderRateSettings& rhs) const;
152     bool operator!=(const EncoderRateSettings& rhs) const;
153 
154     VideoEncoder::RateControlParameters rate_control;
155     // This is the scalar target bitrate before the VideoBitrateAllocator, i.e.
156     // the |target_bitrate| argument of the OnBitrateUpdated() method. This is
157     // needed because the bitrate allocator may truncate the total bitrate and a
158     // later call to the same allocator instance, e.g.
159     // |using last_encoder_rate_setings_->bitrate.get_sum_bps()|, may trick it
160     // into thinking the available bitrate has decreased since the last call.
161     DataRate encoder_target;
162     DataRate stable_encoder_target;
163   };
164 
165   class DegradationPreferenceManager;
166 
167   void ReconfigureEncoder() RTC_RUN_ON(&encoder_queue_);
168   void OnEncoderSettingsChanged() RTC_RUN_ON(&encoder_queue_);
169 
170   // Implements VideoSinkInterface.
171   void OnFrame(const VideoFrame& video_frame) override;
172   void OnDiscardedFrame() override;
173 
174   void MaybeEncodeVideoFrame(const VideoFrame& frame,
175                              int64_t time_when_posted_in_ms);
176 
177   void EncodeVideoFrame(const VideoFrame& frame,
178                         int64_t time_when_posted_in_ms);
179   // Indicates wether frame should be dropped because the pixel count is too
180   // large for the current bitrate configuration.
181   bool DropDueToSize(uint32_t pixel_count) const RTC_RUN_ON(&encoder_queue_);
182 
183   // Implements EncodedImageCallback.
184   EncodedImageCallback::Result OnEncodedImage(
185       const EncodedImage& encoded_image,
186       const CodecSpecificInfo* codec_specific_info) override;
187 
188   void OnDroppedFrame(EncodedImageCallback::DropReason reason) override;
189 
190   bool EncoderPaused() const;
191   void TraceFrameDropStart();
192   void TraceFrameDropEnd();
193 
194   // Returns a copy of |rate_settings| with the |bitrate| field updated using
195   // the current VideoBitrateAllocator, and notifies any listeners of the new
196   // allocation.
197   EncoderRateSettings UpdateBitrateAllocationAndNotifyObserver(
198       const EncoderRateSettings& rate_settings) RTC_RUN_ON(&encoder_queue_);
199 
200   uint32_t GetInputFramerateFps() RTC_RUN_ON(&encoder_queue_);
201   void SetEncoderRates(const EncoderRateSettings& rate_settings)
202       RTC_RUN_ON(&encoder_queue_);
203 
204   void RunPostEncode(const EncodedImage& encoded_image,
205                      int64_t time_sent_us,
206                      int temporal_index,
207                      DataSize frame_size);
208   bool HasInternalSource() const RTC_RUN_ON(&encoder_queue_);
209   void ReleaseEncoder() RTC_RUN_ON(&encoder_queue_);
210   // After calling this function |resource_adaptation_processor_| will be null.
211   void ShutdownResourceAdaptationQueue();
212 
213   void CheckForAnimatedContent(const VideoFrame& frame,
214                                int64_t time_when_posted_in_ms)
215       RTC_RUN_ON(&encoder_queue_);
216 
217   // TODO(bugs.webrtc.org/11341) : Remove this version of RequestEncoderSwitch.
218   void QueueRequestEncoderSwitch(
219       const EncoderSwitchRequestCallback::Config& conf)
220       RTC_RUN_ON(&encoder_queue_);
221   void QueueRequestEncoderSwitch(const webrtc::SdpVideoFormat& format)
222       RTC_RUN_ON(&encoder_queue_);
223 
224   TaskQueueBase* const main_queue_;
225 
226   const uint32_t number_of_cores_;
227 
228   const bool quality_scaling_experiment_enabled_;
229 
230   EncoderSink* sink_;
231   const VideoStreamEncoderSettings settings_;
232   const RateControlSettings rate_control_settings_;
233 
234   std::unique_ptr<VideoEncoderFactory::EncoderSelectorInterface> const
235       encoder_selector_;
236   VideoStreamEncoderObserver* const encoder_stats_observer_;
237 
238   VideoEncoderConfig encoder_config_ RTC_GUARDED_BY(&encoder_queue_);
239   std::unique_ptr<VideoEncoder> encoder_ RTC_GUARDED_BY(&encoder_queue_)
240       RTC_PT_GUARDED_BY(&encoder_queue_);
241   bool encoder_initialized_;
242   std::unique_ptr<VideoBitrateAllocator> rate_allocator_
243       RTC_GUARDED_BY(&encoder_queue_) RTC_PT_GUARDED_BY(&encoder_queue_);
244   int max_framerate_ RTC_GUARDED_BY(&encoder_queue_);
245 
246   // Set when ConfigureEncoder has been called in order to lazy reconfigure the
247   // encoder on the next frame.
248   bool pending_encoder_reconfiguration_ RTC_GUARDED_BY(&encoder_queue_);
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_);
252 
253   absl::optional<VideoFrameInfo> last_frame_info_
254       RTC_GUARDED_BY(&encoder_queue_);
255   int crop_width_ RTC_GUARDED_BY(&encoder_queue_);
256   int crop_height_ RTC_GUARDED_BY(&encoder_queue_);
257   absl::optional<uint32_t> encoder_target_bitrate_bps_
258       RTC_GUARDED_BY(&encoder_queue_);
259   size_t max_data_payload_length_ RTC_GUARDED_BY(&encoder_queue_);
260   absl::optional<EncoderRateSettings> last_encoder_rate_settings_
261       RTC_GUARDED_BY(&encoder_queue_);
262   bool encoder_paused_and_dropped_frame_ RTC_GUARDED_BY(&encoder_queue_);
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 
269   bool encoder_failed_ RTC_GUARDED_BY(&encoder_queue_);
270   Clock* const clock_;
271 
272   rtc::RaceChecker incoming_frame_race_checker_
273       RTC_GUARDED_BY(incoming_frame_race_checker_);
274   std::atomic<int> posted_frames_waiting_for_encode_;
275   // Used to make sure incoming time stamp is increasing for every frame.
276   int64_t last_captured_timestamp_ RTC_GUARDED_BY(incoming_frame_race_checker_);
277   // Delta used for translating between NTP and internal timestamps.
278   const int64_t delta_ntp_internal_ms_
279       RTC_GUARDED_BY(incoming_frame_race_checker_);
280 
281   int64_t last_frame_log_ms_ RTC_GUARDED_BY(incoming_frame_race_checker_);
282   int captured_frame_count_ RTC_GUARDED_BY(&encoder_queue_);
283   int dropped_frame_cwnd_pushback_count_ RTC_GUARDED_BY(&encoder_queue_);
284   int dropped_frame_encoder_block_count_ RTC_GUARDED_BY(&encoder_queue_);
285   absl::optional<VideoFrame> pending_frame_ RTC_GUARDED_BY(&encoder_queue_);
286   int64_t pending_frame_post_time_us_ RTC_GUARDED_BY(&encoder_queue_);
287 
288   VideoFrame::UpdateRect accumulated_update_rect_
289       RTC_GUARDED_BY(&encoder_queue_);
290   bool accumulated_update_rect_is_valid_ RTC_GUARDED_BY(&encoder_queue_);
291 
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 
305   VideoBitrateAllocationObserver* bitrate_observer_
306       RTC_GUARDED_BY(&encoder_queue_);
307   FecControllerOverride* fec_controller_override_
308       RTC_GUARDED_BY(&encoder_queue_);
309   absl::optional<int64_t> last_parameters_update_ms_
310       RTC_GUARDED_BY(&encoder_queue_);
311   absl::optional<int64_t> last_encode_info_ms_ RTC_GUARDED_BY(&encoder_queue_);
312 
313   VideoEncoder::EncoderInfo encoder_info_ RTC_GUARDED_BY(&encoder_queue_);
314   absl::optional<VideoEncoder::ResolutionBitrateLimits> encoder_bitrate_limits_
315       RTC_GUARDED_BY(&encoder_queue_);
316   VideoEncoderFactory::CodecInfo codec_info_ RTC_GUARDED_BY(&encoder_queue_);
317   VideoCodec send_codec_ RTC_GUARDED_BY(&encoder_queue_);
318 
319   FrameDropper frame_dropper_ RTC_GUARDED_BY(&encoder_queue_);
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_;
331 
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 
338   std::unique_ptr<EncoderBitrateAdjuster> bitrate_adjuster_
339       RTC_GUARDED_BY(&encoder_queue_);
340 
341   // TODO(sprang): Change actually support keyframe per simulcast stream, or
342   // turn this into a simple bool |pending_keyframe_request_|.
343   std::vector<VideoFrameType> next_frame_types_ RTC_GUARDED_BY(&encoder_queue_);
344 
345   FrameEncodeMetadataWriter frame_encode_metadata_writer_;
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 
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 
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;
ParserAutomaticAnimationDetectionExperiment389     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 
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 
408   // Provies video stream input states: current resolution and frame rate.
409   VideoStreamInputStateProvider input_state_provider_;
410 
411   std::unique_ptr<VideoStreamAdapter> video_stream_adapter_
412       RTC_GUARDED_BY(&encoder_queue_);
413   // Responsible for adapting input resolution or frame rate to ensure resources
414   // (e.g. CPU or bandwidth) are not overused. Adding resources can occur on any
415   // thread.
416   std::unique_ptr<ResourceAdaptationProcessorInterface>
417       resource_adaptation_processor_;
418   std::unique_ptr<DegradationPreferenceManager> degradation_preference_manager_
419       RTC_GUARDED_BY(&encoder_queue_);
420   std::vector<AdaptationConstraint*> adaptation_constraints_
421       RTC_GUARDED_BY(&encoder_queue_);
422   // Handles input, output and stats reporting related to VideoStreamEncoder
423   // specific resources, such as "encode usage percent" measurements and "QP
424   // scaling". Also involved with various mitigations such as inital frame
425   // dropping.
426   // The manager primarily operates on the |encoder_queue_| but its lifetime is
427   // tied to the VideoStreamEncoder (which is destroyed off the encoder queue)
428   // and its resource list is accessible from any thread.
429   VideoStreamEncoderResourceManager stream_resource_manager_
430       RTC_GUARDED_BY(&encoder_queue_);
431   std::vector<rtc::scoped_refptr<Resource>> additional_resources_
432       RTC_GUARDED_BY(&encoder_queue_);
433   // Carries out the VideoSourceRestrictions provided by the
434   // ResourceAdaptationProcessor, i.e. reconfigures the source of video frames
435   // to provide us with different resolution or frame rate.
436   // This class is thread-safe.
437   VideoSourceSinkController video_source_sink_controller_
438       RTC_GUARDED_BY(main_queue_);
439 
440   // Public methods are proxied to the task queues. The queues must be destroyed
441   // first to make sure no tasks run that use other members.
442   rtc::TaskQueue encoder_queue_;
443 
444   // Used to cancel any potentially pending tasks to the main thread.
445   ScopedTaskSafety task_safety_;
446 
447   RTC_DISALLOW_COPY_AND_ASSIGN(VideoStreamEncoder);
448 };
449 
450 }  // namespace webrtc
451 
452 #endif  // VIDEO_VIDEO_STREAM_ENCODER_H_
453