blob: cb1b5973ebafe5d95a5104c35ec9a2b39ed8b552 [file] [log] [blame]
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +00001/*
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
kwiberg2bb3afa2016-03-16 15:58:08 -070014#include <memory>
15
zijiehe88ac8272017-03-22 11:38:46 -070016#include "webrtc/modules/desktop_capture/desktop_capture_types.h"
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +000017#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 Lemurc20978e2017-07-06 19:44:34 +020020#include "webrtc/rtc_base/constructormagic.h"
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +000021#include "webrtc/typedefs.h"
22
23namespace webrtc {
24
25// DesktopFrame represents a video frame captured from the screen.
26class 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 He5acd9d02017-08-24 12:41:53 -070034 const DesktopSize& size() const { return size_; }
Zijie Hecd66a772017-07-21 14:13:46 -070035
36 // The top-left of the frame in full desktop coordinates. E.g. the top left
37 // monitor should start from (0, 0).
Zijie He5acd9d02017-08-24 12:41:53 -070038 const DesktopVector& top_left() const { return top_left_; }
39 void set_top_left(const DesktopVector& top_left) { top_left_ = top_left; }
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +000040
41 // Distance in the buffer between two neighboring rows in bytes.
42 int stride() const { return stride_; }
43
44 // Data buffer used for the frame.
45 uint8_t* data() const { return data_; }
46
47 // SharedMemory used for the buffer or NULL if memory is allocated on the
48 // heap. The result is guaranteed to be deleted only after the frame is
49 // deleted (classes that inherit from DesktopFrame must ensure it).
50 SharedMemory* shared_memory() const { return shared_memory_; }
51
52 // Indicates region of the screen that has changed since the previous frame.
53 const DesktopRegion& updated_region() const { return updated_region_; }
54 DesktopRegion* mutable_updated_region() { return &updated_region_; }
55
56 // DPI of the screen being captured. May be set to zero, e.g. if DPI is
57 // unknown.
58 const DesktopVector& dpi() const { return dpi_; }
59 void set_dpi(const DesktopVector& dpi) { dpi_ = dpi; }
60
61 // Time taken to capture the frame in milliseconds.
pkasting@chromium.org16825b12015-01-12 21:51:21 +000062 int64_t capture_time_ms() const { return capture_time_ms_; }
63 void set_capture_time_ms(int64_t time_ms) { capture_time_ms_ = time_ms; }
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +000064
sergeyu@chromium.org5d858192013-11-19 02:15:47 +000065 // Copies pixels from a buffer or another frame. |dest_rect| rect must lay
66 // within bounds of this frame.
qiangchen6f79d842016-09-21 16:44:55 -070067 void CopyPixelsFrom(const uint8_t* src_buffer,
zijiehe2970c2a2016-05-20 22:08:00 -070068 int src_stride,
sergeyu@chromium.org5d858192013-11-19 02:15:47 +000069 const DesktopRect& dest_rect);
70 void CopyPixelsFrom(const DesktopFrame& src_frame,
71 const DesktopVector& src_pos,
72 const DesktopRect& dest_rect);
73
jiayl@webrtc.org0e710702014-11-11 18:15:55 +000074 // A helper to return the data pointer of a frame at the specified position.
75 uint8_t* GetFrameDataAtPos(const DesktopVector& pos) const;
76
zijiehe88ac8272017-03-22 11:38:46 -070077 // The DesktopCapturer implementation which generates current DesktopFrame.
78 // Not all DesktopCapturer implementations set this field; it's set to
79 // kUnknown by default.
80 uint32_t capturer_id() const { return capturer_id_; }
81 void set_capturer_id(uint32_t capturer_id) {
82 capturer_id_ = capturer_id;
83 }
84
Zijie He5acd9d02017-08-24 12:41:53 -070085 // Copies various information from |other|. Anything initialized in
86 // constructor are not copied.
87 // This function is usually used when sharing a source DesktopFrame with
88 // several clients: the original DesktopFrame should be kept unchanged. For
89 // example, BasicDesktopFrame::CopyOf() and SharedDesktopFrame::Share().
90 void CopyFrameInfoFrom(const DesktopFrame& other);
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +000091
Zijie He5acd9d02017-08-24 12:41:53 -070092 // Copies various information from |other|. Anything initialized in
93 // constructor are not copied. Not like CopyFrameInfoFrom() function, this
94 // function uses swap or move constructor to avoid data copy. It won't break
95 // the |other|, but some of its information may be missing after this
96 // operation. E.g. other->updated_region_;
97 // This function is usually used when wrapping a DesktopFrame: the wrapper
98 // instance takes the ownership of |other|, so other components cannot access
99 // |other| anymore. For example, CroppedDesktopFrame and
100 // DesktopFrameWithCursor.
101 void MoveFrameInfoFrom(DesktopFrame* other);
102
103 protected:
104 DesktopFrame(DesktopSize size,
Zijie Hecd66a772017-07-21 14:13:46 -0700105 int stride,
106 uint8_t* data,
107 SharedMemory* shared_memory);
108
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +0000109 // Ownership of the buffers is defined by the classes that inherit from this
110 // class. They must guarantee that the buffer is not deleted before the frame
111 // is deleted.
112 uint8_t* const data_;
113 SharedMemory* const shared_memory_;
114
zijiehe2970c2a2016-05-20 22:08:00 -0700115 private:
Zijie He5acd9d02017-08-24 12:41:53 -0700116 const DesktopSize size_;
zijiehe2970c2a2016-05-20 22:08:00 -0700117 const int stride_;
118
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +0000119 DesktopRegion updated_region_;
Zijie He5acd9d02017-08-24 12:41:53 -0700120 DesktopVector top_left_;
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +0000121 DesktopVector dpi_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000122 int64_t capture_time_ms_;
zijiehe88ac8272017-03-22 11:38:46 -0700123 uint32_t capturer_id_;
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +0000124
henrikg3c089d72015-09-16 05:37:44 -0700125 RTC_DISALLOW_COPY_AND_ASSIGN(DesktopFrame);
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +0000126};
127
128// A DesktopFrame that stores data in the heap.
129class BasicDesktopFrame : public DesktopFrame {
130 public:
131 explicit BasicDesktopFrame(DesktopSize size);
Zijie He09f16c62017-07-31 20:26:11 -0700132
Sergey Ulanov098c1de2015-09-01 11:36:40 -0700133 ~BasicDesktopFrame() override;
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +0000134
sergeyu@chromium.org2df89c02013-10-17 19:47:18 +0000135 // Creates a BasicDesktopFrame that contains copy of |frame|.
Zijie He5acd9d02017-08-24 12:41:53 -0700136 // TODO(zijiehe): Return std::unique_ptr<DesktopFrame>
sergeyu@chromium.org2df89c02013-10-17 19:47:18 +0000137 static DesktopFrame* CopyOf(const DesktopFrame& frame);
138
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +0000139 private:
henrikg3c089d72015-09-16 05:37:44 -0700140 RTC_DISALLOW_COPY_AND_ASSIGN(BasicDesktopFrame);
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +0000141};
142
143// A DesktopFrame that stores data in shared memory.
144class SharedMemoryDesktopFrame : public DesktopFrame {
145 public:
Zijie He09f16c62017-07-31 20:26:11 -0700146 // May return nullptr if |shared_memory_factory| failed to create a
147 // SharedMemory instance.
148 // |shared_memory_factory| should not be nullptr.
kwiberg2bb3afa2016-03-16 15:58:08 -0700149 static std::unique_ptr<DesktopFrame> Create(
sergeyucc9669c2016-02-09 15:13:26 -0800150 DesktopSize size,
151 SharedMemoryFactory* shared_memory_factory);
152
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +0000153 // Takes ownership of |shared_memory|.
Zijie He09f16c62017-07-31 20:26:11 -0700154 // Deprecated, use the next constructor.
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +0000155 SharedMemoryDesktopFrame(DesktopSize size,
156 int stride,
157 SharedMemory* shared_memory);
Zijie He09f16c62017-07-31 20:26:11 -0700158
159 // Preferred.
Zijie He5acd9d02017-08-24 12:41:53 -0700160 SharedMemoryDesktopFrame(DesktopSize size,
Zijie He09f16c62017-07-31 20:26:11 -0700161 int stride,
162 std::unique_ptr<SharedMemory> shared_memory);
163
Sergey Ulanov098c1de2015-09-01 11:36:40 -0700164 ~SharedMemoryDesktopFrame() override;
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +0000165
166 private:
Zijie He09f16c62017-07-31 20:26:11 -0700167 // Avoid unexpected order of parameter evaluation.
168 // Executing both std::unique_ptr<T>::operator->() and
169 // std::unique_ptr<T>::release() in the member initializer list is not safe.
170 // Depends on the order of parameter evaluation,
171 // std::unique_ptr<T>::operator->() may trigger assertion failure if it has
172 // been evaluated after std::unique_ptr<T>::release(). By using this
173 // constructor, std::unique_ptr<T>::operator->() won't be involved anymore.
174 SharedMemoryDesktopFrame(DesktopRect rect,
175 int stride,
176 SharedMemory* shared_memory);
177
henrikg3c089d72015-09-16 05:37:44 -0700178 RTC_DISALLOW_COPY_AND_ASSIGN(SharedMemoryDesktopFrame);
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +0000179};
180
181} // namespace webrtc
182
183#endif // WEBRTC_MODULES_DESKTOP_CAPTURE_DESKTOP_FRAME_H_
184