Home | History | Annotate | Download | only in webrtc
      1 /*
      2  * libjingle
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef TALK_MEDIA_WEBRTCVIDEOFRAME_H_
     29 #define TALK_MEDIA_WEBRTCVIDEOFRAME_H_
     30 
     31 #include "talk/base/buffer.h"
     32 #include "talk/base/refcount.h"
     33 #include "talk/base/scoped_ref_ptr.h"
     34 #include "talk/media/base/videoframe.h"
     35 #include "webrtc/common_types.h"
     36 #include "webrtc/modules/interface/module_common_types.h"
     37 
     38 namespace cricket {
     39 
     40 struct CapturedFrame;
     41 
     42 // Class that takes ownership of the frame passed to it.
     43 class FrameBuffer {
     44  public:
     45   FrameBuffer();
     46   explicit FrameBuffer(size_t length);
     47   ~FrameBuffer();
     48 
     49   void SetData(char* data, size_t length);
     50   void ReturnData(char** data, size_t* length);
     51   char* data();
     52   size_t length() const;
     53 
     54   webrtc::VideoFrame* frame();
     55   const webrtc::VideoFrame* frame() const;
     56 
     57  private:
     58   talk_base::scoped_array<char> data_;
     59   size_t length_;
     60   webrtc::VideoFrame video_frame_;
     61 };
     62 
     63 class WebRtcVideoFrame : public VideoFrame {
     64  public:
     65   typedef talk_base::RefCountedObject<FrameBuffer> RefCountedBuffer;
     66 
     67   WebRtcVideoFrame();
     68   ~WebRtcVideoFrame();
     69 
     70   // Creates a frame from a raw sample with FourCC "format" and size "w" x "h".
     71   // "h" can be negative indicating a vertically flipped image.
     72   // "dh" is destination height if cropping is desired and is always positive.
     73   // Returns "true" if successful.
     74   bool Init(uint32 format, int w, int h, int dw, int dh, uint8* sample,
     75             size_t sample_size, size_t pixel_width, size_t pixel_height,
     76             int64 elapsed_time, int64 time_stamp, int rotation);
     77 
     78   bool Init(const CapturedFrame* frame, int dw, int dh);
     79 
     80   bool InitToBlack(int w, int h, size_t pixel_width, size_t pixel_height,
     81                    int64 elapsed_time, int64 time_stamp);
     82 
     83   void Attach(uint8* buffer, size_t buffer_size, int w, int h,
     84               size_t pixel_width, size_t pixel_height, int64 elapsed_time,
     85               int64 time_stamp, int rotation);
     86 
     87   void Detach(uint8** data, size_t* length);
     88   bool AddWatermark();
     89   webrtc::VideoFrame* frame() { return video_buffer_->frame(); }
     90   webrtc::VideoFrame* frame() const { return video_buffer_->frame(); }
     91 
     92   // From base class VideoFrame.
     93   virtual bool Reset(uint32 format, int w, int h, int dw, int dh, uint8* sample,
     94                      size_t sample_size, size_t pixel_width,
     95                      size_t pixel_height, int64 elapsed_time, int64 time_stamp,
     96                      int rotation);
     97 
     98   virtual size_t GetWidth() const;
     99   virtual size_t GetHeight() const;
    100   virtual const uint8* GetYPlane() const;
    101   virtual const uint8* GetUPlane() const;
    102   virtual const uint8* GetVPlane() const;
    103   virtual uint8* GetYPlane();
    104   virtual uint8* GetUPlane();
    105   virtual uint8* GetVPlane();
    106   virtual int32 GetYPitch() const { return frame()->Width(); }
    107   virtual int32 GetUPitch() const { return (frame()->Width() + 1) / 2; }
    108   virtual int32 GetVPitch() const { return (frame()->Width() + 1) / 2; }
    109   virtual void* GetNativeHandle() const { return NULL; }
    110 
    111   virtual size_t GetPixelWidth() const { return pixel_width_; }
    112   virtual size_t GetPixelHeight() const { return pixel_height_; }
    113   virtual int64 GetElapsedTime() const { return elapsed_time_; }
    114   virtual int64 GetTimeStamp() const { return time_stamp_; }
    115   virtual void SetElapsedTime(int64 elapsed_time) {
    116     elapsed_time_ = elapsed_time;
    117   }
    118   virtual void SetTimeStamp(int64 time_stamp) { time_stamp_ = time_stamp; }
    119 
    120   virtual int GetRotation() const { return rotation_; }
    121 
    122   virtual VideoFrame* Copy() const;
    123   virtual bool MakeExclusive();
    124   virtual size_t CopyToBuffer(uint8* buffer, size_t size) const;
    125   virtual size_t ConvertToRgbBuffer(uint32 to_fourcc, uint8* buffer,
    126                                     size_t size, int stride_rgb) const;
    127 
    128  private:
    129   void Attach(RefCountedBuffer* video_buffer, size_t buffer_size, int w, int h,
    130               size_t pixel_width, size_t pixel_height, int64 elapsed_time,
    131               int64 time_stamp, int rotation);
    132 
    133   virtual VideoFrame* CreateEmptyFrame(int w, int h, size_t pixel_width,
    134                                        size_t pixel_height, int64 elapsed_time,
    135                                        int64 time_stamp) const;
    136   void InitToEmptyBuffer(int w, int h, size_t pixel_width, size_t pixel_height,
    137                          int64 elapsed_time, int64 time_stamp);
    138 
    139   talk_base::scoped_refptr<RefCountedBuffer> video_buffer_;
    140   bool is_black_;
    141   size_t pixel_width_;
    142   size_t pixel_height_;
    143   int64 elapsed_time_;
    144   int64 time_stamp_;
    145   int rotation_;
    146 };
    147 
    148 }  // namespace cricket
    149 
    150 #endif  // TALK_MEDIA_WEBRTCVIDEOFRAME_H_
    151