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> |
| 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 |
| 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) { |
| 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); |
| 266 } |
| 267 |
| 268 // Returns a custom distance for constraints that depend on a video-capture |
| 269 // format. |
| 270 double FormatSourceDistance( |
| 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( |
| 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 |value| and |constraint| for the |
| 375 // frameRate constraint. |
| 376 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. |
| 377 double AspectRatioFitnessDistance( |
| 378 long source_height, |
| 379 long source_width, |
| 380 const blink::LongConstraint& height_constraint, |
| 381 const blink::LongConstraint& width_constraint, |
| 382 const blink::DoubleConstraint& aspect_ratio_constraint) { |
| 383 DCHECK_GT(source_height, 1); |
| 384 DCHECK_GT(source_width, 1); |
| 385 |
| 386 if (!aspect_ratio_constraint.hasIdeal()) |
| 387 return 0.0; |
| 388 |
| 389 double min_source_aspect_ratio; |
| 390 double max_source_aspect_ratio; |
| 391 GetSourceAspectRatioRange(source_height, source_width, height_constraint, |
| 392 width_constraint, &min_source_aspect_ratio, |
| 393 &max_source_aspect_ratio); |
| 394 |
| 395 // If the supported aspect ratio range does not include the ideal aspect |
| 396 // ratio, compute fitness using the spec formula. |
| 397 if (max_source_aspect_ratio < |
| 398 aspect_ratio_constraint.ideal() - |
| 399 blink::DoubleConstraint::kConstraintEpsilon) { |
| 400 return FitnessDistance(max_source_aspect_ratio, |
| 401 aspect_ratio_constraint.ideal()); |
| 402 } |
| 403 |
| 404 if (min_source_aspect_ratio > |
| 405 aspect_ratio_constraint.ideal() + |
| 406 blink::DoubleConstraint::kConstraintEpsilon) { |
| 407 return FitnessDistance(min_source_aspect_ratio, |
| 408 aspect_ratio_constraint.ideal()); |
| 409 } |
| 410 |
| 411 // Otherwise, the ideal aspect ratio can be supported and the fitness is 0. |
| 412 return 0.0; |
| 413 } |
| 414 |
| 415 // Returns the fitness distance between |value| and |constraint| for the |
| 416 // frameRate constraint. |
| 417 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. |
| 418 double FrameRateFitnessDistance(double value, |
| 419 const blink::DoubleConstraint& constraint) { |
| 420 if (!constraint.hasIdeal()) |
| 421 return 0.0; |
| 422 |
| 423 // Source frame rates greater than ideal support the ideal value using |
| 424 // frame-rate adjustment. |
| 425 if (value >= |
| 426 constraint.ideal() - blink::DoubleConstraint::kConstraintEpsilon) { |
| 427 return 0.0; |
| 428 } |
| 429 |
| 430 return FitnessDistance(value, constraint.ideal()); |
| 431 } |
| 432 |
| 433 // Returns the fitness distance between |value| and |constraint| for the |
| 434 // frameRate constraint, ignoring frame-rate adjustment. |
| 435 // It measures how well the native frame rate supports the ideal value. |
| 436 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. |
| 437 double NativeFrameRateFitnessDistance( |
| 438 double value, |
| 439 const blink::DoubleConstraint& constraint) { |
| 440 return constraint.hasIdeal() ? FitnessDistance(value, constraint.ideal()) |
| 441 : 0.0; |
| 442 } |
| 443 |
| 444 // Returns the fitness distance between |value| and |constraint| for the |
| 445 // googPowerLineFrequency constraint. |
| 446 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. |
| 447 double PowerLineFitnessDistance(long value, |
| 448 const blink::LongConstraint& constraint) { |
| 449 if (!constraint.hasIdeal()) |
| 450 return 0.0; |
| 451 |
| 452 // This constraint is of type long, but it behaves as an enum. Thus, values |
| 453 // equal to ideal have fitness 0.0 and any other values have fitness 1.0. |
| 454 if (value == constraint.ideal()) |
| 455 return 0.0; |
| 456 |
| 457 return 1.0; |
| 458 } |
| 459 |
| 460 // Returns the fitness distance between a settings candidate and a constraint |
| 461 // set. The returned value is the sum of the fitness distances between each |
| 462 // setting in |candidate| and the corresponding constraint in |constraint_set|. |
| 463 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. |
| 464 double VideoCaptureFitnessDistance( |
| 465 const VideoCaptureSourceSettings& candidate, |
| 466 const blink::WebMediaTrackConstraintSet& constraint_set) { |
| 467 DCHECK(std::isfinite( |
| 468 CandidateSourceDistance(candidate, constraint_set, nullptr))); |
| 469 double fitness = 0.0; |
| 470 fitness += AspectRatioFitnessDistance( |
| 471 candidate.GetHeight(), candidate.GetWidth(), constraint_set.height, |
| 472 constraint_set.width, constraint_set.aspectRatio); |
| 473 fitness += |
| 474 StringFitnessDistance(candidate.GetDeviceId(), constraint_set.deviceId); |
| 475 fitness += StringFitnessDistance(candidate.GetFacingMode(), |
| 476 constraint_set.facingMode); |
| 477 fitness += FrameRateFitnessDistance(candidate.GetFrameRate(), |
| 478 constraint_set.frameRate); |
| 479 fitness += PowerLineFitnessDistance(candidate.GetPowerLineFrequency(), |
| 480 constraint_set.googPowerLineFrequency); |
| 481 fitness += |
| 482 ResolutionFitnessDistance(candidate.GetHeight(), constraint_set.height); |
| 483 fitness += |
| 484 ResolutionFitnessDistance(candidate.GetWidth(), constraint_set.width); |
| 485 |
| 486 return fitness; |
| 487 } |
| 488 |
| 489 // Returns the native fitness distance between a settings candidate and a |
| 490 // constraint set. The returned value is the sum of the fitness distances for |
| 491 // the native values of settings that support a range of values (i.e., width, |
| 492 // height and frame rate). |
| 493 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. |
| 494 double VideoCaptureNativeFitnessDistance( |
| 495 const VideoCaptureSourceSettings& candidate, |
| 496 const blink::WebMediaTrackConstraintSet& constraint_set) { |
| 497 DCHECK(std::isfinite( |
| 498 CandidateSourceDistance(candidate, constraint_set, nullptr))); |
| 499 double fitness = 0.0; |
| 500 fitness += NativeFrameRateFitnessDistance(candidate.GetFrameRate(), |
| 501 constraint_set.frameRate); |
| 502 fitness += NativeResolutionFitnessDistance(candidate.GetHeight(), |
| 503 constraint_set.height); |
| 504 fitness += NativeResolutionFitnessDistance(candidate.GetWidth(), |
| 505 constraint_set.width); |
| 506 |
| 507 return fitness; |
| 508 } |
| 509 |
| 510 using DistanceVector = std::vector<double>; |
| 511 |
| 512 // This function appends additional entries to |distance_vector| based on |
| 513 // custom distance metrics between |candidate| and some default settings. |
| 514 // These entries are to be used as the final tie breaker for candidates that |
| 515 // are equally good according to the spec and the custom distance functions |
| 516 // between candidates and constraints. |
| 517 void AppendDistanceFromDefault(const VideoCaptureSourceSettings& candidate, |
| 518 const VideoCaptureCapabilities& capabilities, |
| 519 DistanceVector* distance_vector) { |
| 520 // Favor IDs that appear first in the enumeration. |
| 521 // This is the final tie breaker. |
| 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 VideoCaptureSourceSelectionResult::VideoCaptureSourceSelectionResult() |
| 613 : failed_constraint_name("unknown") {} |
| 614 VideoCaptureSourceSelectionResult::VideoCaptureSourceSelectionResult( |
| 615 const VideoCaptureSourceSelectionResult& other) = default; |
| 616 VideoCaptureSourceSelectionResult::VideoCaptureSourceSelectionResult( |
| 617 VideoCaptureSourceSelectionResult&& other) = default; |
| 618 VideoCaptureSourceSelectionResult::~VideoCaptureSourceSelectionResult() = |
| 619 default; |
| 620 VideoCaptureSourceSelectionResult& VideoCaptureSourceSelectionResult::operator=( |
| 621 const VideoCaptureSourceSelectionResult& other) = default; |
| 622 VideoCaptureSourceSelectionResult& VideoCaptureSourceSelectionResult::operator=( |
| 623 VideoCaptureSourceSelectionResult&& other) = default; |
| 624 |
| 625 VideoCaptureSourceSelectionResult SelectVideoCaptureSourceSettings( |
| 626 const VideoCaptureCapabilities& capabilities, |
| 627 const blink::WebMediaConstraints& constraints) { |
| 628 // A distance vector contains: |
| 629 // a) For each advanced constraint set, a 0/1 value indicating if the |
| 630 // candidate satisfies the corresponding constraint set. |
| 631 // b) Fitness distance for the candidate based on support for the ideal values |
| 632 // of the basic constraint set. |
| 633 // c) A custom distance value based on how "well" a candidate satisfies each |
| 634 // constraint set, including basic and advanced sets. |
| 635 // d) Native fitness distance for the candidate based on support for the |
| 636 // ideal values of the basic constraint set using native values for |
| 637 // settings that can support a range of values. |
| 638 // e) A custom distance value based on how close the candidate is to default |
| 639 // settings. |
| 640 // Parts (a) and (b) are according to spec. Parts (c) to (e) are |
| 641 // implementation specific and used to break ties. |
| 642 DistanceVector best_distance(2 * constraints.advanced().size() + 3 + |
| 643 kNumDefaultDistanceEntries); |
| 644 std::fill(best_distance.begin(), best_distance.end(), HUGE_VAL); |
| 645 VideoCaptureSourceSelectionResult result; |
| 646 const char* failed_constraint_name; |
| 647 |
| 648 for (auto& device : capabilities.device_capabilities) { |
| 649 double basic_device_distance = |
| 650 DeviceSourceDistance(device->device_id, device->facing_mode, |
| 651 constraints.basic(), &failed_constraint_name); |
| 652 if (!std::isfinite(basic_device_distance)) |
| 653 continue; |
| 654 |
| 655 for (auto& format : device->formats) { |
| 656 double basic_format_distance = FormatSourceDistance( |
| 657 format, constraints.basic(), &failed_constraint_name); |
| 658 if (!std::isfinite(basic_format_distance)) |
| 659 continue; |
| 660 |
| 661 for (auto& power_line_frequency : capabilities.power_line_capabilities) { |
| 662 double basic_power_line_frequency_distance = |
| 663 PowerLineFrequencyConstraintSourceDistance( |
| 664 constraints.basic().googPowerLineFrequency, |
| 665 power_line_frequency, &failed_constraint_name); |
| 666 if (!std::isfinite(basic_power_line_frequency_distance)) |
| 667 continue; |
| 668 |
| 669 // The candidate satisfies the basic constraint set. |
| 670 double candidate_basic_custom_distance = |
| 671 basic_device_distance + basic_format_distance + |
| 672 basic_power_line_frequency_distance; |
| 673 DCHECK(std::isfinite(candidate_basic_custom_distance)); |
| 674 |
| 675 // Temporary vector to save custom distances for advanced constraints. |
| 676 // Custom distances must be added to the candidate distance vector after |
| 677 // all the spec-mandated values. |
| 678 DistanceVector advanced_custom_distance_vector; |
| 679 VideoCaptureSourceSettings candidate(device->device_id, format, |
| 680 device->facing_mode, |
| 681 power_line_frequency); |
| 682 DistanceVector candidate_distance_vector; |
| 683 // First criteria for valid candidates is satisfaction of advanced |
| 684 // constraint sets. |
| 685 for (const auto& advanced : constraints.advanced()) { |
| 686 double custom_distance = |
| 687 CandidateSourceDistance(candidate, advanced, nullptr); |
| 688 advanced_custom_distance_vector.push_back(custom_distance); |
| 689 double spec_distance = std::isfinite(custom_distance) ? 0 : 1; |
| 690 candidate_distance_vector.push_back(spec_distance); |
| 691 } |
| 692 |
| 693 // Second criterion is fitness distance. |
| 694 candidate_distance_vector.push_back( |
| 695 VideoCaptureFitnessDistance(candidate, constraints.basic())); |
| 696 |
| 697 // Third criteria are custom distances to constraint sets. |
| 698 candidate_distance_vector.push_back(candidate_basic_custom_distance); |
| 699 std::copy(advanced_custom_distance_vector.begin(), |
| 700 advanced_custom_distance_vector.end(), |
| 701 std::back_inserter(candidate_distance_vector)); |
| 702 |
| 703 // Fourth criteria is native fitness distance. |
| 704 candidate_distance_vector.push_back( |
| 705 VideoCaptureNativeFitnessDistance(candidate, constraints.basic())); |
| 706 |
| 707 // Final criteria are custom distances to default settings. |
| 708 AppendDistanceFromDefault(candidate, capabilities, |
| 709 &candidate_distance_vector); |
| 710 |
| 711 DCHECK_EQ(best_distance.size(), candidate_distance_vector.size()); |
| 712 if (candidate_distance_vector < best_distance) { |
| 713 best_distance = candidate_distance_vector; |
| 714 result.settings = std::move(candidate); |
| 715 result.failed_constraint_name = nullptr; |
| 716 } |
| 717 } |
| 718 } |
| 719 } |
| 720 |
| 721 if (!result.has_value()) |
| 722 result.failed_constraint_name = failed_constraint_name; |
| 723 |
| 724 return result; |
| 725 } |
| 726 |
| 727 } // namespace content |
OLD | NEW |