desktop_frame.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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_FRAME_H_
  11. #define MODULES_DESKTOP_CAPTURE_DESKTOP_FRAME_H_
  12. #include <stdint.h>
  13. #include <memory>
  14. #include <vector>
  15. #include "modules/desktop_capture/desktop_geometry.h"
  16. #include "modules/desktop_capture/desktop_region.h"
  17. #include "modules/desktop_capture/shared_memory.h"
  18. #include "rtc_base/constructor_magic.h"
  19. #include "rtc_base/system/rtc_export.h"
  20. namespace webrtc {
  21. const float kStandardDPI = 96.0f;
  22. // DesktopFrame represents a video frame captured from the screen.
  23. class RTC_EXPORT DesktopFrame {
  24. public:
  25. // DesktopFrame objects always hold RGBA data.
  26. static const int kBytesPerPixel = 4;
  27. virtual ~DesktopFrame();
  28. // Returns the rectangle in full desktop coordinates to indicate it covers
  29. // the area of top_left() to top_letf() + size() / scale_factor().
  30. DesktopRect rect() const;
  31. // Returns the scale factor from DIPs to physical pixels of the frame.
  32. // Assumes same scale in both X and Y directions at present.
  33. float scale_factor() const;
  34. // Size of the frame. In physical coordinates, mapping directly from the
  35. // underlying buffer.
  36. const DesktopSize& size() const { return size_; }
  37. // The top-left of the frame in full desktop coordinates. E.g. the top left
  38. // monitor should start from (0, 0). The desktop coordinates may be scaled by
  39. // OS, but this is always consistent with the MouseCursorMonitor.
  40. const DesktopVector& top_left() const { return top_left_; }
  41. void set_top_left(const DesktopVector& top_left) { top_left_ = top_left; }
  42. // Distance in the buffer between two neighboring rows in bytes.
  43. int stride() const { return stride_; }
  44. // Data buffer used for the frame.
  45. uint8_t* data() const { return data_; }
  46. // SharedMemory used for the buffer or NULL if memory is allocated on the
  47. // heap. The result is guaranteed to be deleted only after the frame is
  48. // deleted (classes that inherit from DesktopFrame must ensure it).
  49. SharedMemory* shared_memory() const { return shared_memory_; }
  50. // Indicates region of the screen that has changed since the previous frame.
  51. const DesktopRegion& updated_region() const { return updated_region_; }
  52. DesktopRegion* mutable_updated_region() { return &updated_region_; }
  53. // DPI of the screen being captured. May be set to zero, e.g. if DPI is
  54. // unknown.
  55. const DesktopVector& dpi() const { return dpi_; }
  56. void set_dpi(const DesktopVector& dpi) { dpi_ = dpi; }
  57. // Time taken to capture the frame in milliseconds.
  58. int64_t capture_time_ms() const { return capture_time_ms_; }
  59. void set_capture_time_ms(int64_t time_ms) { capture_time_ms_ = time_ms; }
  60. // Copies pixels from a buffer or another frame. |dest_rect| rect must lay
  61. // within bounds of this frame.
  62. void CopyPixelsFrom(const uint8_t* src_buffer,
  63. int src_stride,
  64. const DesktopRect& dest_rect);
  65. void CopyPixelsFrom(const DesktopFrame& src_frame,
  66. const DesktopVector& src_pos,
  67. const DesktopRect& dest_rect);
  68. // Copies pixels from another frame, with the copied & overwritten regions
  69. // representing the intersection between the two frames. Returns true if
  70. // pixels were copied, or false if there's no intersection. The scale factors
  71. // represent the ratios between pixel space & offset coordinate space (e.g.
  72. // 2.0 would indicate the frames are scaled down by 50% for display, so any
  73. // offset between their origins should be doubled).
  74. bool CopyIntersectingPixelsFrom(const DesktopFrame& src_frame,
  75. double horizontal_scale,
  76. double vertical_scale);
  77. // A helper to return the data pointer of a frame at the specified position.
  78. uint8_t* GetFrameDataAtPos(const DesktopVector& pos) const;
  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) { capturer_id_ = capturer_id; }
  84. // Copies various information from |other|. Anything initialized in
  85. // constructor are not copied.
  86. // This function is usually used when sharing a source DesktopFrame with
  87. // several clients: the original DesktopFrame should be kept unchanged. For
  88. // example, BasicDesktopFrame::CopyOf() and SharedDesktopFrame::Share().
  89. void CopyFrameInfoFrom(const DesktopFrame& other);
  90. // Copies various information from |other|. Anything initialized in
  91. // constructor are not copied. Not like CopyFrameInfoFrom() function, this
  92. // function uses swap or move constructor to avoid data copy. It won't break
  93. // the |other|, but some of its information may be missing after this
  94. // operation. E.g. other->updated_region_;
  95. // This function is usually used when wrapping a DesktopFrame: the wrapper
  96. // instance takes the ownership of |other|, so other components cannot access
  97. // |other| anymore. For example, CroppedDesktopFrame and
  98. // DesktopFrameWithCursor.
  99. void MoveFrameInfoFrom(DesktopFrame* other);
  100. // Set and get the ICC profile of the frame data pixels. Useful to build the
  101. // a ColorSpace object from clients of webrtc library like chromium. The
  102. // format of an ICC profile is defined in the following specification
  103. // http://www.color.org/specification/ICC1v43_2010-12.pdf.
  104. const std::vector<uint8_t>& icc_profile() const { return icc_profile_; }
  105. void set_icc_profile(const std::vector<uint8_t>& icc_profile) {
  106. icc_profile_ = icc_profile;
  107. }
  108. protected:
  109. DesktopFrame(DesktopSize size,
  110. int stride,
  111. uint8_t* data,
  112. SharedMemory* shared_memory);
  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. private:
  119. const DesktopSize size_;
  120. const int stride_;
  121. DesktopRegion updated_region_;
  122. DesktopVector top_left_;
  123. DesktopVector dpi_;
  124. int64_t capture_time_ms_;
  125. uint32_t capturer_id_;
  126. std::vector<uint8_t> icc_profile_;
  127. RTC_DISALLOW_COPY_AND_ASSIGN(DesktopFrame);
  128. };
  129. // A DesktopFrame that stores data in the heap.
  130. class RTC_EXPORT BasicDesktopFrame : public DesktopFrame {
  131. public:
  132. // The entire data buffer used for the frame is initialized with zeros.
  133. explicit BasicDesktopFrame(DesktopSize size);
  134. ~BasicDesktopFrame() override;
  135. // Creates a BasicDesktopFrame that contains copy of |frame|.
  136. // TODO(zijiehe): Return std::unique_ptr<DesktopFrame>
  137. static DesktopFrame* CopyOf(const DesktopFrame& frame);
  138. private:
  139. RTC_DISALLOW_COPY_AND_ASSIGN(BasicDesktopFrame);
  140. };
  141. // A DesktopFrame that stores data in shared memory.
  142. class RTC_EXPORT SharedMemoryDesktopFrame : public DesktopFrame {
  143. public:
  144. // May return nullptr if |shared_memory_factory| failed to create a
  145. // SharedMemory instance.
  146. // |shared_memory_factory| should not be nullptr.
  147. static std::unique_ptr<DesktopFrame> Create(
  148. DesktopSize size,
  149. SharedMemoryFactory* shared_memory_factory);
  150. // Takes ownership of |shared_memory|.
  151. // Deprecated, use the next constructor.
  152. SharedMemoryDesktopFrame(DesktopSize size,
  153. int stride,
  154. SharedMemory* shared_memory);
  155. // Preferred.
  156. SharedMemoryDesktopFrame(DesktopSize size,
  157. int stride,
  158. std::unique_ptr<SharedMemory> shared_memory);
  159. ~SharedMemoryDesktopFrame() override;
  160. private:
  161. // Avoid unexpected order of parameter evaluation.
  162. // Executing both std::unique_ptr<T>::operator->() and
  163. // std::unique_ptr<T>::release() in the member initializer list is not safe.
  164. // Depends on the order of parameter evaluation,
  165. // std::unique_ptr<T>::operator->() may trigger assertion failure if it has
  166. // been evaluated after std::unique_ptr<T>::release(). By using this
  167. // constructor, std::unique_ptr<T>::operator->() won't be involved anymore.
  168. SharedMemoryDesktopFrame(DesktopRect rect,
  169. int stride,
  170. SharedMemory* shared_memory);
  171. RTC_DISALLOW_COPY_AND_ASSIGN(SharedMemoryDesktopFrame);
  172. };
  173. } // namespace webrtc
  174. #endif // MODULES_DESKTOP_CAPTURE_DESKTOP_FRAME_H_