blob: de3f0efdc5c0d79c7fbf81815987de9f57ab6dcf [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"
Mirko Bonadeiac194142018-10-22 17:08:37 +020022#include "rtc_base/system/rtc_export.h"
nisseaf916892017-01-10 07:44:26 -080023
nisseaf916892017-01-10 07:44:26 -080024namespace webrtc {
25
Mirko Bonadeiac194142018-10-22 17:08:37 +020026class RTC_EXPORT VideoFrame {
nisseaf916892017-01-10 07:44:26 -080027 public:
Emircan Uysaler800787f2018-07-16 10:01:49 -070028 // Preferred way of building VideoFrame objects.
29 class Builder {
30 public:
31 Builder();
32 ~Builder();
33
34 VideoFrame build();
35 Builder& set_video_frame_buffer(
36 const rtc::scoped_refptr<VideoFrameBuffer>& buffer);
37 Builder& set_timestamp_ms(int64_t timestamp_ms);
38 Builder& set_timestamp_us(int64_t timestamp_us);
39 Builder& set_timestamp_rtp(uint32_t timestamp_rtp);
40 Builder& set_ntp_time_ms(int64_t ntp_time_ms);
41 Builder& set_rotation(VideoRotation rotation);
42 Builder& set_color_space(const ColorSpace& color_space);
Johannes Kron4749e4e2018-11-21 10:18:18 +010043 Builder& set_color_space(const ColorSpace* color_space);
Artem Titov1ebfb6a2019-01-03 23:49:37 +010044 Builder& set_id(uint16_t id);
Emircan Uysaler800787f2018-07-16 10:01:49 -070045
46 private:
Artem Titov1ebfb6a2019-01-03 23:49:37 +010047 uint16_t id_ = 0;
Emircan Uysaler800787f2018-07-16 10:01:49 -070048 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
49 int64_t timestamp_us_ = 0;
50 uint32_t timestamp_rtp_ = 0;
51 int64_t ntp_time_ms_ = 0;
52 VideoRotation rotation_ = kVideoRotation_0;
53 absl::optional<ColorSpace> color_space_;
54 };
55
56 // To be deprecated. Migrate all use to Builder.
nisseaf916892017-01-10 07:44:26 -080057 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
58 webrtc::VideoRotation rotation,
59 int64_t timestamp_us);
nisseaf916892017-01-10 07:44:26 -080060 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 11:14:32 +020061 uint32_t timestamp_rtp,
nisseaf916892017-01-10 07:44:26 -080062 int64_t render_time_ms,
63 VideoRotation rotation);
64
65 ~VideoFrame();
66
67 // Support move and copy.
68 VideoFrame(const VideoFrame&);
69 VideoFrame(VideoFrame&&);
70 VideoFrame& operator=(const VideoFrame&);
71 VideoFrame& operator=(VideoFrame&&);
72
73 // Get frame width.
74 int width() const;
nisseaf916892017-01-10 07:44:26 -080075 // Get frame height.
76 int height() const;
kthelgason2bc68642017-02-07 07:02:22 -080077 // Get frame size in pixels.
78 uint32_t size() const;
nisseaf916892017-01-10 07:44:26 -080079
Artem Titov1ebfb6a2019-01-03 23:49:37 +010080 // Get frame ID. Returns 0 if ID is not set. Not guarantee to be transferred
81 // from the sender to the receiver, but preserved on single side. The id
82 // should be propagated between all frame modifications during its lifetime
83 // from capturing to sending as encoded image. It is intended to be unique
84 // over a time window of a few minutes for peer connection, to which
85 // corresponding video stream belongs to.
86 uint16_t id() const { return id_; }
87 void set_id(uint16_t id) { id_ = id; }
88
nisseaf916892017-01-10 07:44:26 -080089 // System monotonic clock, same timebase as rtc::TimeMicros().
90 int64_t timestamp_us() const { return timestamp_us_; }
91 void set_timestamp_us(int64_t timestamp_us) { timestamp_us_ = timestamp_us; }
92
93 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
94 // merge, timestamps other than timestamp_us will likely be
95 // deprecated.
96
97 // Set frame timestamp (90kHz).
98 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
99
100 // Get frame timestamp (90kHz).
101 uint32_t timestamp() const { return timestamp_rtp_; }
102
103 // For now, transport_frame_id and rtp timestamp are the same.
104 // TODO(nisse): Must be handled differently for QUIC.
105 uint32_t transport_frame_id() const { return timestamp(); }
106
107 // Set capture ntp time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -0800108 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -0800109 void set_ntp_time_ms(int64_t ntp_time_ms) { ntp_time_ms_ = ntp_time_ms; }
110
111 // Get capture ntp time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -0800112 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -0800113 int64_t ntp_time_ms() const { return ntp_time_ms_; }
114
115 // Naming convention for Coordination of Video Orientation. Please see
116 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
117 //
118 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
119 //
120 // "not pending" = a frame that has a VideoRotation == 0.
121 //
122 // "apply rotation" = modify a frame from being "pending" to being "not
123 // pending" rotation (a no-op for "unrotated").
124 //
125 VideoRotation rotation() const { return rotation_; }
126 void set_rotation(VideoRotation rotation) { rotation_ = rotation; }
127
Johannes Kronfbf16832018-11-05 16:13:02 +0100128 // Get color space when available.
Johannes Kron4749e4e2018-11-21 10:18:18 +0100129 const ColorSpace* color_space() const {
130 return color_space_ ? &*color_space_ : nullptr;
Johannes Kron9973fa82018-11-07 14:39:26 +0100131 }
Johannes Kronf7f13e02018-12-12 11:17:43 +0100132 void set_color_space(ColorSpace* color_space) {
133 color_space_ =
134 color_space ? absl::make_optional(*color_space) : absl::nullopt;
135 }
Johannes Kronfbf16832018-11-05 16:13:02 +0100136
nisseaf916892017-01-10 07:44:26 -0800137 // Get render time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -0800138 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -0800139 int64_t render_time_ms() const;
140
Ilya Nikolaevskiy8102a1a2019-01-31 11:57:20 +0000141 // Return the underlying buffer. Never nullptr for a properly
142 // initialized VideoFrame.
nisseaf916892017-01-10 07:44:26 -0800143 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
144
145 // TODO(nisse): Deprecated.
146 // Return true if the frame is stored in a texture.
147 bool is_texture() const {
magjed3f075492017-06-01 10:02:26 -0700148 return video_frame_buffer()->type() == VideoFrameBuffer::Type::kNative;
nisseaf916892017-01-10 07:44:26 -0800149 }
150
151 private:
Ilya Nikolaevskiy8102a1a2019-01-31 11:57:20 +0000152 VideoFrame(uint16_t id,
153 const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
154 int64_t timestamp_us,
155 uint32_t timestamp_rtp,
156 int64_t ntp_time_ms,
157 VideoRotation rotation,
158 const absl::optional<ColorSpace>& color_space);
Emircan Uysaler800787f2018-07-16 10:01:49 -0700159
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100160 uint16_t id_;
nisseaf916892017-01-10 07:44:26 -0800161 // An opaque reference counted handle that stores the pixel data.
162 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
163 uint32_t timestamp_rtp_;
164 int64_t ntp_time_ms_;
165 int64_t timestamp_us_;
166 VideoRotation rotation_;
Emircan Uysaler800787f2018-07-16 10:01:49 -0700167 absl::optional<ColorSpace> color_space_;
nisseaf916892017-01-10 07:44:26 -0800168};
169
170} // namespace webrtc
171
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200172#endif // API_VIDEO_VIDEO_FRAME_H_