[M120 Migration][MM][CAPI] Fix the logic for media using capi player.
[platform/framework/web/chromium-efl.git] / media / mojo / services / mojo_cdm_allocator.h
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.
4
5 #ifndef MEDIA_MOJO_SERVICES_MOJO_CDM_ALLOCATOR_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_CDM_ALLOCATOR_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <map>
12 #include <memory>
13
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"
20
21 namespace media {
22
23 // This is a CdmAllocator that creates buffers using mojo shared memory.
24 class MEDIA_MOJO_EXPORT MojoCdmAllocator final : public CdmAllocator {
25  public:
26   MojoCdmAllocator();
27
28   MojoCdmAllocator(const MojoCdmAllocator&) = delete;
29   MojoCdmAllocator& operator=(const MojoCdmAllocator&) = delete;
30
31   ~MojoCdmAllocator() final;
32
33   // CdmAllocator implementation.
34   cdm::Buffer* CreateCdmBuffer(size_t capacity) final;
35   std::unique_ptr<VideoFrameImpl> CreateCdmVideoFrame() final;
36
37  private:
38   friend class MojoCdmAllocatorTest;
39
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>>;
45
46   // Allocates a shmem region of at least |capacity| bytes.
47   std::unique_ptr<base::MappedReadOnlyRegion> AllocateNewRegion(
48       size_t capacity);
49
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);
54
55   // Returns the base::MappedReadOnlyRegion for a cdm::Buffer allocated by this
56   // class.
57   const base::MappedReadOnlyRegion& GetRegionForTesting(
58       cdm::Buffer* buffer) const;
59
60   // Returns the number of buffers in |available_regions_|.
61   size_t GetAvailableRegionCountForTesting();
62
63   // Map of available, already allocated buffers.
64   AvailableRegionMap available_regions_;
65
66   // Confirms single-threaded access.
67   base::ThreadChecker thread_checker_;
68
69   // NOTE: Weak pointers must be invalidated before all other member variables.
70   base::WeakPtrFactory<MojoCdmAllocator> weak_ptr_factory_{this};
71 };
72
73 }  // namespace media
74
75 #endif  // MEDIA_MOJO_SERVICES_MOJO_CDM_ALLOCATOR_H_