blob: dcb533e29f0c09f2bae8797313ba18d52e716ffc [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"
nisseaf916892017-01-10 07:44:26 -080020
nisseaf916892017-01-10 07:44:26 -080021namespace webrtc {
22
23class VideoFrame {
24 public:
Emircan Uysaler800787f2018-07-16 10:01:49 -070025 // Preferred way of building VideoFrame objects.
26 class Builder {
27 public:
28 Builder();
29 ~Builder();
30
31 VideoFrame build();
32 Builder& set_video_frame_buffer(
33 const rtc::scoped_refptr<VideoFrameBuffer>& buffer);
34 Builder& set_timestamp_ms(int64_t timestamp_ms);
35 Builder& set_timestamp_us(int64_t timestamp_us);
36 Builder& set_timestamp_rtp(uint32_t timestamp_rtp);
37 Builder& set_ntp_time_ms(int64_t ntp_time_ms);
38 Builder& set_rotation(VideoRotation rotation);
39 Builder& set_color_space(const ColorSpace& color_space);
40
41 private:
42 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
43 int64_t timestamp_us_ = 0;
44 uint32_t timestamp_rtp_ = 0;
45 int64_t ntp_time_ms_ = 0;
46 VideoRotation rotation_ = kVideoRotation_0;
47 absl::optional<ColorSpace> color_space_;
48 };
49
50 // To be deprecated. Migrate all use to Builder.
nisseaf916892017-01-10 07:44:26 -080051 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
52 webrtc::VideoRotation rotation,
53 int64_t timestamp_us);
nisseaf916892017-01-10 07:44:26 -080054 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 11:14:32 +020055 uint32_t timestamp_rtp,
nisseaf916892017-01-10 07:44:26 -080056 int64_t render_time_ms,
57 VideoRotation rotation);
58
59 ~VideoFrame();
60
61 // Support move and copy.
62 VideoFrame(const VideoFrame&);
63 VideoFrame(VideoFrame&&);
64 VideoFrame& operator=(const VideoFrame&);
65 VideoFrame& operator=(VideoFrame&&);
66
67 // Get frame width.
68 int width() const;
nisseaf916892017-01-10 07:44:26 -080069 // Get frame height.
70 int height() const;
kthelgason2bc68642017-02-07 07:02:22 -080071 // Get frame size in pixels.
72 uint32_t size() const;
nisseaf916892017-01-10 07:44:26 -080073
74 // System monotonic clock, same timebase as rtc::TimeMicros().
75 int64_t timestamp_us() const { return timestamp_us_; }
76 void set_timestamp_us(int64_t timestamp_us) { timestamp_us_ = timestamp_us; }
77
78 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
79 // merge, timestamps other than timestamp_us will likely be
80 // deprecated.
81
82 // Set frame timestamp (90kHz).
83 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
84
85 // Get frame timestamp (90kHz).
86 uint32_t timestamp() const { return timestamp_rtp_; }
87
88 // For now, transport_frame_id and rtp timestamp are the same.
89 // TODO(nisse): Must be handled differently for QUIC.
90 uint32_t transport_frame_id() const { return timestamp(); }
91
92 // Set capture ntp time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -080093 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -080094 void set_ntp_time_ms(int64_t ntp_time_ms) { ntp_time_ms_ = ntp_time_ms; }
95
96 // Get capture ntp time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -080097 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -080098 int64_t ntp_time_ms() const { return ntp_time_ms_; }
99
100 // Naming convention for Coordination of Video Orientation. Please see
101 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
102 //
103 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
104 //
105 // "not pending" = a frame that has a VideoRotation == 0.
106 //
107 // "apply rotation" = modify a frame from being "pending" to being "not
108 // pending" rotation (a no-op for "unrotated").
109 //
110 VideoRotation rotation() const { return rotation_; }
111 void set_rotation(VideoRotation rotation) { rotation_ = rotation; }
112
Emircan Uysaler800787f2018-07-16 10:01:49 -0700113 // Set Color space when available.
114 absl::optional<ColorSpace> color_space() const { return color_space_; }
115
nisseaf916892017-01-10 07:44:26 -0800116 // Get render time in milliseconds.
nisse1c0dea82017-01-30 02:43:18 -0800117 // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
nisseaf916892017-01-10 07:44:26 -0800118 int64_t render_time_ms() const;
119
120 // Return the underlying buffer. Never nullptr for a properly
121 // initialized VideoFrame.
122 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
123
124 // TODO(nisse): Deprecated.
125 // Return true if the frame is stored in a texture.
126 bool is_texture() const {
magjed3f075492017-06-01 10:02:26 -0700127 return video_frame_buffer()->type() == VideoFrameBuffer::Type::kNative;
nisseaf916892017-01-10 07:44:26 -0800128 }
129
130 private:
Emircan Uysaler800787f2018-07-16 10:01:49 -0700131 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
132 int64_t timestamp_us,
133 uint32_t timestamp_rtp,
134 int64_t ntp_time_ms,
135 VideoRotation rotation,
136 const absl::optional<ColorSpace>& color_space);
137
nisseaf916892017-01-10 07:44:26 -0800138 // An opaque reference counted handle that stores the pixel data.
139 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
140 uint32_t timestamp_rtp_;
141 int64_t ntp_time_ms_;
142 int64_t timestamp_us_;
143 VideoRotation rotation_;
Emircan Uysaler800787f2018-07-16 10:01:49 -0700144 absl::optional<ColorSpace> color_space_;
nisseaf916892017-01-10 07:44:26 -0800145};
146
147} // namespace webrtc
148
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200149#endif // API_VIDEO_VIDEO_FRAME_H_