blob: 338e2fdbd195f607f6bf7358adf58ab55172159f [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 Nikolaevskiy6aca0b72019-02-13 11:55:57 +010052 };
53
Markus Handelle6eded32019-11-15 16:18:21 +010054 // Interface for accessing elements of the encoded frame that was the base for
55 // the rest of the VideoFrame.
56 class EncodedVideoFrameBuffer : public rtc::RefCountInterface {
57 public:
58 // Returns a span of the bitstream data.
59 virtual rtc::ArrayView<const uint8_t> data() const = 0;
60
61 // Returns the colorspace of the encoded frame, or nullptr if not present
62 virtual const webrtc::ColorSpace* color_space() const = 0;
63
64 // Returns the codec of the encoded frame
65 virtual VideoCodecType codec() const = 0;
66
67 // Returns wether the encoded frame is a keyframe
68 virtual bool is_key_frame() const = 0;
69 };
70
Emircan Uysaler800787f2018-07-16 10:01:49 -070071 // Preferred way of building VideoFrame objects.
Mirko Bonadei62a19d02019-11-11 19:59:54 +010072 class RTC_EXPORT Builder {
Emircan Uysaler800787f2018-07-16 10:01:49 -070073 public:
74 Builder();
75 ~Builder();
76
77 VideoFrame build();
78 Builder& set_video_frame_buffer(
79 const rtc::scoped_refptr<VideoFrameBuffer>& buffer);
80 Builder& set_timestamp_ms(int64_t timestamp_ms);
81 Builder& set_timestamp_us(int64_t timestamp_us);
82 Builder& set_timestamp_rtp(uint32_t timestamp_rtp);
83 Builder& set_ntp_time_ms(int64_t ntp_time_ms);
84 Builder& set_rotation(VideoRotation rotation);
Danil Chapovalovb7698942019-02-05 11:32:19 +010085 Builder& set_color_space(const absl::optional<ColorSpace>& color_space);
Johannes Kron4749e4e2018-11-21 10:18:18 +010086 Builder& set_color_space(const ColorSpace* color_space);
Artem Titov1ebfb6a2019-01-03 23:49:37 +010087 Builder& set_id(uint16_t id);
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010088 Builder& set_update_rect(const UpdateRect& update_rect);
Chen Xingf00bf422019-06-20 10:05:55 +020089 Builder& set_packet_infos(RtpPacketInfos packet_infos);
Markus Handelle6eded32019-11-15 16:18:21 +010090 Builder& set_encoded_video_frame_buffer(
91 rtc::scoped_refptr<EncodedVideoFrameBuffer> encoded_frame_buffer);
Emircan Uysaler800787f2018-07-16 10:01:49 -070092
93 private:
Artem Titov1ebfb6a2019-01-03 23:49:37 +010094 uint16_t id_ = 0;
Emircan Uysaler800787f2018-07-16 10:01:49 -070095 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
96 int64_t timestamp_us_ = 0;
97 uint32_t timestamp_rtp_ = 0;
98 int64_t ntp_time_ms_ = 0;
99 VideoRotation rotation_ = kVideoRotation_0;
100 absl::optional<ColorSpace> color_space_;
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100101 absl::optional<UpdateRect> update_rect_;
Chen Xingf00bf422019-06-20 10:05:55 +0200102 RtpPacketInfos packet_infos_;
Markus Handelle6eded32019-11-15 16:18:21 +0100103 rtc::scoped_refptr<EncodedVideoFrameBuffer> encoded_frame_buffer_;
Emircan Uysaler800787f2018-07-16 10:01:49 -0700104 };
105
106 // To be deprecated. Migrate all use to Builder.
nisseaf916892017-01-10 07:44:26 -0800107 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
108 webrtc::VideoRotation rotation,
109 int64_t timestamp_us);
nisseaf916892017-01-10 07:44:26 -0800110 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 11:14:32 +0200111 uint32_t timestamp_rtp,
nisseaf916892017-01-10 07:44:26 -0800112 int64_t render_time_ms,
113 VideoRotation rotation);
114
115 ~VideoFrame();
116
117 // Support move and copy.
118 VideoFrame(const VideoFrame&);
119 VideoFrame(VideoFrame&&);
120 VideoFrame& operator=(const VideoFrame&);
121 VideoFrame& operator=(VideoFrame&&);
122
123 // Get frame width.
124 int width() const;
nisseaf916892017-01-10 07:44:26 -0800125 // Get frame height.
126 int height() const;
kthelgason2bc68642017-02-07 07:02:22 -0800127 // Get frame size in pixels.
128 uint32_t size() const;
nisseaf916892017-01-10 07:44:26 -0800129
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100130 // Get frame ID. Returns 0 if ID is not set. Not guarantee to be transferred
131 // from the sender to the receiver, but preserved on single side. The id
132 // should be propagated between all frame modifications during its lifetime
133 // from capturing to sending as encoded image. It is intended to be unique
134 // over a time window of a few minutes for peer connection, to which
135 // corresponding video stream belongs to.
136 uint16_t id() const { return id_; }
137 void set_id(uint16_t id) { id_ = id; }
138
nisseaf916892017-01-10 07:44:26 -0800139 // System monotonic clock, same timebase as rtc::TimeMicros().
140 int64_t timestamp_us() const { return timestamp_us_; }
141 void set_timestamp_us(int64_t timestamp_us) { timestamp_us_ = timestamp_us; }
142
143 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
144 // merge, timestamps other than timestamp_us will likely be
145 // deprecated.
146
147 // Set frame timestamp (90kHz).
148 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
149
150 // Get frame timestamp (90kHz).
151 uint32_t timestamp() const { return timestamp_rtp_; }
152
153 // For now, transport_frame_id and rtp timestamp are the same.
154 // TODO(nisse): Must be handled differently for QUIC.
155 uint32_t transport_frame_id() const { return timestamp(); }
156
157 // Set capture ntp time in milliseconds.
158 void set_ntp_time_ms(int64_t ntp_time_ms) { ntp_time_ms_ = ntp_time_ms; }
159
160 // Get capture ntp time in milliseconds.
161 int64_t ntp_time_ms() const { return ntp_time_ms_; }
162
163 // Naming convention for Coordination of Video Orientation. Please see
164 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
165 //
166 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
167 //
168 // "not pending" = a frame that has a VideoRotation == 0.
169 //
170 // "apply rotation" = modify a frame from being "pending" to being "not
171 // pending" rotation (a no-op for "unrotated").
172 //
173 VideoRotation rotation() const { return rotation_; }
174 void set_rotation(VideoRotation rotation) { rotation_ = rotation; }
175
Johannes Kronfbf16832018-11-05 16:13:02 +0100176 // Get color space when available.
Danil Chapovalovb7698942019-02-05 11:32:19 +0100177 const absl::optional<ColorSpace>& color_space() const { return color_space_; }
178 void set_color_space(const absl::optional<ColorSpace>& color_space) {
179 color_space_ = color_space;
Johannes Kronf7f13e02018-12-12 11:17:43 +0100180 }
Johannes Kronfbf16832018-11-05 16:13:02 +0100181
nisseaf916892017-01-10 07:44:26 -0800182 // Get render time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -0800183 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -0800184 int64_t render_time_ms() const;
185
Ilya Nikolaevskiy871e1442019-02-11 13:35:03 +0100186 // Return the underlying buffer. Never nullptr for a properly
187 // initialized VideoFrame.
nisseaf916892017-01-10 07:44:26 -0800188 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
189
Ilya Nikolaevskiy4fc08552019-06-05 15:59:12 +0200190 void set_video_frame_buffer(
191 const rtc::scoped_refptr<VideoFrameBuffer>& buffer);
192
Markus Handelle6eded32019-11-15 16:18:21 +0100193 void set_encoded_video_frame_buffer(
194 rtc::scoped_refptr<EncodedVideoFrameBuffer> encoded_frame_buffer);
195
196 rtc::scoped_refptr<EncodedVideoFrameBuffer> encoded_video_frame_buffer()
197 const;
198
nisseaf916892017-01-10 07:44:26 -0800199 // TODO(nisse): Deprecated.
200 // Return true if the frame is stored in a texture.
201 bool is_texture() const {
magjed3f075492017-06-01 10:02:26 -0700202 return video_frame_buffer()->type() == VideoFrameBuffer::Type::kNative;
nisseaf916892017-01-10 07:44:26 -0800203 }
204
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100205 bool has_update_rect() const { return update_rect_.has_value(); }
206
207 // Returns update_rect set by the builder or set_update_rect() or whole frame
208 // rect if no update rect is available.
209 UpdateRect update_rect() const {
210 return update_rect_.value_or(UpdateRect{0, 0, width(), height()});
211 }
212
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100213 // Rectangle must be within the frame dimensions.
214 void set_update_rect(const VideoFrame::UpdateRect& update_rect) {
215 RTC_DCHECK_GE(update_rect.offset_x, 0);
216 RTC_DCHECK_GE(update_rect.offset_y, 0);
217 RTC_DCHECK_LE(update_rect.offset_x + update_rect.width, width());
218 RTC_DCHECK_LE(update_rect.offset_y + update_rect.height, height());
219 update_rect_ = update_rect;
220 }
221
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100222 void clear_update_rect() { update_rect_ = absl::nullopt; }
223
Chen Xingf00bf422019-06-20 10:05:55 +0200224 // Get information about packets used to assemble this video frame. Might be
225 // empty if the information isn't available.
226 const RtpPacketInfos& packet_infos() const { return packet_infos_; }
227 void set_packet_infos(RtpPacketInfos value) {
228 packet_infos_ = std::move(value);
229 }
230
nisseaf916892017-01-10 07:44:26 -0800231 private:
Ilya Nikolaevskiy871e1442019-02-11 13:35:03 +0100232 VideoFrame(uint16_t id,
233 const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
234 int64_t timestamp_us,
235 uint32_t timestamp_rtp,
236 int64_t ntp_time_ms,
237 VideoRotation rotation,
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100238 const absl::optional<ColorSpace>& color_space,
Chen Xingf00bf422019-06-20 10:05:55 +0200239 const absl::optional<UpdateRect>& update_rect,
Markus Handelle6eded32019-11-15 16:18:21 +0100240 RtpPacketInfos packet_infos,
241 const rtc::scoped_refptr<EncodedVideoFrameBuffer>& encoded_frame);
Emircan Uysaler800787f2018-07-16 10:01:49 -0700242
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100243 uint16_t id_;
Markus Handelle6eded32019-11-15 16:18:21 +0100244 // A reference counted handle that stores the pixel data.
nisseaf916892017-01-10 07:44:26 -0800245 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
Markus Handelle6eded32019-11-15 16:18:21 +0100246 // A reference counted handle that points to an encoded frame
247 rtc::scoped_refptr<EncodedVideoFrameBuffer> encoded_frame_buffer_;
nisseaf916892017-01-10 07:44:26 -0800248 uint32_t timestamp_rtp_;
249 int64_t ntp_time_ms_;
250 int64_t timestamp_us_;
251 VideoRotation rotation_;
Emircan Uysaler800787f2018-07-16 10:01:49 -0700252 absl::optional<ColorSpace> color_space_;
Ilya Nikolaevskiy9560d7d2019-10-30 11:19:47 +0100253 // Updated since the last frame area. If present it means that the bounding
254 // box of all the changes is within the rectangular area and is close to it.
255 // If absent, it means that there's no information about the change at all and
256 // update_rect() will return a rectangle corresponding to the entire frame.
257 absl::optional<UpdateRect> update_rect_;
Chen Xingf00bf422019-06-20 10:05:55 +0200258 // Information about packets used to assemble this video frame. This is needed
259 // by |SourceTracker| when the frame is delivered to the RTCRtpReceiver's
260 // MediaStreamTrack, in order to implement getContributingSources(). See:
261 // https://w3c.github.io/webrtc-pc/#dom-rtcrtpreceiver-getcontributingsources
262 RtpPacketInfos packet_infos_;
nisseaf916892017-01-10 07:44:26 -0800263};
264
265} // namespace webrtc
266
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200267#endif // API_VIDEO_VIDEO_FRAME_H_