Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 #include "content/renderer/media/media_stream_constraints_util_video_source.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <cmath> | |
| 9 #include <utility> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "content/renderer/media/media_stream_video_source.h" | |
| 13 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" | |
| 14 #include "third_party/WebKit/public/platform/WebString.h" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // Number of default settings to be used as final tie-breaking criteria for | |
| 21 // settings that are equally good at satisfying constraints: | |
| 22 // device ID, power-line frequency, resolution and frame rate. | |
| 23 const int kNumDefaultDistanceEntries = 4; | |
| 24 | |
| 25 // The default resolution to be preferred as tie-breaking criterion. | |
| 26 const int kDefaultResolutionArea = MediaStreamVideoSource::kDefaultWidth * | |
| 27 MediaStreamVideoSource::kDefaultHeight; | |
| 28 | |
| 29 // The minimum aspect ratio to be supported by sources. | |
| 30 const double kMinSourceAspectRatio = 0.05; | |
| 31 | |
| 32 blink::WebString ToWebString(::mojom::FacingMode facing_mode) { | |
| 33 switch (facing_mode) { | |
| 34 case ::mojom::FacingMode::USER: | |
| 35 return blink::WebString::fromASCII("user"); | |
| 36 case ::mojom::FacingMode::ENVIRONMENT: | |
| 37 return blink::WebString::fromASCII("environment"); | |
| 38 case ::mojom::FacingMode::LEFT: | |
| 39 return blink::WebString::fromASCII("left"); | |
| 40 case ::mojom::FacingMode::RIGHT: | |
| 41 return blink::WebString::fromASCII("right"); | |
| 42 default: | |
| 43 return blink::WebString::fromASCII(""); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 template <typename ConstraintType> | |
|
hta - Chromium
2017/02/07 10:57:41
Not for this CL:
If you find these functions usef
Guido Urdaneta
2017/02/07 13:14:26
Acknowledged.
| |
| 48 bool ConstraintHasMax(const ConstraintType& constraint) { | |
| 49 return constraint.hasMax() || constraint.hasExact(); | |
| 50 } | |
| 51 | |
| 52 template <typename ConstraintType> | |
| 53 bool ConstraintHasMin(const ConstraintType& constraint) { | |
| 54 return constraint.hasMin() || constraint.hasExact(); | |
| 55 } | |
| 56 | |
| 57 template <typename ConstraintType> | |
| 58 auto ConstraintMax(const ConstraintType& constraint) | |
| 59 -> decltype(constraint.max()) { | |
| 60 DCHECK(ConstraintHasMax(constraint)); | |
| 61 return constraint.hasExact() ? constraint.exact() : constraint.max(); | |
| 62 } | |
| 63 | |
| 64 template <typename ConstraintType> | |
| 65 auto ConstraintMin(const ConstraintType& constraint) | |
| 66 -> decltype(constraint.min()) { | |
| 67 DCHECK(ConstraintHasMin(constraint)); | |
| 68 return constraint.hasExact() ? constraint.exact() : constraint.min(); | |
| 69 } | |
| 70 | |
| 71 // Generic fitness distance function between two different values. | |
| 72 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
| 73 double FitnessDistance(double value1, double value2) { | |
| 74 if (std::fabs(value1 - value2) <= blink::DoubleConstraint::kConstraintEpsilon) | |
| 75 return 0.0; | |
| 76 | |
| 77 return std::fabs(value1 - value2) / | |
| 78 std::max(std::fabs(value1), std::fabs(value2)); | |
| 79 } | |
| 80 | |
| 81 // Returns a pair with the minimum and maximum aspect ratios supported by the | |
| 82 // source resolution settings |source_height| and |source_width|, subject to | |
| 83 // given width and height constraints. | |
| 84 void GetSourceAspectRatioRange(int source_height, | |
| 85 int source_width, | |
| 86 const blink::LongConstraint& height_constraint, | |
| 87 const blink::LongConstraint& width_constraint, | |
| 88 double* min_source_aspect_ratio, | |
| 89 double* max_source_aspect_ratio) { | |
| 90 DCHECK_GE(source_height, 1); | |
| 91 DCHECK_GE(source_width, 1); | |
| 92 long min_height = 1; | |
| 93 if (ConstraintHasMin(height_constraint)) | |
| 94 min_height = std::max(min_height, ConstraintMin(height_constraint)); | |
| 95 | |
| 96 long max_height = source_height; | |
| 97 if (ConstraintHasMax(height_constraint)) | |
| 98 max_height = std::min(max_height, ConstraintMax(height_constraint)); | |
| 99 | |
| 100 long min_width = 1; | |
| 101 if (ConstraintHasMin(width_constraint)) | |
| 102 min_width = std::max(min_width, ConstraintMin(width_constraint)); | |
| 103 | |
| 104 long max_width = source_width; | |
| 105 if (ConstraintHasMax(width_constraint)) | |
| 106 max_width = std::min(max_width, ConstraintMax(width_constraint)); | |
| 107 | |
| 108 *min_source_aspect_ratio = | |
| 109 std::max(static_cast<double>(min_width) / static_cast<double>(max_height), | |
| 110 kMinSourceAspectRatio); | |
| 111 *max_source_aspect_ratio = | |
| 112 std::max(static_cast<double>(max_width) / static_cast<double>(min_height), | |
| 113 kMinSourceAspectRatio); | |
| 114 } | |
| 115 | |
| 116 // Returns a custom distance between a string and a string constraint. | |
| 117 // Returns 0 if |value| satisfies |constraint|. HUGE_VAL otherwise. | |
| 118 double StringConstraintSourceDistance(const blink::WebString& value, | |
| 119 const blink::StringConstraint& constraint, | |
| 120 const char** failed_constraint_name) { | |
| 121 if (constraint.matches(value)) | |
| 122 return 0.0; | |
| 123 | |
| 124 if (failed_constraint_name) | |
| 125 *failed_constraint_name = constraint.name(); | |
| 126 return HUGE_VAL; | |
| 127 } | |
| 128 | |
| 129 // Returns a custom distance function suitable for screen dimensions, given | |
| 130 // a |constraint| (e.g. width or height) and a candidate value |source_value|. | |
| 131 // A source can support track resolutions in the range [1, |source_value|], | |
| 132 // using cropping if necessary. | |
| 133 // If the source range and the constraint range are disjoint, return HUGE_VAL. | |
| 134 // If the constraint has maximum, penalize sources that exceed the maximum | |
| 135 // by returning FitnessDistance(|source_value|, maximum). This is intended to | |
| 136 // prefer, among sources that satisfy the constraint, those that have lower | |
| 137 // resource usage. | |
| 138 // Otherwise, return zero. | |
| 139 double ResolutionConstraintSourceDistance( | |
| 140 int source_value, | |
| 141 const blink::LongConstraint& constraint, | |
| 142 const char** failed_constraint_name) { | |
| 143 DCHECK_GE(source_value, 1); | |
| 144 bool constraint_has_max = ConstraintHasMax(constraint); | |
| 145 long constraint_max = constraint_has_max ? ConstraintMax(constraint) : -1; | |
| 146 | |
| 147 // If the intersection between the source range and the constraint range is | |
| 148 // empty, return HUGE_VAL. | |
| 149 if ((constraint_has_max && constraint_max < 1) || | |
| 150 (ConstraintHasMin(constraint) && | |
| 151 source_value < ConstraintMin(constraint))) { | |
| 152 if (failed_constraint_name) | |
| 153 *failed_constraint_name = constraint.name(); | |
| 154 return HUGE_VAL; | |
| 155 } | |
| 156 | |
| 157 // If the source value exceeds the maximum requested, penalize a | |
|
hbos_chromium
2017/02/07 19:17:12
Complete the sentence :)
Guido Urdaneta
2017/02/07 22:31:07
Done.
| |
| 158 if (constraint_has_max && source_value > constraint_max) | |
| 159 return FitnessDistance(source_value, constraint_max); | |
| 160 | |
| 161 return 0.0; | |
| 162 } | |
| 163 | |
| 164 // Returns a custom distance function suitable for frame rate, given | |
| 165 // a |constraint| and a candidate value. | |
| 166 // A source can support track frame rates in the interval (0.0, |source_value|], | |
| 167 // using frame-rate adjustments if necessary. | |
| 168 // If the source range and the constraint range are disjoint, return HUGE_VAL. | |
| 169 // If the constraint has maximum, penalize source frame rates that exceed the | |
| 170 // maximum by returning FitnessDistance(|source_value|, maximum). This is | |
| 171 // intended to prefer, among sources that satisfy the constraint, those that | |
| 172 // have lower resource usage. | |
| 173 // Otherwise, return zero. | |
| 174 double FrameRateConstraintSourceDistance( | |
| 175 double source_value, | |
| 176 const blink::DoubleConstraint& constraint, | |
| 177 const char** failed_constraint_name) { | |
| 178 DCHECK_GT(source_value, 0.0); | |
| 179 bool constraint_has_max = ConstraintHasMax(constraint); | |
| 180 double constraint_max = constraint_has_max ? ConstraintMax(constraint) : -1.0; | |
| 181 | |
| 182 if ((constraint_has_max && constraint_max <= 0.0) || | |
| 183 (ConstraintHasMin(constraint) && | |
| 184 source_value < ConstraintMin(constraint) - | |
| 185 blink::DoubleConstraint::kConstraintEpsilon)) { | |
| 186 if (failed_constraint_name) | |
| 187 *failed_constraint_name = constraint.name(); | |
| 188 return HUGE_VAL; | |
| 189 } | |
| 190 | |
| 191 if (constraint_has_max && source_value > constraint_max) | |
| 192 return FitnessDistance(source_value, constraint_max); | |
| 193 | |
| 194 return 0.0; | |
| 195 } | |
| 196 | |
| 197 // Returns a custom distance function suitable for aspect ratio, given | |
| 198 // the values for the aspect_ratio, width and height constraints, and candidate | |
| 199 // source values for width and height. | |
| 200 // A source can support track resolutions that range from | |
| 201 // min_width x min_height to max_width x max_height | |
| 202 // where | |
| 203 // min_width = max(1, width_constraint.min) | |
| 204 // min_height = max(1, height_constraint.min) | |
| 205 // max_width = min(source_width, width_constraint.max) | |
| 206 // max_height = min(source_height, height_constraint.max) | |
| 207 // The aspect-ratio range supported by the source is determined by the extremes | |
| 208 // of those resolutions. | |
| 209 // min_ar = min_width / max_height. | |
| 210 // max_ar = max_width / min_height. | |
| 211 // | |
| 212 // If the supported range [min_ar, max_ar] and the range specified by the | |
| 213 // aspectRatio constraint are disjoint, return HUGE_VAL. Otherwise, return zero. | |
| 214 double AspectRatioConstraintSourceDistance( | |
| 215 int source_height, | |
| 216 int source_width, | |
| 217 const blink::LongConstraint& height_constraint, | |
| 218 const blink::LongConstraint& width_constraint, | |
| 219 const blink::DoubleConstraint& aspect_ratio_constraint, | |
| 220 const char** failed_constraint_name) { | |
| 221 DCHECK_GT(source_height, 1); | |
| 222 DCHECK_GT(source_width, 1); | |
| 223 | |
| 224 bool ar_constraint_has_min = ConstraintHasMin(aspect_ratio_constraint); | |
| 225 double ar_constraint_min = | |
| 226 ar_constraint_has_min ? ConstraintMin(aspect_ratio_constraint) : -1.0; | |
| 227 bool ar_constraint_has_max = ConstraintHasMax(aspect_ratio_constraint); | |
| 228 double ar_constraint_max = | |
| 229 ar_constraint_has_max ? ConstraintMax(aspect_ratio_constraint) : -1.0; | |
| 230 | |
| 231 double min_source_aspect_ratio; | |
| 232 double max_source_aspect_ratio; | |
| 233 GetSourceAspectRatioRange(source_height, source_width, height_constraint, | |
| 234 width_constraint, &min_source_aspect_ratio, | |
| 235 &max_source_aspect_ratio); | |
| 236 | |
| 237 // If the supported range and the constraint rage are disjoint, return | |
| 238 // HUGE_VAL. | |
| 239 if ((ar_constraint_has_min && | |
| 240 max_source_aspect_ratio < | |
| 241 ar_constraint_min - blink::DoubleConstraint::kConstraintEpsilon) || | |
| 242 (ar_constraint_has_max && | |
| 243 min_source_aspect_ratio > | |
| 244 ar_constraint_max + blink::DoubleConstraint::kConstraintEpsilon)) { | |
| 245 if (failed_constraint_name) | |
| 246 *failed_constraint_name = aspect_ratio_constraint.name(); | |
| 247 return HUGE_VAL; | |
| 248 } | |
| 249 | |
| 250 return 0.0; | |
| 251 } | |
| 252 | |
| 253 // Returns a custom distance for constraints that depend on the device | |
| 254 // characteristics that have a fixed value. | |
| 255 double DeviceSourceDistance( | |
| 256 const std::string& device_id, | |
| 257 ::mojom::FacingMode facing_mode, | |
| 258 const blink::WebMediaTrackConstraintSet& constraint_set, | |
| 259 const char** failed_constraint_name) { | |
|
hbos_chromium
2017/02/07 19:17:12
Should this be called DeviceConstraintSourceDistan
Guido Urdaneta
2017/02/07 22:31:07
The convention is that ConstraintSourceDistance is
| |
| 260 return StringConstraintSourceDistance(blink::WebString::fromASCII(device_id), | |
| 261 constraint_set.deviceId, | |
| 262 failed_constraint_name) + | |
| 263 StringConstraintSourceDistance(ToWebString(facing_mode), | |
| 264 constraint_set.facingMode, | |
| 265 failed_constraint_name); | |
|
hbos_chromium
2017/02/07 19:17:12
HUGE_VAL + HUGE_VAL = HUGE_VAL? DCHECK std::numeri
Guido Urdaneta
2017/02/07 22:31:07
Done. std::numeric_limits<double>::infinity() also
| |
| 266 } | |
| 267 | |
| 268 // Returns a custom distance for constraints that depend on a video-capture | |
| 269 // format. | |
| 270 double FormatSourceDistance( | |
|
hbos_chromium
2017/02/07 19:17:13
Should this be called FormatConstraintSourceDistan
Guido Urdaneta
2017/02/07 22:31:07
ditto.
| |
| 271 const media::VideoCaptureFormat& format, | |
| 272 const blink::WebMediaTrackConstraintSet& constraint_set, | |
| 273 const char** failed_constraint_name) { | |
| 274 return ResolutionConstraintSourceDistance(format.frame_size.height(), | |
| 275 constraint_set.height, | |
| 276 failed_constraint_name) + | |
| 277 ResolutionConstraintSourceDistance(format.frame_size.width(), | |
| 278 constraint_set.width, | |
| 279 failed_constraint_name) + | |
| 280 AspectRatioConstraintSourceDistance( | |
| 281 format.frame_size.height(), format.frame_size.width(), | |
| 282 constraint_set.height, constraint_set.width, | |
| 283 constraint_set.aspectRatio, failed_constraint_name) + | |
| 284 FrameRateConstraintSourceDistance(format.frame_rate, | |
| 285 constraint_set.frameRate, | |
| 286 failed_constraint_name); | |
| 287 } | |
| 288 | |
| 289 // Returns a custom distance function suitable for the googPowerLineFrequency | |
| 290 // constraint, given a |constraint| and a candidate value |source_value|. | |
| 291 // The distance is HUGE_VAL if |source_value| cannot satisfy |constraint|. | |
| 292 // Otherwise, the distance is zero. | |
| 293 double PowerLineFrequencyConstraintSourceDistance( | |
| 294 const blink::LongConstraint& constraint, | |
| 295 media::PowerLineFrequency source_value, | |
| 296 const char** failed_constraint_name) { | |
| 297 bool constraint_has_min = ConstraintHasMin(constraint); | |
| 298 bool constraint_has_max = ConstraintHasMax(constraint); | |
| 299 long constraint_min = constraint_has_min ? ConstraintMin(constraint) : -1L; | |
| 300 long constraint_max = constraint_has_max ? ConstraintMax(constraint) : -1L; | |
| 301 long source_value_long = static_cast<long>(source_value); | |
| 302 | |
| 303 if ((constraint_has_max && source_value_long > constraint_max) || | |
| 304 (constraint_has_min && source_value_long < constraint_min)) { | |
| 305 if (failed_constraint_name) | |
| 306 *failed_constraint_name = constraint.name(); | |
| 307 return HUGE_VAL; | |
| 308 } | |
| 309 | |
| 310 return 0.0; | |
| 311 } | |
| 312 | |
| 313 // Returns a custom distance between a set of candidate settings and a | |
| 314 // constraint set. It is simply the sum of the distances for each individual | |
| 315 // setting in |candidate|. | |
| 316 // If |candidate| cannot satisfy constraint, the distance is HUGE_VAL. | |
| 317 // Otherwise the distance is a finite value. Candidates with lower distance | |
| 318 // satisfy |constraint_set| in a "better" way. | |
| 319 double CandidateSourceDistance( | |
|
hbos_chromium
2017/02/07 19:17:12
Should this be called CandidateConstraintSourceDis
Guido Urdaneta
2017/02/07 22:31:07
ditto.
| |
| 320 const VideoCaptureSourceSettings& candidate, | |
| 321 const blink::WebMediaTrackConstraintSet& constraint_set, | |
| 322 const char** failed_constraint_name) { | |
| 323 return DeviceSourceDistance(candidate.device_id(), candidate.facing_mode(), | |
| 324 constraint_set, failed_constraint_name) + | |
| 325 FormatSourceDistance(candidate.format(), constraint_set, | |
| 326 failed_constraint_name) + | |
| 327 PowerLineFrequencyConstraintSourceDistance( | |
| 328 constraint_set.googPowerLineFrequency, | |
| 329 candidate.power_line_frequency(), failed_constraint_name); | |
| 330 } | |
| 331 | |
| 332 // Returns the fitness distance between |value| and |constraint|. | |
| 333 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
| 334 double StringFitnessDistance(const blink::WebString& value, | |
| 335 const blink::StringConstraint& constraint) { | |
| 336 if (!constraint.hasIdeal()) | |
| 337 return 0.0; | |
| 338 | |
| 339 for (auto& ideal_value : constraint.ideal()) { | |
| 340 if (value == ideal_value) | |
| 341 return 0.0; | |
| 342 } | |
| 343 | |
| 344 return 1.0; | |
| 345 } | |
| 346 | |
| 347 // Returns the fitness distance between |value| and |constraint| for | |
| 348 // resolution constraints (i.e., width and height). | |
| 349 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
| 350 double ResolutionFitnessDistance(long value, | |
| 351 const blink::LongConstraint& constraint) { | |
| 352 if (!constraint.hasIdeal()) | |
| 353 return 0.0; | |
| 354 | |
| 355 // Source resolutions greater than ideal support the ideal value with | |
| 356 // cropping. | |
| 357 if (value >= constraint.ideal()) | |
| 358 return 0.0; | |
| 359 | |
| 360 return FitnessDistance(value, constraint.ideal()); | |
| 361 } | |
| 362 | |
| 363 // Returns the fitness distance between |value| and |constraint| for | |
| 364 // resolution constraints (i.e., width and height), ignoring cropping. | |
| 365 // This measures how well a native resolution supports the idea value. | |
| 366 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
| 367 double NativeResolutionFitnessDistance( | |
| 368 long value, | |
| 369 const blink::LongConstraint& constraint) { | |
| 370 return constraint.hasIdeal() ? FitnessDistance(value, constraint.ideal()) | |
| 371 : 0.0; | |
| 372 } | |
| 373 | |
| 374 // Returns the fitness distance between a source resolution settings | |
| 375 // and the aspectRatio constraint, taking into account resolution restrictions | |
| 376 // on the source imposed by the width and height constraints. | |
| 377 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
| 378 double AspectRatioFitnessDistance( | |
| 379 long source_height, | |
| 380 long source_width, | |
| 381 const blink::LongConstraint& height_constraint, | |
| 382 const blink::LongConstraint& width_constraint, | |
| 383 const blink::DoubleConstraint& aspect_ratio_constraint) { | |
| 384 DCHECK_GT(source_height, 1); | |
| 385 DCHECK_GT(source_width, 1); | |
| 386 | |
| 387 if (!aspect_ratio_constraint.hasIdeal()) | |
| 388 return 0.0; | |
| 389 | |
| 390 double min_source_aspect_ratio; | |
| 391 double max_source_aspect_ratio; | |
| 392 GetSourceAspectRatioRange(source_height, source_width, height_constraint, | |
| 393 width_constraint, &min_source_aspect_ratio, | |
| 394 &max_source_aspect_ratio); | |
| 395 | |
| 396 // If the supported aspect ratio range does not include the ideal aspect | |
| 397 // ratio, compute fitness using the spec formula. | |
| 398 if (max_source_aspect_ratio < | |
| 399 aspect_ratio_constraint.ideal() - | |
| 400 blink::DoubleConstraint::kConstraintEpsilon) { | |
| 401 return FitnessDistance(max_source_aspect_ratio, | |
| 402 aspect_ratio_constraint.ideal()); | |
| 403 } | |
| 404 | |
| 405 if (min_source_aspect_ratio > | |
| 406 aspect_ratio_constraint.ideal() + | |
| 407 blink::DoubleConstraint::kConstraintEpsilon) { | |
| 408 return FitnessDistance(min_source_aspect_ratio, | |
| 409 aspect_ratio_constraint.ideal()); | |
| 410 } | |
| 411 | |
| 412 // Otherwise, the ideal aspect ratio can be supported and the fitness is 0. | |
| 413 return 0.0; | |
| 414 } | |
| 415 | |
| 416 // Returns the fitness distance between |value| and |constraint| for the | |
| 417 // frameRate constraint. | |
| 418 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
| 419 double FrameRateFitnessDistance(double value, | |
| 420 const blink::DoubleConstraint& constraint) { | |
| 421 if (!constraint.hasIdeal()) | |
| 422 return 0.0; | |
| 423 | |
| 424 // Source frame rates greater than ideal support the ideal value using | |
| 425 // frame-rate adjustment. | |
| 426 if (value >= | |
| 427 constraint.ideal() - blink::DoubleConstraint::kConstraintEpsilon) { | |
| 428 return 0.0; | |
| 429 } | |
| 430 | |
| 431 return FitnessDistance(value, constraint.ideal()); | |
| 432 } | |
| 433 | |
| 434 // Returns the fitness distance between |value| and |constraint| for the | |
| 435 // frameRate constraint, ignoring frame-rate adjustment. | |
| 436 // It measures how well the native frame rate supports the ideal value. | |
| 437 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
| 438 double NativeFrameRateFitnessDistance( | |
| 439 double value, | |
| 440 const blink::DoubleConstraint& constraint) { | |
| 441 return constraint.hasIdeal() ? FitnessDistance(value, constraint.ideal()) | |
| 442 : 0.0; | |
| 443 } | |
| 444 | |
| 445 // Returns the fitness distance between |value| and |constraint| for the | |
| 446 // googPowerLineFrequency constraint. | |
| 447 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
| 448 double PowerLineFitnessDistance(long value, | |
| 449 const blink::LongConstraint& constraint) { | |
|
hbos_chromium
2017/02/07 19:17:13
Can you rename this to PowerLineFrequencyFitnessDi
Guido Urdaneta
2017/02/07 22:31:07
Went for PowerLineFrequencyConstraintFitnessDistan
| |
| 450 if (!constraint.hasIdeal()) | |
| 451 return 0.0; | |
| 452 | |
| 453 // This constraint is of type long, but it behaves as an enum. Thus, values | |
| 454 // equal to ideal have fitness 0.0 and any other values have fitness 1.0. | |
| 455 if (value == constraint.ideal()) | |
| 456 return 0.0; | |
| 457 | |
| 458 return 1.0; | |
| 459 } | |
| 460 | |
| 461 // Returns the fitness distance between a settings candidate and a constraint | |
| 462 // set. The returned value is the sum of the fitness distances between each | |
| 463 // setting in |candidate| and the corresponding constraint in |constraint_set|. | |
| 464 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
| 465 double VideoCaptureFitnessDistance( | |
| 466 const VideoCaptureSourceSettings& candidate, | |
| 467 const blink::WebMediaTrackConstraintSet& constraint_set) { | |
| 468 DCHECK(std::isfinite( | |
| 469 CandidateSourceDistance(candidate, constraint_set, nullptr))); | |
| 470 double fitness = 0.0; | |
| 471 fitness += AspectRatioFitnessDistance( | |
| 472 candidate.GetHeight(), candidate.GetWidth(), constraint_set.height, | |
| 473 constraint_set.width, constraint_set.aspectRatio); | |
| 474 fitness += | |
| 475 StringFitnessDistance(candidate.GetDeviceId(), constraint_set.deviceId); | |
| 476 fitness += StringFitnessDistance(candidate.GetFacingMode(), | |
| 477 constraint_set.facingMode); | |
| 478 fitness += FrameRateFitnessDistance(candidate.GetFrameRate(), | |
| 479 constraint_set.frameRate); | |
| 480 fitness += PowerLineFitnessDistance(candidate.GetPowerLineFrequency(), | |
| 481 constraint_set.googPowerLineFrequency); | |
| 482 fitness += | |
| 483 ResolutionFitnessDistance(candidate.GetHeight(), constraint_set.height); | |
| 484 fitness += | |
| 485 ResolutionFitnessDistance(candidate.GetWidth(), constraint_set.width); | |
| 486 | |
| 487 return fitness; | |
| 488 } | |
| 489 | |
| 490 // Returns the native fitness distance between a settings candidate and a | |
| 491 // constraint set. The returned value is the sum of the fitness distances for | |
| 492 // the native values of settings that support a range of values (i.e., width, | |
| 493 // height and frame rate). | |
| 494 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
| 495 double VideoCaptureNativeFitnessDistance( | |
| 496 const VideoCaptureSourceSettings& candidate, | |
| 497 const blink::WebMediaTrackConstraintSet& constraint_set) { | |
| 498 DCHECK(std::isfinite( | |
| 499 CandidateSourceDistance(candidate, constraint_set, nullptr))); | |
| 500 double fitness = 0.0; | |
| 501 fitness += NativeFrameRateFitnessDistance(candidate.GetFrameRate(), | |
| 502 constraint_set.frameRate); | |
| 503 fitness += NativeResolutionFitnessDistance(candidate.GetHeight(), | |
| 504 constraint_set.height); | |
| 505 fitness += NativeResolutionFitnessDistance(candidate.GetWidth(), | |
| 506 constraint_set.width); | |
| 507 | |
| 508 return fitness; | |
| 509 } | |
| 510 | |
| 511 using DistanceVector = std::vector<double>; | |
| 512 | |
| 513 // This function appends additional entries to |distance_vector| based on | |
| 514 // custom distance metrics between |candidate| and some default settings. | |
| 515 // These entries are to be used as the final tie breaker for candidates that | |
| 516 // are equally good according to the spec and the custom distance functions | |
| 517 // between candidates and constraints. | |
| 518 void AppendDistanceFromDefault(const VideoCaptureSourceSettings& candidate, | |
| 519 const VideoCaptureCapabilities& capabilities, | |
| 520 DistanceVector* distance_vector) { | |
| 521 // Favor IDs that appear first in the enumeration. | |
| 522 for (size_t i = 0; i < capabilities.device_capabilities.size(); ++i) { | |
| 523 if (candidate.device_id() == | |
| 524 capabilities.device_capabilities[i]->device_id) { | |
| 525 distance_vector->push_back(i); | |
| 526 break; | |
| 527 } | |
| 528 } | |
| 529 | |
| 530 // Prefer default power-line frequency. | |
| 531 double power_line_frequency_distance = | |
| 532 candidate.power_line_frequency() == | |
| 533 media::PowerLineFrequency::FREQUENCY_DEFAULT | |
| 534 ? 0.0 | |
| 535 : HUGE_VAL; | |
| 536 distance_vector->push_back(power_line_frequency_distance); | |
| 537 | |
| 538 // Prefer a resolution with area close to the default. | |
| 539 int candidate_area = candidate.format().frame_size.GetArea(); | |
| 540 double resolution_distance = | |
| 541 candidate_area == kDefaultResolutionArea | |
| 542 ? 0.0 | |
| 543 : FitnessDistance(candidate_area, kDefaultResolutionArea); | |
| 544 distance_vector->push_back(resolution_distance); | |
| 545 | |
| 546 // Prefer a frame rate close to the default. | |
| 547 double frame_rate_distance = | |
| 548 candidate.format().frame_rate == MediaStreamVideoSource::kDefaultFrameRate | |
| 549 ? 0.0 | |
| 550 : FitnessDistance(candidate.format().frame_rate, | |
| 551 MediaStreamVideoSource::kDefaultFrameRate); | |
| 552 distance_vector->push_back(frame_rate_distance); | |
| 553 } | |
| 554 | |
| 555 } // namespace | |
| 556 | |
| 557 VideoCaptureCapabilities::VideoCaptureCapabilities() = default; | |
| 558 VideoCaptureCapabilities::VideoCaptureCapabilities( | |
| 559 VideoCaptureCapabilities&& other) = default; | |
| 560 VideoCaptureCapabilities::~VideoCaptureCapabilities() = default; | |
| 561 VideoCaptureCapabilities& VideoCaptureCapabilities::operator=( | |
| 562 VideoCaptureCapabilities&& other) = default; | |
| 563 | |
| 564 VideoCaptureSourceSettings::VideoCaptureSourceSettings( | |
| 565 const VideoCaptureSourceSettings& other) = default; | |
| 566 VideoCaptureSourceSettings::VideoCaptureSourceSettings( | |
| 567 VideoCaptureSourceSettings&& other) = default; | |
| 568 VideoCaptureSourceSettings::~VideoCaptureSourceSettings() = default; | |
| 569 VideoCaptureSourceSettings& VideoCaptureSourceSettings::operator=( | |
| 570 const VideoCaptureSourceSettings& other) = default; | |
| 571 VideoCaptureSourceSettings& VideoCaptureSourceSettings::operator=( | |
| 572 VideoCaptureSourceSettings&& other) = default; | |
| 573 | |
| 574 VideoCaptureSourceSettings::VideoCaptureSourceSettings() | |
| 575 : facing_mode_(::mojom::FacingMode::NONE), | |
| 576 power_line_frequency_(media::PowerLineFrequency::FREQUENCY_DEFAULT) {} | |
| 577 | |
| 578 VideoCaptureSourceSettings::VideoCaptureSourceSettings( | |
| 579 const std::string& device_id, | |
| 580 const media::VideoCaptureFormat& format, | |
| 581 ::mojom::FacingMode facing_mode, | |
| 582 media::PowerLineFrequency power_line_frequency) | |
| 583 : device_id_(device_id), | |
| 584 format_(format), | |
| 585 facing_mode_(facing_mode), | |
| 586 power_line_frequency_(power_line_frequency) {} | |
| 587 | |
| 588 blink::WebString VideoCaptureSourceSettings::GetFacingMode() const { | |
| 589 return ToWebString(facing_mode_); | |
| 590 } | |
| 591 | |
| 592 long VideoCaptureSourceSettings::GetPowerLineFrequency() const { | |
| 593 return static_cast<long>(power_line_frequency_); | |
| 594 } | |
| 595 | |
| 596 long VideoCaptureSourceSettings::GetWidth() const { | |
| 597 return format_.frame_size.width(); | |
| 598 } | |
| 599 | |
| 600 long VideoCaptureSourceSettings::GetHeight() const { | |
| 601 return format_.frame_size.height(); | |
| 602 } | |
| 603 | |
| 604 double VideoCaptureSourceSettings::GetFrameRate() const { | |
| 605 return format_.frame_rate; | |
| 606 } | |
| 607 | |
| 608 blink::WebString VideoCaptureSourceSettings::GetDeviceId() const { | |
| 609 return blink::WebString::fromASCII(device_id_.data()); | |
| 610 } | |
| 611 | |
| 612 const char kDefaultFailedConstraintName[] = ""; | |
| 613 | |
| 614 VideoCaptureSourceSelectionResult::VideoCaptureSourceSelectionResult() | |
| 615 : failed_constraint_name(kDefaultFailedConstraintName) {} | |
| 616 VideoCaptureSourceSelectionResult::VideoCaptureSourceSelectionResult( | |
| 617 const VideoCaptureSourceSelectionResult& other) = default; | |
| 618 VideoCaptureSourceSelectionResult::VideoCaptureSourceSelectionResult( | |
| 619 VideoCaptureSourceSelectionResult&& other) = default; | |
| 620 VideoCaptureSourceSelectionResult::~VideoCaptureSourceSelectionResult() = | |
| 621 default; | |
| 622 VideoCaptureSourceSelectionResult& VideoCaptureSourceSelectionResult::operator=( | |
| 623 const VideoCaptureSourceSelectionResult& other) = default; | |
| 624 VideoCaptureSourceSelectionResult& VideoCaptureSourceSelectionResult::operator=( | |
| 625 VideoCaptureSourceSelectionResult&& other) = default; | |
| 626 | |
| 627 VideoCaptureSourceSelectionResult SelectVideoCaptureSourceSettings( | |
| 628 const VideoCaptureCapabilities& capabilities, | |
| 629 const blink::WebMediaConstraints& constraints) { | |
| 630 // A distance vector contains: | |
| 631 // a) For each advanced constraint set, a 0/1 value indicating if the | |
| 632 // candidate satisfies the corresponding constraint set. | |
| 633 // b) Fitness distance for the candidate based on support for the ideal values | |
| 634 // of the basic constraint set. | |
| 635 // c) A custom distance value based on how "well" a candidate satisfies each | |
| 636 // constraint set, including basic and advanced sets. | |
| 637 // d) Native fitness distance for the candidate based on support for the | |
| 638 // ideal values of the basic constraint set using native values for | |
| 639 // settings that can support a range of values. | |
| 640 // e) A custom distance value based on how close the candidate is to default | |
| 641 // settings. | |
| 642 // Parts (a) and (b) are according to spec. Parts (c) to (e) are | |
| 643 // implementation specific and used to break ties. | |
|
hbos_chromium
2017/02/07 19:17:13
Do I get this correctly...?
a) "SourceDistance" a
Guido Urdaneta
2017/02/07 22:31:07
Correct. You understood 100% :)
| |
| 644 DistanceVector best_distance(2 * constraints.advanced().size() + 3 + | |
| 645 kNumDefaultDistanceEntries); | |
| 646 std::fill(best_distance.begin(), best_distance.end(), HUGE_VAL); | |
| 647 VideoCaptureSourceSelectionResult result; | |
| 648 const char* failed_constraint_name = result.failed_constraint_name; | |
| 649 | |
| 650 for (auto& device : capabilities.device_capabilities) { | |
| 651 double basic_device_distance = | |
| 652 DeviceSourceDistance(device->device_id, device->facing_mode, | |
| 653 constraints.basic(), &failed_constraint_name); | |
| 654 if (!std::isfinite(basic_device_distance)) | |
| 655 continue; | |
| 656 | |
| 657 for (auto& format : device->formats) { | |
| 658 double basic_format_distance = FormatSourceDistance( | |
| 659 format, constraints.basic(), &failed_constraint_name); | |
| 660 if (!std::isfinite(basic_format_distance)) | |
| 661 continue; | |
| 662 | |
| 663 for (auto& power_line_frequency : capabilities.power_line_capabilities) { | |
| 664 double basic_power_line_frequency_distance = | |
| 665 PowerLineFrequencyConstraintSourceDistance( | |
| 666 constraints.basic().googPowerLineFrequency, | |
| 667 power_line_frequency, &failed_constraint_name); | |
| 668 if (!std::isfinite(basic_power_line_frequency_distance)) | |
| 669 continue; | |
| 670 | |
| 671 // The candidate satisfies the basic constraint set. | |
| 672 double candidate_basic_custom_distance = | |
| 673 basic_device_distance + basic_format_distance + | |
| 674 basic_power_line_frequency_distance; | |
| 675 DCHECK(std::isfinite(candidate_basic_custom_distance)); | |
| 676 | |
| 677 // Temporary vector to save custom distances for advanced constraints. | |
| 678 // Custom distances must be added to the candidate distance vector after | |
| 679 // all the spec-mandated values. | |
| 680 DistanceVector advanced_custom_distance_vector; | |
| 681 VideoCaptureSourceSettings candidate(device->device_id, format, | |
| 682 device->facing_mode, | |
| 683 power_line_frequency); | |
| 684 DistanceVector candidate_distance_vector; | |
| 685 // First criteria for valid candidates is satisfaction of advanced | |
| 686 // constraint sets. | |
| 687 for (const auto& advanced : constraints.advanced()) { | |
| 688 double custom_distance = | |
| 689 CandidateSourceDistance(candidate, advanced, nullptr); | |
| 690 advanced_custom_distance_vector.push_back(custom_distance); | |
| 691 double spec_distance = std::isfinite(custom_distance) ? 0 : 1; | |
| 692 candidate_distance_vector.push_back(spec_distance); | |
| 693 } | |
| 694 | |
| 695 // Second criterion is fitness distance. | |
| 696 candidate_distance_vector.push_back( | |
| 697 VideoCaptureFitnessDistance(candidate, constraints.basic())); | |
| 698 | |
| 699 // Third criteria are custom distances to constraint sets. | |
| 700 candidate_distance_vector.push_back(candidate_basic_custom_distance); | |
| 701 std::copy(advanced_custom_distance_vector.begin(), | |
| 702 advanced_custom_distance_vector.end(), | |
| 703 std::back_inserter(candidate_distance_vector)); | |
| 704 | |
| 705 // Fourth criteria is native fitness distance. | |
| 706 candidate_distance_vector.push_back( | |
| 707 VideoCaptureNativeFitnessDistance(candidate, constraints.basic())); | |
| 708 | |
| 709 // Final criteria are custom distances to default settings. | |
| 710 AppendDistanceFromDefault(candidate, capabilities, | |
| 711 &candidate_distance_vector); | |
| 712 | |
| 713 DCHECK_EQ(best_distance.size(), candidate_distance_vector.size()); | |
| 714 if (candidate_distance_vector < best_distance) { | |
| 715 best_distance = candidate_distance_vector; | |
| 716 result.settings = std::move(candidate); | |
| 717 result.failed_constraint_name = nullptr; | |
| 718 } | |
| 719 } | |
| 720 } | |
| 721 } | |
| 722 | |
| 723 if (!result.has_value()) | |
| 724 result.failed_constraint_name = failed_constraint_name; | |
| 725 | |
| 726 return result; | |
| 727 } | |
| 728 | |
| 729 } // namespace content | |
| OLD | NEW |