| 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 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 wants.target_pixel_count.reset(); | 203 wants.target_pixel_count.reset(); |
| 204 break; | 204 break; |
| 205 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 205 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
| 206 wants.max_pixel_count = std::numeric_limits<int>::max(); | 206 wants.max_pixel_count = std::numeric_limits<int>::max(); |
| 207 wants.target_pixel_count.reset(); | 207 wants.target_pixel_count.reset(); |
| 208 wants.max_framerate_fps = std::numeric_limits<int>::max(); | 208 wants.max_framerate_fps = std::numeric_limits<int>::max(); |
| 209 } | 209 } |
| 210 return wants; | 210 return wants; |
| 211 } | 211 } |
| 212 | 212 |
| 213 void RequestResolutionLowerThan(int pixel_count) { | 213 bool RequestResolutionLowerThan(int pixel_count) { |
| 214 // Called on the encoder task queue. | 214 // Called on the encoder task queue. |
| 215 rtc::CritScope lock(&crit_); | 215 rtc::CritScope lock(&crit_); |
| 216 if (!IsResolutionScalingEnabledLocked()) { | 216 if (!IsResolutionScalingEnabledLocked()) { |
| 217 // This can happen since |degradation_preference_| is set on libjingle's | 217 // This can happen since |degradation_preference_| is set on libjingle's |
| 218 // worker thread but the adaptation is done on the encoder task queue. | 218 // worker thread but the adaptation is done on the encoder task queue. |
| 219 return; | 219 return false; |
| 220 } | 220 } |
| 221 // The input video frame size will have a resolution with less than or | 221 // The input video frame size will have a resolution with less than or |
| 222 // equal to |max_pixel_count| depending on how the source can scale the | 222 // equal to |max_pixel_count| depending on how the source can scale the |
| 223 // input frame size. | 223 // input frame size. |
| 224 const int pixels_wanted = (pixel_count * 3) / 5; | 224 const int pixels_wanted = (pixel_count * 3) / 5; |
| 225 if (pixels_wanted < kMinPixelsPerFrame) | 225 if (pixels_wanted < kMinPixelsPerFrame) |
| 226 return; | 226 return false; |
| 227 |
| 227 sink_wants_.max_pixel_count = pixels_wanted; | 228 sink_wants_.max_pixel_count = pixels_wanted; |
| 228 sink_wants_.target_pixel_count = rtc::Optional<int>(); | 229 sink_wants_.target_pixel_count = rtc::Optional<int>(); |
| 229 if (source_) | 230 if (source_) |
| 230 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | 231 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); |
| 232 return true; |
| 231 } | 233 } |
| 232 | 234 |
| 233 void RequestFramerateLowerThan(int framerate_fps) { | 235 void RequestFramerateLowerThan(int framerate_fps) { |
| 234 // Called on the encoder task queue. | 236 // Called on the encoder task queue. |
| 235 rtc::CritScope lock(&crit_); | 237 rtc::CritScope lock(&crit_); |
| 236 if (!IsFramerateScalingEnabledLocked()) { | 238 if (!IsFramerateScalingEnabledLocked()) { |
| 237 // This can happen since |degradation_preference_| is set on libjingle's | 239 // This can happen since |degradation_preference_| is set on libjingle's |
| 238 // worker thread but the adaptation is done on the encoder task queue. | 240 // worker thread but the adaptation is done on the encoder task queue. |
| 239 return; | 241 return; |
| 240 } | 242 } |
| (...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 826 // we have to estimate, and can fluctuate naturally over time, don't | 828 // we have to estimate, and can fluctuate naturally over time, don't |
| 827 // make the same kind of limitations as for resolution, but trust the | 829 // make the same kind of limitations as for resolution, but trust the |
| 828 // overuse detector to not trigger too often. | 830 // overuse detector to not trigger too often. |
| 829 return; | 831 return; |
| 830 } | 832 } |
| 831 break; | 833 break; |
| 832 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 834 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
| 833 return; | 835 return; |
| 834 } | 836 } |
| 835 | 837 |
| 836 last_adaptation_request_.emplace(adaptation_request); | 838 if (reason == kCpu) { |
| 837 const std::vector<int>& scale_counter = GetScaleCounters(); | 839 const int cpu_scale_counter = GetScaleCounters()[reason]; |
| 838 | 840 if (cpu_scale_counter >= max_downgrades) |
| 839 switch (reason) { | 841 return; |
| 840 case kQuality: | |
| 841 stats_proxy_->OnQualityRestrictedResolutionChanged(scale_counter[reason] + | |
| 842 1); | |
| 843 break; | |
| 844 case kCpu: | |
| 845 if (scale_counter[reason] >= max_downgrades) | |
| 846 return; | |
| 847 // Update stats accordingly. | |
| 848 stats_proxy_->OnCpuRestrictedResolutionChanged(true); | |
| 849 break; | |
| 850 } | 842 } |
| 851 | 843 |
| 852 IncrementScaleCounter(reason, 1); | |
| 853 | |
| 854 switch (degradation_preference_) { | 844 switch (degradation_preference_) { |
| 855 case VideoSendStream::DegradationPreference::kBalanced: | 845 case VideoSendStream::DegradationPreference::kBalanced: |
| 856 FALLTHROUGH(); | 846 FALLTHROUGH(); |
| 857 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 847 case VideoSendStream::DegradationPreference::kMaintainFramerate: |
| 858 source_proxy_->RequestResolutionLowerThan( | 848 if (!source_proxy_->RequestResolutionLowerThan( |
| 859 adaptation_request.input_pixel_count_); | 849 adaptation_request.input_pixel_count_)) { |
| 850 return; |
| 851 } |
| 860 LOG(LS_INFO) << "Scaling down resolution."; | 852 LOG(LS_INFO) << "Scaling down resolution."; |
| 861 break; | 853 break; |
| 862 case VideoSendStream::DegradationPreference::kMaintainResolution: | 854 case VideoSendStream::DegradationPreference::kMaintainResolution: |
| 863 source_proxy_->RequestFramerateLowerThan( | 855 source_proxy_->RequestFramerateLowerThan( |
| 864 adaptation_request.framerate_fps_); | 856 adaptation_request.framerate_fps_); |
| 865 LOG(LS_INFO) << "Scaling down framerate."; | 857 LOG(LS_INFO) << "Scaling down framerate."; |
| 866 break; | 858 break; |
| 867 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 859 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
| 868 RTC_NOTREACHED(); | 860 RTC_NOTREACHED(); |
| 869 } | 861 } |
| 870 | 862 |
| 863 last_adaptation_request_.emplace(adaptation_request); |
| 864 |
| 865 IncrementScaleCounter(reason, 1); |
| 866 |
| 867 // Update stats. |
| 868 const std::vector<int>& scale_counters = GetScaleCounters(); |
| 869 switch (reason) { |
| 870 case kQuality: |
| 871 stats_proxy_->OnQualityRestrictedResolutionChanged( |
| 872 scale_counters[reason]); |
| 873 break; |
| 874 case kCpu: |
| 875 stats_proxy_->OnCpuRestrictedResolutionChanged(true); |
| 876 break; |
| 877 } |
| 878 |
| 871 for (size_t i = 0; i < kScaleReasonSize; ++i) { | 879 for (size_t i = 0; i < kScaleReasonSize; ++i) { |
| 872 LOG(LS_INFO) << "Scaled " << GetScaleCounters()[i] | 880 LOG(LS_INFO) << "Scaled " << scale_counters[i] |
| 873 << " times for reason: " << (i ? "cpu" : "quality"); | 881 << " times for reason: " << (i ? "cpu" : "quality"); |
| 874 } | 882 } |
| 875 } | 883 } |
| 876 | 884 |
| 877 void ViEEncoder::AdaptUp(AdaptReason reason) { | 885 void ViEEncoder::AdaptUp(AdaptReason reason) { |
| 878 RTC_DCHECK_RUN_ON(&encoder_queue_); | 886 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 879 int scale_counter = GetScaleCounters()[reason]; | 887 int scale_counter = GetScaleCounters()[reason]; |
| 880 if (scale_counter == 0) | 888 if (scale_counter == 0) |
| 881 return; | 889 return; |
| 882 RTC_DCHECK_GT(scale_counter, 0); | 890 RTC_DCHECK_GT(scale_counter, 0); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 901 } | 909 } |
| 902 break; | 910 break; |
| 903 case VideoSendStream::DegradationPreference::kMaintainResolution: | 911 case VideoSendStream::DegradationPreference::kMaintainResolution: |
| 904 // TODO(sprang): Don't request higher framerate if we are already at | 912 // TODO(sprang): Don't request higher framerate if we are already at |
| 905 // max requested fps? | 913 // max requested fps? |
| 906 break; | 914 break; |
| 907 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 915 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
| 908 return; | 916 return; |
| 909 } | 917 } |
| 910 | 918 |
| 911 last_adaptation_request_.emplace(adaptation_request); | |
| 912 | |
| 913 switch (reason) { | |
| 914 case kQuality: | |
| 915 stats_proxy_->OnQualityRestrictedResolutionChanged(scale_counter - 1); | |
| 916 break; | |
| 917 case kCpu: | |
| 918 // Update stats accordingly. | |
| 919 stats_proxy_->OnCpuRestrictedResolutionChanged(scale_counter > 1); | |
| 920 break; | |
| 921 } | |
| 922 | |
| 923 // Decrease counter of how many times we have scaled down, for this | 919 // Decrease counter of how many times we have scaled down, for this |
| 924 // degradation preference mode and reason. | 920 // degradation preference mode and reason. |
| 925 IncrementScaleCounter(reason, -1); | 921 IncrementScaleCounter(reason, -1); |
| 926 | 922 |
| 927 // Get a sum of how many times have scaled down, in total, for this | 923 // Get a sum of how many times have scaled down, in total, for this |
| 928 // degradation preference mode. If it is 0, remove any restraints. | 924 // degradation preference mode. If it is 0, remove any restraints. |
| 929 const std::vector<int>& current_scale_counters = GetScaleCounters(); | 925 const std::vector<int>& scale_counters = GetScaleCounters(); |
| 930 const int scale_sum = std::accumulate(current_scale_counters.begin(), | 926 const int scale_sum = |
| 931 current_scale_counters.end(), 0); | 927 std::accumulate(scale_counters.begin(), scale_counters.end(), 0); |
| 932 switch (degradation_preference_) { | 928 switch (degradation_preference_) { |
| 933 case VideoSendStream::DegradationPreference::kBalanced: | 929 case VideoSendStream::DegradationPreference::kBalanced: |
| 934 FALLTHROUGH(); | 930 FALLTHROUGH(); |
| 935 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 931 case VideoSendStream::DegradationPreference::kMaintainFramerate: |
| 936 if (scale_sum == 0) { | 932 if (scale_sum == 0) { |
| 937 LOG(LS_INFO) << "Removing resolution down-scaling setting."; | 933 LOG(LS_INFO) << "Removing resolution down-scaling setting."; |
| 938 source_proxy_->RequestHigherResolutionThan( | 934 source_proxy_->RequestHigherResolutionThan( |
| 939 std::numeric_limits<int>::max()); | 935 std::numeric_limits<int>::max()); |
| 940 } else { | 936 } else { |
| 941 source_proxy_->RequestHigherResolutionThan( | 937 source_proxy_->RequestHigherResolutionThan( |
| 942 adaptation_request.input_pixel_count_); | 938 adaptation_request.input_pixel_count_); |
| 943 LOG(LS_INFO) << "Scaling up resolution."; | 939 LOG(LS_INFO) << "Scaling up resolution."; |
| 944 } | 940 } |
| 945 break; | 941 break; |
| 946 case VideoSendStream::DegradationPreference::kMaintainResolution: | 942 case VideoSendStream::DegradationPreference::kMaintainResolution: |
| 947 if (scale_sum == 0) { | 943 if (scale_sum == 0) { |
| 948 LOG(LS_INFO) << "Removing framerate down-scaling setting."; | 944 LOG(LS_INFO) << "Removing framerate down-scaling setting."; |
| 949 source_proxy_->RequestHigherFramerateThan( | 945 source_proxy_->RequestHigherFramerateThan( |
| 950 std::numeric_limits<int>::max()); | 946 std::numeric_limits<int>::max()); |
| 951 } else { | 947 } else { |
| 952 source_proxy_->RequestHigherFramerateThan( | 948 source_proxy_->RequestHigherFramerateThan( |
| 953 adaptation_request.framerate_fps_); | 949 adaptation_request.framerate_fps_); |
| 954 LOG(LS_INFO) << "Scaling up framerate."; | 950 LOG(LS_INFO) << "Scaling up framerate."; |
| 955 } | 951 } |
| 956 break; | 952 break; |
| 957 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 953 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
| 958 RTC_NOTREACHED(); | 954 RTC_NOTREACHED(); |
| 959 } | 955 } |
| 960 | 956 |
| 957 last_adaptation_request_.emplace(adaptation_request); |
| 958 |
| 959 // Update stats. |
| 960 switch (reason) { |
| 961 case kQuality: |
| 962 stats_proxy_->OnQualityRestrictedResolutionChanged( |
| 963 scale_counters[reason]); |
| 964 break; |
| 965 case kCpu: |
| 966 stats_proxy_->OnCpuRestrictedResolutionChanged(scale_counters[reason] > |
| 967 0); |
| 968 break; |
| 969 } |
| 970 |
| 961 for (size_t i = 0; i < kScaleReasonSize; ++i) { | 971 for (size_t i = 0; i < kScaleReasonSize; ++i) { |
| 962 LOG(LS_INFO) << "Scaled " << current_scale_counters[i] | 972 LOG(LS_INFO) << "Scaled " << scale_counters[i] |
| 963 << " times for reason: " << (i ? "cpu" : "quality"); | 973 << " times for reason: " << (i ? "cpu" : "quality"); |
| 964 } | 974 } |
| 965 } | 975 } |
| 966 | 976 |
| 967 const std::vector<int>& ViEEncoder::GetScaleCounters() { | 977 const std::vector<int>& ViEEncoder::GetScaleCounters() { |
| 968 auto it = scale_counters_.find(degradation_preference_); | 978 auto it = scale_counters_.find(degradation_preference_); |
| 969 if (it == scale_counters_.end()) { | 979 if (it == scale_counters_.end()) { |
| 970 scale_counters_[degradation_preference_].resize(kScaleReasonSize); | 980 scale_counters_[degradation_preference_].resize(kScaleReasonSize); |
| 971 return scale_counters_[degradation_preference_]; | 981 return scale_counters_[degradation_preference_]; |
| 972 } | 982 } |
| 973 return it->second; | 983 return it->second; |
| 974 } | 984 } |
| 975 | 985 |
| 976 void ViEEncoder::IncrementScaleCounter(int reason, int delta) { | 986 void ViEEncoder::IncrementScaleCounter(int reason, int delta) { |
| 977 // Get the counters and validate. This may also lazily initialize the state. | 987 // Get the counters and validate. This may also lazily initialize the state. |
| 978 const std::vector<int>& counter = GetScaleCounters(); | 988 const std::vector<int>& counter = GetScaleCounters(); |
| 979 if (delta < 0) { | 989 if (delta < 0) { |
| 980 RTC_DCHECK_GE(counter[reason], delta); | 990 RTC_DCHECK_GE(counter[reason], delta); |
| 981 } | 991 } |
| 982 scale_counters_[degradation_preference_][reason] += delta; | 992 scale_counters_[degradation_preference_][reason] += delta; |
| 983 } | 993 } |
| 984 | 994 |
| 985 } // namespace webrtc | 995 } // namespace webrtc |
| OLD | NEW |