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.

223 lines
8.1 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_ENCODED_IMAGE_H_
  11. #define API_VIDEO_ENCODED_IMAGE_H_
  12. #include <stdint.h>
  13. #include <map>
  14. #include <utility>
  15. #include "absl/types/optional.h"
  16. #include "api/rtp_packet_infos.h"
  17. #include "api/scoped_refptr.h"
  18. #include "api/video/color_space.h"
  19. #include "api/video/video_codec_constants.h"
  20. #include "api/video/video_codec_type.h"
  21. #include "api/video/video_content_type.h"
  22. #include "api/video/video_frame_type.h"
  23. #include "api/video/video_rotation.h"
  24. #include "api/video/video_timing.h"
  25. #include "common_types.h" // NOLINT(build/include)
  26. #include "rtc_base/checks.h"
  27. #include "rtc_base/deprecation.h"
  28. #include "rtc_base/ref_count.h"
  29. #include "rtc_base/system/rtc_export.h"
  30. namespace webrtc {
  31. // Abstract interface for buffer storage. Intended to support buffers owned by
  32. // external encoders with special release requirements, e.g, java encoders with
  33. // releaseOutputBuffer.
  34. class EncodedImageBufferInterface : public rtc::RefCountInterface {
  35. public:
  36. virtual const uint8_t* data() const = 0;
  37. // TODO(bugs.webrtc.org/9378): Make interface essentially read-only, delete
  38. // this non-const data method.
  39. virtual uint8_t* data() = 0;
  40. virtual size_t size() const = 0;
  41. };
  42. // Basic implementation of EncodedImageBufferInterface.
  43. class RTC_EXPORT EncodedImageBuffer : public EncodedImageBufferInterface {
  44. public:
  45. static rtc::scoped_refptr<EncodedImageBuffer> Create() { return Create(0); }
  46. static rtc::scoped_refptr<EncodedImageBuffer> Create(size_t size);
  47. static rtc::scoped_refptr<EncodedImageBuffer> Create(const uint8_t* data,
  48. size_t size);
  49. const uint8_t* data() const override;
  50. uint8_t* data() override;
  51. size_t size() const override;
  52. void Realloc(size_t t);
  53. protected:
  54. explicit EncodedImageBuffer(size_t size);
  55. EncodedImageBuffer(const uint8_t* data, size_t size);
  56. ~EncodedImageBuffer();
  57. size_t size_;
  58. uint8_t* buffer_;
  59. };
  60. // TODO(bug.webrtc.org/9378): This is a legacy api class, which is slowly being
  61. // cleaned up. Direct use of its members is strongly discouraged.
  62. class RTC_EXPORT EncodedImage {
  63. public:
  64. EncodedImage();
  65. EncodedImage(EncodedImage&&);
  66. // Discouraged: potentially expensive.
  67. EncodedImage(const EncodedImage&);
  68. EncodedImage(uint8_t* buffer, size_t length, size_t capacity);
  69. ~EncodedImage();
  70. EncodedImage& operator=(EncodedImage&&);
  71. // Discouraged: potentially expensive.
  72. EncodedImage& operator=(const EncodedImage&);
  73. // TODO(nisse): Change style to timestamp(), set_timestamp(), for consistency
  74. // with the VideoFrame class.
  75. // Set frame timestamp (90kHz).
  76. void SetTimestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
  77. // Get frame timestamp (90kHz).
  78. uint32_t Timestamp() const { return timestamp_rtp_; }
  79. void SetEncodeTime(int64_t encode_start_ms, int64_t encode_finish_ms);
  80. absl::optional<int> SpatialIndex() const { return spatial_index_; }
  81. void SetSpatialIndex(absl::optional<int> spatial_index) {
  82. RTC_DCHECK_GE(spatial_index.value_or(0), 0);
  83. RTC_DCHECK_LT(spatial_index.value_or(0), kMaxSpatialLayers);
  84. spatial_index_ = spatial_index;
  85. }
  86. // These methods can be used to set/get size of subframe with spatial index
  87. // |spatial_index| on encoded frames that consist of multiple spatial layers.
  88. absl::optional<size_t> SpatialLayerFrameSize(int spatial_index) const;
  89. void SetSpatialLayerFrameSize(int spatial_index, size_t size_bytes);
  90. const webrtc::ColorSpace* ColorSpace() const {
  91. return color_space_ ? &*color_space_ : nullptr;
  92. }
  93. void SetColorSpace(const absl::optional<webrtc::ColorSpace>& color_space) {
  94. color_space_ = color_space;
  95. }
  96. const RtpPacketInfos& PacketInfos() const { return packet_infos_; }
  97. void SetPacketInfos(RtpPacketInfos packet_infos) {
  98. packet_infos_ = std::move(packet_infos);
  99. }
  100. bool RetransmissionAllowed() const { return retransmission_allowed_; }
  101. void SetRetransmissionAllowed(bool retransmission_allowed) {
  102. retransmission_allowed_ = retransmission_allowed;
  103. }
  104. size_t size() const { return size_; }
  105. void set_size(size_t new_size) {
  106. // Allow set_size(0) even if we have no buffer.
  107. RTC_DCHECK_LE(new_size, new_size == 0 ? 0 : capacity());
  108. size_ = new_size;
  109. }
  110. // TODO(nisse): Delete, provide only read-only access to the buffer.
  111. size_t capacity() const {
  112. return buffer_ ? capacity_ : (encoded_data_ ? encoded_data_->size() : 0);
  113. }
  114. void set_buffer(uint8_t* buffer, size_t capacity) {
  115. buffer_ = buffer;
  116. capacity_ = capacity;
  117. }
  118. void SetEncodedData(
  119. rtc::scoped_refptr<EncodedImageBufferInterface> encoded_data) {
  120. encoded_data_ = encoded_data;
  121. size_ = encoded_data->size();
  122. buffer_ = nullptr;
  123. }
  124. // TODO(nisse): Delete, provide only read-only access to the buffer.
  125. uint8_t* data() {
  126. return buffer_ ? buffer_
  127. : (encoded_data_ ? encoded_data_->data() : nullptr);
  128. }
  129. const uint8_t* data() const {
  130. return buffer_ ? buffer_
  131. : (encoded_data_ ? encoded_data_->data() : nullptr);
  132. }
  133. // TODO(nisse): At some places, code accepts a const ref EncodedImage, but
  134. // still writes to it, to clear padding at the end of the encoded data.
  135. // Padding is required by ffmpeg; the best way to deal with that is likely to
  136. // make this class ensure that buffers always have a few zero padding bytes.
  137. uint8_t* mutable_data() const { return const_cast<uint8_t*>(data()); }
  138. // TODO(bugs.webrtc.org/9378): Delete. Used by code that wants to modify a
  139. // buffer corresponding to a const EncodedImage. Requires an un-owned buffer.
  140. uint8_t* buffer() const { return buffer_; }
  141. // Hack to workaround lack of ownership of the encoded data. If we don't
  142. // already own the underlying data, make an owned copy.
  143. void Retain();
  144. uint32_t _encodedWidth = 0;
  145. uint32_t _encodedHeight = 0;
  146. // NTP time of the capture time in local timebase in milliseconds.
  147. int64_t ntp_time_ms_ = 0;
  148. int64_t capture_time_ms_ = 0;
  149. VideoFrameType _frameType = VideoFrameType::kVideoFrameDelta;
  150. VideoRotation rotation_ = kVideoRotation_0;
  151. VideoContentType content_type_ = VideoContentType::UNSPECIFIED;
  152. bool _completeFrame = false;
  153. int qp_ = -1; // Quantizer value.
  154. // When an application indicates non-zero values here, it is taken as an
  155. // indication that all future frames will be constrained with those limits
  156. // until the application indicates a change again.
  157. PlayoutDelay playout_delay_ = {-1, -1};
  158. struct Timing {
  159. uint8_t flags = VideoSendTiming::kInvalid;
  160. int64_t encode_start_ms = 0;
  161. int64_t encode_finish_ms = 0;
  162. int64_t packetization_finish_ms = 0;
  163. int64_t pacer_exit_ms = 0;
  164. int64_t network_timestamp_ms = 0;
  165. int64_t network2_timestamp_ms = 0;
  166. int64_t receive_start_ms = 0;
  167. int64_t receive_finish_ms = 0;
  168. } timing_;
  169. private:
  170. // TODO(bugs.webrtc.org/9378): We're transitioning to always owning the
  171. // encoded data.
  172. rtc::scoped_refptr<EncodedImageBufferInterface> encoded_data_;
  173. size_t size_; // Size of encoded frame data.
  174. // Non-null when used with an un-owned buffer.
  175. uint8_t* buffer_;
  176. // Allocated size of _buffer; relevant only if it's non-null.
  177. size_t capacity_;
  178. uint32_t timestamp_rtp_ = 0;
  179. absl::optional<int> spatial_index_;
  180. std::map<int, size_t> spatial_layer_frame_size_bytes_;
  181. absl::optional<webrtc::ColorSpace> color_space_;
  182. // Information about packets used to assemble this video frame. This is needed
  183. // by |SourceTracker| when the frame is delivered to the RTCRtpReceiver's
  184. // MediaStreamTrack, in order to implement getContributingSources(). See:
  185. // https://w3c.github.io/webrtc-pc/#dom-rtcrtpreceiver-getcontributingsources
  186. RtpPacketInfos packet_infos_;
  187. bool retransmission_allowed_ = true;
  188. };
  189. } // namespace webrtc
  190. #endif // API_VIDEO_ENCODED_IMAGE_H_