blob: 75a30b28a970ea122d96da4f0955b8a9810121d3 [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_,
Johannes Kron4749e4e2018-11-21 10:18:18 +010024 ntp_time_ms_, rotation_, color_space_);
Emircan Uysaler800787f2018-07-16 10:01:49 -070025}
26
27VideoFrame::Builder& VideoFrame::Builder::set_video_frame_buffer(
28 const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
29 video_frame_buffer_ = buffer;
30 return *this;
31}
32
33VideoFrame::Builder& VideoFrame::Builder::set_timestamp_ms(
34 int64_t timestamp_ms) {
35 timestamp_us_ = timestamp_ms * rtc::kNumMicrosecsPerMillisec;
36 return *this;
37}
38
39VideoFrame::Builder& VideoFrame::Builder::set_timestamp_us(
40 int64_t timestamp_us) {
41 timestamp_us_ = timestamp_us;
42 return *this;
43}
44
45VideoFrame::Builder& VideoFrame::Builder::set_timestamp_rtp(
46 uint32_t timestamp_rtp) {
47 timestamp_rtp_ = timestamp_rtp;
48 return *this;
49}
50
51VideoFrame::Builder& VideoFrame::Builder::set_ntp_time_ms(int64_t ntp_time_ms) {
52 ntp_time_ms_ = ntp_time_ms;
53 return *this;
54}
55
56VideoFrame::Builder& VideoFrame::Builder::set_rotation(VideoRotation rotation) {
57 rotation_ = rotation;
58 return *this;
59}
60
61VideoFrame::Builder& VideoFrame::Builder::set_color_space(
62 const ColorSpace& color_space) {
63 color_space_ = color_space;
64 return *this;
65}
66
Johannes Kron4749e4e2018-11-21 10:18:18 +010067VideoFrame::Builder& VideoFrame::Builder::set_color_space(
68 const ColorSpace* color_space) {
69 color_space_ =
70 color_space ? absl::make_optional(*color_space) : absl::nullopt;
Johannes Kronfbf16832018-11-05 16:13:02 +010071 return *this;
72}
73
Artem Titov1ebfb6a2019-01-03 23:49:37 +010074VideoFrame::Builder& VideoFrame::Builder::set_id(uint16_t id) {
75 id_ = id;
76 return *this;
77}
78
nisseaf916892017-01-10 07:44:26 -080079VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
80 webrtc::VideoRotation rotation,
81 int64_t timestamp_us)
82 : video_frame_buffer_(buffer),
83 timestamp_rtp_(0),
84 ntp_time_ms_(0),
85 timestamp_us_(timestamp_us),
86 rotation_(rotation) {}
87
88VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Niels Möller2ac64462018-06-11 11:14:32 +020089 uint32_t timestamp_rtp,
nisseaf916892017-01-10 07:44:26 -080090 int64_t render_time_ms,
91 VideoRotation rotation)
92 : video_frame_buffer_(buffer),
Niels Möller2ac64462018-06-11 11:14:32 +020093 timestamp_rtp_(timestamp_rtp),
nisseaf916892017-01-10 07:44:26 -080094 ntp_time_ms_(0),
95 timestamp_us_(render_time_ms * rtc::kNumMicrosecsPerMillisec),
96 rotation_(rotation) {
97 RTC_DCHECK(buffer);
98}
99
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100100VideoFrame::VideoFrame(uint16_t id,
101 const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Emircan Uysaler800787f2018-07-16 10:01:49 -0700102 int64_t timestamp_us,
103 uint32_t timestamp_rtp,
104 int64_t ntp_time_ms,
105 VideoRotation rotation,
Johannes Kron4749e4e2018-11-21 10:18:18 +0100106 const absl::optional<ColorSpace>& color_space)
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100107 : id_(id),
108 video_frame_buffer_(buffer),
Emircan Uysaler800787f2018-07-16 10:01:49 -0700109 timestamp_rtp_(timestamp_rtp),
110 ntp_time_ms_(ntp_time_ms),
111 timestamp_us_(timestamp_us),
112 rotation_(rotation),
Johannes Kron4749e4e2018-11-21 10:18:18 +0100113 color_space_(color_space) {}
Emircan Uysaler800787f2018-07-16 10:01:49 -0700114
nisseaf916892017-01-10 07:44:26 -0800115VideoFrame::~VideoFrame() = default;
116
117VideoFrame::VideoFrame(const VideoFrame&) = default;
118VideoFrame::VideoFrame(VideoFrame&&) = default;
119VideoFrame& VideoFrame::operator=(const VideoFrame&) = default;
120VideoFrame& VideoFrame::operator=(VideoFrame&&) = default;
121
122int VideoFrame::width() const {
123 return video_frame_buffer_ ? video_frame_buffer_->width() : 0;
124}
125
126int VideoFrame::height() const {
127 return video_frame_buffer_ ? video_frame_buffer_->height() : 0;
128}
129
kthelgason2bc68642017-02-07 07:02:22 -0800130uint32_t VideoFrame::size() const {
131 return width() * height();
132}
133
nisseaf916892017-01-10 07:44:26 -0800134rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const {
135 return video_frame_buffer_;
136}
137
nisseaf916892017-01-10 07:44:26 -0800138int64_t VideoFrame::render_time_ms() const {
139 return timestamp_us() / rtc::kNumMicrosecsPerMillisec;
140}
141
142} // namespace webrtc