blob: c91aeda1242f02c3c4c0003f1a9b551a25a99e90 [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() {
Artem Titov1ebfb6a2019-01-03 23:49:37 +010023 return VideoFrame(id_, video_frame_buffer_, timestamp_us_, timestamp_rtp_,
Ilya Nikolaevskiy12e5d392019-01-31 13:01:35 +010024 ntp_time_ms_, rotation_, color_space_,
25 partial_frame_description_,
26 cache_buffer_for_partial_updates_);
Emircan Uysaler800787f2018-07-16 10:01:49 -070027}
28
29VideoFrame::Builder& VideoFrame::Builder::set_video_frame_buffer(
30 const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
31 video_frame_buffer_ = buffer;
32 return *this;
33}
34
35VideoFrame::Builder& VideoFrame::Builder::set_timestamp_ms(
36 int64_t timestamp_ms) {
37 timestamp_us_ = timestamp_ms * rtc::kNumMicrosecsPerMillisec;
38 return *this;
39}
40
41VideoFrame::Builder& VideoFrame::Builder::set_timestamp_us(
42 int64_t timestamp_us) {
43 timestamp_us_ = timestamp_us;
44 return *this;
45}
46
47VideoFrame::Builder& VideoFrame::Builder::set_timestamp_rtp(
48 uint32_t timestamp_rtp) {
49 timestamp_rtp_ = timestamp_rtp;
50 return *this;
51}
52
53VideoFrame::Builder& VideoFrame::Builder::set_ntp_time_ms(int64_t ntp_time_ms) {
54 ntp_time_ms_ = ntp_time_ms;
55 return *this;
56}
57
58VideoFrame::Builder& VideoFrame::Builder::set_rotation(VideoRotation rotation) {
59 rotation_ = rotation;
60 return *this;
61}
62
63VideoFrame::Builder& VideoFrame::Builder::set_color_space(
64 const ColorSpace& color_space) {
65 color_space_ = color_space;
66 return *this;
67}
68
Johannes Kron4749e4e2018-11-21 10:18:18 +010069VideoFrame::Builder& VideoFrame::Builder::set_color_space(
70 const ColorSpace* color_space) {
71 color_space_ =
72 color_space ? absl::make_optional(*color_space) : absl::nullopt;
Johannes Kronfbf16832018-11-05 16:13:02 +010073 return *this;
74}
75
Artem Titov1ebfb6a2019-01-03 23:49:37 +010076VideoFrame::Builder& VideoFrame::Builder::set_id(uint16_t id) {
77 id_ = id;
78 return *this;
79}
80
Ilya Nikolaevskiy12e5d392019-01-31 13:01:35 +010081VideoFrame::Builder& VideoFrame::Builder::set_partial_frame_description(
82 const absl::optional<PartialFrameDescription>& description) {
83 partial_frame_description_ = description;
84 return *this;
85}
86
87VideoFrame::Builder& VideoFrame::Builder::set_cache_buffer_for_partial_updates(
88 bool cache_buffer_for_partial_updates) {
89 cache_buffer_for_partial_updates_ = cache_buffer_for_partial_updates;
90 return *this;
91}
92
nisseaf916892017-01-10 07:44:26 -080093VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
94 webrtc::VideoRotation rotation,
95 int64_t timestamp_us)
96 : video_frame_buffer_(buffer),
97 timestamp_rtp_(0),
98 ntp_time_ms_(0),
99 timestamp_us_(timestamp_us),
100 rotation_(rotation) {}
101
102VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 11:14:32 +0200103 uint32_t timestamp_rtp,
nisseaf916892017-01-10 07:44:26 -0800104 int64_t render_time_ms,
105 VideoRotation rotation)
106 : video_frame_buffer_(buffer),
Niels Möller2ac64462018-06-11 11:14:32 +0200107 timestamp_rtp_(timestamp_rtp),
nisseaf916892017-01-10 07:44:26 -0800108 ntp_time_ms_(0),
109 timestamp_us_(render_time_ms * rtc::kNumMicrosecsPerMillisec),
110 rotation_(rotation) {
111 RTC_DCHECK(buffer);
112}
113
Ilya Nikolaevskiy12e5d392019-01-31 13:01:35 +0100114VideoFrame::VideoFrame(
115 uint16_t id,
116 const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
117 int64_t timestamp_us,
118 uint32_t timestamp_rtp,
119 int64_t ntp_time_ms,
120 VideoRotation rotation,
121 const absl::optional<ColorSpace>& color_space,
122 const absl::optional<PartialFrameDescription> partial_frame_description,
123 bool cache_buffer_for_partial_updates)
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100124 : id_(id),
125 video_frame_buffer_(buffer),
Emircan Uysaler800787f2018-07-16 10:01:49 -0700126 timestamp_rtp_(timestamp_rtp),
127 ntp_time_ms_(ntp_time_ms),
128 timestamp_us_(timestamp_us),
129 rotation_(rotation),
Ilya Nikolaevskiy12e5d392019-01-31 13:01:35 +0100130 color_space_(color_space),
131 partial_frame_description_(partial_frame_description),
132 cache_buffer_for_partial_updates_(cache_buffer_for_partial_updates) {}
Emircan Uysaler800787f2018-07-16 10:01:49 -0700133
nisseaf916892017-01-10 07:44:26 -0800134VideoFrame::~VideoFrame() = default;
135
136VideoFrame::VideoFrame(const VideoFrame&) = default;
137VideoFrame::VideoFrame(VideoFrame&&) = default;
138VideoFrame& VideoFrame::operator=(const VideoFrame&) = default;
139VideoFrame& VideoFrame::operator=(VideoFrame&&) = default;
140
141int VideoFrame::width() const {
142 return video_frame_buffer_ ? video_frame_buffer_->width() : 0;
143}
144
145int VideoFrame::height() const {
146 return video_frame_buffer_ ? video_frame_buffer_->height() : 0;
147}
148
kthelgason2bc68642017-02-07 07:02:22 -0800149uint32_t VideoFrame::size() const {
150 return width() * height();
151}
152
nisseaf916892017-01-10 07:44:26 -0800153rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const {
154 return video_frame_buffer_;
155}
156
Ilya Nikolaevskiy12e5d392019-01-31 13:01:35 +0100157void VideoFrame::set_video_frame_buffer(
158 rtc::scoped_refptr<VideoFrameBuffer> buffer) {
Ilya Nikolaevskiy12e5d392019-01-31 13:01:35 +0100159 video_frame_buffer_ = buffer;
160}
161
nisseaf916892017-01-10 07:44:26 -0800162int64_t VideoFrame::render_time_ms() const {
163 return timestamp_us() / rtc::kNumMicrosecsPerMillisec;
164}
165
166} // namespace webrtc