| 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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 if (kbps > 0) { | 66 if (kbps > 0) { |
| 67 if (kbps < 300 /* qvga */) { | 67 if (kbps < 300 /* qvga */) { |
| 68 return 320 * 240; | 68 return 320 * 240; |
| 69 } else if (kbps < 500 /* vga */) { | 69 } else if (kbps < 500 /* vga */) { |
| 70 return 640 * 480; | 70 return 640 * 480; |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 return std::numeric_limits<uint32_t>::max(); | 73 return std::numeric_limits<uint32_t>::max(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 // Initial limits for kBalanced degradation preference. |
| 77 int MinFps(int pixels) { |
| 78 if (pixels <= 320 * 240) { |
| 79 return 7; |
| 80 } else if (pixels <= 480 * 270) { |
| 81 return 10; |
| 82 } else if (pixels <= 640 * 480) { |
| 83 return 15; |
| 84 } else { |
| 85 return std::numeric_limits<int>::max(); |
| 86 } |
| 87 } |
| 88 |
| 89 int MaxFps(int pixels) { |
| 90 if (pixels <= 320 * 240) { |
| 91 return 10; |
| 92 } else if (pixels <= 480 * 270) { |
| 93 return 15; |
| 94 } else { |
| 95 return std::numeric_limits<int>::max(); |
| 96 } |
| 97 } |
| 98 |
| 76 bool IsResolutionScalingEnabled( | 99 bool IsResolutionScalingEnabled( |
| 77 VideoSendStream::DegradationPreference degradation_preference) { | 100 VideoSendStream::DegradationPreference degradation_preference) { |
| 78 return degradation_preference == | 101 return degradation_preference == |
| 79 VideoSendStream::DegradationPreference::kMaintainFramerate || | 102 VideoSendStream::DegradationPreference::kMaintainFramerate || |
| 80 degradation_preference == | 103 degradation_preference == |
| 81 VideoSendStream::DegradationPreference::kBalanced; | 104 VideoSendStream::DegradationPreference::kBalanced; |
| 82 } | 105 } |
| 83 | 106 |
| 84 bool IsFramerateScalingEnabled( | 107 bool IsFramerateScalingEnabled( |
| 85 VideoSendStream::DegradationPreference degradation_preference) { | 108 VideoSendStream::DegradationPreference degradation_preference) { |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 if (source_) | 227 if (source_) |
| 205 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | 228 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); |
| 206 } | 229 } |
| 207 | 230 |
| 208 rtc::VideoSinkWants GetActiveSinkWants() EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | 231 rtc::VideoSinkWants GetActiveSinkWants() EXCLUSIVE_LOCKS_REQUIRED(&crit_) { |
| 209 rtc::VideoSinkWants wants = sink_wants_; | 232 rtc::VideoSinkWants wants = sink_wants_; |
| 210 // Clear any constraints from the current sink wants that don't apply to | 233 // Clear any constraints from the current sink wants that don't apply to |
| 211 // the used degradation_preference. | 234 // the used degradation_preference. |
| 212 switch (degradation_preference_) { | 235 switch (degradation_preference_) { |
| 213 case VideoSendStream::DegradationPreference::kBalanced: | 236 case VideoSendStream::DegradationPreference::kBalanced: |
| 214 FALLTHROUGH(); | 237 break; |
| 215 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 238 case VideoSendStream::DegradationPreference::kMaintainFramerate: |
| 216 wants.max_framerate_fps = std::numeric_limits<int>::max(); | 239 wants.max_framerate_fps = std::numeric_limits<int>::max(); |
| 217 break; | 240 break; |
| 218 case VideoSendStream::DegradationPreference::kMaintainResolution: | 241 case VideoSendStream::DegradationPreference::kMaintainResolution: |
| 219 wants.max_pixel_count = std::numeric_limits<int>::max(); | 242 wants.max_pixel_count = std::numeric_limits<int>::max(); |
| 220 wants.target_pixel_count.reset(); | 243 wants.target_pixel_count.reset(); |
| 221 break; | 244 break; |
| 222 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 245 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
| 223 wants.max_pixel_count = std::numeric_limits<int>::max(); | 246 wants.max_pixel_count = std::numeric_limits<int>::max(); |
| 224 wants.target_pixel_count.reset(); | 247 wants.target_pixel_count.reset(); |
| 225 wants.max_framerate_fps = std::numeric_limits<int>::max(); | 248 wants.max_framerate_fps = std::numeric_limits<int>::max(); |
| 226 } | 249 } |
| 227 return wants; | 250 return wants; |
| 228 } | 251 } |
| 229 | 252 |
| 253 void ResetPixelFpsCount() { |
| 254 rtc::CritScope lock(&crit_); |
| 255 sink_wants_.max_pixel_count = std::numeric_limits<int>::max(); |
| 256 sink_wants_.target_pixel_count.reset(); |
| 257 sink_wants_.max_framerate_fps = std::numeric_limits<int>::max(); |
| 258 if (source_) |
| 259 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); |
| 260 } |
| 261 |
| 230 bool RequestResolutionLowerThan(int pixel_count) { | 262 bool RequestResolutionLowerThan(int pixel_count) { |
| 231 // Called on the encoder task queue. | 263 // Called on the encoder task queue. |
| 232 rtc::CritScope lock(&crit_); | 264 rtc::CritScope lock(&crit_); |
| 233 if (!source_ || !IsResolutionScalingEnabled(degradation_preference_)) { | 265 if (!source_ || !IsResolutionScalingEnabled(degradation_preference_)) { |
| 234 // This can happen since |degradation_preference_| is set on libjingle's | 266 // This can happen since |degradation_preference_| is set on libjingle's |
| 235 // worker thread but the adaptation is done on the encoder task queue. | 267 // worker thread but the adaptation is done on the encoder task queue. |
| 236 return false; | 268 return false; |
| 237 } | 269 } |
| 238 // The input video frame size will have a resolution less than or equal to | 270 // The input video frame size will have a resolution less than or equal to |
| 239 // |max_pixel_count| depending on how the source can scale the frame size. | 271 // |max_pixel_count| depending on how the source can scale the frame size. |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 rtc::VideoSourceInterface<VideoFrame>* source, | 472 rtc::VideoSourceInterface<VideoFrame>* source, |
| 441 const VideoSendStream::DegradationPreference& degradation_preference) { | 473 const VideoSendStream::DegradationPreference& degradation_preference) { |
| 442 RTC_DCHECK_RUN_ON(&thread_checker_); | 474 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 443 source_proxy_->SetSource(source, degradation_preference); | 475 source_proxy_->SetSource(source, degradation_preference); |
| 444 encoder_queue_.PostTask([this, degradation_preference] { | 476 encoder_queue_.PostTask([this, degradation_preference] { |
| 445 RTC_DCHECK_RUN_ON(&encoder_queue_); | 477 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 446 if (degradation_preference_ != degradation_preference) { | 478 if (degradation_preference_ != degradation_preference) { |
| 447 // Reset adaptation state, so that we're not tricked into thinking there's | 479 // Reset adaptation state, so that we're not tricked into thinking there's |
| 448 // an already pending request of the same type. | 480 // an already pending request of the same type. |
| 449 last_adaptation_request_.reset(); | 481 last_adaptation_request_.reset(); |
| 482 if (degradation_preference == |
| 483 VideoSendStream::DegradationPreference::kBalanced || |
| 484 degradation_preference_ == |
| 485 VideoSendStream::DegradationPreference::kBalanced) { |
| 486 // TODO(asapersson): Consider removing |adapt_counters_| map and use one |
| 487 // AdaptCounter for all modes. |
| 488 source_proxy_->ResetPixelFpsCount(); |
| 489 adapt_counters_.clear(); |
| 490 } |
| 450 } | 491 } |
| 451 degradation_preference_ = degradation_preference; | 492 degradation_preference_ = degradation_preference; |
| 452 bool allow_scaling = IsResolutionScalingEnabled(degradation_preference_); | 493 bool allow_scaling = IsResolutionScalingEnabled(degradation_preference_); |
| 453 initial_rampup_ = allow_scaling ? 0 : kMaxInitialFramedrop; | 494 initial_rampup_ = allow_scaling ? 0 : kMaxInitialFramedrop; |
| 454 ConfigureQualityScaler(); | 495 ConfigureQualityScaler(); |
| 455 }); | 496 }); |
| 456 } | 497 } |
| 457 | 498 |
| 458 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { | 499 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { |
| 459 source_proxy_->SetWantsRotationApplied(rotation_applied); | 500 source_proxy_->SetWantsRotationApplied(rotation_applied); |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 796 RTC_DCHECK_RUN_ON(&encoder_queue_); | 837 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 797 AdaptationRequest adaptation_request = { | 838 AdaptationRequest adaptation_request = { |
| 798 last_frame_info_->pixel_count(), | 839 last_frame_info_->pixel_count(), |
| 799 stats_proxy_->GetStats().input_frame_rate, | 840 stats_proxy_->GetStats().input_frame_rate, |
| 800 AdaptationRequest::Mode::kAdaptDown}; | 841 AdaptationRequest::Mode::kAdaptDown}; |
| 801 | 842 |
| 802 bool downgrade_requested = | 843 bool downgrade_requested = |
| 803 last_adaptation_request_ && | 844 last_adaptation_request_ && |
| 804 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown; | 845 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown; |
| 805 | 846 |
| 806 int max_downgrades = 0; | |
| 807 switch (degradation_preference_) { | 847 switch (degradation_preference_) { |
| 808 case VideoSendStream::DegradationPreference::kBalanced: | 848 case VideoSendStream::DegradationPreference::kBalanced: |
| 809 FALLTHROUGH(); | 849 break; |
| 810 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 850 case VideoSendStream::DegradationPreference::kMaintainFramerate: |
| 811 max_downgrades = kMaxCpuResolutionDowngrades; | |
| 812 if (downgrade_requested && | 851 if (downgrade_requested && |
| 813 adaptation_request.input_pixel_count_ >= | 852 adaptation_request.input_pixel_count_ >= |
| 814 last_adaptation_request_->input_pixel_count_) { | 853 last_adaptation_request_->input_pixel_count_) { |
| 815 // Don't request lower resolution if the current resolution is not | 854 // Don't request lower resolution if the current resolution is not |
| 816 // lower than the last time we asked for the resolution to be lowered. | 855 // lower than the last time we asked for the resolution to be lowered. |
| 817 return; | 856 return; |
| 818 } | 857 } |
| 819 break; | 858 break; |
| 820 case VideoSendStream::DegradationPreference::kMaintainResolution: | 859 case VideoSendStream::DegradationPreference::kMaintainResolution: |
| 821 max_downgrades = kMaxCpuFramerateDowngrades; | |
| 822 if (adaptation_request.framerate_fps_ <= 0 || | 860 if (adaptation_request.framerate_fps_ <= 0 || |
| 823 (downgrade_requested && | 861 (downgrade_requested && |
| 824 adaptation_request.framerate_fps_ < kMinFramerateFps)) { | 862 adaptation_request.framerate_fps_ < kMinFramerateFps)) { |
| 825 // If no input fps estimate available, can't determine how to scale down | 863 // If no input fps estimate available, can't determine how to scale down |
| 826 // framerate. Otherwise, don't request lower framerate if we don't have | 864 // framerate. Otherwise, don't request lower framerate if we don't have |
| 827 // a valid frame rate. Since framerate, unlike resolution, is a measure | 865 // a valid frame rate. Since framerate, unlike resolution, is a measure |
| 828 // we have to estimate, and can fluctuate naturally over time, don't | 866 // we have to estimate, and can fluctuate naturally over time, don't |
| 829 // make the same kind of limitations as for resolution, but trust the | 867 // make the same kind of limitations as for resolution, but trust the |
| 830 // overuse detector to not trigger too often. | 868 // overuse detector to not trigger too often. |
| 831 return; | 869 return; |
| 832 } | 870 } |
| 833 break; | 871 break; |
| 834 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 872 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
| 835 return; | 873 return; |
| 836 } | 874 } |
| 837 | 875 |
| 838 if (reason == kCpu) { | 876 if (reason == kCpu) { |
| 839 if (GetConstAdaptCounter().TotalCount(kCpu) >= max_downgrades) | 877 if (GetConstAdaptCounter().ResolutionCount(kCpu) >= |
| 878 kMaxCpuResolutionDowngrades || |
| 879 GetConstAdaptCounter().FramerateCount(kCpu) >= |
| 880 kMaxCpuFramerateDowngrades) { |
| 840 return; | 881 return; |
| 882 } |
| 841 } | 883 } |
| 842 | 884 |
| 843 switch (degradation_preference_) { | 885 switch (degradation_preference_) { |
| 844 case VideoSendStream::DegradationPreference::kBalanced: | 886 case VideoSendStream::DegradationPreference::kBalanced: { |
| 887 // Try scale down framerate, if lower. |
| 888 int fps = MinFps(last_frame_info_->pixel_count()); |
| 889 if (source_proxy_->RestrictFramerate(fps)) { |
| 890 GetAdaptCounter().IncrementFramerate(reason); |
| 891 break; |
| 892 } |
| 893 // Scale down resolution. |
| 845 FALLTHROUGH(); | 894 FALLTHROUGH(); |
| 895 } |
| 846 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 896 case VideoSendStream::DegradationPreference::kMaintainFramerate: |
| 847 // Scale down resolution. | 897 // Scale down resolution. |
| 848 if (!source_proxy_->RequestResolutionLowerThan( | 898 if (!source_proxy_->RequestResolutionLowerThan( |
| 849 adaptation_request.input_pixel_count_)) { | 899 adaptation_request.input_pixel_count_)) { |
| 850 return; | 900 return; |
| 851 } | 901 } |
| 852 GetAdaptCounter().IncrementResolution(reason, 1); | 902 GetAdaptCounter().IncrementResolution(reason); |
| 853 break; | 903 break; |
| 854 case VideoSendStream::DegradationPreference::kMaintainResolution: | 904 case VideoSendStream::DegradationPreference::kMaintainResolution: |
| 855 // Scale down framerate. | 905 // Scale down framerate. |
| 856 if (!source_proxy_->RequestFramerateLowerThan( | 906 if (!source_proxy_->RequestFramerateLowerThan( |
| 857 adaptation_request.framerate_fps_)) { | 907 adaptation_request.framerate_fps_)) { |
| 858 return; | 908 return; |
| 859 } | 909 } |
| 860 GetAdaptCounter().IncrementFramerate(reason, 1); | 910 GetAdaptCounter().IncrementFramerate(reason); |
| 861 break; | 911 break; |
| 862 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 912 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
| 863 RTC_NOTREACHED(); | 913 RTC_NOTREACHED(); |
| 864 } | 914 } |
| 865 | 915 |
| 866 last_adaptation_request_.emplace(adaptation_request); | 916 last_adaptation_request_.emplace(adaptation_request); |
| 867 | 917 |
| 868 UpdateAdaptationStats(reason); | 918 UpdateAdaptationStats(reason); |
| 869 | 919 |
| 870 LOG(LS_INFO) << GetConstAdaptCounter().ToString(); | 920 LOG(LS_INFO) << GetConstAdaptCounter().ToString(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 881 | 931 |
| 882 AdaptationRequest adaptation_request = { | 932 AdaptationRequest adaptation_request = { |
| 883 last_frame_info_->pixel_count(), | 933 last_frame_info_->pixel_count(), |
| 884 stats_proxy_->GetStats().input_frame_rate, | 934 stats_proxy_->GetStats().input_frame_rate, |
| 885 AdaptationRequest::Mode::kAdaptUp}; | 935 AdaptationRequest::Mode::kAdaptUp}; |
| 886 | 936 |
| 887 bool adapt_up_requested = | 937 bool adapt_up_requested = |
| 888 last_adaptation_request_ && | 938 last_adaptation_request_ && |
| 889 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptUp; | 939 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptUp; |
| 890 | 940 |
| 891 switch (degradation_preference_) { | 941 if (degradation_preference_ == |
| 892 case VideoSendStream::DegradationPreference::kBalanced: | 942 VideoSendStream::DegradationPreference::kMaintainFramerate) { |
| 893 FALLTHROUGH(); | 943 if (adapt_up_requested && |
| 894 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 944 adaptation_request.input_pixel_count_ <= |
| 895 if (adapt_up_requested && | 945 last_adaptation_request_->input_pixel_count_) { |
| 896 adaptation_request.input_pixel_count_ <= | 946 // Don't request higher resolution if the current resolution is not |
| 897 last_adaptation_request_->input_pixel_count_) { | 947 // higher than the last time we asked for the resolution to be higher. |
| 898 // Don't request higher resolution if the current resolution is not | |
| 899 // higher than the last time we asked for the resolution to be higher. | |
| 900 return; | |
| 901 } | |
| 902 break; | |
| 903 case VideoSendStream::DegradationPreference::kMaintainResolution: | |
| 904 // TODO(sprang): Don't request higher framerate if we are already at | |
| 905 // max requested fps? | |
| 906 break; | |
| 907 case VideoSendStream::DegradationPreference::kDegradationDisabled: | |
| 908 return; | 948 return; |
| 949 } |
| 909 } | 950 } |
| 910 | 951 |
| 911 switch (degradation_preference_) { | 952 switch (degradation_preference_) { |
| 912 case VideoSendStream::DegradationPreference::kBalanced: | 953 case VideoSendStream::DegradationPreference::kBalanced: { |
| 954 // Try scale up framerate, if higher. |
| 955 int fps = MaxFps(last_frame_info_->pixel_count()); |
| 956 if (source_proxy_->IncreaseFramerate(fps)) { |
| 957 GetAdaptCounter().DecrementFramerate(reason, fps); |
| 958 // Reset framerate in case of fewer fps steps down than up. |
| 959 if (adapt_counter.FramerateCount() == 0 && |
| 960 fps != std::numeric_limits<int>::max()) { |
| 961 LOG(LS_INFO) << "Removing framerate down-scaling setting."; |
| 962 source_proxy_->IncreaseFramerate(std::numeric_limits<int>::max()); |
| 963 } |
| 964 break; |
| 965 } |
| 966 // Scale up resolution. |
| 913 FALLTHROUGH(); | 967 FALLTHROUGH(); |
| 968 } |
| 914 case VideoSendStream::DegradationPreference::kMaintainFramerate: { | 969 case VideoSendStream::DegradationPreference::kMaintainFramerate: { |
| 915 // Scale up resolution. | 970 // Scale up resolution. |
| 916 int pixel_count = adaptation_request.input_pixel_count_; | 971 int pixel_count = adaptation_request.input_pixel_count_; |
| 917 if (adapt_counter.ResolutionCount() == 1) { | 972 if (adapt_counter.ResolutionCount() == 1) { |
| 918 LOG(LS_INFO) << "Removing resolution down-scaling setting."; | 973 LOG(LS_INFO) << "Removing resolution down-scaling setting."; |
| 919 pixel_count = std::numeric_limits<int>::max(); | 974 pixel_count = std::numeric_limits<int>::max(); |
| 920 } | 975 } |
| 921 if (!source_proxy_->RequestHigherResolutionThan(pixel_count)) | 976 if (!source_proxy_->RequestHigherResolutionThan(pixel_count)) |
| 922 return; | 977 return; |
| 923 GetAdaptCounter().IncrementResolution(reason, -1); | 978 GetAdaptCounter().DecrementResolution(reason); |
| 924 break; | 979 break; |
| 925 } | 980 } |
| 926 case VideoSendStream::DegradationPreference::kMaintainResolution: { | 981 case VideoSendStream::DegradationPreference::kMaintainResolution: { |
| 927 // Scale up framerate. | 982 // Scale up framerate. |
| 928 int fps = adaptation_request.framerate_fps_; | 983 int fps = adaptation_request.framerate_fps_; |
| 929 if (adapt_counter.FramerateCount() == 1) { | 984 if (adapt_counter.FramerateCount() == 1) { |
| 930 LOG(LS_INFO) << "Removing framerate down-scaling setting."; | 985 LOG(LS_INFO) << "Removing framerate down-scaling setting."; |
| 931 fps = std::numeric_limits<int>::max(); | 986 fps = std::numeric_limits<int>::max(); |
| 932 } | 987 } |
| 933 if (!source_proxy_->RequestHigherFramerateThan(fps)) | 988 if (!source_proxy_->RequestHigherFramerateThan(fps)) |
| 934 return; | 989 return; |
| 935 GetAdaptCounter().IncrementFramerate(reason, -1); | 990 GetAdaptCounter().DecrementFramerate(reason); |
| 936 break; | 991 break; |
| 937 } | 992 } |
| 938 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 993 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
| 939 RTC_NOTREACHED(); | 994 return; |
| 940 } | 995 } |
| 941 | 996 |
| 942 last_adaptation_request_.emplace(adaptation_request); | 997 last_adaptation_request_.emplace(adaptation_request); |
| 943 | 998 |
| 944 UpdateAdaptationStats(reason); | 999 UpdateAdaptationStats(reason); |
| 945 | 1000 |
| 946 LOG(LS_INFO) << adapt_counter.ToString(); | 1001 LOG(LS_INFO) << adapt_counter.ToString(); |
| 947 } | 1002 } |
| 948 | 1003 |
| 949 void ViEEncoder::UpdateAdaptationStats(AdaptReason reason) { | 1004 void ViEEncoder::UpdateAdaptationStats(AdaptReason reason) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 987 } | 1042 } |
| 988 | 1043 |
| 989 const ViEEncoder::AdaptCounter& ViEEncoder::GetConstAdaptCounter() { | 1044 const ViEEncoder::AdaptCounter& ViEEncoder::GetConstAdaptCounter() { |
| 990 return adapt_counters_[degradation_preference_]; | 1045 return adapt_counters_[degradation_preference_]; |
| 991 } | 1046 } |
| 992 | 1047 |
| 993 // Class holding adaptation information. | 1048 // Class holding adaptation information. |
| 994 ViEEncoder::AdaptCounter::AdaptCounter() { | 1049 ViEEncoder::AdaptCounter::AdaptCounter() { |
| 995 fps_counters_.resize(kScaleReasonSize); | 1050 fps_counters_.resize(kScaleReasonSize); |
| 996 resolution_counters_.resize(kScaleReasonSize); | 1051 resolution_counters_.resize(kScaleReasonSize); |
| 1052 static_assert(kScaleReasonSize == 2, "Update MoveCount."); |
| 997 } | 1053 } |
| 998 | 1054 |
| 999 ViEEncoder::AdaptCounter::~AdaptCounter() {} | 1055 ViEEncoder::AdaptCounter::~AdaptCounter() {} |
| 1000 | 1056 |
| 1001 std::string ViEEncoder::AdaptCounter::ToString() const { | 1057 std::string ViEEncoder::AdaptCounter::ToString() const { |
| 1002 std::stringstream ss; | 1058 std::stringstream ss; |
| 1003 ss << "Downgrade counts: fps: {" << ToString(fps_counters_); | 1059 ss << "Downgrade counts: fps: {" << ToString(fps_counters_); |
| 1004 ss << "}, resolution: {" << ToString(resolution_counters_) << "}"; | 1060 ss << "}, resolution: {" << ToString(resolution_counters_) << "}"; |
| 1005 return ss.str(); | 1061 return ss.str(); |
| 1006 } | 1062 } |
| 1007 | 1063 |
| 1008 ViEEncoder::AdaptCounts ViEEncoder::AdaptCounter::Counts(int reason) const { | 1064 ViEEncoder::AdaptCounts ViEEncoder::AdaptCounter::Counts(int reason) const { |
| 1009 AdaptCounts counts; | 1065 AdaptCounts counts; |
| 1010 counts.fps = fps_counters_[reason]; | 1066 counts.fps = fps_counters_[reason]; |
| 1011 counts.resolution = resolution_counters_[reason]; | 1067 counts.resolution = resolution_counters_[reason]; |
| 1012 return counts; | 1068 return counts; |
| 1013 } | 1069 } |
| 1014 | 1070 |
| 1015 void ViEEncoder::AdaptCounter::IncrementFramerate(int reason, int delta) { | 1071 void ViEEncoder::AdaptCounter::IncrementFramerate(int reason) { |
| 1016 fps_counters_[reason] += delta; | 1072 ++(fps_counters_[reason]); |
| 1017 } | 1073 } |
| 1018 | 1074 |
| 1019 void ViEEncoder::AdaptCounter::IncrementResolution(int reason, int delta) { | 1075 void ViEEncoder::AdaptCounter::IncrementResolution(int reason) { |
| 1020 resolution_counters_[reason] += delta; | 1076 ++(resolution_counters_[reason]); |
| 1077 } |
| 1078 |
| 1079 void ViEEncoder::AdaptCounter::DecrementFramerate(int reason) { |
| 1080 if (fps_counters_[reason] == 0) { |
| 1081 // Balanced mode: Adapt up is in a different order, switch reason. |
| 1082 // E.g. framerate adapt down: quality (2), framerate adapt up: cpu (3). |
| 1083 // 1. Down resolution (cpu): res={quality:0,cpu:1}, fps={quality:0,cpu:0} |
| 1084 // 2. Down fps (quality): res={quality:0,cpu:1}, fps={quality:1,cpu:0} |
| 1085 // 3. Up fps (cpu): res={quality:1,cpu:0}, fps={quality:0,cpu:0} |
| 1086 // 4. Up resolution (quality): res={quality:0,cpu:0}, fps={quality:0,cpu:0} |
| 1087 RTC_DCHECK_GT(TotalCount(reason), 0) << "No downgrade for reason."; |
| 1088 RTC_DCHECK_GT(FramerateCount(), 0) << "Framerate not downgraded."; |
| 1089 MoveCount(&resolution_counters_, reason); |
| 1090 MoveCount(&fps_counters_, (reason + 1) % kScaleReasonSize); |
| 1091 } |
| 1092 --(fps_counters_[reason]); |
| 1093 RTC_DCHECK_GE(fps_counters_[reason], 0); |
| 1094 } |
| 1095 |
| 1096 void ViEEncoder::AdaptCounter::DecrementResolution(int reason) { |
| 1097 if (resolution_counters_[reason] == 0) { |
| 1098 // Balanced mode: Adapt up is in a different order, switch reason. |
| 1099 RTC_DCHECK_GT(TotalCount(reason), 0) << "No downgrade for reason."; |
| 1100 RTC_DCHECK_GT(ResolutionCount(), 0) << "Resolution not downgraded."; |
| 1101 MoveCount(&fps_counters_, reason); |
| 1102 MoveCount(&resolution_counters_, (reason + 1) % kScaleReasonSize); |
| 1103 } |
| 1104 --(resolution_counters_[reason]); |
| 1105 RTC_DCHECK_GE(resolution_counters_[reason], 0); |
| 1106 } |
| 1107 |
| 1108 void ViEEncoder::AdaptCounter::DecrementFramerate(int reason, int cur_fps) { |
| 1109 DecrementFramerate(reason); |
| 1110 // Reset if at max fps (i.e. in case of fewer steps up than down). |
| 1111 if (cur_fps == std::numeric_limits<int>::max()) |
| 1112 std::fill(fps_counters_.begin(), fps_counters_.end(), 0); |
| 1021 } | 1113 } |
| 1022 | 1114 |
| 1023 int ViEEncoder::AdaptCounter::FramerateCount() const { | 1115 int ViEEncoder::AdaptCounter::FramerateCount() const { |
| 1024 return Count(fps_counters_); | 1116 return Count(fps_counters_); |
| 1025 } | 1117 } |
| 1026 | 1118 |
| 1027 int ViEEncoder::AdaptCounter::ResolutionCount() const { | 1119 int ViEEncoder::AdaptCounter::ResolutionCount() const { |
| 1028 return Count(resolution_counters_); | 1120 return Count(resolution_counters_); |
| 1029 } | 1121 } |
| 1030 | 1122 |
| 1031 int ViEEncoder::AdaptCounter::TotalCount() const { | |
| 1032 return FramerateCount() + ResolutionCount(); | |
| 1033 } | |
| 1034 | |
| 1035 int ViEEncoder::AdaptCounter::FramerateCount(int reason) const { | 1123 int ViEEncoder::AdaptCounter::FramerateCount(int reason) const { |
| 1036 return fps_counters_[reason]; | 1124 return fps_counters_[reason]; |
| 1037 } | 1125 } |
| 1038 | 1126 |
| 1039 int ViEEncoder::AdaptCounter::ResolutionCount(int reason) const { | 1127 int ViEEncoder::AdaptCounter::ResolutionCount(int reason) const { |
| 1040 return resolution_counters_[reason]; | 1128 return resolution_counters_[reason]; |
| 1041 } | 1129 } |
| 1042 | 1130 |
| 1043 int ViEEncoder::AdaptCounter::TotalCount(int reason) const { | 1131 int ViEEncoder::AdaptCounter::TotalCount(int reason) const { |
| 1044 return FramerateCount(reason) + ResolutionCount(reason); | 1132 return FramerateCount(reason) + ResolutionCount(reason); |
| 1045 } | 1133 } |
| 1046 | 1134 |
| 1047 int ViEEncoder::AdaptCounter::Count(const std::vector<int>& counters) const { | 1135 int ViEEncoder::AdaptCounter::Count(const std::vector<int>& counters) const { |
| 1048 return std::accumulate(counters.begin(), counters.end(), 0); | 1136 return std::accumulate(counters.begin(), counters.end(), 0); |
| 1049 } | 1137 } |
| 1050 | 1138 |
| 1139 void ViEEncoder::AdaptCounter::MoveCount(std::vector<int>* counters, |
| 1140 int from_reason) { |
| 1141 int to_reason = (from_reason + 1) % kScaleReasonSize; |
| 1142 ++((*counters)[to_reason]); |
| 1143 --((*counters)[from_reason]); |
| 1144 } |
| 1145 |
| 1051 std::string ViEEncoder::AdaptCounter::ToString( | 1146 std::string ViEEncoder::AdaptCounter::ToString( |
| 1052 const std::vector<int>& counters) const { | 1147 const std::vector<int>& counters) const { |
| 1053 std::stringstream ss; | 1148 std::stringstream ss; |
| 1054 for (size_t reason = 0; reason < kScaleReasonSize; ++reason) { | 1149 for (size_t reason = 0; reason < kScaleReasonSize; ++reason) { |
| 1055 ss << (reason ? " cpu" : "quality") << ":" << counters[reason]; | 1150 ss << (reason ? " cpu" : "quality") << ":" << counters[reason]; |
| 1056 } | 1151 } |
| 1057 return ss.str(); | 1152 return ss.str(); |
| 1058 } | 1153 } |
| 1059 | 1154 |
| 1060 } // namespace webrtc | 1155 } // namespace webrtc |
| OLD | NEW |