| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/capture/content/capture_resolution_chooser.h" | 5 #include "device/capture/content/capture_resolution_chooser.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "media/base/limits.h" | 11 #include "media/base/limits.h" |
| 12 #include "media/base/video_util.h" | 12 #include "media/base/video_util.h" |
| 13 | 13 |
| 14 namespace media { | 14 namespace device { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // Each snapped frame size is an integer multiple of this many lines apart. | 18 // Each snapped frame size is an integer multiple of this many lines apart. |
| 19 // This is ideal for 16:9 content, but seems to also work well for many | 19 // This is ideal for 16:9 content, but seems to also work well for many |
| 20 // arbitrary aspect ratios. | 20 // arbitrary aspect ratios. |
| 21 const int kSnappedHeightStep = 90; | 21 const int kSnappedHeightStep = 90; |
| 22 | 22 |
| 23 // The minimum amount of decrease in area between consecutive snapped frame | 23 // The minimum amount of decrease in area between consecutive snapped frame |
| 24 // sizes. This matters externally, where the end-to-end system is hunting for a | 24 // sizes. This matters externally, where the end-to-end system is hunting for a |
| 25 // capture size that works within all resource bottlenecks. If the snapped | 25 // capture size that works within all resource bottlenecks. If the snapped |
| 26 // frame sizes are too-close together, the end-to-end system cannot stablize. | 26 // frame sizes are too-close together, the end-to-end system cannot stablize. |
| 27 // If they are too-far apart, quality is being sacrificed. | 27 // If they are too-far apart, quality is being sacrificed. |
| 28 const int kMinAreaDecreasePercent = 15; | 28 const int kMinAreaDecreasePercent = 15; |
| 29 | 29 |
| 30 // Compute the minimum frame size from the given |max_frame_size| and | 30 // Compute the minimum frame size from the given |max_frame_size| and |
| 31 // |resolution_change_policy|. | 31 // |resolution_change_policy|. |
| 32 gfx::Size ComputeMinimumCaptureSize( | 32 gfx::Size ComputeMinimumCaptureSize( |
| 33 const gfx::Size& max_frame_size, | 33 const gfx::Size& max_frame_size, |
| 34 ResolutionChangePolicy resolution_change_policy) { | 34 ResolutionChangePolicy resolution_change_policy) { |
| 35 switch (resolution_change_policy) { | 35 switch (resolution_change_policy) { |
| 36 case RESOLUTION_POLICY_FIXED_RESOLUTION: | 36 case media::RESOLUTION_POLICY_FIXED_RESOLUTION: |
| 37 return max_frame_size; | 37 return max_frame_size; |
| 38 case RESOLUTION_POLICY_FIXED_ASPECT_RATIO: { | 38 case media::RESOLUTION_POLICY_FIXED_ASPECT_RATIO: { |
| 39 // TODO(miu): This is a place-holder until "min constraints" are plumbed- | 39 // TODO(miu): This is a place-holder until "min constraints" are plumbed- |
| 40 // in from the MediaStream framework. http://crbug.com/473336 | 40 // in from the MediaStream framework. http://crbug.com/473336 |
| 41 const int kMinLines = 180; | 41 const int kMinLines = 180; |
| 42 if (max_frame_size.height() <= kMinLines) | 42 if (max_frame_size.height() <= kMinLines) |
| 43 return max_frame_size; | 43 return max_frame_size; |
| 44 const gfx::Size result( | 44 const gfx::Size result( |
| 45 kMinLines * max_frame_size.width() / max_frame_size.height(), | 45 kMinLines * max_frame_size.width() / max_frame_size.height(), |
| 46 kMinLines); | 46 kMinLines); |
| 47 if (result.width() <= 0 || result.width() > limits::kMaxDimension) | 47 if (result.width() <= 0 || result.width() > media::limits::kMaxDimension) |
| 48 return max_frame_size; | 48 return max_frame_size; |
| 49 return result; | 49 return result; |
| 50 } | 50 } |
| 51 case RESOLUTION_POLICY_ANY_WITHIN_LIMIT: | 51 case media::RESOLUTION_POLICY_ANY_WITHIN_LIMIT: |
| 52 return gfx::Size(1, 1); | 52 return gfx::Size(1, 1); |
| 53 } | 53 } |
| 54 NOTREACHED(); | 54 NOTREACHED(); |
| 55 return gfx::Size(1, 1); | 55 return gfx::Size(1, 1); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Returns |size|, unless it exceeds |max_size| or is under |min_size|. When | 58 // Returns |size|, unless it exceeds |max_size| or is under |min_size|. When |
| 59 // the bounds are exceeded, computes and returns an alternate size of similar | 59 // the bounds are exceeded, computes and returns an alternate size of similar |
| 60 // aspect ratio that is within the bounds. | 60 // aspect ratio that is within the bounds. |
| 61 gfx::Size ComputeBoundedCaptureSize(const gfx::Size& size, | 61 gfx::Size ComputeBoundedCaptureSize(const gfx::Size& size, |
| 62 const gfx::Size& min_size, | 62 const gfx::Size& min_size, |
| 63 const gfx::Size& max_size) { | 63 const gfx::Size& max_size) { |
| 64 if (size.width() > max_size.width() || size.height() > max_size.height()) { | 64 if (size.width() > max_size.width() || size.height() > max_size.height()) { |
| 65 gfx::Size result = ScaleSizeToFitWithinTarget(size, max_size); | 65 gfx::Size result = media::ScaleSizeToFitWithinTarget(size, max_size); |
| 66 result.SetToMax(min_size); | 66 result.SetToMax(min_size); |
| 67 return result; | 67 return result; |
| 68 } else if (size.width() < min_size.width() || | 68 } else if (size.width() < min_size.width() || |
| 69 size.height() < min_size.height()) { | 69 size.height() < min_size.height()) { |
| 70 gfx::Size result = ScaleSizeToEncompassTarget(size, min_size); | 70 gfx::Size result = media::ScaleSizeToEncompassTarget(size, min_size); |
| 71 result.SetToMin(max_size); | 71 result.SetToMin(max_size); |
| 72 return result; | 72 return result; |
| 73 } else { | 73 } else { |
| 74 return size; | 74 return size; |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 | 77 |
| 78 // Returns true if the area of |a| is less than that of |b|. | 78 // Returns true if the area of |a| is less than that of |b|. |
| 79 bool CompareByArea(const gfx::Size& a, const gfx::Size& b) { | 79 bool CompareByArea(const gfx::Size& a, const gfx::Size& b) { |
| 80 return a.GetArea() < b.GetArea(); | 80 return a.GetArea() < b.GetArea(); |
| 81 } | 81 } |
| 82 | 82 |
| 83 } // namespace | 83 } // namespace |
| 84 | 84 |
| 85 CaptureResolutionChooser::CaptureResolutionChooser( | 85 CaptureResolutionChooser::CaptureResolutionChooser( |
| 86 const gfx::Size& max_frame_size, | 86 const gfx::Size& max_frame_size, |
| 87 ResolutionChangePolicy resolution_change_policy) | 87 ResolutionChangePolicy resolution_change_policy) |
| 88 : max_frame_size_(max_frame_size), | 88 : max_frame_size_(max_frame_size), |
| 89 min_frame_size_( | 89 min_frame_size_( |
| 90 ComputeMinimumCaptureSize(max_frame_size, resolution_change_policy)), | 90 ComputeMinimumCaptureSize(max_frame_size, resolution_change_policy)), |
| 91 resolution_change_policy_(resolution_change_policy), | 91 resolution_change_policy_(resolution_change_policy), |
| 92 target_area_(std::numeric_limits<decltype(target_area_)>::max()) { | 92 target_area_(std::numeric_limits<decltype(target_area_)>::max()) { |
| 93 DCHECK_LT(0, max_frame_size_.width()); | 93 DCHECK_LT(0, max_frame_size_.width()); |
| 94 DCHECK_LT(0, max_frame_size_.height()); | 94 DCHECK_LT(0, max_frame_size_.height()); |
| 95 DCHECK_LE(min_frame_size_.width(), max_frame_size_.width()); | 95 DCHECK_LE(min_frame_size_.width(), max_frame_size_.width()); |
| 96 DCHECK_LE(min_frame_size_.height(), max_frame_size_.height()); | 96 DCHECK_LE(min_frame_size_.height(), max_frame_size_.height()); |
| 97 DCHECK_LE(resolution_change_policy_, RESOLUTION_POLICY_LAST); | 97 DCHECK_LE(resolution_change_policy_, media::RESOLUTION_POLICY_LAST); |
| 98 | 98 |
| 99 UpdateSnappedFrameSizes(max_frame_size_); | 99 UpdateSnappedFrameSizes(max_frame_size_); |
| 100 RecomputeCaptureSize(); | 100 RecomputeCaptureSize(); |
| 101 } | 101 } |
| 102 | 102 |
| 103 CaptureResolutionChooser::~CaptureResolutionChooser() { | 103 CaptureResolutionChooser::~CaptureResolutionChooser() { |
| 104 } | 104 } |
| 105 | 105 |
| 106 void CaptureResolutionChooser::SetSourceSize(const gfx::Size& source_size) { | 106 void CaptureResolutionChooser::SetSourceSize(const gfx::Size& source_size) { |
| 107 if (source_size.IsEmpty()) | 107 if (source_size.IsEmpty()) |
| 108 return; | 108 return; |
| 109 | 109 |
| 110 switch (resolution_change_policy_) { | 110 switch (resolution_change_policy_) { |
| 111 case RESOLUTION_POLICY_FIXED_RESOLUTION: | 111 case media::RESOLUTION_POLICY_FIXED_RESOLUTION: |
| 112 // Source size changes do not affect the frame resolution. Frame | 112 // Source size changes do not affect the frame resolution. Frame |
| 113 // resolution is always fixed to |max_frame_size_|. | 113 // resolution is always fixed to |max_frame_size_|. |
| 114 break; | 114 break; |
| 115 | 115 |
| 116 case RESOLUTION_POLICY_FIXED_ASPECT_RATIO: | 116 case media::RESOLUTION_POLICY_FIXED_ASPECT_RATIO: |
| 117 UpdateSnappedFrameSizes(ComputeBoundedCaptureSize( | 117 UpdateSnappedFrameSizes(ComputeBoundedCaptureSize( |
| 118 PadToMatchAspectRatio(source_size, max_frame_size_), min_frame_size_, | 118 media::PadToMatchAspectRatio(source_size, max_frame_size_), |
| 119 max_frame_size_)); | 119 min_frame_size_, max_frame_size_)); |
| 120 RecomputeCaptureSize(); | 120 RecomputeCaptureSize(); |
| 121 break; | 121 break; |
| 122 | 122 |
| 123 case RESOLUTION_POLICY_ANY_WITHIN_LIMIT: | 123 case media::RESOLUTION_POLICY_ANY_WITHIN_LIMIT: |
| 124 UpdateSnappedFrameSizes(ComputeBoundedCaptureSize( | 124 UpdateSnappedFrameSizes(ComputeBoundedCaptureSize( |
| 125 source_size, min_frame_size_, max_frame_size_)); | 125 source_size, min_frame_size_, max_frame_size_)); |
| 126 RecomputeCaptureSize(); | 126 RecomputeCaptureSize(); |
| 127 break; | 127 break; |
| 128 } | 128 } |
| 129 } | 129 } |
| 130 | 130 |
| 131 void CaptureResolutionChooser::SetTargetFrameArea(int area) { | 131 void CaptureResolutionChooser::SetTargetFrameArea(int area) { |
| 132 DCHECK_GE(area, 0); | 132 DCHECK_GE(area, 0); |
| 133 target_area_ = area; | 133 target_area_ = area; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 | 229 |
| 230 if (VLOG_IS_ON(1)) { | 230 if (VLOG_IS_ON(1)) { |
| 231 std::vector<std::string> stringified_sizes; | 231 std::vector<std::string> stringified_sizes; |
| 232 for (const gfx::Size& size : snapped_sizes_) | 232 for (const gfx::Size& size : snapped_sizes_) |
| 233 stringified_sizes.push_back(size.ToString()); | 233 stringified_sizes.push_back(size.ToString()); |
| 234 VLOG_STREAM(1) << "Recomputed snapped frame sizes: " | 234 VLOG_STREAM(1) << "Recomputed snapped frame sizes: " |
| 235 << base::JoinString(stringified_sizes, " <--> "); | 235 << base::JoinString(stringified_sizes, " <--> "); |
| 236 } | 236 } |
| 237 } | 237 } |
| 238 | 238 |
| 239 } // namespace media | 239 } // namespace device |
| OLD | NEW |