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 int cpu_scale_counter = GetScaleCounters()[reason]; |
kthelgason
2017/04/19 08:23:04
this can be const right?
åsapersson
2017/04/20 13:44:47
Done.
| |
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 | |
924 // degradation preference mode and reason. | |
925 IncrementScaleCounter(reason, -1); | |
926 | |
927 // Get a sum of how many times have scaled down, in total, for this | 919 // 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. | 920 // degradation preference mode. If it is 1, remove any restraints. |
929 const std::vector<int>& current_scale_counters = GetScaleCounters(); | 921 const std::vector<int>& current_scale_counters = GetScaleCounters(); |
930 const int scale_sum = std::accumulate(current_scale_counters.begin(), | 922 const int scale_sum = std::accumulate(current_scale_counters.begin(), |
931 current_scale_counters.end(), 0); | 923 current_scale_counters.end(), 0); |
932 switch (degradation_preference_) { | 924 switch (degradation_preference_) { |
933 case VideoSendStream::DegradationPreference::kBalanced: | 925 case VideoSendStream::DegradationPreference::kBalanced: |
934 FALLTHROUGH(); | 926 FALLTHROUGH(); |
935 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 927 case VideoSendStream::DegradationPreference::kMaintainFramerate: |
936 if (scale_sum == 0) { | 928 if (scale_sum == 1) { |
937 LOG(LS_INFO) << "Removing resolution down-scaling setting."; | 929 LOG(LS_INFO) << "Removing resolution down-scaling setting."; |
938 source_proxy_->RequestHigherResolutionThan( | 930 source_proxy_->RequestHigherResolutionThan( |
939 std::numeric_limits<int>::max()); | 931 std::numeric_limits<int>::max()); |
940 } else { | 932 } else { |
941 source_proxy_->RequestHigherResolutionThan( | 933 source_proxy_->RequestHigherResolutionThan( |
942 adaptation_request.input_pixel_count_); | 934 adaptation_request.input_pixel_count_); |
943 LOG(LS_INFO) << "Scaling up resolution."; | 935 LOG(LS_INFO) << "Scaling up resolution."; |
944 } | 936 } |
945 break; | 937 break; |
946 case VideoSendStream::DegradationPreference::kMaintainResolution: | 938 case VideoSendStream::DegradationPreference::kMaintainResolution: |
947 if (scale_sum == 0) { | 939 if (scale_sum == 1) { |
948 LOG(LS_INFO) << "Removing framerate down-scaling setting."; | 940 LOG(LS_INFO) << "Removing framerate down-scaling setting."; |
949 source_proxy_->RequestHigherFramerateThan( | 941 source_proxy_->RequestHigherFramerateThan( |
950 std::numeric_limits<int>::max()); | 942 std::numeric_limits<int>::max()); |
951 } else { | 943 } else { |
952 source_proxy_->RequestHigherFramerateThan( | 944 source_proxy_->RequestHigherFramerateThan( |
953 adaptation_request.framerate_fps_); | 945 adaptation_request.framerate_fps_); |
954 LOG(LS_INFO) << "Scaling up framerate."; | 946 LOG(LS_INFO) << "Scaling up framerate."; |
955 } | 947 } |
956 break; | 948 break; |
957 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 949 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
958 RTC_NOTREACHED(); | 950 RTC_NOTREACHED(); |
959 } | 951 } |
960 | 952 |
953 last_adaptation_request_.emplace(adaptation_request); | |
954 | |
955 // Decrease counter of how many times we have scaled down, for this | |
956 // degradation preference mode and reason. | |
957 IncrementScaleCounter(reason, -1); | |
kthelgason
2017/04/19 08:23:04
Why move this below the switch-case? IMO it's less
åsapersson
2017/04/20 13:44:47
Moved this up.
| |
958 | |
959 // Update stats. | |
960 const std::vector<int>& scale_counters = GetScaleCounters(); | |
961 switch (reason) { | |
962 case kQuality: | |
963 stats_proxy_->OnQualityRestrictedResolutionChanged( | |
964 scale_counters[reason]); | |
965 break; | |
966 case kCpu: | |
967 stats_proxy_->OnCpuRestrictedResolutionChanged(scale_counters[reason] > | |
968 0); | |
969 break; | |
970 } | |
971 | |
961 for (size_t i = 0; i < kScaleReasonSize; ++i) { | 972 for (size_t i = 0; i < kScaleReasonSize; ++i) { |
962 LOG(LS_INFO) << "Scaled " << current_scale_counters[i] | 973 LOG(LS_INFO) << "Scaled " << scale_counters[i] |
963 << " times for reason: " << (i ? "cpu" : "quality"); | 974 << " times for reason: " << (i ? "cpu" : "quality"); |
964 } | 975 } |
965 } | 976 } |
966 | 977 |
967 const std::vector<int>& ViEEncoder::GetScaleCounters() { | 978 const std::vector<int>& ViEEncoder::GetScaleCounters() { |
968 auto it = scale_counters_.find(degradation_preference_); | 979 auto it = scale_counters_.find(degradation_preference_); |
969 if (it == scale_counters_.end()) { | 980 if (it == scale_counters_.end()) { |
970 scale_counters_[degradation_preference_].resize(kScaleReasonSize); | 981 scale_counters_[degradation_preference_].resize(kScaleReasonSize); |
971 return scale_counters_[degradation_preference_]; | 982 return scale_counters_[degradation_preference_]; |
972 } | 983 } |
973 return it->second; | 984 return it->second; |
974 } | 985 } |
975 | 986 |
976 void ViEEncoder::IncrementScaleCounter(int reason, int delta) { | 987 void ViEEncoder::IncrementScaleCounter(int reason, int delta) { |
977 // Get the counters and validate. This may also lazily initialize the state. | 988 // Get the counters and validate. This may also lazily initialize the state. |
978 const std::vector<int>& counter = GetScaleCounters(); | 989 const std::vector<int>& counter = GetScaleCounters(); |
979 if (delta < 0) { | 990 if (delta < 0) { |
980 RTC_DCHECK_GE(counter[reason], delta); | 991 RTC_DCHECK_GE(counter[reason], delta); |
981 } | 992 } |
982 scale_counters_[degradation_preference_][reason] += delta; | 993 scale_counters_[degradation_preference_][reason] += delta; |
983 } | 994 } |
984 | 995 |
985 } // namespace webrtc | 996 } // namespace webrtc |
OLD | NEW |