blob: 5f9d1d77fe19a566c81c1ed3dbb20c9100edb16d [file] [log] [blame]
jrummell550e1c052016-02-17 21:37:551// 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>
danakj4d43bc262016-04-26 03:36:046#include <memory>
jrummell550e1c052016-02-17 21:37:557
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
16namespace media {
17
18class 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 Cheng9f0b7012018-04-26 21:09:0525 void Destroy() override {
jrummell550e1c052016-02-17 21:37:5526 DestroyCalled();
27 delete this;
28 }
Daniel Cheng9f0b7012018-04-26 21:09:0529 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_; }
jrummell550e1c052016-02-17 21:37:5533
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 Watkins2de69292017-12-01 03:08:0139 ~TestCdmBuffer() final = default;
jrummell550e1c052016-02-17 21:37:5540
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
49class SimpleCdmAllocatorTest : public testing::Test {
50 public:
Chris Watkins2de69292017-12-01 03:08:0151 SimpleCdmAllocatorTest() = default;
52 ~SimpleCdmAllocatorTest() override = default;
jrummell550e1c052016-02-17 21:37:5553
54 protected:
55 SimpleCdmAllocator allocator_;
56
57 private:
58 DISALLOW_COPY_AND_ASSIGN(SimpleCdmAllocatorTest);
59};
60
61TEST_F(SimpleCdmAllocatorTest, CreateCdmBuffer) {
62 cdm::Buffer* buffer = allocator_.CreateCdmBuffer(100);
63 EXPECT_GE(buffer->Capacity(), 100u);
64 buffer->Destroy();
65}
66
67TEST_F(SimpleCdmAllocatorTest, CreateCdmVideoFrame) {
danakj4d43bc262016-04-26 03:36:0468 std::unique_ptr<VideoFrameImpl> video_frame =
69 allocator_.CreateCdmVideoFrame();
jrummell550e1c052016-02-17 21:37:5570 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
79TEST_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 Casas9e7766022018-01-08 16:13:1383 size_t memory_needed = VideoFrame::AllocationSize(PIXEL_FORMAT_I420, size);
jrummell550e1c052016-02-17 21:37:5584
85 // Now create a VideoFrameImpl.
danakj4d43bc262016-04-26 03:36:0486 std::unique_ptr<VideoFrameImpl> video_frame =
87 allocator_.CreateCdmVideoFrame();
jrummell550e1c052016-02-17 21:37:5588 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 Rummell6444def2018-01-31 22:06:0492 video_frame->SetSize({size.width(), size.height()});
jrummell550e1c052016-02-17 21:37:5593 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