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