Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: content/renderer/media/media_stream_constraints_util_sets.h

Issue 2728633002: Add utility set classes to support getUserMedia constraint proccessing. (Closed)
Patch Set: Add missing#include and make ResolutionSet::ClosestPointTo private Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_CONSTRAINTS_UTIL_SETS_H_
6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_CONSTRAINTS_UTIL_SETS_H_
7
8 #include <algorithm>
9 #include <limits>
10 #include <utility>
11 #include <vector>
12
13 #include "base/gtest_prod_util.h"
14 #include "base/logging.h"
15 #include "content/common/content_export.h"
16 #include "content/renderer/media/media_stream_constraints_util.h"
17
18 namespace blink {
19 struct WebMediaTrackConstraintSet;
20 }
21
22 namespace content {
23
24 // This class template represents a set of candidates suitable for a numeric
25 // range-based constraint.
26 template <typename T>
27 class NumericRangeSet {
28 public:
29 NumericRangeSet() : min_(0), max_(DefaultMax()) {}
30 NumericRangeSet(T min, T max) : min_(min), max_(max) {}
31 NumericRangeSet(const NumericRangeSet& other) = default;
32 NumericRangeSet& operator=(const NumericRangeSet& other) = default;
33 ~NumericRangeSet() = default;
34
35 T Min() const { return min_; }
36 T Max() const { return max_; }
37 bool IsEmpty() const { return max_ < min_; }
38
39 NumericRangeSet Intersection(const NumericRangeSet& other) const {
40 return NumericRangeSet(std::max(min_, other.min_),
41 std::min(max_, other.max_));
42 }
43
44 // Creates a NumericRangeSet based on the minimum and maximum values of
45 // |constraint|.
46 template <typename ConstraintType>
47 static auto FromConstraint(ConstraintType constraint)
48 -> NumericRangeSet<decltype(constraint.min())> {
49 return NumericRangeSet<decltype(constraint.min())>(
50 ConstraintHasMin(constraint) ? ConstraintMin(constraint) : 0,
51 ConstraintHasMax(constraint) ? ConstraintMax(constraint)
52 : DefaultMax());
53 }
54
55 private:
56 static inline T DefaultMax() {
57 return std::numeric_limits<T>::has_infinity
58 ? std::numeric_limits<T>::infinity()
59 : std::numeric_limits<T>::max();
60 }
61 T min_;
62 T max_;
63 };
64
65 // This class defines a set of discrete elements suitable for resolving
66 // constraints with a countable number of choices not suitable to be constrained
67 // by range. Examples are strings, booleans and certain constraints of type
68 // long. A DiscreteSet can be empty, have their elements explicitly stated, or
69 // be the universal set. The universal set is a set that contains all possible
70 // elements. The specific definition of what elements are in the universal set
71 // is application defined (e.g., it could be all possible boolean values, all
72 // possible strings of length N, or anything that suits a particular
73 // application).
74 template <typename T>
75 class DiscreteSet {
76 public:
77 // Creates a set containing the elements in |elements|.
78 // It is the responsibility of the caller to ensure that |elements| is not
79 // equivalent to the universal set and that |elements| has no repeated
80 // values. Takes ownership of |elements|.
81 explicit DiscreteSet(std::vector<T> elements)
82 : is_universal_(false), elements_(std::move(elements)) {}
83 // Creates an empty set;
84 static DiscreteSet EmptySet() { return DiscreteSet(std::vector<T>()); }
85 static DiscreteSet UniversalSet() { return DiscreteSet(); }
86
87 DiscreteSet(const DiscreteSet& other) = default;
88 DiscreteSet& operator=(const DiscreteSet& other) = default;
89 DiscreteSet(DiscreteSet&& other) = default;
90 DiscreteSet& operator=(DiscreteSet&& other) = default;
91 ~DiscreteSet() = default;
92
93 bool Contains(const T& value) const {
94 return is_universal_ ||
95 std::find(elements_.begin(), elements_.end(), value) !=
96 elements_.end();
97 }
98
99 bool IsEmpty() const { return !is_universal_ && elements_.empty(); }
100
101 bool HasExplicitElements() const { return !elements_.empty(); }
102
103 DiscreteSet Intersection(const DiscreteSet& other) const {
104 if (is_universal_)
105 return other;
106 if (other.is_universal_)
107 return *this;
108 if (IsEmpty() || other.IsEmpty())
109 return EmptySet();
110
111 // Both sets have explicit elements.
112 std::vector<T> intersection;
113 for (const auto& entry : elements_) {
114 auto it =
115 std::find(other.elements_.begin(), other.elements_.end(), entry);
116 if (it != other.elements_.end()) {
117 intersection.push_back(entry);
118 }
119 }
120 return DiscreteSet(std::move(intersection));
121 }
122
123 // Returns a copy of the first element in the set. This is useful as a simple
124 // tie-breaker rule. This applies only to constrained nonempty sets.
125 // Behavior is undefined if the set is empty or universal.
126 T FirstElement() const {
127 DCHECK(HasExplicitElements());
128 return elements_[0];
129 }
130
131 bool is_universal() const { return is_universal_; }
132
133 private:
134 // Creates a universal set.
135 DiscreteSet() : is_universal_(true) {}
136
137 bool is_universal_;
138 std::vector<T> elements_;
139 };
140
141 // This class represents a set of (height, width) screen resolution candidates
142 // determined by width, height and aspect-ratio constraints.
143 // This class supports widths and heights from 0 to kMaxDimension, both
144 // inclusive and aspect ratios from 0.0 to positive infinity, both inclusive.
145 class CONTENT_EXPORT ResolutionSet {
146 public:
147 static constexpr int kMaxDimension = std::numeric_limits<int>::max();
148
149 // Helper class that represents (height, width) points on a plane.
150 class Point {
151 public:
152 // Creates a (|height|, |width|) point. |height| and |width| must be finite.
153 Point(double height, double width);
154 Point(const Point& other);
155 Point& operator=(const Point& other);
156 ~Point();
157
158 // Accessors.
159 double height() const { return height_; }
160 double width() const { return width_; }
161 double AspectRatio() const { return width_ / height_; }
162
163 // Exact equality/inequality operators.
164 bool operator==(const Point& other) const;
165 bool operator!=(const Point& other) const;
166
167 // Returns true if both coordinates of this point and |other| are
168 // approximately equal.
169 bool IsApproximatelyEqualTo(const Point& other) const;
170
171 // Vector-style addition and subtraction operators.
172 Point operator+(const Point& other) const;
173 Point operator-(const Point& other) const;
174
175 // Returns the dot product between |p1| and |p2|.
176 static double Dot(const Point& p1, const Point& p2);
177
178 // Returns the square Euclidean distance between |p1| and |p2|.
179 static double SquareEuclideanDistance(const Point& p1, const Point& p2);
180
181 // Returns the point in the line segment determined by |s1| and |s2| that
182 // is closest to |p|.
183 static Point ClosestPointInSegment(const Point& p,
184 const Point& s1,
185 const Point& s2);
186
187 private:
188 double height_;
189 double width_;
190 };
191
192 // Creates a set with the maximum supported ranges for width, height and
193 // aspect ratio.
194 ResolutionSet();
195 ResolutionSet(int min_height,
196 int max_height,
197 int min_width,
198 int max_width,
199 double min_aspect_ratio,
200 double max_aspect_ratio);
201 ResolutionSet(const ResolutionSet& other);
202 ResolutionSet& operator=(const ResolutionSet& other);
203 ~ResolutionSet();
204
205 // Getters.
206 int min_height() const { return min_height_; }
207 int max_height() const { return max_height_; }
208 int min_width() const { return min_width_; }
209 int max_width() const { return max_width_; }
210 double min_aspect_ratio() const { return min_aspect_ratio_; }
211 double max_aspect_ratio() const { return max_aspect_ratio_; }
212
213 // Returns true if this set is empty.
214 bool IsEmpty() const;
215
216 // These functions return true if a particular variable causes the set to be
217 // empty.
218 bool IsHeightEmpty() const;
219 bool IsWidthEmpty() const;
220 bool IsAspectRatioEmpty() const;
221
222 // These functions return true if the given point is included in this set.
223 bool ContainsPoint(const Point& point) const;
224 bool ContainsPoint(int height, int width) const;
225
226 // Returns a new set with the intersection of |*this| and |other|.
227 ResolutionSet Intersection(const ResolutionSet& other) const;
228
229 // Returns a point in this (nonempty) set closest to the ideal values for the
230 // height, width and aspectRatio constraints in |constraint_set|.
231 // Note that this function ignores all the other data in |constraint_set|.
232 // Only the ideal height, width and aspect ratio are used, and from now on
233 // referred to as |ideal_height|, |ideal_width| and |ideal_aspect_ratio|
234 // respectively.
235 //
236 // * If all three ideal values are given, |ideal_aspect_ratio| is ignored and
237 // the point closest to (|ideal_height|, |ideal_width|) is returned.
238 // * If two ideal values are given, they are used to determine a single ideal
239 // point, which can be one of:
240 // - (|ideal_height|, |ideal_width|),
241 // - (|ideal_height|, |ideal_height|*|ideal_aspect_ratio|), or
242 // - (|ideal_width| / |ideal_aspect_ratio|, |ideal_width|).
243 // The point in the set closest to the ideal point is returned.
244 // * If a single ideal value is given, a point in the set closest to the line
245 // defined by the ideal value is returned. If there is more than one point
246 // closest to the ideal line, the following tie-breaker rules are used:
247 // - If |ideal_height| is provided, the point closest to
248 // (|ideal_height|, |ideal_height| * kDefaultAspectRatio).
249 // - If |ideal_width| is provided, the point closest to
250 // (|ideal_width| / kDefaultAspectRatio, |ideal_width|).
251 // - If |ideal_aspect_ratio| is provided, the point with largest area among
252 // the points closest to
253 // (kDefaultHeight, kDefaultHeight * aspect_ratio) and
254 // (kDefaultWidth / aspect_ratio, kDefaultWidth),
255 // where aspect_ratio is |ideal_aspect_ratio| if the ideal line intersects
256 // the set, and the closest aspect ratio to |ideal_aspect_ratio| among the
257 // points in the set if no point in the set has an aspect ratio equal to
258 // |ideal_aspect_ratio|.
259 // * If no ideal value is given, proceed as if only |ideal_aspect_ratio| was
260 // provided with a value of kDefaultAspectRatio.
261 //
262 // This is intended to support the implementation of the spec algorithm for
263 // selection of track settings, as defined in
264 // https://w3c.github.io/mediacapture-main/#dfn-selectsettings.
265 //
266 // The main difference between this algorithm and the spec is that when ideal
267 // values are provided, the spec mandates finding a point that minimizes the
268 // sum of custom relative distances for each provided ideal value, while this
269 // algorithm minimizes either the Euclidean distance (sum of square distances)
270 // on a height-width plane for the cases where two or three ideal values are
271 // provided, or the absolute distance for the case with one ideal value.
272 // Also, in the case with three ideal values, this algorithm ignores the
273 // distance to the ideal aspect ratio.
274 // In most cases the difference in the final result should be negligible.
275 // The reason to follow this approach is that optimization in the worst case
276 // is reduced to projection of a point on line segment, which is a simple
277 // operation that provides exact results. Using the distance function of the
278 // spec, which is not continuous, would require complex optimization methods
279 // that do not necessarily guarantee finding the real optimal value.
280 //
281 // This function has undefined behavior if this set is empty.
282 Point SelectClosestPointToIdeal(
283 const blink::WebMediaTrackConstraintSet& constraint_set) const;
284
285 // Utilities that return ResolutionSets constrained on a specific variable.
286 static ResolutionSet FromHeight(int min, int max);
287 static ResolutionSet FromExactHeight(int value);
288 static ResolutionSet FromWidth(int min, int max);
289 static ResolutionSet FromExactWidth(int value);
290 static ResolutionSet FromAspectRatio(double min, double max);
291 static ResolutionSet FromExactAspectRatio(double value);
292
293 // Returns a ResolutionCandidateSet initialized with |constraint_set|'s
294 // width, height and aspectRatio constraints.
295 static ResolutionSet FromConstraintSet(
296 const blink::WebMediaTrackConstraintSet& constraint_set);
297
298 private:
299 FRIEND_TEST_ALL_PREFIXES(MediaStreamConstraintsUtilSetsTest,
300 ResolutionVertices);
301 FRIEND_TEST_ALL_PREFIXES(MediaStreamConstraintsUtilSetsTest,
302 ResolutionPointSetClosestPoint);
303 FRIEND_TEST_ALL_PREFIXES(MediaStreamConstraintsUtilSetsTest,
304 ResolutionLineSetClosestPoint);
305 FRIEND_TEST_ALL_PREFIXES(MediaStreamConstraintsUtilSetsTest,
306 ResolutionGeneralSetClosestPoint);
307 FRIEND_TEST_ALL_PREFIXES(MediaStreamConstraintsUtilSetsTest,
308 ResolutionIdealOutsideSinglePoint);
309
310 // Implements SelectClosestPointToIdeal() for the case when only the ideal
311 // aspect ratio is provided.
312 Point SelectClosestPointToIdealAspectRatio(double ideal_aspect_ratio) const;
313
314 // Returns the closest point in this set to |point|. If |point| is included in
315 // this set, Point is returned. If this set is empty, behavior is undefined.
316 Point ClosestPointTo(const Point& point) const;
317
318 // Returns the vertices of the set that have the property accessed
319 // by |accessor| closest to |value|. The returned vector always has one or two
320 // elements. Behavior is undefined if the set is empty.
321 std::vector<Point> GetClosestVertices(double (Point::*accessor)() const,
322 double value) const;
323
324 // Returns a list of the vertices defined by the constraints on a height-width
325 // Cartesian plane.
326 // If the list is empty, the set is empty.
327 // If the list contains a single point, the set contains a single point.
328 // If the list contains two points, the set is composed of points on a line
329 // segment.
330 // If the list contains three to six points, they are the vertices of a
331 // convex polygon containing all valid points in the set. Each pair of
332 // consecutive vertices (modulo the size of the list) corresponds to a side of
333 // the polygon, with the vertices given in counterclockwise order.
334 // The list cannot contain more than six points.
335 std::vector<Point> ComputeVertices() const;
336
337 // Adds |point| to |vertices| if |point| is included in this candidate set.
338 void TryAddVertex(std::vector<ResolutionSet::Point>* vertices,
339 const ResolutionSet::Point& point) const;
340
341 int min_height_;
342 int max_height_;
343 int min_width_;
344 int max_width_;
345 double min_aspect_ratio_;
346 double max_aspect_ratio_;
347 };
348
349 // Scalar multiplication for Points.
350 ResolutionSet::Point CONTENT_EXPORT operator*(double d,
351 const ResolutionSet::Point& p);
352
353 } // namespace content
354
355 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_CONSTRAINTS_UTIL_SETS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698