blob: 9b3761e96db98565024bbae42f85bfe5ba0f6bbf [file] [log] [blame]
nisseaf916892017-01-10 07:44:26 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef API_VIDEO_VIDEO_FRAME_H_
12#define API_VIDEO_VIDEO_FRAME_H_
nisseaf916892017-01-10 07:44:26 -080013
14#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Chen Xingf00bf422019-06-20 10:05:55 +020016#include <utility>
nisseaf916892017-01-10 07:44:26 -080017
Emircan Uysaler800787f2018-07-16 10:01:49 -070018#include "absl/types/optional.h"
Chen Xingf00bf422019-06-20 10:05:55 +020019#include "api/rtp_packet_infos.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010020#include "api/scoped_refptr.h"
Emircan Uysaler800787f2018-07-16 10:01:49 -070021#include "api/video/color_space.h"
Johannes Kronfbf16832018-11-05 16:13:02 +010022#include "api/video/hdr_metadata.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "api/video/video_frame_buffer.h"
Yves Gerey665174f2018-06-19 15:03:05 +020024#include "api/video/video_rotation.h"
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010025#include "rtc_base/checks.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020026#include "rtc_base/system/rtc_export.h"
nisseaf916892017-01-10 07:44:26 -080027
nisseaf916892017-01-10 07:44:26 -080028namespace webrtc {
29
Mirko Bonadeiac194142018-10-22 17:08:37 +020030class RTC_EXPORT VideoFrame {
nisseaf916892017-01-10 07:44:26 -080031 public:
Mirko Bonadeid4002a72019-11-12 20:11:48 +010032 struct RTC_EXPORT UpdateRect {
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010033 int offset_x;
34 int offset_y;
35 int width;
36 int height;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +010037
38 // Makes this UpdateRect a bounding box of this and other rect.
39 void Union(const UpdateRect& other);
40
41 // Makes this UpdateRect an intersection of this and other rect.
42 void Intersect(const UpdateRect& other);
43
44 // Sets everything to 0, making this UpdateRect a zero-size (empty) update.
45 void MakeEmptyUpdate();
46
47 bool IsEmpty() const;
Ilya Nikolaevskiy0660cee2019-11-19 14:57:57 +010048
49 // Per-member equality check. Empty rectangles with different offsets would
50 // be considered different.
51 bool operator==(const UpdateRect& other) const {
52 return other.offset_x == offset_x && other.offset_y == offset_y &&
53 other.width == width && other.height == height;
54 }
55
56 bool operator!=(const UpdateRect& other) const { return !(*this == other); }
57
58 // Scales update_rect given original frame dimensions.
59 // Cropping is applied first, then rect is scaled down.
60 // Update rect is snapped to 2x2 grid due to possible UV subsampling and
61 // then expanded by additional 2 pixels in each direction to accommodate any
62 // possible scaling artifacts.
63 // Note, close but not equal update_rects on original frame may result in
64 // the same scaled update rects.
65 UpdateRect ScaleWithFrame(int frame_width,
66 int frame_height,
67 int crop_x,
68 int crop_y,
69 int crop_width,
70 int crop_height,
71 int scaled_width,
72 int scaled_height) const;
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010073 };
74
Emircan Uysaler800787f2018-07-16 10:01:49 -070075 // Preferred way of building VideoFrame objects.
Mirko Bonadei62a19d02019-11-11 19:59:54 +010076 class RTC_EXPORT Builder {
Emircan Uysaler800787f2018-07-16 10:01:49 -070077 public:
78 Builder();
79 ~Builder();
80
81 VideoFrame build();
82 Builder& set_video_frame_buffer(
83 const rtc::scoped_refptr<VideoFrameBuffer>& buffer);
84 Builder& set_timestamp_ms(int64_t timestamp_ms);
85 Builder& set_timestamp_us(int64_t timestamp_us);
86 Builder& set_timestamp_rtp(uint32_t timestamp_rtp);
87 Builder& set_ntp_time_ms(int64_t ntp_time_ms);
88 Builder& set_rotation(VideoRotation rotation);
Danil Chapovalovb7698942019-02-05 11:32:19 +010089 Builder& set_color_space(const absl::optional<ColorSpace>& color_space);
Johannes Kron4749e4e2018-11-21 10:18:18 +010090 Builder& set_color_space(const ColorSpace* color_space);
Artem Titov1ebfb6a2019-01-03 23:49:37 +010091 Builder& set_id(uint16_t id);
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010092 Builder& set_update_rect(const UpdateRect& update_rect);
Chen Xingf00bf422019-06-20 10:05:55 +020093 Builder& set_packet_infos(RtpPacketInfos packet_infos);
Emircan Uysaler800787f2018-07-16 10:01:49 -070094
95 private:
Artem Titov1ebfb6a2019-01-03 23:49:37 +010096 uint16_t id_ = 0;
Emircan Uysaler800787f2018-07-16 10:01:49 -070097 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
98 int64_t timestamp_us_ = 0;
99 uint32_t timestamp_rtp_ = 0;
100 int64_t ntp_time_ms_ = 0;
101 VideoRotation rotation_ = kVideoRotation_0;
102 absl::optional<ColorSpace> color_space_;
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100103 absl::optional<UpdateRect> update_rect_;
Chen Xingf00bf422019-06-20 10:05:55 +0200104 RtpPacketInfos packet_infos_;
Emircan Uysaler800787f2018-07-16 10:01:49 -0700105 };
106
107 // To be deprecated. Migrate all use to Builder.
nisseaf916892017-01-10 07:44:26 -0800108 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
109 webrtc::VideoRotation rotation,
110 int64_t timestamp_us);
nisseaf916892017-01-10 07:44:26 -0800111 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 11:14:32 +0200112 uint32_t timestamp_rtp,
nisseaf916892017-01-10 07:44:26 -0800113 int64_t render_time_ms,
114 VideoRotation rotation);
115
116 ~VideoFrame();
117
118 // Support move and copy.
119 VideoFrame(const VideoFrame&);
120 VideoFrame(VideoFrame&&);
121 VideoFrame& operator=(const VideoFrame&);
122 VideoFrame& operator=(VideoFrame&&);
123
124 // Get frame width.
125 int width() const;
nisseaf916892017-01-10 07:44:26 -0800126 // Get frame height.
127 int height() const;
kthelgason2bc68642017-02-07 07:02:22 -0800128 // Get frame size in pixels.
129 uint32_t size() const;
nisseaf916892017-01-10 07:44:26 -0800130
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100131 // Get frame ID. Returns 0 if ID is not set. Not guarantee to be transferred
132 // from the sender to the receiver, but preserved on single side. The id
133 // should be propagated between all frame modifications during its lifetime
134 // from capturing to sending as encoded image. It is intended to be unique
135 // over a time window of a few minutes for peer connection, to which
136 // corresponding video stream belongs to.
137 uint16_t id() const { return id_; }
138 void set_id(uint16_t id) { id_ = id; }
139
nisseaf916892017-01-10 07:44:26 -0800140 // System monotonic clock, same timebase as rtc::TimeMicros().
141 int64_t timestamp_us() const { return timestamp_us_; }
142 void set_timestamp_us(int64_t timestamp_us) { timestamp_us_ = timestamp_us; }
143
144 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
145 // merge, timestamps other than timestamp_us will likely be
146 // deprecated.
147
148 // Set frame timestamp (90kHz).
149 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
150
151 // Get frame timestamp (90kHz).
152 uint32_t timestamp() const { return timestamp_rtp_; }
153
154 // For now, transport_frame_id and rtp timestamp are the same.
155 // TODO(nisse): Must be handled differently for QUIC.
156 uint32_t transport_frame_id() const { return timestamp(); }
157
158 // Set capture ntp time in milliseconds.
159 void set_ntp_time_ms(int64_t ntp_time_ms) { ntp_time_ms_ = ntp_time_ms; }
160
161 // Get capture ntp time in milliseconds.
162 int64_t ntp_time_ms() const { return ntp_time_ms_; }
163
164 // Naming convention for Coordination of Video Orientation. Please see
165 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
166 //
167 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
168 //
169 // "not pending" = a frame that has a VideoRotation == 0.
170 //
171 // "apply rotation" = modify a frame from being "pending" to being "not
172 // pending" rotation (a no-op for "unrotated").
173 //
174 VideoRotation rotation() const { return rotation_; }
175 void set_rotation(VideoRotation rotation) { rotation_ = rotation; }
176
Johannes Kronfbf16832018-11-05 16:13:02 +0100177 // Get color space when available.
Danil Chapovalovb7698942019-02-05 11:32:19 +0100178 const absl::optional<ColorSpace>& color_space() const { return color_space_; }
179 void set_color_space(const absl::optional<ColorSpace>& color_space) {
180 color_space_ = color_space;
Johannes Kronf7f13e02018-12-12 11:17:43 +0100181 }
Johannes Kronfbf16832018-11-05 16:13:02 +0100182
nisseaf916892017-01-10 07:44:26 -0800183 // Get render time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -0800184 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -0800185 int64_t render_time_ms() const;
186
Ilya Nikolaevskiy871e1442019-02-11 13:35:03 +0100187 // Return the underlying buffer. Never nullptr for a properly
188 // initialized VideoFrame.
nisseaf916892017-01-10 07:44:26 -0800189 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
190
Ilya Nikolaevskiy4fc08552019-06-05 15:59:12 +0200191 void set_video_frame_buffer(
192 const rtc::scoped_refptr<VideoFrameBuffer>& buffer);
193
nisseaf916892017-01-10 07:44:26 -0800194 // TODO(nisse): Deprecated.
195 // Return true if the frame is stored in a texture.
196 bool is_texture() const {
magjed3f075492017-06-01 10:02:26 -0700197 return video_frame_buffer()->type() == VideoFrameBuffer::Type::kNative;
nisseaf916892017-01-10 07:44:26 -0800198 }
199
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100200 bool has_update_rect() const { return update_rect_.has_value(); }
201
202 // Returns update_rect set by the builder or set_update_rect() or whole frame
203 // rect if no update rect is available.
204 UpdateRect update_rect() const {
205 return update_rect_.value_or(UpdateRect{0, 0, width(), height()});
206 }
207
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100208 // Rectangle must be within the frame dimensions.
209 void set_update_rect(const VideoFrame::UpdateRect& update_rect) {
210 RTC_DCHECK_GE(update_rect.offset_x, 0);
211 RTC_DCHECK_GE(update_rect.offset_y, 0);
212 RTC_DCHECK_LE(update_rect.offset_x + update_rect.width, width());
213 RTC_DCHECK_LE(update_rect.offset_y + update_rect.height, height());
214 update_rect_ = update_rect;
215 }
216
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100217 void clear_update_rect() { update_rect_ = absl::nullopt; }
218
Chen Xingf00bf422019-06-20 10:05:55 +0200219 // Get information about packets used to assemble this video frame. Might be
220 // empty if the information isn't available.
221 const RtpPacketInfos& packet_infos() const { return packet_infos_; }
222 void set_packet_infos(RtpPacketInfos value) {
223 packet_infos_ = std::move(value);
224 }
225
nisseaf916892017-01-10 07:44:26 -0800226 private:
Ilya Nikolaevskiy871e1442019-02-11 13:35:03 +0100227 VideoFrame(uint16_t id,
228 const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
229 int64_t timestamp_us,
230 uint32_t timestamp_rtp,
231 int64_t ntp_time_ms,
232 VideoRotation rotation,
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100233 const absl::optional<ColorSpace>& color_space,
Chen Xingf00bf422019-06-20 10:05:55 +0200234 const absl::optional<UpdateRect>& update_rect,
Markus Handell026f64f2019-11-21 14:07:19 +0000235 RtpPacketInfos packet_infos);
Emircan Uysaler800787f2018-07-16 10:01:49 -0700236
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100237 uint16_t id_;
Markus Handell026f64f2019-11-21 14:07:19 +0000238 // An opaque reference counted handle that stores the pixel data.
nisseaf916892017-01-10 07:44:26 -0800239 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
240 uint32_t timestamp_rtp_;
241 int64_t ntp_time_ms_;
242 int64_t timestamp_us_;
243 VideoRotation rotation_;
Emircan Uysaler800787f2018-07-16 10:01:49 -0700244 absl::optional<ColorSpace> color_space_;
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100245 // Updated since the last frame area. If present it means that the bounding
246 // box of all the changes is within the rectangular area and is close to it.
247 // If absent, it means that there's no information about the change at all and
248 // update_rect() will return a rectangle corresponding to the entire frame.
249 absl::optional<UpdateRect> update_rect_;
Chen Xingf00bf422019-06-20 10:05:55 +0200250 // Information about packets used to assemble this video frame. This is needed
251 // by |SourceTracker| when the frame is delivered to the RTCRtpReceiver's
252 // MediaStreamTrack, in order to implement getContributingSources(). See:
253 // https://w3c.github.io/webrtc-pc/#dom-rtcrtpreceiver-getcontributingsources
254 RtpPacketInfos packet_infos_;
nisseaf916892017-01-10 07:44:26 -0800255};
256
257} // namespace webrtc
258
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200259#endif // API_VIDEO_VIDEO_FRAME_H_