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 | #include "video/video_stream_encoder.h" |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 12 | |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 13 | #include <algorithm> |
perkj | 57c21f9 | 2016-06-17 07:27:16 -0700 | [diff] [blame] | 14 | #include <limits> |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 15 | #include <numeric> |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 16 | #include <utility> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 18 | #include "absl/memory/memory.h" |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 19 | #include "api/video/encoded_image.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "api/video/i420_buffer.h" |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 21 | #include "api/video/video_bitrate_allocator_factory.h" |
Sergey Silkin | 8b9b5f9 | 2018-12-10 09:28:53 +0100 | [diff] [blame] | 22 | #include "modules/video_coding/codecs/vp9/svc_rate_allocator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "modules/video_coding/include/video_codec_initializer.h" |
| 24 | #include "modules/video_coding/include/video_coding.h" |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 25 | #include "modules/video_coding/utility/default_video_bitrate_allocator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/arraysize.h" |
| 27 | #include "rtc_base/checks.h" |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 28 | #include "rtc_base/experiments/quality_scaling_experiment.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "rtc_base/location.h" |
| 30 | #include "rtc_base/logging.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 31 | #include "rtc_base/strings/string_builder.h" |
Karl Wiberg | 80ba333 | 2018-02-05 10:33:35 +0100 | [diff] [blame] | 32 | #include "rtc_base/system/fallthrough.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 33 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 34 | #include "rtc_base/trace_event.h" |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 35 | #include "system_wrappers/include/field_trial.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 36 | #include "video/overuse_frame_detector.h" |
nisse | ea3a798 | 2017-05-15 02:42:11 -0700 | [diff] [blame] | 37 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | namespace webrtc { |
| 39 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 40 | namespace { |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 41 | |
asapersson | 6ffb67d | 2016-09-12 00:10:45 -0700 | [diff] [blame] | 42 | // Time interval for logging frame counts. |
| 43 | const int64_t kFrameLogIntervalMs = 60000; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 44 | const int kMinFramerateFps = 2; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 45 | |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 46 | // Time to keep a single cached pending frame in paused state. |
| 47 | const int64_t kPendingFrameTimeoutMs = 1000; |
| 48 | |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 49 | const char kInitialFramedropFieldTrial[] = "WebRTC-InitialFramedrop"; |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 50 | constexpr char kFrameDropperFieldTrial[] = "WebRTC-FrameDropper"; |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 51 | |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 52 | // The maximum number of frames to drop at beginning of stream |
| 53 | // to try and achieve desired bitrate. |
| 54 | const int kMaxInitialFramedrop = 4; |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 55 | // When the first change in BWE above this threshold occurs, |
| 56 | // enable DropFrameDueToSize logic. |
| 57 | const float kFramedropThreshold = 0.3; |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 58 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 59 | // Averaging window spanning 90 frames at default 30fps, matching old media |
| 60 | // optimization module defaults. |
| 61 | const int64_t kFrameRateAvergingWindowSizeMs = (1000 / 30) * 90; |
| 62 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 63 | // Initial limits for BALANCED degradation preference. |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 64 | int MinFps(int pixels) { |
| 65 | if (pixels <= 320 * 240) { |
| 66 | return 7; |
| 67 | } else if (pixels <= 480 * 270) { |
| 68 | return 10; |
| 69 | } else if (pixels <= 640 * 480) { |
| 70 | return 15; |
| 71 | } else { |
| 72 | return std::numeric_limits<int>::max(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | int MaxFps(int pixels) { |
| 77 | if (pixels <= 320 * 240) { |
| 78 | return 10; |
| 79 | } else if (pixels <= 480 * 270) { |
| 80 | return 15; |
| 81 | } else { |
| 82 | return std::numeric_limits<int>::max(); |
| 83 | } |
| 84 | } |
| 85 | |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 86 | uint32_t abs_diff(uint32_t a, uint32_t b) { |
| 87 | return (a < b) ? b - a : a - b; |
| 88 | } |
| 89 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 90 | bool IsResolutionScalingEnabled(DegradationPreference degradation_preference) { |
| 91 | return degradation_preference == DegradationPreference::MAINTAIN_FRAMERATE || |
| 92 | degradation_preference == DegradationPreference::BALANCED; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 95 | bool IsFramerateScalingEnabled(DegradationPreference degradation_preference) { |
| 96 | return degradation_preference == DegradationPreference::MAINTAIN_RESOLUTION || |
| 97 | degradation_preference == DegradationPreference::BALANCED; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 100 | // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle |
| 101 | // pipelining encoders better (multiple input frames before something comes |
| 102 | // out). This should effectively turn off CPU adaptations for systems that |
| 103 | // remotely cope with the load right now. |
| 104 | CpuOveruseOptions GetCpuOveruseOptions( |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 105 | const VideoStreamEncoderSettings& settings, |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 106 | bool full_overuse_time) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 107 | CpuOveruseOptions options; |
| 108 | |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 109 | if (full_overuse_time) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 110 | options.low_encode_usage_threshold_percent = 150; |
| 111 | options.high_encode_usage_threshold_percent = 200; |
| 112 | } |
| 113 | if (settings.experiment_cpu_load_estimator) { |
| 114 | options.filter_time_ms = 5 * rtc::kNumMillisecsPerSec; |
| 115 | } |
| 116 | |
| 117 | return options; |
| 118 | } |
| 119 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 120 | } // namespace |
| 121 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 122 | // VideoSourceProxy is responsible ensuring thread safety between calls to |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 123 | // VideoStreamEncoder::SetSource that will happen on libjingle's worker thread |
| 124 | // when a video capturer is connected to the encoder and the encoder task queue |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 125 | // (encoder_queue_) where the encoder reports its VideoSinkWants. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 126 | class VideoStreamEncoder::VideoSourceProxy { |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 127 | public: |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 128 | explicit VideoSourceProxy(VideoStreamEncoder* video_stream_encoder) |
| 129 | : video_stream_encoder_(video_stream_encoder), |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 130 | degradation_preference_(DegradationPreference::DISABLED), |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 131 | source_(nullptr), |
| 132 | max_framerate_(std::numeric_limits<int>::max()) {} |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 133 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 134 | void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, |
| 135 | const DegradationPreference& degradation_preference) { |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 136 | // Called on libjingle's worker thread. |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 137 | RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); |
| 138 | rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 139 | rtc::VideoSinkWants wants; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 140 | { |
| 141 | rtc::CritScope lock(&crit_); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 142 | degradation_preference_ = degradation_preference; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 143 | old_source = source_; |
| 144 | source_ = source; |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 145 | wants = GetActiveSinkWantsInternal(); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | if (old_source != source && old_source != nullptr) { |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 149 | old_source->RemoveSink(video_stream_encoder_); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (!source) { |
| 153 | return; |
| 154 | } |
| 155 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 156 | source->AddOrUpdateSink(video_stream_encoder_, wants); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 159 | void SetMaxFramerate(int max_framerate) { |
| 160 | RTC_DCHECK_GT(max_framerate, 0); |
| 161 | rtc::CritScope lock(&crit_); |
| 162 | if (max_framerate == max_framerate_) |
| 163 | return; |
| 164 | |
| 165 | RTC_LOG(LS_INFO) << "Set max framerate: " << max_framerate; |
| 166 | max_framerate_ = max_framerate; |
| 167 | if (source_) { |
| 168 | source_->AddOrUpdateSink(video_stream_encoder_, |
| 169 | GetActiveSinkWantsInternal()); |
| 170 | } |
| 171 | } |
| 172 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 173 | void SetWantsRotationApplied(bool rotation_applied) { |
| 174 | rtc::CritScope lock(&crit_); |
| 175 | sink_wants_.rotation_applied = rotation_applied; |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 176 | if (source_) { |
| 177 | source_->AddOrUpdateSink(video_stream_encoder_, |
| 178 | GetActiveSinkWantsInternal()); |
| 179 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 180 | } |
| 181 | |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 182 | rtc::VideoSinkWants GetActiveSinkWants() { |
| 183 | rtc::CritScope lock(&crit_); |
| 184 | return GetActiveSinkWantsInternal(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 185 | } |
| 186 | |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 187 | void ResetPixelFpsCount() { |
| 188 | rtc::CritScope lock(&crit_); |
| 189 | sink_wants_.max_pixel_count = std::numeric_limits<int>::max(); |
| 190 | sink_wants_.target_pixel_count.reset(); |
| 191 | sink_wants_.max_framerate_fps = std::numeric_limits<int>::max(); |
| 192 | if (source_) |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 193 | source_->AddOrUpdateSink(video_stream_encoder_, |
| 194 | GetActiveSinkWantsInternal()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Åsa Persson | c3ed630 | 2017-11-16 14:04:52 +0100 | [diff] [blame] | 197 | bool RequestResolutionLowerThan(int pixel_count, |
| 198 | int min_pixels_per_frame, |
| 199 | bool* min_pixels_reached) { |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 200 | // Called on the encoder task queue. |
| 201 | rtc::CritScope lock(&crit_); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 202 | if (!source_ || !IsResolutionScalingEnabled(degradation_preference_)) { |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 203 | // This can happen since |degradation_preference_| is set on libjingle's |
| 204 | // worker thread but the adaptation is done on the encoder task queue. |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 205 | return false; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 206 | } |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 207 | // The input video frame size will have a resolution less than or equal to |
| 208 | // |max_pixel_count| depending on how the source can scale the frame size. |
kthelgason | 5e13d41 | 2016-12-01 03:59:51 -0800 | [diff] [blame] | 209 | const int pixels_wanted = (pixel_count * 3) / 5; |
Åsa Persson | c3ed630 | 2017-11-16 14:04:52 +0100 | [diff] [blame] | 210 | if (pixels_wanted >= sink_wants_.max_pixel_count) { |
| 211 | return false; |
| 212 | } |
| 213 | if (pixels_wanted < min_pixels_per_frame) { |
| 214 | *min_pixels_reached = true; |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 215 | return false; |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 216 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 217 | RTC_LOG(LS_INFO) << "Scaling down resolution, max pixels: " |
| 218 | << pixels_wanted; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 219 | sink_wants_.max_pixel_count = pixels_wanted; |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 220 | sink_wants_.target_pixel_count = absl::nullopt; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 221 | source_->AddOrUpdateSink(video_stream_encoder_, |
| 222 | GetActiveSinkWantsInternal()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 223 | return true; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 224 | } |
| 225 | |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 226 | int RequestFramerateLowerThan(int fps) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 227 | // Called on the encoder task queue. |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 228 | // The input video frame rate will be scaled down to 2/3, rounding down. |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 229 | int framerate_wanted = (fps * 2) / 3; |
| 230 | return RestrictFramerate(framerate_wanted) ? framerate_wanted : -1; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 231 | } |
| 232 | |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 233 | bool RequestHigherResolutionThan(int pixel_count) { |
| 234 | // Called on the encoder task queue. |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 235 | rtc::CritScope lock(&crit_); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 236 | if (!source_ || !IsResolutionScalingEnabled(degradation_preference_)) { |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 237 | // This can happen since |degradation_preference_| is set on libjingle's |
| 238 | // worker thread but the adaptation is done on the encoder task queue. |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 239 | return false; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 240 | } |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 241 | int max_pixels_wanted = pixel_count; |
| 242 | if (max_pixels_wanted != std::numeric_limits<int>::max()) |
| 243 | max_pixels_wanted = pixel_count * 4; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 244 | |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 245 | if (max_pixels_wanted <= sink_wants_.max_pixel_count) |
| 246 | return false; |
| 247 | |
| 248 | sink_wants_.max_pixel_count = max_pixels_wanted; |
| 249 | if (max_pixels_wanted == std::numeric_limits<int>::max()) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 250 | // Remove any constraints. |
| 251 | sink_wants_.target_pixel_count.reset(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 252 | } else { |
| 253 | // On step down we request at most 3/5 the pixel count of the previous |
| 254 | // resolution, so in order to take "one step up" we request a resolution |
| 255 | // as close as possible to 5/3 of the current resolution. The actual pixel |
| 256 | // count selected depends on the capabilities of the source. In order to |
| 257 | // not take a too large step up, we cap the requested pixel count to be at |
| 258 | // most four time the current number of pixels. |
Oskar Sundbom | 8e07c13 | 2018-01-08 16:45:42 +0100 | [diff] [blame] | 259 | sink_wants_.target_pixel_count = (pixel_count * 5) / 3; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 260 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 261 | RTC_LOG(LS_INFO) << "Scaling up resolution, max pixels: " |
| 262 | << max_pixels_wanted; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 263 | source_->AddOrUpdateSink(video_stream_encoder_, |
| 264 | GetActiveSinkWantsInternal()); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 265 | return true; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 266 | } |
| 267 | |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 268 | // Request upgrade in framerate. Returns the new requested frame, or -1 if |
| 269 | // no change requested. Note that maxint may be returned if limits due to |
| 270 | // adaptation requests are removed completely. In that case, consider |
| 271 | // |max_framerate_| to be the current limit (assuming the capturer complies). |
| 272 | int RequestHigherFramerateThan(int fps) { |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 273 | // Called on the encoder task queue. |
| 274 | // The input frame rate will be scaled up to the last step, with rounding. |
| 275 | int framerate_wanted = fps; |
| 276 | if (fps != std::numeric_limits<int>::max()) |
| 277 | framerate_wanted = (fps * 3) / 2; |
| 278 | |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 279 | return IncreaseFramerate(framerate_wanted) ? framerate_wanted : -1; |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | bool RestrictFramerate(int fps) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 283 | // Called on the encoder task queue. |
| 284 | rtc::CritScope lock(&crit_); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 285 | if (!source_ || !IsFramerateScalingEnabled(degradation_preference_)) |
| 286 | return false; |
| 287 | |
| 288 | const int fps_wanted = std::max(kMinFramerateFps, fps); |
| 289 | if (fps_wanted >= sink_wants_.max_framerate_fps) |
| 290 | return false; |
| 291 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 292 | RTC_LOG(LS_INFO) << "Scaling down framerate: " << fps_wanted; |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 293 | sink_wants_.max_framerate_fps = fps_wanted; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 294 | source_->AddOrUpdateSink(video_stream_encoder_, |
| 295 | GetActiveSinkWantsInternal()); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 296 | return true; |
| 297 | } |
| 298 | |
| 299 | bool IncreaseFramerate(int fps) { |
| 300 | // Called on the encoder task queue. |
| 301 | rtc::CritScope lock(&crit_); |
| 302 | if (!source_ || !IsFramerateScalingEnabled(degradation_preference_)) |
| 303 | return false; |
| 304 | |
| 305 | const int fps_wanted = std::max(kMinFramerateFps, fps); |
| 306 | if (fps_wanted <= sink_wants_.max_framerate_fps) |
| 307 | return false; |
| 308 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 309 | RTC_LOG(LS_INFO) << "Scaling up framerate: " << fps_wanted; |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 310 | sink_wants_.max_framerate_fps = fps_wanted; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 311 | source_->AddOrUpdateSink(video_stream_encoder_, |
| 312 | GetActiveSinkWantsInternal()); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 313 | return true; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 314 | } |
| 315 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 316 | private: |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 317 | rtc::VideoSinkWants GetActiveSinkWantsInternal() |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 318 | RTC_EXCLUSIVE_LOCKS_REQUIRED(&crit_) { |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 319 | rtc::VideoSinkWants wants = sink_wants_; |
| 320 | // Clear any constraints from the current sink wants that don't apply to |
| 321 | // the used degradation_preference. |
| 322 | switch (degradation_preference_) { |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 323 | case DegradationPreference::BALANCED: |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 324 | break; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 325 | case DegradationPreference::MAINTAIN_FRAMERATE: |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 326 | wants.max_framerate_fps = std::numeric_limits<int>::max(); |
| 327 | break; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 328 | case DegradationPreference::MAINTAIN_RESOLUTION: |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 329 | wants.max_pixel_count = std::numeric_limits<int>::max(); |
| 330 | wants.target_pixel_count.reset(); |
| 331 | break; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 332 | case DegradationPreference::DISABLED: |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 333 | wants.max_pixel_count = std::numeric_limits<int>::max(); |
| 334 | wants.target_pixel_count.reset(); |
| 335 | wants.max_framerate_fps = std::numeric_limits<int>::max(); |
| 336 | } |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 337 | // Limit to configured max framerate. |
| 338 | wants.max_framerate_fps = std::min(max_framerate_, wants.max_framerate_fps); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 339 | return wants; |
| 340 | } |
| 341 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 342 | rtc::CriticalSection crit_; |
| 343 | rtc::SequencedTaskChecker main_checker_; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 344 | VideoStreamEncoder* const video_stream_encoder_; |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 345 | rtc::VideoSinkWants sink_wants_ RTC_GUARDED_BY(&crit_); |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 346 | DegradationPreference degradation_preference_ RTC_GUARDED_BY(&crit_); |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 347 | rtc::VideoSourceInterface<VideoFrame>* source_ RTC_GUARDED_BY(&crit_); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 348 | int max_framerate_ RTC_GUARDED_BY(&crit_); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 349 | |
| 350 | RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); |
| 351 | }; |
| 352 | |
Åsa Persson | 0122e84 | 2017-10-16 12:19:23 +0200 | [diff] [blame] | 353 | VideoStreamEncoder::VideoStreamEncoder( |
| 354 | uint32_t number_of_cores, |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 355 | VideoStreamEncoderObserver* encoder_stats_observer, |
| 356 | const VideoStreamEncoderSettings& settings, |
Åsa Persson | 0122e84 | 2017-10-16 12:19:23 +0200 | [diff] [blame] | 357 | std::unique_ptr<OveruseFrameDetector> overuse_detector) |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 358 | : shutdown_event_(true /* manual_reset */, false), |
| 359 | number_of_cores_(number_of_cores), |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 360 | initial_framedrop_(0), |
| 361 | initial_framedrop_on_bwe_enabled_( |
| 362 | webrtc::field_trial::IsEnabled(kInitialFramedropFieldTrial)), |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 363 | quality_scaling_experiment_enabled_(QualityScalingExperiment::Enabled()), |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 364 | source_proxy_(new VideoSourceProxy(this)), |
Per | a48ddb7 | 2016-09-29 11:48:50 +0200 | [diff] [blame] | 365 | sink_(nullptr), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 366 | settings_(settings), |
Niels Möller | a056599 | 2017-10-24 11:37:08 +0200 | [diff] [blame] | 367 | video_sender_(Clock::GetRealTimeClock(), this), |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 368 | overuse_detector_(std::move(overuse_detector)), |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 369 | encoder_stats_observer_(encoder_stats_observer), |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 370 | max_framerate_(-1), |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 371 | pending_encoder_reconfiguration_(false), |
Mirta Dvornicic | 1ec2a16 | 2018-12-10 09:47:34 +0000 | [diff] [blame] | 372 | pending_encoder_creation_(false), |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 373 | crop_width_(0), |
| 374 | crop_height_(0), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 375 | encoder_start_bitrate_bps_(0), |
Per | a48ddb7 | 2016-09-29 11:48:50 +0200 | [diff] [blame] | 376 | max_data_payload_length_(0), |
pbos@webrtc.org | 143451d | 2015-03-18 14:40:03 +0000 | [diff] [blame] | 377 | last_observed_bitrate_bps_(0), |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 378 | encoder_paused_and_dropped_frame_(false), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 379 | clock_(Clock::GetRealTimeClock()), |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 380 | degradation_preference_(DegradationPreference::DISABLED), |
Yuwei Huang | d9f99c1 | 2017-10-24 15:40:52 -0700 | [diff] [blame] | 381 | posted_frames_waiting_for_encode_(0), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 382 | last_captured_timestamp_(0), |
| 383 | delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - |
| 384 | clock_->TimeInMilliseconds()), |
asapersson | 6ffb67d | 2016-09-12 00:10:45 -0700 | [diff] [blame] | 385 | last_frame_log_ms_(clock_->TimeInMilliseconds()), |
| 386 | captured_frame_count_(0), |
| 387 | dropped_frame_count_(0), |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 388 | pending_frame_post_time_us_(0), |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 389 | bitrate_observer_(nullptr), |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 390 | force_disable_frame_dropper_(false), |
| 391 | input_framerate_(kFrameRateAvergingWindowSizeMs, 1000), |
| 392 | pending_frame_drops_(0), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 393 | encoder_queue_("EncoderQueue") { |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 394 | RTC_DCHECK(encoder_stats_observer); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 395 | RTC_DCHECK(overuse_detector_); |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 396 | } |
| 397 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 398 | VideoStreamEncoder::~VideoStreamEncoder() { |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 399 | RTC_DCHECK_RUN_ON(&thread_checker_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 400 | RTC_DCHECK(shutdown_event_.Wait(0)) |
| 401 | << "Must call ::Stop() before destruction."; |
| 402 | } |
| 403 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 404 | void VideoStreamEncoder::Stop() { |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 405 | RTC_DCHECK_RUN_ON(&thread_checker_); |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 406 | source_proxy_->SetSource(nullptr, DegradationPreference()); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 407 | encoder_queue_.PostTask([this] { |
| 408 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 409 | overuse_detector_->StopCheckForOveruse(); |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 410 | rate_allocator_.reset(); |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 411 | bitrate_observer_ = nullptr; |
Niels Möller | bf3dbb4 | 2018-03-16 13:38:46 +0100 | [diff] [blame] | 412 | video_sender_.RegisterExternalEncoder(nullptr, false); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 413 | quality_scaler_ = nullptr; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 414 | shutdown_event_.Set(); |
| 415 | }); |
| 416 | |
| 417 | shutdown_event_.Wait(rtc::Event::kForever); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 420 | void VideoStreamEncoder::SetBitrateAllocationObserver( |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 421 | VideoBitrateAllocationObserver* bitrate_observer) { |
| 422 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 423 | encoder_queue_.PostTask([this, bitrate_observer] { |
| 424 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 425 | RTC_DCHECK(!bitrate_observer_); |
| 426 | bitrate_observer_ = bitrate_observer; |
| 427 | }); |
| 428 | } |
| 429 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 430 | void VideoStreamEncoder::SetSource( |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 431 | rtc::VideoSourceInterface<VideoFrame>* source, |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 432 | const DegradationPreference& degradation_preference) { |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 433 | RTC_DCHECK_RUN_ON(&thread_checker_); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 434 | source_proxy_->SetSource(source, degradation_preference); |
| 435 | encoder_queue_.PostTask([this, degradation_preference] { |
| 436 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 437 | if (degradation_preference_ != degradation_preference) { |
| 438 | // Reset adaptation state, so that we're not tricked into thinking there's |
| 439 | // an already pending request of the same type. |
| 440 | last_adaptation_request_.reset(); |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 441 | if (degradation_preference == DegradationPreference::BALANCED || |
| 442 | degradation_preference_ == DegradationPreference::BALANCED) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 443 | // TODO(asapersson): Consider removing |adapt_counters_| map and use one |
| 444 | // AdaptCounter for all modes. |
| 445 | source_proxy_->ResetPixelFpsCount(); |
| 446 | adapt_counters_.clear(); |
| 447 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 448 | } |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 449 | degradation_preference_ = degradation_preference; |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 450 | |
Mirta Dvornicic | 1ec2a16 | 2018-12-10 09:47:34 +0000 | [diff] [blame] | 451 | if (encoder_) |
Niels Möller | 2d06118 | 2018-04-24 09:13:08 +0200 | [diff] [blame] | 452 | ConfigureQualityScaler(); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 453 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 454 | if (!IsFramerateScalingEnabled(degradation_preference) && |
| 455 | max_framerate_ != -1) { |
| 456 | // If frame rate scaling is no longer allowed, remove any potential |
| 457 | // allowance for longer frame intervals. |
| 458 | overuse_detector_->OnTargetFramerateUpdated(max_framerate_); |
| 459 | } |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 460 | }); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 461 | } |
| 462 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 463 | void VideoStreamEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 464 | source_proxy_->SetWantsRotationApplied(rotation_applied); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 465 | encoder_queue_.PostTask([this, sink] { |
| 466 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 467 | sink_ = sink; |
| 468 | }); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 469 | } |
| 470 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 471 | void VideoStreamEncoder::SetStartBitrate(int start_bitrate_bps) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 472 | encoder_queue_.PostTask([this, start_bitrate_bps] { |
| 473 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 474 | encoder_start_bitrate_bps_ = start_bitrate_bps; |
| 475 | }); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 476 | } |
Peter Boström | 00b9d21 | 2016-05-19 16:59:03 +0200 | [diff] [blame] | 477 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 478 | void VideoStreamEncoder::ConfigureEncoder(VideoEncoderConfig config, |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 479 | size_t max_data_payload_length) { |
Sebastian Jansson | 3dc0125 | 2018-03-19 19:27:44 +0100 | [diff] [blame] | 480 | // TODO(srte): This struct should be replaced by a lambda with move capture |
| 481 | // when C++14 lambda is allowed. |
| 482 | struct ConfigureEncoderTask { |
| 483 | void operator()() { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 484 | encoder->ConfigureEncoderOnTaskQueue(std::move(config), |
| 485 | max_data_payload_length); |
Sebastian Jansson | 3dc0125 | 2018-03-19 19:27:44 +0100 | [diff] [blame] | 486 | } |
| 487 | VideoStreamEncoder* encoder; |
| 488 | VideoEncoderConfig config; |
| 489 | size_t max_data_payload_length; |
Sebastian Jansson | 3dc0125 | 2018-03-19 19:27:44 +0100 | [diff] [blame] | 490 | }; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 491 | encoder_queue_.PostTask( |
| 492 | ConfigureEncoderTask{this, std::move(config), max_data_payload_length}); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 493 | } |
| 494 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 495 | void VideoStreamEncoder::ConfigureEncoderOnTaskQueue( |
| 496 | VideoEncoderConfig config, |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 497 | size_t max_data_payload_length) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 498 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 499 | RTC_DCHECK(sink_); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 500 | RTC_LOG(LS_INFO) << "ConfigureEncoder requested."; |
Per | a48ddb7 | 2016-09-29 11:48:50 +0200 | [diff] [blame] | 501 | |
| 502 | max_data_payload_length_ = max_data_payload_length; |
Mirta Dvornicic | 1ec2a16 | 2018-12-10 09:47:34 +0000 | [diff] [blame] | 503 | pending_encoder_creation_ = |
| 504 | (!encoder_ || encoder_config_.video_format != config.video_format); |
Per | a48ddb7 | 2016-09-29 11:48:50 +0200 | [diff] [blame] | 505 | encoder_config_ = std::move(config); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 506 | pending_encoder_reconfiguration_ = true; |
Per | a48ddb7 | 2016-09-29 11:48:50 +0200 | [diff] [blame] | 507 | |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 508 | // Reconfigure the encoder now if the encoder has an internal source or |
Per | 21d45d2 | 2016-10-30 21:37:57 +0100 | [diff] [blame] | 509 | // if the frame resolution is known. Otherwise, the reconfiguration is |
| 510 | // deferred until the next frame to minimize the number of reconfigurations. |
| 511 | // The codec configuration depends on incoming video frame size. |
| 512 | if (last_frame_info_) { |
| 513 | ReconfigureEncoder(); |
Mirta Dvornicic | 1ec2a16 | 2018-12-10 09:47:34 +0000 | [diff] [blame] | 514 | } else if (settings_.encoder_factory |
| 515 | ->QueryVideoEncoder(encoder_config_.video_format) |
| 516 | .has_internal_source) { |
| 517 | last_frame_info_ = VideoFrameInfo(176, 144, false); |
| 518 | ReconfigureEncoder(); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 521 | |
Seth Hampson | cc7125f | 2018-02-02 08:46:16 -0800 | [diff] [blame] | 522 | // TODO(bugs.webrtc.org/8807): Currently this always does a hard |
| 523 | // reconfiguration, but this isn't always necessary. Add in logic to only update |
| 524 | // the VideoBitrateAllocator and call OnEncoderConfigurationChanged with a |
| 525 | // "soft" reconfiguration. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 526 | void VideoStreamEncoder::ReconfigureEncoder() { |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 527 | RTC_DCHECK(pending_encoder_reconfiguration_); |
| 528 | std::vector<VideoStream> streams = |
| 529 | encoder_config_.video_stream_factory->CreateEncoderStreams( |
| 530 | last_frame_info_->width, last_frame_info_->height, encoder_config_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 531 | |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 532 | // TODO(ilnik): If configured resolution is significantly less than provided, |
| 533 | // e.g. because there are not enough SSRCs for all simulcast streams, |
| 534 | // signal new resolutions via SinkWants to video source. |
| 535 | |
| 536 | // Stream dimensions may be not equal to given because of a simulcast |
| 537 | // restrictions. |
Florent Castelli | 450b548 | 2018-11-29 17:32:47 +0100 | [diff] [blame] | 538 | auto highest_stream = std::max_element( |
| 539 | streams.begin(), streams.end(), |
| 540 | [](const webrtc::VideoStream& a, const webrtc::VideoStream& b) { |
| 541 | return std::tie(a.width, a.height) < std::tie(b.width, b.height); |
| 542 | }); |
| 543 | int highest_stream_width = static_cast<int>(highest_stream->width); |
| 544 | int highest_stream_height = static_cast<int>(highest_stream->height); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 545 | // Dimension may be reduced to be, e.g. divisible by 4. |
| 546 | RTC_CHECK_GE(last_frame_info_->width, highest_stream_width); |
| 547 | RTC_CHECK_GE(last_frame_info_->height, highest_stream_height); |
| 548 | crop_width_ = last_frame_info_->width - highest_stream_width; |
| 549 | crop_height_ = last_frame_info_->height - highest_stream_height; |
| 550 | |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 551 | VideoCodec codec; |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 552 | if (!VideoCodecInitializer::SetupCodec(encoder_config_, streams, &codec)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 553 | RTC_LOG(LS_ERROR) << "Failed to create encoder configuration."; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 554 | } |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 555 | |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 556 | rate_allocator_ = |
| 557 | settings_.bitrate_allocator_factory->CreateVideoBitrateAllocator(codec); |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 558 | |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame] | 559 | // Set min_bitrate_bps, max_bitrate_bps, and max padding bit rate for VP9. |
| 560 | if (encoder_config_.codec_type == kVideoCodecVP9) { |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame] | 561 | // Lower max bitrate to the level codec actually can produce. |
Sergey Silkin | 8b9b5f9 | 2018-12-10 09:28:53 +0100 | [diff] [blame] | 562 | streams[0].max_bitrate_bps = std::min<int>( |
| 563 | streams[0].max_bitrate_bps, SvcRateAllocator::GetMaxBitrateBps(codec)); |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame] | 564 | streams[0].min_bitrate_bps = codec.spatialLayers[0].minBitrate * 1000; |
Sergey Silkin | 8b9b5f9 | 2018-12-10 09:28:53 +0100 | [diff] [blame] | 565 | // target_bitrate_bps specifies the maximum padding bitrate. |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame] | 566 | streams[0].target_bitrate_bps = |
Sergey Silkin | 8b9b5f9 | 2018-12-10 09:28:53 +0100 | [diff] [blame] | 567 | SvcRateAllocator::GetPaddingBitrateBps(codec); |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame] | 568 | } |
| 569 | |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 570 | codec.startBitrate = |
| 571 | std::max(encoder_start_bitrate_bps_ / 1000, codec.minBitrate); |
| 572 | codec.startBitrate = std::min(codec.startBitrate, codec.maxBitrate); |
| 573 | codec.expect_encode_from_texture = last_frame_info_->is_texture; |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 574 | max_framerate_ = codec.maxFramerate; |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 575 | |
| 576 | // Inform source about max configured framerate. |
| 577 | int max_framerate = 0; |
| 578 | for (const auto& stream : streams) { |
| 579 | max_framerate = std::max(stream.max_framerate, max_framerate); |
| 580 | } |
| 581 | source_proxy_->SetMaxFramerate(max_framerate); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 582 | |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 583 | // Keep the same encoder, as long as the video_format is unchanged. |
Mirta Dvornicic | ccc1b57 | 2019-01-15 12:42:18 +0100 | [diff] [blame] | 584 | // Encoder creation block is split in two since EncoderInfo needed to start |
| 585 | // CPU adaptation with the correct settings should be polled after |
| 586 | // encoder_->InitEncode(). |
Mirta Dvornicic | 1ec2a16 | 2018-12-10 09:47:34 +0000 | [diff] [blame] | 587 | if (pending_encoder_creation_) { |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 588 | if (encoder_) { |
| 589 | video_sender_.RegisterExternalEncoder(nullptr, false); |
| 590 | } |
| 591 | |
Mirta Dvornicic | 1ec2a16 | 2018-12-10 09:47:34 +0000 | [diff] [blame] | 592 | encoder_ = settings_.encoder_factory->CreateVideoEncoder( |
| 593 | encoder_config_.video_format); |
| 594 | // TODO(nisse): What to do if creating the encoder fails? Crash, |
| 595 | // or just discard incoming frames? |
| 596 | RTC_CHECK(encoder_); |
| 597 | |
| 598 | const webrtc::VideoEncoderFactory::CodecInfo info = |
| 599 | settings_.encoder_factory->QueryVideoEncoder( |
| 600 | encoder_config_.video_format); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 601 | |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 602 | video_sender_.RegisterExternalEncoder(encoder_.get(), |
Mirta Dvornicic | 1ec2a16 | 2018-12-10 09:47:34 +0000 | [diff] [blame] | 603 | info.has_internal_source); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 604 | } |
| 605 | // RegisterSendCodec implies an unconditional call to |
| 606 | // encoder_->InitEncode(). |
Peter Boström | cd5c25c | 2016-04-21 16:48:08 +0200 | [diff] [blame] | 607 | bool success = video_sender_.RegisterSendCodec( |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 608 | &codec, number_of_cores_, |
| 609 | static_cast<uint32_t>(max_data_payload_length_)) == VCM_OK; |
Peter Boström | 905f8e7 | 2016-03-02 16:59:56 +0100 | [diff] [blame] | 610 | if (!success) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 611 | RTC_LOG(LS_ERROR) << "Failed to configure encoder."; |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 612 | rate_allocator_.reset(); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 613 | } |
Peter Boström | 905f8e7 | 2016-03-02 16:59:56 +0100 | [diff] [blame] | 614 | |
Mirta Dvornicic | ccc1b57 | 2019-01-15 12:42:18 +0100 | [diff] [blame] | 615 | if (pending_encoder_creation_) { |
| 616 | overuse_detector_->StopCheckForOveruse(); |
| 617 | overuse_detector_->StartCheckForOveruse( |
| 618 | GetCpuOveruseOptions( |
| 619 | settings_, encoder_->GetEncoderInfo().is_hardware_accelerated), |
| 620 | this); |
| 621 | pending_encoder_creation_ = false; |
| 622 | } |
| 623 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 624 | int num_layers; |
| 625 | if (codec.codecType == kVideoCodecVP8) { |
| 626 | num_layers = codec.VP8()->numberOfTemporalLayers; |
| 627 | } else if (codec.codecType == kVideoCodecVP9) { |
| 628 | num_layers = codec.VP9()->numberOfTemporalLayers; |
| 629 | } else if (codec.codecType == kVideoCodecGeneric && |
| 630 | codec.numberOfSimulcastStreams > 0) { |
| 631 | // This is mainly for unit testing, disabling frame dropping. |
| 632 | // TODO(sprang): Add a better way to disable frame dropping. |
| 633 | num_layers = codec.simulcastStream[0].numberOfTemporalLayers; |
| 634 | } else { |
| 635 | num_layers = 1; |
| 636 | } |
| 637 | |
| 638 | frame_dropper_.Reset(); |
| 639 | frame_dropper_.SetRates(codec.startBitrate, max_framerate_); |
| 640 | uint32_t framerate_fps = GetInputFramerateFps(); |
| 641 | // Force-disable frame dropper if either: |
| 642 | // * We have screensharing with layers. |
| 643 | // * "WebRTC-FrameDropper" field trial is "Disabled". |
| 644 | force_disable_frame_dropper_ = |
| 645 | field_trial::IsDisabled(kFrameDropperFieldTrial) || |
| 646 | (num_layers > 1 && codec.mode == VideoCodecMode::kScreensharing); |
| 647 | |
| 648 | if (rate_allocator_ && last_observed_bitrate_bps_ > 0) { |
| 649 | // We have a new rate allocator instance and already configured target |
| 650 | // bitrate. Update the rate allocation and notify observsers. |
| 651 | VideoBitrateAllocation bitrate_allocation = |
| 652 | GetBitrateAllocationAndNotifyObserver(last_observed_bitrate_bps_, |
| 653 | framerate_fps); |
| 654 | |
| 655 | video_sender_.SetChannelParameters(bitrate_allocation, framerate_fps); |
| 656 | } |
ilnik | 35b7de4 | 2017-03-15 04:24:21 -0700 | [diff] [blame] | 657 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 658 | encoder_stats_observer_->OnEncoderReconfigured(encoder_config_, streams); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 659 | |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 660 | pending_encoder_reconfiguration_ = false; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 661 | |
Per | a48ddb7 | 2016-09-29 11:48:50 +0200 | [diff] [blame] | 662 | sink_->OnEncoderConfigurationChanged( |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 663 | std::move(streams), encoder_config_.min_transmit_bitrate_bps); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 664 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 665 | // Get the current target framerate, ie the maximum framerate as specified by |
| 666 | // the current codec configuration, or any limit imposed by cpu adaption in |
| 667 | // maintain-resolution or balanced mode. This is used to make sure overuse |
| 668 | // detection doesn't needlessly trigger in low and/or variable framerate |
| 669 | // scenarios. |
| 670 | int target_framerate = std::min( |
| 671 | max_framerate_, source_proxy_->GetActiveSinkWants().max_framerate_fps); |
| 672 | overuse_detector_->OnTargetFramerateUpdated(target_framerate); |
Niels Möller | 2d06118 | 2018-04-24 09:13:08 +0200 | [diff] [blame] | 673 | |
| 674 | ConfigureQualityScaler(); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 675 | } |
| 676 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 677 | void VideoStreamEncoder::ConfigureQualityScaler() { |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 678 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 679 | const auto scaling_settings = encoder_->GetEncoderInfo().scaling_settings; |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 680 | const bool quality_scaling_allowed = |
asapersson | 91914e2 | 2017-06-01 00:34:08 -0700 | [diff] [blame] | 681 | IsResolutionScalingEnabled(degradation_preference_) && |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 682 | scaling_settings.thresholds; |
kthelgason | 3af6cc0 | 2017-03-22 00:25:28 -0700 | [diff] [blame] | 683 | |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 684 | if (quality_scaling_allowed) { |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 685 | if (quality_scaler_.get() == nullptr) { |
| 686 | // Quality scaler has not already been configured. |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 687 | |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 688 | // Use experimental thresholds if available. |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 689 | absl::optional<VideoEncoder::QpThresholds> experimental_thresholds; |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 690 | if (quality_scaling_experiment_enabled_) { |
| 691 | experimental_thresholds = QualityScalingExperiment::GetQpThresholds( |
| 692 | encoder_config_.codec_type); |
| 693 | } |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 694 | // Since the interface is non-public, absl::make_unique can't do this |
| 695 | // upcast. |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 696 | AdaptationObserverInterface* observer = this; |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 697 | quality_scaler_ = absl::make_unique<QualityScaler>( |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 698 | observer, experimental_thresholds ? *experimental_thresholds |
| 699 | : *(scaling_settings.thresholds)); |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 700 | has_seen_first_significant_bwe_change_ = false; |
| 701 | initial_framedrop_ = 0; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 702 | } |
| 703 | } else { |
| 704 | quality_scaler_.reset(nullptr); |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 705 | initial_framedrop_ = kMaxInitialFramedrop; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 706 | } |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 707 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 708 | encoder_stats_observer_->OnAdaptationChanged( |
| 709 | VideoStreamEncoderObserver::AdaptationReason::kNone, |
| 710 | GetActiveCounts(kCpu), GetActiveCounts(kQuality)); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 711 | } |
| 712 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 713 | void VideoStreamEncoder::OnFrame(const VideoFrame& video_frame) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 714 | RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 715 | VideoFrame incoming_frame = video_frame; |
| 716 | |
| 717 | // Local time in webrtc time base. |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 718 | int64_t current_time_us = clock_->TimeInMicroseconds(); |
| 719 | int64_t current_time_ms = current_time_us / rtc::kNumMicrosecsPerMillisec; |
| 720 | // In some cases, e.g., when the frame from decoder is fed to encoder, |
| 721 | // the timestamp may be set to the future. As the encoding pipeline assumes |
| 722 | // capture time to be less than present time, we should reset the capture |
| 723 | // timestamps here. Otherwise there may be issues with RTP send stream. |
| 724 | if (incoming_frame.timestamp_us() > current_time_us) |
| 725 | incoming_frame.set_timestamp_us(current_time_us); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 726 | |
| 727 | // Capture time may come from clock with an offset and drift from clock_. |
| 728 | int64_t capture_ntp_time_ms; |
nisse | 891419f | 2017-01-12 10:02:22 -0800 | [diff] [blame] | 729 | if (video_frame.ntp_time_ms() > 0) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 730 | capture_ntp_time_ms = video_frame.ntp_time_ms(); |
| 731 | } else if (video_frame.render_time_ms() != 0) { |
| 732 | capture_ntp_time_ms = video_frame.render_time_ms() + delta_ntp_internal_ms_; |
| 733 | } else { |
nisse | 1c0dea8 | 2017-01-30 02:43:18 -0800 | [diff] [blame] | 734 | capture_ntp_time_ms = current_time_ms + delta_ntp_internal_ms_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 735 | } |
| 736 | incoming_frame.set_ntp_time_ms(capture_ntp_time_ms); |
| 737 | |
| 738 | // Convert NTP time, in ms, to RTP timestamp. |
| 739 | const int kMsToRtpTimestamp = 90; |
| 740 | incoming_frame.set_timestamp( |
| 741 | kMsToRtpTimestamp * static_cast<uint32_t>(incoming_frame.ntp_time_ms())); |
| 742 | |
| 743 | if (incoming_frame.ntp_time_ms() <= last_captured_timestamp_) { |
| 744 | // We don't allow the same capture time for two frames, drop this one. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 745 | RTC_LOG(LS_WARNING) << "Same/old NTP timestamp (" |
| 746 | << incoming_frame.ntp_time_ms() |
| 747 | << " <= " << last_captured_timestamp_ |
| 748 | << ") for incoming frame. Dropping."; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 749 | return; |
| 750 | } |
| 751 | |
asapersson | 6ffb67d | 2016-09-12 00:10:45 -0700 | [diff] [blame] | 752 | bool log_stats = false; |
nisse | 1c0dea8 | 2017-01-30 02:43:18 -0800 | [diff] [blame] | 753 | if (current_time_ms - last_frame_log_ms_ > kFrameLogIntervalMs) { |
| 754 | last_frame_log_ms_ = current_time_ms; |
asapersson | 6ffb67d | 2016-09-12 00:10:45 -0700 | [diff] [blame] | 755 | log_stats = true; |
| 756 | } |
| 757 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 758 | last_captured_timestamp_ = incoming_frame.ntp_time_ms(); |
Sebastian Jansson | 3ab5c40 | 2018-04-05 12:30:50 +0200 | [diff] [blame] | 759 | |
| 760 | int64_t post_time_us = rtc::TimeMicros(); |
| 761 | ++posted_frames_waiting_for_encode_; |
| 762 | |
| 763 | encoder_queue_.PostTask( |
| 764 | [this, incoming_frame, post_time_us, log_stats]() { |
| 765 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 766 | encoder_stats_observer_->OnIncomingFrame(incoming_frame.width(), |
| 767 | incoming_frame.height()); |
Sebastian Jansson | 3ab5c40 | 2018-04-05 12:30:50 +0200 | [diff] [blame] | 768 | ++captured_frame_count_; |
| 769 | const int posted_frames_waiting_for_encode = |
| 770 | posted_frames_waiting_for_encode_.fetch_sub(1); |
| 771 | RTC_DCHECK_GT(posted_frames_waiting_for_encode, 0); |
| 772 | if (posted_frames_waiting_for_encode == 1) { |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 773 | MaybeEncodeVideoFrame(incoming_frame, post_time_us); |
Sebastian Jansson | 3ab5c40 | 2018-04-05 12:30:50 +0200 | [diff] [blame] | 774 | } else { |
| 775 | // There is a newer frame in flight. Do not encode this frame. |
| 776 | RTC_LOG(LS_VERBOSE) |
| 777 | << "Incoming frame dropped due to that the encoder is blocked."; |
| 778 | ++dropped_frame_count_; |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 779 | encoder_stats_observer_->OnFrameDropped( |
| 780 | VideoStreamEncoderObserver::DropReason::kEncoderQueue); |
Sebastian Jansson | 3ab5c40 | 2018-04-05 12:30:50 +0200 | [diff] [blame] | 781 | } |
| 782 | if (log_stats) { |
| 783 | RTC_LOG(LS_INFO) << "Number of frames: captured " |
| 784 | << captured_frame_count_ |
| 785 | << ", dropped (due to encoder blocked) " |
| 786 | << dropped_frame_count_ << ", interval_ms " |
| 787 | << kFrameLogIntervalMs; |
| 788 | captured_frame_count_ = 0; |
| 789 | dropped_frame_count_ = 0; |
| 790 | } |
| 791 | }); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 794 | void VideoStreamEncoder::OnDiscardedFrame() { |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 795 | encoder_stats_observer_->OnFrameDropped( |
| 796 | VideoStreamEncoderObserver::DropReason::kSource); |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 797 | } |
| 798 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 799 | bool VideoStreamEncoder::EncoderPaused() const { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 800 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
pwestin@webrtc.org | 91563e4 | 2013-04-25 22:20:08 +0000 | [diff] [blame] | 801 | // Pause video if paused by caller or as long as the network is down or the |
| 802 | // pacer queue has grown too large in buffered mode. |
perkj | 57c21f9 | 2016-06-17 07:27:16 -0700 | [diff] [blame] | 803 | // If the pacer queue has grown too large or the network is down, |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 804 | // last_observed_bitrate_bps_ will be 0. |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 805 | return last_observed_bitrate_bps_ == 0; |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 806 | } |
| 807 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 808 | void VideoStreamEncoder::TraceFrameDropStart() { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 809 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 810 | // Start trace event only on the first frame after encoder is paused. |
| 811 | if (!encoder_paused_and_dropped_frame_) { |
| 812 | TRACE_EVENT_ASYNC_BEGIN0("webrtc", "EncoderPaused", this); |
| 813 | } |
| 814 | encoder_paused_and_dropped_frame_ = true; |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 815 | } |
| 816 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 817 | void VideoStreamEncoder::TraceFrameDropEnd() { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 818 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 819 | // End trace event on first frame after encoder resumes, if frame was dropped. |
| 820 | if (encoder_paused_and_dropped_frame_) { |
| 821 | TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this); |
| 822 | } |
| 823 | encoder_paused_and_dropped_frame_ = false; |
| 824 | } |
| 825 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 826 | VideoBitrateAllocation |
| 827 | VideoStreamEncoder::GetBitrateAllocationAndNotifyObserver( |
| 828 | const uint32_t target_bitrate_bps, |
| 829 | uint32_t framerate_fps) { |
| 830 | // Only call allocators if bitrate > 0 (ie, not suspended), otherwise they |
| 831 | // might cap the bitrate to the min bitrate configured. |
| 832 | VideoBitrateAllocation bitrate_allocation; |
| 833 | if (rate_allocator_ && target_bitrate_bps > 0) { |
| 834 | bitrate_allocation = |
| 835 | rate_allocator_->GetAllocation(target_bitrate_bps, framerate_fps); |
| 836 | } |
| 837 | |
| 838 | if (bitrate_observer_ && bitrate_allocation.get_sum_bps() > 0) { |
| 839 | bitrate_observer_->OnBitrateAllocationUpdated(bitrate_allocation); |
| 840 | } |
| 841 | |
| 842 | return bitrate_allocation; |
| 843 | } |
| 844 | |
| 845 | uint32_t VideoStreamEncoder::GetInputFramerateFps() { |
| 846 | const uint32_t default_fps = max_framerate_ != -1 ? max_framerate_ : 30; |
| 847 | return input_framerate_.Rate(clock_->TimeInMilliseconds()) |
| 848 | .value_or(default_fps); |
| 849 | } |
| 850 | |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 851 | void VideoStreamEncoder::MaybeEncodeVideoFrame(const VideoFrame& video_frame, |
| 852 | int64_t time_when_posted_us) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 853 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 854 | |
Per | 21d45d2 | 2016-10-30 21:37:57 +0100 | [diff] [blame] | 855 | if (!last_frame_info_ || video_frame.width() != last_frame_info_->width || |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 856 | video_frame.height() != last_frame_info_->height || |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 857 | video_frame.is_texture() != last_frame_info_->is_texture) { |
| 858 | pending_encoder_reconfiguration_ = true; |
Oskar Sundbom | 8e07c13 | 2018-01-08 16:45:42 +0100 | [diff] [blame] | 859 | last_frame_info_ = VideoFrameInfo(video_frame.width(), video_frame.height(), |
| 860 | video_frame.is_texture()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 861 | RTC_LOG(LS_INFO) << "Video frame parameters changed: dimensions=" |
| 862 | << last_frame_info_->width << "x" |
| 863 | << last_frame_info_->height |
| 864 | << ", texture=" << last_frame_info_->is_texture << "."; |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 867 | // We have to create then encoder before the frame drop logic, |
| 868 | // because the latter depends on encoder_->GetScalingSettings. |
| 869 | // According to the testcase |
| 870 | // InitialFrameDropOffWhenEncoderDisabledScaling, the return value |
| 871 | // from GetScalingSettings should enable or disable the frame drop. |
| 872 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 873 | uint32_t framerate_fps = GetInputFramerateFps(); |
| 874 | |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 875 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 876 | if (pending_encoder_reconfiguration_) { |
| 877 | ReconfigureEncoder(); |
| 878 | last_parameters_update_ms_.emplace(now_ms); |
| 879 | } else if (!last_parameters_update_ms_ || |
| 880 | now_ms - *last_parameters_update_ms_ >= |
| 881 | vcm::VCMProcessTimer::kDefaultProcessIntervalMs) { |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 882 | video_sender_.SetChannelParameters( |
| 883 | GetBitrateAllocationAndNotifyObserver(last_observed_bitrate_bps_, |
| 884 | framerate_fps), |
| 885 | framerate_fps); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 886 | last_parameters_update_ms_.emplace(now_ms); |
| 887 | } |
| 888 | |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 889 | if (DropDueToSize(video_frame.size())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 890 | RTC_LOG(LS_INFO) << "Dropping frame. Too large for target bitrate."; |
Åsa Persson | 875841d | 2018-01-08 08:49:53 +0100 | [diff] [blame] | 891 | int count = GetConstAdaptCounter().ResolutionCount(kQuality); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 892 | AdaptDown(kQuality); |
Åsa Persson | 875841d | 2018-01-08 08:49:53 +0100 | [diff] [blame] | 893 | if (GetConstAdaptCounter().ResolutionCount(kQuality) > count) { |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 894 | encoder_stats_observer_->OnInitialQualityResolutionAdaptDown(); |
Åsa Persson | 875841d | 2018-01-08 08:49:53 +0100 | [diff] [blame] | 895 | } |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 896 | ++initial_framedrop_; |
Sebastian Jansson | 0d70e37 | 2018-04-17 13:57:13 +0200 | [diff] [blame] | 897 | // Storing references to a native buffer risks blocking frame capture. |
| 898 | if (video_frame.video_frame_buffer()->type() != |
| 899 | VideoFrameBuffer::Type::kNative) { |
| 900 | pending_frame_ = video_frame; |
| 901 | pending_frame_post_time_us_ = time_when_posted_us; |
| 902 | } else { |
| 903 | // Ensure that any previously stored frame is dropped. |
| 904 | pending_frame_.reset(); |
| 905 | } |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 906 | return; |
| 907 | } |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 908 | initial_framedrop_ = kMaxInitialFramedrop; |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 909 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 910 | if (EncoderPaused()) { |
Sebastian Jansson | 0d70e37 | 2018-04-17 13:57:13 +0200 | [diff] [blame] | 911 | // Storing references to a native buffer risks blocking frame capture. |
| 912 | if (video_frame.video_frame_buffer()->type() != |
| 913 | VideoFrameBuffer::Type::kNative) { |
| 914 | if (pending_frame_) |
| 915 | TraceFrameDropStart(); |
| 916 | pending_frame_ = video_frame; |
| 917 | pending_frame_post_time_us_ = time_when_posted_us; |
| 918 | } else { |
| 919 | // Ensure that any previously stored frame is dropped. |
| 920 | pending_frame_.reset(); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 921 | TraceFrameDropStart(); |
Sebastian Jansson | 0d70e37 | 2018-04-17 13:57:13 +0200 | [diff] [blame] | 922 | } |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 923 | return; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 924 | } |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 925 | |
| 926 | pending_frame_.reset(); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 927 | |
| 928 | frame_dropper_.Leak(framerate_fps); |
| 929 | // Frame dropping is enabled iff frame dropping is not force-disabled, and |
| 930 | // rate controller is not trusted. |
| 931 | const bool frame_dropping_enabled = |
| 932 | !force_disable_frame_dropper_ && |
| 933 | !encoder_info_.has_trusted_rate_controller; |
| 934 | frame_dropper_.Enable(frame_dropping_enabled); |
| 935 | if (frame_dropping_enabled && frame_dropper_.DropFrame()) { |
| 936 | RTC_LOG(LS_VERBOSE) << "Drop Frame: " |
| 937 | << "target bitrate " << last_observed_bitrate_bps_ |
| 938 | << ", input frame rate " << framerate_fps; |
| 939 | OnDroppedFrame( |
| 940 | EncodedImageCallback::DropReason::kDroppedByMediaOptimizations); |
| 941 | return; |
| 942 | } |
| 943 | |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 944 | EncodeVideoFrame(video_frame, time_when_posted_us); |
| 945 | } |
| 946 | |
| 947 | void VideoStreamEncoder::EncodeVideoFrame(const VideoFrame& video_frame, |
| 948 | int64_t time_when_posted_us) { |
| 949 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 950 | TraceFrameDropEnd(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 951 | |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 952 | VideoFrame out_frame(video_frame); |
| 953 | // Crop frame if needed. |
| 954 | if (crop_width_ > 0 || crop_height_ > 0) { |
| 955 | int cropped_width = video_frame.width() - crop_width_; |
| 956 | int cropped_height = video_frame.height() - crop_height_; |
| 957 | rtc::scoped_refptr<I420Buffer> cropped_buffer = |
| 958 | I420Buffer::Create(cropped_width, cropped_height); |
| 959 | // TODO(ilnik): Remove scaling if cropping is too big, as it should never |
| 960 | // happen after SinkWants signaled correctly from ReconfigureEncoder. |
| 961 | if (crop_width_ < 4 && crop_height_ < 4) { |
| 962 | cropped_buffer->CropAndScaleFrom( |
| 963 | *video_frame.video_frame_buffer()->ToI420(), crop_width_ / 2, |
| 964 | crop_height_ / 2, cropped_width, cropped_height); |
| 965 | } else { |
| 966 | cropped_buffer->ScaleFrom( |
| 967 | *video_frame.video_frame_buffer()->ToI420().get()); |
| 968 | } |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 969 | out_frame = VideoFrame::Builder() |
| 970 | .set_video_frame_buffer(cropped_buffer) |
| 971 | .set_timestamp_rtp(video_frame.timestamp()) |
| 972 | .set_timestamp_ms(video_frame.render_time_ms()) |
| 973 | .set_rotation(video_frame.rotation()) |
| 974 | .set_id(video_frame.id()) |
| 975 | .build(); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 976 | out_frame.set_ntp_time_ms(video_frame.ntp_time_ms()); |
| 977 | } |
| 978 | |
Magnus Jedvert | 26679d6 | 2015-04-07 14:07:41 +0200 | [diff] [blame] | 979 | TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(), |
hclam@chromium.org | 1a7b9b9 | 2013-07-08 21:31:18 +0000 | [diff] [blame] | 980 | "Encode"); |
pbos@webrtc.org | fe1ef93 | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 981 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 982 | overuse_detector_->FrameCaptured(out_frame, time_when_posted_us); |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 983 | |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 984 | // Encoder metadata needs to be updated before encode complete callback. |
| 985 | VideoEncoder::EncoderInfo info = encoder_->GetEncoderInfo(); |
| 986 | if (info.implementation_name != encoder_info_.implementation_name) { |
| 987 | encoder_stats_observer_->OnEncoderImplementationChanged( |
| 988 | info.implementation_name); |
| 989 | } |
| 990 | encoder_info_ = info; |
| 991 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 992 | input_framerate_.Update(1u, clock_->TimeInMilliseconds()); |
Erik Språng | eee3920 | 2018-11-15 17:52:43 +0100 | [diff] [blame] | 993 | video_sender_.AddVideoFrame(out_frame, nullptr, encoder_info_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 994 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 995 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 996 | void VideoStreamEncoder::SendKeyFrame() { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 997 | if (!encoder_queue_.IsCurrent()) { |
| 998 | encoder_queue_.PostTask([this] { SendKeyFrame(); }); |
| 999 | return; |
| 1000 | } |
| 1001 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
Niels Möller | 1c9aa1e | 2018-02-16 10:27:23 +0100 | [diff] [blame] | 1002 | TRACE_EVENT0("webrtc", "OnKeyFrameRequest"); |
Peter Boström | cd5c25c | 2016-04-21 16:48:08 +0200 | [diff] [blame] | 1003 | video_sender_.IntraFrameRequest(0); |
stefan@webrtc.org | 07b45a5 | 2012-02-02 08:37:48 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1006 | EncodedImageCallback::Result VideoStreamEncoder::OnEncodedImage( |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 1007 | const EncodedImage& encoded_image, |
| 1008 | const CodecSpecificInfo* codec_specific_info, |
| 1009 | const RTPFragmentationHeader* fragmentation) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1010 | // Encoded is called on whatever thread the real encoder implementation run |
| 1011 | // on. In the case of hardware encoders, there might be several encoders |
| 1012 | // running in parallel on different threads. |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1013 | encoder_stats_observer_->OnSendEncodedImage(encoded_image, |
| 1014 | codec_specific_info); |
sprang | 3911c26 | 2016-04-15 01:24:14 -0700 | [diff] [blame] | 1015 | |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 1016 | EncodedImageCallback::Result result = |
| 1017 | sink_->OnEncodedImage(encoded_image, codec_specific_info, fragmentation); |
perkj | bc75d97 | 2016-05-02 06:31:25 -0700 | [diff] [blame] | 1018 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 1019 | int64_t time_sent_us = rtc::TimeMicros(); |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 1020 | uint32_t timestamp = encoded_image.Timestamp(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1021 | const int qp = encoded_image.qp_; |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 1022 | int64_t capture_time_us = |
| 1023 | encoded_image.capture_time_ms_ * rtc::kNumMicrosecsPerMillisec; |
| 1024 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 1025 | absl::optional<int> encode_duration_us; |
Ilya Nikolaevskiy | b6c462d | 2018-06-05 15:21:32 +0200 | [diff] [blame] | 1026 | if (encoded_image.timing_.flags != VideoSendTiming::kInvalid) { |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 1027 | encode_duration_us.emplace( |
| 1028 | // TODO(nisse): Maybe use capture_time_ms_ rather than encode_start_ms_? |
| 1029 | rtc::kNumMicrosecsPerMillisec * |
| 1030 | (encoded_image.timing_.encode_finish_ms - |
| 1031 | encoded_image.timing_.encode_start_ms)); |
| 1032 | } |
| 1033 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 1034 | // Run post encode tasks, such as overuse detection and frame rate/drop |
| 1035 | // stats for internal encoders. |
| 1036 | const size_t frame_size = encoded_image.size(); |
| 1037 | const bool keyframe = encoded_image._frameType == FrameType::kVideoFrameKey; |
| 1038 | RunPostEncode(timestamp, time_sent_us, capture_time_us, encode_duration_us, |
| 1039 | qp, frame_size, keyframe); |
| 1040 | |
| 1041 | if (result.error == Result::OK) { |
| 1042 | // In case of an internal encoder running on a separate thread, the |
| 1043 | // decision to drop a frame might be a frame late and signaled via |
| 1044 | // atomic flag. This is because we can't easily wait for the worker thread |
| 1045 | // without risking deadlocks, eg during shutdown when the worker thread |
| 1046 | // might be waiting for the internal encoder threads to stop. |
| 1047 | if (pending_frame_drops_.load() > 0) { |
| 1048 | int pending_drops = pending_frame_drops_.fetch_sub(1); |
| 1049 | RTC_DCHECK_GT(pending_drops, 0); |
| 1050 | result.drop_next_frame = true; |
| 1051 | } |
| 1052 | } |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1053 | |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 1054 | return result; |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 1055 | } |
| 1056 | |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 1057 | void VideoStreamEncoder::OnDroppedFrame(DropReason reason) { |
| 1058 | switch (reason) { |
| 1059 | case DropReason::kDroppedByMediaOptimizations: |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1060 | encoder_stats_observer_->OnFrameDropped( |
| 1061 | VideoStreamEncoderObserver::DropReason::kMediaOptimization); |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 1062 | encoder_queue_.PostTask([this] { |
| 1063 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 1064 | if (quality_scaler_) |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 1065 | quality_scaler_->ReportDroppedFrameByMediaOpt(); |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 1066 | }); |
| 1067 | break; |
| 1068 | case DropReason::kDroppedByEncoder: |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1069 | encoder_stats_observer_->OnFrameDropped( |
| 1070 | VideoStreamEncoderObserver::DropReason::kEncoder); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 1071 | encoder_queue_.PostTask([this] { |
| 1072 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 1073 | if (quality_scaler_) |
| 1074 | quality_scaler_->ReportDroppedFrameByEncoder(); |
| 1075 | }); |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 1076 | break; |
| 1077 | } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1080 | void VideoStreamEncoder::OnBitrateUpdated(uint32_t bitrate_bps, |
| 1081 | uint8_t fraction_lost, |
| 1082 | int64_t round_trip_time_ms) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1083 | if (!encoder_queue_.IsCurrent()) { |
| 1084 | encoder_queue_.PostTask( |
| 1085 | [this, bitrate_bps, fraction_lost, round_trip_time_ms] { |
| 1086 | OnBitrateUpdated(bitrate_bps, fraction_lost, round_trip_time_ms); |
| 1087 | }); |
| 1088 | return; |
| 1089 | } |
| 1090 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 1091 | RTC_DCHECK(sink_) << "sink_ must be set before the encoder is active."; |
| 1092 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1093 | RTC_LOG(LS_VERBOSE) << "OnBitrateUpdated, bitrate " << bitrate_bps |
| 1094 | << " packet loss " << static_cast<int>(fraction_lost) |
| 1095 | << " rtt " << round_trip_time_ms; |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 1096 | // On significant changes to BWE at the start of the call, |
| 1097 | // enable frame drops to quickly react to jumps in available bandwidth. |
| 1098 | if (encoder_start_bitrate_bps_ != 0 && |
| 1099 | !has_seen_first_significant_bwe_change_ && quality_scaler_ && |
| 1100 | initial_framedrop_on_bwe_enabled_ && |
| 1101 | abs_diff(bitrate_bps, encoder_start_bitrate_bps_) >= |
| 1102 | kFramedropThreshold * encoder_start_bitrate_bps_) { |
| 1103 | // Reset initial framedrop feature when first real BW estimate arrives. |
| 1104 | // TODO(kthelgason): Update BitrateAllocator to not call OnBitrateUpdated |
| 1105 | // without an actual BW estimate. |
| 1106 | initial_framedrop_ = 0; |
| 1107 | has_seen_first_significant_bwe_change_ = true; |
| 1108 | } |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1109 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 1110 | uint32_t framerate_fps = GetInputFramerateFps(); |
| 1111 | frame_dropper_.SetRates((bitrate_bps + 500) / 1000, framerate_fps); |
| 1112 | |
| 1113 | VideoBitrateAllocation bitrate_allocation = |
| 1114 | GetBitrateAllocationAndNotifyObserver(bitrate_bps, framerate_fps); |
| 1115 | video_sender_.SetChannelParameters(bitrate_allocation, framerate_fps); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1116 | |
| 1117 | encoder_start_bitrate_bps_ = |
| 1118 | bitrate_bps != 0 ? bitrate_bps : encoder_start_bitrate_bps_; |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 1119 | bool video_is_suspended = bitrate_bps == 0; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 1120 | bool video_suspension_changed = video_is_suspended != EncoderPaused(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1121 | last_observed_bitrate_bps_ = bitrate_bps; |
Peter Boström | d153a37 | 2015-11-10 15:27:12 +0000 | [diff] [blame] | 1122 | |
sprang | 552c7c7 | 2017-02-13 04:41:45 -0800 | [diff] [blame] | 1123 | if (video_suspension_changed) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1124 | RTC_LOG(LS_INFO) << "Video suspend state changed to: " |
| 1125 | << (video_is_suspended ? "suspended" : "not suspended"); |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1126 | encoder_stats_observer_->OnSuspendChange(video_is_suspended); |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 1127 | } |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 1128 | if (video_suspension_changed && !video_is_suspended && pending_frame_ && |
| 1129 | !DropDueToSize(pending_frame_->size())) { |
| 1130 | int64_t pending_time_us = rtc::TimeMicros() - pending_frame_post_time_us_; |
| 1131 | if (pending_time_us < kPendingFrameTimeoutMs * 1000) |
| 1132 | EncodeVideoFrame(*pending_frame_, pending_frame_post_time_us_); |
| 1133 | pending_frame_.reset(); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | bool VideoStreamEncoder::DropDueToSize(uint32_t pixel_count) const { |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 1138 | if (initial_framedrop_ < kMaxInitialFramedrop && |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 1139 | encoder_start_bitrate_bps_ > 0) { |
| 1140 | if (encoder_start_bitrate_bps_ < 300000 /* qvga */) { |
| 1141 | return pixel_count > 320 * 240; |
| 1142 | } else if (encoder_start_bitrate_bps_ < 500000 /* vga */) { |
| 1143 | return pixel_count > 640 * 480; |
| 1144 | } |
| 1145 | } |
| 1146 | return false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1149 | void VideoStreamEncoder::AdaptDown(AdaptReason reason) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 1150 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1151 | AdaptationRequest adaptation_request = { |
| 1152 | last_frame_info_->pixel_count(), |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1153 | encoder_stats_observer_->GetInputFrameRate(), |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1154 | AdaptationRequest::Mode::kAdaptDown}; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1155 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1156 | bool downgrade_requested = |
| 1157 | last_adaptation_request_ && |
| 1158 | last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown; |
| 1159 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1160 | switch (degradation_preference_) { |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1161 | case DegradationPreference::BALANCED: |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1162 | break; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1163 | case DegradationPreference::MAINTAIN_FRAMERATE: |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1164 | if (downgrade_requested && |
| 1165 | adaptation_request.input_pixel_count_ >= |
| 1166 | last_adaptation_request_->input_pixel_count_) { |
| 1167 | // Don't request lower resolution if the current resolution is not |
| 1168 | // lower than the last time we asked for the resolution to be lowered. |
| 1169 | return; |
| 1170 | } |
| 1171 | break; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1172 | case DegradationPreference::MAINTAIN_RESOLUTION: |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1173 | if (adaptation_request.framerate_fps_ <= 0 || |
| 1174 | (downgrade_requested && |
| 1175 | adaptation_request.framerate_fps_ < kMinFramerateFps)) { |
| 1176 | // If no input fps estimate available, can't determine how to scale down |
| 1177 | // framerate. Otherwise, don't request lower framerate if we don't have |
| 1178 | // a valid frame rate. Since framerate, unlike resolution, is a measure |
| 1179 | // we have to estimate, and can fluctuate naturally over time, don't |
| 1180 | // make the same kind of limitations as for resolution, but trust the |
| 1181 | // overuse detector to not trigger too often. |
| 1182 | return; |
| 1183 | } |
| 1184 | break; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1185 | case DegradationPreference::DISABLED: |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1186 | return; |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 1187 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1188 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1189 | switch (degradation_preference_) { |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1190 | case DegradationPreference::BALANCED: { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1191 | // Try scale down framerate, if lower. |
| 1192 | int fps = MinFps(last_frame_info_->pixel_count()); |
| 1193 | if (source_proxy_->RestrictFramerate(fps)) { |
| 1194 | GetAdaptCounter().IncrementFramerate(reason); |
| 1195 | break; |
| 1196 | } |
| 1197 | // Scale down resolution. |
Karl Wiberg | 80ba333 | 2018-02-05 10:33:35 +0100 | [diff] [blame] | 1198 | RTC_FALLTHROUGH(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1199 | } |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1200 | case DegradationPreference::MAINTAIN_FRAMERATE: { |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1201 | // Scale down resolution. |
Åsa Persson | c3ed630 | 2017-11-16 14:04:52 +0100 | [diff] [blame] | 1202 | bool min_pixels_reached = false; |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1203 | if (!source_proxy_->RequestResolutionLowerThan( |
asapersson | 142fcc9 | 2017-08-17 08:58:54 -0700 | [diff] [blame] | 1204 | adaptation_request.input_pixel_count_, |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 1205 | encoder_->GetEncoderInfo().scaling_settings.min_pixels_per_frame, |
Åsa Persson | c3ed630 | 2017-11-16 14:04:52 +0100 | [diff] [blame] | 1206 | &min_pixels_reached)) { |
| 1207 | if (min_pixels_reached) |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1208 | encoder_stats_observer_->OnMinPixelLimitReached(); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1209 | return; |
| 1210 | } |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1211 | GetAdaptCounter().IncrementResolution(reason); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1212 | break; |
Åsa Persson | c3ed630 | 2017-11-16 14:04:52 +0100 | [diff] [blame] | 1213 | } |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1214 | case DegradationPreference::MAINTAIN_RESOLUTION: { |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1215 | // Scale down framerate. |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 1216 | const int requested_framerate = source_proxy_->RequestFramerateLowerThan( |
| 1217 | adaptation_request.framerate_fps_); |
| 1218 | if (requested_framerate == -1) |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1219 | return; |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 1220 | RTC_DCHECK_NE(max_framerate_, -1); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 1221 | overuse_detector_->OnTargetFramerateUpdated( |
| 1222 | std::min(max_framerate_, requested_framerate)); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1223 | GetAdaptCounter().IncrementFramerate(reason); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1224 | break; |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 1225 | } |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1226 | case DegradationPreference::DISABLED: |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1227 | RTC_NOTREACHED(); |
| 1228 | } |
| 1229 | |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1230 | last_adaptation_request_.emplace(adaptation_request); |
| 1231 | |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1232 | UpdateAdaptationStats(reason); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1233 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1234 | RTC_LOG(LS_INFO) << GetConstAdaptCounter().ToString(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1237 | void VideoStreamEncoder::AdaptUp(AdaptReason reason) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 1238 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1239 | |
| 1240 | const AdaptCounter& adapt_counter = GetConstAdaptCounter(); |
| 1241 | int num_downgrades = adapt_counter.TotalCount(reason); |
| 1242 | if (num_downgrades == 0) |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1243 | return; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1244 | RTC_DCHECK_GT(num_downgrades, 0); |
| 1245 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1246 | AdaptationRequest adaptation_request = { |
| 1247 | last_frame_info_->pixel_count(), |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1248 | encoder_stats_observer_->GetInputFrameRate(), |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1249 | AdaptationRequest::Mode::kAdaptUp}; |
| 1250 | |
| 1251 | bool adapt_up_requested = |
| 1252 | last_adaptation_request_ && |
| 1253 | last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptUp; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1254 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1255 | if (degradation_preference_ == DegradationPreference::MAINTAIN_FRAMERATE) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1256 | if (adapt_up_requested && |
| 1257 | adaptation_request.input_pixel_count_ <= |
| 1258 | last_adaptation_request_->input_pixel_count_) { |
| 1259 | // Don't request higher resolution if the current resolution is not |
| 1260 | // higher than the last time we asked for the resolution to be higher. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1261 | return; |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1262 | } |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 1263 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1264 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1265 | switch (degradation_preference_) { |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1266 | case DegradationPreference::BALANCED: { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1267 | // Try scale up framerate, if higher. |
| 1268 | int fps = MaxFps(last_frame_info_->pixel_count()); |
| 1269 | if (source_proxy_->IncreaseFramerate(fps)) { |
| 1270 | GetAdaptCounter().DecrementFramerate(reason, fps); |
| 1271 | // Reset framerate in case of fewer fps steps down than up. |
| 1272 | if (adapt_counter.FramerateCount() == 0 && |
| 1273 | fps != std::numeric_limits<int>::max()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1274 | RTC_LOG(LS_INFO) << "Removing framerate down-scaling setting."; |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1275 | source_proxy_->IncreaseFramerate(std::numeric_limits<int>::max()); |
| 1276 | } |
| 1277 | break; |
| 1278 | } |
| 1279 | // Scale up resolution. |
Karl Wiberg | 80ba333 | 2018-02-05 10:33:35 +0100 | [diff] [blame] | 1280 | RTC_FALLTHROUGH(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1281 | } |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1282 | case DegradationPreference::MAINTAIN_FRAMERATE: { |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1283 | // Scale up resolution. |
| 1284 | int pixel_count = adaptation_request.input_pixel_count_; |
| 1285 | if (adapt_counter.ResolutionCount() == 1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1286 | RTC_LOG(LS_INFO) << "Removing resolution down-scaling setting."; |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1287 | pixel_count = std::numeric_limits<int>::max(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1288 | } |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1289 | if (!source_proxy_->RequestHigherResolutionThan(pixel_count)) |
| 1290 | return; |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1291 | GetAdaptCounter().DecrementResolution(reason); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1292 | break; |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1293 | } |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1294 | case DegradationPreference::MAINTAIN_RESOLUTION: { |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1295 | // Scale up framerate. |
| 1296 | int fps = adaptation_request.framerate_fps_; |
| 1297 | if (adapt_counter.FramerateCount() == 1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1298 | RTC_LOG(LS_INFO) << "Removing framerate down-scaling setting."; |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1299 | fps = std::numeric_limits<int>::max(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1300 | } |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 1301 | |
| 1302 | const int requested_framerate = |
| 1303 | source_proxy_->RequestHigherFramerateThan(fps); |
| 1304 | if (requested_framerate == -1) { |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 1305 | overuse_detector_->OnTargetFramerateUpdated(max_framerate_); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1306 | return; |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 1307 | } |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 1308 | overuse_detector_->OnTargetFramerateUpdated( |
| 1309 | std::min(max_framerate_, requested_framerate)); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1310 | GetAdaptCounter().DecrementFramerate(reason); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1311 | break; |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1312 | } |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1313 | case DegradationPreference::DISABLED: |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1314 | return; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1317 | last_adaptation_request_.emplace(adaptation_request); |
| 1318 | |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1319 | UpdateAdaptationStats(reason); |
| 1320 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1321 | RTC_LOG(LS_INFO) << adapt_counter.ToString(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1324 | // TODO(nisse): Delete, once AdaptReason and AdaptationReason are merged. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1325 | void VideoStreamEncoder::UpdateAdaptationStats(AdaptReason reason) { |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1326 | switch (reason) { |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1327 | case kCpu: |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1328 | encoder_stats_observer_->OnAdaptationChanged( |
| 1329 | VideoStreamEncoderObserver::AdaptationReason::kCpu, |
| 1330 | GetActiveCounts(kCpu), GetActiveCounts(kQuality)); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1331 | break; |
| 1332 | case kQuality: |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1333 | encoder_stats_observer_->OnAdaptationChanged( |
| 1334 | VideoStreamEncoderObserver::AdaptationReason::kQuality, |
| 1335 | GetActiveCounts(kCpu), GetActiveCounts(kQuality)); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1336 | break; |
| 1337 | } |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1338 | } |
| 1339 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1340 | VideoStreamEncoderObserver::AdaptationSteps VideoStreamEncoder::GetActiveCounts( |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1341 | AdaptReason reason) { |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1342 | VideoStreamEncoderObserver::AdaptationSteps counts = |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1343 | GetConstAdaptCounter().Counts(reason); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1344 | switch (reason) { |
| 1345 | case kCpu: |
| 1346 | if (!IsFramerateScalingEnabled(degradation_preference_)) |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1347 | counts.num_framerate_reductions = absl::nullopt; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1348 | if (!IsResolutionScalingEnabled(degradation_preference_)) |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1349 | counts.num_resolution_reductions = absl::nullopt; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1350 | break; |
| 1351 | case kQuality: |
| 1352 | if (!IsFramerateScalingEnabled(degradation_preference_) || |
| 1353 | !quality_scaler_) { |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1354 | counts.num_framerate_reductions = absl::nullopt; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1355 | } |
| 1356 | if (!IsResolutionScalingEnabled(degradation_preference_) || |
| 1357 | !quality_scaler_) { |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1358 | counts.num_resolution_reductions = absl::nullopt; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1359 | } |
| 1360 | break; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1361 | } |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1362 | return counts; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1365 | VideoStreamEncoder::AdaptCounter& VideoStreamEncoder::GetAdaptCounter() { |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1366 | return adapt_counters_[degradation_preference_]; |
| 1367 | } |
| 1368 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1369 | const VideoStreamEncoder::AdaptCounter& |
| 1370 | VideoStreamEncoder::GetConstAdaptCounter() { |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1371 | return adapt_counters_[degradation_preference_]; |
| 1372 | } |
| 1373 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 1374 | void VideoStreamEncoder::RunPostEncode(uint32_t frame_timestamp, |
| 1375 | int64_t time_sent_us, |
| 1376 | int64_t capture_time_us, |
| 1377 | absl::optional<int> encode_durations_us, |
| 1378 | int qp, |
| 1379 | size_t frame_size_bytes, |
| 1380 | bool keyframe) { |
| 1381 | if (!encoder_queue_.IsCurrent()) { |
| 1382 | encoder_queue_.PostTask([this, frame_timestamp, time_sent_us, qp, |
| 1383 | capture_time_us, encode_durations_us, |
| 1384 | frame_size_bytes, keyframe] { |
| 1385 | RunPostEncode(frame_timestamp, time_sent_us, capture_time_us, |
| 1386 | encode_durations_us, qp, frame_size_bytes, keyframe); |
| 1387 | }); |
| 1388 | return; |
| 1389 | } |
| 1390 | |
| 1391 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 1392 | if (frame_size_bytes > 0) { |
| 1393 | frame_dropper_.Fill(frame_size_bytes, !keyframe); |
| 1394 | } |
| 1395 | |
| 1396 | if (encoder_info_.has_internal_source) { |
| 1397 | // Update frame dropper after the fact for internal sources. |
| 1398 | input_framerate_.Update(1u, clock_->TimeInMilliseconds()); |
| 1399 | frame_dropper_.Leak(GetInputFramerateFps()); |
| 1400 | // Signal to encoder to drop next frame. |
| 1401 | if (frame_dropper_.DropFrame()) { |
| 1402 | pending_frame_drops_.fetch_add(1); |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | overuse_detector_->FrameSent(frame_timestamp, time_sent_us, capture_time_us, |
| 1407 | encode_durations_us); |
| 1408 | if (quality_scaler_ && qp >= 0) |
| 1409 | quality_scaler_->ReportQp(qp); |
| 1410 | } |
| 1411 | |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1412 | // Class holding adaptation information. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1413 | VideoStreamEncoder::AdaptCounter::AdaptCounter() { |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1414 | fps_counters_.resize(kScaleReasonSize); |
| 1415 | resolution_counters_.resize(kScaleReasonSize); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1416 | static_assert(kScaleReasonSize == 2, "Update MoveCount."); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1419 | VideoStreamEncoder::AdaptCounter::~AdaptCounter() {} |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1420 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1421 | std::string VideoStreamEncoder::AdaptCounter::ToString() const { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 1422 | rtc::StringBuilder ss; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1423 | ss << "Downgrade counts: fps: {" << ToString(fps_counters_); |
| 1424 | ss << "}, resolution: {" << ToString(resolution_counters_) << "}"; |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 1425 | return ss.Release(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 1428 | VideoStreamEncoderObserver::AdaptationSteps |
| 1429 | VideoStreamEncoder::AdaptCounter::Counts(int reason) const { |
| 1430 | VideoStreamEncoderObserver::AdaptationSteps counts; |
| 1431 | counts.num_framerate_reductions = fps_counters_[reason]; |
| 1432 | counts.num_resolution_reductions = resolution_counters_[reason]; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1433 | return counts; |
| 1434 | } |
| 1435 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1436 | void VideoStreamEncoder::AdaptCounter::IncrementFramerate(int reason) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1437 | ++(fps_counters_[reason]); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1440 | void VideoStreamEncoder::AdaptCounter::IncrementResolution(int reason) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1441 | ++(resolution_counters_[reason]); |
| 1442 | } |
| 1443 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1444 | void VideoStreamEncoder::AdaptCounter::DecrementFramerate(int reason) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1445 | if (fps_counters_[reason] == 0) { |
| 1446 | // Balanced mode: Adapt up is in a different order, switch reason. |
| 1447 | // E.g. framerate adapt down: quality (2), framerate adapt up: cpu (3). |
| 1448 | // 1. Down resolution (cpu): res={quality:0,cpu:1}, fps={quality:0,cpu:0} |
| 1449 | // 2. Down fps (quality): res={quality:0,cpu:1}, fps={quality:1,cpu:0} |
| 1450 | // 3. Up fps (cpu): res={quality:1,cpu:0}, fps={quality:0,cpu:0} |
| 1451 | // 4. Up resolution (quality): res={quality:0,cpu:0}, fps={quality:0,cpu:0} |
| 1452 | RTC_DCHECK_GT(TotalCount(reason), 0) << "No downgrade for reason."; |
| 1453 | RTC_DCHECK_GT(FramerateCount(), 0) << "Framerate not downgraded."; |
| 1454 | MoveCount(&resolution_counters_, reason); |
| 1455 | MoveCount(&fps_counters_, (reason + 1) % kScaleReasonSize); |
| 1456 | } |
| 1457 | --(fps_counters_[reason]); |
| 1458 | RTC_DCHECK_GE(fps_counters_[reason], 0); |
| 1459 | } |
| 1460 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1461 | void VideoStreamEncoder::AdaptCounter::DecrementResolution(int reason) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1462 | if (resolution_counters_[reason] == 0) { |
| 1463 | // Balanced mode: Adapt up is in a different order, switch reason. |
| 1464 | RTC_DCHECK_GT(TotalCount(reason), 0) << "No downgrade for reason."; |
| 1465 | RTC_DCHECK_GT(ResolutionCount(), 0) << "Resolution not downgraded."; |
| 1466 | MoveCount(&fps_counters_, reason); |
| 1467 | MoveCount(&resolution_counters_, (reason + 1) % kScaleReasonSize); |
| 1468 | } |
| 1469 | --(resolution_counters_[reason]); |
| 1470 | RTC_DCHECK_GE(resolution_counters_[reason], 0); |
| 1471 | } |
| 1472 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1473 | void VideoStreamEncoder::AdaptCounter::DecrementFramerate(int reason, |
| 1474 | int cur_fps) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1475 | DecrementFramerate(reason); |
| 1476 | // Reset if at max fps (i.e. in case of fewer steps up than down). |
| 1477 | if (cur_fps == std::numeric_limits<int>::max()) |
| 1478 | std::fill(fps_counters_.begin(), fps_counters_.end(), 0); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1479 | } |
| 1480 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1481 | int VideoStreamEncoder::AdaptCounter::FramerateCount() const { |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1482 | return Count(fps_counters_); |
| 1483 | } |
| 1484 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1485 | int VideoStreamEncoder::AdaptCounter::ResolutionCount() const { |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1486 | return Count(resolution_counters_); |
| 1487 | } |
| 1488 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1489 | int VideoStreamEncoder::AdaptCounter::FramerateCount(int reason) const { |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1490 | return fps_counters_[reason]; |
| 1491 | } |
| 1492 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1493 | int VideoStreamEncoder::AdaptCounter::ResolutionCount(int reason) const { |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1494 | return resolution_counters_[reason]; |
| 1495 | } |
| 1496 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1497 | int VideoStreamEncoder::AdaptCounter::TotalCount(int reason) const { |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1498 | return FramerateCount(reason) + ResolutionCount(reason); |
| 1499 | } |
| 1500 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1501 | int VideoStreamEncoder::AdaptCounter::Count( |
| 1502 | const std::vector<int>& counters) const { |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1503 | return std::accumulate(counters.begin(), counters.end(), 0); |
| 1504 | } |
| 1505 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1506 | void VideoStreamEncoder::AdaptCounter::MoveCount(std::vector<int>* counters, |
| 1507 | int from_reason) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1508 | int to_reason = (from_reason + 1) % kScaleReasonSize; |
| 1509 | ++((*counters)[to_reason]); |
| 1510 | --((*counters)[from_reason]); |
| 1511 | } |
| 1512 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1513 | std::string VideoStreamEncoder::AdaptCounter::ToString( |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1514 | const std::vector<int>& counters) const { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 1515 | rtc::StringBuilder ss; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1516 | for (size_t reason = 0; reason < kScaleReasonSize; ++reason) { |
| 1517 | ss << (reason ? " cpu" : "quality") << ":" << counters[reason]; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1518 | } |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 1519 | return ss.Release(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 1522 | } // namespace webrtc |