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