henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 4 | * 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
kjellander@webrtc.org | 5ad1297 | 2016-02-12 06:39:40 +0100 | [diff] [blame] | 11 | #ifndef WEBRTC_MEDIA_ENGINE_WEBRTCVIDEOFRAME_H_ |
| 12 | #define WEBRTC_MEDIA_ENGINE_WEBRTCVIDEOFRAME_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 16 | #include "webrtc/base/buffer.h" |
| 17 | #include "webrtc/base/refcount.h" |
| 18 | #include "webrtc/base/scoped_ref_ptr.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | #include "webrtc/common_types.h" |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 20 | #include "webrtc/common_video/include/video_frame_buffer.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 21 | #include "webrtc/media/base/videoframe.h" |
magjed@webrtc.org | e575e9c | 2014-12-14 11:09:23 +0000 | [diff] [blame] | 22 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 23 | namespace cricket { |
| 24 | |
| 25 | struct CapturedFrame; |
| 26 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 27 | class WebRtcVideoFrame : public VideoFrame { |
| 28 | public: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | WebRtcVideoFrame(); |
nisse | b17712f | 2016-04-14 02:29:29 -0700 | [diff] [blame] | 30 | |
| 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.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 37 | WebRtcVideoFrame(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer, |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 38 | int64_t time_stamp_ns, |
| 39 | webrtc::VideoRotation rotation); |
| 40 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 41 | ~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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 47 | bool Init(uint32_t format, |
guoweis@webrtc.org | 6c930c7 | 2015-02-09 01:28:12 +0000 | [diff] [blame] | 48 | int w, |
| 49 | int h, |
| 50 | int dw, |
| 51 | int dh, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 52 | uint8_t* sample, |
guoweis@webrtc.org | 6c930c7 | 2015-02-09 01:28:12 +0000 | [diff] [blame] | 53 | size_t sample_size, |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 54 | int64_t time_stamp_ns, |
guoweis@webrtc.org | 6c930c7 | 2015-02-09 01:28:12 +0000 | [diff] [blame] | 55 | webrtc::VideoRotation rotation); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 56 | |
nisse | b17712f | 2016-04-14 02:29:29 -0700 | [diff] [blame] | 57 | // 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.org | 1d82813 | 2015-03-03 06:44:06 +0000 | [diff] [blame] | 61 | bool Init(const CapturedFrame* frame, int dw, int dh, bool apply_rotation); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | |
nisse | b17712f | 2016-04-14 02:29:29 -0700 | [diff] [blame] | 63 | void InitToEmptyBuffer(int w, int h); |
nisse | 8b1e431 | 2016-01-18 01:46:27 -0800 | [diff] [blame] | 64 | void InitToEmptyBuffer(int w, int h, int64_t time_stamp_ns); |
wu@webrtc.org | 16d6254 | 2013-11-05 23:45:14 +0000 | [diff] [blame] | 65 | |
Niels Möller | 03bd400 | 2016-04-06 13:07:06 +0200 | [diff] [blame] | 66 | bool InitToBlack(int w, int h, int64_t time_stamp_ns); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 67 | |
nisse | 71a0c2f | 2016-04-04 00:57:29 -0700 | [diff] [blame] | 68 | int width() const override; |
| 69 | int height() const override; |
| 70 | |
rlester | ec9d187 | 2015-10-27 14:22:16 -0700 | [diff] [blame] | 71 | void* GetNativeHandle() const override; |
nisse | 0565451 | 2016-04-29 02:56:00 -0700 | [diff] [blame] | 72 | const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& video_frame_buffer() |
rlester | ec9d187 | 2015-10-27 14:22:16 -0700 | [diff] [blame] | 73 | const override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 74 | |
nisse | b17712f | 2016-04-14 02:29:29 -0700 | [diff] [blame] | 75 | /* 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 78 | |
nisse | f386876 | 2016-04-13 03:29:16 -0700 | [diff] [blame] | 79 | webrtc::VideoRotation rotation() const override { return rotation_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 80 | |
rlester | ec9d187 | 2015-10-27 14:22:16 -0700 | [diff] [blame] | 81 | VideoFrame* Copy() const override; |
| 82 | bool IsExclusive() const override; |
rlester | ec9d187 | 2015-10-27 14:22:16 -0700 | [diff] [blame] | 83 | size_t ConvertToRgbBuffer(uint32_t to_fourcc, |
| 84 | uint8_t* buffer, |
| 85 | size_t size, |
| 86 | int stride_rgb) const override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 87 | |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 88 | const VideoFrame* GetCopyWithRotationApplied() const override; |
| 89 | |
| 90 | protected: |
nisse | f386876 | 2016-04-13 03:29:16 -0700 | [diff] [blame] | 91 | void set_rotation(webrtc::VideoRotation rotation) override { |
Guo-wei Shieh | 17b889b | 2015-05-19 12:45:47 -0700 | [diff] [blame] | 92 | rotation_ = rotation; |
| 93 | } |
Niels Möller | 03bd400 | 2016-04-06 13:07:06 +0200 | [diff] [blame] | 94 | // 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, |
nisse | b17712f | 2016-04-14 02:29:29 -0700 | [diff] [blame] | 100 | 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.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 109 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | private: |
nisse | 8b1e431 | 2016-01-18 01:46:27 -0800 | [diff] [blame] | 111 | VideoFrame* CreateEmptyFrame(int w, int h, |
rlester | ec9d187 | 2015-10-27 14:22:16 -0700 | [diff] [blame] | 112 | int64_t time_stamp_ns) const override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 114 | // An opaque reference counted handle that stores the pixel data. |
| 115 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_; |
nisse | b17712f | 2016-04-14 02:29:29 -0700 | [diff] [blame] | 116 | int64_t timestamp_us_; |
guoweis@webrtc.org | 6c930c7 | 2015-02-09 01:28:12 +0000 | [diff] [blame] | 117 | webrtc::VideoRotation rotation_; |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 118 | |
| 119 | // This is mutable as the calculation is expensive but once calculated, it |
| 120 | // remains const. |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 121 | mutable std::unique_ptr<VideoFrame> rotated_frame_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 124 | } // namespace cricket |
| 125 | |
kjellander@webrtc.org | 5ad1297 | 2016-02-12 06:39:40 +0100 | [diff] [blame] | 126 | #endif // WEBRTC_MEDIA_ENGINE_WEBRTCVIDEOFRAME_H_ |