blob: 4008426bac9cf0e6dc28e92864484713001256d5 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.org07b45a52012-02-02 08:37:48 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 06:47:31 +020011#ifndef VIDEO_VIDEO_STREAM_ENCODER_H_
12#define VIDEO_VIDEO_STREAM_ENCODER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Yuwei Huangd9f99c12017-10-24 15:40:52 -070014#include <atomic>
sprangc5d62e22017-04-02 23:53:04 -070015#include <map>
kwiberg27f982b2016-03-01 11:52:33 -080016#include <memory>
perkj376b1922016-05-02 11:35:24 -070017#include <string>
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000018#include <vector>
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +000019
Jiawei Ou4206a0a2018-07-20 15:49:43 -070020#include "api/video/video_bitrate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/video/video_rotation.h"
Niels Möllerc6ce9c52018-05-11 11:15:30 +020022#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020023#include "api/video/video_stream_encoder_interface.h"
Niels Möller213618e2018-07-24 09:29:58 +020024#include "api/video/video_stream_encoder_observer.h"
25#include "api/video/video_stream_encoder_settings.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020026#include "api/video_codecs/video_encoder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#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 06:47:31 +020033#include "video/overuse_frame_detector.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000034
35namespace webrtc {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000036
Sebastian Jansson652dc912018-04-19 17:09:15 +020037// 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 08:27:51 -070050 VideoStreamEncoder(uint32_t number_of_cores,
Niels Möller213618e2018-07-24 09:29:58 +020051 VideoStreamEncoderObserver* encoder_stats_observer,
52 const VideoStreamEncoderSettings& settings,
mflodmancc3d4422017-08-03 08:27:51 -070053 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback,
mflodmancc3d4422017-08-03 08:27:51 -070054 std::unique_ptr<OveruseFrameDetector> overuse_detector);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020055 ~VideoStreamEncoder() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000056
Sebastian Jansson652dc912018-04-19 17:09:15 +020057 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source,
Taylor Brandstetter49fcc102018-05-16 14:20:41 -070058 const DegradationPreference& degradation_preference) override;
perkj803d97f2016-11-01 11:45:46 -070059
Sebastian Jansson652dc912018-04-19 17:09:15 +020060 void SetSink(EncoderSink* sink, bool rotation_applied) override;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000061
perkj26091b12016-09-01 01:17:40 -070062 // TODO(perkj): Can we remove VideoCodec.startBitrate ?
Sebastian Jansson652dc912018-04-19 17:09:15 +020063 void SetStartBitrate(int start_bitrate_bps) override;
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +000064
Niels Möller0327c2d2018-05-21 14:09:31 +020065 void SetBitrateAllocationObserver(
Sebastian Jansson652dc912018-04-19 17:09:15 +020066 VideoBitrateAllocationObserver* bitrate_observer) override;
sprang1a646ee2016-12-01 06:34:11 -080067
Per512ecb32016-09-23 15:52:06 +020068 void ConfigureEncoder(VideoEncoderConfig config,
Niels Möllerf1338562018-04-26 09:51:47 +020069 size_t max_data_payload_length) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000070
perkj26091b12016-09-01 01:17:40 -070071 // 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 17:09:15 +020073 void Stop() override;
perkj26091b12016-09-01 01:17:40 -070074
Sebastian Jansson652dc912018-04-19 17:09:15 +020075 void SendKeyFrame() override;
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +000076
mflodman86aabb22016-03-11 15:44:32 +010077 void OnBitrateUpdated(uint32_t bitrate_bps,
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000078 uint8_t fraction_lost,
Sebastian Jansson652dc912018-04-19 17:09:15 +020079 int64_t round_trip_time_ms) override;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000080
perkj803d97f2016-11-01 11:45:46 -070081 protected:
kthelgason876222f2016-11-29 01:44:11 -080082 // Used for testing. For example the |ScalingObserverInterface| methods must
83 // be called on |encoder_queue_|.
perkj803d97f2016-11-01 11:45:46 -070084 rtc::TaskQueue* encoder_queue() { return &encoder_queue_; }
85
Niels Möllerd692ef92017-10-04 15:28:55 +020086 // AdaptationObserverInterface implementation.
perkj803d97f2016-11-01 11:45:46 -070087 // These methods are protected for easier testing.
sprangb1ca0732017-02-01 08:38:12 -080088 void AdaptUp(AdaptReason reason) override;
89 void AdaptDown(AdaptReason reason) override;
perkj803d97f2016-11-01 11:45:46 -070090
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000091 private:
perkja49cbd32016-09-16 07:53:41 -070092 class VideoSourceProxy;
perkj26091b12016-09-01 01:17:40 -070093
kthelgason93f16d72017-01-16 06:15:23 -080094 class VideoFrameInfo {
95 public:
Yves Gerey665174f2018-06-19 15:03:05 +020096 VideoFrameInfo(int width, int height, bool is_texture)
97 : width(width), height(height), is_texture(is_texture) {}
perkjfa10b552016-10-02 23:45:26 -070098 int width;
99 int height;
perkjfa10b552016-10-02 23:45:26 -0700100 bool is_texture;
kthelgason93f16d72017-01-16 06:15:23 -0800101 int pixel_count() const { return width * height; }
perkjfa10b552016-10-02 23:45:26 -0700102 };
103
Pera48ddb72016-09-29 11:48:50 +0200104 void ConfigureEncoderOnTaskQueue(VideoEncoderConfig config,
Niels Möllerf1338562018-04-26 09:51:47 +0200105 size_t max_data_payload_length);
Niels Möllera8b15082018-02-07 13:42:09 +0100106 void ReconfigureEncoder() RTC_RUN_ON(&encoder_queue_);
perkj26091b12016-09-01 01:17:40 -0700107
kthelgason2bc68642017-02-07 07:02:22 -0800108 void ConfigureQualityScaler();
109
perkja49cbd32016-09-16 07:53:41 -0700110 // Implements VideoSinkInterface.
111 void OnFrame(const VideoFrame& video_frame) override;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200112 void OnDiscardedFrame() override;
perkja49cbd32016-09-16 07:53:41 -0700113
Sebastian Janssona3177052018-04-10 13:05:49 +0200114 void MaybeEncodeVideoFrame(const VideoFrame& frame,
115 int64_t time_when_posted_in_ms);
116
perkjd52063f2016-09-07 06:32:18 -0700117 void EncodeVideoFrame(const VideoFrame& frame,
118 int64_t time_when_posted_in_ms);
Sebastian Janssona3177052018-04-10 13:05:49 +0200119 // 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 01:17:40 -0700122
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 10:45:37 +0200129 void OnDroppedFrame(EncodedImageCallback::DropReason reason) override;
kthelgason876222f2016-11-29 01:44:11 -0800130
perkj26091b12016-09-01 01:17:40 -0700131 bool EncoderPaused() const;
132 void TraceFrameDropStart();
133 void TraceFrameDropEnd();
134
asapersson09f05612017-05-15 23:40:18 -0700135 // 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 09:29:58 +0200142 VideoStreamEncoderObserver::AdaptationSteps Counts(int reason) const;
asapersson09f05612017-05-15 23:40:18 -0700143
144 std::string ToString() const;
145
asaperssonf7e294d2017-06-13 23:25:22 -0700146 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-15 23:40:18 -0700151
152 // Gets the total number of downgrades (for all adapt reasons).
153 int FramerateCount() const;
154 int ResolutionCount() const;
asapersson09f05612017-05-15 23:40:18 -0700155
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-13 23:25:22 -0700164 void MoveCount(std::vector<int>* counters, int from_reason);
asapersson09f05612017-05-15 23:40:18 -0700165
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 04:17:22 -0700172 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 09:29:58 +0200175 VideoStreamEncoderObserver::AdaptationSteps GetActiveCounts(
176 AdaptReason reason) RTC_RUN_ON(&encoder_queue_);
sprangc5d62e22017-04-02 23:53:04 -0700177
perkj26091b12016-09-01 01:17:40 -0700178 rtc::Event shutdown_event_;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000179
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000180 const uint32_t number_of_cores_;
Kári Tristan Helgason639602a2018-08-02 10:51:40 +0200181 // 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 07:53:41 -0700185
Åsa Perssona945aee2018-04-24 16:53:25 +0200186 const bool quality_scaling_experiment_enabled_;
187
perkja49cbd32016-09-16 07:53:41 -0700188 const std::unique_ptr<VideoSourceProxy> source_proxy_;
Per512ecb32016-09-23 15:52:06 +0200189 EncoderSink* sink_;
Niels Möller213618e2018-07-24 09:29:58 +0200190 const VideoStreamEncoderSettings settings_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000191
Niels Möller1e062892018-02-07 10:18:32 +0100192 vcm::VideoSender video_sender_ RTC_GUARDED_BY(&encoder_queue_);
Niels Möllera8b15082018-02-07 13:42:09 +0100193 const std::unique_ptr<OveruseFrameDetector> overuse_detector_
194 RTC_PT_GUARDED_BY(&encoder_queue_);
Yves Gerey665174f2018-06-19 15:03:05 +0200195 std::unique_ptr<QualityScaler> quality_scaler_ RTC_GUARDED_BY(&encoder_queue_)
Niels Möllera8b15082018-02-07 13:42:09 +0100196 RTC_PT_GUARDED_BY(&encoder_queue_);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000197
Niels Möller213618e2018-07-24 09:29:58 +0200198 VideoStreamEncoderObserver* const encoder_stats_observer_;
perkj26091b12016-09-01 01:17:40 -0700199 rtc::VideoSinkInterface<VideoFrame>* const pre_encode_callback_;
perkja49cbd32016-09-16 07:53:41 -0700200 // |thread_checker_| checks that public methods that are related to lifetime
mflodmancc3d4422017-08-03 08:27:51 -0700201 // of VideoStreamEncoder are called on the same thread.
perkja49cbd32016-09-16 07:53:41 -0700202 rtc::ThreadChecker thread_checker_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000203
Niels Möller1e062892018-02-07 10:18:32 +0100204 VideoEncoderConfig encoder_config_ RTC_GUARDED_BY(&encoder_queue_);
Niels Möller4db138e2018-04-19 09:04:13 +0200205 std::unique_ptr<VideoEncoder> encoder_ RTC_GUARDED_BY(&encoder_queue_)
206 RTC_PT_GUARDED_BY(&encoder_queue_);
Erik Språng08127a92016-11-16 16:41:30 +0100207 std::unique_ptr<VideoBitrateAllocator> rate_allocator_
Yves Gerey665174f2018-06-19 15:03:05 +0200208 RTC_GUARDED_BY(&encoder_queue_) RTC_PT_GUARDED_BY(&encoder_queue_);
sprangfda496a2017-06-15 04:21:07 -0700209 // The maximum frame rate of the current codec configuration, as determined
210 // at the last ReconfigureEncoder() call.
Niels Möller1e062892018-02-07 10:18:32 +0100211 int max_framerate_ RTC_GUARDED_BY(&encoder_queue_);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000212
perkjfa10b552016-10-02 23:45:26 -0700213 // Set when ConfigureEncoder has been called in order to lazy reconfigure the
214 // encoder on the next frame.
Niels Möller1e062892018-02-07 10:18:32 +0100215 bool pending_encoder_reconfiguration_ RTC_GUARDED_BY(&encoder_queue_);
Niels Möller4db138e2018-04-19 09:04:13 +0200216 // 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 12:28:07 +0200219 absl::optional<VideoFrameInfo> last_frame_info_
Niels Möller1e062892018-02-07 10:18:32 +0100220 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 10:18:32 +0100225 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 01:17:40 -0700227 Clock* const clock_;
asapersson09f05612017-05-15 23:40:18 -0700228 // 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-02 23:53:04 -0700231 // 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 14:20:41 -0700233 std::map<const DegradationPreference, AdaptCounter> adapt_counters_
Niels Möller1e062892018-02-07 10:18:32 +0100234 RTC_GUARDED_BY(&encoder_queue_);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700235 // Set depending on degradation preferences.
236 DegradationPreference degradation_preference_ RTC_GUARDED_BY(&encoder_queue_);
perkj803d97f2016-11-01 11:45:46 -0700237
sprang84a37592017-02-10 07:04:27 -0800238 struct AdaptationRequest {
239 // The pixel count produced by the source at the time of the adaptation.
240 int input_pixel_count_;
sprangc5d62e22017-04-02 23:53:04 -0700241 // Framerate received from the source at the time of the adaptation.
242 int framerate_fps_;
sprang84a37592017-02-10 07:04:27 -0800243 // 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 12:28:07 +0200248 absl::optional<AdaptationRequest> last_adaptation_request_
Niels Möller1e062892018-02-07 10:18:32 +0100249 RTC_GUARDED_BY(&encoder_queue_);
perkj803d97f2016-11-01 11:45:46 -0700250
251 rtc::RaceChecker incoming_frame_race_checker_
danilchapa37de392017-09-09 04:17:22 -0700252 RTC_GUARDED_BY(incoming_frame_race_checker_);
Yuwei Huangd9f99c12017-10-24 15:40:52 -0700253 std::atomic<int> posted_frames_waiting_for_encode_;
perkj26091b12016-09-01 01:17:40 -0700254 // Used to make sure incoming time stamp is increasing for every frame.
danilchapa37de392017-09-09 04:17:22 -0700255 int64_t last_captured_timestamp_ RTC_GUARDED_BY(incoming_frame_race_checker_);
perkj26091b12016-09-01 01:17:40 -0700256 // Delta used for translating between NTP and internal timestamps.
danilchapa37de392017-09-09 04:17:22 -0700257 const int64_t delta_ntp_internal_ms_
258 RTC_GUARDED_BY(incoming_frame_race_checker_);
perkj26091b12016-09-01 01:17:40 -0700259
danilchapa37de392017-09-09 04:17:22 -0700260 int64_t last_frame_log_ms_ RTC_GUARDED_BY(incoming_frame_race_checker_);
Niels Möller1e062892018-02-07 10:18:32 +0100261 int captured_frame_count_ RTC_GUARDED_BY(&encoder_queue_);
262 int dropped_frame_count_ RTC_GUARDED_BY(&encoder_queue_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200263 absl::optional<VideoFrame> pending_frame_ RTC_GUARDED_BY(&encoder_queue_);
Sebastian Janssona3177052018-04-10 13:05:49 +0200264 int64_t pending_frame_post_time_us_ RTC_GUARDED_BY(&encoder_queue_);
asapersson6ffb67d2016-09-12 00:10:45 -0700265
danilchapa37de392017-09-09 04:17:22 -0700266 VideoBitrateAllocationObserver* bitrate_observer_
Niels Möller1e062892018-02-07 10:18:32 +0100267 RTC_GUARDED_BY(&encoder_queue_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200268 absl::optional<int64_t> last_parameters_update_ms_
Niels Möller1e062892018-02-07 10:18:32 +0100269 RTC_GUARDED_BY(&encoder_queue_);
sprang1a646ee2016-12-01 06:34:11 -0800270
perkj26091b12016-09-01 01:17:40 -0700271 // 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 07:53:41 -0700274
mflodmancc3d4422017-08-03 08:27:51 -0700275 RTC_DISALLOW_COPY_AND_ASSIGN(VideoStreamEncoder);
niklase@google.com470e71d2011-07-07 08:21:25 +0000276};
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000277
278} // namespace webrtc
279
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200280#endif // VIDEO_VIDEO_STREAM_ENCODER_H_