henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | webrtc::VideoFrame video_frame_; |
| 59 | }; |
| 60 | |
| 61 | class WebRtcVideoFrame : public VideoFrame { |
| 62 | public: |
| 63 | typedef talk_base::RefCountedObject<FrameBuffer> RefCountedBuffer; |
| 64 | |
| 65 | WebRtcVideoFrame(); |
| 66 | ~WebRtcVideoFrame(); |
| 67 | |
| 68 | // Creates a frame from a raw sample with FourCC "format" and size "w" x "h". |
| 69 | // "h" can be negative indicating a vertically flipped image. |
| 70 | // "dh" is destination height if cropping is desired and is always positive. |
| 71 | // Returns "true" if successful. |
| 72 | bool Init(uint32 format, int w, int h, int dw, int dh, uint8* sample, |
| 73 | size_t sample_size, size_t pixel_width, size_t pixel_height, |
| 74 | int64 elapsed_time, int64 time_stamp, int rotation); |
| 75 | |
| 76 | bool Init(const CapturedFrame* frame, int dw, int dh); |
| 77 | |
| 78 | bool InitToBlack(int w, int h, size_t pixel_width, size_t pixel_height, |
| 79 | int64 elapsed_time, int64 time_stamp); |
| 80 | |
| 81 | void Attach(uint8* buffer, size_t buffer_size, int w, int h, |
| 82 | size_t pixel_width, size_t pixel_height, int64 elapsed_time, |
| 83 | int64 time_stamp, int rotation); |
| 84 | |
| 85 | void Detach(uint8** data, size_t* length); |
| 86 | bool AddWatermark(); |
| 87 | webrtc::VideoFrame* frame() { return video_buffer_->frame(); } |
| 88 | webrtc::VideoFrame* frame() const { return video_buffer_->frame(); } |
| 89 | |
| 90 | // From base class VideoFrame. |
| 91 | virtual bool Reset(uint32 format, int w, int h, int dw, int dh, uint8* sample, |
| 92 | size_t sample_size, size_t pixel_width, |
| 93 | size_t pixel_height, int64 elapsed_time, int64 time_stamp, |
| 94 | int rotation); |
| 95 | |
| 96 | virtual size_t GetWidth() const; |
| 97 | virtual size_t GetHeight() const; |
| 98 | virtual const uint8* GetYPlane() const; |
| 99 | virtual const uint8* GetUPlane() const; |
| 100 | virtual const uint8* GetVPlane() const; |
| 101 | virtual uint8* GetYPlane(); |
| 102 | virtual uint8* GetUPlane(); |
| 103 | virtual uint8* GetVPlane(); |
| 104 | virtual int32 GetYPitch() const { return frame()->Width(); } |
| 105 | virtual int32 GetUPitch() const { return (frame()->Width() + 1) / 2; } |
| 106 | virtual int32 GetVPitch() const { return (frame()->Width() + 1) / 2; } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 107 | virtual void* GetNativeHandle() const { return NULL; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 108 | |
| 109 | virtual size_t GetPixelWidth() const { return pixel_width_; } |
| 110 | virtual size_t GetPixelHeight() const { return pixel_height_; } |
| 111 | virtual int64 GetElapsedTime() const { return elapsed_time_; } |
| 112 | virtual int64 GetTimeStamp() const { return time_stamp_; } |
| 113 | virtual void SetElapsedTime(int64 elapsed_time) { |
| 114 | elapsed_time_ = elapsed_time; |
| 115 | } |
| 116 | virtual void SetTimeStamp(int64 time_stamp) { time_stamp_ = time_stamp; } |
| 117 | |
| 118 | virtual int GetRotation() const { return rotation_; } |
| 119 | |
| 120 | virtual VideoFrame* Copy() const; |
| 121 | virtual bool MakeExclusive(); |
| 122 | virtual size_t CopyToBuffer(uint8* buffer, size_t size) const; |
| 123 | virtual size_t ConvertToRgbBuffer(uint32 to_fourcc, uint8* buffer, |
| 124 | size_t size, int stride_rgb) const; |
| 125 | |
| 126 | private: |
| 127 | void Attach(RefCountedBuffer* video_buffer, size_t buffer_size, int w, int h, |
| 128 | size_t pixel_width, size_t pixel_height, int64 elapsed_time, |
| 129 | int64 time_stamp, int rotation); |
| 130 | |
| 131 | virtual VideoFrame* CreateEmptyFrame(int w, int h, size_t pixel_width, |
| 132 | size_t pixel_height, int64 elapsed_time, |
| 133 | int64 time_stamp) const; |
| 134 | void InitToEmptyBuffer(int w, int h, size_t pixel_width, size_t pixel_height, |
| 135 | int64 elapsed_time, int64 time_stamp); |
| 136 | |
| 137 | talk_base::scoped_refptr<RefCountedBuffer> video_buffer_; |
| 138 | bool is_black_; |
| 139 | size_t pixel_width_; |
| 140 | size_t pixel_height_; |
| 141 | int64 elapsed_time_; |
| 142 | int64 time_stamp_; |
| 143 | int rotation_; |
| 144 | }; |
| 145 | |
| 146 | } // namespace cricket |
| 147 | |
| 148 | #endif // TALK_MEDIA_WEBRTCVIDEOFRAME_H_ |