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 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
223 wants.max_pixel_count = std::numeric_limits<int>::max(); | 223 wants.max_pixel_count = std::numeric_limits<int>::max(); |
224 wants.target_pixel_count.reset(); | 224 wants.target_pixel_count.reset(); |
225 wants.max_framerate_fps = std::numeric_limits<int>::max(); | 225 wants.max_framerate_fps = std::numeric_limits<int>::max(); |
226 } | 226 } |
227 return wants; | 227 return wants; |
228 } | 228 } |
229 | 229 |
230 bool RequestResolutionLowerThan(int pixel_count) { | 230 bool RequestResolutionLowerThan(int pixel_count) { |
231 // Called on the encoder task queue. | 231 // Called on the encoder task queue. |
232 rtc::CritScope lock(&crit_); | 232 rtc::CritScope lock(&crit_); |
233 if (!IsResolutionScalingEnabledLocked()) { | 233 if (!source_ || !IsResolutionScalingEnabledLocked()) { |
234 // This can happen since |degradation_preference_| is set on libjingle's | 234 // This can happen since |degradation_preference_| is set on libjingle's |
235 // worker thread but the adaptation is done on the encoder task queue. | 235 // worker thread but the adaptation is done on the encoder task queue. |
236 return false; | 236 return false; |
237 } | 237 } |
238 // The input video frame size will have a resolution with less than or | 238 // The input video frame size will have a resolution less than or equal to |
239 // equal to |max_pixel_count| depending on how the source can scale the | 239 // |max_pixel_count| depending on how the source can scale the frame size. |
240 // input frame size. | |
241 const int pixels_wanted = (pixel_count * 3) / 5; | 240 const int pixels_wanted = (pixel_count * 3) / 5; |
242 if (pixels_wanted < kMinPixelsPerFrame) | 241 if (pixels_wanted < kMinPixelsPerFrame || |
242 pixels_wanted >= sink_wants_.max_pixel_count) { | |
243 return false; | 243 return false; |
244 | 244 } |
245 LOG(LS_INFO) << "Scaling down resolution."; | |
sprang_webrtc
2017/05/29 14:45:47
Might be nice with the actual pixel counts in the
åsapersson
2017/05/30 13:57:31
Done.
| |
245 sink_wants_.max_pixel_count = pixels_wanted; | 246 sink_wants_.max_pixel_count = pixels_wanted; |
246 sink_wants_.target_pixel_count = rtc::Optional<int>(); | 247 sink_wants_.target_pixel_count = rtc::Optional<int>(); |
247 if (source_) | 248 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); |
248 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | |
249 return true; | 249 return true; |
250 } | 250 } |
251 | 251 |
252 void RequestFramerateLowerThan(int framerate_fps) { | 252 bool RequestFramerateLowerThan(int fps) { |
253 // Called on the encoder task queue. | |
sprang_webrtc
2017/05/29 14:45:47
Can we add thread/sequence checkers for this maybe
åsapersson
2017/05/30 13:57:30
I can have a look but maybe in a separate cl?
sprang_webrtc
2017/05/31 08:44:30
Sure, maybe you can add it to one of the upcoming
| |
254 // The input video frame rate will be scaled down to 2/3, rounding down. | |
255 return RestrictFramerate((fps * 2) / 3); | |
256 } | |
257 | |
258 bool RequestHigherResolutionThan(int pixel_count) { | |
253 // Called on the encoder task queue. | 259 // Called on the encoder task queue. |
254 rtc::CritScope lock(&crit_); | 260 rtc::CritScope lock(&crit_); |
255 if (!IsFramerateScalingEnabledLocked()) { | 261 if (!source_ || !IsResolutionScalingEnabledLocked()) { |
256 // This can happen since |degradation_preference_| is set on libjingle's | 262 // This can happen since |degradation_preference_| is set on libjingle's |
257 // worker thread but the adaptation is done on the encoder task queue. | 263 // worker thread but the adaptation is done on the encoder task queue. |
258 return; | 264 return false; |
259 } | 265 } |
260 // The input video frame rate will be scaled down to 2/3 of input fps, | 266 int max_pixels_wanted = pixel_count; |
261 // rounding down. | 267 if (max_pixels_wanted != std::numeric_limits<int>::max()) |
262 const int framerate_wanted = | 268 max_pixels_wanted = pixel_count * 4; |
263 std::max(kMinFramerateFps, (framerate_fps * 2) / 3); | |
264 sink_wants_.max_framerate_fps = framerate_wanted; | |
265 if (source_) | |
266 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | |
267 } | |
268 | 269 |
269 void RequestHigherResolutionThan(int pixel_count) { | 270 if (max_pixels_wanted <= sink_wants_.max_pixel_count) |
270 rtc::CritScope lock(&crit_); | 271 return false; |
sprang_webrtc
2017/05/29 14:45:47
When would this happen? Should we reset sink_wants
åsapersson
2017/05/30 13:57:31
In balanced mode (https://codereview.webrtc.org/28
sprang_webrtc
2017/05/31 08:44:31
Ack. Maybe add comment about that?
| |
271 if (!IsResolutionScalingEnabledLocked()) { | |
272 // This can happen since |degradation_preference_| is set on libjingle's | |
273 // worker thread but the adaptation is done on the encoder task queue. | |
274 return; | |
275 } | |
276 | 272 |
277 if (pixel_count == std::numeric_limits<int>::max()) { | 273 sink_wants_.max_pixel_count = max_pixels_wanted; |
274 if (max_pixels_wanted == std::numeric_limits<int>::max()) { | |
278 // Remove any constraints. | 275 // Remove any constraints. |
279 sink_wants_.target_pixel_count.reset(); | 276 sink_wants_.target_pixel_count.reset(); |
280 sink_wants_.max_pixel_count = std::numeric_limits<int>::max(); | |
281 } else { | 277 } else { |
282 // On step down we request at most 3/5 the pixel count of the previous | 278 // On step down we request at most 3/5 the pixel count of the previous |
283 // resolution, so in order to take "one step up" we request a resolution | 279 // resolution, so in order to take "one step up" we request a resolution |
284 // as close as possible to 5/3 of the current resolution. The actual pixel | 280 // as close as possible to 5/3 of the current resolution. The actual pixel |
285 // count selected depends on the capabilities of the source. In order to | 281 // count selected depends on the capabilities of the source. In order to |
286 // not take a too large step up, we cap the requested pixel count to be at | 282 // not take a too large step up, we cap the requested pixel count to be at |
287 // most four time the current number of pixels. | 283 // most four time the current number of pixels. |
288 sink_wants_.target_pixel_count = | 284 sink_wants_.target_pixel_count = |
289 rtc::Optional<int>((pixel_count * 5) / 3); | 285 rtc::Optional<int>((pixel_count * 5) / 3); |
290 sink_wants_.max_pixel_count = pixel_count * 4; | |
291 } | 286 } |
292 if (source_) | 287 LOG(LS_INFO) << "Scaling up resolution."; |
293 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | 288 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); |
289 return true; | |
294 } | 290 } |
295 | 291 |
296 void RequestHigherFramerateThan(int framerate_fps) { | 292 bool RequestHigherFramerateThan(int fps) { |
293 // Called on the encoder task queue. | |
294 // The input frame rate will be scaled up to the last step, with rounding. | |
295 int framerate_wanted = fps; | |
296 if (fps != std::numeric_limits<int>::max()) | |
297 framerate_wanted = (fps * 3) / 2; | |
298 | |
299 return IncreaseFramerate(framerate_wanted); | |
300 } | |
301 | |
302 bool RestrictFramerate(int fps) { | |
297 // Called on the encoder task queue. | 303 // Called on the encoder task queue. |
298 rtc::CritScope lock(&crit_); | 304 rtc::CritScope lock(&crit_); |
299 if (!IsFramerateScalingEnabledLocked()) { | 305 if (!source_ || !IsFramerateScalingEnabledLocked()) |
300 // This can happen since |degradation_preference_| is set on libjingle's | 306 return false; |
301 // worker thread but the adaptation is done on the encoder task queue. | 307 |
302 return; | 308 const int fps_wanted = std::max(kMinFramerateFps, fps); |
303 } | 309 if (fps_wanted >= sink_wants_.max_framerate_fps) |
304 if (framerate_fps == std::numeric_limits<int>::max()) { | 310 return false; |
305 // Remove any restrains. | 311 |
306 sink_wants_.max_framerate_fps = std::numeric_limits<int>::max(); | 312 LOG(LS_INFO) << "Scaling down framerate: " << fps_wanted; |
307 } else { | 313 sink_wants_.max_framerate_fps = fps_wanted; |
308 // The input video frame rate will be scaled up to the last step, with | 314 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); |
309 // rounding. | 315 return true; |
310 const int framerate_wanted = (framerate_fps * 3) / 2; | 316 } |
311 sink_wants_.max_framerate_fps = framerate_wanted; | 317 |
312 } | 318 bool IncreaseFramerate(int fps) { |
313 if (source_) | 319 // Called on the encoder task queue. |
314 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | 320 rtc::CritScope lock(&crit_); |
321 if (!source_ || !IsFramerateScalingEnabledLocked()) | |
322 return false; | |
323 | |
324 const int fps_wanted = std::max(kMinFramerateFps, fps); | |
325 if (fps_wanted <= sink_wants_.max_framerate_fps) | |
326 return false; | |
327 | |
328 LOG(LS_INFO) << "Scaling up framerate: " << fps_wanted; | |
329 sink_wants_.max_framerate_fps = fps_wanted; | |
330 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | |
331 return true; | |
315 } | 332 } |
316 | 333 |
317 private: | 334 private: |
318 bool IsResolutionScalingEnabledLocked() const | 335 bool IsResolutionScalingEnabledLocked() const |
319 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | 336 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { |
320 return degradation_preference_ == | 337 return degradation_preference_ == |
321 VideoSendStream::DegradationPreference::kMaintainFramerate || | 338 VideoSendStream::DegradationPreference::kMaintainFramerate || |
322 degradation_preference_ == | 339 degradation_preference_ == |
323 VideoSendStream::DegradationPreference::kBalanced; | 340 VideoSendStream::DegradationPreference::kBalanced; |
324 } | 341 } |
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
848 | 865 |
849 if (reason == kCpu) { | 866 if (reason == kCpu) { |
850 if (GetConstAdaptCounter().TotalCount(kCpu) >= max_downgrades) | 867 if (GetConstAdaptCounter().TotalCount(kCpu) >= max_downgrades) |
851 return; | 868 return; |
852 } | 869 } |
853 | 870 |
854 switch (degradation_preference_) { | 871 switch (degradation_preference_) { |
855 case VideoSendStream::DegradationPreference::kBalanced: | 872 case VideoSendStream::DegradationPreference::kBalanced: |
856 FALLTHROUGH(); | 873 FALLTHROUGH(); |
857 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 874 case VideoSendStream::DegradationPreference::kMaintainFramerate: |
875 // Scale down resolution. | |
858 if (!source_proxy_->RequestResolutionLowerThan( | 876 if (!source_proxy_->RequestResolutionLowerThan( |
859 adaptation_request.input_pixel_count_)) { | 877 adaptation_request.input_pixel_count_)) { |
860 return; | 878 return; |
861 } | 879 } |
862 LOG(LS_INFO) << "Scaling down resolution."; | |
863 GetAdaptCounter().IncrementResolution(reason, 1); | 880 GetAdaptCounter().IncrementResolution(reason, 1); |
864 break; | 881 break; |
865 case VideoSendStream::DegradationPreference::kMaintainResolution: | 882 case VideoSendStream::DegradationPreference::kMaintainResolution: |
866 source_proxy_->RequestFramerateLowerThan( | 883 // Scale down framerate. |
867 adaptation_request.framerate_fps_); | 884 if (!source_proxy_->RequestFramerateLowerThan( |
868 LOG(LS_INFO) << "Scaling down framerate."; | 885 adaptation_request.framerate_fps_)) { |
886 return; | |
887 } | |
869 GetAdaptCounter().IncrementFramerate(reason, 1); | 888 GetAdaptCounter().IncrementFramerate(reason, 1); |
870 break; | 889 break; |
871 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 890 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
872 RTC_NOTREACHED(); | 891 RTC_NOTREACHED(); |
873 } | 892 } |
874 | 893 |
875 last_adaptation_request_.emplace(adaptation_request); | 894 last_adaptation_request_.emplace(adaptation_request); |
876 | 895 |
877 UpdateAdaptationStats(reason); | 896 UpdateAdaptationStats(reason); |
878 | 897 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
913 // TODO(sprang): Don't request higher framerate if we are already at | 932 // TODO(sprang): Don't request higher framerate if we are already at |
914 // max requested fps? | 933 // max requested fps? |
915 break; | 934 break; |
916 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 935 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
917 return; | 936 return; |
918 } | 937 } |
919 | 938 |
920 switch (degradation_preference_) { | 939 switch (degradation_preference_) { |
921 case VideoSendStream::DegradationPreference::kBalanced: | 940 case VideoSendStream::DegradationPreference::kBalanced: |
922 FALLTHROUGH(); | 941 FALLTHROUGH(); |
923 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 942 case VideoSendStream::DegradationPreference::kMaintainFramerate: { |
924 if (adapt_counter.TotalCount() == 1) { | 943 // Scale up resolution. |
944 int pixel_count = adaptation_request.input_pixel_count_; | |
945 if (adapt_counter.ResolutionCount() == 1) { | |
925 LOG(LS_INFO) << "Removing resolution down-scaling setting."; | 946 LOG(LS_INFO) << "Removing resolution down-scaling setting."; |
926 source_proxy_->RequestHigherResolutionThan( | 947 pixel_count = std::numeric_limits<int>::max(); |
927 std::numeric_limits<int>::max()); | |
928 } else { | |
929 source_proxy_->RequestHigherResolutionThan( | |
930 adaptation_request.input_pixel_count_); | |
931 LOG(LS_INFO) << "Scaling up resolution."; | |
932 } | 948 } |
949 if (!source_proxy_->RequestHigherResolutionThan(pixel_count)) | |
950 return; | |
933 GetAdaptCounter().IncrementResolution(reason, -1); | 951 GetAdaptCounter().IncrementResolution(reason, -1); |
934 break; | 952 break; |
935 case VideoSendStream::DegradationPreference::kMaintainResolution: | 953 } |
936 if (adapt_counter.TotalCount() == 1) { | 954 case VideoSendStream::DegradationPreference::kMaintainResolution: { |
955 // Scale up framerate. | |
956 int fps = adaptation_request.framerate_fps_; | |
957 if (adapt_counter.FramerateCount() == 1) { | |
937 LOG(LS_INFO) << "Removing framerate down-scaling setting."; | 958 LOG(LS_INFO) << "Removing framerate down-scaling setting."; |
938 source_proxy_->RequestHigherFramerateThan( | 959 fps = std::numeric_limits<int>::max(); |
939 std::numeric_limits<int>::max()); | |
940 } else { | |
941 source_proxy_->RequestHigherFramerateThan( | |
942 adaptation_request.framerate_fps_); | |
943 LOG(LS_INFO) << "Scaling up framerate."; | |
944 } | 960 } |
961 if (!source_proxy_->RequestHigherFramerateThan(fps)) | |
962 return; | |
945 GetAdaptCounter().IncrementFramerate(reason, -1); | 963 GetAdaptCounter().IncrementFramerate(reason, -1); |
946 break; | 964 break; |
965 } | |
947 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 966 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
948 RTC_NOTREACHED(); | 967 RTC_NOTREACHED(); |
949 } | 968 } |
950 | 969 |
951 last_adaptation_request_.emplace(adaptation_request); | 970 last_adaptation_request_.emplace(adaptation_request); |
952 | 971 |
953 UpdateAdaptationStats(reason); | 972 UpdateAdaptationStats(reason); |
954 | 973 |
955 LOG(LS_INFO) << adapt_counter.ToString(); | 974 LOG(LS_INFO) << adapt_counter.ToString(); |
956 } | 975 } |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1060 std::string ViEEncoder::AdaptCounter::ToString( | 1079 std::string ViEEncoder::AdaptCounter::ToString( |
1061 const std::vector<int>& counters) const { | 1080 const std::vector<int>& counters) const { |
1062 std::stringstream ss; | 1081 std::stringstream ss; |
1063 for (size_t reason = 0; reason < kScaleReasonSize; ++reason) { | 1082 for (size_t reason = 0; reason < kScaleReasonSize; ++reason) { |
1064 ss << (reason ? " cpu" : "quality") << ":" << counters[reason]; | 1083 ss << (reason ? " cpu" : "quality") << ":" << counters[reason]; |
1065 } | 1084 } |
1066 return ss.str(); | 1085 return ss.str(); |
1067 } | 1086 } |
1068 | 1087 |
1069 } // namespace webrtc | 1088 } // namespace webrtc |
OLD | NEW |