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 11 #ifndef API_VIDEO_ENCODED_IMAGE_H_ 12 #define API_VIDEO_ENCODED_IMAGE_H_ 13 14 #include <stdint.h> 15 16 #include <map> 17 #include <utility> 18 19 #include "absl/types/optional.h" 20 #include "api/rtp_packet_infos.h" 21 #include "api/scoped_refptr.h" 22 #include "api/video/color_space.h" 23 #include "api/video/video_codec_constants.h" 24 #include "api/video/video_content_type.h" 25 #include "api/video/video_frame_type.h" 26 #include "api/video/video_rotation.h" 27 #include "api/video/video_timing.h" 28 #include "rtc_base/checks.h" 29 #include "rtc_base/ref_count.h" 30 #include "rtc_base/system/rtc_export.h" 31 32 namespace webrtc { 33 34 // Abstract interface for buffer storage. Intended to support buffers owned by 35 // external encoders with special release requirements, e.g, java encoders with 36 // releaseOutputBuffer. 37 class EncodedImageBufferInterface : public rtc::RefCountInterface { 38 public: 39 virtual const uint8_t* data() const = 0; 40 // TODO(bugs.webrtc.org/9378): Make interface essentially read-only, delete 41 // this non-const data method. 42 virtual uint8_t* data() = 0; 43 virtual size_t size() const = 0; 44 }; 45 46 // Basic implementation of EncodedImageBufferInterface. 47 class RTC_EXPORT EncodedImageBuffer : public EncodedImageBufferInterface { 48 public: Create()49 static rtc::scoped_refptr<EncodedImageBuffer> Create() { return Create(0); } 50 static rtc::scoped_refptr<EncodedImageBuffer> Create(size_t size); 51 static rtc::scoped_refptr<EncodedImageBuffer> Create(const uint8_t* data, 52 size_t size); 53 54 const uint8_t* data() const override; 55 uint8_t* data() override; 56 size_t size() const override; 57 void Realloc(size_t t); 58 59 protected: 60 explicit EncodedImageBuffer(size_t size); 61 EncodedImageBuffer(const uint8_t* data, size_t size); 62 ~EncodedImageBuffer(); 63 64 size_t size_; 65 uint8_t* buffer_; 66 }; 67 68 // TODO(bug.webrtc.org/9378): This is a legacy api class, which is slowly being 69 // cleaned up. Direct use of its members is strongly discouraged. 70 class RTC_EXPORT EncodedImage { 71 public: 72 EncodedImage(); 73 EncodedImage(EncodedImage&&); 74 EncodedImage(const EncodedImage&); 75 76 ~EncodedImage(); 77 78 EncodedImage& operator=(EncodedImage&&); 79 EncodedImage& operator=(const EncodedImage&); 80 81 // TODO(bugs.webrtc.org/9378): Change style to timestamp(), set_timestamp(), 82 // for consistency with the VideoFrame class. Set frame timestamp (90kHz). SetTimestamp(uint32_t timestamp)83 void SetTimestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; } 84 85 // Get frame timestamp (90kHz). Timestamp()86 uint32_t Timestamp() const { return timestamp_rtp_; } 87 88 void SetEncodeTime(int64_t encode_start_ms, int64_t encode_finish_ms); 89 NtpTimeMs()90 int64_t NtpTimeMs() const { return ntp_time_ms_; } 91 SpatialIndex()92 absl::optional<int> SpatialIndex() const { return spatial_index_; } SetSpatialIndex(absl::optional<int> spatial_index)93 void SetSpatialIndex(absl::optional<int> spatial_index) { 94 RTC_DCHECK_GE(spatial_index.value_or(0), 0); 95 RTC_DCHECK_LT(spatial_index.value_or(0), kMaxSpatialLayers); 96 spatial_index_ = spatial_index; 97 } 98 TemporalIndex()99 absl::optional<int> TemporalIndex() const { return temporal_index_; } SetTemporalIndex(absl::optional<int> temporal_index)100 void SetTemporalIndex(absl::optional<int> temporal_index) { 101 RTC_DCHECK_GE(temporal_index_.value_or(0), 0); 102 RTC_DCHECK_LT(temporal_index_.value_or(0), kMaxTemporalStreams); 103 temporal_index_ = temporal_index; 104 } 105 106 // These methods can be used to set/get size of subframe with spatial index 107 // `spatial_index` on encoded frames that consist of multiple spatial layers. 108 absl::optional<size_t> SpatialLayerFrameSize(int spatial_index) const; 109 void SetSpatialLayerFrameSize(int spatial_index, size_t size_bytes); 110 ColorSpace()111 const webrtc::ColorSpace* ColorSpace() const { 112 return color_space_ ? &*color_space_ : nullptr; 113 } SetColorSpace(const absl::optional<webrtc::ColorSpace> & color_space)114 void SetColorSpace(const absl::optional<webrtc::ColorSpace>& color_space) { 115 color_space_ = color_space; 116 } 117 118 // These methods along with the private member video_frame_tracking_id_ are 119 // meant for media quality testing purpose only. VideoFrameTrackingId()120 absl::optional<uint16_t> VideoFrameTrackingId() const { 121 return video_frame_tracking_id_; 122 } SetVideoFrameTrackingId(absl::optional<uint16_t> tracking_id)123 void SetVideoFrameTrackingId(absl::optional<uint16_t> tracking_id) { 124 video_frame_tracking_id_ = tracking_id; 125 } 126 PacketInfos()127 const RtpPacketInfos& PacketInfos() const { return packet_infos_; } SetPacketInfos(RtpPacketInfos packet_infos)128 void SetPacketInfos(RtpPacketInfos packet_infos) { 129 packet_infos_ = std::move(packet_infos); 130 } 131 RetransmissionAllowed()132 bool RetransmissionAllowed() const { return retransmission_allowed_; } SetRetransmissionAllowed(bool retransmission_allowed)133 void SetRetransmissionAllowed(bool retransmission_allowed) { 134 retransmission_allowed_ = retransmission_allowed; 135 } 136 size()137 size_t size() const { return size_; } set_size(size_t new_size)138 void set_size(size_t new_size) { 139 // Allow set_size(0) even if we have no buffer. 140 RTC_DCHECK_LE(new_size, new_size == 0 ? 0 : capacity()); 141 size_ = new_size; 142 } 143 SetEncodedData(rtc::scoped_refptr<EncodedImageBufferInterface> encoded_data)144 void SetEncodedData( 145 rtc::scoped_refptr<EncodedImageBufferInterface> encoded_data) { 146 encoded_data_ = encoded_data; 147 size_ = encoded_data->size(); 148 } 149 ClearEncodedData()150 void ClearEncodedData() { 151 encoded_data_ = nullptr; 152 size_ = 0; 153 } 154 GetEncodedData()155 rtc::scoped_refptr<EncodedImageBufferInterface> GetEncodedData() const { 156 return encoded_data_; 157 } 158 data()159 const uint8_t* data() const { 160 return encoded_data_ ? encoded_data_->data() : nullptr; 161 } 162 163 // Returns whether the encoded image can be considered to be of target 164 // quality. IsAtTargetQuality()165 bool IsAtTargetQuality() const { return at_target_quality_; } 166 167 // Sets that the encoded image can be considered to be of target quality to 168 // true or false. SetAtTargetQuality(bool at_target_quality)169 void SetAtTargetQuality(bool at_target_quality) { 170 at_target_quality_ = at_target_quality; 171 } 172 173 uint32_t _encodedWidth = 0; 174 uint32_t _encodedHeight = 0; 175 // NTP time of the capture time in local timebase in milliseconds. 176 // TODO(minyue): make this member private. 177 int64_t ntp_time_ms_ = 0; 178 int64_t capture_time_ms_ = 0; 179 VideoFrameType _frameType = VideoFrameType::kVideoFrameDelta; 180 VideoRotation rotation_ = kVideoRotation_0; 181 VideoContentType content_type_ = VideoContentType::UNSPECIFIED; 182 int qp_ = -1; // Quantizer value. 183 184 // When an application indicates non-zero values here, it is taken as an 185 // indication that all future frames will be constrained with those limits 186 // until the application indicates a change again. 187 VideoPlayoutDelay playout_delay_; 188 189 struct Timing { 190 uint8_t flags = VideoSendTiming::kInvalid; 191 int64_t encode_start_ms = 0; 192 int64_t encode_finish_ms = 0; 193 int64_t packetization_finish_ms = 0; 194 int64_t pacer_exit_ms = 0; 195 int64_t network_timestamp_ms = 0; 196 int64_t network2_timestamp_ms = 0; 197 int64_t receive_start_ms = 0; 198 int64_t receive_finish_ms = 0; 199 } timing_; 200 201 private: capacity()202 size_t capacity() const { return encoded_data_ ? encoded_data_->size() : 0; } 203 204 rtc::scoped_refptr<EncodedImageBufferInterface> encoded_data_; 205 size_t size_ = 0; // Size of encoded frame data. 206 uint32_t timestamp_rtp_ = 0; 207 absl::optional<int> spatial_index_; 208 absl::optional<int> temporal_index_; 209 std::map<int, size_t> spatial_layer_frame_size_bytes_; 210 absl::optional<webrtc::ColorSpace> color_space_; 211 // This field is meant for media quality testing purpose only. When enabled it 212 // carries the webrtc::VideoFrame id field from the sender to the receiver. 213 absl::optional<uint16_t> video_frame_tracking_id_; 214 // Information about packets used to assemble this video frame. This is needed 215 // by `SourceTracker` when the frame is delivered to the RTCRtpReceiver's 216 // MediaStreamTrack, in order to implement getContributingSources(). See: 217 // https://w3c.github.io/webrtc-pc/#dom-rtcrtpreceiver-getcontributingsources 218 RtpPacketInfos packet_infos_; 219 bool retransmission_allowed_ = true; 220 // True if the encoded image can be considered to be of target quality. 221 bool at_target_quality_ = false; 222 }; 223 224 } // namespace webrtc 225 226 #endif // API_VIDEO_ENCODED_IMAGE_H_ 227