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