blob: 126c3d05de07d9927dd5b404622a4a7dbc41ffbd [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>
15
Emircan Uysaler800787f2018-07-16 10:01:49 -070016#include "absl/types/optional.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010017#include "api/scoped_refptr.h"
Emircan Uysaler800787f2018-07-16 10:01:49 -070018#include "api/video/color_space.h"
Johannes Kronfbf16832018-11-05 16:13:02 +010019#include "api/video/hdr_metadata.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/video/video_frame_buffer.h"
Yves Gerey665174f2018-06-19 15:03:05 +020021#include "api/video/video_rotation.h"
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010022#include "rtc_base/checks.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020023#include "rtc_base/system/rtc_export.h"
nisseaf916892017-01-10 07:44:26 -080024
nisseaf916892017-01-10 07:44:26 -080025namespace webrtc {
26
Mirko Bonadeiac194142018-10-22 17:08:37 +020027class RTC_EXPORT VideoFrame {
nisseaf916892017-01-10 07:44:26 -080028 public:
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010029 struct UpdateRect {
30 int offset_x;
31 int offset_y;
32 int width;
33 int height;
34 };
35
Emircan Uysaler800787f2018-07-16 10:01:49 -070036 // 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 Chapovalovb7698942019-02-05 11:32:19 +010050 Builder& set_color_space(const absl::optional<ColorSpace>& color_space);
Johannes Kron4749e4e2018-11-21 10:18:18 +010051 Builder& set_color_space(const ColorSpace* color_space);
Artem Titov1ebfb6a2019-01-03 23:49:37 +010052 Builder& set_id(uint16_t id);
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010053 Builder& set_update_rect(const UpdateRect& update_rect);
Emircan Uysaler800787f2018-07-16 10:01:49 -070054
55 private:
Artem Titov1ebfb6a2019-01-03 23:49:37 +010056 uint16_t id_ = 0;
Emircan Uysaler800787f2018-07-16 10:01:49 -070057 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
58 int64_t timestamp_us_ = 0;
59 uint32_t timestamp_rtp_ = 0;
60 int64_t ntp_time_ms_ = 0;
61 VideoRotation rotation_ = kVideoRotation_0;
62 absl::optional<ColorSpace> color_space_;
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010063 absl::optional<UpdateRect> update_rect_;
Emircan Uysaler800787f2018-07-16 10:01:49 -070064 };
65
66 // To be deprecated. Migrate all use to Builder.
nisseaf916892017-01-10 07:44:26 -080067 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
68 webrtc::VideoRotation rotation,
69 int64_t timestamp_us);
nisseaf916892017-01-10 07:44:26 -080070 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 11:14:32 +020071 uint32_t timestamp_rtp,
nisseaf916892017-01-10 07:44:26 -080072 int64_t render_time_ms,
73 VideoRotation rotation);
74
75 ~VideoFrame();
76
77 // Support move and copy.
78 VideoFrame(const VideoFrame&);
79 VideoFrame(VideoFrame&&);
80 VideoFrame& operator=(const VideoFrame&);
81 VideoFrame& operator=(VideoFrame&&);
82
83 // Get frame width.
84 int width() const;
nisseaf916892017-01-10 07:44:26 -080085 // Get frame height.
86 int height() const;
kthelgason2bc68642017-02-07 07:02:22 -080087 // Get frame size in pixels.
88 uint32_t size() const;
nisseaf916892017-01-10 07:44:26 -080089
Artem Titov1ebfb6a2019-01-03 23:49:37 +010090 // Get frame ID. Returns 0 if ID is not set. Not guarantee to be transferred
91 // from the sender to the receiver, but preserved on single side. The id
92 // should be propagated between all frame modifications during its lifetime
93 // from capturing to sending as encoded image. It is intended to be unique
94 // over a time window of a few minutes for peer connection, to which
95 // corresponding video stream belongs to.
96 uint16_t id() const { return id_; }
97 void set_id(uint16_t id) { id_ = id; }
98
nisseaf916892017-01-10 07:44:26 -080099 // System monotonic clock, same timebase as rtc::TimeMicros().
100 int64_t timestamp_us() const { return timestamp_us_; }
101 void set_timestamp_us(int64_t timestamp_us) { timestamp_us_ = timestamp_us; }
102
103 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
104 // merge, timestamps other than timestamp_us will likely be
105 // deprecated.
106
107 // Set frame timestamp (90kHz).
108 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
109
110 // Get frame timestamp (90kHz).
111 uint32_t timestamp() const { return timestamp_rtp_; }
112
113 // For now, transport_frame_id and rtp timestamp are the same.
114 // TODO(nisse): Must be handled differently for QUIC.
115 uint32_t transport_frame_id() const { return timestamp(); }
116
117 // Set capture ntp time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -0800118 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -0800119 void set_ntp_time_ms(int64_t ntp_time_ms) { ntp_time_ms_ = ntp_time_ms; }
120
121 // Get capture ntp time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -0800122 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -0800123 int64_t ntp_time_ms() const { return ntp_time_ms_; }
124
125 // Naming convention for Coordination of Video Orientation. Please see
126 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
127 //
128 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
129 //
130 // "not pending" = a frame that has a VideoRotation == 0.
131 //
132 // "apply rotation" = modify a frame from being "pending" to being "not
133 // pending" rotation (a no-op for "unrotated").
134 //
135 VideoRotation rotation() const { return rotation_; }
136 void set_rotation(VideoRotation rotation) { rotation_ = rotation; }
137
Johannes Kronfbf16832018-11-05 16:13:02 +0100138 // Get color space when available.
Danil Chapovalovb7698942019-02-05 11:32:19 +0100139 const absl::optional<ColorSpace>& color_space() const { return color_space_; }
140 void set_color_space(const absl::optional<ColorSpace>& color_space) {
141 color_space_ = color_space;
Johannes Kronf7f13e02018-12-12 11:17:43 +0100142 }
Johannes Kronfbf16832018-11-05 16:13:02 +0100143
nisseaf916892017-01-10 07:44:26 -0800144 // Get render time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -0800145 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -0800146 int64_t render_time_ms() const;
147
Ilya Nikolaevskiy871e1442019-02-11 13:35:03 +0100148 // Return the underlying buffer. Never nullptr for a properly
149 // initialized VideoFrame.
nisseaf916892017-01-10 07:44:26 -0800150 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
151
152 // TODO(nisse): Deprecated.
153 // Return true if the frame is stored in a texture.
154 bool is_texture() const {
magjed3f075492017-06-01 10:02:26 -0700155 return video_frame_buffer()->type() == VideoFrameBuffer::Type::kNative;
nisseaf916892017-01-10 07:44:26 -0800156 }
157
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100158 // Always initialized to whole frame update, can be set by Builder or manually
159 // by |set_update_rect|.
160 UpdateRect update_rect() const { return update_rect_; }
161 // Rectangle must be within the frame dimensions.
162 void set_update_rect(const VideoFrame::UpdateRect& update_rect) {
163 RTC_DCHECK_GE(update_rect.offset_x, 0);
164 RTC_DCHECK_GE(update_rect.offset_y, 0);
165 RTC_DCHECK_LE(update_rect.offset_x + update_rect.width, width());
166 RTC_DCHECK_LE(update_rect.offset_y + update_rect.height, height());
167 update_rect_ = update_rect;
168 }
169
nisseaf916892017-01-10 07:44:26 -0800170 private:
Ilya Nikolaevskiy871e1442019-02-11 13:35:03 +0100171 VideoFrame(uint16_t id,
172 const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
173 int64_t timestamp_us,
174 uint32_t timestamp_rtp,
175 int64_t ntp_time_ms,
176 VideoRotation rotation,
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100177 const absl::optional<ColorSpace>& color_space,
178 const absl::optional<UpdateRect>& update_rect);
Emircan Uysaler800787f2018-07-16 10:01:49 -0700179
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100180 uint16_t id_;
nisseaf916892017-01-10 07:44:26 -0800181 // An opaque reference counted handle that stores the pixel data.
182 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
183 uint32_t timestamp_rtp_;
184 int64_t ntp_time_ms_;
185 int64_t timestamp_us_;
186 VideoRotation rotation_;
Emircan Uysaler800787f2018-07-16 10:01:49 -0700187 absl::optional<ColorSpace> color_space_;
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100188 // Updated since the last frame area. Unless set explicitly, will always be
189 // a full frame rectangle.
190 UpdateRect update_rect_;
nisseaf916892017-01-10 07:44:26 -0800191};
192
193} // namespace webrtc
194
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200195#endif // API_VIDEO_VIDEO_FRAME_H_