jrummell | 550e1c05 | 2016-02-17 21:37:55 | [diff] [blame] | 1 | // Copyright 2016 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 <stdint.h> |
danakj | 4d43bc26 | 2016-04-26 03:36:04 | [diff] [blame] | 6 | #include <memory> |
jrummell | 550e1c05 | 2016-02-17 21:37:55 | [diff] [blame] | 7 | |
| 8 | #include "base/macros.h" |
| 9 | #include "media/base/video_frame.h" |
| 10 | #include "media/cdm/api/content_decryption_module.h" |
| 11 | #include "media/cdm/cdm_helpers.h" |
| 12 | #include "media/cdm/simple_cdm_allocator.h" |
| 13 | #include "testing/gmock/include/gmock/gmock.h" |
| 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | namespace media { |
| 17 | |
| 18 | class TestCdmBuffer : public cdm::Buffer { |
| 19 | public: |
| 20 | static TestCdmBuffer* Create(uint32_t capacity) { |
| 21 | return new TestCdmBuffer(capacity); |
| 22 | } |
| 23 | |
| 24 | // cdm::Buffer implementation. |
Daniel Cheng | 9f0b701 | 2018-04-26 21:09:05 | [diff] [blame] | 25 | void Destroy() override { |
jrummell | 550e1c05 | 2016-02-17 21:37:55 | [diff] [blame] | 26 | DestroyCalled(); |
| 27 | delete this; |
| 28 | } |
Daniel Cheng | 9f0b701 | 2018-04-26 21:09:05 | [diff] [blame] | 29 | uint32_t Capacity() const override { return buffer_.size(); } |
| 30 | uint8_t* Data() override { return buffer_.data(); } |
| 31 | void SetSize(uint32_t size) override { size_ = size > Capacity() ? 0 : size; } |
| 32 | uint32_t Size() const override { return size_; } |
jrummell | 550e1c05 | 2016-02-17 21:37:55 | [diff] [blame] | 33 | |
| 34 | private: |
| 35 | TestCdmBuffer(uint32_t capacity) : buffer_(capacity), size_(0) { |
| 36 | // Verify that Destroy() is called on this object. |
| 37 | EXPECT_CALL(*this, DestroyCalled()); |
| 38 | } |
Chris Watkins | 2de6929 | 2017-12-01 03:08:01 | [diff] [blame] | 39 | ~TestCdmBuffer() final = default; |
jrummell | 550e1c05 | 2016-02-17 21:37:55 | [diff] [blame] | 40 | |
| 41 | MOCK_METHOD0(DestroyCalled, void()); |
| 42 | |
| 43 | std::vector<uint8_t> buffer_; |
| 44 | uint32_t size_; |
| 45 | |
| 46 | DISALLOW_COPY_AND_ASSIGN(TestCdmBuffer); |
| 47 | }; |
| 48 | |
| 49 | class SimpleCdmAllocatorTest : public testing::Test { |
| 50 | public: |
Chris Watkins | 2de6929 | 2017-12-01 03:08:01 | [diff] [blame] | 51 | SimpleCdmAllocatorTest() = default; |
| 52 | ~SimpleCdmAllocatorTest() override = default; |
jrummell | 550e1c05 | 2016-02-17 21:37:55 | [diff] [blame] | 53 | |
| 54 | protected: |
| 55 | SimpleCdmAllocator allocator_; |
| 56 | |
| 57 | private: |
| 58 | DISALLOW_COPY_AND_ASSIGN(SimpleCdmAllocatorTest); |
| 59 | }; |
| 60 | |
| 61 | TEST_F(SimpleCdmAllocatorTest, CreateCdmBuffer) { |
| 62 | cdm::Buffer* buffer = allocator_.CreateCdmBuffer(100); |
| 63 | EXPECT_GE(buffer->Capacity(), 100u); |
| 64 | buffer->Destroy(); |
| 65 | } |
| 66 | |
| 67 | TEST_F(SimpleCdmAllocatorTest, CreateCdmVideoFrame) { |
danakj | 4d43bc26 | 2016-04-26 03:36:04 | [diff] [blame] | 68 | std::unique_ptr<VideoFrameImpl> video_frame = |
| 69 | allocator_.CreateCdmVideoFrame(); |
jrummell | 550e1c05 | 2016-02-17 21:37:55 | [diff] [blame] | 70 | EXPECT_EQ(video_frame->FrameBuffer(), nullptr); |
| 71 | video_frame->SetFrameBuffer(TestCdmBuffer::Create(100)); |
| 72 | EXPECT_NE(video_frame->FrameBuffer(), nullptr); |
| 73 | |
| 74 | // Releasing |video_frame| should free the cdm::Buffer created above |
| 75 | // (verified by the mock method TestCdmBuffer::DestroyCalled). |
| 76 | video_frame.reset(); |
| 77 | } |
| 78 | |
| 79 | TEST_F(SimpleCdmAllocatorTest, TransformToVideoFrame) { |
| 80 | // For this test we need to pretend we have valid video data. So create |
| 81 | // a small video frame of size 2x2. |
| 82 | gfx::Size size(2, 2); |
Miguel Casas | 9e776602 | 2018-01-08 16:13:13 | [diff] [blame] | 83 | size_t memory_needed = VideoFrame::AllocationSize(PIXEL_FORMAT_I420, size); |
jrummell | 550e1c05 | 2016-02-17 21:37:55 | [diff] [blame] | 84 | |
| 85 | // Now create a VideoFrameImpl. |
danakj | 4d43bc26 | 2016-04-26 03:36:04 | [diff] [blame] | 86 | std::unique_ptr<VideoFrameImpl> video_frame = |
| 87 | allocator_.CreateCdmVideoFrame(); |
jrummell | 550e1c05 | 2016-02-17 21:37:55 | [diff] [blame] | 88 | EXPECT_EQ(video_frame->FrameBuffer(), nullptr); |
| 89 | |
| 90 | // Fill VideoFrameImpl as if it was a small video frame. |
| 91 | video_frame->SetFormat(cdm::kI420); |
John Rummell | 6444def | 2018-01-31 22:06:04 | [diff] [blame] | 92 | video_frame->SetSize({size.width(), size.height()}); |
jrummell | 550e1c05 | 2016-02-17 21:37:55 | [diff] [blame] | 93 | video_frame->SetFrameBuffer(TestCdmBuffer::Create(memory_needed)); |
| 94 | video_frame->FrameBuffer()->SetSize(memory_needed); |
| 95 | |
| 96 | // Now transform VideoFrameImpl to a VideoFrame, and make sure that |
| 97 | // FrameBuffer() is transferred to the new object. |
| 98 | EXPECT_NE(video_frame->FrameBuffer(), nullptr); |
| 99 | scoped_refptr<media::VideoFrame> transformed_frame = |
| 100 | video_frame->TransformToVideoFrame(size); |
| 101 | EXPECT_EQ(video_frame->FrameBuffer(), nullptr); |
| 102 | |
| 103 | // Releasing |transformed_frame| should free the cdm::Buffer created above |
| 104 | // (verified by the mock method TestCdmBuffer::DestroyCalled). |
| 105 | transformed_frame = nullptr; |
| 106 | } |
| 107 | |
| 108 | } // namespace media |