capture_resolution_chooser.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.
  4. #ifndef MEDIA_CAPTURE_CONTENT_CAPTURE_RESOLUTION_CHOOSER_H_
  5. #define MEDIA_CAPTURE_CONTENT_CAPTURE_RESOLUTION_CHOOSER_H_
  6. #include <vector>
  7. #include "media/capture/capture_export.h"
  8. #include "ui/gfx/geometry/size.h"
  9. namespace media {
  10. // Encapsulates the logic that determines the capture frame resolution based on:
  11. // 1. The configured minimum/maximum frame resolution.
  12. // 2. Whether the capture frame resolution must be of a fixed aspect ratio.
  13. // 3. Changes to resolution of the source content.
  14. // 4. Changes to the (externally-computed) target data volume, provided in
  15. // terms of the number of pixels in the frame.
  16. //
  17. // In variable-resolution use cases, the capture sizes are "snapped" to a small
  18. // (i.e., usually less than a dozen) set of possibilities. This is to prevent
  19. // the end-to-end system from having to deal with rapidly-changing video frame
  20. // resolutions that results from providing a fine-grained range of values. The
  21. // possibile snapped frame sizes are computed relative to the resolution of the
  22. // source content: They are the same or smaller in size, and are of the same
  23. // aspect ratio.
  24. class CAPTURE_EXPORT CaptureResolutionChooser {
  25. public:
  26. // Default constructor. Capture size is fixed at kDefaultCaptureSize until
  27. // SetConstraints() is called.
  28. CaptureResolutionChooser();
  29. ~CaptureResolutionChooser();
  30. // Returns the current capture frame resolution to use.
  31. const gfx::Size& capture_size() const { return capture_size_; }
  32. // Specifies a new range of acceptable capture resolutions and whether a fixed
  33. // aspect ratio is required. When |min_frame_size| is equal to
  34. // |max_frame_size|, capture resolution will be held constant. If a fixed
  35. // aspect ratio is required, the aspect ratio of |max_frame_size| is used.
  36. void SetConstraints(const gfx::Size& min_frame_size,
  37. const gfx::Size& max_frame_size,
  38. bool use_fixed_aspect_ratio);
  39. // Returns the currently-set source size.
  40. const gfx::Size& source_size() const { return source_size_; }
  41. // Updates the capture size based on a change in the resolution of the source
  42. // content.
  43. void SetSourceSize(const gfx::Size& source_size);
  44. // Updates the capture size to target the given frame area, in terms of
  45. // gfx::Size::GetArea(). The initial target frame area is the maximum int
  46. // (i.e., always target the source size).
  47. void SetTargetFrameArea(int area);
  48. // Search functions to, given a frame |area|, return the nearest snapped frame
  49. // size, or N size steps up/down. Snapped frame sizes are based on the
  50. // current source size.
  51. gfx::Size FindNearestFrameSize(int area) const;
  52. gfx::Size FindLargerFrameSize(int area, int num_steps_up) const;
  53. gfx::Size FindSmallerFrameSize(int area, int num_steps_down) const;
  54. // The default capture size, if SetConstraints() is never called.
  55. static constexpr gfx::Size kDefaultCaptureSize = gfx::Size(640, 360);
  56. private:
  57. // Called after any update that requires |capture_size_| be re-computed.
  58. void RecomputeCaptureSize();
  59. // Recomputes the |snapped_sizes_| cache.
  60. void UpdateSnappedFrameSizes();
  61. // Hard constraints.
  62. gfx::Size min_frame_size_;
  63. gfx::Size max_frame_size_;
  64. // If true, adjust the |source_size_| to match the aspect ratio of
  65. // |max_frame_size_| before computing the snapped frame sizes.
  66. bool apply_aspect_ratio_adjustment_;
  67. // Current source size.
  68. gfx::Size source_size_;
  69. // |capture_size_| will be computed such that its area is as close to this
  70. // value as possible.
  71. int target_area_;
  72. // The current computed capture frame resolution.
  73. gfx::Size capture_size_;
  74. // Cache of the set of possible values |capture_size_| can have, in order from
  75. // smallest to largest. This is recomputed whenever UpdateSnappedFrameSizes()
  76. // is called.
  77. std::vector<gfx::Size> snapped_sizes_;
  78. };
  79. } // namespace media
  80. #endif // MEDIA_CAPTURE_CONTENT_CAPTURE_RESOLUTION_CHOOSER_H_