1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/base/video_frame_pool.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/synchronization/lock.h"
15 class VideoFramePool::PoolImpl
16 : public base::RefCountedThreadSafe
<VideoFramePool::PoolImpl
> {
20 // See VideoFramePool::CreateFrame() for usage.
21 scoped_refptr
<VideoFrame
> CreateFrame(VideoPixelFormat format
,
22 const gfx::Size
& coded_size
,
23 const gfx::Rect
& visible_rect
,
24 const gfx::Size
& natural_size
,
25 base::TimeDelta timestamp
);
27 // Shuts down the frame pool and releases all frames in |frames_|.
28 // Once this is called frames will no longer be inserted back into
32 size_t GetPoolSizeForTesting() const { return frames_
.size(); }
35 friend class base::RefCountedThreadSafe
<VideoFramePool::PoolImpl
>;
38 // Called when the frame wrapper gets destroyed.
39 // |frame| is the actual frame that was wrapped and is placed
40 // in |frames_| by this function so it can be reused.
41 void FrameReleased(const scoped_refptr
<VideoFrame
>& frame
);
45 std::list
<scoped_refptr
<VideoFrame
> > frames_
;
47 DISALLOW_COPY_AND_ASSIGN(PoolImpl
);
50 VideoFramePool::PoolImpl::PoolImpl() : is_shutdown_(false) {}
52 VideoFramePool::PoolImpl::~PoolImpl() {
56 scoped_refptr
<VideoFrame
> VideoFramePool::PoolImpl::CreateFrame(
57 VideoPixelFormat format
,
58 const gfx::Size
& coded_size
,
59 const gfx::Rect
& visible_rect
,
60 const gfx::Size
& natural_size
,
61 base::TimeDelta timestamp
) {
62 base::AutoLock
auto_lock(lock_
);
63 DCHECK(!is_shutdown_
);
65 scoped_refptr
<VideoFrame
> frame
;
66 while (!frame
.get() && !frames_
.empty()) {
67 scoped_refptr
<VideoFrame
> pool_frame
= frames_
.front();
70 if (pool_frame
->format() == format
&&
71 pool_frame
->coded_size() == coded_size
&&
72 pool_frame
->visible_rect() == visible_rect
&&
73 pool_frame
->natural_size() == natural_size
) {
75 frame
->set_timestamp(timestamp
);
81 frame
= VideoFrame::CreateZeroInitializedFrame(
82 format
, coded_size
, visible_rect
, natural_size
, timestamp
);
85 scoped_refptr
<VideoFrame
> wrapped_frame
= VideoFrame::WrapVideoFrame(
86 frame
, frame
->visible_rect(), frame
->natural_size());
87 wrapped_frame
->AddDestructionObserver(
88 base::Bind(&VideoFramePool::PoolImpl::FrameReleased
, this, frame
));
92 void VideoFramePool::PoolImpl::Shutdown() {
93 base::AutoLock
auto_lock(lock_
);
98 void VideoFramePool::PoolImpl::FrameReleased(
99 const scoped_refptr
<VideoFrame
>& frame
) {
100 base::AutoLock
auto_lock(lock_
);
104 frames_
.push_back(frame
);
107 VideoFramePool::VideoFramePool() : pool_(new PoolImpl()) {
110 VideoFramePool::~VideoFramePool() {
114 scoped_refptr
<VideoFrame
> VideoFramePool::CreateFrame(
115 VideoPixelFormat format
,
116 const gfx::Size
& coded_size
,
117 const gfx::Rect
& visible_rect
,
118 const gfx::Size
& natural_size
,
119 base::TimeDelta timestamp
) {
120 return pool_
->CreateFrame(format
, coded_size
, visible_rect
, natural_size
,
124 size_t VideoFramePool::GetPoolSizeForTesting() const {
125 return pool_
->GetPoolSizeForTesting();