OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/base/video_frame_pool.h" | 5 #include "media/base/video_frame_pool.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
13 | 13 |
14 namespace media { | 14 namespace media { |
15 | 15 |
16 class VideoFramePool::PoolImpl | 16 class VideoFramePool::PoolImpl |
17 : public base::RefCountedThreadSafe<VideoFramePool::PoolImpl> { | 17 : public base::RefCountedThreadSafe<VideoFramePool::PoolImpl> { |
18 public: | 18 public: |
19 PoolImpl(); | 19 PoolImpl(); |
20 | 20 |
21 // See VideoFramePool::CreateFrame() for usage. | 21 // See VideoFramePool::CreateFrame() for usage. |
22 scoped_refptr<VideoFrame> CreateFrame(VideoPixelFormat format, | 22 scoped_refptr<VideoFrame> CreateFrame(VideoPixelFormat format, |
23 const gfx::Size& coded_size, | 23 const gfx::Size& coded_size, |
24 const gfx::Rect& visible_rect, | 24 const gfx::Rect& visible_rect, |
25 const gfx::Size& natural_size, | 25 const gfx::Size& natural_size, |
26 base::TimeDelta timestamp); | 26 base::TimeDelta timestamp, |
27 VideoFramePoolDelegate* delegate); | |
27 | 28 |
28 // Shuts down the frame pool and releases all frames in |frames_|. | 29 // Shuts down the frame pool and releases all frames in |frames_|. |
29 // Once this is called frames will no longer be inserted back into | 30 // Once this is called frames will no longer be inserted back into |
30 // |frames_|. | 31 // |frames_|. |
31 void Shutdown(); | 32 void Shutdown(); |
32 | 33 |
33 size_t GetPoolSizeForTesting() const { return frames_.size(); } | 34 size_t GetPoolSizeForTesting() const { return frames_.size(); } |
34 | 35 |
35 private: | 36 private: |
36 friend class base::RefCountedThreadSafe<VideoFramePool::PoolImpl>; | 37 friend class base::RefCountedThreadSafe<VideoFramePool::PoolImpl>; |
(...skipping 15 matching lines...) Expand all Loading... | |
52 | 53 |
53 VideoFramePool::PoolImpl::~PoolImpl() { | 54 VideoFramePool::PoolImpl::~PoolImpl() { |
54 DCHECK(is_shutdown_); | 55 DCHECK(is_shutdown_); |
55 } | 56 } |
56 | 57 |
57 scoped_refptr<VideoFrame> VideoFramePool::PoolImpl::CreateFrame( | 58 scoped_refptr<VideoFrame> VideoFramePool::PoolImpl::CreateFrame( |
58 VideoPixelFormat format, | 59 VideoPixelFormat format, |
59 const gfx::Size& coded_size, | 60 const gfx::Size& coded_size, |
60 const gfx::Rect& visible_rect, | 61 const gfx::Rect& visible_rect, |
61 const gfx::Size& natural_size, | 62 const gfx::Size& natural_size, |
62 base::TimeDelta timestamp) { | 63 base::TimeDelta timestamp, |
64 VideoFramePoolDelegate* delegate) { | |
63 base::AutoLock auto_lock(lock_); | 65 base::AutoLock auto_lock(lock_); |
64 DCHECK(!is_shutdown_); | 66 DCHECK(!is_shutdown_); |
65 | 67 |
66 scoped_refptr<VideoFrame> frame; | 68 scoped_refptr<VideoFrame> frame; |
67 while (!frame.get() && !frames_.empty()) { | 69 while (!frame.get() && !frames_.empty()) { |
68 scoped_refptr<VideoFrame> pool_frame = frames_.front(); | 70 scoped_refptr<VideoFrame> pool_frame = frames_.front(); |
69 frames_.pop_front(); | 71 frames_.pop_front(); |
70 | 72 |
71 if (pool_frame->format() == format && | 73 if (pool_frame->format() == format && |
72 pool_frame->coded_size() == coded_size && | 74 pool_frame->coded_size() == coded_size && |
73 pool_frame->visible_rect() == visible_rect && | 75 pool_frame->visible_rect() == visible_rect && |
74 pool_frame->natural_size() == natural_size) { | 76 pool_frame->natural_size() == natural_size) { |
75 frame = pool_frame; | 77 frame = pool_frame; |
76 frame->set_timestamp(timestamp); | 78 frame->set_timestamp(timestamp); |
77 frame->metadata()->Clear(); | 79 frame->metadata()->Clear(); |
78 break; | 80 break; |
79 } | 81 } |
80 } | 82 } |
81 | 83 |
82 if (!frame.get()) { | 84 if (!frame.get()) { |
83 frame = VideoFrame::CreateZeroInitializedFrame( | 85 if (delegate) { |
84 format, coded_size, visible_rect, natural_size, timestamp); | 86 frame = delegate->CreateZeroInitializedFrame( |
87 format, coded_size, visible_rect, natural_size, timestamp); | |
88 } else { | |
sandersd (OOO until July 31)
2016/12/15 00:38:01
These conditions can be removed by creating a defa
| |
89 frame = VideoFrame::CreateZeroInitializedFrame( | |
90 format, coded_size, visible_rect, natural_size, timestamp); | |
91 } | |
92 | |
85 // This can happen if the arguments are not valid. | 93 // This can happen if the arguments are not valid. |
86 if (!frame) { | 94 if (!frame) { |
87 LOG(ERROR) << "Failed to create a video frame"; | 95 LOG(ERROR) << "Failed to create a video frame"; |
88 return nullptr; | 96 return nullptr; |
89 } | 97 } |
90 } | 98 } |
91 | 99 |
92 scoped_refptr<VideoFrame> wrapped_frame = VideoFrame::WrapVideoFrame( | 100 scoped_refptr<VideoFrame> wrapped_frame; |
93 frame, frame->format(), frame->visible_rect(), frame->natural_size()); | 101 if (delegate) { |
102 wrapped_frame = delegate->WrapVideoFrame(frame); | |
103 } else { | |
104 wrapped_frame = VideoFrame::WrapVideoFrame( | |
105 frame, frame->format(), frame->visible_rect(), frame->natural_size()); | |
106 } | |
107 | |
94 wrapped_frame->AddDestructionObserver( | 108 wrapped_frame->AddDestructionObserver( |
95 base::Bind(&VideoFramePool::PoolImpl::FrameReleased, this, frame)); | 109 base::Bind(&VideoFramePool::PoolImpl::FrameReleased, this, frame)); |
96 return wrapped_frame; | 110 return wrapped_frame; |
97 } | 111 } |
98 | 112 |
99 void VideoFramePool::PoolImpl::Shutdown() { | 113 void VideoFramePool::PoolImpl::Shutdown() { |
100 base::AutoLock auto_lock(lock_); | 114 base::AutoLock auto_lock(lock_); |
101 is_shutdown_ = true; | 115 is_shutdown_ = true; |
102 frames_.clear(); | 116 frames_.clear(); |
103 } | 117 } |
104 | 118 |
105 void VideoFramePool::PoolImpl::FrameReleased( | 119 void VideoFramePool::PoolImpl::FrameReleased( |
106 const scoped_refptr<VideoFrame>& frame) { | 120 const scoped_refptr<VideoFrame>& frame) { |
107 base::AutoLock auto_lock(lock_); | 121 base::AutoLock auto_lock(lock_); |
108 if (is_shutdown_) | 122 if (is_shutdown_) |
109 return; | 123 return; |
110 | 124 |
111 frames_.push_back(frame); | 125 frames_.push_back(frame); |
112 } | 126 } |
113 | 127 |
114 VideoFramePool::VideoFramePool() : pool_(new PoolImpl()) { | 128 VideoFramePool::VideoFramePool() : pool_(new PoolImpl()), delegate_(nullptr) {} |
115 } | |
116 | 129 |
117 VideoFramePool::~VideoFramePool() { | 130 VideoFramePool::~VideoFramePool() { |
118 pool_->Shutdown(); | 131 pool_->Shutdown(); |
119 } | 132 } |
120 | 133 |
121 scoped_refptr<VideoFrame> VideoFramePool::CreateFrame( | 134 scoped_refptr<VideoFrame> VideoFramePool::CreateFrame( |
122 VideoPixelFormat format, | 135 VideoPixelFormat format, |
123 const gfx::Size& coded_size, | 136 const gfx::Size& coded_size, |
124 const gfx::Rect& visible_rect, | 137 const gfx::Rect& visible_rect, |
125 const gfx::Size& natural_size, | 138 const gfx::Size& natural_size, |
126 base::TimeDelta timestamp) { | 139 base::TimeDelta timestamp) { |
127 return pool_->CreateFrame(format, coded_size, visible_rect, natural_size, | 140 return pool_->CreateFrame(format, coded_size, visible_rect, natural_size, |
128 timestamp); | 141 timestamp, delegate_.get()); |
142 } | |
143 | |
144 void VideoFramePool::SetDelegate( | |
145 std::unique_ptr<VideoFramePoolDelegate> delegate) { | |
146 delegate_ = std::move(delegate); | |
129 } | 147 } |
130 | 148 |
131 size_t VideoFramePool::GetPoolSizeForTesting() const { | 149 size_t VideoFramePool::GetPoolSizeForTesting() const { |
132 return pool_->GetPoolSizeForTesting(); | 150 return pool_->GetPoolSizeForTesting(); |
133 } | 151 } |
134 | 152 |
135 } // namespace media | 153 } // namespace media |
OLD | NEW |