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