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