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