Henrik Boström | bd9e4a9 | 2021-03-22 12:24:30 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 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 TEST_MAPPABLE_NATIVE_BUFFER_H_ |
| 12 | #define TEST_MAPPABLE_NATIVE_BUFFER_H_ |
| 13 | |
| 14 | #include <utility> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "api/array_view.h" |
| 18 | #include "api/video/video_frame.h" |
| 19 | #include "common_video/include/video_frame_buffer.h" |
| 20 | #include "rtc_base/ref_counted_object.h" |
| 21 | #include "rtc_base/synchronization/mutex.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | namespace test { |
| 25 | |
| 26 | class MappableNativeBuffer; |
| 27 | |
| 28 | VideoFrame CreateMappableNativeFrame(int64_t ntp_time_ms, |
| 29 | VideoFrameBuffer::Type mappable_type, |
| 30 | int width, |
| 31 | int height); |
| 32 | |
| 33 | rtc::scoped_refptr<MappableNativeBuffer> GetMappableNativeBufferFromVideoFrame( |
| 34 | const VideoFrame& frame); |
| 35 | |
| 36 | // A for-testing native buffer that is scalable and mappable. The contents of |
| 37 | // the buffer is black and the pixels are created upon mapping. Mapped buffers |
| 38 | // are stored inside MappableNativeBuffer, allowing tests to verify which |
| 39 | // resolutions were mapped, e.g. when passing them in to an encoder or other |
| 40 | // modules. |
| 41 | class MappableNativeBuffer : public VideoFrameBuffer { |
| 42 | public: |
Artem Titov | 1ee563d | 2021-07-27 12:46:29 +0200 | [diff] [blame] | 43 | // If `allow_i420_conversion` is false, calling ToI420() on a non-I420 buffer |
Henrik Boström | bd9e4a9 | 2021-03-22 12:24:30 +0100 | [diff] [blame] | 44 | // will DCHECK-crash. Used to ensure zero-copy in tests. |
| 45 | MappableNativeBuffer(VideoFrameBuffer::Type mappable_type, |
| 46 | int width, |
| 47 | int height); |
| 48 | ~MappableNativeBuffer() override; |
| 49 | |
| 50 | VideoFrameBuffer::Type mappable_type() const { return mappable_type_; } |
| 51 | |
| 52 | VideoFrameBuffer::Type type() const override { return Type::kNative; } |
| 53 | int width() const override { return width_; } |
| 54 | int height() const override { return height_; } |
| 55 | |
| 56 | rtc::scoped_refptr<VideoFrameBuffer> CropAndScale(int offset_x, |
| 57 | int offset_y, |
| 58 | int crop_width, |
| 59 | int crop_height, |
| 60 | int scaled_width, |
| 61 | int scaled_height) override; |
| 62 | |
| 63 | rtc::scoped_refptr<I420BufferInterface> ToI420() override; |
| 64 | rtc::scoped_refptr<VideoFrameBuffer> GetMappedFrameBuffer( |
| 65 | rtc::ArrayView<VideoFrameBuffer::Type> types) override; |
| 66 | |
| 67 | // Gets all the buffers that have been mapped so far, including mappings of |
| 68 | // cropped and scaled buffers. |
| 69 | std::vector<rtc::scoped_refptr<VideoFrameBuffer>> GetMappedFramedBuffers() |
| 70 | const; |
| 71 | bool DidConvertToI420() const; |
| 72 | |
| 73 | private: |
| 74 | friend class rtc::RefCountedObject<MappableNativeBuffer>; |
| 75 | |
| 76 | class ScaledBuffer : public VideoFrameBuffer { |
| 77 | public: |
| 78 | ScaledBuffer(rtc::scoped_refptr<MappableNativeBuffer> parent, |
| 79 | int width, |
| 80 | int height); |
| 81 | ~ScaledBuffer() override; |
| 82 | |
| 83 | VideoFrameBuffer::Type type() const override { return Type::kNative; } |
| 84 | int width() const override { return width_; } |
| 85 | int height() const override { return height_; } |
| 86 | |
| 87 | rtc::scoped_refptr<VideoFrameBuffer> CropAndScale( |
| 88 | int offset_x, |
| 89 | int offset_y, |
| 90 | int crop_width, |
| 91 | int crop_height, |
| 92 | int scaled_width, |
| 93 | int scaled_height) override; |
| 94 | |
| 95 | rtc::scoped_refptr<I420BufferInterface> ToI420() override; |
| 96 | rtc::scoped_refptr<VideoFrameBuffer> GetMappedFrameBuffer( |
| 97 | rtc::ArrayView<VideoFrameBuffer::Type> types) override; |
| 98 | |
| 99 | private: |
| 100 | friend class rtc::RefCountedObject<ScaledBuffer>; |
| 101 | |
| 102 | const rtc::scoped_refptr<MappableNativeBuffer> parent_; |
| 103 | const int width_; |
| 104 | const int height_; |
| 105 | }; |
| 106 | |
| 107 | rtc::scoped_refptr<ScaledBuffer> FullSizeBuffer(); |
| 108 | rtc::scoped_refptr<VideoFrameBuffer> GetOrCreateMappedBuffer(int width, |
| 109 | int height); |
| 110 | |
| 111 | const VideoFrameBuffer::Type mappable_type_; |
| 112 | const int width_; |
| 113 | const int height_; |
| 114 | mutable Mutex lock_; |
| 115 | std::vector<rtc::scoped_refptr<VideoFrameBuffer>> mapped_buffers_ |
| 116 | RTC_GUARDED_BY(&lock_); |
| 117 | }; |
| 118 | |
| 119 | } // namespace test |
| 120 | } // namespace webrtc |
| 121 | |
| 122 | #endif // TEST_MAPPABLE_NATIVE_BUFFER_H_ |