| 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 | #include "api/video/encoded_image.h" |
| 12 |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 |
| 16 | namespace webrtc { |
| 17 |
| 18 | EncodedImageBuffer::EncodedImageBuffer(size_t size) : size_(size) { |
| 19 | buffer_ = static_cast<uint8_t*>(malloc(size)); |
| 20 | } |
| 21 |
| 22 | EncodedImageBuffer::EncodedImageBuffer(const uint8_t* data, size_t size) |
| 23 | : EncodedImageBuffer(size) { |
| 24 | memcpy(buffer_, data, size); |
| 25 | } |
| 26 |
| 27 | EncodedImageBuffer::~EncodedImageBuffer() { |
| 28 | free(buffer_); |
| 29 | } |
| 30 |
| 31 | // static |
| 32 | rtc::scoped_refptr<EncodedImageBuffer> EncodedImageBuffer::Create(size_t size) { |
| 33 | return rtc::make_ref_counted<EncodedImageBuffer>(size); |
| 34 | } |
| 35 | // static |
| 36 | rtc::scoped_refptr<EncodedImageBuffer> EncodedImageBuffer::Create( |
| 37 | const uint8_t* data, |
| 38 | size_t size) { |
| 39 | return rtc::make_ref_counted<EncodedImageBuffer>(data, size); |
| 40 | } |
| 41 |
| 42 | const uint8_t* EncodedImageBuffer::data() const { |
| 43 | return buffer_; |
| 44 | } |
| 45 | uint8_t* EncodedImageBuffer::data() { |
| 46 | return buffer_; |
| 47 | } |
| 48 | size_t EncodedImageBuffer::size() const { |
| 49 | return size_; |
| 50 | } |
| 51 |
| 52 | void EncodedImageBuffer::Realloc(size_t size) { |
| 53 | // Calling realloc with size == 0 is equivalent to free, and returns nullptr. |
| 54 | // Which is confusing on systems where malloc(0) doesn't return a nullptr. |
| 55 | // More specifically, it breaks expectations of |
| 56 | // VCMSessionInfo::UpdateDataPointers. |
| 57 | RTC_DCHECK(size > 0); |
| 58 | buffer_ = static_cast<uint8_t*>(realloc(buffer_, size)); |
| 59 | size_ = size; |
| 60 | } |
| 61 |
| 62 | EncodedImage::EncodedImage() = default; |
| 63 |
| 64 | EncodedImage::EncodedImage(EncodedImage&&) = default; |
| 65 | EncodedImage::EncodedImage(const EncodedImage&) = default; |
| 66 |
| 67 | EncodedImage::~EncodedImage() = default; |
| 68 |
| 69 | EncodedImage& EncodedImage::operator=(EncodedImage&&) = default; |
| 70 | EncodedImage& EncodedImage::operator=(const EncodedImage&) = default; |
| 71 |
| 72 | void EncodedImage::SetEncodeTime(int64_t encode_start_ms, |
| 73 | int64_t encode_finish_ms) { |
| 74 | timing_.encode_start_ms = encode_start_ms; |
| 75 | timing_.encode_finish_ms = encode_finish_ms; |
| 76 | } |
| 77 |
| 78 | webrtc::Timestamp EncodedImage::CaptureTime() const { |
| 79 | return capture_time_ms_ > 0 ? Timestamp::Millis(capture_time_ms_) |
| 80 | : Timestamp::MinusInfinity(); |
| 81 | } |
| 82 |
| 83 | absl::optional<size_t> EncodedImage::SpatialLayerFrameSize( |
| 84 | int spatial_index) const { |
| 85 | RTC_DCHECK_GE(spatial_index, 0); |
| 86 | RTC_DCHECK_LE(spatial_index, spatial_index_.value_or(0)); |
| 87 |
| 88 | auto it = spatial_layer_frame_size_bytes_.find(spatial_index); |
| 89 | if (it == spatial_layer_frame_size_bytes_.end()) { |
| 90 | return absl::nullopt; |
| 91 | } |
| 92 |
| 93 | return it->second; |
| 94 | } |
| 95 |
| 96 | void EncodedImage::SetSpatialLayerFrameSize(int spatial_index, |
| 97 | size_t size_bytes) { |
| 98 | RTC_DCHECK_GE(spatial_index, 0); |
| 99 | RTC_DCHECK_LE(spatial_index, spatial_index_.value_or(0)); |
| 100 | RTC_DCHECK_GE(size_bytes, 0); |
| 101 | spatial_layer_frame_size_bytes_[spatial_index] = size_bytes; |
| 102 | } |
| 103 |
| 104 | } // namespace webrtc |
| 105 |