blob: 58362b0bd82dc298c6a6ccd0af291ead29dfb241 [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"
17#include "api/video/color_space.h"
Johannes Kronfbf16832018-11-05 16:13:02 +010018#include "api/video/hdr_metadata.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/video/video_frame_buffer.h"
Yves Gerey665174f2018-06-19 15:03:05 +020020#include "api/video/video_rotation.h"
Yves Gerey988cc082018-10-23 12:03:01 +020021#include "rtc_base/scoped_ref_ptr.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 Kronfbf16832018-11-05 16:13:02 +010043 Builder& set_hdr_metadata(const HdrMetadata& hdr_metadata);
Emircan Uysaler800787f2018-07-16 10:01:49 -070044
45 private:
46 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
47 int64_t timestamp_us_ = 0;
48 uint32_t timestamp_rtp_ = 0;
49 int64_t ntp_time_ms_ = 0;
50 VideoRotation rotation_ = kVideoRotation_0;
51 absl::optional<ColorSpace> color_space_;
Johannes Kronfbf16832018-11-05 16:13:02 +010052 absl::optional<HdrMetadata> hdr_metadata_;
Emircan Uysaler800787f2018-07-16 10:01:49 -070053 };
54
55 // To be deprecated. Migrate all use to Builder.
nisseaf916892017-01-10 07:44:26 -080056 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
57 webrtc::VideoRotation rotation,
58 int64_t timestamp_us);
nisseaf916892017-01-10 07:44:26 -080059 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 11:14:32 +020060 uint32_t timestamp_rtp,
nisseaf916892017-01-10 07:44:26 -080061 int64_t render_time_ms,
62 VideoRotation rotation);
63
64 ~VideoFrame();
65
66 // Support move and copy.
67 VideoFrame(const VideoFrame&);
68 VideoFrame(VideoFrame&&);
69 VideoFrame& operator=(const VideoFrame&);
70 VideoFrame& operator=(VideoFrame&&);
71
72 // Get frame width.
73 int width() const;
nisseaf916892017-01-10 07:44:26 -080074 // Get frame height.
75 int height() const;
kthelgason2bc68642017-02-07 07:02:22 -080076 // Get frame size in pixels.
77 uint32_t size() const;
nisseaf916892017-01-10 07:44:26 -080078
79 // System monotonic clock, same timebase as rtc::TimeMicros().
80 int64_t timestamp_us() const { return timestamp_us_; }
81 void set_timestamp_us(int64_t timestamp_us) { timestamp_us_ = timestamp_us; }
82
83 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
84 // merge, timestamps other than timestamp_us will likely be
85 // deprecated.
86
87 // Set frame timestamp (90kHz).
88 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
89
90 // Get frame timestamp (90kHz).
91 uint32_t timestamp() const { return timestamp_rtp_; }
92
93 // For now, transport_frame_id and rtp timestamp are the same.
94 // TODO(nisse): Must be handled differently for QUIC.
95 uint32_t transport_frame_id() const { return timestamp(); }
96
97 // Set capture ntp time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -080098 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -080099 void set_ntp_time_ms(int64_t ntp_time_ms) { ntp_time_ms_ = ntp_time_ms; }
100
101 // Get capture ntp time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -0800102 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -0800103 int64_t ntp_time_ms() const { return ntp_time_ms_; }
104
105 // Naming convention for Coordination of Video Orientation. Please see
106 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
107 //
108 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
109 //
110 // "not pending" = a frame that has a VideoRotation == 0.
111 //
112 // "apply rotation" = modify a frame from being "pending" to being "not
113 // pending" rotation (a no-op for "unrotated").
114 //
115 VideoRotation rotation() const { return rotation_; }
116 void set_rotation(VideoRotation rotation) { rotation_ = rotation; }
117
Johannes Kronfbf16832018-11-05 16:13:02 +0100118 // Get color space when available.
Emircan Uysaler800787f2018-07-16 10:01:49 -0700119 absl::optional<ColorSpace> color_space() const { return color_space_; }
120
Johannes Kronfbf16832018-11-05 16:13:02 +0100121 // Get HDR metadata when available.
122 absl::optional<HdrMetadata> hdr_metadata() const { return hdr_metadata_; }
123
nisseaf916892017-01-10 07:44:26 -0800124 // Get render time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -0800125 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -0800126 int64_t render_time_ms() const;
127
128 // Return the underlying buffer. Never nullptr for a properly
129 // initialized VideoFrame.
130 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
131
132 // TODO(nisse): Deprecated.
133 // Return true if the frame is stored in a texture.
134 bool is_texture() const {
magjed3f075492017-06-01 10:02:26 -0700135 return video_frame_buffer()->type() == VideoFrameBuffer::Type::kNative;
nisseaf916892017-01-10 07:44:26 -0800136 }
137
138 private:
Emircan Uysaler800787f2018-07-16 10:01:49 -0700139 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
140 int64_t timestamp_us,
141 uint32_t timestamp_rtp,
142 int64_t ntp_time_ms,
143 VideoRotation rotation,
Johannes Kronfbf16832018-11-05 16:13:02 +0100144 const absl::optional<ColorSpace>& color_space,
145 const absl::optional<HdrMetadata>& hdr_metadata);
Emircan Uysaler800787f2018-07-16 10:01:49 -0700146
nisseaf916892017-01-10 07:44:26 -0800147 // An opaque reference counted handle that stores the pixel data.
148 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
149 uint32_t timestamp_rtp_;
150 int64_t ntp_time_ms_;
151 int64_t timestamp_us_;
152 VideoRotation rotation_;
Emircan Uysaler800787f2018-07-16 10:01:49 -0700153 absl::optional<ColorSpace> color_space_;
Johannes Kronfbf16832018-11-05 16:13:02 +0100154 absl::optional<HdrMetadata> hdr_metadata_;
nisseaf916892017-01-10 07:44:26 -0800155};
156
157} // namespace webrtc
158
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200159#endif // API_VIDEO_VIDEO_FRAME_H_