| 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 #include "media/video/default_video_frame_provider.h" |
| 13 | 14 |
| 14 namespace media { | 15 namespace media { |
| 15 | 16 |
| 16 class VideoFramePool::PoolImpl | 17 class VideoFramePool::PoolImpl |
| 17 : public base::RefCountedThreadSafe<VideoFramePool::PoolImpl> { | 18 : public base::RefCountedThreadSafe<VideoFramePool::PoolImpl> { |
| 18 public: | 19 public: |
| 19 PoolImpl(); | 20 PoolImpl(); |
| 20 | 21 |
| 21 // See VideoFramePool::CreateFrame() for usage. | 22 // See VideoFramePool::CreateFrame() for usage. |
| 22 scoped_refptr<VideoFrame> CreateFrame(VideoPixelFormat format, | 23 scoped_refptr<VideoFrame> CreateFrame( |
| 23 const gfx::Size& coded_size, | 24 VideoPixelFormat format, |
| 24 const gfx::Rect& visible_rect, | 25 const gfx::Size& coded_size, |
| 25 const gfx::Size& natural_size, | 26 const gfx::Rect& visible_rect, |
| 26 base::TimeDelta timestamp); | 27 const gfx::Size& natural_size, |
| 28 base::TimeDelta timestamp, |
| 29 VideoFrameProvider* video_frame_provider); |
| 27 | 30 |
| 28 // Shuts down the frame pool and releases all frames in |frames_|. | 31 // Shuts down the frame pool and releases all frames in |frames_|. |
| 29 // Once this is called frames will no longer be inserted back into | 32 // Once this is called frames will no longer be inserted back into |
| 30 // |frames_|. | 33 // |frames_|. |
| 31 void Shutdown(); | 34 void Shutdown(); |
| 32 | 35 |
| 33 size_t GetPoolSizeForTesting() const { return frames_.size(); } | 36 size_t GetPoolSizeForTesting() const { return frames_.size(); } |
| 34 | 37 |
| 35 private: | 38 private: |
| 36 friend class base::RefCountedThreadSafe<VideoFramePool::PoolImpl>; | 39 friend class base::RefCountedThreadSafe<VideoFramePool::PoolImpl>; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 52 | 55 |
| 53 VideoFramePool::PoolImpl::~PoolImpl() { | 56 VideoFramePool::PoolImpl::~PoolImpl() { |
| 54 DCHECK(is_shutdown_); | 57 DCHECK(is_shutdown_); |
| 55 } | 58 } |
| 56 | 59 |
| 57 scoped_refptr<VideoFrame> VideoFramePool::PoolImpl::CreateFrame( | 60 scoped_refptr<VideoFrame> VideoFramePool::PoolImpl::CreateFrame( |
| 58 VideoPixelFormat format, | 61 VideoPixelFormat format, |
| 59 const gfx::Size& coded_size, | 62 const gfx::Size& coded_size, |
| 60 const gfx::Rect& visible_rect, | 63 const gfx::Rect& visible_rect, |
| 61 const gfx::Size& natural_size, | 64 const gfx::Size& natural_size, |
| 62 base::TimeDelta timestamp) { | 65 base::TimeDelta timestamp, |
| 66 VideoFrameProvider* video_frame_provider) { |
| 63 base::AutoLock auto_lock(lock_); | 67 base::AutoLock auto_lock(lock_); |
| 64 DCHECK(!is_shutdown_); | 68 DCHECK(!is_shutdown_); |
| 65 | 69 |
| 66 scoped_refptr<VideoFrame> frame; | 70 scoped_refptr<VideoFrame> frame; |
| 67 while (!frame.get() && !frames_.empty()) { | 71 while (!frame.get() && !frames_.empty()) { |
| 68 scoped_refptr<VideoFrame> pool_frame = frames_.front(); | 72 scoped_refptr<VideoFrame> pool_frame = frames_.front(); |
| 69 frames_.pop_front(); | 73 frames_.pop_front(); |
| 70 | 74 |
| 71 if (pool_frame->format() == format && | 75 if (pool_frame->format() == format && |
| 72 pool_frame->coded_size() == coded_size && | 76 pool_frame->coded_size() == coded_size && |
| 73 pool_frame->visible_rect() == visible_rect && | 77 pool_frame->visible_rect() == visible_rect && |
| 74 pool_frame->natural_size() == natural_size) { | 78 pool_frame->natural_size() == natural_size) { |
| 75 frame = pool_frame; | 79 frame = pool_frame; |
| 76 frame->set_timestamp(timestamp); | 80 frame->set_timestamp(timestamp); |
| 77 frame->metadata()->Clear(); | 81 frame->metadata()->Clear(); |
| 78 break; | 82 break; |
| 79 } | 83 } |
| 80 } | 84 } |
| 81 | 85 |
| 82 if (!frame.get()) { | 86 if (!frame.get()) { |
| 83 frame = VideoFrame::CreateZeroInitializedFrame( | 87 frame = video_frame_provider->CreateZeroInitializedFrame( |
| 84 format, coded_size, visible_rect, natural_size, timestamp); | 88 format, coded_size, visible_rect, natural_size, timestamp); |
| 89 |
| 85 // This can happen if the arguments are not valid. | 90 // This can happen if the arguments are not valid. |
| 86 if (!frame) { | 91 if (!frame) { |
| 87 LOG(ERROR) << "Failed to create a video frame"; | 92 LOG(ERROR) << "Failed to create a video frame"; |
| 88 return nullptr; | 93 return nullptr; |
| 89 } | 94 } |
| 90 } | 95 } |
| 91 | 96 |
| 92 scoped_refptr<VideoFrame> wrapped_frame = VideoFrame::WrapVideoFrame( | 97 scoped_refptr<VideoFrame> wrapped_frame = |
| 93 frame, frame->format(), frame->visible_rect(), frame->natural_size()); | 98 video_frame_provider->WrapVideoFrame(frame); |
| 94 wrapped_frame->AddDestructionObserver( | 99 wrapped_frame->AddDestructionObserver( |
| 95 base::Bind(&VideoFramePool::PoolImpl::FrameReleased, this, frame)); | 100 base::Bind(&VideoFramePool::PoolImpl::FrameReleased, this, frame)); |
| 96 return wrapped_frame; | 101 return wrapped_frame; |
| 97 } | 102 } |
| 98 | 103 |
| 99 void VideoFramePool::PoolImpl::Shutdown() { | 104 void VideoFramePool::PoolImpl::Shutdown() { |
| 100 base::AutoLock auto_lock(lock_); | 105 base::AutoLock auto_lock(lock_); |
| 101 is_shutdown_ = true; | 106 is_shutdown_ = true; |
| 102 frames_.clear(); | 107 frames_.clear(); |
| 103 } | 108 } |
| 104 | 109 |
| 105 void VideoFramePool::PoolImpl::FrameReleased( | 110 void VideoFramePool::PoolImpl::FrameReleased( |
| 106 const scoped_refptr<VideoFrame>& frame) { | 111 const scoped_refptr<VideoFrame>& frame) { |
| 107 base::AutoLock auto_lock(lock_); | 112 base::AutoLock auto_lock(lock_); |
| 108 if (is_shutdown_) | 113 if (is_shutdown_) |
| 109 return; | 114 return; |
| 110 | 115 |
| 111 frames_.push_back(frame); | 116 frames_.push_back(frame); |
| 112 } | 117 } |
| 113 | 118 |
| 114 VideoFramePool::VideoFramePool() : pool_(new PoolImpl()) { | 119 VideoFramePool::VideoFramePool() |
| 115 } | 120 : video_frame_provider_(new DefaultVideoFrameProvider()), |
| 121 pool_(new PoolImpl()) {} |
| 122 |
| 123 VideoFramePool::VideoFramePool( |
| 124 std::unique_ptr<VideoFrameProvider> video_frame_provider) |
| 125 : video_frame_provider_(std::move(video_frame_provider)), |
| 126 pool_(new PoolImpl()) {} |
| 116 | 127 |
| 117 VideoFramePool::~VideoFramePool() { | 128 VideoFramePool::~VideoFramePool() { |
| 118 pool_->Shutdown(); | 129 pool_->Shutdown(); |
| 119 } | 130 } |
| 120 | 131 |
| 121 scoped_refptr<VideoFrame> VideoFramePool::CreateFrame( | 132 scoped_refptr<VideoFrame> VideoFramePool::CreateFrame( |
| 122 VideoPixelFormat format, | 133 VideoPixelFormat format, |
| 123 const gfx::Size& coded_size, | 134 const gfx::Size& coded_size, |
| 124 const gfx::Rect& visible_rect, | 135 const gfx::Rect& visible_rect, |
| 125 const gfx::Size& natural_size, | 136 const gfx::Size& natural_size, |
| 126 base::TimeDelta timestamp) { | 137 base::TimeDelta timestamp) { |
| 127 return pool_->CreateFrame(format, coded_size, visible_rect, natural_size, | 138 return pool_->CreateFrame(format, coded_size, visible_rect, natural_size, |
| 128 timestamp); | 139 timestamp, video_frame_provider_.get()); |
| 129 } | 140 } |
| 130 | 141 |
| 131 size_t VideoFramePool::GetPoolSizeForTesting() const { | 142 size_t VideoFramePool::GetPoolSizeForTesting() const { |
| 132 return pool_->GetPoolSizeForTesting(); | 143 return pool_->GetPoolSizeForTesting(); |
| 133 } | 144 } |
| 134 | 145 |
| 135 } // namespace media | 146 } // namespace media |
| OLD | NEW |