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 8namespace media { 9 10class 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 20 scoped_refptr<VideoFrame> frame = 21 pool_->CreateFrame( 22 format, coded_size, visible_rect, natural_size, 23 base::TimeDelta::FromMilliseconds(timestamp_ms)); 24 EXPECT_EQ(format, frame->format()); 25 EXPECT_EQ(base::TimeDelta::FromMilliseconds(timestamp_ms), 26 frame->timestamp()); 27 EXPECT_EQ(coded_size, frame->coded_size()); 28 EXPECT_EQ(visible_rect, frame->visible_rect()); 29 EXPECT_EQ(natural_size, frame->natural_size()); 30 31 return frame; 32 } 33 34 void CheckPoolSize(size_t size) const { 35 EXPECT_EQ(size, pool_->GetPoolSizeForTesting()); 36 } 37 38 protected: 39 scoped_ptr<VideoFramePool> pool_; 40}; 41 42TEST_F(VideoFramePoolTest, SimpleFrameReuse) { 43 scoped_refptr<VideoFrame> frame = CreateFrame(VideoFrame::YV12, 10); 44 const uint8* old_y_data = frame->data(VideoFrame::kYPlane); 45 46 // Clear frame reference to return the frame to the pool. 47 frame = NULL; 48 49 // Verify that the next frame from the pool uses the same memory. 50 scoped_refptr<VideoFrame> new_frame = CreateFrame(VideoFrame::YV12, 20); 51 EXPECT_EQ(old_y_data, new_frame->data(VideoFrame::kYPlane)); 52} 53 54TEST_F(VideoFramePoolTest, SimpleFormatChange) { 55 scoped_refptr<VideoFrame> frame_a = CreateFrame(VideoFrame::YV12, 10); 56 scoped_refptr<VideoFrame> frame_b = CreateFrame(VideoFrame::YV12, 10); 57 58 // Clear frame references to return the frames to the pool. 59 frame_a = NULL; 60 frame_b = NULL; 61 62 // Verify that both frames are in the pool. 63 CheckPoolSize(2u); 64 65 // Verify that requesting a frame with a different format causes the pool 66 // to get drained. 67 scoped_refptr<VideoFrame> new_frame = CreateFrame(VideoFrame::YV12A, 10); 68 CheckPoolSize(0u); 69} 70 71TEST_F(VideoFramePoolTest, FrameValidAfterPoolDestruction) { 72 scoped_refptr<VideoFrame> frame = CreateFrame(VideoFrame::YV12, 10); 73 74 // Destroy the pool. 75 pool_.reset(); 76 77 // Write to the Y plane. The memory tools should detect a 78 // use-after-free if the storage was actually removed by pool destruction. 79 memset(frame->data(VideoFrame::kYPlane), 0xff, 80 frame->rows(VideoFrame::kYPlane) * frame->stride(VideoFrame::kYPlane)); 81} 82 83} // namespace media 84