1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_MEDIA_AV_SHARED_MEMORY_BASE_H
17 #define OHOS_MEDIA_AV_SHARED_MEMORY_BASE_H
18 
19 #include <string>
20 #include "buffer/avsharedmemory.h"
21 #include "nocopyable.h"
22 
23 namespace OHOS {
24 namespace Media {
25 class __attribute__((visibility("default"))) AVSharedMemoryBase : public AVSharedMemory, public NoCopyable {
26 public:
27     /**
28      * @brief Construct a new AVSharedMemoryBase object. This function should only be used in the
29      * local process.
30      *
31      * @param size the memory's size, bytes.
32      * @param flags the memory's accessible flags, refer to {@AVSharedMemory::Flags}.
33      * @param name the debug string
34      */
35     static std::shared_ptr<AVSharedMemory> CreateFromLocal(int32_t size, uint32_t flags, const std::string &name);
36 
37     /**
38      * @brief Construct a new AVSharedMemoryBase object. This function should only be used in the
39      * remote process.
40      *
41      * @param fd the memory's fd
42      * @param size the memory's size, bytes.
43      * @param flags the memory's accessible flags, refer to {@AVSharedMemory::Flags}.
44      * @param name the debug string
45      */
46     static std::shared_ptr<AVSharedMemory> CreateFromRemote(int32_t fd, int32_t size, uint32_t flags,
47                                                             const std::string &name);
48 
49     ~AVSharedMemoryBase();
50 
51     /**
52      * @brief Construct a new AVSharedMemoryBase object. This function should only be used in the
53      * local process.
54      *
55      * @param size the memory's size, bytes.
56      * @param flags the memory's accessible flags, refer to {@AVSharedMemory::Flags}.
57      * @param name the debug string
58      */
59     AVSharedMemoryBase(int32_t size, uint32_t flags, const std::string &name);
60 
61     /**
62      * @brief Intialize the memory. Call this interface firstly before the other interface.
63      * @param isMapVirAddr the memory's map virtual address flag, the default value is equal to true.
64      * @return Status::OK if success, otherwise the errcode.
65      */
66     int32_t Init(bool isMapVirAddr = true);
67 
68     /**
69      * @brief Get the memory's fd, which only valid when the underlying memory
70      * chunk is allocated through the ashmem.
71      * @return the memory's fd if the memory is allocated through the ashmem, otherwise -1.
72      */
GetFd()73     int32_t GetFd() const
74     {
75         return fd_;
76     }
77 
GetName()78     std::string GetName() const
79     {
80         return name_;
81     }
82 
83     int32_t Write(const uint8_t *in, int32_t writeSize, int32_t position = INVALID_POSITION);
84 
85     int32_t Read(uint8_t *out, int32_t readSize, int32_t position = INVALID_POSITION);
86 
87     int32_t GetUsedSize() const;
88 
89     void ClearUsedSize();
90 
91     /**
92      * @brief Get the memory's virtual address
93      * @return the memory's virtual address if the memory is valid, otherwise nullptr.
94      */
GetBase()95     virtual uint8_t *GetBase() const override
96     {
97         return base_;
98     }
99 
100     /**
101      * @brief Get the memory's size
102      * @return the memory's size if the memory is valid, otherwise -1.
103      */
GetSize()104     virtual int32_t GetSize() const override
105     {
106         return (base_ != nullptr) ? capacity_ : -1;
107     }
108 
109     /**
110      * @brief Get the memory's flags set by the creator, refer to {@Flags}
111      * @return the memory's flags if the memory is valid, otherwise 0.
112      */
GetFlags()113     virtual uint32_t GetFlags() const final
114     {
115         return (base_ != nullptr) ? flags_ : 0;
116     }
117 
118 protected:
119     AVSharedMemoryBase(int32_t fd, int32_t size, uint32_t flags, const std::string &name);
120 
121 private:
122     int32_t MapMemory(bool isRemote);
123     void Close() noexcept;
124 
125     uint8_t *base_;
126     int32_t capacity_;
127     uint32_t flags_;
128     std::string name_;
129     int32_t fd_;
130     int32_t size_;
131     static constexpr int32_t INVALID_POSITION = -1;
132 };
133 } // namespace Media
134 } // namespace OHOS
135 
136 #endif