encoded_image.h 7.6 KB

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