jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 9 | */ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 10 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 11 | #include "webrtc/media/base/videoadapter.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
| 13 | #include <limits.h> // For INT_MAX |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 14 | #include <algorithm> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 15 | |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 16 | #include "webrtc/base/logging.h" |
| 17 | #include "webrtc/base/timeutils.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 18 | #include "webrtc/media/base/constants.h" |
| 19 | #include "webrtc/media/base/videocommon.h" |
| 20 | #include "webrtc/media/base/videoframe.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | |
| 22 | namespace cricket { |
| 23 | |
| 24 | // TODO(fbarchard): Make downgrades settable |
| 25 | static const int kMaxCpuDowngrades = 2; // Downgrade at most 2 times for CPU. |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 26 | // The number of cpu samples to require before adapting. This value depends on |
| 27 | // the cpu monitor sampling frequency being 2000ms. |
| 28 | static const int kCpuLoadMinSamples = 3; |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 29 | // The amount of weight to give to each new cpu load sample. The lower the |
| 30 | // value, the slower we'll adapt to changing cpu conditions. |
| 31 | static const float kCpuLoadWeightCoefficient = 0.4f; |
| 32 | // The seed value for the cpu load moving average. |
| 33 | static const float kCpuLoadInitialAverage = 0.5f; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 35 | // Desktop needs 1/8 scale for HD (1280 x 720) to QQVGA (160 x 90) |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 36 | static const float kScaleFactors[] = { |
| 37 | 1.f / 1.f, // Full size. |
| 38 | 3.f / 4.f, // 3/4 scale. |
| 39 | 1.f / 2.f, // 1/2 scale. |
| 40 | 3.f / 8.f, // 3/8 scale. |
| 41 | 1.f / 4.f, // 1/4 scale. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | 3.f / 16.f, // 3/16 scale. |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 43 | 1.f / 8.f, // 1/8 scale. |
| 44 | 0.f // End of table. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | }; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 47 | // TODO(fbarchard): Use this table (optionally) for CPU and GD as well. |
| 48 | static const float kViewScaleFactors[] = { |
| 49 | 1.f / 1.f, // Full size. |
| 50 | 3.f / 4.f, // 3/4 scale. |
| 51 | 2.f / 3.f, // 2/3 scale. // Allow 1080p to 720p. |
| 52 | 1.f / 2.f, // 1/2 scale. |
| 53 | 3.f / 8.f, // 3/8 scale. |
| 54 | 1.f / 3.f, // 1/3 scale. // Allow 1080p to 360p. |
| 55 | 1.f / 4.f, // 1/4 scale. |
| 56 | 3.f / 16.f, // 3/16 scale. |
| 57 | 1.f / 8.f, // 1/8 scale. |
| 58 | 0.f // End of table. |
| 59 | }; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 60 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 61 | const float* VideoAdapter::GetViewScaleFactors() const { |
| 62 | return scale_third_ ? kViewScaleFactors : kScaleFactors; |
| 63 | } |
| 64 | |
| 65 | // For resolutions that would scale down a little instead of up a little, |
| 66 | // bias toward scaling up a little. This will tend to choose 3/4 scale instead |
| 67 | // of 2/3 scale, when the 2/3 is not an exact match. |
| 68 | static const float kUpBias = -0.9f; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | // Find the scale factor that, when applied to width and height, is closest |
| 70 | // to num_pixels. |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 71 | float VideoAdapter::FindScale(const float* scale_factors, |
| 72 | const float upbias, |
| 73 | int width, int height, |
| 74 | int target_num_pixels) { |
| 75 | const float kMinNumPixels = 160 * 90; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 76 | if (!target_num_pixels) { |
| 77 | return 0.f; |
| 78 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 79 | float best_distance = static_cast<float>(INT_MAX); |
| 80 | float best_scale = 1.f; // Default to unscaled if nothing matches. |
| 81 | float pixels = static_cast<float>(width * height); |
| 82 | for (int i = 0; ; ++i) { |
| 83 | float scale = scale_factors[i]; |
| 84 | float test_num_pixels = pixels * scale * scale; |
| 85 | // Do not consider scale factors that produce too small images. |
| 86 | // Scale factor of 0 at end of table will also exit here. |
| 87 | if (test_num_pixels < kMinNumPixels) { |
| 88 | break; |
| 89 | } |
| 90 | float diff = target_num_pixels - test_num_pixels; |
| 91 | // If resolution is higher than desired, bias the difference based on |
| 92 | // preference for slightly larger for nearest, or avoid completely if |
| 93 | // looking for lower resolutions only. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | if (diff < 0) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 95 | diff = diff * kUpBias; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 96 | } |
| 97 | if (diff < best_distance) { |
| 98 | best_distance = diff; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 99 | best_scale = scale; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 100 | if (best_distance == 0) { // Found exact match. |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 105 | return best_scale; |
| 106 | } |
| 107 | |
| 108 | // Find the closest scale factor. |
| 109 | float VideoAdapter::FindClosestScale(int width, int height, |
| 110 | int target_num_pixels) { |
| 111 | return FindScale(kScaleFactors, kUpBias, |
| 112 | width, height, target_num_pixels); |
| 113 | } |
| 114 | |
| 115 | // Find the closest view scale factor. |
| 116 | float VideoAdapter::FindClosestViewScale(int width, int height, |
| 117 | int target_num_pixels) { |
| 118 | return FindScale(GetViewScaleFactors(), kUpBias, |
| 119 | width, height, target_num_pixels); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // Finds the scale factor that, when applied to width and height, produces |
| 123 | // fewer than num_pixels. |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 124 | static const float kUpAvoidBias = -1000000000.f; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | float VideoAdapter::FindLowerScale(int width, int height, |
| 126 | int target_num_pixels) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 127 | return FindScale(GetViewScaleFactors(), kUpAvoidBias, |
| 128 | width, height, target_num_pixels); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // There are several frame sizes used by Adapter. This explains them |
| 132 | // input_format - set once by server to frame size expected from the camera. |
Magnus Jedvert | ac27e20 | 2015-03-24 15:18:39 +0100 | [diff] [blame] | 133 | // The input frame size is also updated in AdaptFrameResolution. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | // output_format - size that output would like to be. Includes framerate. |
Magnus Jedvert | ac27e20 | 2015-03-24 15:18:39 +0100 | [diff] [blame] | 135 | // The output frame size is also updated in AdaptFrameResolution. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 136 | // output_num_pixels - size that output should be constrained to. Used to |
| 137 | // compute output_format from in_frame. |
| 138 | // in_frame - actual camera captured frame size, which is typically the same |
| 139 | // as input_format. This can also be rotated or cropped for aspect ratio. |
| 140 | // out_frame - actual frame output by adapter. Should be a direct scale of |
| 141 | // in_frame maintaining rotation and aspect ratio. |
| 142 | // OnOutputFormatRequest - server requests you send this resolution based on |
| 143 | // view requests. |
| 144 | // OnEncoderResolutionRequest - encoder requests you send this resolution based |
| 145 | // on bandwidth |
| 146 | // OnCpuLoadUpdated - cpu monitor requests you send this resolution based on |
| 147 | // cpu load. |
| 148 | |
| 149 | /////////////////////////////////////////////////////////////////////// |
| 150 | // Implementation of VideoAdapter |
| 151 | VideoAdapter::VideoAdapter() |
| 152 | : output_num_pixels_(INT_MAX), |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 153 | scale_third_(false), |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 154 | frames_in_(0), |
| 155 | frames_out_(0), |
| 156 | frames_scaled_(0), |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 157 | adaption_changes_(0), |
magjed@webrtc.org | a73d746 | 2014-11-14 13:25:25 +0000 | [diff] [blame] | 158 | previous_width_(0), |
| 159 | previous_height_(0), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 160 | interval_next_frame_(0) { |
| 161 | } |
| 162 | |
| 163 | VideoAdapter::~VideoAdapter() { |
| 164 | } |
| 165 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 166 | void VideoAdapter::SetInputFormat(const VideoFormat& format) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 167 | rtc::CritScope cs(&critical_section_); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 168 | int64_t old_input_interval = input_format_.interval; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 169 | input_format_ = format; |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 170 | output_format_.interval = |
| 171 | std::max(output_format_.interval, input_format_.interval); |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 172 | if (old_input_interval != input_format_.interval) { |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 173 | LOG(LS_INFO) << "VAdapt input interval changed from " |
| 174 | << old_input_interval << " to " << input_format_.interval; |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 175 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 176 | } |
| 177 | |
mallinath@webrtc.org | 1b15f42 | 2013-09-06 22:56:28 +0000 | [diff] [blame] | 178 | void CoordinatedVideoAdapter::SetInputFormat(const VideoFormat& format) { |
| 179 | int previous_width = input_format().width; |
| 180 | int previous_height = input_format().height; |
| 181 | bool is_resolution_change = previous_width > 0 && format.width > 0 && |
| 182 | (previous_width != format.width || |
| 183 | previous_height != format.height); |
| 184 | VideoAdapter::SetInputFormat(format); |
| 185 | if (is_resolution_change) { |
| 186 | int width, height; |
| 187 | // Trigger the adaptation logic again, to potentially reset the adaptation |
| 188 | // state for things like view requests that may not longer be capping |
| 189 | // output (or may now cap output). |
| 190 | AdaptToMinimumFormat(&width, &height); |
| 191 | LOG(LS_INFO) << "VAdapt Input Resolution Change: " |
| 192 | << "Previous input resolution: " |
| 193 | << previous_width << "x" << previous_height |
| 194 | << " New input resolution: " |
| 195 | << format.width << "x" << format.height |
| 196 | << " New output resolution: " |
| 197 | << width << "x" << height; |
| 198 | } |
| 199 | } |
| 200 | |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 201 | void CoordinatedVideoAdapter::set_cpu_smoothing(bool enable) { |
| 202 | LOG(LS_INFO) << "CPU smoothing is now " |
| 203 | << (enable ? "enabled" : "disabled"); |
| 204 | cpu_smoothing_ = enable; |
| 205 | } |
| 206 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 207 | void VideoAdapter::SetOutputFormat(const VideoFormat& format) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 208 | rtc::CritScope cs(&critical_section_); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 209 | int64_t old_output_interval = output_format_.interval; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 210 | output_format_ = format; |
| 211 | output_num_pixels_ = output_format_.width * output_format_.height; |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 212 | output_format_.interval = |
| 213 | std::max(output_format_.interval, input_format_.interval); |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 214 | if (old_output_interval != output_format_.interval) { |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 215 | LOG(LS_INFO) << "VAdapt output interval changed from " |
| 216 | << old_output_interval << " to " << output_format_.interval; |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 217 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | const VideoFormat& VideoAdapter::input_format() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 221 | rtc::CritScope cs(&critical_section_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 222 | return input_format_; |
| 223 | } |
| 224 | |
henrike@webrtc.org | 704bf9e | 2014-02-27 17:52:04 +0000 | [diff] [blame] | 225 | bool VideoAdapter::drops_all_frames() const { |
| 226 | return output_num_pixels_ == 0; |
| 227 | } |
| 228 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 229 | const VideoFormat& VideoAdapter::output_format() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 230 | rtc::CritScope cs(&critical_section_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 231 | return output_format_; |
| 232 | } |
| 233 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 234 | // Constrain output resolution to this many pixels overall |
| 235 | void VideoAdapter::SetOutputNumPixels(int num_pixels) { |
| 236 | output_num_pixels_ = num_pixels; |
| 237 | } |
| 238 | |
| 239 | int VideoAdapter::GetOutputNumPixels() const { |
| 240 | return output_num_pixels_; |
| 241 | } |
| 242 | |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 243 | VideoFormat VideoAdapter::AdaptFrameResolution(int in_width, int in_height) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 244 | rtc::CritScope cs(&critical_section_); |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 245 | ++frames_in_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 246 | |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 247 | SetInputFormat(VideoFormat( |
| 248 | in_width, in_height, input_format_.interval, input_format_.fourcc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 249 | |
| 250 | // Drop the input frame if necessary. |
| 251 | bool should_drop = false; |
| 252 | if (!output_num_pixels_) { |
| 253 | // Drop all frames as the output format is 0x0. |
| 254 | should_drop = true; |
| 255 | } else { |
| 256 | // Drop some frames based on input fps and output fps. |
| 257 | // Normally output fps is less than input fps. |
| 258 | // TODO(fbarchard): Consider adjusting interval to reflect the adjusted |
| 259 | // interval between frames after dropping some frames. |
| 260 | interval_next_frame_ += input_format_.interval; |
| 261 | if (output_format_.interval > 0) { |
| 262 | if (interval_next_frame_ >= output_format_.interval) { |
| 263 | interval_next_frame_ %= output_format_.interval; |
| 264 | } else { |
| 265 | should_drop = true; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | if (should_drop) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 270 | // Show VAdapt log every 90 frames dropped. (3 seconds) |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 271 | if ((frames_in_ - frames_out_) % 90 == 0) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 272 | // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed |
| 273 | // in default calls. |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 274 | LOG(LS_INFO) << "VAdapt Drop Frame: scaled " << frames_scaled_ |
| 275 | << " / out " << frames_out_ |
| 276 | << " / in " << frames_in_ |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 277 | << " Changes: " << adaption_changes_ |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 278 | << " Input: " << in_width |
| 279 | << "x" << in_height |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 280 | << " i" << input_format_.interval |
| 281 | << " Output: i" << output_format_.interval; |
| 282 | } |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 283 | |
| 284 | return VideoFormat(); // Drop frame. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 285 | } |
| 286 | |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 287 | const float scale = VideoAdapter::FindClosestViewScale( |
| 288 | in_width, in_height, output_num_pixels_); |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 289 | const size_t output_width = static_cast<size_t>(in_width * scale + .5f); |
| 290 | const size_t output_height = static_cast<size_t>(in_height * scale + .5f); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 291 | |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 292 | ++frames_out_; |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 293 | if (scale != 1) |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 294 | ++frames_scaled_; |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 295 | // Show VAdapt log every 90 frames output. (3 seconds) |
| 296 | // TODO(fbarchard): Consider GetLogSeverity() to change interval to less |
| 297 | // for LS_VERBOSE and more for LS_INFO. |
| 298 | bool show = (frames_out_) % 90 == 0; |
| 299 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 300 | // TODO(fbarchard): LOG the previous output resolution and track input |
| 301 | // resolution changes as well. Consider dropping the statistics into their |
| 302 | // own class which could be queried publically. |
| 303 | bool changed = false; |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 304 | if (previous_width_ && (previous_width_ != output_width || |
| 305 | previous_height_ != output_height)) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 306 | show = true; |
| 307 | ++adaption_changes_; |
| 308 | changed = true; |
| 309 | } |
| 310 | if (show) { |
| 311 | // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed |
| 312 | // in default calls. |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 313 | LOG(LS_INFO) << "VAdapt Frame: scaled " << frames_scaled_ |
| 314 | << " / out " << frames_out_ |
| 315 | << " / in " << frames_in_ |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 316 | << " Changes: " << adaption_changes_ |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 317 | << " Input: " << in_width |
| 318 | << "x" << in_height |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 319 | << " i" << input_format_.interval |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 320 | << " Scale: " << scale |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 321 | << " Output: " << output_width |
| 322 | << "x" << output_height |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 323 | << " i" << output_format_.interval |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 324 | << " Changed: " << (changed ? "true" : "false"); |
| 325 | } |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 326 | |
| 327 | output_format_.width = output_width; |
| 328 | output_format_.height = output_height; |
| 329 | previous_width_ = output_width; |
| 330 | previous_height_ = output_height; |
| 331 | |
| 332 | return output_format_; |
| 333 | } |
| 334 | |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 335 | void VideoAdapter::set_scale_third(bool enable) { |
| 336 | LOG(LS_INFO) << "Video Adapter third scaling is now " |
| 337 | << (enable ? "enabled" : "disabled"); |
| 338 | scale_third_ = enable; |
| 339 | } |
| 340 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 341 | /////////////////////////////////////////////////////////////////////// |
| 342 | // Implementation of CoordinatedVideoAdapter |
| 343 | CoordinatedVideoAdapter::CoordinatedVideoAdapter() |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 344 | : cpu_adaptation_(true), |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 345 | cpu_smoothing_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 346 | gd_adaptation_(true), |
| 347 | view_adaptation_(true), |
| 348 | view_switch_(false), |
| 349 | cpu_downgrade_count_(0), |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 350 | cpu_load_min_samples_(kCpuLoadMinSamples), |
| 351 | cpu_load_num_samples_(0), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 352 | high_system_threshold_(kHighSystemCpuThreshold), |
| 353 | low_system_threshold_(kLowSystemCpuThreshold), |
| 354 | process_threshold_(kProcessCpuThreshold), |
| 355 | view_desired_num_pixels_(INT_MAX), |
| 356 | view_desired_interval_(0), |
| 357 | encoder_desired_num_pixels_(INT_MAX), |
| 358 | cpu_desired_num_pixels_(INT_MAX), |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 359 | adapt_reason_(ADAPTREASON_NONE), |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 360 | system_load_average_(kCpuLoadInitialAverage) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // Helper function to UPGRADE or DOWNGRADE a number of pixels |
| 364 | void CoordinatedVideoAdapter::StepPixelCount( |
| 365 | CoordinatedVideoAdapter::AdaptRequest request, |
| 366 | int* num_pixels) { |
| 367 | switch (request) { |
| 368 | case CoordinatedVideoAdapter::DOWNGRADE: |
| 369 | *num_pixels /= 2; |
| 370 | break; |
| 371 | |
| 372 | case CoordinatedVideoAdapter::UPGRADE: |
| 373 | *num_pixels *= 2; |
| 374 | break; |
| 375 | |
| 376 | default: // No change in pixel count |
| 377 | break; |
| 378 | } |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | // Find the adaptation request of the cpu based on the load. Return UPGRADE if |
| 383 | // the load is low, DOWNGRADE if the load is high, and KEEP otherwise. |
| 384 | CoordinatedVideoAdapter::AdaptRequest CoordinatedVideoAdapter::FindCpuRequest( |
| 385 | int current_cpus, int max_cpus, |
| 386 | float process_load, float system_load) { |
| 387 | // Downgrade if system is high and plugin is at least more than midrange. |
| 388 | if (system_load >= high_system_threshold_ * max_cpus && |
| 389 | process_load >= process_threshold_ * current_cpus) { |
| 390 | return CoordinatedVideoAdapter::DOWNGRADE; |
| 391 | // Upgrade if system is low. |
| 392 | } else if (system_load < low_system_threshold_ * max_cpus) { |
| 393 | return CoordinatedVideoAdapter::UPGRADE; |
| 394 | } |
| 395 | return CoordinatedVideoAdapter::KEEP; |
| 396 | } |
| 397 | |
| 398 | // A remote view request for a new resolution. |
| 399 | void CoordinatedVideoAdapter::OnOutputFormatRequest(const VideoFormat& format) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 400 | rtc::CritScope cs(&request_critical_section_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 401 | if (!view_adaptation_) { |
| 402 | return; |
| 403 | } |
| 404 | // Set output for initial aspect ratio in mediachannel unittests. |
| 405 | int old_num_pixels = GetOutputNumPixels(); |
| 406 | SetOutputFormat(format); |
| 407 | SetOutputNumPixels(old_num_pixels); |
| 408 | view_desired_num_pixels_ = format.width * format.height; |
| 409 | view_desired_interval_ = format.interval; |
| 410 | int new_width, new_height; |
| 411 | bool changed = AdaptToMinimumFormat(&new_width, &new_height); |
| 412 | LOG(LS_INFO) << "VAdapt View Request: " |
| 413 | << format.width << "x" << format.height |
| 414 | << " Pixels: " << view_desired_num_pixels_ |
| 415 | << " Changed: " << (changed ? "true" : "false") |
| 416 | << " To: " << new_width << "x" << new_height; |
| 417 | } |
| 418 | |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 419 | void CoordinatedVideoAdapter::set_cpu_load_min_samples( |
| 420 | int cpu_load_min_samples) { |
| 421 | if (cpu_load_min_samples_ != cpu_load_min_samples) { |
| 422 | LOG(LS_INFO) << "VAdapt Change Cpu Adapt Min Samples from: " |
| 423 | << cpu_load_min_samples_ << " to " |
| 424 | << cpu_load_min_samples; |
| 425 | cpu_load_min_samples_ = cpu_load_min_samples; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | void CoordinatedVideoAdapter::set_high_system_threshold( |
| 430 | float high_system_threshold) { |
| 431 | ASSERT(high_system_threshold <= 1.0f); |
| 432 | ASSERT(high_system_threshold >= 0.0f); |
| 433 | if (high_system_threshold_ != high_system_threshold) { |
| 434 | LOG(LS_INFO) << "VAdapt Change High System Threshold from: " |
| 435 | << high_system_threshold_ << " to " << high_system_threshold; |
| 436 | high_system_threshold_ = high_system_threshold; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | void CoordinatedVideoAdapter::set_low_system_threshold( |
| 441 | float low_system_threshold) { |
| 442 | ASSERT(low_system_threshold <= 1.0f); |
| 443 | ASSERT(low_system_threshold >= 0.0f); |
| 444 | if (low_system_threshold_ != low_system_threshold) { |
| 445 | LOG(LS_INFO) << "VAdapt Change Low System Threshold from: " |
| 446 | << low_system_threshold_ << " to " << low_system_threshold; |
| 447 | low_system_threshold_ = low_system_threshold; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | void CoordinatedVideoAdapter::set_process_threshold(float process_threshold) { |
| 452 | ASSERT(process_threshold <= 1.0f); |
| 453 | ASSERT(process_threshold >= 0.0f); |
| 454 | if (process_threshold_ != process_threshold) { |
| 455 | LOG(LS_INFO) << "VAdapt Change High Process Threshold from: " |
| 456 | << process_threshold_ << " to " << process_threshold; |
| 457 | process_threshold_ = process_threshold; |
| 458 | } |
| 459 | } |
| 460 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 461 | // A Bandwidth GD request for new resolution |
| 462 | void CoordinatedVideoAdapter::OnEncoderResolutionRequest( |
| 463 | int width, int height, AdaptRequest request) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 464 | rtc::CritScope cs(&request_critical_section_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 465 | if (!gd_adaptation_) { |
| 466 | return; |
| 467 | } |
| 468 | int old_encoder_desired_num_pixels = encoder_desired_num_pixels_; |
| 469 | if (KEEP != request) { |
| 470 | int new_encoder_desired_num_pixels = width * height; |
| 471 | int old_num_pixels = GetOutputNumPixels(); |
| 472 | if (new_encoder_desired_num_pixels != old_num_pixels) { |
| 473 | LOG(LS_VERBOSE) << "VAdapt GD resolution stale. Ignored"; |
| 474 | } else { |
| 475 | // Update the encoder desired format based on the request. |
| 476 | encoder_desired_num_pixels_ = new_encoder_desired_num_pixels; |
| 477 | StepPixelCount(request, &encoder_desired_num_pixels_); |
| 478 | } |
| 479 | } |
| 480 | int new_width, new_height; |
| 481 | bool changed = AdaptToMinimumFormat(&new_width, &new_height); |
| 482 | |
| 483 | // Ignore up or keep if no change. |
| 484 | if (DOWNGRADE != request && view_switch_ && !changed) { |
| 485 | encoder_desired_num_pixels_ = old_encoder_desired_num_pixels; |
| 486 | LOG(LS_VERBOSE) << "VAdapt ignoring GD request."; |
| 487 | } |
| 488 | |
| 489 | LOG(LS_INFO) << "VAdapt GD Request: " |
| 490 | << (DOWNGRADE == request ? "down" : |
| 491 | (UPGRADE == request ? "up" : "keep")) |
| 492 | << " From: " << width << "x" << height |
| 493 | << " Pixels: " << encoder_desired_num_pixels_ |
| 494 | << " Changed: " << (changed ? "true" : "false") |
| 495 | << " To: " << new_width << "x" << new_height; |
| 496 | } |
| 497 | |
perkj | 2d5f091 | 2016-02-29 00:04:41 -0800 | [diff] [blame^] | 498 | void CoordinatedVideoAdapter::OnCpuResolutionRequest( |
| 499 | rtc::Optional<int> max_pixel_count, |
| 500 | rtc::Optional<int> max_pixel_count_step_up) { |
| 501 | rtc::CritScope cs(&request_critical_section_); |
| 502 | // TODO(perkj): We should support taking larger steps up and down and |
| 503 | // actually look at the values set in max_pixel_count and |
| 504 | // max_pixel_count_step_up. |
| 505 | if (max_pixel_count && *max_pixel_count < GetOutputNumPixels()) { |
| 506 | OnCpuResolutionRequest(DOWNGRADE); |
| 507 | } else if (max_pixel_count_step_up && |
| 508 | *max_pixel_count_step_up >= GetOutputNumPixels()) { |
| 509 | OnCpuResolutionRequest(UPGRADE); |
| 510 | } |
| 511 | } |
| 512 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 513 | // A Bandwidth GD request for new resolution |
| 514 | void CoordinatedVideoAdapter::OnCpuResolutionRequest(AdaptRequest request) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 515 | rtc::CritScope cs(&request_critical_section_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 516 | if (!cpu_adaptation_) { |
| 517 | return; |
| 518 | } |
perkj | 2d5f091 | 2016-02-29 00:04:41 -0800 | [diff] [blame^] | 519 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 520 | // Update how many times we have downgraded due to the cpu load. |
| 521 | switch (request) { |
| 522 | case DOWNGRADE: |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 523 | // Ignore downgrades if we have downgraded the maximum times. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 524 | if (cpu_downgrade_count_ < kMaxCpuDowngrades) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 525 | ++cpu_downgrade_count_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 526 | } else { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 527 | LOG(LS_VERBOSE) << "VAdapt CPU load high but do not downgrade " |
| 528 | "because maximum downgrades reached"; |
| 529 | SignalCpuAdaptationUnable(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 530 | } |
| 531 | break; |
| 532 | case UPGRADE: |
| 533 | if (cpu_downgrade_count_ > 0) { |
| 534 | bool is_min = IsMinimumFormat(cpu_desired_num_pixels_); |
| 535 | if (is_min) { |
| 536 | --cpu_downgrade_count_; |
| 537 | } else { |
| 538 | LOG(LS_VERBOSE) << "VAdapt CPU load low but do not upgrade " |
| 539 | "because cpu is not limiting resolution"; |
| 540 | } |
| 541 | } else { |
| 542 | LOG(LS_VERBOSE) << "VAdapt CPU load low but do not upgrade " |
| 543 | "because minimum downgrades reached"; |
| 544 | } |
| 545 | break; |
| 546 | case KEEP: |
| 547 | default: |
| 548 | break; |
| 549 | } |
| 550 | if (KEEP != request) { |
| 551 | // TODO(fbarchard): compute stepping up/down from OutputNumPixels but |
| 552 | // clamp to inputpixels / 4 (2 steps) |
| 553 | cpu_desired_num_pixels_ = cpu_downgrade_count_ == 0 ? INT_MAX : |
| 554 | static_cast<int>(input_format().width * input_format().height >> |
| 555 | cpu_downgrade_count_); |
| 556 | } |
| 557 | int new_width, new_height; |
| 558 | bool changed = AdaptToMinimumFormat(&new_width, &new_height); |
| 559 | LOG(LS_INFO) << "VAdapt CPU Request: " |
| 560 | << (DOWNGRADE == request ? "down" : |
| 561 | (UPGRADE == request ? "up" : "keep")) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 562 | << " Steps: " << cpu_downgrade_count_ |
| 563 | << " Changed: " << (changed ? "true" : "false") |
| 564 | << " To: " << new_width << "x" << new_height; |
| 565 | } |
| 566 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 567 | // A CPU request for new resolution |
| 568 | // TODO(fbarchard): Move outside adapter. |
| 569 | void CoordinatedVideoAdapter::OnCpuLoadUpdated( |
| 570 | int current_cpus, int max_cpus, float process_load, float system_load) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 571 | rtc::CritScope cs(&request_critical_section_); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 572 | if (!cpu_adaptation_) { |
| 573 | return; |
| 574 | } |
| 575 | // Update the moving average of system load. Even if we aren't smoothing, |
| 576 | // we'll still calculate this information, in case smoothing is later enabled. |
| 577 | system_load_average_ = kCpuLoadWeightCoefficient * system_load + |
| 578 | (1.0f - kCpuLoadWeightCoefficient) * system_load_average_; |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 579 | ++cpu_load_num_samples_; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 580 | if (cpu_smoothing_) { |
| 581 | system_load = system_load_average_; |
| 582 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 583 | AdaptRequest request = FindCpuRequest(current_cpus, max_cpus, |
| 584 | process_load, system_load); |
| 585 | // Make sure we're not adapting too quickly. |
| 586 | if (request != KEEP) { |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 587 | if (cpu_load_num_samples_ < cpu_load_min_samples_) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 588 | LOG(LS_VERBOSE) << "VAdapt CPU load high/low but do not adapt until " |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 589 | << (cpu_load_min_samples_ - cpu_load_num_samples_) |
| 590 | << " more samples"; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 591 | request = KEEP; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | OnCpuResolutionRequest(request); |
| 596 | } |
| 597 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 598 | // Called by cpu adapter on up requests. |
| 599 | bool CoordinatedVideoAdapter::IsMinimumFormat(int pixels) { |
| 600 | // Find closest scale factor that matches input resolution to min_num_pixels |
| 601 | // and set that for output resolution. This is not needed for VideoAdapter, |
| 602 | // but provides feedback to unittests and users on expected resolution. |
| 603 | // Actual resolution is based on input frame. |
| 604 | VideoFormat new_output = output_format(); |
| 605 | VideoFormat input = input_format(); |
| 606 | if (input_format().IsSize0x0()) { |
| 607 | input = new_output; |
| 608 | } |
| 609 | float scale = 1.0f; |
| 610 | if (!input.IsSize0x0()) { |
| 611 | scale = FindClosestScale(input.width, |
| 612 | input.height, |
| 613 | pixels); |
| 614 | } |
| 615 | new_output.width = static_cast<int>(input.width * scale + .5f); |
| 616 | new_output.height = static_cast<int>(input.height * scale + .5f); |
| 617 | int new_pixels = new_output.width * new_output.height; |
| 618 | int num_pixels = GetOutputNumPixels(); |
| 619 | return new_pixels <= num_pixels; |
| 620 | } |
| 621 | |
| 622 | // Called by all coordinators when there is a change. |
| 623 | bool CoordinatedVideoAdapter::AdaptToMinimumFormat(int* new_width, |
| 624 | int* new_height) { |
| 625 | VideoFormat new_output = output_format(); |
| 626 | VideoFormat input = input_format(); |
| 627 | if (input_format().IsSize0x0()) { |
| 628 | input = new_output; |
| 629 | } |
| 630 | int old_num_pixels = GetOutputNumPixels(); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 631 | int min_num_pixels = INT_MAX; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 632 | adapt_reason_ = ADAPTREASON_NONE; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 633 | |
| 634 | // Reduce resolution based on encoder bandwidth (GD). |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 635 | if (encoder_desired_num_pixels_ && |
| 636 | (encoder_desired_num_pixels_ < min_num_pixels)) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 637 | adapt_reason_ |= ADAPTREASON_BANDWIDTH; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 638 | min_num_pixels = encoder_desired_num_pixels_; |
| 639 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 640 | // Reduce resolution based on CPU. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 641 | if (cpu_adaptation_ && cpu_desired_num_pixels_ && |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 642 | (cpu_desired_num_pixels_ <= min_num_pixels)) { |
| 643 | if (cpu_desired_num_pixels_ < min_num_pixels) { |
| 644 | adapt_reason_ = ADAPTREASON_CPU; |
| 645 | } else { |
| 646 | adapt_reason_ |= ADAPTREASON_CPU; |
| 647 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 648 | min_num_pixels = cpu_desired_num_pixels_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 649 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 650 | // Round resolution for GD or CPU to allow 1/2 to map to 9/16. |
| 651 | if (!input.IsSize0x0() && min_num_pixels != INT_MAX) { |
| 652 | float scale = FindClosestScale(input.width, input.height, min_num_pixels); |
| 653 | min_num_pixels = static_cast<int>(input.width * scale + .5f) * |
| 654 | static_cast<int>(input.height * scale + .5f); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 655 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 656 | // Reduce resolution based on View Request. |
| 657 | if (view_desired_num_pixels_ <= min_num_pixels) { |
| 658 | if (view_desired_num_pixels_ < min_num_pixels) { |
| 659 | adapt_reason_ = ADAPTREASON_VIEW; |
| 660 | } else { |
| 661 | adapt_reason_ |= ADAPTREASON_VIEW; |
| 662 | } |
| 663 | min_num_pixels = view_desired_num_pixels_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 664 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 665 | // Snap to a scale factor. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 666 | float scale = 1.0f; |
| 667 | if (!input.IsSize0x0()) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 668 | scale = FindLowerScale(input.width, input.height, min_num_pixels); |
| 669 | min_num_pixels = static_cast<int>(input.width * scale + .5f) * |
| 670 | static_cast<int>(input.height * scale + .5f); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 671 | } |
| 672 | if (scale == 1.0f) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 673 | adapt_reason_ = ADAPTREASON_NONE; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 674 | } |
| 675 | *new_width = new_output.width = static_cast<int>(input.width * scale + .5f); |
| 676 | *new_height = new_output.height = static_cast<int>(input.height * scale + |
| 677 | .5f); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 678 | SetOutputNumPixels(min_num_pixels); |
| 679 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 680 | new_output.interval = view_desired_interval_; |
| 681 | SetOutputFormat(new_output); |
| 682 | int new_num_pixels = GetOutputNumPixels(); |
| 683 | bool changed = new_num_pixels != old_num_pixels; |
| 684 | |
| 685 | static const char* kReasons[8] = { |
| 686 | "None", |
| 687 | "CPU", |
| 688 | "BANDWIDTH", |
| 689 | "CPU+BANDWIDTH", |
| 690 | "VIEW", |
| 691 | "CPU+VIEW", |
| 692 | "BANDWIDTH+VIEW", |
| 693 | "CPU+BANDWIDTH+VIEW", |
| 694 | }; |
| 695 | |
| 696 | LOG(LS_VERBOSE) << "VAdapt Status View: " << view_desired_num_pixels_ |
| 697 | << " GD: " << encoder_desired_num_pixels_ |
| 698 | << " CPU: " << cpu_desired_num_pixels_ |
| 699 | << " Pixels: " << min_num_pixels |
| 700 | << " Input: " << input.width |
| 701 | << "x" << input.height |
| 702 | << " Scale: " << scale |
| 703 | << " Resolution: " << new_output.width |
| 704 | << "x" << new_output.height |
| 705 | << " Changed: " << (changed ? "true" : "false") |
| 706 | << " Reason: " << kReasons[adapt_reason_]; |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 707 | |
| 708 | if (changed) { |
| 709 | // When any adaptation occurs, historic CPU load levels are no longer |
| 710 | // accurate. Clear out our state so we can re-learn at the new normal. |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 711 | cpu_load_num_samples_ = 0; |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 712 | system_load_average_ = kCpuLoadInitialAverage; |
| 713 | } |
| 714 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 715 | return changed; |
| 716 | } |
| 717 | |
| 718 | } // namespace cricket |