OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "media/base/video_frame_pool.h" |
| 6 #include "testing/gmock/include/gmock/gmock.h" |
| 7 |
| 8 namespace media { |
| 9 |
| 10 class VideoFramePoolTest : public ::testing::Test { |
| 11 public: |
| 12 VideoFramePoolTest() : pool_(new VideoFramePool()) {} |
| 13 |
| 14 scoped_refptr<VideoFrame> CreateFrame(VideoFrame::Format format, |
| 15 int timestamp_ms) { |
| 16 gfx::Size coded_size(320,240); |
| 17 gfx::Rect visible_rect(coded_size); |
| 18 gfx::Size natural_size(coded_size); |
| 19 return pool_->CreateFrame( |
| 20 format, coded_size, visible_rect, natural_size, |
| 21 base::TimeDelta::FromMilliseconds(timestamp_ms)); |
| 22 } |
| 23 |
| 24 void CheckPoolSize(size_t size) const { |
| 25 EXPECT_EQ(size, pool_->GetPoolSizeForTesting()); |
| 26 } |
| 27 |
| 28 protected: |
| 29 scoped_ptr<VideoFramePool> pool_; |
| 30 }; |
| 31 |
| 32 TEST_F(VideoFramePoolTest, SimpleFrameReuse) { |
| 33 scoped_refptr<VideoFrame> frame = CreateFrame(VideoFrame::YV12, 10); |
| 34 const uint8* old_y_data = frame->data(VideoFrame::kYPlane); |
| 35 |
| 36 // Clear frame reference to return the frame to the pool. |
| 37 frame = NULL; |
| 38 |
| 39 // Verify that the next frame from the pool uses the same memory. |
| 40 scoped_refptr<VideoFrame> new_frame = CreateFrame(VideoFrame::YV12, 10); |
| 41 EXPECT_EQ(old_y_data, new_frame->data(VideoFrame::kYPlane)); |
| 42 } |
| 43 |
| 44 TEST_F(VideoFramePoolTest, SimpleFormatChange) { |
| 45 scoped_refptr<VideoFrame> frame_a = CreateFrame(VideoFrame::YV12, 10); |
| 46 scoped_refptr<VideoFrame> frame_b = CreateFrame(VideoFrame::YV12, 10); |
| 47 |
| 48 // Clear frame references to return the frames to the pool. |
| 49 frame_a = NULL; |
| 50 frame_b = NULL; |
| 51 |
| 52 // Verify that both frames are in the pool. |
| 53 CheckPoolSize(2u); |
| 54 |
| 55 // Verify that requesting a frame with a different format causes the pool |
| 56 // to get drained. |
| 57 scoped_refptr<VideoFrame> new_frame = CreateFrame(VideoFrame::YV12A, 10); |
| 58 CheckPoolSize(0u); |
| 59 } |
| 60 |
| 61 TEST_F(VideoFramePoolTest, FrameValidAfterPoolDestruction) { |
| 62 scoped_refptr<VideoFrame> frame = CreateFrame(VideoFrame::YV12, 10); |
| 63 |
| 64 // Destroy the pool. |
| 65 pool_.reset(); |
| 66 |
| 67 // Write to the Y plane. The memory tools should detect a |
| 68 // use-after-free if the storage was actually removed by pool destruction. |
| 69 memset(frame->data(VideoFrame::kYPlane), 0xff, |
| 70 frame->rows(VideoFrame::kYPlane) * frame->stride(VideoFrame::kYPlane)); |
| 71 } |
| 72 |
| 73 } // namespace media |
OLD | NEW |