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.

225 lines
8.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef API_VIDEO_VIDEO_FRAME_H_
  11. #define API_VIDEO_VIDEO_FRAME_H_
  12. #include <stdint.h>
  13. #include <utility>
  14. #include "absl/types/optional.h"
  15. #include "api/rtp_packet_infos.h"
  16. #include "api/scoped_refptr.h"
  17. #include "api/video/color_space.h"
  18. #include "api/video/hdr_metadata.h"
  19. #include "api/video/video_frame_buffer.h"
  20. #include "api/video/video_rotation.h"
  21. #include "rtc_base/checks.h"
  22. #include "rtc_base/system/rtc_export.h"
  23. namespace webrtc {
  24. class RTC_EXPORT VideoFrame {
  25. public:
  26. struct UpdateRect {
  27. int offset_x;
  28. int offset_y;
  29. int width;
  30. int height;
  31. // Makes this UpdateRect a bounding box of this and other rect.
  32. void Union(const UpdateRect& other);
  33. // Makes this UpdateRect an intersection of this and other rect.
  34. void Intersect(const UpdateRect& other);
  35. // Sets everything to 0, making this UpdateRect a zero-size (empty) update.
  36. void MakeEmptyUpdate();
  37. bool IsEmpty() const;
  38. };
  39. // Preferred way of building VideoFrame objects.
  40. class Builder {
  41. public:
  42. Builder();
  43. ~Builder();
  44. VideoFrame build();
  45. Builder& set_video_frame_buffer(
  46. const rtc::scoped_refptr<VideoFrameBuffer>& buffer);
  47. Builder& set_timestamp_ms(int64_t timestamp_ms);
  48. Builder& set_timestamp_us(int64_t timestamp_us);
  49. Builder& set_timestamp_rtp(uint32_t timestamp_rtp);
  50. Builder& set_ntp_time_ms(int64_t ntp_time_ms);
  51. Builder& set_rotation(VideoRotation rotation);
  52. Builder& set_color_space(const absl::optional<ColorSpace>& color_space);
  53. Builder& set_color_space(const ColorSpace* color_space);
  54. Builder& set_id(uint16_t id);
  55. Builder& set_update_rect(const UpdateRect& update_rect);
  56. Builder& set_packet_infos(RtpPacketInfos packet_infos);
  57. private:
  58. uint16_t id_ = 0;
  59. rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
  60. int64_t timestamp_us_ = 0;
  61. uint32_t timestamp_rtp_ = 0;
  62. int64_t ntp_time_ms_ = 0;
  63. VideoRotation rotation_ = kVideoRotation_0;
  64. absl::optional<ColorSpace> color_space_;
  65. absl::optional<UpdateRect> update_rect_;
  66. RtpPacketInfos packet_infos_;
  67. };
  68. // To be deprecated. Migrate all use to Builder.
  69. VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
  70. webrtc::VideoRotation rotation,
  71. int64_t timestamp_us);
  72. VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
  73. uint32_t timestamp_rtp,
  74. int64_t render_time_ms,
  75. VideoRotation rotation);
  76. ~VideoFrame();
  77. // Support move and copy.
  78. VideoFrame(const VideoFrame&);
  79. VideoFrame(VideoFrame&&);
  80. VideoFrame& operator=(const VideoFrame&);
  81. VideoFrame& operator=(VideoFrame&&);
  82. // Get frame width.
  83. int width() const;
  84. // Get frame height.
  85. int height() const;
  86. // Get frame size in pixels.
  87. uint32_t size() const;
  88. // Get frame ID. Returns 0 if ID is not set. Not guarantee to be transferred
  89. // from the sender to the receiver, but preserved on single side. The id
  90. // should be propagated between all frame modifications during its lifetime
  91. // from capturing to sending as encoded image. It is intended to be unique
  92. // over a time window of a few minutes for peer connection, to which
  93. // corresponding video stream belongs to.
  94. uint16_t id() const { return id_; }
  95. void set_id(uint16_t id) { id_ = id; }
  96. // System monotonic clock, same timebase as rtc::TimeMicros().
  97. int64_t timestamp_us() const { return timestamp_us_; }
  98. void set_timestamp_us(int64_t timestamp_us) { timestamp_us_ = timestamp_us; }
  99. // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
  100. // merge, timestamps other than timestamp_us will likely be
  101. // deprecated.
  102. // Set frame timestamp (90kHz).
  103. void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
  104. // Get frame timestamp (90kHz).
  105. uint32_t timestamp() const { return timestamp_rtp_; }
  106. // For now, transport_frame_id and rtp timestamp are the same.
  107. // TODO(nisse): Must be handled differently for QUIC.
  108. uint32_t transport_frame_id() const { return timestamp(); }
  109. // Set capture ntp time in milliseconds.
  110. void set_ntp_time_ms(int64_t ntp_time_ms) { ntp_time_ms_ = ntp_time_ms; }
  111. // Get capture ntp time in milliseconds.
  112. int64_t ntp_time_ms() const { return ntp_time_ms_; }
  113. // Naming convention for Coordination of Video Orientation. Please see
  114. // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
  115. //
  116. // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
  117. //
  118. // "not pending" = a frame that has a VideoRotation == 0.
  119. //
  120. // "apply rotation" = modify a frame from being "pending" to being "not
  121. // pending" rotation (a no-op for "unrotated").
  122. //
  123. VideoRotation rotation() const { return rotation_; }
  124. void set_rotation(VideoRotation rotation) { rotation_ = rotation; }
  125. // Get color space when available.
  126. const absl::optional<ColorSpace>& color_space() const { return color_space_; }
  127. void set_color_space(const absl::optional<ColorSpace>& color_space) {
  128. color_space_ = color_space;
  129. }
  130. // Get render time in milliseconds.
  131. // TODO(nisse): Deprecated. Migrate all users to timestamp_us().
  132. int64_t render_time_ms() const;
  133. // Return the underlying buffer. Never nullptr for a properly
  134. // initialized VideoFrame.
  135. rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
  136. void set_video_frame_buffer(
  137. const rtc::scoped_refptr<VideoFrameBuffer>& buffer);
  138. // TODO(nisse): Deprecated.
  139. // Return true if the frame is stored in a texture.
  140. bool is_texture() const {
  141. return video_frame_buffer()->type() == VideoFrameBuffer::Type::kNative;
  142. }
  143. // Always initialized to whole frame update, can be set by Builder or manually
  144. // by |set_update_rect|.
  145. UpdateRect update_rect() const { return update_rect_; }
  146. // Rectangle must be within the frame dimensions.
  147. void set_update_rect(const VideoFrame::UpdateRect& update_rect) {
  148. RTC_DCHECK_GE(update_rect.offset_x, 0);
  149. RTC_DCHECK_GE(update_rect.offset_y, 0);
  150. RTC_DCHECK_LE(update_rect.offset_x + update_rect.width, width());
  151. RTC_DCHECK_LE(update_rect.offset_y + update_rect.height, height());
  152. update_rect_ = update_rect;
  153. }
  154. // Get information about packets used to assemble this video frame. Might be
  155. // empty if the information isn't available.
  156. const RtpPacketInfos& packet_infos() const { return packet_infos_; }
  157. void set_packet_infos(RtpPacketInfos value) {
  158. packet_infos_ = std::move(value);
  159. }
  160. private:
  161. VideoFrame(uint16_t id,
  162. const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
  163. int64_t timestamp_us,
  164. uint32_t timestamp_rtp,
  165. int64_t ntp_time_ms,
  166. VideoRotation rotation,
  167. const absl::optional<ColorSpace>& color_space,
  168. const absl::optional<UpdateRect>& update_rect,
  169. RtpPacketInfos packet_infos);
  170. uint16_t id_;
  171. // An opaque reference counted handle that stores the pixel data.
  172. rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
  173. uint32_t timestamp_rtp_;
  174. int64_t ntp_time_ms_;
  175. int64_t timestamp_us_;
  176. VideoRotation rotation_;
  177. absl::optional<ColorSpace> color_space_;
  178. // Updated since the last frame area. Unless set explicitly, will always be
  179. // a full frame rectangle.
  180. UpdateRect update_rect_;
  181. // Information about packets used to assemble this video frame. This is needed
  182. // by |SourceTracker| when the frame is delivered to the RTCRtpReceiver's
  183. // MediaStreamTrack, in order to implement getContributingSources(). See:
  184. // https://w3c.github.io/webrtc-pc/#dom-rtcrtpreceiver-getcontributingsources
  185. RtpPacketInfos packet_infos_;
  186. };
  187. } // namespace webrtc
  188. #endif // API_VIDEO_VIDEO_FRAME_H_