blob: e91eedede835f00aa882178e784d51dc0f3672dc [file] [log] [blame]
nisseaf916892017-01-10 07:44:26 -08001/*
2 * Copyright (c) 2012 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#include "api/video/video_frame.h"
nisseaf916892017-01-10 07:44:26 -080012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080014#include "rtc_base/time_utils.h"
nisseaf916892017-01-10 07:44:26 -080015
16namespace webrtc {
17
Emircan Uysaler800787f2018-07-16 10:01:49 -070018VideoFrame::Builder::Builder() = default;
19
20VideoFrame::Builder::~Builder() = default;
21
22VideoFrame VideoFrame::Builder::build() {
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010023 RTC_CHECK(video_frame_buffer_ != nullptr);
Artem Titov1ebfb6a2019-01-03 23:49:37 +010024 return VideoFrame(id_, video_frame_buffer_, timestamp_us_, timestamp_rtp_,
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010025 ntp_time_ms_, rotation_, color_space_, update_rect_);
Emircan Uysaler800787f2018-07-16 10:01:49 -070026}
27
28VideoFrame::Builder& VideoFrame::Builder::set_video_frame_buffer(
29 const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
30 video_frame_buffer_ = buffer;
31 return *this;
32}
33
34VideoFrame::Builder& VideoFrame::Builder::set_timestamp_ms(
35 int64_t timestamp_ms) {
36 timestamp_us_ = timestamp_ms * rtc::kNumMicrosecsPerMillisec;
37 return *this;
38}
39
40VideoFrame::Builder& VideoFrame::Builder::set_timestamp_us(
41 int64_t timestamp_us) {
42 timestamp_us_ = timestamp_us;
43 return *this;
44}
45
46VideoFrame::Builder& VideoFrame::Builder::set_timestamp_rtp(
47 uint32_t timestamp_rtp) {
48 timestamp_rtp_ = timestamp_rtp;
49 return *this;
50}
51
52VideoFrame::Builder& VideoFrame::Builder::set_ntp_time_ms(int64_t ntp_time_ms) {
53 ntp_time_ms_ = ntp_time_ms;
54 return *this;
55}
56
57VideoFrame::Builder& VideoFrame::Builder::set_rotation(VideoRotation rotation) {
58 rotation_ = rotation;
59 return *this;
60}
61
62VideoFrame::Builder& VideoFrame::Builder::set_color_space(
Danil Chapovalovb7698942019-02-05 11:32:19 +010063 const absl::optional<ColorSpace>& color_space) {
Emircan Uysaler800787f2018-07-16 10:01:49 -070064 color_space_ = color_space;
65 return *this;
66}
67
Johannes Kron4749e4e2018-11-21 10:18:18 +010068VideoFrame::Builder& VideoFrame::Builder::set_color_space(
69 const ColorSpace* color_space) {
70 color_space_ =
71 color_space ? absl::make_optional(*color_space) : absl::nullopt;
Johannes Kronfbf16832018-11-05 16:13:02 +010072 return *this;
73}
74
Artem Titov1ebfb6a2019-01-03 23:49:37 +010075VideoFrame::Builder& VideoFrame::Builder::set_id(uint16_t id) {
76 id_ = id;
77 return *this;
78}
79
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010080VideoFrame::Builder& VideoFrame::Builder::set_update_rect(
81 const VideoFrame::UpdateRect& update_rect) {
82 update_rect_ = update_rect;
83 return *this;
84}
85
nisseaf916892017-01-10 07:44:26 -080086VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
87 webrtc::VideoRotation rotation,
88 int64_t timestamp_us)
89 : video_frame_buffer_(buffer),
90 timestamp_rtp_(0),
91 ntp_time_ms_(0),
92 timestamp_us_(timestamp_us),
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +010093 rotation_(rotation),
94 update_rect_{0, 0, buffer->width(), buffer->height()} {}
nisseaf916892017-01-10 07:44:26 -080095
96VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 11:14:32 +020097 uint32_t timestamp_rtp,
nisseaf916892017-01-10 07:44:26 -080098 int64_t render_time_ms,
99 VideoRotation rotation)
100 : video_frame_buffer_(buffer),
Niels Möller2ac64462018-06-11 11:14:32 +0200101 timestamp_rtp_(timestamp_rtp),
nisseaf916892017-01-10 07:44:26 -0800102 ntp_time_ms_(0),
103 timestamp_us_(render_time_ms * rtc::kNumMicrosecsPerMillisec),
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100104 rotation_(rotation),
105 update_rect_{0, 0, buffer->width(), buffer->height()} {
nisseaf916892017-01-10 07:44:26 -0800106 RTC_DCHECK(buffer);
107}
108
Ilya Nikolaevskiy871e1442019-02-11 13:35:03 +0100109VideoFrame::VideoFrame(uint16_t id,
110 const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
111 int64_t timestamp_us,
112 uint32_t timestamp_rtp,
113 int64_t ntp_time_ms,
114 VideoRotation rotation,
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100115 const absl::optional<ColorSpace>& color_space,
116 const absl::optional<UpdateRect>& update_rect)
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100117 : id_(id),
118 video_frame_buffer_(buffer),
Emircan Uysaler800787f2018-07-16 10:01:49 -0700119 timestamp_rtp_(timestamp_rtp),
120 ntp_time_ms_(ntp_time_ms),
121 timestamp_us_(timestamp_us),
122 rotation_(rotation),
Ilya Nikolaevskiy6aca0b72019-02-13 11:55:57 +0100123 color_space_(color_space),
124 update_rect_(update_rect.value_or(UpdateRect{
125 0, 0, video_frame_buffer_->width(), video_frame_buffer_->height()})) {
126 RTC_DCHECK_GE(update_rect_.offset_x, 0);
127 RTC_DCHECK_GE(update_rect_.offset_y, 0);
128 RTC_DCHECK_LE(update_rect_.offset_x + update_rect_.width, width());
129 RTC_DCHECK_LE(update_rect_.offset_y + update_rect_.height, height());
130}
Emircan Uysaler800787f2018-07-16 10:01:49 -0700131
nisseaf916892017-01-10 07:44:26 -0800132VideoFrame::~VideoFrame() = default;
133
134VideoFrame::VideoFrame(const VideoFrame&) = default;
135VideoFrame::VideoFrame(VideoFrame&&) = default;
136VideoFrame& VideoFrame::operator=(const VideoFrame&) = default;
137VideoFrame& VideoFrame::operator=(VideoFrame&&) = default;
138
139int VideoFrame::width() const {
140 return video_frame_buffer_ ? video_frame_buffer_->width() : 0;
141}
142
143int VideoFrame::height() const {
144 return video_frame_buffer_ ? video_frame_buffer_->height() : 0;
145}
146
kthelgason2bc68642017-02-07 07:02:22 -0800147uint32_t VideoFrame::size() const {
148 return width() * height();
149}
150
nisseaf916892017-01-10 07:44:26 -0800151rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const {
152 return video_frame_buffer_;
153}
154
nisseaf916892017-01-10 07:44:26 -0800155int64_t VideoFrame::render_time_ms() const {
156 return timestamp_us() / rtc::kNumMicrosecsPerMillisec;
157}
158
159} // namespace webrtc