blob: 4008426bac9cf0e6dc28e92864484713001256d5 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:251/*
stefan@webrtc.org07b45a52012-02-02 08:37:482 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:253 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 04:47:3111#ifndef VIDEO_VIDEO_STREAM_ENCODER_H_
12#define VIDEO_VIDEO_STREAM_ENCODER_H_
niklase@google.com470e71d2011-07-07 08:21:2513
Yuwei Huangd9f99c12017-10-24 22:40:5214#include <atomic>
sprangc5d62e22017-04-03 06:53:0415#include <map>
kwiberg27f982b2016-03-01 19:52:3316#include <memory>
perkj376b1922016-05-02 18:35:2417#include <string>
mflodman@webrtc.org02270cd2015-02-06 13:10:1918#include <vector>
mflodman@webrtc.orgd6ec3862012-10-25 11:30:2919
Jiawei Ou4206a0a2018-07-20 22:49:4320#include "api/video/video_bitrate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3121#include "api/video/video_rotation.h"
Niels Möllerc6ce9c52018-05-11 09:15:3022#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 12:09:3123#include "api/video/video_stream_encoder_interface.h"
Niels Möller213618e2018-07-24 07:29:5824#include "api/video/video_stream_encoder_observer.h"
25#include "api/video/video_stream_encoder_settings.h"
Niels Möller0327c2d2018-05-21 12:09:3126#include "api/video_codecs/video_encoder.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3127#include "modules/video_coding/utility/quality_scaler.h"
28#include "modules/video_coding/video_coding_impl.h"
29#include "rtc_base/criticalsection.h"
30#include "rtc_base/event.h"
31#include "rtc_base/sequenced_task_checker.h"
32#include "rtc_base/task_queue.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3133#include "video/overuse_frame_detector.h"
niklase@google.com470e71d2011-07-07 08:21:2534
35namespace webrtc {
mflodman@webrtc.org84d17832011-12-01 17:02:2336
Sebastian Jansson652dc912018-04-19 15:09:1537// VideoStreamEncoder represent a video encoder that accepts raw video frames as
38// input and produces an encoded bit stream.
39// Usage:
40// Instantiate.
41// Call SetSink.
42// Call SetSource.
43// Call ConfigureEncoder with the codec settings.
44// Call Stop() when done.
45class VideoStreamEncoder : public VideoStreamEncoderInterface,
46 private EncodedImageCallback,
47 // Protected only to provide access to tests.
48 protected AdaptationObserverInterface {
49 public:
mflodmancc3d4422017-08-03 15:27:5150 VideoStreamEncoder(uint32_t number_of_cores,
Niels Möller213618e2018-07-24 07:29:5851 VideoStreamEncoderObserver* encoder_stats_observer,
52 const VideoStreamEncoderSettings& settings,
mflodmancc3d4422017-08-03 15:27:5153 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback,
mflodmancc3d4422017-08-03 15:27:5154 std::unique_ptr<OveruseFrameDetector> overuse_detector);
Stefan Holmerdbdb3a02018-07-17 14:03:4655 ~VideoStreamEncoder() override;
niklase@google.com470e71d2011-07-07 08:21:2556
Sebastian Jansson652dc912018-04-19 15:09:1557 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source,
Taylor Brandstetter49fcc102018-05-16 21:20:4158 const DegradationPreference& degradation_preference) override;
perkj803d97f2016-11-01 18:45:4659
Sebastian Jansson652dc912018-04-19 15:09:1560 void SetSink(EncoderSink* sink, bool rotation_applied) override;
mflodman@webrtc.org02270cd2015-02-06 13:10:1961
perkj26091b12016-09-01 08:17:4062 // TODO(perkj): Can we remove VideoCodec.startBitrate ?
Sebastian Jansson652dc912018-04-19 15:09:1563 void SetStartBitrate(int start_bitrate_bps) override;
mflodman@webrtc.org9ec883e2012-03-05 17:12:4164
Niels Möller0327c2d2018-05-21 12:09:3165 void SetBitrateAllocationObserver(
Sebastian Jansson652dc912018-04-19 15:09:1566 VideoBitrateAllocationObserver* bitrate_observer) override;
sprang1a646ee2016-12-01 14:34:1167
Per512ecb32016-09-23 13:52:0668 void ConfigureEncoder(VideoEncoderConfig config,
Niels Möllerf1338562018-04-26 07:51:4769 size_t max_data_payload_length) override;
niklase@google.com470e71d2011-07-07 08:21:2570
perkj26091b12016-09-01 08:17:4071 // Permanently stop encoding. After this method has returned, it is
72 // guaranteed that no encoded frames will be delivered to the sink.
Sebastian Jansson652dc912018-04-19 15:09:1573 void Stop() override;
perkj26091b12016-09-01 08:17:4074
Sebastian Jansson652dc912018-04-19 15:09:1575 void SendKeyFrame() override;
mflodman@webrtc.orgd6ec3862012-10-25 11:30:2976
mflodman86aabb22016-03-11 14:44:3277 void OnBitrateUpdated(uint32_t bitrate_bps,
stefan@webrtc.orgedeea912014-12-08 19:46:2378 uint8_t fraction_lost,
Sebastian Jansson652dc912018-04-19 15:09:1579 int64_t round_trip_time_ms) override;
pwestin@webrtc.org49888ce2012-04-27 05:25:5380
perkj803d97f2016-11-01 18:45:4681 protected:
kthelgason876222f2016-11-29 09:44:1182 // Used for testing. For example the |ScalingObserverInterface| methods must
83 // be called on |encoder_queue_|.
perkj803d97f2016-11-01 18:45:4684 rtc::TaskQueue* encoder_queue() { return &encoder_queue_; }
85
Niels Möllerd692ef92017-10-04 13:28:5586 // AdaptationObserverInterface implementation.
perkj803d97f2016-11-01 18:45:4687 // These methods are protected for easier testing.
sprangb1ca0732017-02-01 16:38:1288 void AdaptUp(AdaptReason reason) override;
89 void AdaptDown(AdaptReason reason) override;
perkj803d97f2016-11-01 18:45:4690
mflodman@webrtc.org84d17832011-12-01 17:02:2391 private:
perkja49cbd32016-09-16 14:53:4192 class VideoSourceProxy;
perkj26091b12016-09-01 08:17:4093
kthelgason93f16d72017-01-16 14:15:2394 class VideoFrameInfo {
95 public:
Yves Gerey665174f2018-06-19 13:03:0596 VideoFrameInfo(int width, int height, bool is_texture)
97 : width(width), height(height), is_texture(is_texture) {}
perkjfa10b552016-10-03 06:45:2698 int width;
99 int height;
perkjfa10b552016-10-03 06:45:26100 bool is_texture;
kthelgason93f16d72017-01-16 14:15:23101 int pixel_count() const { return width * height; }
perkjfa10b552016-10-03 06:45:26102 };
103
Pera48ddb72016-09-29 09:48:50104 void ConfigureEncoderOnTaskQueue(VideoEncoderConfig config,
Niels Möllerf1338562018-04-26 07:51:47105 size_t max_data_payload_length);
Niels Möllera8b15082018-02-07 12:42:09106 void ReconfigureEncoder() RTC_RUN_ON(&encoder_queue_);
perkj26091b12016-09-01 08:17:40107
kthelgason2bc68642017-02-07 15:02:22108 void ConfigureQualityScaler();
109
perkja49cbd32016-09-16 14:53:41110 // Implements VideoSinkInterface.
111 void OnFrame(const VideoFrame& video_frame) override;
Ilya Nikolaevskiyd79314f2017-10-23 08:45:37112 void OnDiscardedFrame() override;
perkja49cbd32016-09-16 14:53:41113
Sebastian Janssona3177052018-04-10 11:05:49114 void MaybeEncodeVideoFrame(const VideoFrame& frame,
115 int64_t time_when_posted_in_ms);
116
perkjd52063f2016-09-07 13:32:18117 void EncodeVideoFrame(const VideoFrame& frame,
118 int64_t time_when_posted_in_ms);
Sebastian Janssona3177052018-04-10 11:05:49119 // Indicates wether frame should be dropped because the pixel count is too
120 // large for the current bitrate configuration.
121 bool DropDueToSize(uint32_t pixel_count) const RTC_RUN_ON(&encoder_queue_);
perkj26091b12016-09-01 08:17:40122
123 // Implements EncodedImageCallback.
124 EncodedImageCallback::Result OnEncodedImage(
125 const EncodedImage& encoded_image,
126 const CodecSpecificInfo* codec_specific_info,
127 const RTPFragmentationHeader* fragmentation) override;
128
Ilya Nikolaevskiyd79314f2017-10-23 08:45:37129 void OnDroppedFrame(EncodedImageCallback::DropReason reason) override;
kthelgason876222f2016-11-29 09:44:11130
perkj26091b12016-09-01 08:17:40131 bool EncoderPaused() const;
132 void TraceFrameDropStart();
133 void TraceFrameDropEnd();
134
asapersson09f05612017-05-16 06:40:18135 // Class holding adaptation information.
136 class AdaptCounter final {
137 public:
138 AdaptCounter();
139 ~AdaptCounter();
140
141 // Get number of adaptation downscales for |reason|.
Niels Möller213618e2018-07-24 07:29:58142 VideoStreamEncoderObserver::AdaptationSteps Counts(int reason) const;
asapersson09f05612017-05-16 06:40:18143
144 std::string ToString() const;
145
asaperssonf7e294d2017-06-14 06:25:22146 void IncrementFramerate(int reason);
147 void IncrementResolution(int reason);
148 void DecrementFramerate(int reason);
149 void DecrementResolution(int reason);
150 void DecrementFramerate(int reason, int cur_fps);
asapersson09f05612017-05-16 06:40:18151
152 // Gets the total number of downgrades (for all adapt reasons).
153 int FramerateCount() const;
154 int ResolutionCount() const;
asapersson09f05612017-05-16 06:40:18155
156 // Gets the total number of downgrades for |reason|.
157 int FramerateCount(int reason) const;
158 int ResolutionCount(int reason) const;
159 int TotalCount(int reason) const;
160
161 private:
162 std::string ToString(const std::vector<int>& counters) const;
163 int Count(const std::vector<int>& counters) const;
asaperssonf7e294d2017-06-14 06:25:22164 void MoveCount(std::vector<int>* counters, int from_reason);
asapersson09f05612017-05-16 06:40:18165
166 // Degradation counters holding number of framerate/resolution reductions
167 // per adapt reason.
168 std::vector<int> fps_counters_;
169 std::vector<int> resolution_counters_;
170 };
171
danilchapa37de392017-09-09 11:17:22172 AdaptCounter& GetAdaptCounter() RTC_RUN_ON(&encoder_queue_);
173 const AdaptCounter& GetConstAdaptCounter() RTC_RUN_ON(&encoder_queue_);
174 void UpdateAdaptationStats(AdaptReason reason) RTC_RUN_ON(&encoder_queue_);
Niels Möller213618e2018-07-24 07:29:58175 VideoStreamEncoderObserver::AdaptationSteps GetActiveCounts(
176 AdaptReason reason) RTC_RUN_ON(&encoder_queue_);
sprangc5d62e22017-04-03 06:53:04177
perkj26091b12016-09-01 08:17:40178 rtc::Event shutdown_event_;
stefan@webrtc.orgbfacda62013-03-27 16:36:01179
pbos@webrtc.orgb238d122013-04-09 13:41:51180 const uint32_t number_of_cores_;
Kári Tristan Helgason639602a2018-08-02 08:51:40181 // Counts how many frames we've dropped in the initial framedrop phase.
182 int initial_framedrop_;
183 const bool initial_framedrop_on_bwe_enabled_;
184 bool has_seen_first_significant_bwe_change_ = false;
perkja49cbd32016-09-16 14:53:41185
Åsa Perssona945aee2018-04-24 14:53:25186 const bool quality_scaling_experiment_enabled_;
187
perkja49cbd32016-09-16 14:53:41188 const std::unique_ptr<VideoSourceProxy> source_proxy_;
Per512ecb32016-09-23 13:52:06189 EncoderSink* sink_;
Niels Möller213618e2018-07-24 07:29:58190 const VideoStreamEncoderSettings settings_;
niklase@google.com470e71d2011-07-07 08:21:25191
Niels Möller1e062892018-02-07 09:18:32192 vcm::VideoSender video_sender_ RTC_GUARDED_BY(&encoder_queue_);
Niels Möllera8b15082018-02-07 12:42:09193 const std::unique_ptr<OveruseFrameDetector> overuse_detector_
194 RTC_PT_GUARDED_BY(&encoder_queue_);
Yves Gerey665174f2018-06-19 13:03:05195 std::unique_ptr<QualityScaler> quality_scaler_ RTC_GUARDED_BY(&encoder_queue_)
Niels Möllera8b15082018-02-07 12:42:09196 RTC_PT_GUARDED_BY(&encoder_queue_);
pwestin@webrtc.org49888ce2012-04-27 05:25:53197
Niels Möller213618e2018-07-24 07:29:58198 VideoStreamEncoderObserver* const encoder_stats_observer_;
perkj26091b12016-09-01 08:17:40199 rtc::VideoSinkInterface<VideoFrame>* const pre_encode_callback_;
perkja49cbd32016-09-16 14:53:41200 // |thread_checker_| checks that public methods that are related to lifetime
mflodmancc3d4422017-08-03 15:27:51201 // of VideoStreamEncoder are called on the same thread.
perkja49cbd32016-09-16 14:53:41202 rtc::ThreadChecker thread_checker_;
mflodman@webrtc.org84d17832011-12-01 17:02:23203
Niels Möller1e062892018-02-07 09:18:32204 VideoEncoderConfig encoder_config_ RTC_GUARDED_BY(&encoder_queue_);
Niels Möller4db138e2018-04-19 07:04:13205 std::unique_ptr<VideoEncoder> encoder_ RTC_GUARDED_BY(&encoder_queue_)
206 RTC_PT_GUARDED_BY(&encoder_queue_);
Erik Språng08127a92016-11-16 15:41:30207 std::unique_ptr<VideoBitrateAllocator> rate_allocator_
Yves Gerey665174f2018-06-19 13:03:05208 RTC_GUARDED_BY(&encoder_queue_) RTC_PT_GUARDED_BY(&encoder_queue_);
sprangfda496a2017-06-15 11:21:07209 // The maximum frame rate of the current codec configuration, as determined
210 // at the last ReconfigureEncoder() call.
Niels Möller1e062892018-02-07 09:18:32211 int max_framerate_ RTC_GUARDED_BY(&encoder_queue_);
mflodman@webrtc.org84d17832011-12-01 17:02:23212
perkjfa10b552016-10-03 06:45:26213 // Set when ConfigureEncoder has been called in order to lazy reconfigure the
214 // encoder on the next frame.
Niels Möller1e062892018-02-07 09:18:32215 bool pending_encoder_reconfiguration_ RTC_GUARDED_BY(&encoder_queue_);
Niels Möller4db138e2018-04-19 07:04:13216 // Set when configuration must create a new encoder object, e.g.,
217 // because of a codec change.
218 bool pending_encoder_creation_ RTC_GUARDED_BY(&encoder_queue_);
Danil Chapovalovb9b146c2018-06-15 10:28:07219 absl::optional<VideoFrameInfo> last_frame_info_
Niels Möller1e062892018-02-07 09:18:32220 RTC_GUARDED_BY(&encoder_queue_);
221 int crop_width_ RTC_GUARDED_BY(&encoder_queue_);
222 int crop_height_ RTC_GUARDED_BY(&encoder_queue_);
223 uint32_t encoder_start_bitrate_bps_ RTC_GUARDED_BY(&encoder_queue_);
224 size_t max_data_payload_length_ RTC_GUARDED_BY(&encoder_queue_);
Niels Möller1e062892018-02-07 09:18:32225 uint32_t last_observed_bitrate_bps_ RTC_GUARDED_BY(&encoder_queue_);
226 bool encoder_paused_and_dropped_frame_ RTC_GUARDED_BY(&encoder_queue_);
perkj26091b12016-09-01 08:17:40227 Clock* const clock_;
asapersson09f05612017-05-16 06:40:18228 // Counters used for deciding if the video resolution or framerate is
229 // currently restricted, and if so, why, on a per degradation preference
230 // basis.
sprangc5d62e22017-04-03 06:53:04231 // TODO(sprang): Replace this with a state holding a relative overuse measure
232 // instead, that can be translated into suitable down-scale or fps limit.
Taylor Brandstetter49fcc102018-05-16 21:20:41233 std::map<const DegradationPreference, AdaptCounter> adapt_counters_
Niels Möller1e062892018-02-07 09:18:32234 RTC_GUARDED_BY(&encoder_queue_);
Taylor Brandstetter49fcc102018-05-16 21:20:41235 // Set depending on degradation preferences.
236 DegradationPreference degradation_preference_ RTC_GUARDED_BY(&encoder_queue_);
perkj803d97f2016-11-01 18:45:46237
sprang84a37592017-02-10 15:04:27238 struct AdaptationRequest {
239 // The pixel count produced by the source at the time of the adaptation.
240 int input_pixel_count_;
sprangc5d62e22017-04-03 06:53:04241 // Framerate received from the source at the time of the adaptation.
242 int framerate_fps_;
sprang84a37592017-02-10 15:04:27243 // Indicates if request was to adapt up or down.
244 enum class Mode { kAdaptUp, kAdaptDown } mode_;
245 };
246 // Stores a snapshot of the last adaptation request triggered by an AdaptUp
247 // or AdaptDown signal.
Danil Chapovalovb9b146c2018-06-15 10:28:07248 absl::optional<AdaptationRequest> last_adaptation_request_
Niels Möller1e062892018-02-07 09:18:32249 RTC_GUARDED_BY(&encoder_queue_);
perkj803d97f2016-11-01 18:45:46250
251 rtc::RaceChecker incoming_frame_race_checker_
danilchapa37de392017-09-09 11:17:22252 RTC_GUARDED_BY(incoming_frame_race_checker_);
Yuwei Huangd9f99c12017-10-24 22:40:52253 std::atomic<int> posted_frames_waiting_for_encode_;
perkj26091b12016-09-01 08:17:40254 // Used to make sure incoming time stamp is increasing for every frame.
danilchapa37de392017-09-09 11:17:22255 int64_t last_captured_timestamp_ RTC_GUARDED_BY(incoming_frame_race_checker_);
perkj26091b12016-09-01 08:17:40256 // Delta used for translating between NTP and internal timestamps.
danilchapa37de392017-09-09 11:17:22257 const int64_t delta_ntp_internal_ms_
258 RTC_GUARDED_BY(incoming_frame_race_checker_);
perkj26091b12016-09-01 08:17:40259
danilchapa37de392017-09-09 11:17:22260 int64_t last_frame_log_ms_ RTC_GUARDED_BY(incoming_frame_race_checker_);
Niels Möller1e062892018-02-07 09:18:32261 int captured_frame_count_ RTC_GUARDED_BY(&encoder_queue_);
262 int dropped_frame_count_ RTC_GUARDED_BY(&encoder_queue_);
Danil Chapovalovb9b146c2018-06-15 10:28:07263 absl::optional<VideoFrame> pending_frame_ RTC_GUARDED_BY(&encoder_queue_);
Sebastian Janssona3177052018-04-10 11:05:49264 int64_t pending_frame_post_time_us_ RTC_GUARDED_BY(&encoder_queue_);
asapersson6ffb67d2016-09-12 07:10:45265
danilchapa37de392017-09-09 11:17:22266 VideoBitrateAllocationObserver* bitrate_observer_
Niels Möller1e062892018-02-07 09:18:32267 RTC_GUARDED_BY(&encoder_queue_);
Danil Chapovalovb9b146c2018-06-15 10:28:07268 absl::optional<int64_t> last_parameters_update_ms_
Niels Möller1e062892018-02-07 09:18:32269 RTC_GUARDED_BY(&encoder_queue_);
sprang1a646ee2016-12-01 14:34:11270
perkj26091b12016-09-01 08:17:40271 // All public methods are proxied to |encoder_queue_|. It must must be
272 // destroyed first to make sure no tasks are run that use other members.
273 rtc::TaskQueue encoder_queue_;
perkja49cbd32016-09-16 14:53:41274
mflodmancc3d4422017-08-03 15:27:51275 RTC_DISALLOW_COPY_AND_ASSIGN(VideoStreamEncoder);
niklase@google.com470e71d2011-07-07 08:21:25276};
mflodman@webrtc.org84d17832011-12-01 17:02:23277
278} // namespace webrtc
279
Mirko Bonadei92ea95e2017-09-15 04:47:31280#endif // VIDEO_VIDEO_STREAM_ENCODER_H_