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 | |
| 11 | #ifndef WEBRTC_MODULES_DESKTOP_CAPTURE_DESKTOP_FRAME_H_ |
| 12 | #define WEBRTC_MODULES_DESKTOP_CAPTURE_DESKTOP_FRAME_H_ |
| 13 | |
kwiberg | 2bb3afa | 2016-03-16 15:58:08 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
zijiehe | 88ac827 | 2017-03-22 11:38:46 -0700 | [diff] [blame] | 16 | #include "webrtc/modules/desktop_capture/desktop_capture_types.h" |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 17 | #include "webrtc/modules/desktop_capture/desktop_geometry.h" |
| 18 | #include "webrtc/modules/desktop_capture/desktop_region.h" |
| 19 | #include "webrtc/modules/desktop_capture/shared_memory.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 20 | #include "webrtc/rtc_base/constructormagic.h" |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 21 | #include "webrtc/typedefs.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | // DesktopFrame represents a video frame captured from the screen. |
| 26 | class DesktopFrame { |
| 27 | public: |
| 28 | // DesktopFrame objects always hold RGBA data. |
| 29 | static const int kBytesPerPixel = 4; |
| 30 | |
| 31 | virtual ~DesktopFrame(); |
| 32 | |
| 33 | // Size of the frame. |
Zijie He | cd66a77 | 2017-07-21 14:13:46 -0700 | [diff] [blame] | 34 | DesktopSize size() const { return rect_.size(); } |
| 35 | |
| 36 | // The top-left of the frame in full desktop coordinates. E.g. the top left |
| 37 | // monitor should start from (0, 0). |
| 38 | DesktopVector top_left() const { return rect_.top_left(); } |
| 39 | |
| 40 | // Rectangle covered by the frame. |
| 41 | const DesktopRect& rect() const { return rect_; } |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 42 | |
| 43 | // Distance in the buffer between two neighboring rows in bytes. |
| 44 | int stride() const { return stride_; } |
| 45 | |
| 46 | // Data buffer used for the frame. |
| 47 | uint8_t* data() const { return data_; } |
| 48 | |
| 49 | // SharedMemory used for the buffer or NULL if memory is allocated on the |
| 50 | // heap. The result is guaranteed to be deleted only after the frame is |
| 51 | // deleted (classes that inherit from DesktopFrame must ensure it). |
| 52 | SharedMemory* shared_memory() const { return shared_memory_; } |
| 53 | |
| 54 | // Indicates region of the screen that has changed since the previous frame. |
| 55 | const DesktopRegion& updated_region() const { return updated_region_; } |
| 56 | DesktopRegion* mutable_updated_region() { return &updated_region_; } |
| 57 | |
| 58 | // DPI of the screen being captured. May be set to zero, e.g. if DPI is |
| 59 | // unknown. |
| 60 | const DesktopVector& dpi() const { return dpi_; } |
| 61 | void set_dpi(const DesktopVector& dpi) { dpi_ = dpi; } |
| 62 | |
| 63 | // Time taken to capture the frame in milliseconds. |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 64 | int64_t capture_time_ms() const { return capture_time_ms_; } |
| 65 | void set_capture_time_ms(int64_t time_ms) { capture_time_ms_ = time_ms; } |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 66 | |
sergeyu@chromium.org | 5d85819 | 2013-11-19 02:15:47 +0000 | [diff] [blame] | 67 | // Copies pixels from a buffer or another frame. |dest_rect| rect must lay |
| 68 | // within bounds of this frame. |
qiangchen | 6f79d84 | 2016-09-21 16:44:55 -0700 | [diff] [blame] | 69 | void CopyPixelsFrom(const uint8_t* src_buffer, |
zijiehe | 2970c2a | 2016-05-20 22:08:00 -0700 | [diff] [blame] | 70 | int src_stride, |
sergeyu@chromium.org | 5d85819 | 2013-11-19 02:15:47 +0000 | [diff] [blame] | 71 | const DesktopRect& dest_rect); |
| 72 | void CopyPixelsFrom(const DesktopFrame& src_frame, |
| 73 | const DesktopVector& src_pos, |
| 74 | const DesktopRect& dest_rect); |
| 75 | |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 76 | // A helper to return the data pointer of a frame at the specified position. |
| 77 | uint8_t* GetFrameDataAtPos(const DesktopVector& pos) const; |
| 78 | |
zijiehe | 88ac827 | 2017-03-22 11:38:46 -0700 | [diff] [blame] | 79 | // The DesktopCapturer implementation which generates current DesktopFrame. |
| 80 | // Not all DesktopCapturer implementations set this field; it's set to |
| 81 | // kUnknown by default. |
| 82 | uint32_t capturer_id() const { return capturer_id_; } |
| 83 | void set_capturer_id(uint32_t capturer_id) { |
| 84 | capturer_id_ = capturer_id; |
| 85 | } |
| 86 | |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 87 | protected: |
Zijie He | cd66a77 | 2017-07-21 14:13:46 -0700 | [diff] [blame] | 88 | // Deprecated, use the constructor below. |
Zijie He | 09f16c6 | 2017-07-31 20:26:11 -0700 | [diff] [blame^] | 89 | // TODO(zijiehe): Remove deprecated constructors. DesktopFrame should describe |
| 90 | // its own position in the system. See |
| 91 | // https://bugs.chromium.org/p/webrtc/issues/detail?id=7950 |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 92 | DesktopFrame(DesktopSize size, |
| 93 | int stride, |
| 94 | uint8_t* data, |
| 95 | SharedMemory* shared_memory); |
| 96 | |
Zijie He | cd66a77 | 2017-07-21 14:13:46 -0700 | [diff] [blame] | 97 | // Preferred. |
| 98 | DesktopFrame(DesktopRect rect, |
| 99 | int stride, |
| 100 | uint8_t* data, |
| 101 | SharedMemory* shared_memory); |
| 102 | |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 103 | // Ownership of the buffers is defined by the classes that inherit from this |
| 104 | // class. They must guarantee that the buffer is not deleted before the frame |
| 105 | // is deleted. |
| 106 | uint8_t* const data_; |
| 107 | SharedMemory* const shared_memory_; |
| 108 | |
zijiehe | 2970c2a | 2016-05-20 22:08:00 -0700 | [diff] [blame] | 109 | private: |
Zijie He | cd66a77 | 2017-07-21 14:13:46 -0700 | [diff] [blame] | 110 | const DesktopRect rect_; |
zijiehe | 2970c2a | 2016-05-20 22:08:00 -0700 | [diff] [blame] | 111 | const int stride_; |
| 112 | |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 113 | DesktopRegion updated_region_; |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 114 | DesktopVector dpi_; |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 115 | int64_t capture_time_ms_; |
zijiehe | 88ac827 | 2017-03-22 11:38:46 -0700 | [diff] [blame] | 116 | uint32_t capturer_id_; |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 117 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 118 | RTC_DISALLOW_COPY_AND_ASSIGN(DesktopFrame); |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | // A DesktopFrame that stores data in the heap. |
| 122 | class BasicDesktopFrame : public DesktopFrame { |
| 123 | public: |
Zijie He | 09f16c6 | 2017-07-31 20:26:11 -0700 | [diff] [blame^] | 124 | // Deprecated, use the next constructor. |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 125 | explicit BasicDesktopFrame(DesktopSize size); |
Zijie He | 09f16c6 | 2017-07-31 20:26:11 -0700 | [diff] [blame^] | 126 | |
| 127 | // Preferred. |
| 128 | explicit BasicDesktopFrame(DesktopRect rect); |
| 129 | |
Sergey Ulanov | 098c1de | 2015-09-01 11:36:40 -0700 | [diff] [blame] | 130 | ~BasicDesktopFrame() override; |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 131 | |
sergeyu@chromium.org | 2df89c0 | 2013-10-17 19:47:18 +0000 | [diff] [blame] | 132 | // Creates a BasicDesktopFrame that contains copy of |frame|. |
| 133 | static DesktopFrame* CopyOf(const DesktopFrame& frame); |
| 134 | |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 135 | private: |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 136 | RTC_DISALLOW_COPY_AND_ASSIGN(BasicDesktopFrame); |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | // A DesktopFrame that stores data in shared memory. |
| 140 | class SharedMemoryDesktopFrame : public DesktopFrame { |
| 141 | public: |
Zijie He | 09f16c6 | 2017-07-31 20:26:11 -0700 | [diff] [blame^] | 142 | // May return nullptr if |shared_memory_factory| failed to create a |
| 143 | // SharedMemory instance. |
| 144 | // |shared_memory_factory| should not be nullptr. |
| 145 | // Deprecated, use the next Create() function. |
kwiberg | 2bb3afa | 2016-03-16 15:58:08 -0700 | [diff] [blame] | 146 | static std::unique_ptr<DesktopFrame> Create( |
sergeyu | cc9669c | 2016-02-09 15:13:26 -0800 | [diff] [blame] | 147 | DesktopSize size, |
| 148 | SharedMemoryFactory* shared_memory_factory); |
| 149 | |
Zijie He | 09f16c6 | 2017-07-31 20:26:11 -0700 | [diff] [blame^] | 150 | // Preferred. |
zijiehe | 2970c2a | 2016-05-20 22:08:00 -0700 | [diff] [blame] | 151 | static std::unique_ptr<DesktopFrame> Create( |
Zijie He | 09f16c6 | 2017-07-31 20:26:11 -0700 | [diff] [blame^] | 152 | DesktopRect rect, |
| 153 | SharedMemoryFactory* shared_memory_factory); |
zijiehe | 2970c2a | 2016-05-20 22:08:00 -0700 | [diff] [blame] | 154 | |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 155 | // Takes ownership of |shared_memory|. |
Zijie He | 09f16c6 | 2017-07-31 20:26:11 -0700 | [diff] [blame^] | 156 | // Deprecated, use the next constructor. |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 157 | SharedMemoryDesktopFrame(DesktopSize size, |
| 158 | int stride, |
| 159 | SharedMemory* shared_memory); |
Zijie He | 09f16c6 | 2017-07-31 20:26:11 -0700 | [diff] [blame^] | 160 | |
| 161 | // Preferred. |
| 162 | SharedMemoryDesktopFrame(DesktopRect rect, |
| 163 | int stride, |
| 164 | std::unique_ptr<SharedMemory> shared_memory); |
| 165 | |
Sergey Ulanov | 098c1de | 2015-09-01 11:36:40 -0700 | [diff] [blame] | 166 | ~SharedMemoryDesktopFrame() override; |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 167 | |
| 168 | private: |
Zijie He | 09f16c6 | 2017-07-31 20:26:11 -0700 | [diff] [blame^] | 169 | // Avoid unexpected order of parameter evaluation. |
| 170 | // Executing both std::unique_ptr<T>::operator->() and |
| 171 | // std::unique_ptr<T>::release() in the member initializer list is not safe. |
| 172 | // Depends on the order of parameter evaluation, |
| 173 | // std::unique_ptr<T>::operator->() may trigger assertion failure if it has |
| 174 | // been evaluated after std::unique_ptr<T>::release(). By using this |
| 175 | // constructor, std::unique_ptr<T>::operator->() won't be involved anymore. |
| 176 | SharedMemoryDesktopFrame(DesktopRect rect, |
| 177 | int stride, |
| 178 | SharedMemory* shared_memory); |
| 179 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 180 | RTC_DISALLOW_COPY_AND_ASSIGN(SharedMemoryDesktopFrame); |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | } // namespace webrtc |
| 184 | |
| 185 | #endif // WEBRTC_MODULES_DESKTOP_CAPTURE_DESKTOP_FRAME_H_ |
| 186 | |