You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
6.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  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. #include "api/video/video_frame.h"
  11. #include <algorithm>
  12. #include <utility>
  13. #include "rtc_base/checks.h"
  14. #include "rtc_base/time_utils.h"
  15. namespace webrtc {
  16. void VideoFrame::UpdateRect::Union(const UpdateRect& other) {
  17. if (other.IsEmpty())
  18. return;
  19. if (IsEmpty()) {
  20. *this = other;
  21. return;
  22. }
  23. int right = std::max(offset_x + width, other.offset_x + other.width);
  24. int bottom = std::max(offset_y + height, other.offset_y + other.height);
  25. offset_x = std::min(offset_x, other.offset_x);
  26. offset_y = std::min(offset_y, other.offset_y);
  27. width = right - offset_x;
  28. height = bottom - offset_y;
  29. RTC_DCHECK_GT(width, 0);
  30. RTC_DCHECK_GT(height, 0);
  31. }
  32. void VideoFrame::UpdateRect::Intersect(const UpdateRect& other) {
  33. if (other.IsEmpty() || IsEmpty()) {
  34. MakeEmptyUpdate();
  35. return;
  36. }
  37. int right = std::min(offset_x + width, other.offset_x + other.width);
  38. int bottom = std::min(offset_y + height, other.offset_y + other.height);
  39. offset_x = std::max(offset_x, other.offset_x);
  40. offset_y = std::max(offset_y, other.offset_y);
  41. width = right - offset_x;
  42. height = bottom - offset_y;
  43. if (width <= 0 || height <= 0) {
  44. MakeEmptyUpdate();
  45. }
  46. }
  47. void VideoFrame::UpdateRect::MakeEmptyUpdate() {
  48. width = height = offset_x = offset_y = 0;
  49. }
  50. bool VideoFrame::UpdateRect::IsEmpty() const {
  51. return width == 0 && height == 0;
  52. }
  53. VideoFrame::Builder::Builder() = default;
  54. VideoFrame::Builder::~Builder() = default;
  55. VideoFrame VideoFrame::Builder::build() {
  56. RTC_CHECK(video_frame_buffer_ != nullptr);
  57. return VideoFrame(id_, video_frame_buffer_, timestamp_us_, timestamp_rtp_,
  58. ntp_time_ms_, rotation_, color_space_, update_rect_,
  59. packet_infos_);
  60. }
  61. VideoFrame::Builder& VideoFrame::Builder::set_video_frame_buffer(
  62. const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
  63. video_frame_buffer_ = buffer;
  64. return *this;
  65. }
  66. VideoFrame::Builder& VideoFrame::Builder::set_timestamp_ms(
  67. int64_t timestamp_ms) {
  68. timestamp_us_ = timestamp_ms * rtc::kNumMicrosecsPerMillisec;
  69. return *this;
  70. }
  71. VideoFrame::Builder& VideoFrame::Builder::set_timestamp_us(
  72. int64_t timestamp_us) {
  73. timestamp_us_ = timestamp_us;
  74. return *this;
  75. }
  76. VideoFrame::Builder& VideoFrame::Builder::set_timestamp_rtp(
  77. uint32_t timestamp_rtp) {
  78. timestamp_rtp_ = timestamp_rtp;
  79. return *this;
  80. }
  81. VideoFrame::Builder& VideoFrame::Builder::set_ntp_time_ms(int64_t ntp_time_ms) {
  82. ntp_time_ms_ = ntp_time_ms;
  83. return *this;
  84. }
  85. VideoFrame::Builder& VideoFrame::Builder::set_rotation(VideoRotation rotation) {
  86. rotation_ = rotation;
  87. return *this;
  88. }
  89. VideoFrame::Builder& VideoFrame::Builder::set_color_space(
  90. const absl::optional<ColorSpace>& color_space) {
  91. color_space_ = color_space;
  92. return *this;
  93. }
  94. VideoFrame::Builder& VideoFrame::Builder::set_color_space(
  95. const ColorSpace* color_space) {
  96. color_space_ =
  97. color_space ? absl::make_optional(*color_space) : absl::nullopt;
  98. return *this;
  99. }
  100. VideoFrame::Builder& VideoFrame::Builder::set_id(uint16_t id) {
  101. id_ = id;
  102. return *this;
  103. }
  104. VideoFrame::Builder& VideoFrame::Builder::set_update_rect(
  105. const VideoFrame::UpdateRect& update_rect) {
  106. update_rect_ = update_rect;
  107. return *this;
  108. }
  109. VideoFrame::Builder& VideoFrame::Builder::set_packet_infos(
  110. RtpPacketInfos packet_infos) {
  111. packet_infos_ = std::move(packet_infos);
  112. return *this;
  113. }
  114. VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
  115. webrtc::VideoRotation rotation,
  116. int64_t timestamp_us)
  117. : video_frame_buffer_(buffer),
  118. timestamp_rtp_(0),
  119. ntp_time_ms_(0),
  120. timestamp_us_(timestamp_us),
  121. rotation_(rotation),
  122. update_rect_{0, 0, buffer->width(), buffer->height()} {}
  123. VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
  124. uint32_t timestamp_rtp,
  125. int64_t render_time_ms,
  126. VideoRotation rotation)
  127. : video_frame_buffer_(buffer),
  128. timestamp_rtp_(timestamp_rtp),
  129. ntp_time_ms_(0),
  130. timestamp_us_(render_time_ms * rtc::kNumMicrosecsPerMillisec),
  131. rotation_(rotation),
  132. update_rect_{0, 0, buffer->width(), buffer->height()} {
  133. RTC_DCHECK(buffer);
  134. }
  135. VideoFrame::VideoFrame(uint16_t id,
  136. const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
  137. int64_t timestamp_us,
  138. uint32_t timestamp_rtp,
  139. int64_t ntp_time_ms,
  140. VideoRotation rotation,
  141. const absl::optional<ColorSpace>& color_space,
  142. const absl::optional<UpdateRect>& update_rect,
  143. RtpPacketInfos packet_infos)
  144. : id_(id),
  145. video_frame_buffer_(buffer),
  146. timestamp_rtp_(timestamp_rtp),
  147. ntp_time_ms_(ntp_time_ms),
  148. timestamp_us_(timestamp_us),
  149. rotation_(rotation),
  150. color_space_(color_space),
  151. update_rect_(update_rect.value_or(UpdateRect{
  152. 0, 0, video_frame_buffer_->width(), video_frame_buffer_->height()})),
  153. packet_infos_(std::move(packet_infos)) {
  154. RTC_DCHECK_GE(update_rect_.offset_x, 0);
  155. RTC_DCHECK_GE(update_rect_.offset_y, 0);
  156. RTC_DCHECK_LE(update_rect_.offset_x + update_rect_.width, width());
  157. RTC_DCHECK_LE(update_rect_.offset_y + update_rect_.height, height());
  158. }
  159. VideoFrame::~VideoFrame() = default;
  160. VideoFrame::VideoFrame(const VideoFrame&) = default;
  161. VideoFrame::VideoFrame(VideoFrame&&) = default;
  162. VideoFrame& VideoFrame::operator=(const VideoFrame&) = default;
  163. VideoFrame& VideoFrame::operator=(VideoFrame&&) = default;
  164. int VideoFrame::width() const {
  165. return video_frame_buffer_ ? video_frame_buffer_->width() : 0;
  166. }
  167. int VideoFrame::height() const {
  168. return video_frame_buffer_ ? video_frame_buffer_->height() : 0;
  169. }
  170. uint32_t VideoFrame::size() const {
  171. return width() * height();
  172. }
  173. rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const {
  174. return video_frame_buffer_;
  175. }
  176. void VideoFrame::set_video_frame_buffer(
  177. const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
  178. RTC_CHECK(buffer);
  179. video_frame_buffer_ = buffer;
  180. }
  181. int64_t VideoFrame::render_time_ms() const {
  182. return timestamp_us() / rtc::kNumMicrosecsPerMillisec;
  183. }
  184. } // namespace webrtc