blob: d16ef8ce2464ff88236a56ecdf17851f3fd603ca [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
Markus Handelle6eded32019-11-15 16:18:21 +010016#include <memory>
Chen Xingf00bf422019-06-20 10:05:55 +020017#include <utility>
nisseaf916892017-01-10 07:44:26 -080018
Emircan Uysaler800787f2018-07-16 10:01:49 -070019#include "absl/types/optional.h"
Markus Handelle6eded32019-11-15 16:18:21 +010020#include "api/array_view.h"
Chen Xingf00bf422019-06-20 10:05:55 +020021#include "api/rtp_packet_infos.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010022#include "api/scoped_refptr.h"
Emircan Uysaler800787f2018-07-16 10:01:49 -070023#include "api/video/color_space.h"
Johannes Kronfbf16832018-11-05 16:13:02 +010024#include "api/video/hdr_metadata.h"
Markus Handelle6eded32019-11-15 16:18:21 +010025#include "api/video/video_codec_type.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "api/video/video_frame_buffer.h"
Yves Gerey665174f2018-06-19 15:03:05 +020027#include "api/video/video_rotation.h"
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010028#include "rtc_base/checks.h"
Markus Handelle6eded32019-11-15 16:18:21 +010029#include "rtc_base/ref_count.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020030#include "rtc_base/system/rtc_export.h"
nisseaf916892017-01-10 07:44:26 -080031
nisseaf916892017-01-10 07:44:26 -080032namespace webrtc {
33
Mirko Bonadeiac194142018-10-22 17:08:37 +020034class RTC_EXPORT VideoFrame {
nisseaf916892017-01-10 07:44:26 -080035 public:
Mirko Bonadeid4002a72019-11-12 20:11:48 +010036 struct RTC_EXPORT UpdateRect {
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010037 int offset_x;
38 int offset_y;
39 int width;
40 int height;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +010041
42 // Makes this UpdateRect a bounding box of this and other rect.
43 void Union(const UpdateRect& other);
44
45 // Makes this UpdateRect an intersection of this and other rect.
46 void Intersect(const UpdateRect& other);
47
48 // Sets everything to 0, making this UpdateRect a zero-size (empty) update.
49 void MakeEmptyUpdate();
50
51 bool IsEmpty() const;
Ilya Nikolaevskiy0660cee2019-11-19 14:57:57 +010052
53 // Per-member equality check. Empty rectangles with different offsets would
54 // be considered different.
55 bool operator==(const UpdateRect& other) const {
56 return other.offset_x == offset_x && other.offset_y == offset_y &&
57 other.width == width && other.height == height;
58 }
59
60 bool operator!=(const UpdateRect& other) const { return !(*this == other); }
61
62 // Scales update_rect given original frame dimensions.
63 // Cropping is applied first, then rect is scaled down.
64 // Update rect is snapped to 2x2 grid due to possible UV subsampling and
65 // then expanded by additional 2 pixels in each direction to accommodate any
66 // possible scaling artifacts.
67 // Note, close but not equal update_rects on original frame may result in
68 // the same scaled update rects.
69 UpdateRect ScaleWithFrame(int frame_width,
70 int frame_height,
71 int crop_x,
72 int crop_y,
73 int crop_width,
74 int crop_height,
75 int scaled_width,
76 int scaled_height) const;
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010077 };
78
Markus Handelle6eded32019-11-15 16:18:21 +010079 // Interface for accessing elements of the encoded frame that was the base for
80 // the rest of the VideoFrame.
81 class EncodedVideoFrameBuffer : public rtc::RefCountInterface {
82 public:
83 // Returns a span of the bitstream data.
84 virtual rtc::ArrayView<const uint8_t> data() const = 0;
85
86 // Returns the colorspace of the encoded frame, or nullptr if not present
87 virtual const webrtc::ColorSpace* color_space() const = 0;
88
89 // Returns the codec of the encoded frame
90 virtual VideoCodecType codec() const = 0;
91
92 // Returns wether the encoded frame is a keyframe
93 virtual bool is_key_frame() const = 0;
94 };
95
Emircan Uysaler800787f2018-07-16 10:01:49 -070096 // Preferred way of building VideoFrame objects.
Mirko Bonadei62a19d02019-11-11 19:59:54 +010097 class RTC_EXPORT Builder {
Emircan Uysaler800787f2018-07-16 10:01:49 -070098 public:
99 Builder();
100 ~Builder();
101
102 VideoFrame build();
103 Builder& set_video_frame_buffer(
104 const rtc::scoped_refptr<VideoFrameBuffer>& buffer);
105 Builder& set_timestamp_ms(int64_t timestamp_ms);
106 Builder& set_timestamp_us(int64_t timestamp_us);
107 Builder& set_timestamp_rtp(uint32_t timestamp_rtp);
108 Builder& set_ntp_time_ms(int64_t ntp_time_ms);
109 Builder& set_rotation(VideoRotation rotation);
Danil Chapovalovb7698942019-02-05 11:32:19 +0100110 Builder& set_color_space(const absl::optional<ColorSpace>& color_space);
Johannes Kron4749e4e2018-11-21 10:18:18 +0100111 Builder& set_color_space(const ColorSpace* color_space);
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100112 Builder& set_id(uint16_t id);
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100113 Builder& set_update_rect(const UpdateRect& update_rect);
Chen Xingf00bf422019-06-20 10:05:55 +0200114 Builder& set_packet_infos(RtpPacketInfos packet_infos);
Markus Handelle6eded32019-11-15 16:18:21 +0100115 Builder& set_encoded_video_frame_buffer(
116 rtc::scoped_refptr<EncodedVideoFrameBuffer> encoded_frame_buffer);
Emircan Uysaler800787f2018-07-16 10:01:49 -0700117
118 private:
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100119 uint16_t id_ = 0;
Emircan Uysaler800787f2018-07-16 10:01:49 -0700120 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
121 int64_t timestamp_us_ = 0;
122 uint32_t timestamp_rtp_ = 0;
123 int64_t ntp_time_ms_ = 0;
124 VideoRotation rotation_ = kVideoRotation_0;
125 absl::optional<ColorSpace> color_space_;
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100126 absl::optional<UpdateRect> update_rect_;
Chen Xingf00bf422019-06-20 10:05:55 +0200127 RtpPacketInfos packet_infos_;
Markus Handelle6eded32019-11-15 16:18:21 +0100128 rtc::scoped_refptr<EncodedVideoFrameBuffer> encoded_frame_buffer_;
Emircan Uysaler800787f2018-07-16 10:01:49 -0700129 };
130
131 // To be deprecated. Migrate all use to Builder.
nisseaf916892017-01-10 07:44:26 -0800132 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
133 webrtc::VideoRotation rotation,
134 int64_t timestamp_us);
nisseaf916892017-01-10 07:44:26 -0800135 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 11:14:32 +0200136 uint32_t timestamp_rtp,
nisseaf916892017-01-10 07:44:26 -0800137 int64_t render_time_ms,
138 VideoRotation rotation);
139
140 ~VideoFrame();
141
142 // Support move and copy.
143 VideoFrame(const VideoFrame&);
144 VideoFrame(VideoFrame&&);
145 VideoFrame& operator=(const VideoFrame&);
146 VideoFrame& operator=(VideoFrame&&);
147
148 // Get frame width.
149 int width() const;
nisseaf916892017-01-10 07:44:26 -0800150 // Get frame height.
151 int height() const;
kthelgason2bc68642017-02-07 07:02:22 -0800152 // Get frame size in pixels.
153 uint32_t size() const;
nisseaf916892017-01-10 07:44:26 -0800154
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100155 // Get frame ID. Returns 0 if ID is not set. Not guarantee to be transferred
156 // from the sender to the receiver, but preserved on single side. The id
157 // should be propagated between all frame modifications during its lifetime
158 // from capturing to sending as encoded image. It is intended to be unique
159 // over a time window of a few minutes for peer connection, to which
160 // corresponding video stream belongs to.
161 uint16_t id() const { return id_; }
162 void set_id(uint16_t id) { id_ = id; }
163
nisseaf916892017-01-10 07:44:26 -0800164 // System monotonic clock, same timebase as rtc::TimeMicros().
165 int64_t timestamp_us() const { return timestamp_us_; }
166 void set_timestamp_us(int64_t timestamp_us) { timestamp_us_ = timestamp_us; }
167
168 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
169 // merge, timestamps other than timestamp_us will likely be
170 // deprecated.
171
172 // Set frame timestamp (90kHz).
173 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
174
175 // Get frame timestamp (90kHz).
176 uint32_t timestamp() const { return timestamp_rtp_; }
177
178 // For now, transport_frame_id and rtp timestamp are the same.
179 // TODO(nisse): Must be handled differently for QUIC.
180 uint32_t transport_frame_id() const { return timestamp(); }
181
182 // Set capture ntp time in milliseconds.
183 void set_ntp_time_ms(int64_t ntp_time_ms) { ntp_time_ms_ = ntp_time_ms; }
184
185 // Get capture ntp time in milliseconds.
186 int64_t ntp_time_ms() const { return ntp_time_ms_; }
187
188 // Naming convention for Coordination of Video Orientation. Please see
189 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
190 //
191 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
192 //
193 // "not pending" = a frame that has a VideoRotation == 0.
194 //
195 // "apply rotation" = modify a frame from being "pending" to being "not
196 // pending" rotation (a no-op for "unrotated").
197 //
198 VideoRotation rotation() const { return rotation_; }
199 void set_rotation(VideoRotation rotation) { rotation_ = rotation; }
200
Johannes Kronfbf16832018-11-05 16:13:02 +0100201 // Get color space when available.
Danil Chapovalovb7698942019-02-05 11:32:19 +0100202 const absl::optional<ColorSpace>& color_space() const { return color_space_; }
203 void set_color_space(const absl::optional<ColorSpace>& color_space) {
204 color_space_ = color_space;
Johannes Kronf7f13e02018-12-12 11:17:43 +0100205 }
Johannes Kronfbf16832018-11-05 16:13:02 +0100206
nisseaf916892017-01-10 07:44:26 -0800207 // Get render time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -0800208 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -0800209 int64_t render_time_ms() const;
210
Ilya Nikolaevskiy871e1442019-02-11 13:35:03 +0100211 // Return the underlying buffer. Never nullptr for a properly
212 // initialized VideoFrame.
nisseaf916892017-01-10 07:44:26 -0800213 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
214
Ilya Nikolaevskiy4fc08552019-06-05 15:59:12 +0200215 void set_video_frame_buffer(
216 const rtc::scoped_refptr<VideoFrameBuffer>& buffer);
217
Markus Handelle6eded32019-11-15 16:18:21 +0100218 void set_encoded_video_frame_buffer(
219 rtc::scoped_refptr<EncodedVideoFrameBuffer> encoded_frame_buffer);
220
221 rtc::scoped_refptr<EncodedVideoFrameBuffer> encoded_video_frame_buffer()
222 const;
223
nisseaf916892017-01-10 07:44:26 -0800224 // TODO(nisse): Deprecated.
225 // Return true if the frame is stored in a texture.
226 bool is_texture() const {
magjed3f075492017-06-01 10:02:26 -0700227 return video_frame_buffer()->type() == VideoFrameBuffer::Type::kNative;
nisseaf916892017-01-10 07:44:26 -0800228 }
229
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100230 bool has_update_rect() const { return update_rect_.has_value(); }
231
232 // Returns update_rect set by the builder or set_update_rect() or whole frame
233 // rect if no update rect is available.
234 UpdateRect update_rect() const {
235 return update_rect_.value_or(UpdateRect{0, 0, width(), height()});
236 }
237
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100238 // Rectangle must be within the frame dimensions.
239 void set_update_rect(const VideoFrame::UpdateRect& update_rect) {
240 RTC_DCHECK_GE(update_rect.offset_x, 0);
241 RTC_DCHECK_GE(update_rect.offset_y, 0);
242 RTC_DCHECK_LE(update_rect.offset_x + update_rect.width, width());
243 RTC_DCHECK_LE(update_rect.offset_y + update_rect.height, height());
244 update_rect_ = update_rect;
245 }
246
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100247 void clear_update_rect() { update_rect_ = absl::nullopt; }
248
Chen Xingf00bf422019-06-20 10:05:55 +0200249 // Get information about packets used to assemble this video frame. Might be
250 // empty if the information isn't available.
251 const RtpPacketInfos& packet_infos() const { return packet_infos_; }
252 void set_packet_infos(RtpPacketInfos value) {
253 packet_infos_ = std::move(value);
254 }
255
nisseaf916892017-01-10 07:44:26 -0800256 private:
Ilya Nikolaevskiy871e1442019-02-11 13:35:03 +0100257 VideoFrame(uint16_t id,
258 const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
259 int64_t timestamp_us,
260 uint32_t timestamp_rtp,
261 int64_t ntp_time_ms,
262 VideoRotation rotation,
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100263 const absl::optional<ColorSpace>& color_space,
Chen Xingf00bf422019-06-20 10:05:55 +0200264 const absl::optional<UpdateRect>& update_rect,
Markus Handelle6eded32019-11-15 16:18:21 +0100265 RtpPacketInfos packet_infos,
266 const rtc::scoped_refptr<EncodedVideoFrameBuffer>& encoded_frame);
Emircan Uysaler800787f2018-07-16 10:01:49 -0700267
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100268 uint16_t id_;
Markus Handelle6eded32019-11-15 16:18:21 +0100269 // A reference counted handle that stores the pixel data.
nisseaf916892017-01-10 07:44:26 -0800270 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
Markus Handelle6eded32019-11-15 16:18:21 +0100271 // A reference counted handle that points to an encoded frame
272 rtc::scoped_refptr<EncodedVideoFrameBuffer> encoded_frame_buffer_;
nisseaf916892017-01-10 07:44:26 -0800273 uint32_t timestamp_rtp_;
274 int64_t ntp_time_ms_;
275 int64_t timestamp_us_;
276 VideoRotation rotation_;
Emircan Uysaler800787f2018-07-16 10:01:49 -0700277 absl::optional<ColorSpace> color_space_;
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100278 // Updated since the last frame area. If present it means that the bounding
279 // box of all the changes is within the rectangular area and is close to it.
280 // If absent, it means that there's no information about the change at all and
281 // update_rect() will return a rectangle corresponding to the entire frame.
282 absl::optional<UpdateRect> update_rect_;
Chen Xingf00bf422019-06-20 10:05:55 +0200283 // Information about packets used to assemble this video frame. This is needed
284 // by |SourceTracker| when the frame is delivered to the RTCRtpReceiver's
285 // MediaStreamTrack, in order to implement getContributingSources(). See:
286 // https://w3c.github.io/webrtc-pc/#dom-rtcrtpreceiver-getcontributingsources
287 RtpPacketInfos packet_infos_;
nisseaf916892017-01-10 07:44:26 -0800288};
289
290} // namespace webrtc
291
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200292#endif // API_VIDEO_VIDEO_FRAME_H_