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/modules/interface/module_common_types.h"
37
38namespace cricket {
39
40struct CapturedFrame;
41
42class WebRtcVideoFrame : public VideoFrame {
43 public:
44  WebRtcVideoFrame();
45  ~WebRtcVideoFrame();
46
47  // Creates a frame from a raw sample with FourCC "format" and size "w" x "h".
48  // "h" can be negative indicating a vertically flipped image.
49  // "dh" is destination height if cropping is desired and is always positive.
50  // Returns "true" if successful.
51  bool Init(uint32 format, int w, int h, int dw, int dh, uint8* sample,
52            size_t sample_size, size_t pixel_width, size_t pixel_height,
53            int64 elapsed_time, int64 time_stamp, int rotation);
54
55  bool Init(const CapturedFrame* frame, int dw, int dh);
56
57  // Aliases this WebRtcVideoFrame to a CapturedFrame. |frame| must outlive
58  // this WebRtcVideoFrame.
59  bool Alias(const CapturedFrame* frame, int dw, int dh);
60
61  bool InitToBlack(int w, int h, size_t pixel_width, size_t pixel_height,
62                   int64 elapsed_time, int64 time_stamp);
63
64  // Aliases this WebRtcVideoFrame to a memory buffer. |buffer| must outlive
65  // this WebRtcVideoFrame.
66  void Alias(uint8* buffer, size_t buffer_size, int w, int h,
67             size_t pixel_width, size_t pixel_height, int64 elapsed_time,
68             int64 time_stamp, int rotation);
69
70  webrtc::VideoFrame* frame();
71  const webrtc::VideoFrame* frame() const;
72
73  // From base class VideoFrame.
74  virtual bool Reset(uint32 format, int w, int h, int dw, int dh, uint8* sample,
75                     size_t sample_size, size_t pixel_width,
76                     size_t pixel_height, int64 elapsed_time, int64 time_stamp,
77                     int rotation);
78
79  virtual size_t GetWidth() const;
80  virtual size_t GetHeight() const;
81  virtual const uint8* GetYPlane() const;
82  virtual const uint8* GetUPlane() const;
83  virtual const uint8* GetVPlane() const;
84  virtual uint8* GetYPlane();
85  virtual uint8* GetUPlane();
86  virtual uint8* GetVPlane();
87  virtual int32 GetYPitch() const { return frame()->Width(); }
88  virtual int32 GetUPitch() const { return (frame()->Width() + 1) / 2; }
89  virtual int32 GetVPitch() const { return (frame()->Width() + 1) / 2; }
90  virtual void* GetNativeHandle() const { return NULL; }
91
92  virtual size_t GetPixelWidth() const { return pixel_width_; }
93  virtual size_t GetPixelHeight() const { return pixel_height_; }
94  virtual int64 GetElapsedTime() const { return elapsed_time_; }
95  virtual int64 GetTimeStamp() const { return time_stamp_; }
96  virtual void SetElapsedTime(int64 elapsed_time) {
97    elapsed_time_ = elapsed_time;
98  }
99  virtual void SetTimeStamp(int64 time_stamp) { time_stamp_ = time_stamp; }
100
101  virtual int GetRotation() const { return rotation_; }
102
103  virtual VideoFrame* Copy() const;
104  virtual bool MakeExclusive();
105  virtual size_t CopyToBuffer(uint8* buffer, size_t size) const;
106  virtual size_t ConvertToRgbBuffer(uint32 to_fourcc, uint8* buffer,
107                                    size_t size, int stride_rgb) const;
108
109 private:
110  class FrameBuffer;
111  typedef rtc::RefCountedObject<FrameBuffer> RefCountedBuffer;
112
113  void Attach(RefCountedBuffer* video_buffer, size_t buffer_size, int w, int h,
114              size_t pixel_width, size_t pixel_height, int64 elapsed_time,
115              int64 time_stamp, int rotation);
116
117  virtual VideoFrame* CreateEmptyFrame(int w, int h, size_t pixel_width,
118                                       size_t pixel_height, int64 elapsed_time,
119                                       int64 time_stamp) const;
120  void InitToEmptyBuffer(int w, int h, size_t pixel_width, size_t pixel_height,
121                         int64 elapsed_time, int64 time_stamp);
122
123  rtc::scoped_refptr<RefCountedBuffer> video_buffer_;
124  bool is_black_;
125  size_t pixel_width_;
126  size_t pixel_height_;
127  int64 elapsed_time_;
128  int64 time_stamp_;
129  int rotation_;
130};
131
132}  // namespace cricket
133
134#endif  // TALK_MEDIA_WEBRTCVIDEOFRAME_H_
135