video_frame_buffer_pool.h (3625B)
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 #ifndef COMMON_VIDEO_INCLUDE_VIDEO_FRAME_BUFFER_POOL_H_ 12 #define COMMON_VIDEO_INCLUDE_VIDEO_FRAME_BUFFER_POOL_H_ 13 14 #include <stddef.h> 15 16 #include <list> 17 18 #include "api/scoped_refptr.h" 19 #include "api/video/i010_buffer.h" 20 #include "api/video/i210_buffer.h" 21 #include "api/video/i410_buffer.h" 22 #include "api/video/i420_buffer.h" 23 #include "api/video/i422_buffer.h" 24 #include "api/video/i444_buffer.h" 25 #include "api/video/nv12_buffer.h" 26 #include "api/video/video_frame_buffer.h" 27 #include "rtc_base/race_checker.h" 28 29 namespace webrtc { 30 31 // Simple buffer pool to avoid unnecessary allocations of video frame buffers. 32 // The pool manages the memory of the I420Buffer/NV12Buffer returned from 33 // Create(I420|NV12)Buffer. When the buffer is destructed, the memory is 34 // returned to the pool for use by subsequent calls to Create(I420|NV12)Buffer. 35 // If the resolution passed to Create(I420|NV12)Buffer changes or requested 36 // pixel format changes, old buffers will be purged from the pool. 37 // Note that Create(I420|NV12)Buffer will crash if more than 38 // kMaxNumberOfFramesBeforeCrash are created. This is to prevent memory leaks 39 // where frames are not returned. 40 class VideoFrameBufferPool { 41 public: 42 VideoFrameBufferPool(); 43 explicit VideoFrameBufferPool(bool zero_initialize); 44 VideoFrameBufferPool(bool zero_initialize, size_t max_number_of_buffers); 45 ~VideoFrameBufferPool(); 46 47 // Returns a buffer from the pool. If no suitable buffer exist in the pool 48 // and there are less than `max_number_of_buffers` pending, a buffer is 49 // created. Returns null otherwise. 50 scoped_refptr<I420Buffer> CreateI420Buffer(int width, int height); 51 scoped_refptr<I422Buffer> CreateI422Buffer(int width, int height); 52 scoped_refptr<I444Buffer> CreateI444Buffer(int width, int height); 53 scoped_refptr<I010Buffer> CreateI010Buffer(int width, int height); 54 scoped_refptr<I210Buffer> CreateI210Buffer(int width, int height); 55 scoped_refptr<I410Buffer> CreateI410Buffer(int width, int height); 56 scoped_refptr<NV12Buffer> CreateNV12Buffer(int width, int height); 57 58 // Changes the max amount of buffers in the pool to the new value. 59 // Returns true if change was successful and false if the amount of already 60 // allocated buffers is bigger than new value. 61 bool Resize(size_t max_number_of_buffers); 62 63 // Clears buffers_ and detaches the thread checker so that it can be reused 64 // later from another thread. 65 void Release(); 66 67 private: 68 scoped_refptr<VideoFrameBuffer> 69 GetExistingBuffer(int width, int height, VideoFrameBuffer::Type type); 70 71 RaceChecker race_checker_; 72 std::list<scoped_refptr<VideoFrameBuffer>> buffers_; 73 // If true, newly allocated buffers are zero-initialized. Note that recycled 74 // buffers are not zero'd before reuse. This is required of buffers used by 75 // FFmpeg according to http://crbug.com/390941, which only requires it for the 76 // initial allocation (as shown by FFmpeg's own buffer allocation code). It 77 // has to do with "Use-of-uninitialized-value" on "Linux_msan_chrome". 78 const bool zero_initialize_; 79 // Max number of buffers this pool can have pending. 80 size_t max_number_of_buffers_; 81 }; 82 83 } // namespace webrtc 84 85 #endif // COMMON_VIDEO_INCLUDE_VIDEO_FRAME_BUFFER_POOL_H_