blob: b62ef53330dbcc0a43ad1981d45b8fc715586f02 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/video/video_frame_buffer.h"
Yves Gerey665174f2018-06-19 15:03:05 +020019#include "api/video/video_rotation.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020020#include "rtc_base/system/rtc_export.h"
nisseaf916892017-01-10 07:44:26 -080021
nisseaf916892017-01-10 07:44:26 -080022namespace webrtc {
23
Mirko Bonadeiac194142018-10-22 17:08:37 +020024class RTC_EXPORT VideoFrame {
nisseaf916892017-01-10 07:44:26 -080025 public:
Emircan Uysaler800787f2018-07-16 10:01:49 -070026 // Preferred way of building VideoFrame objects.
27 class Builder {
28 public:
29 Builder();
30 ~Builder();
31
32 VideoFrame build();
33 Builder& set_video_frame_buffer(
34 const rtc::scoped_refptr<VideoFrameBuffer>& buffer);
35 Builder& set_timestamp_ms(int64_t timestamp_ms);
36 Builder& set_timestamp_us(int64_t timestamp_us);
37 Builder& set_timestamp_rtp(uint32_t timestamp_rtp);
38 Builder& set_ntp_time_ms(int64_t ntp_time_ms);
39 Builder& set_rotation(VideoRotation rotation);
40 Builder& set_color_space(const ColorSpace& color_space);
41
42 private:
43 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
44 int64_t timestamp_us_ = 0;
45 uint32_t timestamp_rtp_ = 0;
46 int64_t ntp_time_ms_ = 0;
47 VideoRotation rotation_ = kVideoRotation_0;
48 absl::optional<ColorSpace> color_space_;
49 };
50
51 // To be deprecated. Migrate all use to Builder.
nisseaf916892017-01-10 07:44:26 -080052 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
53 webrtc::VideoRotation rotation,
54 int64_t timestamp_us);
nisseaf916892017-01-10 07:44:26 -080055 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 11:14:32 +020056 uint32_t timestamp_rtp,
nisseaf916892017-01-10 07:44:26 -080057 int64_t render_time_ms,
58 VideoRotation rotation);
59
60 ~VideoFrame();
61
62 // Support move and copy.
63 VideoFrame(const VideoFrame&);
64 VideoFrame(VideoFrame&&);
65 VideoFrame& operator=(const VideoFrame&);
66 VideoFrame& operator=(VideoFrame&&);
67
68 // Get frame width.
69 int width() const;
nisseaf916892017-01-10 07:44:26 -080070 // Get frame height.
71 int height() const;
kthelgason2bc68642017-02-07 07:02:22 -080072 // Get frame size in pixels.
73 uint32_t size() const;
nisseaf916892017-01-10 07:44:26 -080074
75 // System monotonic clock, same timebase as rtc::TimeMicros().
76 int64_t timestamp_us() const { return timestamp_us_; }
77 void set_timestamp_us(int64_t timestamp_us) { timestamp_us_ = timestamp_us; }
78
79 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
80 // merge, timestamps other than timestamp_us will likely be
81 // deprecated.
82
83 // Set frame timestamp (90kHz).
84 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
85
86 // Get frame timestamp (90kHz).
87 uint32_t timestamp() const { return timestamp_rtp_; }
88
89 // For now, transport_frame_id and rtp timestamp are the same.
90 // TODO(nisse): Must be handled differently for QUIC.
91 uint32_t transport_frame_id() const { return timestamp(); }
92
93 // Set capture ntp time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -080094 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -080095 void set_ntp_time_ms(int64_t ntp_time_ms) { ntp_time_ms_ = ntp_time_ms; }
96
97 // Get 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 int64_t ntp_time_ms() const { return ntp_time_ms_; }
100
101 // Naming convention for Coordination of Video Orientation. Please see
102 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
103 //
104 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
105 //
106 // "not pending" = a frame that has a VideoRotation == 0.
107 //
108 // "apply rotation" = modify a frame from being "pending" to being "not
109 // pending" rotation (a no-op for "unrotated").
110 //
111 VideoRotation rotation() const { return rotation_; }
112 void set_rotation(VideoRotation rotation) { rotation_ = rotation; }
113
Emircan Uysaler800787f2018-07-16 10:01:49 -0700114 // Set Color space when available.
115 absl::optional<ColorSpace> color_space() const { return color_space_; }
116
nisseaf916892017-01-10 07:44:26 -0800117 // Get render 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 int64_t render_time_ms() const;
120
121 // Return the underlying buffer. Never nullptr for a properly
122 // initialized VideoFrame.
123 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
124
125 // TODO(nisse): Deprecated.
126 // Return true if the frame is stored in a texture.
127 bool is_texture() const {
magjed3f075492017-06-01 10:02:26 -0700128 return video_frame_buffer()->type() == VideoFrameBuffer::Type::kNative;
nisseaf916892017-01-10 07:44:26 -0800129 }
130
131 private:
Emircan Uysaler800787f2018-07-16 10:01:49 -0700132 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
133 int64_t timestamp_us,
134 uint32_t timestamp_rtp,
135 int64_t ntp_time_ms,
136 VideoRotation rotation,
137 const absl::optional<ColorSpace>& color_space);
138
nisseaf916892017-01-10 07:44:26 -0800139 // An opaque reference counted handle that stores the pixel data.
140 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
141 uint32_t timestamp_rtp_;
142 int64_t ntp_time_ms_;
143 int64_t timestamp_us_;
144 VideoRotation rotation_;
Emircan Uysaler800787f2018-07-16 10:01:49 -0700145 absl::optional<ColorSpace> color_space_;
nisseaf916892017-01-10 07:44:26 -0800146};
147
148} // namespace webrtc
149
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200150#endif // API_VIDEO_VIDEO_FRAME_H_