simple_cdm_allocator_unittest.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include <stdint.h>
  5. #include <memory>
  6. #include "media/base/video_frame.h"
  7. #include "media/cdm/api/content_decryption_module.h"
  8. #include "media/cdm/cdm_helpers.h"
  9. #include "media/cdm/simple_cdm_allocator.h"
  10. #include "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace media {
  13. class TestCdmBuffer final : public cdm::Buffer {
  14. public:
  15. static TestCdmBuffer* Create(uint32_t capacity) {
  16. return new TestCdmBuffer(capacity);
  17. }
  18. TestCdmBuffer(const TestCdmBuffer&) = delete;
  19. TestCdmBuffer& operator=(const TestCdmBuffer&) = delete;
  20. // cdm::Buffer implementation.
  21. void Destroy() override {
  22. DestroyCalled();
  23. delete this;
  24. }
  25. uint32_t Capacity() const override { return buffer_.size(); }
  26. uint8_t* Data() override { return buffer_.data(); }
  27. void SetSize(uint32_t size) override { size_ = size > Capacity() ? 0 : size; }
  28. uint32_t Size() const override { return size_; }
  29. private:
  30. TestCdmBuffer(uint32_t capacity) : buffer_(capacity), size_(0) {
  31. // Verify that Destroy() is called on this object.
  32. EXPECT_CALL(*this, DestroyCalled());
  33. }
  34. ~TestCdmBuffer() override = default;
  35. MOCK_METHOD0(DestroyCalled, void());
  36. std::vector<uint8_t> buffer_;
  37. uint32_t size_;
  38. };
  39. class SimpleCdmAllocatorTest : public testing::Test {
  40. public:
  41. SimpleCdmAllocatorTest() = default;
  42. SimpleCdmAllocatorTest(const SimpleCdmAllocatorTest&) = delete;
  43. SimpleCdmAllocatorTest& operator=(const SimpleCdmAllocatorTest&) = delete;
  44. ~SimpleCdmAllocatorTest() override = default;
  45. protected:
  46. SimpleCdmAllocator allocator_;
  47. };
  48. TEST_F(SimpleCdmAllocatorTest, CreateCdmBuffer) {
  49. cdm::Buffer* buffer = allocator_.CreateCdmBuffer(100);
  50. EXPECT_GE(buffer->Capacity(), 100u);
  51. buffer->Destroy();
  52. }
  53. TEST_F(SimpleCdmAllocatorTest, CreateCdmVideoFrame) {
  54. std::unique_ptr<VideoFrameImpl> video_frame =
  55. allocator_.CreateCdmVideoFrame();
  56. EXPECT_EQ(video_frame->FrameBuffer(), nullptr);
  57. video_frame->SetFrameBuffer(TestCdmBuffer::Create(100));
  58. EXPECT_NE(video_frame->FrameBuffer(), nullptr);
  59. // Releasing |video_frame| should free the cdm::Buffer created above
  60. // (verified by the mock method TestCdmBuffer::DestroyCalled).
  61. video_frame.reset();
  62. }
  63. TEST_F(SimpleCdmAllocatorTest, TransformToVideoFrame) {
  64. // For this test we need to pretend we have valid video data. So create
  65. // a small video frame of size 2x2.
  66. gfx::Size size(2, 2);
  67. size_t memory_needed = VideoFrame::AllocationSize(PIXEL_FORMAT_I420, size);
  68. // Now create a VideoFrameImpl.
  69. std::unique_ptr<VideoFrameImpl> video_frame =
  70. allocator_.CreateCdmVideoFrame();
  71. EXPECT_EQ(video_frame->FrameBuffer(), nullptr);
  72. // Fill VideoFrameImpl as if it was a small video frame.
  73. video_frame->SetFormat(cdm::kI420);
  74. video_frame->SetSize({size.width(), size.height()});
  75. video_frame->SetFrameBuffer(TestCdmBuffer::Create(memory_needed));
  76. video_frame->FrameBuffer()->SetSize(memory_needed);
  77. // Now transform VideoFrameImpl to a VideoFrame, and make sure that
  78. // FrameBuffer() is transferred to the new object.
  79. EXPECT_NE(video_frame->FrameBuffer(), nullptr);
  80. scoped_refptr<media::VideoFrame> transformed_frame =
  81. video_frame->TransformToVideoFrame(size);
  82. EXPECT_EQ(video_frame->FrameBuffer(), nullptr);
  83. // Releasing |transformed_frame| should free the cdm::Buffer created above
  84. // (verified by the mock method TestCdmBuffer::DestroyCalled).
  85. transformed_frame = nullptr;
  86. }
  87. } // namespace media