Add missing mandoline build deps caught by the new GN version.
[chromium-blink-merge.git] / media / capture / capture_resolution_chooser.cc
blobff2ee9d8c5a9f4fa52ef72cab981cf3c615f2be9
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.
5 #include "media/capture/capture_resolution_chooser.h"
7 #include "media/base/limits.h"
8 #include "media/base/video_util.h"
10 namespace media {
12 namespace {
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 ResolutionChangePolicy resolution_change_policy) {
19 switch (resolution_change_policy) {
20 case RESOLUTION_POLICY_FIXED_RESOLUTION:
21 return max_frame_size;
22 case 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() > limits::kMaxDimension)
32 return max_frame_size;
33 return result;
35 case RESOLUTION_POLICY_ANY_WITHIN_LIMIT:
36 return gfx::Size(1, 1);
37 case RESOLUTION_POLICY_LAST:
38 break;
40 NOTREACHED();
41 return gfx::Size(1, 1);
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 = 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 = ScaleSizeToEncompassTarget(size, min_size);
57 result.SetToMin(max_size);
58 return result;
59 } else {
60 return size;
64 } // namespace
66 CaptureResolutionChooser::CaptureResolutionChooser(
67 const gfx::Size& max_frame_size,
68 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());
79 RecomputeCaptureSize();
82 CaptureResolutionChooser::~CaptureResolutionChooser() {}
84 void CaptureResolutionChooser::SetSourceSize(const gfx::Size& source_size) {
85 if (source_size.IsEmpty())
86 return;
88 switch (resolution_change_policy_) {
89 case 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;
94 case RESOLUTION_POLICY_FIXED_ASPECT_RATIO:
95 constrained_size_ = ComputeBoundedCaptureSize(
96 PadToMatchAspectRatio(source_size, max_frame_size_),
97 min_frame_size_,
98 max_frame_size_);
99 RecomputeCaptureSize();
100 break;
102 case RESOLUTION_POLICY_ANY_WITHIN_LIMIT:
103 constrained_size_ = ComputeBoundedCaptureSize(
104 source_size, min_frame_size_, max_frame_size_);
105 RecomputeCaptureSize();
106 break;
108 case RESOLUTION_POLICY_LAST:
109 NOTREACHED();
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_;
120 } // namespace media