OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 using DegradationPreference = VideoSendStream::DegradationPreference; | 34 using DegradationPreference = VideoSendStream::DegradationPreference; |
35 | 35 |
36 // Time interval for logging frame counts. | 36 // Time interval for logging frame counts. |
37 const int64_t kFrameLogIntervalMs = 60000; | 37 const int64_t kFrameLogIntervalMs = 60000; |
38 | 38 |
39 // We will never ask for a resolution lower than this. | 39 // We will never ask for a resolution lower than this. |
40 // TODO(kthelgason): Lower this limit when better testing | 40 // TODO(kthelgason): Lower this limit when better testing |
41 // on MediaCodec and fallback implementations are in place. | 41 // on MediaCodec and fallback implementations are in place. |
42 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7206 | 42 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7206 |
43 const int kMinPixelsPerFrame = 320 * 180; | 43 const int kMinPixelsPerFrame = 320 * 180; |
| 44 const int kMinFramerateFps = 2; |
44 | 45 |
45 // The maximum number of frames to drop at beginning of stream | 46 // The maximum number of frames to drop at beginning of stream |
46 // to try and achieve desired bitrate. | 47 // to try and achieve desired bitrate. |
47 const int kMaxInitialFramedrop = 4; | 48 const int kMaxInitialFramedrop = 4; |
48 | 49 |
49 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle | 50 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle |
50 // pipelining encoders better (multiple input frames before something comes | 51 // pipelining encoders better (multiple input frames before something comes |
51 // out). This should effectively turn off CPU adaptations for systems that | 52 // out). This should effectively turn off CPU adaptations for systems that |
52 // remotely cope with the load right now. | 53 // remotely cope with the load right now. |
53 CpuOveruseOptions GetCpuOveruseOptions(bool full_overuse_time) { | 54 CpuOveruseOptions GetCpuOveruseOptions(bool full_overuse_time) { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 source_(nullptr) {} | 154 source_(nullptr) {} |
154 | 155 |
155 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, | 156 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, |
156 const DegradationPreference& degradation_preference) { | 157 const DegradationPreference& degradation_preference) { |
157 // Called on libjingle's worker thread. | 158 // Called on libjingle's worker thread. |
158 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); | 159 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); |
159 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; | 160 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; |
160 rtc::VideoSinkWants wants; | 161 rtc::VideoSinkWants wants; |
161 { | 162 { |
162 rtc::CritScope lock(&crit_); | 163 rtc::CritScope lock(&crit_); |
| 164 wants = sink_wants_; |
| 165 // If changing degradation preference, clear any constraints from the |
| 166 // current sink wants that will no longer apply. |
| 167 if (degradation_preference_ != degradation_preference) { |
| 168 switch (degradation_preference) { |
| 169 case DegradationPreference::kBalanced: |
| 170 wants.max_framerate_fps_.reset(); |
| 171 break; |
| 172 case DegradationPreference::kMaintainResolution: |
| 173 wants.max_pixel_count.reset(); |
| 174 wants.target_pixel_count.reset(); |
| 175 break; |
| 176 } |
| 177 } |
| 178 degradation_preference_ = degradation_preference; |
163 old_source = source_; | 179 old_source = source_; |
164 source_ = source; | 180 source_ = source; |
165 degradation_preference_ = degradation_preference; | |
166 wants = current_wants(); | |
167 } | 181 } |
168 | 182 |
169 if (old_source != source && old_source != nullptr) { | 183 if (old_source != source && old_source != nullptr) { |
170 old_source->RemoveSink(vie_encoder_); | 184 old_source->RemoveSink(vie_encoder_); |
171 } | 185 } |
172 | 186 |
173 if (!source) { | 187 if (!source) { |
174 return; | 188 return; |
175 } | 189 } |
176 | 190 |
177 source->AddOrUpdateSink(vie_encoder_, wants); | 191 source->AddOrUpdateSink(vie_encoder_, wants); |
178 } | 192 } |
179 | 193 |
180 void SetWantsRotationApplied(bool rotation_applied) { | 194 void SetWantsRotationApplied(bool rotation_applied) { |
181 rtc::CritScope lock(&crit_); | 195 rtc::CritScope lock(&crit_); |
182 sink_wants_.rotation_applied = rotation_applied; | 196 sink_wants_.rotation_applied = rotation_applied; |
183 disabled_scaling_sink_wants_.rotation_applied = rotation_applied; | 197 if (source_) |
184 if (source_) { | 198 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); |
185 source_->AddOrUpdateSink(vie_encoder_, current_wants()); | |
186 } | |
187 } | 199 } |
188 | 200 |
189 void RequestResolutionLowerThan(int pixel_count) { | 201 void RequestResolutionLowerThan(int pixel_count) { |
190 // Called on the encoder task queue. | 202 // Called on the encoder task queue. |
191 rtc::CritScope lock(&crit_); | 203 rtc::CritScope lock(&crit_); |
192 if (!IsResolutionScalingEnabledLocked()) { | 204 if (!IsResolutionScalingEnabledLocked()) { |
193 // This can happen since |degradation_preference_| is set on | 205 // This can happen since |degradation_preference_| is set on |
194 // libjingle's worker thread but the adaptation is done on the encoder | 206 // libjingle's worker thread but the adaptation is done on the encoder |
195 // task queue. | 207 // task queue. |
196 return; | 208 return; |
197 } | 209 } |
198 // The input video frame size will have a resolution with less than or | 210 // The input video frame size will have a resolution with less than or |
199 // equal to |max_pixel_count| depending on how the source can scale the | 211 // equal to |max_pixel_count| depending on how the source can scale the |
200 // input frame size. | 212 // input frame size. |
201 const int pixels_wanted = (pixel_count * 3) / 5; | 213 const int pixels_wanted = (pixel_count * 3) / 5; |
202 if (pixels_wanted < kMinPixelsPerFrame) | 214 if (pixels_wanted < kMinPixelsPerFrame) |
203 return; | 215 return; |
204 sink_wants_.max_pixel_count = rtc::Optional<int>(pixels_wanted); | 216 sink_wants_.max_pixel_count = rtc::Optional<int>(pixels_wanted); |
205 sink_wants_.target_pixel_count = rtc::Optional<int>(); | 217 sink_wants_.target_pixel_count = rtc::Optional<int>(); |
206 if (source_) | 218 if (source_) |
207 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | 219 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); |
208 } | 220 } |
209 | 221 |
| 222 void RequestFramerateLowerThan(int framerate_fps) { |
| 223 // Called on the encoder task queue. |
| 224 rtc::CritScope lock(&crit_); |
| 225 if (!IsFramerateScalingEnabledLocked()) { |
| 226 // This can happen since |degradation_preference_| is set on |
| 227 // libjingle's worker thread but the adaptation is done on the encoder |
| 228 // task queue. |
| 229 return; |
| 230 } |
| 231 // The input video frame rate will be scaled down to 2/3 of input fps, |
| 232 // rounding down. |
| 233 const int framerate_wanted = (framerate_fps * 2) / 3; |
| 234 if (framerate_wanted < kMinFramerateFps) |
| 235 return; |
| 236 sink_wants_.max_framerate_fps_.emplace(framerate_wanted); |
| 237 if (source_) |
| 238 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); |
| 239 } |
| 240 |
210 void RequestHigherResolutionThan(int pixel_count) { | 241 void RequestHigherResolutionThan(int pixel_count) { |
211 rtc::CritScope lock(&crit_); | 242 rtc::CritScope lock(&crit_); |
212 if (!IsResolutionScalingEnabledLocked()) { | 243 if (!IsResolutionScalingEnabledLocked()) { |
213 // This can happen since |degradation_preference_| is set on | 244 // This can happen since |degradation_preference_| is set on |
214 // libjingle's worker thread but the adaptation is done on the encoder | 245 // libjingle's worker thread but the adaptation is done on the encoder |
215 // task queue. | 246 // task queue. |
216 return; | 247 return; |
217 } | 248 } |
218 // On step down we request at most 3/5 the pixel count of the previous | 249 // On step down we request at most 3/5 the pixel count of the previous |
219 // resolution, so in order to take "one step up" we request a resolution as | 250 // resolution, so in order to take "one step up" we request a resolution as |
220 // close as possible to 5/3 of the current resolution. The actual pixel | 251 // close as possible to 5/3 of the current resolution. The actual pixel |
221 // count selected depends on the capabilities of the source. In order to not | 252 // count selected depends on the capabilities of the source. In order to not |
222 // take a too large step up, we cap the requested pixel count to be at most | 253 // take a too large step up, we cap the requested pixel count to be at most |
223 // four time the current number of pixels. | 254 // four time the current number of pixels. |
224 sink_wants_.target_pixel_count = rtc::Optional<int>((pixel_count * 5) / 3); | 255 sink_wants_.target_pixel_count = rtc::Optional<int>((pixel_count * 5) / 3); |
225 sink_wants_.max_pixel_count = rtc::Optional<int>(pixel_count * 4); | 256 sink_wants_.max_pixel_count = rtc::Optional<int>(pixel_count * 4); |
226 if (source_) | 257 if (source_) |
227 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | 258 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); |
228 } | 259 } |
229 | 260 |
| 261 void RequestHigherFramerateThan(int framerate_fps) { |
| 262 // Called on the encoder task queue. |
| 263 rtc::CritScope lock(&crit_); |
| 264 if (!IsFramerateScalingEnabledLocked()) { |
| 265 // This can happen since |degradation_preference_| is set on |
| 266 // libjingle's worker thread but the adaptation is done on the encoder |
| 267 // task queue. |
| 268 return; |
| 269 } |
| 270 // The input video frame rate will be scaled up to the last step, with |
| 271 // rounding. |
| 272 const int framerate_wanted = (framerate_fps * 3) / 2; |
| 273 sink_wants_.max_framerate_fps_.emplace(framerate_wanted); |
| 274 if (source_) |
| 275 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); |
| 276 } |
| 277 |
230 private: | 278 private: |
231 bool IsResolutionScalingEnabledLocked() const | 279 bool IsResolutionScalingEnabledLocked() const |
232 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | 280 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { |
233 return degradation_preference_ != | 281 return degradation_preference_ != |
234 DegradationPreference::kMaintainResolution; | 282 DegradationPreference::kMaintainResolution; |
235 } | 283 } |
236 | 284 |
237 const rtc::VideoSinkWants& current_wants() const | 285 bool IsFramerateScalingEnabledLocked() const |
238 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | 286 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { |
239 return IsResolutionScalingEnabledLocked() ? sink_wants_ | 287 return degradation_preference_ == |
240 : disabled_scaling_sink_wants_; | 288 DegradationPreference::kMaintainResolution; |
241 } | 289 } |
242 | 290 |
243 rtc::CriticalSection crit_; | 291 rtc::CriticalSection crit_; |
244 rtc::SequencedTaskChecker main_checker_; | 292 rtc::SequencedTaskChecker main_checker_; |
245 ViEEncoder* const vie_encoder_; | 293 ViEEncoder* const vie_encoder_; |
246 rtc::VideoSinkWants sink_wants_ GUARDED_BY(&crit_); | 294 rtc::VideoSinkWants sink_wants_ GUARDED_BY(&crit_); |
247 rtc::VideoSinkWants disabled_scaling_sink_wants_ GUARDED_BY(&crit_); | |
248 DegradationPreference degradation_preference_ GUARDED_BY(&crit_); | 295 DegradationPreference degradation_preference_ GUARDED_BY(&crit_); |
249 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); | 296 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); |
250 | 297 |
251 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); | 298 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); |
252 }; | 299 }; |
253 | 300 |
254 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 301 ViEEncoder::ViEEncoder(uint32_t number_of_cores, |
255 SendStatisticsProxy* stats_proxy, | 302 SendStatisticsProxy* stats_proxy, |
256 const VideoSendStream::Config::EncoderSettings& settings, | 303 const VideoSendStream::Config::EncoderSettings& settings, |
257 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 304 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
(...skipping 18 matching lines...) Expand all Loading... |
276 encoder_start_bitrate_bps_(0), | 323 encoder_start_bitrate_bps_(0), |
277 max_data_payload_length_(0), | 324 max_data_payload_length_(0), |
278 nack_enabled_(false), | 325 nack_enabled_(false), |
279 last_observed_bitrate_bps_(0), | 326 last_observed_bitrate_bps_(0), |
280 encoder_paused_and_dropped_frame_(false), | 327 encoder_paused_and_dropped_frame_(false), |
281 has_received_sli_(false), | 328 has_received_sli_(false), |
282 picture_id_sli_(0), | 329 picture_id_sli_(0), |
283 has_received_rpsi_(false), | 330 has_received_rpsi_(false), |
284 picture_id_rpsi_(0), | 331 picture_id_rpsi_(0), |
285 clock_(Clock::GetRealTimeClock()), | 332 clock_(Clock::GetRealTimeClock()), |
286 scale_counter_(kScaleReasonSize, 0), | 333 scale_counters_{ |
| 334 {VideoSendStream::DegradationPreference::kMaintainResolution, |
| 335 std::vector<int>(kScaleReasonSize, 0)}, |
| 336 {VideoSendStream::DegradationPreference::kBalanced, |
| 337 std::vector<int>(kScaleReasonSize, 0)}}, |
287 degradation_preference_(DegradationPreference::kMaintainResolution), | 338 degradation_preference_(DegradationPreference::kMaintainResolution), |
288 last_captured_timestamp_(0), | 339 last_captured_timestamp_(0), |
289 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - | 340 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - |
290 clock_->TimeInMilliseconds()), | 341 clock_->TimeInMilliseconds()), |
291 last_frame_log_ms_(clock_->TimeInMilliseconds()), | 342 last_frame_log_ms_(clock_->TimeInMilliseconds()), |
292 captured_frame_count_(0), | 343 captured_frame_count_(0), |
293 dropped_frame_count_(0), | 344 dropped_frame_count_(0), |
294 bitrate_observer_(nullptr), | 345 bitrate_observer_(nullptr), |
295 encoder_queue_("EncoderQueue") { | 346 encoder_queue_("EncoderQueue") { |
296 RTC_DCHECK(stats_proxy); | 347 RTC_DCHECK(stats_proxy); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 if (scaling_settings.thresholds) { | 518 if (scaling_settings.thresholds) { |
468 quality_scaler_.reset( | 519 quality_scaler_.reset( |
469 new QualityScaler(this, *(scaling_settings.thresholds))); | 520 new QualityScaler(this, *(scaling_settings.thresholds))); |
470 } else { | 521 } else { |
471 quality_scaler_.reset(new QualityScaler(this, codec_type_)); | 522 quality_scaler_.reset(new QualityScaler(this, codec_type_)); |
472 } | 523 } |
473 } else { | 524 } else { |
474 quality_scaler_.reset(nullptr); | 525 quality_scaler_.reset(nullptr); |
475 initial_rampup_ = kMaxInitialFramedrop; | 526 initial_rampup_ = kMaxInitialFramedrop; |
476 } | 527 } |
| 528 const std::vector<int>& scale_counter = *GetScaleCounters(); |
477 stats_proxy_->SetResolutionRestrictionStats( | 529 stats_proxy_->SetResolutionRestrictionStats( |
478 degradation_preference_allows_scaling, scale_counter_[kCpu] > 0, | 530 degradation_preference_allows_scaling, scale_counter[kCpu] > 0, |
479 scale_counter_[kQuality]); | 531 scale_counter[kQuality]); |
480 } | 532 } |
481 | 533 |
482 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { | 534 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { |
483 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); | 535 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); |
484 VideoFrame incoming_frame = video_frame; | 536 VideoFrame incoming_frame = video_frame; |
485 | 537 |
486 // Local time in webrtc time base. | 538 // Local time in webrtc time base. |
487 int64_t current_time_us = clock_->TimeInMicroseconds(); | 539 int64_t current_time_us = clock_->TimeInMicroseconds(); |
488 int64_t current_time_ms = current_time_us / rtc::kNumMicrosecsPerMillisec; | 540 int64_t current_time_ms = current_time_us / rtc::kNumMicrosecsPerMillisec; |
489 // TODO(nisse): This always overrides the incoming timestamp. Don't | 541 // TODO(nisse): This always overrides the incoming timestamp. Don't |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 | 784 |
733 if (video_suspension_changed) { | 785 if (video_suspension_changed) { |
734 LOG(LS_INFO) << "Video suspend state changed to: " | 786 LOG(LS_INFO) << "Video suspend state changed to: " |
735 << (video_is_suspended ? "suspended" : "not suspended"); | 787 << (video_is_suspended ? "suspended" : "not suspended"); |
736 stats_proxy_->OnSuspendChange(video_is_suspended); | 788 stats_proxy_->OnSuspendChange(video_is_suspended); |
737 } | 789 } |
738 } | 790 } |
739 | 791 |
740 void ViEEncoder::AdaptDown(AdaptReason reason) { | 792 void ViEEncoder::AdaptDown(AdaptReason reason) { |
741 RTC_DCHECK_RUN_ON(&encoder_queue_); | 793 RTC_DCHECK_RUN_ON(&encoder_queue_); |
742 if (degradation_preference_ != DegradationPreference::kBalanced) | 794 AdaptationRequest adaptation_request = { |
743 return; | 795 last_frame_info_->pixel_count(), |
744 RTC_DCHECK(static_cast<bool>(last_frame_info_)); | 796 stats_proxy_->GetStats().input_frame_rate, |
745 int current_pixel_count = last_frame_info_->pixel_count(); | 797 AdaptationRequest::Mode::kAdaptDown}; |
746 if (last_adaptation_request_ && | 798 |
747 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown && | 799 if (degradation_preference_ == DegradationPreference::kMaintainResolution && |
748 current_pixel_count >= last_adaptation_request_->input_pixel_count_) { | 800 adaptation_request.framerate_fps_ <= 0) { |
749 // Don't request lower resolution if the current resolution is not lower | 801 // No input fps estimate available, can't determine how to scale down fps. |
750 // than the last time we asked for the resolution to be lowered. | |
751 return; | 802 return; |
752 } | 803 } |
753 last_adaptation_request_.emplace(AdaptationRequest{ | 804 |
754 current_pixel_count, AdaptationRequest::Mode::kAdaptDown}); | 805 if (last_adaptation_request_ && |
| 806 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown) { |
| 807 switch (degradation_preference_) { |
| 808 case DegradationPreference::kBalanced: |
| 809 if (adaptation_request.input_pixel_count_ >= |
| 810 last_adaptation_request_->input_pixel_count_) { |
| 811 // Don't request lower resolution if the current resolution is not |
| 812 // lower than the last time we asked for the resolution to be lowered. |
| 813 return; |
| 814 } |
| 815 break; |
| 816 case DegradationPreference::kMaintainResolution: |
| 817 if (adaptation_request.framerate_fps_ < kMinFramerateFps) { |
| 818 // Don't request lower framerate if we don't have a valid frame rate. |
| 819 // Since framerate, unlike resolution, is a measure we have to |
| 820 // estimate, and can fluctuate naturally over time, don't make the |
| 821 // same kind of limitations as for resolution, but trust the overuse |
| 822 // detector to not trigger too often. |
| 823 return; |
| 824 } |
| 825 break; |
| 826 } |
| 827 } |
| 828 |
| 829 last_adaptation_request_.emplace(adaptation_request); |
| 830 std::vector<int>* scale_counter = GetScaleCounters(); |
755 | 831 |
756 switch (reason) { | 832 switch (reason) { |
757 case kQuality: | 833 case kQuality: |
758 stats_proxy_->OnQualityRestrictedResolutionChanged( | 834 stats_proxy_->OnQualityRestrictedResolutionChanged( |
759 scale_counter_[reason] + 1); | 835 (*scale_counter)[reason] + 1); |
760 break; | 836 break; |
761 case kCpu: | 837 case kCpu: |
762 if (scale_counter_[reason] >= kMaxCpuDowngrades) | 838 if ((*scale_counter)[reason] >= kMaxCpuDowngrades) |
763 return; | 839 return; |
764 // Update stats accordingly. | 840 // Update stats accordingly. |
765 stats_proxy_->OnCpuRestrictedResolutionChanged(true); | 841 stats_proxy_->OnCpuRestrictedResolutionChanged(true); |
766 break; | 842 break; |
767 } | 843 } |
768 ++scale_counter_[reason]; | 844 ++(*scale_counter)[reason]; |
769 source_proxy_->RequestResolutionLowerThan(current_pixel_count); | 845 |
| 846 switch (degradation_preference_) { |
| 847 case DegradationPreference::kBalanced: |
| 848 source_proxy_->RequestResolutionLowerThan( |
| 849 adaptation_request.input_pixel_count_); |
| 850 LOG(LS_INFO) << "Scaling down resolution."; |
| 851 break; |
| 852 case DegradationPreference::kMaintainResolution: |
| 853 source_proxy_->RequestFramerateLowerThan( |
| 854 adaptation_request.framerate_fps_); |
| 855 LOG(LS_INFO) << "Scaling down framerate."; |
| 856 break; |
| 857 } |
| 858 |
770 LOG(LS_INFO) << "Scaling down resolution."; | 859 LOG(LS_INFO) << "Scaling down resolution."; |
771 for (size_t i = 0; i < kScaleReasonSize; ++i) { | 860 for (size_t i = 0; i < kScaleReasonSize; ++i) { |
772 LOG(LS_INFO) << "Scaled " << scale_counter_[i] | 861 LOG(LS_INFO) << "Scaled " << (*scale_counter)[i] |
773 << " times for reason: " << (i ? "cpu" : "quality"); | 862 << " times for reason: " << (i ? "cpu" : "quality"); |
774 } | 863 } |
775 } | 864 } |
776 | 865 |
777 void ViEEncoder::AdaptUp(AdaptReason reason) { | 866 void ViEEncoder::AdaptUp(AdaptReason reason) { |
778 RTC_DCHECK_RUN_ON(&encoder_queue_); | 867 RTC_DCHECK_RUN_ON(&encoder_queue_); |
779 if (scale_counter_[reason] == 0 || | 868 std::vector<int>* scale_counter = GetScaleCounters(); |
780 degradation_preference_ != DegradationPreference::kBalanced) { | 869 if ((*scale_counter)[reason] == 0) |
781 return; | 870 return; |
| 871 AdaptationRequest adaptation_request = { |
| 872 last_frame_info_->pixel_count(), |
| 873 stats_proxy_->GetStats().input_frame_rate, |
| 874 AdaptationRequest::Mode::kAdaptUp}; |
| 875 |
| 876 if (last_adaptation_request_ && |
| 877 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptUp) { |
| 878 switch (degradation_preference_) { |
| 879 case DegradationPreference::kBalanced: |
| 880 if (adaptation_request.input_pixel_count_ <= |
| 881 last_adaptation_request_->input_pixel_count_) { |
| 882 // Don't request higher resolution if the current resolution is not |
| 883 // higher than the last time we asked for the resolution to be higher. |
| 884 return; |
| 885 } |
| 886 break; |
| 887 case DegradationPreference::kMaintainResolution: |
| 888 // TODO(sprang): Don't request higher framerate if we are already at |
| 889 // max requested fps? |
| 890 break; |
| 891 } |
782 } | 892 } |
783 // Only scale if resolution is higher than last time we requested higher | |
784 // resolution. | |
785 RTC_DCHECK(static_cast<bool>(last_frame_info_)); | |
786 int current_pixel_count = last_frame_info_->pixel_count(); | |
787 if (last_adaptation_request_ && | |
788 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptUp && | |
789 current_pixel_count <= last_adaptation_request_->input_pixel_count_) { | |
790 // Don't request higher resolution if the current resolution is not higher | |
791 // than the last time we asked for the resolution to be higher. | |
792 return; | |
793 } | |
794 last_adaptation_request_.emplace(AdaptationRequest{ | |
795 current_pixel_count, AdaptationRequest::Mode::kAdaptUp}); | |
796 | 893 |
797 switch (reason) { | 894 switch (reason) { |
798 case kQuality: | 895 case kQuality: |
799 stats_proxy_->OnQualityRestrictedResolutionChanged( | 896 stats_proxy_->OnQualityRestrictedResolutionChanged( |
800 scale_counter_[reason] - 1); | 897 (*scale_counter)[reason] - 1); |
801 break; | 898 break; |
802 case kCpu: | 899 case kCpu: |
803 // Update stats accordingly. | 900 // Update stats accordingly. |
804 stats_proxy_->OnCpuRestrictedResolutionChanged(scale_counter_[reason] > | 901 stats_proxy_->OnCpuRestrictedResolutionChanged((*scale_counter)[reason] > |
805 1); | 902 1); |
806 break; | 903 break; |
807 } | 904 } |
808 --scale_counter_[reason]; | 905 --(*scale_counter)[reason]; |
809 source_proxy_->RequestHigherResolutionThan(current_pixel_count); | 906 |
| 907 switch (degradation_preference_) { |
| 908 case DegradationPreference::kBalanced: |
| 909 source_proxy_->RequestHigherResolutionThan( |
| 910 adaptation_request.input_pixel_count_); |
| 911 LOG(LS_INFO) << "Scaling up resolution."; |
| 912 break; |
| 913 case DegradationPreference::kMaintainResolution: |
| 914 source_proxy_->RequestHigherFramerateThan( |
| 915 adaptation_request.framerate_fps_); |
| 916 LOG(LS_INFO) << "Scaling up framerate."; |
| 917 break; |
| 918 } |
| 919 |
810 LOG(LS_INFO) << "Scaling up resolution."; | 920 LOG(LS_INFO) << "Scaling up resolution."; |
811 for (size_t i = 0; i < kScaleReasonSize; ++i) { | 921 for (size_t i = 0; i < kScaleReasonSize; ++i) { |
812 LOG(LS_INFO) << "Scaled " << scale_counter_[i] | 922 LOG(LS_INFO) << "Scaled " << (*scale_counter)[i] |
813 << " times for reason: " << (i ? "cpu" : "quality"); | 923 << " times for reason: " << (i ? "cpu" : "quality"); |
814 } | 924 } |
815 } | 925 } |
816 | 926 |
| 927 std::vector<int>* ViEEncoder::GetScaleCounters() { |
| 928 auto it = scale_counters_.find(degradation_preference_); |
| 929 RTC_CHECK(it != scale_counters_.end()); |
| 930 return &it->second; |
| 931 } |
| 932 |
817 } // namespace webrtc | 933 } // namespace webrtc |
OLD | NEW |