henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | // libjingle |
| 2 | // Copyright 2010 Google Inc. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // 1. Redistributions of source code must retain the above copyright notice, |
| 8 | // this list of conditions and the following disclaimer. |
| 9 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | // this list of conditions and the following disclaimer in the documentation |
| 11 | // and/or other materials provided with the distribution. |
| 12 | // 3. The name of the author may not be used to endorse or promote products |
| 13 | // derived from this software without specific prior written permission. |
| 14 | // |
| 15 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 16 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 17 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 18 | // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 21 | // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 22 | // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 | // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 24 | // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | |
| 26 | #include "talk/media/base/videoadapter.h" |
| 27 | |
| 28 | #include <limits.h> // For INT_MAX |
| 29 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 30 | #include "talk/base/logging.h" |
| 31 | #include "talk/base/timeutils.h" |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 32 | #include "talk/media/base/constants.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | #include "talk/media/base/videoframe.h" |
| 34 | |
| 35 | namespace cricket { |
| 36 | |
| 37 | // TODO(fbarchard): Make downgrades settable |
| 38 | static const int kMaxCpuDowngrades = 2; // Downgrade at most 2 times for CPU. |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 39 | // The number of milliseconds of data to require before acting on cpu sampling |
| 40 | // information. |
| 41 | static const size_t kCpuLoadMinSampleTime = 5000; |
| 42 | // The amount of weight to give to each new cpu load sample. The lower the |
| 43 | // value, the slower we'll adapt to changing cpu conditions. |
| 44 | static const float kCpuLoadWeightCoefficient = 0.4f; |
| 45 | // The seed value for the cpu load moving average. |
| 46 | static const float kCpuLoadInitialAverage = 0.5f; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 47 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 48 | // 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] | 49 | static const float kScaleFactors[] = { |
| 50 | 1.f / 1.f, // Full size. |
| 51 | 3.f / 4.f, // 3/4 scale. |
| 52 | 1.f / 2.f, // 1/2 scale. |
| 53 | 3.f / 8.f, // 3/8 scale. |
| 54 | 1.f / 4.f, // 1/4 scale. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 55 | 3.f / 16.f, // 3/16 scale. |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 56 | 1.f / 8.f, // 1/8 scale. |
| 57 | 0.f // End of table. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | }; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 60 | // TODO(fbarchard): Use this table (optionally) for CPU and GD as well. |
| 61 | static const float kViewScaleFactors[] = { |
| 62 | 1.f / 1.f, // Full size. |
| 63 | 3.f / 4.f, // 3/4 scale. |
| 64 | 2.f / 3.f, // 2/3 scale. // Allow 1080p to 720p. |
| 65 | 1.f / 2.f, // 1/2 scale. |
| 66 | 3.f / 8.f, // 3/8 scale. |
| 67 | 1.f / 3.f, // 1/3 scale. // Allow 1080p to 360p. |
| 68 | 1.f / 4.f, // 1/4 scale. |
| 69 | 3.f / 16.f, // 3/16 scale. |
| 70 | 1.f / 8.f, // 1/8 scale. |
| 71 | 0.f // End of table. |
| 72 | }; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 73 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 74 | const float* VideoAdapter::GetViewScaleFactors() const { |
| 75 | return scale_third_ ? kViewScaleFactors : kScaleFactors; |
| 76 | } |
| 77 | |
| 78 | // For resolutions that would scale down a little instead of up a little, |
| 79 | // bias toward scaling up a little. This will tend to choose 3/4 scale instead |
| 80 | // of 2/3 scale, when the 2/3 is not an exact match. |
| 81 | static const float kUpBias = -0.9f; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | // Find the scale factor that, when applied to width and height, is closest |
| 83 | // to num_pixels. |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 84 | float VideoAdapter::FindScale(const float* scale_factors, |
| 85 | const float upbias, |
| 86 | int width, int height, |
| 87 | int target_num_pixels) { |
| 88 | const float kMinNumPixels = 160 * 90; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | if (!target_num_pixels) { |
| 90 | return 0.f; |
| 91 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 92 | float best_distance = static_cast<float>(INT_MAX); |
| 93 | float best_scale = 1.f; // Default to unscaled if nothing matches. |
| 94 | float pixels = static_cast<float>(width * height); |
| 95 | for (int i = 0; ; ++i) { |
| 96 | float scale = scale_factors[i]; |
| 97 | float test_num_pixels = pixels * scale * scale; |
| 98 | // Do not consider scale factors that produce too small images. |
| 99 | // Scale factor of 0 at end of table will also exit here. |
| 100 | if (test_num_pixels < kMinNumPixels) { |
| 101 | break; |
| 102 | } |
| 103 | float diff = target_num_pixels - test_num_pixels; |
| 104 | // If resolution is higher than desired, bias the difference based on |
| 105 | // preference for slightly larger for nearest, or avoid completely if |
| 106 | // looking for lower resolutions only. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | if (diff < 0) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 108 | diff = diff * kUpBias; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | } |
| 110 | if (diff < best_distance) { |
| 111 | best_distance = diff; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 112 | best_scale = scale; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | if (best_distance == 0) { // Found exact match. |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 118 | return best_scale; |
| 119 | } |
| 120 | |
| 121 | // Find the closest scale factor. |
| 122 | float VideoAdapter::FindClosestScale(int width, int height, |
| 123 | int target_num_pixels) { |
| 124 | return FindScale(kScaleFactors, kUpBias, |
| 125 | width, height, target_num_pixels); |
| 126 | } |
| 127 | |
| 128 | // Find the closest view scale factor. |
| 129 | float VideoAdapter::FindClosestViewScale(int width, int height, |
| 130 | int target_num_pixels) { |
| 131 | return FindScale(GetViewScaleFactors(), kUpBias, |
| 132 | width, height, target_num_pixels); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Finds the scale factor that, when applied to width and height, produces |
| 136 | // fewer than num_pixels. |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 137 | static const float kUpAvoidBias = -1000000000.f; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 138 | float VideoAdapter::FindLowerScale(int width, int height, |
| 139 | int target_num_pixels) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 140 | return FindScale(GetViewScaleFactors(), kUpAvoidBias, |
| 141 | width, height, target_num_pixels); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // There are several frame sizes used by Adapter. This explains them |
| 145 | // input_format - set once by server to frame size expected from the camera. |
| 146 | // output_format - size that output would like to be. Includes framerate. |
| 147 | // output_num_pixels - size that output should be constrained to. Used to |
| 148 | // compute output_format from in_frame. |
| 149 | // in_frame - actual camera captured frame size, which is typically the same |
| 150 | // as input_format. This can also be rotated or cropped for aspect ratio. |
| 151 | // out_frame - actual frame output by adapter. Should be a direct scale of |
| 152 | // in_frame maintaining rotation and aspect ratio. |
| 153 | // OnOutputFormatRequest - server requests you send this resolution based on |
| 154 | // view requests. |
| 155 | // OnEncoderResolutionRequest - encoder requests you send this resolution based |
| 156 | // on bandwidth |
| 157 | // OnCpuLoadUpdated - cpu monitor requests you send this resolution based on |
| 158 | // cpu load. |
| 159 | |
| 160 | /////////////////////////////////////////////////////////////////////// |
| 161 | // Implementation of VideoAdapter |
| 162 | VideoAdapter::VideoAdapter() |
| 163 | : output_num_pixels_(INT_MAX), |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 164 | scale_third_(false), |
| 165 | frames_(0), |
| 166 | adapted_frames_(0), |
| 167 | adaption_changes_(0), |
| 168 | previous_width(0), |
| 169 | previous_height(0), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 170 | black_output_(false), |
| 171 | is_black_(false), |
| 172 | interval_next_frame_(0) { |
| 173 | } |
| 174 | |
| 175 | VideoAdapter::~VideoAdapter() { |
| 176 | } |
| 177 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 178 | void VideoAdapter::SetInputFormat(const VideoFormat& format) { |
| 179 | talk_base::CritScope cs(&critical_section_); |
| 180 | input_format_ = format; |
| 181 | output_format_.interval = talk_base::_max( |
| 182 | output_format_.interval, input_format_.interval); |
| 183 | } |
| 184 | |
mallinath@webrtc.org | 1b15f42 | 2013-09-06 22:56:28 +0000 | [diff] [blame^] | 185 | void CoordinatedVideoAdapter::SetInputFormat(const VideoFormat& format) { |
| 186 | int previous_width = input_format().width; |
| 187 | int previous_height = input_format().height; |
| 188 | bool is_resolution_change = previous_width > 0 && format.width > 0 && |
| 189 | (previous_width != format.width || |
| 190 | previous_height != format.height); |
| 191 | VideoAdapter::SetInputFormat(format); |
| 192 | if (is_resolution_change) { |
| 193 | int width, height; |
| 194 | // Trigger the adaptation logic again, to potentially reset the adaptation |
| 195 | // state for things like view requests that may not longer be capping |
| 196 | // output (or may now cap output). |
| 197 | AdaptToMinimumFormat(&width, &height); |
| 198 | LOG(LS_INFO) << "VAdapt Input Resolution Change: " |
| 199 | << "Previous input resolution: " |
| 200 | << previous_width << "x" << previous_height |
| 201 | << " New input resolution: " |
| 202 | << format.width << "x" << format.height |
| 203 | << " New output resolution: " |
| 204 | << width << "x" << height; |
| 205 | } |
| 206 | } |
| 207 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 208 | void VideoAdapter::SetOutputFormat(const VideoFormat& format) { |
| 209 | talk_base::CritScope cs(&critical_section_); |
| 210 | output_format_ = format; |
| 211 | output_num_pixels_ = output_format_.width * output_format_.height; |
| 212 | output_format_.interval = talk_base::_max( |
| 213 | output_format_.interval, input_format_.interval); |
| 214 | } |
| 215 | |
| 216 | const VideoFormat& VideoAdapter::input_format() { |
| 217 | talk_base::CritScope cs(&critical_section_); |
| 218 | return input_format_; |
| 219 | } |
| 220 | |
| 221 | const VideoFormat& VideoAdapter::output_format() { |
| 222 | talk_base::CritScope cs(&critical_section_); |
| 223 | return output_format_; |
| 224 | } |
| 225 | |
| 226 | void VideoAdapter::SetBlackOutput(bool black) { |
| 227 | talk_base::CritScope cs(&critical_section_); |
| 228 | black_output_ = black; |
| 229 | } |
| 230 | |
| 231 | // Constrain output resolution to this many pixels overall |
| 232 | void VideoAdapter::SetOutputNumPixels(int num_pixels) { |
| 233 | output_num_pixels_ = num_pixels; |
| 234 | } |
| 235 | |
| 236 | int VideoAdapter::GetOutputNumPixels() const { |
| 237 | return output_num_pixels_; |
| 238 | } |
| 239 | |
| 240 | // TODO(fbarchard): Add AdaptFrameRate function that only drops frames but |
| 241 | // not resolution. |
| 242 | bool VideoAdapter::AdaptFrame(const VideoFrame* in_frame, |
| 243 | const VideoFrame** out_frame) { |
| 244 | talk_base::CritScope cs(&critical_section_); |
| 245 | if (!in_frame || !out_frame) { |
| 246 | return false; |
| 247 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 248 | ++frames_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 249 | |
| 250 | // Update input to actual frame dimensions. |
mallinath@webrtc.org | 1b15f42 | 2013-09-06 22:56:28 +0000 | [diff] [blame^] | 251 | VideoFormat format(in_frame->GetWidth(), in_frame->GetHeight(), |
| 252 | input_format_.interval, input_format_.fourcc); |
| 253 | SetInputFormat(format); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 254 | |
| 255 | // Drop the input frame if necessary. |
| 256 | bool should_drop = false; |
| 257 | if (!output_num_pixels_) { |
| 258 | // Drop all frames as the output format is 0x0. |
| 259 | should_drop = true; |
| 260 | } else { |
| 261 | // Drop some frames based on input fps and output fps. |
| 262 | // Normally output fps is less than input fps. |
| 263 | // TODO(fbarchard): Consider adjusting interval to reflect the adjusted |
| 264 | // interval between frames after dropping some frames. |
| 265 | interval_next_frame_ += input_format_.interval; |
| 266 | if (output_format_.interval > 0) { |
| 267 | if (interval_next_frame_ >= output_format_.interval) { |
| 268 | interval_next_frame_ %= output_format_.interval; |
| 269 | } else { |
| 270 | should_drop = true; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | if (should_drop) { |
| 275 | *out_frame = NULL; |
| 276 | return true; |
| 277 | } |
| 278 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 279 | float scale = 1.f; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 280 | if (output_num_pixels_) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 281 | scale = VideoAdapter::FindClosestViewScale( |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 282 | static_cast<int>(in_frame->GetWidth()), |
| 283 | static_cast<int>(in_frame->GetHeight()), |
| 284 | output_num_pixels_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 285 | output_format_.width = static_cast<int>(in_frame->GetWidth() * scale + .5f); |
| 286 | output_format_.height = static_cast<int>(in_frame->GetHeight() * scale + |
| 287 | .5f); |
| 288 | } |
| 289 | |
| 290 | if (!StretchToOutputFrame(in_frame)) { |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | *out_frame = output_frame_.get(); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 295 | |
| 296 | // Show VAdapt log every 300 frames. (10 seconds) |
| 297 | // TODO(fbarchard): Consider GetLogSeverity() to change interval to less |
| 298 | // for LS_VERBOSE and more for LS_INFO. |
| 299 | bool show = frames_ % 300 == 0; |
| 300 | if (in_frame->GetWidth() != (*out_frame)->GetWidth() || |
| 301 | in_frame->GetHeight() != (*out_frame)->GetHeight()) { |
| 302 | ++adapted_frames_; |
| 303 | } |
| 304 | // TODO(fbarchard): LOG the previous output resolution and track input |
| 305 | // resolution changes as well. Consider dropping the statistics into their |
| 306 | // own class which could be queried publically. |
| 307 | bool changed = false; |
| 308 | if (previous_width && (previous_width != (*out_frame)->GetWidth() || |
| 309 | previous_height != (*out_frame)->GetHeight())) { |
| 310 | show = true; |
| 311 | ++adaption_changes_; |
| 312 | changed = true; |
| 313 | } |
| 314 | if (show) { |
| 315 | // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed |
| 316 | // in default calls. |
| 317 | LOG(LS_INFO) << "VAdapt Frame: " << adapted_frames_ |
| 318 | << " / " << frames_ |
| 319 | << " Changes: " << adaption_changes_ |
| 320 | << " Input: " << in_frame->GetWidth() |
| 321 | << "x" << in_frame->GetHeight() |
| 322 | << " Scale: " << scale |
| 323 | << " Output: " << (*out_frame)->GetWidth() |
| 324 | << "x" << (*out_frame)->GetHeight() |
| 325 | << " Changed: " << (changed ? "true" : "false"); |
| 326 | } |
| 327 | previous_width = (*out_frame)->GetWidth(); |
| 328 | previous_height = (*out_frame)->GetHeight(); |
| 329 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 330 | return true; |
| 331 | } |
| 332 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 333 | // Scale or Blacken the frame. Returns true if successful. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 334 | bool VideoAdapter::StretchToOutputFrame(const VideoFrame* in_frame) { |
| 335 | int output_width = output_format_.width; |
| 336 | int output_height = output_format_.height; |
| 337 | |
| 338 | // Create and stretch the output frame if it has not been created yet or its |
| 339 | // size is not same as the expected. |
| 340 | bool stretched = false; |
| 341 | if (!output_frame_ || |
| 342 | output_frame_->GetWidth() != static_cast<size_t>(output_width) || |
| 343 | output_frame_->GetHeight() != static_cast<size_t>(output_height)) { |
| 344 | output_frame_.reset( |
| 345 | in_frame->Stretch(output_width, output_height, true, true)); |
| 346 | if (!output_frame_) { |
| 347 | LOG(LS_WARNING) << "Adapter failed to stretch frame to " |
| 348 | << output_width << "x" << output_height; |
| 349 | return false; |
| 350 | } |
| 351 | stretched = true; |
| 352 | is_black_ = false; |
| 353 | } |
| 354 | |
| 355 | if (!black_output_) { |
| 356 | if (!stretched) { |
| 357 | // The output frame does not need to be blacken and has not been stretched |
| 358 | // from the input frame yet, stretch the input frame. This is the most |
| 359 | // common case. |
| 360 | in_frame->StretchToFrame(output_frame_.get(), true, true); |
| 361 | } |
| 362 | is_black_ = false; |
| 363 | } else { |
| 364 | if (!is_black_) { |
| 365 | output_frame_->SetToBlack(); |
| 366 | is_black_ = true; |
| 367 | } |
| 368 | output_frame_->SetElapsedTime(in_frame->GetElapsedTime()); |
| 369 | output_frame_->SetTimeStamp(in_frame->GetTimeStamp()); |
| 370 | } |
| 371 | |
| 372 | return true; |
| 373 | } |
| 374 | |
| 375 | /////////////////////////////////////////////////////////////////////// |
| 376 | // Implementation of CoordinatedVideoAdapter |
| 377 | CoordinatedVideoAdapter::CoordinatedVideoAdapter() |
| 378 | : cpu_adaptation_(false), |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 379 | cpu_smoothing_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 380 | gd_adaptation_(true), |
| 381 | view_adaptation_(true), |
| 382 | view_switch_(false), |
| 383 | cpu_downgrade_count_(0), |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 384 | cpu_adapt_wait_time_(0), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 385 | high_system_threshold_(kHighSystemCpuThreshold), |
| 386 | low_system_threshold_(kLowSystemCpuThreshold), |
| 387 | process_threshold_(kProcessCpuThreshold), |
| 388 | view_desired_num_pixels_(INT_MAX), |
| 389 | view_desired_interval_(0), |
| 390 | encoder_desired_num_pixels_(INT_MAX), |
| 391 | cpu_desired_num_pixels_(INT_MAX), |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 392 | adapt_reason_(0), |
| 393 | system_load_average_(kCpuLoadInitialAverage) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | // Helper function to UPGRADE or DOWNGRADE a number of pixels |
| 397 | void CoordinatedVideoAdapter::StepPixelCount( |
| 398 | CoordinatedVideoAdapter::AdaptRequest request, |
| 399 | int* num_pixels) { |
| 400 | switch (request) { |
| 401 | case CoordinatedVideoAdapter::DOWNGRADE: |
| 402 | *num_pixels /= 2; |
| 403 | break; |
| 404 | |
| 405 | case CoordinatedVideoAdapter::UPGRADE: |
| 406 | *num_pixels *= 2; |
| 407 | break; |
| 408 | |
| 409 | default: // No change in pixel count |
| 410 | break; |
| 411 | } |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | // Find the adaptation request of the cpu based on the load. Return UPGRADE if |
| 416 | // the load is low, DOWNGRADE if the load is high, and KEEP otherwise. |
| 417 | CoordinatedVideoAdapter::AdaptRequest CoordinatedVideoAdapter::FindCpuRequest( |
| 418 | int current_cpus, int max_cpus, |
| 419 | float process_load, float system_load) { |
| 420 | // Downgrade if system is high and plugin is at least more than midrange. |
| 421 | if (system_load >= high_system_threshold_ * max_cpus && |
| 422 | process_load >= process_threshold_ * current_cpus) { |
| 423 | return CoordinatedVideoAdapter::DOWNGRADE; |
| 424 | // Upgrade if system is low. |
| 425 | } else if (system_load < low_system_threshold_ * max_cpus) { |
| 426 | return CoordinatedVideoAdapter::UPGRADE; |
| 427 | } |
| 428 | return CoordinatedVideoAdapter::KEEP; |
| 429 | } |
| 430 | |
| 431 | // A remote view request for a new resolution. |
| 432 | void CoordinatedVideoAdapter::OnOutputFormatRequest(const VideoFormat& format) { |
| 433 | talk_base::CritScope cs(&request_critical_section_); |
| 434 | if (!view_adaptation_) { |
| 435 | return; |
| 436 | } |
| 437 | // Set output for initial aspect ratio in mediachannel unittests. |
| 438 | int old_num_pixels = GetOutputNumPixels(); |
| 439 | SetOutputFormat(format); |
| 440 | SetOutputNumPixels(old_num_pixels); |
| 441 | view_desired_num_pixels_ = format.width * format.height; |
| 442 | view_desired_interval_ = format.interval; |
| 443 | int new_width, new_height; |
| 444 | bool changed = AdaptToMinimumFormat(&new_width, &new_height); |
| 445 | LOG(LS_INFO) << "VAdapt View Request: " |
| 446 | << format.width << "x" << format.height |
| 447 | << " Pixels: " << view_desired_num_pixels_ |
| 448 | << " Changed: " << (changed ? "true" : "false") |
| 449 | << " To: " << new_width << "x" << new_height; |
| 450 | } |
| 451 | |
| 452 | // A Bandwidth GD request for new resolution |
| 453 | void CoordinatedVideoAdapter::OnEncoderResolutionRequest( |
| 454 | int width, int height, AdaptRequest request) { |
| 455 | talk_base::CritScope cs(&request_critical_section_); |
| 456 | if (!gd_adaptation_) { |
| 457 | return; |
| 458 | } |
| 459 | int old_encoder_desired_num_pixels = encoder_desired_num_pixels_; |
| 460 | if (KEEP != request) { |
| 461 | int new_encoder_desired_num_pixels = width * height; |
| 462 | int old_num_pixels = GetOutputNumPixels(); |
| 463 | if (new_encoder_desired_num_pixels != old_num_pixels) { |
| 464 | LOG(LS_VERBOSE) << "VAdapt GD resolution stale. Ignored"; |
| 465 | } else { |
| 466 | // Update the encoder desired format based on the request. |
| 467 | encoder_desired_num_pixels_ = new_encoder_desired_num_pixels; |
| 468 | StepPixelCount(request, &encoder_desired_num_pixels_); |
| 469 | } |
| 470 | } |
| 471 | int new_width, new_height; |
| 472 | bool changed = AdaptToMinimumFormat(&new_width, &new_height); |
| 473 | |
| 474 | // Ignore up or keep if no change. |
| 475 | if (DOWNGRADE != request && view_switch_ && !changed) { |
| 476 | encoder_desired_num_pixels_ = old_encoder_desired_num_pixels; |
| 477 | LOG(LS_VERBOSE) << "VAdapt ignoring GD request."; |
| 478 | } |
| 479 | |
| 480 | LOG(LS_INFO) << "VAdapt GD Request: " |
| 481 | << (DOWNGRADE == request ? "down" : |
| 482 | (UPGRADE == request ? "up" : "keep")) |
| 483 | << " From: " << width << "x" << height |
| 484 | << " Pixels: " << encoder_desired_num_pixels_ |
| 485 | << " Changed: " << (changed ? "true" : "false") |
| 486 | << " To: " << new_width << "x" << new_height; |
| 487 | } |
| 488 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 489 | // A Bandwidth GD request for new resolution |
| 490 | void CoordinatedVideoAdapter::OnCpuResolutionRequest(AdaptRequest request) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 491 | talk_base::CritScope cs(&request_critical_section_); |
| 492 | if (!cpu_adaptation_) { |
| 493 | return; |
| 494 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 495 | // Update how many times we have downgraded due to the cpu load. |
| 496 | switch (request) { |
| 497 | case DOWNGRADE: |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 498 | // Ignore downgrades if we have downgraded the maximum times. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 499 | if (cpu_downgrade_count_ < kMaxCpuDowngrades) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 500 | ++cpu_downgrade_count_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 501 | } else { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 502 | LOG(LS_VERBOSE) << "VAdapt CPU load high but do not downgrade " |
| 503 | "because maximum downgrades reached"; |
| 504 | SignalCpuAdaptationUnable(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 505 | } |
| 506 | break; |
| 507 | case UPGRADE: |
| 508 | if (cpu_downgrade_count_ > 0) { |
| 509 | bool is_min = IsMinimumFormat(cpu_desired_num_pixels_); |
| 510 | if (is_min) { |
| 511 | --cpu_downgrade_count_; |
| 512 | } else { |
| 513 | LOG(LS_VERBOSE) << "VAdapt CPU load low but do not upgrade " |
| 514 | "because cpu is not limiting resolution"; |
| 515 | } |
| 516 | } else { |
| 517 | LOG(LS_VERBOSE) << "VAdapt CPU load low but do not upgrade " |
| 518 | "because minimum downgrades reached"; |
| 519 | } |
| 520 | break; |
| 521 | case KEEP: |
| 522 | default: |
| 523 | break; |
| 524 | } |
| 525 | if (KEEP != request) { |
| 526 | // TODO(fbarchard): compute stepping up/down from OutputNumPixels but |
| 527 | // clamp to inputpixels / 4 (2 steps) |
| 528 | cpu_desired_num_pixels_ = cpu_downgrade_count_ == 0 ? INT_MAX : |
| 529 | static_cast<int>(input_format().width * input_format().height >> |
| 530 | cpu_downgrade_count_); |
| 531 | } |
| 532 | int new_width, new_height; |
| 533 | bool changed = AdaptToMinimumFormat(&new_width, &new_height); |
| 534 | LOG(LS_INFO) << "VAdapt CPU Request: " |
| 535 | << (DOWNGRADE == request ? "down" : |
| 536 | (UPGRADE == request ? "up" : "keep")) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 537 | << " Steps: " << cpu_downgrade_count_ |
| 538 | << " Changed: " << (changed ? "true" : "false") |
| 539 | << " To: " << new_width << "x" << new_height; |
| 540 | } |
| 541 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 542 | // A CPU request for new resolution |
| 543 | // TODO(fbarchard): Move outside adapter. |
| 544 | void CoordinatedVideoAdapter::OnCpuLoadUpdated( |
| 545 | int current_cpus, int max_cpus, float process_load, float system_load) { |
| 546 | talk_base::CritScope cs(&request_critical_section_); |
| 547 | if (!cpu_adaptation_) { |
| 548 | return; |
| 549 | } |
| 550 | // Update the moving average of system load. Even if we aren't smoothing, |
| 551 | // we'll still calculate this information, in case smoothing is later enabled. |
| 552 | system_load_average_ = kCpuLoadWeightCoefficient * system_load + |
| 553 | (1.0f - kCpuLoadWeightCoefficient) * system_load_average_; |
| 554 | if (cpu_smoothing_) { |
| 555 | system_load = system_load_average_; |
| 556 | } |
| 557 | // If we haven't started taking samples yet, wait until we have at least |
| 558 | // the correct number of samples per the wait time. |
| 559 | if (cpu_adapt_wait_time_ == 0) { |
| 560 | cpu_adapt_wait_time_ = talk_base::TimeAfter(kCpuLoadMinSampleTime); |
| 561 | } |
| 562 | AdaptRequest request = FindCpuRequest(current_cpus, max_cpus, |
| 563 | process_load, system_load); |
| 564 | // Make sure we're not adapting too quickly. |
| 565 | if (request != KEEP) { |
| 566 | if (talk_base::TimeIsLater(talk_base::Time(), |
| 567 | cpu_adapt_wait_time_)) { |
| 568 | LOG(LS_VERBOSE) << "VAdapt CPU load high/low but do not adapt until " |
| 569 | << talk_base::TimeUntil(cpu_adapt_wait_time_) << " ms"; |
| 570 | request = KEEP; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | OnCpuResolutionRequest(request); |
| 575 | } |
| 576 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 577 | // Called by cpu adapter on up requests. |
| 578 | bool CoordinatedVideoAdapter::IsMinimumFormat(int pixels) { |
| 579 | // Find closest scale factor that matches input resolution to min_num_pixels |
| 580 | // and set that for output resolution. This is not needed for VideoAdapter, |
| 581 | // but provides feedback to unittests and users on expected resolution. |
| 582 | // Actual resolution is based on input frame. |
| 583 | VideoFormat new_output = output_format(); |
| 584 | VideoFormat input = input_format(); |
| 585 | if (input_format().IsSize0x0()) { |
| 586 | input = new_output; |
| 587 | } |
| 588 | float scale = 1.0f; |
| 589 | if (!input.IsSize0x0()) { |
| 590 | scale = FindClosestScale(input.width, |
| 591 | input.height, |
| 592 | pixels); |
| 593 | } |
| 594 | new_output.width = static_cast<int>(input.width * scale + .5f); |
| 595 | new_output.height = static_cast<int>(input.height * scale + .5f); |
| 596 | int new_pixels = new_output.width * new_output.height; |
| 597 | int num_pixels = GetOutputNumPixels(); |
| 598 | return new_pixels <= num_pixels; |
| 599 | } |
| 600 | |
| 601 | // Called by all coordinators when there is a change. |
| 602 | bool CoordinatedVideoAdapter::AdaptToMinimumFormat(int* new_width, |
| 603 | int* new_height) { |
| 604 | VideoFormat new_output = output_format(); |
| 605 | VideoFormat input = input_format(); |
| 606 | if (input_format().IsSize0x0()) { |
| 607 | input = new_output; |
| 608 | } |
| 609 | int old_num_pixels = GetOutputNumPixels(); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 610 | int min_num_pixels = INT_MAX; |
| 611 | adapt_reason_ = 0; |
| 612 | |
| 613 | // Reduce resolution based on encoder bandwidth (GD). |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 614 | if (encoder_desired_num_pixels_ && |
| 615 | (encoder_desired_num_pixels_ < min_num_pixels)) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 616 | adapt_reason_ |= ADAPTREASON_BANDWIDTH; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 617 | min_num_pixels = encoder_desired_num_pixels_; |
| 618 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 619 | // Reduce resolution based on CPU. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 620 | if (cpu_adaptation_ && cpu_desired_num_pixels_ && |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 621 | (cpu_desired_num_pixels_ <= min_num_pixels)) { |
| 622 | if (cpu_desired_num_pixels_ < min_num_pixels) { |
| 623 | adapt_reason_ = ADAPTREASON_CPU; |
| 624 | } else { |
| 625 | adapt_reason_ |= ADAPTREASON_CPU; |
| 626 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 627 | min_num_pixels = cpu_desired_num_pixels_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 628 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 629 | // Round resolution for GD or CPU to allow 1/2 to map to 9/16. |
| 630 | if (!input.IsSize0x0() && min_num_pixels != INT_MAX) { |
| 631 | float scale = FindClosestScale(input.width, input.height, min_num_pixels); |
| 632 | min_num_pixels = static_cast<int>(input.width * scale + .5f) * |
| 633 | static_cast<int>(input.height * scale + .5f); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 634 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 635 | // Reduce resolution based on View Request. |
| 636 | if (view_desired_num_pixels_ <= min_num_pixels) { |
| 637 | if (view_desired_num_pixels_ < min_num_pixels) { |
| 638 | adapt_reason_ = ADAPTREASON_VIEW; |
| 639 | } else { |
| 640 | adapt_reason_ |= ADAPTREASON_VIEW; |
| 641 | } |
| 642 | min_num_pixels = view_desired_num_pixels_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 643 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 644 | // Snap to a scale factor. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 645 | float scale = 1.0f; |
| 646 | if (!input.IsSize0x0()) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 647 | scale = FindLowerScale(input.width, input.height, min_num_pixels); |
| 648 | min_num_pixels = static_cast<int>(input.width * scale + .5f) * |
| 649 | static_cast<int>(input.height * scale + .5f); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 650 | } |
| 651 | if (scale == 1.0f) { |
| 652 | adapt_reason_ = 0; |
| 653 | } |
| 654 | *new_width = new_output.width = static_cast<int>(input.width * scale + .5f); |
| 655 | *new_height = new_output.height = static_cast<int>(input.height * scale + |
| 656 | .5f); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 657 | SetOutputNumPixels(min_num_pixels); |
| 658 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 659 | new_output.interval = view_desired_interval_; |
| 660 | SetOutputFormat(new_output); |
| 661 | int new_num_pixels = GetOutputNumPixels(); |
| 662 | bool changed = new_num_pixels != old_num_pixels; |
| 663 | |
| 664 | static const char* kReasons[8] = { |
| 665 | "None", |
| 666 | "CPU", |
| 667 | "BANDWIDTH", |
| 668 | "CPU+BANDWIDTH", |
| 669 | "VIEW", |
| 670 | "CPU+VIEW", |
| 671 | "BANDWIDTH+VIEW", |
| 672 | "CPU+BANDWIDTH+VIEW", |
| 673 | }; |
| 674 | |
| 675 | LOG(LS_VERBOSE) << "VAdapt Status View: " << view_desired_num_pixels_ |
| 676 | << " GD: " << encoder_desired_num_pixels_ |
| 677 | << " CPU: " << cpu_desired_num_pixels_ |
| 678 | << " Pixels: " << min_num_pixels |
| 679 | << " Input: " << input.width |
| 680 | << "x" << input.height |
| 681 | << " Scale: " << scale |
| 682 | << " Resolution: " << new_output.width |
| 683 | << "x" << new_output.height |
| 684 | << " Changed: " << (changed ? "true" : "false") |
| 685 | << " Reason: " << kReasons[adapt_reason_]; |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 686 | |
| 687 | if (changed) { |
| 688 | // When any adaptation occurs, historic CPU load levels are no longer |
| 689 | // accurate. Clear out our state so we can re-learn at the new normal. |
| 690 | cpu_adapt_wait_time_ = talk_base::TimeAfter(kCpuLoadMinSampleTime); |
| 691 | system_load_average_ = kCpuLoadInitialAverage; |
| 692 | } |
| 693 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 694 | return changed; |
| 695 | } |
| 696 | |
| 697 | } // namespace cricket |