| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/media/capture/capture_resolution_chooser.h" | |
| 6 | |
| 7 #include "media/base/limits.h" | |
| 8 #include "media/base/video_util.h" | |
| 9 | |
| 10 namespace content { | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 // Compute the minimum frame size from the given |max_frame_size| and | |
| 15 // |resolution_change_policy|. | |
| 16 gfx::Size ComputeMinimumCaptureSize( | |
| 17 const gfx::Size& max_frame_size, | |
| 18 media::ResolutionChangePolicy resolution_change_policy) { | |
| 19 switch (resolution_change_policy) { | |
| 20 case media::RESOLUTION_POLICY_FIXED_RESOLUTION: | |
| 21 return max_frame_size; | |
| 22 case media::RESOLUTION_POLICY_FIXED_ASPECT_RATIO: { | |
| 23 // TODO(miu): This is a place-holder until "min constraints" are plumbed- | |
| 24 // in from the MediaStream framework. http://crbug.com/473336 | |
| 25 const int kMinLines = 180; | |
| 26 if (max_frame_size.height() <= kMinLines) | |
| 27 return max_frame_size; | |
| 28 const gfx::Size result( | |
| 29 kMinLines * max_frame_size.width() / max_frame_size.height(), | |
| 30 kMinLines); | |
| 31 if (result.width() <= 0 || result.width() > media::limits::kMaxDimension) | |
| 32 return max_frame_size; | |
| 33 return result; | |
| 34 } | |
| 35 case media::RESOLUTION_POLICY_ANY_WITHIN_LIMIT: | |
| 36 return gfx::Size(1, 1); | |
| 37 case media::RESOLUTION_POLICY_LAST: | |
| 38 break; | |
| 39 } | |
| 40 NOTREACHED(); | |
| 41 return gfx::Size(1, 1); | |
| 42 } | |
| 43 | |
| 44 // Returns |size|, unless it exceeds |max_size| or is under |min_size|. When | |
| 45 // the bounds are exceeded, computes and returns an alternate size of similar | |
| 46 // aspect ratio that is within the bounds. | |
| 47 gfx::Size ComputeBoundedCaptureSize(const gfx::Size& size, | |
| 48 const gfx::Size& min_size, | |
| 49 const gfx::Size& max_size) { | |
| 50 if (size.width() > max_size.width() || size.height() > max_size.height()) { | |
| 51 gfx::Size result = media::ScaleSizeToFitWithinTarget(size, max_size); | |
| 52 result.SetToMax(min_size); | |
| 53 return result; | |
| 54 } else if (size.width() < min_size.width() || | |
| 55 size.height() < min_size.height()) { | |
| 56 gfx::Size result = media::ScaleSizeToEncompassTarget(size, min_size); | |
| 57 result.SetToMin(max_size); | |
| 58 return result; | |
| 59 } else { | |
| 60 return size; | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 } // namespace | |
| 65 | |
| 66 CaptureResolutionChooser::CaptureResolutionChooser( | |
| 67 const gfx::Size& max_frame_size, | |
| 68 media::ResolutionChangePolicy resolution_change_policy) | |
| 69 : max_frame_size_(max_frame_size), | |
| 70 min_frame_size_(ComputeMinimumCaptureSize(max_frame_size, | |
| 71 resolution_change_policy)), | |
| 72 resolution_change_policy_(resolution_change_policy), | |
| 73 constrained_size_(max_frame_size) { | |
| 74 DCHECK_LT(0, max_frame_size_.width()); | |
| 75 DCHECK_LT(0, max_frame_size_.height()); | |
| 76 DCHECK_LE(min_frame_size_.width(), max_frame_size_.width()); | |
| 77 DCHECK_LE(min_frame_size_.height(), max_frame_size_.height()); | |
| 78 | |
| 79 RecomputeCaptureSize(); | |
| 80 } | |
| 81 | |
| 82 CaptureResolutionChooser::~CaptureResolutionChooser() {} | |
| 83 | |
| 84 void CaptureResolutionChooser::SetSourceSize(const gfx::Size& source_size) { | |
| 85 if (source_size.IsEmpty()) | |
| 86 return; | |
| 87 | |
| 88 switch (resolution_change_policy_) { | |
| 89 case media::RESOLUTION_POLICY_FIXED_RESOLUTION: | |
| 90 // Source size changes do not affect the frame resolution. Frame | |
| 91 // resolution is always fixed to |max_frame_size_|. | |
| 92 break; | |
| 93 | |
| 94 case media::RESOLUTION_POLICY_FIXED_ASPECT_RATIO: | |
| 95 constrained_size_ = ComputeBoundedCaptureSize( | |
| 96 media::PadToMatchAspectRatio(source_size, max_frame_size_), | |
| 97 min_frame_size_, | |
| 98 max_frame_size_); | |
| 99 RecomputeCaptureSize(); | |
| 100 break; | |
| 101 | |
| 102 case media::RESOLUTION_POLICY_ANY_WITHIN_LIMIT: | |
| 103 constrained_size_ = ComputeBoundedCaptureSize( | |
| 104 source_size, min_frame_size_, max_frame_size_); | |
| 105 RecomputeCaptureSize(); | |
| 106 break; | |
| 107 | |
| 108 case media::RESOLUTION_POLICY_LAST: | |
| 109 NOTREACHED(); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 void CaptureResolutionChooser::RecomputeCaptureSize() { | |
| 114 // TODO(miu): An upcoming change will introduce the ability to find the best | |
| 115 // capture resolution, given the current capabilities of the system. | |
| 116 // http://crbug.com/156767 | |
| 117 capture_size_ = constrained_size_; | |
| 118 } | |
| 119 | |
| 120 } // namespace content | |
| OLD | NEW |