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/media/base/videoframe.h"
32 #include "webrtc/base/buffer.h"
33 #include "webrtc/base/refcount.h"
34 #include "webrtc/base/scoped_ref_ptr.h"
35 #include "webrtc/common_types.h"
36 #include "webrtc/common_video/include/video_frame_buffer.h"
37 
38 namespace cricket {
39 
40 struct CapturedFrame;
41 
42 class WebRtcVideoFrame : public VideoFrame {
43  public:
44   WebRtcVideoFrame();
45   WebRtcVideoFrame(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
46                    int64_t time_stamp_ns,
47                    webrtc::VideoRotation rotation);
48 
49   ~WebRtcVideoFrame();
50 
51   // Creates a frame from a raw sample with FourCC "format" and size "w" x "h".
52   // "h" can be negative indicating a vertically flipped image.
53   // "dh" is destination height if cropping is desired and is always positive.
54   // Returns "true" if successful.
55   bool Init(uint32_t format,
56             int w,
57             int h,
58             int dw,
59             int dh,
60             uint8_t* sample,
61             size_t sample_size,
62             size_t pixel_width,
63             size_t pixel_height,
64             int64_t time_stamp_ns,
65             webrtc::VideoRotation rotation);
66 
67   bool Init(const CapturedFrame* frame, int dw, int dh, bool apply_rotation);
68 
69   void InitToEmptyBuffer(int w, int h, size_t pixel_width, size_t pixel_height,
70                          int64_t time_stamp_ns);
71 
72   bool InitToBlack(int w, int h, size_t pixel_width, size_t pixel_height,
73                    int64_t time_stamp_ns) override;
74 
75   // From base class VideoFrame.
76   bool Reset(uint32_t format,
77                      int w,
78                      int h,
79                      int dw,
80                      int dh,
81                      uint8_t* sample,
82                      size_t sample_size,
83                      size_t pixel_width,
84                      size_t pixel_height,
85                      int64_t time_stamp_ns,
86                      webrtc::VideoRotation rotation,
87                      bool apply_rotation) override;
88 
89   size_t GetWidth() const override;
90   size_t GetHeight() const override;
91   const uint8_t* GetYPlane() const override;
92   const uint8_t* GetUPlane() const override;
93   const uint8_t* GetVPlane() const override;
94   uint8_t* GetYPlane() override;
95   uint8_t* GetUPlane() override;
96   uint8_t* GetVPlane() override;
97   int32_t GetYPitch() const override;
98   int32_t GetUPitch() const override;
99   int32_t GetVPitch() const override;
100   void* GetNativeHandle() const override;
101   rtc::scoped_refptr<webrtc::VideoFrameBuffer> GetVideoFrameBuffer()
102       const override;
103 
GetPixelWidth()104   size_t GetPixelWidth() const override { return pixel_width_; }
GetPixelHeight()105   size_t GetPixelHeight() const override { return pixel_height_; }
GetTimeStamp()106   int64_t GetTimeStamp() const override { return time_stamp_ns_; }
SetTimeStamp(int64_t time_stamp_ns)107   void SetTimeStamp(int64_t time_stamp_ns) override {
108     time_stamp_ns_ = time_stamp_ns;
109   }
110 
GetVideoRotation()111   webrtc::VideoRotation GetVideoRotation() const override {
112     return rotation_;
113   }
114 
115   VideoFrame* Copy() const override;
116   bool IsExclusive() const override;
117   bool MakeExclusive() override;
118   size_t ConvertToRgbBuffer(uint32_t to_fourcc,
119                             uint8_t* buffer,
120                             size_t size,
121                             int stride_rgb) const override;
122 
123   const VideoFrame* GetCopyWithRotationApplied() const override;
124 
125  protected:
SetRotation(webrtc::VideoRotation rotation)126   void SetRotation(webrtc::VideoRotation rotation) override {
127     rotation_ = rotation;
128   }
129 
130  private:
131   VideoFrame* CreateEmptyFrame(int w, int h, size_t pixel_width,
132                                size_t pixel_height,
133                                int64_t time_stamp_ns) const override;
134 
135   // An opaque reference counted handle that stores the pixel data.
136   rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
137   size_t pixel_width_;
138   size_t pixel_height_;
139   int64_t time_stamp_ns_;
140   webrtc::VideoRotation rotation_;
141 
142   // This is mutable as the calculation is expensive but once calculated, it
143   // remains const.
144   mutable rtc::scoped_ptr<VideoFrame> rotated_frame_;
145 };
146 
147 }  // namespace cricket
148 
149 #endif  // TALK_MEDIA_WEBRTCVIDEOFRAME_H_
150