nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef API_VIDEO_VIDEO_FRAME_H_ |
| 12 | #define API_VIDEO_VIDEO_FRAME_H_ |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 13 | |
| 14 | #include <stdint.h> |
| 15 | |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 16 | #include "absl/types/optional.h" |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 17 | #include "api/scoped_refptr.h" |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 18 | #include "api/video/color_space.h" |
Johannes Kron | fbf1683 | 2018-11-05 16:13:02 +0100 | [diff] [blame] | 19 | #include "api/video/hdr_metadata.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "api/video/video_frame_buffer.h" |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 21 | #include "api/video/video_rotation.h" |
Mirko Bonadei | ac19414 | 2018-10-22 17:08:37 +0200 | [diff] [blame] | 22 | #include "rtc_base/system/rtc_export.h" |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 23 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 24 | namespace webrtc { |
| 25 | |
Mirko Bonadei | ac19414 | 2018-10-22 17:08:37 +0200 | [diff] [blame] | 26 | class RTC_EXPORT VideoFrame { |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 27 | public: |
Ilya Nikolaevskiy | 12e5d39 | 2019-01-31 13:01:35 +0100 | [diff] [blame] | 28 | // Describes a partial frame, which contains only a changed region compared |
| 29 | // to a previous frame. Shouldn't be set on the fully updated picture. |
| 30 | struct PartialFrameDescription { |
| 31 | // Coordinates of top-left corner of the changed region in the full picture. |
| 32 | int offset_x; |
| 33 | int offset_y; |
| 34 | }; |
| 35 | |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 36 | // Preferred way of building VideoFrame objects. |
| 37 | class Builder { |
| 38 | public: |
| 39 | Builder(); |
| 40 | ~Builder(); |
| 41 | |
| 42 | VideoFrame build(); |
| 43 | Builder& set_video_frame_buffer( |
| 44 | const rtc::scoped_refptr<VideoFrameBuffer>& buffer); |
| 45 | Builder& set_timestamp_ms(int64_t timestamp_ms); |
| 46 | Builder& set_timestamp_us(int64_t timestamp_us); |
| 47 | Builder& set_timestamp_rtp(uint32_t timestamp_rtp); |
| 48 | Builder& set_ntp_time_ms(int64_t ntp_time_ms); |
| 49 | Builder& set_rotation(VideoRotation rotation); |
Danil Chapovalov | b769894 | 2019-02-05 11:32:19 +0100 | [diff] [blame^] | 50 | Builder& set_color_space(const absl::optional<ColorSpace>& color_space); |
Johannes Kron | 4749e4e | 2018-11-21 10:18:18 +0100 | [diff] [blame] | 51 | Builder& set_color_space(const ColorSpace* color_space); |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 52 | Builder& set_id(uint16_t id); |
Ilya Nikolaevskiy | 12e5d39 | 2019-01-31 13:01:35 +0100 | [diff] [blame] | 53 | Builder& set_partial_frame_description( |
| 54 | const absl::optional<PartialFrameDescription>& description); |
| 55 | Builder& set_cache_buffer_for_partial_updates( |
| 56 | bool cache_buffer_for_partial_updates); |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 57 | |
| 58 | private: |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 59 | uint16_t id_ = 0; |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 60 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_; |
| 61 | int64_t timestamp_us_ = 0; |
| 62 | uint32_t timestamp_rtp_ = 0; |
| 63 | int64_t ntp_time_ms_ = 0; |
| 64 | VideoRotation rotation_ = kVideoRotation_0; |
| 65 | absl::optional<ColorSpace> color_space_; |
Ilya Nikolaevskiy | 12e5d39 | 2019-01-31 13:01:35 +0100 | [diff] [blame] | 66 | absl::optional<PartialFrameDescription> partial_frame_description_; |
| 67 | bool cache_buffer_for_partial_updates_ = false; |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | // To be deprecated. Migrate all use to Builder. |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 71 | VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
| 72 | webrtc::VideoRotation rotation, |
| 73 | int64_t timestamp_us); |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 74 | VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
Niels Möller | 2ac6446 | 2018-06-11 11:14:32 +0200 | [diff] [blame] | 75 | uint32_t timestamp_rtp, |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 76 | int64_t render_time_ms, |
| 77 | VideoRotation rotation); |
| 78 | |
| 79 | ~VideoFrame(); |
| 80 | |
| 81 | // Support move and copy. |
| 82 | VideoFrame(const VideoFrame&); |
| 83 | VideoFrame(VideoFrame&&); |
| 84 | VideoFrame& operator=(const VideoFrame&); |
| 85 | VideoFrame& operator=(VideoFrame&&); |
| 86 | |
| 87 | // Get frame width. |
| 88 | int width() const; |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 89 | // Get frame height. |
| 90 | int height() const; |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 91 | // Get frame size in pixels. |
| 92 | uint32_t size() const; |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 93 | |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 94 | // Get frame ID. Returns 0 if ID is not set. Not guarantee to be transferred |
| 95 | // from the sender to the receiver, but preserved on single side. The id |
| 96 | // should be propagated between all frame modifications during its lifetime |
| 97 | // from capturing to sending as encoded image. It is intended to be unique |
| 98 | // over a time window of a few minutes for peer connection, to which |
| 99 | // corresponding video stream belongs to. |
| 100 | uint16_t id() const { return id_; } |
| 101 | void set_id(uint16_t id) { id_ = id; } |
| 102 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 103 | // System monotonic clock, same timebase as rtc::TimeMicros(). |
| 104 | int64_t timestamp_us() const { return timestamp_us_; } |
| 105 | void set_timestamp_us(int64_t timestamp_us) { timestamp_us_ = timestamp_us; } |
| 106 | |
| 107 | // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame |
| 108 | // merge, timestamps other than timestamp_us will likely be |
| 109 | // deprecated. |
| 110 | |
| 111 | // Set frame timestamp (90kHz). |
| 112 | void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; } |
| 113 | |
| 114 | // Get frame timestamp (90kHz). |
| 115 | uint32_t timestamp() const { return timestamp_rtp_; } |
| 116 | |
| 117 | // For now, transport_frame_id and rtp timestamp are the same. |
| 118 | // TODO(nisse): Must be handled differently for QUIC. |
| 119 | uint32_t transport_frame_id() const { return timestamp(); } |
| 120 | |
| 121 | // Set capture ntp time in milliseconds. |
nisse | 1c0dea8 | 2017-01-30 02:43:18 -0800 | [diff] [blame] | 122 | // TODO(nisse): Deprecated. Migrate all users to timestamp_us(). |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 123 | void set_ntp_time_ms(int64_t ntp_time_ms) { ntp_time_ms_ = ntp_time_ms; } |
| 124 | |
| 125 | // Get capture ntp time in milliseconds. |
nisse | 1c0dea8 | 2017-01-30 02:43:18 -0800 | [diff] [blame] | 126 | // TODO(nisse): Deprecated. Migrate all users to timestamp_us(). |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 127 | int64_t ntp_time_ms() const { return ntp_time_ms_; } |
| 128 | |
| 129 | // Naming convention for Coordination of Video Orientation. Please see |
| 130 | // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf |
| 131 | // |
| 132 | // "pending rotation" or "pending" = a frame that has a VideoRotation > 0. |
| 133 | // |
| 134 | // "not pending" = a frame that has a VideoRotation == 0. |
| 135 | // |
| 136 | // "apply rotation" = modify a frame from being "pending" to being "not |
| 137 | // pending" rotation (a no-op for "unrotated"). |
| 138 | // |
| 139 | VideoRotation rotation() const { return rotation_; } |
| 140 | void set_rotation(VideoRotation rotation) { rotation_ = rotation; } |
| 141 | |
Johannes Kron | fbf1683 | 2018-11-05 16:13:02 +0100 | [diff] [blame] | 142 | // Get color space when available. |
Danil Chapovalov | b769894 | 2019-02-05 11:32:19 +0100 | [diff] [blame^] | 143 | const absl::optional<ColorSpace>& color_space() const { return color_space_; } |
| 144 | void set_color_space(const absl::optional<ColorSpace>& color_space) { |
| 145 | color_space_ = color_space; |
Johannes Kron | f7f13e0 | 2018-12-12 11:17:43 +0100 | [diff] [blame] | 146 | } |
Johannes Kron | fbf1683 | 2018-11-05 16:13:02 +0100 | [diff] [blame] | 147 | |
Ilya Nikolaevskiy | 12e5d39 | 2019-01-31 13:01:35 +0100 | [diff] [blame] | 148 | const PartialFrameDescription* partial_frame_description() const { |
| 149 | return partial_frame_description_ ? &partial_frame_description_.value() |
| 150 | : nullptr; |
| 151 | } |
| 152 | void set_partial_frame_description( |
| 153 | const absl::optional<PartialFrameDescription>& description) { |
| 154 | partial_frame_description_ = description; |
| 155 | } |
| 156 | |
| 157 | void set_cache_buffer_for_partial_updates( |
| 158 | bool cache_buffer_for_partial_updates) { |
| 159 | cache_buffer_for_partial_updates_ = cache_buffer_for_partial_updates; |
| 160 | } |
| 161 | bool cache_buffer_for_partial_updates() const { |
| 162 | return cache_buffer_for_partial_updates_; |
| 163 | } |
| 164 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 165 | // Get render time in milliseconds. |
nisse | 1c0dea8 | 2017-01-30 02:43:18 -0800 | [diff] [blame] | 166 | // TODO(nisse): Deprecated. Migrate all users to timestamp_us(). |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 167 | int64_t render_time_ms() const; |
| 168 | |
Ilya Nikolaevskiy | 12e5d39 | 2019-01-31 13:01:35 +0100 | [diff] [blame] | 169 | // Return the underlying buffer. This can only be a nullptr for a partial |
| 170 | // update VideoFrame with no changed pixels. |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 171 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const; |
Ilya Nikolaevskiy | 12e5d39 | 2019-01-31 13:01:35 +0100 | [diff] [blame] | 172 | void set_video_frame_buffer(rtc::scoped_refptr<VideoFrameBuffer> buffer); |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 173 | |
| 174 | // TODO(nisse): Deprecated. |
| 175 | // Return true if the frame is stored in a texture. |
| 176 | bool is_texture() const { |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 177 | return video_frame_buffer()->type() == VideoFrameBuffer::Type::kNative; |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | private: |
Ilya Nikolaevskiy | 12e5d39 | 2019-01-31 13:01:35 +0100 | [diff] [blame] | 181 | VideoFrame( |
| 182 | uint16_t id, |
| 183 | const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
| 184 | int64_t timestamp_us, |
| 185 | uint32_t timestamp_rtp, |
| 186 | int64_t ntp_time_ms, |
| 187 | VideoRotation rotation, |
| 188 | const absl::optional<ColorSpace>& color_space, |
| 189 | const absl::optional<PartialFrameDescription> partial_frame_description, |
| 190 | bool cache_buffer_for_partial_updates_); |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 191 | |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 192 | uint16_t id_; |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 193 | // An opaque reference counted handle that stores the pixel data. |
| 194 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_; |
| 195 | uint32_t timestamp_rtp_; |
| 196 | int64_t ntp_time_ms_; |
| 197 | int64_t timestamp_us_; |
| 198 | VideoRotation rotation_; |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 199 | absl::optional<ColorSpace> color_space_; |
Ilya Nikolaevskiy | 12e5d39 | 2019-01-31 13:01:35 +0100 | [diff] [blame] | 200 | absl::optional<PartialFrameDescription> partial_frame_description_; |
| 201 | // Should be set on all frames, if the source may produce partial updates. |
| 202 | bool cache_buffer_for_partial_updates_; |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | } // namespace webrtc |
| 206 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 207 | #endif // API_VIDEO_VIDEO_FRAME_H_ |