video_capture_params.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2020 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 ASH_SERVICES_RECORDING_VIDEO_CAPTURE_PARAMS_H_
  5. #define ASH_SERVICES_RECORDING_VIDEO_CAPTURE_PARAMS_H_
  6. #include <memory>
  7. #include "components/viz/common/surfaces/frame_sink_id.h"
  8. #include "components/viz/common/surfaces/subtree_capture_id.h"
  9. #include "mojo/public/cpp/bindings/remote.h"
  10. #include "mojo/public/cpp/bindings/struct_forward.h"
  11. #include "services/viz/privileged/mojom/compositing/frame_sink_video_capture.mojom-forward.h"
  12. #include "ui/gfx/geometry/size.h"
  13. namespace gfx {
  14. class Rect;
  15. class Size;
  16. } // namespace gfx
  17. namespace recording {
  18. // Encapsulates the parameters for an ongoing video capture, and knows how to
  19. // initialize a video capturer according to the requested capture source
  20. // (fullscreen, window, or region).
  21. class VideoCaptureParams {
  22. public:
  23. VideoCaptureParams(const VideoCaptureParams&) = delete;
  24. VideoCaptureParams& operator=(const VideoCaptureParams&) = delete;
  25. virtual ~VideoCaptureParams() = default;
  26. // Returns a capture params instance for a fullscreen recording of a root
  27. // window which has the given |frame_sink_id|. Using the given
  28. // |frame_sink_size_dip| and |device_scale_factor|, the resulting video will
  29. // have a resolution equal to the pixel size of the recorded frame sink.
  30. // |frame_sink_id| must be valid.
  31. static std::unique_ptr<VideoCaptureParams> CreateForFullscreenCapture(
  32. viz::FrameSinkId frame_sink_id,
  33. const gfx::Size& frame_sink_size_dip,
  34. float device_scale_factor);
  35. // Returns a capture params instance for a recording of a window. The given
  36. // |frame_sink_id| is either of that window (if it submits compositor frames
  37. // independently), or of the root window it descends from (if it doesn't
  38. // submit its compositor frames). In the latter case, the window must be
  39. // identifiable by a valid |subtree_capture_id| (created by calling
  40. // aura::window::MakeWindowCapturable() before recording starts).
  41. // |window_size_dip| is the initial size of the recorded window, and
  42. // |frame_sink_size_dip| is the current size of the frame sink.
  43. // |device_scale_factor| will be used to compute and perform the capture at
  44. // the pixel size of the window.
  45. // |frame_sink_id| must be valid.
  46. static std::unique_ptr<VideoCaptureParams> CreateForWindowCapture(
  47. viz::FrameSinkId frame_sink_id,
  48. viz::SubtreeCaptureId subtree_capture_id,
  49. const gfx::Size& frame_sink_size_dip,
  50. float device_scale_factor,
  51. const gfx::Size& window_size_dip);
  52. // Returns a capture params instance for a recording of a partial region of a
  53. // root window which has the given |frame_sink_id|. Using the given
  54. // |frame_sink_size_dip| and |device_scale_factor|, the video will be captured
  55. // at a resolution equal to the size of the frame sink in pixels, but the
  56. // resulting video frames will be cropped to the pixel bounds that corresponds
  57. // to the given |crop_region_dip|. |frame_sink_id| must be valid.
  58. static std::unique_ptr<VideoCaptureParams> CreateForRegionCapture(
  59. viz::FrameSinkId frame_sink_id,
  60. const gfx::Size& frame_sink_size_dip,
  61. float device_scale_factor,
  62. const gfx::Rect& crop_region_dip);
  63. const viz::FrameSinkId& frame_sink_id() const { return frame_sink_id_; }
  64. float current_device_scale_factor() const {
  65. return current_device_scale_factor_;
  66. }
  67. gfx::Size current_frame_sink_size_pixels() const {
  68. return current_frame_sink_size_pixels_;
  69. }
  70. // Initializes the given |capturer| (passed by ref) according to the capture
  71. // parameters. The given |capturer| must be bound before calling this.
  72. void InitializeVideoCapturer(
  73. mojo::Remote<viz::mojom::FrameSinkVideoCapturer>& capturer) const;
  74. // Returns the bounds to which a video frame, whose
  75. // |original_frame_visible_rect_pixels| is given, should be cropped. If no
  76. // cropping is desired, |original_frame_visible_rect_pixels| is returned.
  77. virtual gfx::Rect GetVideoFrameVisibleRect(
  78. const gfx::Rect& original_frame_visible_rect_pixels) const;
  79. // Returns the size in pixels with which the video encoder will be
  80. // initialized.
  81. virtual gfx::Size GetVideoSize() const = 0;
  82. // Called when a window, being recorded by the given |capturer|, is moved to
  83. // a different display whose root window has the given |new_frame_sink_id|,
  84. // |new_frame_sink_size_dip|, and |new_device_scale_factor|.
  85. // The default implementation is to *crash* the service, as this is only valid
  86. // when recording a window.
  87. // Returns true if the video encoder needs to be reconfigured, which happens
  88. // when the pixel size of the window changes, resulting in a change in the
  89. // size of the video. Returns false otherwise.
  90. [[nodiscard]] virtual bool OnRecordedWindowChangingRoot(
  91. mojo::Remote<viz::mojom::FrameSinkVideoCapturer>& capturer,
  92. viz::FrameSinkId new_frame_sink_id,
  93. const gfx::Size& new_frame_sink_size_dip,
  94. float new_device_scale_factor);
  95. // Called when a window being recorded by the given |capturer| is resized
  96. // (e.g. due to snapping, maximizing, user resizing, ... etc.) to
  97. // |new_window_size_dip|.
  98. // The default implementation is to *crash* the service, as this is only valid
  99. // when recording a window.
  100. // Returns true if the video encoder needs to be reconfigured, indicating that
  101. // there's a change in the pixel size of the recorded window, resulting in a
  102. // change in the video size. False otherwise.
  103. [[nodiscard]] virtual bool OnRecordedWindowSizeChanged(
  104. mojo::Remote<viz::mojom::FrameSinkVideoCapturer>& capturer,
  105. const gfx::Size& new_window_size_dip);
  106. // Called when the frame sink being recorded changes its size or device scale
  107. // factor to |new_frame_sink_size_dip| or |new_device_scale_factor|
  108. // respectively. The |current_frame_sink_size_pixels_| will be updated, and
  109. // OnVideoSizeMayHaveChanged() will be called. Subclasses should implement
  110. // OnVideoSizeMayHaveChanged() to handle possible changes in the pixel size
  111. // of the recorded surface, and hence a change in the output video size, which
  112. // would require a video encoder reconfiguration.
  113. // Returns true if the video encoder needs to be reconfigured. False
  114. // otherwise.
  115. [[nodiscard]] bool OnFrameSinkSizeChanged(
  116. mojo::Remote<viz::mojom::FrameSinkVideoCapturer>& capturer,
  117. const gfx::Size& new_frame_sink_size_dip,
  118. float new_device_scale_factor);
  119. protected:
  120. VideoCaptureParams(viz::FrameSinkId frame_sink_id,
  121. viz::SubtreeCaptureId subtree_capture_id,
  122. const gfx::Size& current_frame_sink_size,
  123. float device_scale_factor);
  124. // Sets the desired resolution constraints on the given |capturer|. Subclasses
  125. // should override this to request a resolution from |capturer| that matches
  126. // the pixel size of the recorded surface.
  127. virtual void SetCapturerResolutionConstraints(
  128. mojo::Remote<viz::mojom::FrameSinkVideoCapturer>& capturer) const = 0;
  129. // Called when an event occurs that may lead to changing the size of the
  130. // video (such as changing the device scale factor, or resizing a recorded
  131. // window). Implementations should recompute the video size, and return true
  132. // if there was actually a change in the video size that the video encoder
  133. // needs to be reconfigured. Returns false otherwise.
  134. virtual bool OnVideoSizeMayHaveChanged(
  135. mojo::Remote<viz::mojom::FrameSinkVideoCapturer>& capturer) = 0;
  136. // Computes and returns the pixel size of the frame sink according to the
  137. // current values of |current_frame_sink_size_dips_| and
  138. // |current_device_scale_factor_|.
  139. gfx::Size CalculateFrameSinkSizeInPixels() const;
  140. viz::FrameSinkId frame_sink_id_;
  141. const viz::SubtreeCaptureId subtree_capture_id_;
  142. gfx::Size current_frame_sink_size_dips_;
  143. float current_device_scale_factor_;
  144. gfx::Size current_frame_sink_size_pixels_;
  145. };
  146. } // namespace recording
  147. #endif // ASH_SERVICES_RECORDING_VIDEO_CAPTURE_PARAMS_H_