Home | History | Annotate | Download | only in desktop_capture
      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 
     14 #include "webrtc/base/scoped_ptr.h"
     15 #include "webrtc/modules/desktop_capture/desktop_geometry.h"
     16 #include "webrtc/modules/desktop_capture/desktop_region.h"
     17 #include "webrtc/modules/desktop_capture/shared_memory.h"
     18 #include "webrtc/typedefs.h"
     19 
     20 namespace webrtc {
     21 
     22 // DesktopFrame represents a video frame captured from the screen.
     23 class DesktopFrame {
     24  public:
     25   // DesktopFrame objects always hold RGBA data.
     26   static const int kBytesPerPixel = 4;
     27 
     28   virtual ~DesktopFrame();
     29 
     30   // Size of the frame.
     31   const DesktopSize& size() const { return size_; }
     32 
     33   // Distance in the buffer between two neighboring rows in bytes.
     34   int stride() const { return stride_; }
     35 
     36   // Data buffer used for the frame.
     37   uint8_t* data() const { return data_; }
     38 
     39   // SharedMemory used for the buffer or NULL if memory is allocated on the
     40   // heap. The result is guaranteed to be deleted only after the frame is
     41   // deleted (classes that inherit from DesktopFrame must ensure it).
     42   SharedMemory* shared_memory() const { return shared_memory_; }
     43 
     44   // Indicates region of the screen that has changed since the previous frame.
     45   const DesktopRegion& updated_region() const { return updated_region_; }
     46   DesktopRegion* mutable_updated_region() { return &updated_region_; }
     47 
     48   // DPI of the screen being captured. May be set to zero, e.g. if DPI is
     49   // unknown.
     50   const DesktopVector& dpi() const { return dpi_; }
     51   void set_dpi(const DesktopVector& dpi) { dpi_ = dpi; }
     52 
     53   // Time taken to capture the frame in milliseconds.
     54   int64_t capture_time_ms() const { return capture_time_ms_; }
     55   void set_capture_time_ms(int64_t time_ms) { capture_time_ms_ = time_ms; }
     56 
     57   // Optional shape for the frame. Frames may be shaped e.g. if
     58   // capturing the contents of a shaped window.
     59   const DesktopRegion* shape() const { return shape_.get(); }
     60   void set_shape(DesktopRegion* shape) { shape_.reset(shape); }
     61 
     62   // Copies pixels from a buffer or another frame. |dest_rect| rect must lay
     63   // within bounds of this frame.
     64   void CopyPixelsFrom(uint8_t* src_buffer, int src_stride,
     65                       const DesktopRect& dest_rect);
     66   void CopyPixelsFrom(const DesktopFrame& src_frame,
     67                       const DesktopVector& src_pos,
     68                       const DesktopRect& dest_rect);
     69 
     70   // A helper to return the data pointer of a frame at the specified position.
     71   uint8_t* GetFrameDataAtPos(const DesktopVector& pos) const;
     72 
     73  protected:
     74   DesktopFrame(DesktopSize size,
     75                int stride,
     76                uint8_t* data,
     77                SharedMemory* shared_memory);
     78 
     79   const DesktopSize size_;
     80   const int stride_;
     81 
     82   // Ownership of the buffers is defined by the classes that inherit from this
     83   // class. They must guarantee that the buffer is not deleted before the frame
     84   // is deleted.
     85   uint8_t* const data_;
     86   SharedMemory* const shared_memory_;
     87 
     88   DesktopRegion updated_region_;
     89   DesktopVector dpi_;
     90   int64_t capture_time_ms_;
     91   rtc::scoped_ptr<DesktopRegion> shape_;
     92 
     93  private:
     94   RTC_DISALLOW_COPY_AND_ASSIGN(DesktopFrame);
     95 };
     96 
     97 // A DesktopFrame that stores data in the heap.
     98 class BasicDesktopFrame : public DesktopFrame {
     99  public:
    100   explicit BasicDesktopFrame(DesktopSize size);
    101   ~BasicDesktopFrame() override;
    102 
    103   // Creates a BasicDesktopFrame that contains copy of |frame|.
    104   static DesktopFrame* CopyOf(const DesktopFrame& frame);
    105 
    106  private:
    107   RTC_DISALLOW_COPY_AND_ASSIGN(BasicDesktopFrame);
    108 };
    109 
    110 // A DesktopFrame that stores data in shared memory.
    111 class SharedMemoryDesktopFrame : public DesktopFrame {
    112  public:
    113   // Takes ownership of |shared_memory|.
    114   SharedMemoryDesktopFrame(DesktopSize size,
    115                            int stride,
    116                            SharedMemory* shared_memory);
    117   ~SharedMemoryDesktopFrame() override;
    118 
    119  private:
    120   RTC_DISALLOW_COPY_AND_ASSIGN(SharedMemoryDesktopFrame);
    121 };
    122 
    123 }  // namespace webrtc
    124 
    125 #endif  // WEBRTC_MODULES_DESKTOP_CAPTURE_DESKTOP_FRAME_H_
    126 
    127