[Sync] Fix autofill tests to be autofill profile entities.
[chromium-blink-merge.git] / media / capture / capture_resolution_chooser.cc
blob4f1ea530289accf7d60cb2b59e0f53959ca6d24c
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);
38 NOTREACHED();
39 return gfx::Size(1, 1);
42 // Returns |size|, unless it exceeds |max_size| or is under |min_size|. When
43 // the bounds are exceeded, computes and returns an alternate size of similar
44 // aspect ratio that is within the bounds.
45 gfx::Size ComputeBoundedCaptureSize(const gfx::Size& size,
46 const gfx::Size& min_size,
47 const gfx::Size& max_size) {
48 if (size.width() > max_size.width() || size.height() > max_size.height()) {
49 gfx::Size result = ScaleSizeToFitWithinTarget(size, max_size);
50 result.SetToMax(min_size);
51 return result;
52 } else if (size.width() < min_size.width() ||
53 size.height() < min_size.height()) {
54 gfx::Size result = ScaleSizeToEncompassTarget(size, min_size);
55 result.SetToMin(max_size);
56 return result;
57 } else {
58 return size;
62 } // namespace
64 CaptureResolutionChooser::CaptureResolutionChooser(
65 const gfx::Size& max_frame_size,
66 ResolutionChangePolicy resolution_change_policy)
67 : max_frame_size_(max_frame_size),
68 min_frame_size_(ComputeMinimumCaptureSize(max_frame_size,
69 resolution_change_policy)),
70 resolution_change_policy_(resolution_change_policy),
71 constrained_size_(max_frame_size) {
72 DCHECK_LT(0, max_frame_size_.width());
73 DCHECK_LT(0, max_frame_size_.height());
74 DCHECK_LE(min_frame_size_.width(), max_frame_size_.width());
75 DCHECK_LE(min_frame_size_.height(), max_frame_size_.height());
76 DCHECK_LE(resolution_change_policy_, RESOLUTION_POLICY_LAST);
78 RecomputeCaptureSize();
81 CaptureResolutionChooser::~CaptureResolutionChooser() {}
83 void CaptureResolutionChooser::SetSourceSize(const gfx::Size& source_size) {
84 if (source_size.IsEmpty())
85 return;
87 switch (resolution_change_policy_) {
88 case RESOLUTION_POLICY_FIXED_RESOLUTION:
89 // Source size changes do not affect the frame resolution. Frame
90 // resolution is always fixed to |max_frame_size_|.
91 break;
93 case RESOLUTION_POLICY_FIXED_ASPECT_RATIO:
94 constrained_size_ = ComputeBoundedCaptureSize(
95 PadToMatchAspectRatio(source_size, max_frame_size_),
96 min_frame_size_,
97 max_frame_size_);
98 RecomputeCaptureSize();
99 break;
101 case RESOLUTION_POLICY_ANY_WITHIN_LIMIT:
102 constrained_size_ = ComputeBoundedCaptureSize(
103 source_size, min_frame_size_, max_frame_size_);
104 RecomputeCaptureSize();
105 break;
109 void CaptureResolutionChooser::RecomputeCaptureSize() {
110 // TODO(miu): An upcoming change will introduce the ability to find the best
111 // capture resolution, given the current capabilities of the system.
112 // http://crbug.com/156767
113 capture_size_ = constrained_size_;
116 } // namespace media