encoded_image.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_directory)
  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. int64_t NtpTimeMs() const { return ntp_time_ms_; }
  81. absl::optional<int> SpatialIndex() const { return spatial_index_; }
  82. void SetSpatialIndex(absl::optional<int> spatial_index) {
  83. RTC_DCHECK_GE(spatial_index.value_or(0), 0);
  84. RTC_DCHECK_LT(spatial_index.value_or(0), kMaxSpatialLayers);
  85. spatial_index_ = spatial_index;
  86. }
  87. // These methods can be used to set/get size of subframe with spatial index
  88. // |spatial_index| on encoded frames that consist of multiple spatial layers.
  89. absl::optional<size_t> SpatialLayerFrameSize(int spatial_index) const;
  90. void SetSpatialLayerFrameSize(int spatial_index, size_t size_bytes);
  91. const webrtc::ColorSpace* ColorSpace() const {
  92. return color_space_ ? &*color_space_ : nullptr;
  93. }
  94. void SetColorSpace(const absl::optional<webrtc::ColorSpace>& color_space) {
  95. color_space_ = color_space;
  96. }
  97. const RtpPacketInfos& PacketInfos() const { return packet_infos_; }
  98. void SetPacketInfos(RtpPacketInfos packet_infos) {
  99. packet_infos_ = std::move(packet_infos);
  100. }
  101. bool RetransmissionAllowed() const { return retransmission_allowed_; }
  102. void SetRetransmissionAllowed(bool retransmission_allowed) {
  103. retransmission_allowed_ = retransmission_allowed;
  104. }
  105. size_t size() const { return size_; }
  106. void set_size(size_t new_size) {
  107. // Allow set_size(0) even if we have no buffer.
  108. RTC_DCHECK_LE(new_size, new_size == 0 ? 0 : capacity());
  109. size_ = new_size;
  110. }
  111. // TODO(nisse): Delete, provide only read-only access to the buffer.
  112. size_t capacity() const {
  113. return buffer_ ? capacity_ : (encoded_data_ ? encoded_data_->size() : 0);
  114. }
  115. void SetEncodedData(
  116. rtc::scoped_refptr<EncodedImageBufferInterface> encoded_data) {
  117. encoded_data_ = encoded_data;
  118. size_ = encoded_data->size();
  119. buffer_ = nullptr;
  120. }
  121. void ClearEncodedData() {
  122. encoded_data_ = nullptr;
  123. size_ = 0;
  124. buffer_ = nullptr;
  125. capacity_ = 0;
  126. }
  127. rtc::scoped_refptr<EncodedImageBufferInterface> GetEncodedData() const {
  128. RTC_DCHECK(buffer_ == nullptr);
  129. return encoded_data_;
  130. }
  131. // TODO(nisse): Delete, provide only read-only access to the buffer.
  132. uint8_t* data() {
  133. return buffer_ ? buffer_
  134. : (encoded_data_ ? encoded_data_->data() : nullptr);
  135. }
  136. const uint8_t* data() const {
  137. return buffer_ ? buffer_
  138. : (encoded_data_ ? encoded_data_->data() : nullptr);
  139. }
  140. // Hack to workaround lack of ownership of the encoded data. If we don't
  141. // already own the underlying data, make an owned copy.
  142. void Retain();
  143. uint32_t _encodedWidth = 0;
  144. uint32_t _encodedHeight = 0;
  145. // NTP time of the capture time in local timebase in milliseconds.
  146. // TODO(minyue): make this member private.
  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_