blob: 83b45b2a316b8b6f385a3e18c8c3542fe63cd0ec [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"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "rtc_base/scoped_ref_ptr.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020021#include "rtc_base/system/rtc_export.h"
nisseaf916892017-01-10 07:44:26 -080022
nisseaf916892017-01-10 07:44:26 -080023namespace webrtc {
24
Mirko Bonadeiac194142018-10-22 17:08:37 +020025class RTC_EXPORT VideoFrame {
nisseaf916892017-01-10 07:44:26 -080026 public:
Emircan Uysaler800787f2018-07-16 10:01:49 -070027 // Preferred way of building VideoFrame objects.
28 class Builder {
29 public:
30 Builder();
31 ~Builder();
32
33 VideoFrame build();
34 Builder& set_video_frame_buffer(
35 const rtc::scoped_refptr<VideoFrameBuffer>& buffer);
36 Builder& set_timestamp_ms(int64_t timestamp_ms);
37 Builder& set_timestamp_us(int64_t timestamp_us);
38 Builder& set_timestamp_rtp(uint32_t timestamp_rtp);
39 Builder& set_ntp_time_ms(int64_t ntp_time_ms);
40 Builder& set_rotation(VideoRotation rotation);
41 Builder& set_color_space(const ColorSpace& color_space);
42
43 private:
44 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
45 int64_t timestamp_us_ = 0;
46 uint32_t timestamp_rtp_ = 0;
47 int64_t ntp_time_ms_ = 0;
48 VideoRotation rotation_ = kVideoRotation_0;
49 absl::optional<ColorSpace> color_space_;
50 };
51
52 // To be deprecated. Migrate all use to Builder.
nisseaf916892017-01-10 07:44:26 -080053 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
54 webrtc::VideoRotation rotation,
55 int64_t timestamp_us);
nisseaf916892017-01-10 07:44:26 -080056 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 11:14:32 +020057 uint32_t timestamp_rtp,
nisseaf916892017-01-10 07:44:26 -080058 int64_t render_time_ms,
59 VideoRotation rotation);
60
61 ~VideoFrame();
62
63 // Support move and copy.
64 VideoFrame(const VideoFrame&);
65 VideoFrame(VideoFrame&&);
66 VideoFrame& operator=(const VideoFrame&);
67 VideoFrame& operator=(VideoFrame&&);
68
69 // Get frame width.
70 int width() const;
nisseaf916892017-01-10 07:44:26 -080071 // Get frame height.
72 int height() const;
kthelgason2bc68642017-02-07 07:02:22 -080073 // Get frame size in pixels.
74 uint32_t size() const;
nisseaf916892017-01-10 07:44:26 -080075
76 // System monotonic clock, same timebase as rtc::TimeMicros().
77 int64_t timestamp_us() const { return timestamp_us_; }
78 void set_timestamp_us(int64_t timestamp_us) { timestamp_us_ = timestamp_us; }
79
80 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
81 // merge, timestamps other than timestamp_us will likely be
82 // deprecated.
83
84 // Set frame timestamp (90kHz).
85 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
86
87 // Get frame timestamp (90kHz).
88 uint32_t timestamp() const { return timestamp_rtp_; }
89
90 // For now, transport_frame_id and rtp timestamp are the same.
91 // TODO(nisse): Must be handled differently for QUIC.
92 uint32_t transport_frame_id() const { return timestamp(); }
93
94 // Set capture ntp time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -080095 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -080096 void set_ntp_time_ms(int64_t ntp_time_ms) { ntp_time_ms_ = ntp_time_ms; }
97
98 // Get capture ntp time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -080099 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -0800100 int64_t ntp_time_ms() const { return ntp_time_ms_; }
101
102 // Naming convention for Coordination of Video Orientation. Please see
103 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
104 //
105 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
106 //
107 // "not pending" = a frame that has a VideoRotation == 0.
108 //
109 // "apply rotation" = modify a frame from being "pending" to being "not
110 // pending" rotation (a no-op for "unrotated").
111 //
112 VideoRotation rotation() const { return rotation_; }
113 void set_rotation(VideoRotation rotation) { rotation_ = rotation; }
114
Emircan Uysaler800787f2018-07-16 10:01:49 -0700115 // Set Color space when available.
116 absl::optional<ColorSpace> color_space() const { return color_space_; }
117
nisseaf916892017-01-10 07:44:26 -0800118 // Get render time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -0800119 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -0800120 int64_t render_time_ms() const;
121
122 // Return the underlying buffer. Never nullptr for a properly
123 // initialized VideoFrame.
124 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
125
126 // TODO(nisse): Deprecated.
127 // Return true if the frame is stored in a texture.
128 bool is_texture() const {
magjed3f075492017-06-01 10:02:26 -0700129 return video_frame_buffer()->type() == VideoFrameBuffer::Type::kNative;
nisseaf916892017-01-10 07:44:26 -0800130 }
131
132 private:
Emircan Uysaler800787f2018-07-16 10:01:49 -0700133 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
134 int64_t timestamp_us,
135 uint32_t timestamp_rtp,
136 int64_t ntp_time_ms,
137 VideoRotation rotation,
138 const absl::optional<ColorSpace>& color_space);
139
nisseaf916892017-01-10 07:44:26 -0800140 // An opaque reference counted handle that stores the pixel data.
141 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
142 uint32_t timestamp_rtp_;
143 int64_t ntp_time_ms_;
144 int64_t timestamp_us_;
145 VideoRotation rotation_;
Emircan Uysaler800787f2018-07-16 10:01:49 -0700146 absl::optional<ColorSpace> color_space_;
nisseaf916892017-01-10 07:44:26 -0800147};
148
149} // namespace webrtc
150
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200151#endif // API_VIDEO_VIDEO_FRAME_H_