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"
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(),
31 if (result
.width() <= 0 || result
.width() > limits::kMaxDimension
)
32 return max_frame_size
;
35 case RESOLUTION_POLICY_ANY_WITHIN_LIMIT
:
36 return gfx::Size(1, 1);
37 case RESOLUTION_POLICY_LAST
:
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
);
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
);
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())
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_|.
94 case RESOLUTION_POLICY_FIXED_ASPECT_RATIO
:
95 constrained_size_
= ComputeBoundedCaptureSize(
96 PadToMatchAspectRatio(source_size
, max_frame_size_
),
99 RecomputeCaptureSize();
102 case RESOLUTION_POLICY_ANY_WITHIN_LIMIT
:
103 constrained_size_
= ComputeBoundedCaptureSize(
104 source_size
, min_frame_size_
, max_frame_size_
);
105 RecomputeCaptureSize();
108 case RESOLUTION_POLICY_LAST
:
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_
;