blob: eb9b73f1a2843aa1ae918bea5df6e1ad2a27af81 [file] [log] [blame]
Ilya Nikolaevskiy4c87d832020-09-18 15:18:54 +02001/*
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 <stdint.h>
14#include <string.h>
15
16#include "api/scoped_refptr.h"
17#include "api/video/i420_buffer.h"
18#include "api/video/video_frame_buffer.h"
19#include "test/gtest.h"
20
21namespace webrtc {
22
23TEST(TestVideoFrameBufferPool, SimpleFrameReuse) {
24 VideoFrameBufferPool pool;
25 auto buffer = pool.CreateI420Buffer(16, 16);
26 EXPECT_EQ(16, buffer->width());
27 EXPECT_EQ(16, buffer->height());
28 // Extract non-refcounted pointers for testing.
29 const uint8_t* y_ptr = buffer->DataY();
30 const uint8_t* u_ptr = buffer->DataU();
31 const uint8_t* v_ptr = buffer->DataV();
32 // Release buffer so that it is returned to the pool.
33 buffer = nullptr;
34 // Check that the memory is resued.
35 buffer = pool.CreateI420Buffer(16, 16);
36 EXPECT_EQ(y_ptr, buffer->DataY());
37 EXPECT_EQ(u_ptr, buffer->DataU());
38 EXPECT_EQ(v_ptr, buffer->DataV());
39}
40
41TEST(TestVideoFrameBufferPool, FailToReuseWrongSize) {
42 // Set max frames to 1, just to make sure the first buffer is being released.
43 VideoFrameBufferPool pool(/*zero_initialize=*/false, 1);
44 auto buffer = pool.CreateI420Buffer(16, 16);
45 EXPECT_EQ(16, buffer->width());
46 EXPECT_EQ(16, buffer->height());
47 // Release buffer so that it is returned to the pool.
48 buffer = nullptr;
49 // Check that the pool doesn't try to reuse buffers of incorrect size.
50 buffer = pool.CreateI420Buffer(32, 16);
51 ASSERT_TRUE(buffer);
52 EXPECT_EQ(32, buffer->width());
53 EXPECT_EQ(16, buffer->height());
54}
55
56TEST(TestVideoFrameBufferPool, FrameValidAfterPoolDestruction) {
57 rtc::scoped_refptr<I420Buffer> buffer;
58 {
59 VideoFrameBufferPool pool;
60 buffer = pool.CreateI420Buffer(16, 16);
61 }
62 EXPECT_EQ(16, buffer->width());
63 EXPECT_EQ(16, buffer->height());
64 // Access buffer, so that ASAN could find any issues if buffer
65 // doesn't outlive the buffer pool.
66 memset(buffer->MutableDataY(), 0xA5, 16 * buffer->StrideY());
67}
68
69TEST(TestVideoFrameBufferPool, MaxNumberOfBuffers) {
70 VideoFrameBufferPool pool(false, 1);
71 auto buffer = pool.CreateI420Buffer(16, 16);
72 EXPECT_NE(nullptr, buffer.get());
73 EXPECT_EQ(nullptr, pool.CreateI420Buffer(16, 16).get());
74}
75
76TEST(TestVideoFrameBufferPool, ProducesNv12) {
77 VideoFrameBufferPool pool(false, 1);
78 auto buffer = pool.CreateNV12Buffer(16, 16);
79 EXPECT_NE(nullptr, buffer.get());
80}
81
82TEST(TestVideoFrameBufferPool, SwitchingPixelFormat) {
83 VideoFrameBufferPool pool(false, 1);
84 auto buffer = pool.CreateNV12Buffer(16, 16);
85 EXPECT_EQ(nullptr, pool.CreateNV12Buffer(16, 16).get());
86 auto buffer2 = pool.CreateI420Buffer(16, 16);
87 EXPECT_NE(nullptr, buffer2.get());
88 EXPECT_EQ(nullptr, pool.CreateI420Buffer(16, 16).get());
89}
90
91} // namespace webrtc