xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/desktop_and_cursor_composer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MODULES_DESKTOP_CAPTURE_DESKTOP_AND_CURSOR_COMPOSER_H_
12 #define MODULES_DESKTOP_CAPTURE_DESKTOP_AND_CURSOR_COMPOSER_H_
13 
14 #include <memory>
15 #if defined(WEBRTC_USE_GIO)
16 #include "modules/desktop_capture/desktop_capture_metadata.h"
17 #endif  // defined(WEBRTC_USE_GIO)
18 #include "modules/desktop_capture/desktop_capture_options.h"
19 #include "modules/desktop_capture/desktop_capture_types.h"
20 #include "modules/desktop_capture/desktop_capturer.h"
21 #include "modules/desktop_capture/desktop_frame.h"
22 #include "modules/desktop_capture/desktop_geometry.h"
23 #include "modules/desktop_capture/mouse_cursor.h"
24 #include "modules/desktop_capture/mouse_cursor_monitor.h"
25 #include "modules/desktop_capture/shared_memory.h"
26 #include "rtc_base/system/rtc_export.h"
27 
28 namespace webrtc {
29 
30 // A wrapper for DesktopCapturer that also captures mouse using specified
31 // MouseCursorMonitor and renders it on the generated streams.
32 class RTC_EXPORT DesktopAndCursorComposer
33     : public DesktopCapturer,
34       public DesktopCapturer::Callback,
35       public MouseCursorMonitor::Callback {
36  public:
37   // Creates a new composer that captures mouse cursor using
38   // MouseCursorMonitor::Create(options) and renders it into the frames
39   // generated by `desktop_capturer`.
40   DesktopAndCursorComposer(std::unique_ptr<DesktopCapturer> desktop_capturer,
41                            const DesktopCaptureOptions& options);
42 
43   ~DesktopAndCursorComposer() override;
44 
45   DesktopAndCursorComposer(const DesktopAndCursorComposer&) = delete;
46   DesktopAndCursorComposer& operator=(const DesktopAndCursorComposer&) = delete;
47 
48   // Creates a new composer that relies on an external source for cursor shape
49   // and position information via the MouseCursorMonitor::Callback interface.
50   static std::unique_ptr<DesktopAndCursorComposer>
51   CreateWithoutMouseCursorMonitor(
52       std::unique_ptr<DesktopCapturer> desktop_capturer);
53 
54   // DesktopCapturer interface.
55   void Start(DesktopCapturer::Callback* callback) override;
56   void SetSharedMemoryFactory(
57       std::unique_ptr<SharedMemoryFactory> shared_memory_factory) override;
58   void CaptureFrame() override;
59   void SetExcludedWindow(WindowId window) override;
60   bool GetSourceList(SourceList* sources) override;
61   bool SelectSource(SourceId id) override;
62   bool FocusOnSelectedSource() override;
63   bool IsOccluded(const DesktopVector& pos) override;
64 #if defined(WEBRTC_USE_GIO)
65   DesktopCaptureMetadata GetMetadata() override;
66 #endif  // defined(WEBRTC_USE_GIO)
67 
68   // MouseCursorMonitor::Callback interface.
69   void OnMouseCursor(MouseCursor* cursor) override;
70   void OnMouseCursorPosition(const DesktopVector& position) override;
71 
72  private:
73   // Allows test cases to use a fake MouseCursorMonitor implementation.
74   friend class DesktopAndCursorComposerTest;
75 
76   // Constructor to delegate both deprecated and new constructors and allows
77   // test cases to use a fake MouseCursorMonitor implementation.
78   DesktopAndCursorComposer(DesktopCapturer* desktop_capturer,
79                            MouseCursorMonitor* mouse_monitor);
80 
81   // DesktopCapturer::Callback interface.
82   void OnCaptureResult(DesktopCapturer::Result result,
83                        std::unique_ptr<DesktopFrame> frame) override;
84 
85   const std::unique_ptr<DesktopCapturer> desktop_capturer_;
86   const std::unique_ptr<MouseCursorMonitor> mouse_monitor_;
87 
88   DesktopCapturer::Callback* callback_;
89 
90   std::unique_ptr<MouseCursor> cursor_;
91   DesktopVector cursor_position_;
92   DesktopRect previous_cursor_rect_;
93   bool cursor_changed_ = false;
94 };
95 
96 }  // namespace webrtc
97 
98 #endif  // MODULES_DESKTOP_CAPTURE_DESKTOP_AND_CURSOR_COMPOSER_H_
99