video_frame_buffer_pool_unittest.cc (4365B)
1 /* 2 * Copyright (c) 2015 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 #include "common_video/include/video_frame_buffer_pool.h" 12 13 #include <cstdint> 14 #include <cstring> 15 16 #include "api/scoped_refptr.h" 17 #include "api/video/i420_buffer.h" 18 #include "test/gtest.h" 19 20 namespace webrtc { 21 22 TEST(TestVideoFrameBufferPool, SimpleFrameReuse) { 23 VideoFrameBufferPool pool; 24 auto buffer = pool.CreateI420Buffer(16, 16); 25 EXPECT_EQ(16, buffer->width()); 26 EXPECT_EQ(16, buffer->height()); 27 // Extract non-refcounted pointers for testing. 28 const uint8_t* y_ptr = buffer->DataY(); 29 const uint8_t* u_ptr = buffer->DataU(); 30 const uint8_t* v_ptr = buffer->DataV(); 31 // Release buffer so that it is returned to the pool. 32 buffer = nullptr; 33 // Check that the memory is resued. 34 buffer = pool.CreateI420Buffer(16, 16); 35 EXPECT_EQ(y_ptr, buffer->DataY()); 36 EXPECT_EQ(u_ptr, buffer->DataU()); 37 EXPECT_EQ(v_ptr, buffer->DataV()); 38 } 39 40 TEST(TestVideoFrameBufferPool, FailToReuseWrongSize) { 41 // Set max frames to 1, just to make sure the first buffer is being released. 42 VideoFrameBufferPool pool(/*zero_initialize=*/false, 1); 43 auto buffer = pool.CreateI420Buffer(16, 16); 44 EXPECT_EQ(16, buffer->width()); 45 EXPECT_EQ(16, buffer->height()); 46 // Release buffer so that it is returned to the pool. 47 buffer = nullptr; 48 // Check that the pool doesn't try to reuse buffers of incorrect size. 49 buffer = pool.CreateI420Buffer(32, 16); 50 ASSERT_TRUE(buffer); 51 EXPECT_EQ(32, buffer->width()); 52 EXPECT_EQ(16, buffer->height()); 53 } 54 55 TEST(TestVideoFrameBufferPool, FrameValidAfterPoolDestruction) { 56 scoped_refptr<I420Buffer> buffer; 57 { 58 VideoFrameBufferPool pool; 59 buffer = pool.CreateI420Buffer(16, 16); 60 } 61 EXPECT_EQ(16, buffer->width()); 62 EXPECT_EQ(16, buffer->height()); 63 // Access buffer, so that ASAN could find any issues if buffer 64 // doesn't outlive the buffer pool. 65 memset(buffer->MutableDataY(), 0xA5, 16 * buffer->StrideY()); 66 } 67 68 TEST(TestVideoFrameBufferPool, MaxNumberOfBuffers) { 69 VideoFrameBufferPool pool(false, 1); 70 auto buffer = pool.CreateI420Buffer(16, 16); 71 EXPECT_NE(nullptr, buffer.get()); 72 EXPECT_EQ(nullptr, pool.CreateI420Buffer(16, 16).get()); 73 } 74 75 TEST(TestVideoFrameBufferPool, ProducesNv12) { 76 VideoFrameBufferPool pool(false, 1); 77 auto buffer = pool.CreateNV12Buffer(16, 16); 78 EXPECT_NE(nullptr, buffer.get()); 79 } 80 81 TEST(TestVideoFrameBufferPool, ProducesI422) { 82 VideoFrameBufferPool pool(false, 1); 83 auto buffer = pool.CreateI422Buffer(16, 16); 84 EXPECT_NE(nullptr, buffer.get()); 85 } 86 87 TEST(TestVideoFrameBufferPool, ProducesI444) { 88 VideoFrameBufferPool pool(false, 1); 89 auto buffer = pool.CreateI444Buffer(16, 16); 90 EXPECT_NE(nullptr, buffer.get()); 91 } 92 93 TEST(TestVideoFrameBufferPool, ProducesI010) { 94 VideoFrameBufferPool pool(false, 1); 95 auto buffer = pool.CreateI010Buffer(16, 16); 96 EXPECT_NE(nullptr, buffer.get()); 97 } 98 99 TEST(TestVideoFrameBufferPool, ProducesI210) { 100 VideoFrameBufferPool pool(false, 1); 101 auto buffer = pool.CreateI210Buffer(16, 16); 102 EXPECT_NE(nullptr, buffer.get()); 103 } 104 105 TEST(TestVideoFrameBufferPool, SwitchingPixelFormat) { 106 VideoFrameBufferPool pool(false, 1); 107 auto buffeNV12 = pool.CreateNV12Buffer(16, 16); 108 EXPECT_EQ(nullptr, pool.CreateNV12Buffer(16, 16).get()); 109 110 auto bufferI420 = pool.CreateI420Buffer(16, 16); 111 EXPECT_NE(nullptr, bufferI420.get()); 112 EXPECT_EQ(nullptr, pool.CreateI420Buffer(16, 16).get()); 113 114 auto bufferI444 = pool.CreateI444Buffer(16, 16); 115 EXPECT_NE(nullptr, bufferI444.get()); 116 EXPECT_EQ(nullptr, pool.CreateI444Buffer(16, 16).get()); 117 118 auto bufferI422 = pool.CreateI422Buffer(16, 16); 119 EXPECT_NE(nullptr, bufferI422.get()); 120 EXPECT_EQ(nullptr, pool.CreateI422Buffer(16, 16).get()); 121 122 auto bufferI010 = pool.CreateI010Buffer(16, 16); 123 EXPECT_NE(nullptr, bufferI010.get()); 124 EXPECT_EQ(nullptr, pool.CreateI010Buffer(16, 16).get()); 125 126 auto bufferI210 = pool.CreateI210Buffer(16, 16); 127 EXPECT_NE(nullptr, bufferI210.get()); 128 EXPECT_EQ(nullptr, pool.CreateI210Buffer(16, 16).get()); 129 } 130 131 } // namespace webrtc