blob: 284667f991c9ca0bd9aa29a5186b1f131d69bb40 [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:
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010032 struct UpdateRect {
33 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 Nikolaevskiy6aca0b72019-02-13 11:55:57 +010048 };
49
Emircan Uysaler800787f2018-07-16 10:01:49 -070050 // Preferred way of building VideoFrame objects.
Mirko Bonadei62a19d02019-11-11 19:59:54 +010051 class RTC_EXPORT Builder {
Emircan Uysaler800787f2018-07-16 10:01:49 -070052 public:
53 Builder();
54 ~Builder();
55
56 VideoFrame build();
57 Builder& set_video_frame_buffer(
58 const rtc::scoped_refptr<VideoFrameBuffer>& buffer);
59 Builder& set_timestamp_ms(int64_t timestamp_ms);
60 Builder& set_timestamp_us(int64_t timestamp_us);
61 Builder& set_timestamp_rtp(uint32_t timestamp_rtp);
62 Builder& set_ntp_time_ms(int64_t ntp_time_ms);
63 Builder& set_rotation(VideoRotation rotation);
Danil Chapovalovb7698942019-02-05 11:32:19 +010064 Builder& set_color_space(const absl::optional<ColorSpace>& color_space);
Johannes Kron4749e4e2018-11-21 10:18:18 +010065 Builder& set_color_space(const ColorSpace* color_space);
Artem Titov1ebfb6a2019-01-03 23:49:37 +010066 Builder& set_id(uint16_t id);
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010067 Builder& set_update_rect(const UpdateRect& update_rect);
Chen Xingf00bf422019-06-20 10:05:55 +020068 Builder& set_packet_infos(RtpPacketInfos packet_infos);
Emircan Uysaler800787f2018-07-16 10:01:49 -070069
70 private:
Artem Titov1ebfb6a2019-01-03 23:49:37 +010071 uint16_t id_ = 0;
Emircan Uysaler800787f2018-07-16 10:01:49 -070072 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
73 int64_t timestamp_us_ = 0;
74 uint32_t timestamp_rtp_ = 0;
75 int64_t ntp_time_ms_ = 0;
76 VideoRotation rotation_ = kVideoRotation_0;
77 absl::optional<ColorSpace> color_space_;
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010078 absl::optional<UpdateRect> update_rect_;
Chen Xingf00bf422019-06-20 10:05:55 +020079 RtpPacketInfos packet_infos_;
Emircan Uysaler800787f2018-07-16 10:01:49 -070080 };
81
82 // To be deprecated. Migrate all use to Builder.
nisseaf916892017-01-10 07:44:26 -080083 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
84 webrtc::VideoRotation rotation,
85 int64_t timestamp_us);
nisseaf916892017-01-10 07:44:26 -080086 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 11:14:32 +020087 uint32_t timestamp_rtp,
nisseaf916892017-01-10 07:44:26 -080088 int64_t render_time_ms,
89 VideoRotation rotation);
90
91 ~VideoFrame();
92
93 // Support move and copy.
94 VideoFrame(const VideoFrame&);
95 VideoFrame(VideoFrame&&);
96 VideoFrame& operator=(const VideoFrame&);
97 VideoFrame& operator=(VideoFrame&&);
98
99 // Get frame width.
100 int width() const;
nisseaf916892017-01-10 07:44:26 -0800101 // Get frame height.
102 int height() const;
kthelgason2bc68642017-02-07 07:02:22 -0800103 // Get frame size in pixels.
104 uint32_t size() const;
nisseaf916892017-01-10 07:44:26 -0800105
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100106 // Get frame ID. Returns 0 if ID is not set. Not guarantee to be transferred
107 // from the sender to the receiver, but preserved on single side. The id
108 // should be propagated between all frame modifications during its lifetime
109 // from capturing to sending as encoded image. It is intended to be unique
110 // over a time window of a few minutes for peer connection, to which
111 // corresponding video stream belongs to.
112 uint16_t id() const { return id_; }
113 void set_id(uint16_t id) { id_ = id; }
114
nisseaf916892017-01-10 07:44:26 -0800115 // System monotonic clock, same timebase as rtc::TimeMicros().
116 int64_t timestamp_us() const { return timestamp_us_; }
117 void set_timestamp_us(int64_t timestamp_us) { timestamp_us_ = timestamp_us; }
118
119 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
120 // merge, timestamps other than timestamp_us will likely be
121 // deprecated.
122
123 // Set frame timestamp (90kHz).
124 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
125
126 // Get frame timestamp (90kHz).
127 uint32_t timestamp() const { return timestamp_rtp_; }
128
129 // For now, transport_frame_id and rtp timestamp are the same.
130 // TODO(nisse): Must be handled differently for QUIC.
131 uint32_t transport_frame_id() const { return timestamp(); }
132
133 // Set capture ntp time in milliseconds.
134 void set_ntp_time_ms(int64_t ntp_time_ms) { ntp_time_ms_ = ntp_time_ms; }
135
136 // Get capture ntp time in milliseconds.
137 int64_t ntp_time_ms() const { return ntp_time_ms_; }
138
139 // Naming convention for Coordination of Video Orientation. Please see
140 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
141 //
142 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
143 //
144 // "not pending" = a frame that has a VideoRotation == 0.
145 //
146 // "apply rotation" = modify a frame from being "pending" to being "not
147 // pending" rotation (a no-op for "unrotated").
148 //
149 VideoRotation rotation() const { return rotation_; }
150 void set_rotation(VideoRotation rotation) { rotation_ = rotation; }
151
Johannes Kronfbf16832018-11-05 16:13:02 +0100152 // Get color space when available.
Danil Chapovalovb7698942019-02-05 11:32:19 +0100153 const absl::optional<ColorSpace>& color_space() const { return color_space_; }
154 void set_color_space(const absl::optional<ColorSpace>& color_space) {
155 color_space_ = color_space;
Johannes Kronf7f13e02018-12-12 11:17:43 +0100156 }
Johannes Kronfbf16832018-11-05 16:13:02 +0100157
nisseaf916892017-01-10 07:44:26 -0800158 // Get render time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -0800159 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -0800160 int64_t render_time_ms() const;
161
Ilya Nikolaevskiy871e1442019-02-11 13:35:03 +0100162 // Return the underlying buffer. Never nullptr for a properly
163 // initialized VideoFrame.
nisseaf916892017-01-10 07:44:26 -0800164 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
165
Ilya Nikolaevskiy4fc08552019-06-05 15:59:12 +0200166 void set_video_frame_buffer(
167 const rtc::scoped_refptr<VideoFrameBuffer>& buffer);
168
nisseaf916892017-01-10 07:44:26 -0800169 // TODO(nisse): Deprecated.
170 // Return true if the frame is stored in a texture.
171 bool is_texture() const {
magjed3f075492017-06-01 10:02:26 -0700172 return video_frame_buffer()->type() == VideoFrameBuffer::Type::kNative;
nisseaf916892017-01-10 07:44:26 -0800173 }
174
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100175 bool has_update_rect() const { return update_rect_.has_value(); }
176
177 // Returns update_rect set by the builder or set_update_rect() or whole frame
178 // rect if no update rect is available.
179 UpdateRect update_rect() const {
180 return update_rect_.value_or(UpdateRect{0, 0, width(), height()});
181 }
182
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100183 // Rectangle must be within the frame dimensions.
184 void set_update_rect(const VideoFrame::UpdateRect& update_rect) {
185 RTC_DCHECK_GE(update_rect.offset_x, 0);
186 RTC_DCHECK_GE(update_rect.offset_y, 0);
187 RTC_DCHECK_LE(update_rect.offset_x + update_rect.width, width());
188 RTC_DCHECK_LE(update_rect.offset_y + update_rect.height, height());
189 update_rect_ = update_rect;
190 }
191
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100192 void clear_update_rect() { update_rect_ = absl::nullopt; }
193
Chen Xingf00bf422019-06-20 10:05:55 +0200194 // Get information about packets used to assemble this video frame. Might be
195 // empty if the information isn't available.
196 const RtpPacketInfos& packet_infos() const { return packet_infos_; }
197 void set_packet_infos(RtpPacketInfos value) {
198 packet_infos_ = std::move(value);
199 }
200
nisseaf916892017-01-10 07:44:26 -0800201 private:
Ilya Nikolaevskiy871e1442019-02-11 13:35:03 +0100202 VideoFrame(uint16_t id,
203 const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
204 int64_t timestamp_us,
205 uint32_t timestamp_rtp,
206 int64_t ntp_time_ms,
207 VideoRotation rotation,
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100208 const absl::optional<ColorSpace>& color_space,
Chen Xingf00bf422019-06-20 10:05:55 +0200209 const absl::optional<UpdateRect>& update_rect,
210 RtpPacketInfos packet_infos);
Emircan Uysaler800787f2018-07-16 10:01:49 -0700211
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100212 uint16_t id_;
nisseaf916892017-01-10 07:44:26 -0800213 // An opaque reference counted handle that stores the pixel data.
214 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
215 uint32_t timestamp_rtp_;
216 int64_t ntp_time_ms_;
217 int64_t timestamp_us_;
218 VideoRotation rotation_;
Emircan Uysaler800787f2018-07-16 10:01:49 -0700219 absl::optional<ColorSpace> color_space_;
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100220 // Updated since the last frame area. If present it means that the bounding
221 // box of all the changes is within the rectangular area and is close to it.
222 // If absent, it means that there's no information about the change at all and
223 // update_rect() will return a rectangle corresponding to the entire frame.
224 absl::optional<UpdateRect> update_rect_;
Chen Xingf00bf422019-06-20 10:05:55 +0200225 // Information about packets used to assemble this video frame. This is needed
226 // by |SourceTracker| when the frame is delivered to the RTCRtpReceiver's
227 // MediaStreamTrack, in order to implement getContributingSources(). See:
228 // https://w3c.github.io/webrtc-pc/#dom-rtcrtpreceiver-getcontributingsources
229 RtpPacketInfos packet_infos_;
nisseaf916892017-01-10 07:44:26 -0800230};
231
232} // namespace webrtc
233
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200234#endif // API_VIDEO_VIDEO_FRAME_H_