1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_MOJO_SERVICES_MOJO_CDM_ALLOCATOR_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_CDM_ALLOCATOR_H_
14 #include "base/memory/read_only_shared_memory_region.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/thread_checker.h"
17 #include "media/cdm/cdm_allocator.h"
18 #include "media/mojo/services/media_mojo_export.h"
19 #include "mojo/public/cpp/system/buffer.h"
23 // This is a CdmAllocator that creates buffers using mojo shared memory.
24 class MEDIA_MOJO_EXPORT MojoCdmAllocator final : public CdmAllocator {
28 MojoCdmAllocator(const MojoCdmAllocator&) = delete;
29 MojoCdmAllocator& operator=(const MojoCdmAllocator&) = delete;
31 ~MojoCdmAllocator() final;
33 // CdmAllocator implementation.
34 cdm::Buffer* CreateCdmBuffer(size_t capacity) final;
35 std::unique_ptr<VideoFrameImpl> CreateCdmVideoFrame() final;
38 friend class MojoCdmAllocatorTest;
40 // Map of available buffers. Done as a mapping of capacity to shmem regions to
41 // make it efficient to find an available buffer of a particular size.
42 // Regions in the map are unmapped.
43 using AvailableRegionMap =
44 std::multimap<size_t, std::unique_ptr<base::MappedReadOnlyRegion>>;
46 // Allocates a shmem region of at least |capacity| bytes.
47 std::unique_ptr<base::MappedReadOnlyRegion> AllocateNewRegion(
50 // Returns |region| to the map of available buffers, ready to be used the
51 // next time CreateCdmBuffer() is called.
52 void AddRegionToAvailableMap(
53 std::unique_ptr<base::MappedReadOnlyRegion> region);
55 // Returns the base::MappedReadOnlyRegion for a cdm::Buffer allocated by this
57 const base::MappedReadOnlyRegion& GetRegionForTesting(
58 cdm::Buffer* buffer) const;
60 // Returns the number of buffers in |available_regions_|.
61 size_t GetAvailableRegionCountForTesting();
63 // Map of available, already allocated buffers.
64 AvailableRegionMap available_regions_;
66 // Confirms single-threaded access.
67 base::ThreadChecker thread_checker_;
69 // NOTE: Weak pointers must be invalidated before all other member variables.
70 base::WeakPtrFactory<MojoCdmAllocator> weak_ptr_factory_{this};
75 #endif // MEDIA_MOJO_SERVICES_MOJO_CDM_ALLOCATOR_H_