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