desktop_capturer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #ifndef MODULES_DESKTOP_CAPTURE_DESKTOP_CAPTURER_H_
  11. #define MODULES_DESKTOP_CAPTURE_DESKTOP_CAPTURER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <memory>
  15. #include <string>
  16. #include <type_traits>
  17. #include <vector>
  18. #include "modules/desktop_capture/desktop_capture_types.h"
  19. #include "modules/desktop_capture/desktop_frame.h"
  20. #include "modules/desktop_capture/shared_memory.h"
  21. #include "rtc_base/system/rtc_export.h"
  22. namespace webrtc {
  23. class DesktopCaptureOptions;
  24. class DesktopFrame;
  25. // Abstract interface for screen and window capturers.
  26. class RTC_EXPORT DesktopCapturer {
  27. public:
  28. enum class Result {
  29. // The frame was captured successfully.
  30. SUCCESS,
  31. // There was a temporary error. The caller should continue calling
  32. // CaptureFrame(), in the expectation that it will eventually recover.
  33. ERROR_TEMPORARY,
  34. // Capture has failed and will keep failing if the caller tries calling
  35. // CaptureFrame() again.
  36. ERROR_PERMANENT,
  37. MAX_VALUE = ERROR_PERMANENT
  38. };
  39. // Interface that must be implemented by the DesktopCapturer consumers.
  40. class Callback {
  41. public:
  42. // Called after a frame has been captured. |frame| is not nullptr if and
  43. // only if |result| is SUCCESS.
  44. virtual void OnCaptureResult(Result result,
  45. std::unique_ptr<DesktopFrame> frame) = 0;
  46. protected:
  47. virtual ~Callback() {}
  48. };
  49. typedef intptr_t SourceId;
  50. static_assert(std::is_same<SourceId, ScreenId>::value,
  51. "SourceId should be a same type as ScreenId.");
  52. struct Source {
  53. // The unique id to represent a Source of current DesktopCapturer.
  54. SourceId id;
  55. // Title of the window or screen in UTF-8 encoding, maybe empty. This field
  56. // should not be used to identify a source.
  57. std::string title;
  58. };
  59. typedef std::vector<Source> SourceList;
  60. virtual ~DesktopCapturer();
  61. // Called at the beginning of a capturing session. |callback| must remain
  62. // valid until capturer is destroyed.
  63. virtual void Start(Callback* callback) = 0;
  64. // Sets SharedMemoryFactory that will be used to create buffers for the
  65. // captured frames. The factory can be invoked on a thread other than the one
  66. // where CaptureFrame() is called. It will be destroyed on the same thread.
  67. // Shared memory is currently supported only by some DesktopCapturer
  68. // implementations.
  69. virtual void SetSharedMemoryFactory(
  70. std::unique_ptr<SharedMemoryFactory> shared_memory_factory);
  71. // Captures next frame, and involve callback provided by Start() function.
  72. // Pending capture requests are canceled when DesktopCapturer is deleted.
  73. virtual void CaptureFrame() = 0;
  74. // Sets the window to be excluded from the captured image in the future
  75. // Capture calls. Used to exclude the screenshare notification window for
  76. // screen capturing.
  77. virtual void SetExcludedWindow(WindowId window);
  78. // TODO(zijiehe): Following functions should be pure virtual. The default
  79. // implementations are for backward compatibility only. Remove default
  80. // implementations once all DesktopCapturer implementations in Chromium have
  81. // implemented these functions.
  82. // Gets a list of sources current capturer supports. Returns false in case of
  83. // a failure.
  84. // For DesktopCapturer implementations to capture screens, this function
  85. // should return monitors.
  86. // For DesktopCapturer implementations to capture windows, this function
  87. // should only return root windows owned by applications.
  88. virtual bool GetSourceList(SourceList* sources);
  89. // Selects a source to be captured. Returns false in case of a failure (e.g.
  90. // if there is no source with the specified type and id.)
  91. virtual bool SelectSource(SourceId id);
  92. // Brings the selected source to the front and sets the input focus on it.
  93. // Returns false in case of a failure or no source has been selected or the
  94. // implementation does not support this functionality.
  95. virtual bool FocusOnSelectedSource();
  96. // Returns true if the |pos| on the selected source is covered by other
  97. // elements on the display, and is not visible to the users.
  98. // |pos| is in full desktop coordinates, i.e. the top-left monitor always
  99. // starts from (0, 0).
  100. // The return value if |pos| is out of the scope of the source is undefined.
  101. virtual bool IsOccluded(const DesktopVector& pos);
  102. // Creates a DesktopCapturer instance which targets to capture windows.
  103. static std::unique_ptr<DesktopCapturer> CreateWindowCapturer(
  104. const DesktopCaptureOptions& options);
  105. // Creates a DesktopCapturer instance which targets to capture screens.
  106. static std::unique_ptr<DesktopCapturer> CreateScreenCapturer(
  107. const DesktopCaptureOptions& options);
  108. #if defined(WEBRTC_USE_PIPEWIRE) || defined(WEBRTC_USE_X11)
  109. static bool IsRunningUnderWayland();
  110. #endif // defined(WEBRTC_USE_PIPEWIRE) || defined(WEBRTC_USE_X11)
  111. protected:
  112. // CroppingWindowCapturer needs to create raw capturers without wrappers, so
  113. // the following two functions are protected.
  114. // Creates a platform specific DesktopCapturer instance which targets to
  115. // capture windows.
  116. static std::unique_ptr<DesktopCapturer> CreateRawWindowCapturer(
  117. const DesktopCaptureOptions& options);
  118. // Creates a platform specific DesktopCapturer instance which targets to
  119. // capture screens.
  120. static std::unique_ptr<DesktopCapturer> CreateRawScreenCapturer(
  121. const DesktopCaptureOptions& options);
  122. };
  123. } // namespace webrtc
  124. #endif // MODULES_DESKTOP_CAPTURE_DESKTOP_CAPTURER_H_