blob: ee34c414a475cd9bf6a4b647f21c6ab1ccb558b5 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010011#ifndef WEBRTC_MEDIA_ENGINE_WEBRTCVIDEOFRAME_H_
12#define WEBRTC_MEDIA_ENGINE_WEBRTCVIDEOFRAME_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
kwiberg686a8ef2016-02-26 03:00:35 -080014#include <memory>
15
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000016#include "webrtc/base/buffer.h"
17#include "webrtc/base/refcount.h"
18#include "webrtc/base/scoped_ref_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019#include "webrtc/common_types.h"
kjellander6f8ce062015-11-16 13:52:24 -080020#include "webrtc/common_video/include/video_frame_buffer.h"
kjellandera96e2d72016-02-04 23:52:28 -080021#include "webrtc/media/base/videoframe.h"
magjed@webrtc.orge575e9c2014-12-14 11:09:23 +000022
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023namespace cricket {
24
25struct CapturedFrame;
26
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027class WebRtcVideoFrame : public VideoFrame {
28 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029 WebRtcVideoFrame();
nisseb17712f2016-04-14 02:29:29 -070030
31 // Preferred construction, with microsecond timestamp.
32 WebRtcVideoFrame(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
33 webrtc::VideoRotation rotation,
34 int64_t timestamp_us);
35
36 // TODO(nisse): Deprecate/delete.
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000037 WebRtcVideoFrame(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -070038 int64_t time_stamp_ns,
39 webrtc::VideoRotation rotation);
40
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041 ~WebRtcVideoFrame();
42
43 // Creates a frame from a raw sample with FourCC "format" and size "w" x "h".
44 // "h" can be negative indicating a vertically flipped image.
45 // "dh" is destination height if cropping is desired and is always positive.
46 // Returns "true" if successful.
Peter Boström0c4e06b2015-10-07 12:23:21 +020047 bool Init(uint32_t format,
guoweis@webrtc.org6c930c72015-02-09 01:28:12 +000048 int w,
49 int h,
50 int dw,
51 int dh,
Peter Boström0c4e06b2015-10-07 12:23:21 +020052 uint8_t* sample,
guoweis@webrtc.org6c930c72015-02-09 01:28:12 +000053 size_t sample_size,
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000054 int64_t time_stamp_ns,
guoweis@webrtc.org6c930c72015-02-09 01:28:12 +000055 webrtc::VideoRotation rotation);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056
nisseb17712f2016-04-14 02:29:29 -070057 // The timestamp of the captured frame is expected to use the same
58 // timescale and epoch as rtc::Time.
59 // TODO(nisse): Consider adding a warning message, or even an RTC_DCHECK, if
60 // the time is too far off.
perkj@webrtc.org1d828132015-03-03 06:44:06 +000061 bool Init(const CapturedFrame* frame, int dw, int dh, bool apply_rotation);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
nisseb17712f2016-04-14 02:29:29 -070063 void InitToEmptyBuffer(int w, int h);
nisse8b1e4312016-01-18 01:46:27 -080064 void InitToEmptyBuffer(int w, int h, int64_t time_stamp_ns);
wu@webrtc.org16d62542013-11-05 23:45:14 +000065
Niels Möller03bd4002016-04-06 13:07:06 +020066 bool InitToBlack(int w, int h, int64_t time_stamp_ns);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067
nisse71a0c2f2016-04-04 00:57:29 -070068 int width() const override;
69 int height() const override;
70
rlesterec9d1872015-10-27 14:22:16 -070071 void* GetNativeHandle() const override;
nisse05654512016-04-29 02:56:00 -070072 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& video_frame_buffer()
rlesterec9d1872015-10-27 14:22:16 -070073 const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074
nisseb17712f2016-04-14 02:29:29 -070075 /* System monotonic clock */
76 int64_t timestamp_us() const override { return timestamp_us_; }
77 void set_timestamp_us(int64_t time_us) override { timestamp_us_ = time_us; };
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078
nissef3868762016-04-13 03:29:16 -070079 webrtc::VideoRotation rotation() const override { return rotation_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080
rlesterec9d1872015-10-27 14:22:16 -070081 VideoFrame* Copy() const override;
82 bool IsExclusive() const override;
rlesterec9d1872015-10-27 14:22:16 -070083 size_t ConvertToRgbBuffer(uint32_t to_fourcc,
84 uint8_t* buffer,
85 size_t size,
86 int stride_rgb) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +000088 const VideoFrame* GetCopyWithRotationApplied() const override;
89
90 protected:
nissef3868762016-04-13 03:29:16 -070091 void set_rotation(webrtc::VideoRotation rotation) override {
Guo-wei Shieh17b889b2015-05-19 12:45:47 -070092 rotation_ = rotation;
93 }
Niels Möller03bd4002016-04-06 13:07:06 +020094 // Creates a frame from a raw sample with FourCC |format| and size |w| x |h|.
95 // |h| can be negative indicating a vertically flipped image.
96 // |dw| is destination width; can be less than |w| if cropping is desired.
97 // |dh| is destination height, like |dw|, but must be a positive number.
98 // Returns whether the function succeeded or failed.
99 bool Reset(uint32_t format,
nisseb17712f2016-04-14 02:29:29 -0700100 int w,
101 int h,
102 int dw,
103 int dh,
104 uint8_t* sample,
105 size_t sample_size,
106 int64_t timestamp_us,
107 webrtc::VideoRotation rotation,
108 bool apply_rotation);
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +0000109
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 private:
nisse8b1e4312016-01-18 01:46:27 -0800111 VideoFrame* CreateEmptyFrame(int w, int h,
rlesterec9d1872015-10-27 14:22:16 -0700112 int64_t time_stamp_ns) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000114 // An opaque reference counted handle that stores the pixel data.
115 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
nisseb17712f2016-04-14 02:29:29 -0700116 int64_t timestamp_us_;
guoweis@webrtc.org6c930c72015-02-09 01:28:12 +0000117 webrtc::VideoRotation rotation_;
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +0000118
119 // This is mutable as the calculation is expensive but once calculated, it
120 // remains const.
kwiberg686a8ef2016-02-26 03:00:35 -0800121 mutable std::unique_ptr<VideoFrame> rotated_frame_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122};
123
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124} // namespace cricket
125
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +0100126#endif // WEBRTC_MEDIA_ENGINE_WEBRTCVIDEOFRAME_H_