nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef API_VIDEO_VIDEO_FRAME_H_ |
| 12 | #define API_VIDEO_VIDEO_FRAME_H_ |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 13 | |
| 14 | #include <stdint.h> |
| 15 | |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 16 | #include "absl/types/optional.h" |
| 17 | #include "api/video/color_space.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/video/video_frame_buffer.h" |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 19 | #include "api/video/video_rotation.h" |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 20 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 21 | namespace webrtc { |
| 22 | |
| 23 | class VideoFrame { |
| 24 | public: |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 25 | // 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. |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 51 | VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
| 52 | webrtc::VideoRotation rotation, |
| 53 | int64_t timestamp_us); |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 54 | VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
Niels Möller | 2ac6446 | 2018-06-11 11:14:32 +0200 | [diff] [blame] | 55 | uint32_t timestamp_rtp, |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 56 | 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; |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 69 | // Get frame height. |
| 70 | int height() const; |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 71 | // Get frame size in pixels. |
| 72 | uint32_t size() const; |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 73 | |
| 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. |
nisse | 1c0dea8 | 2017-01-30 02:43:18 -0800 | [diff] [blame] | 93 | // TODO(nisse): Deprecated. Migrate all users to timestamp_us(). |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 94 | void set_ntp_time_ms(int64_t ntp_time_ms) { ntp_time_ms_ = ntp_time_ms; } |
| 95 | |
| 96 | // Get capture ntp time in milliseconds. |
nisse | 1c0dea8 | 2017-01-30 02:43:18 -0800 | [diff] [blame] | 97 | // TODO(nisse): Deprecated. Migrate all users to timestamp_us(). |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 98 | 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 Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 113 | // Set Color space when available. |
| 114 | absl::optional<ColorSpace> color_space() const { return color_space_; } |
| 115 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 116 | // Get render time in milliseconds. |
nisse | 1c0dea8 | 2017-01-30 02:43:18 -0800 | [diff] [blame] | 117 | // TODO(nisse): Deprecated. Migrate all users to timestamp_us(). |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 118 | 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 { |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 127 | return video_frame_buffer()->type() == VideoFrameBuffer::Type::kNative; |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | private: |
Emircan Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 131 | 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 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 138 | // 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 Uysaler | 800787f | 2018-07-16 10:01:49 -0700 | [diff] [blame] | 144 | absl::optional<ColorSpace> color_space_; |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | } // namespace webrtc |
| 148 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 149 | #endif // API_VIDEO_VIDEO_FRAME_H_ |