• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 AV_SHARED_MEMORY_BASE_H
17 #define AV_SHARED_MEMORY_BASE_H
18 
19 #include <string>
20 #include "av_shared_memory.h"
21 #include "nocopyable.h"
22 #include "iremote_proxy.h"
23 
24 namespace OHOS {
25 namespace AVSession {
26 class __attribute__((visibility("default"))) AVSharedMemoryBase :
27     public AVSharedMemory, public NoCopyable, public Parcelable {
28 public:
29     /**
30      * @brief Construct a new AVSharedMemoryBase object. This function should only be used in the
31      * local process.
32      *
33      * @param size the memory's size, bytes.
34      * @param flags the memory's accessible flags, refer to {@AVSharedMemoryBase::Flags}.
35      * @param name the debug string
36      */
37     static std::shared_ptr<AVSharedMemoryBase> CreateFromLocal(int32_t size, uint32_t flags, const std::string &name);
38 
39     /**
40      * @brief Construct a new AVSharedMemoryBase object. This function should only be used in the
41      * remote process.
42      *
43      * @param fd the memory's fdInit()
44      * @param size the memory's size, bytes.
45      * @param flags the memory's accessible flags, refer to {@AVSharedMemoryBase::Flags}.
46      * @param name the debug string
47      */
48     static std::shared_ptr<AVSharedMemoryBase> CreateFromRemote(int32_t fd, int32_t size, uint32_t flags,
49                                                             const std::string &name);
50 
51     ~AVSharedMemoryBase();
52 
53     /**
54      * @brief Construct a new AVSharedMemoryBase object. This function should only be used in the
55      * local process.
56      *
57      * @param size the memory's size, bytes.
58      * @param flags the memory's accessible flags, refer to {@AVSharedMemoryBase::Flags}.
59      * @param name the debug string
60      */
61     AVSharedMemoryBase(int32_t size, uint32_t flags, const std::string &name);
62 
63     /**
64      * @brief Construct a new AVSharedMemoryBase object. This function should only be used in the
65      * local process.
66      */
AVSharedMemoryBase()67     AVSharedMemoryBase() {};
68 
69     /**
70      * @brief Intialize the memory. Call this interface firstly before the other interface.
71      * @param isMapVirAddr the memory's map virtual address flag, the default value is equal to true.
72      * @return Status::OK if success, otherwise the errcode.
73      */
74     int32_t Init(bool isMapVirAddr = true);
75 
76     /**
77      * @brief Get the memory's fd, which only valid when the underlying memory
78      * chunk is allocated through the ashmem.
79      * @return the memory's fd if the memory is allocated through the ashmem, otherwise -1.
80      */
GetFd()81     int32_t GetFd() const
82     {
83         return fd_;
84     }
85 
GetName()86     std::string GetName() const
87     {
88         return name_;
89     }
90 
91     int32_t Write(const uint8_t *in, int32_t writeSize, int32_t position = INVALID_POSITION);
92 
93     int32_t Read(uint8_t *out, int32_t readSize, int32_t position = INVALID_POSITION);
94 
95     int32_t GetUsedSize() const;
96 
97     void ClearUsedSize();
98 
99     /**
100      * @brief Get the memory's virtual address
101      * @return the memory's virtual address if the memory is valid, otherwise nullptr.
102      */
GetBase()103     virtual uint8_t *GetBase() const override
104     {
105         return base_;
106     }
107 
108     /**
109      * @brief Get the memory's size
110      * @return the memory's size if the memory is valid, otherwise -1.
111      */
GetSize()112     virtual int32_t GetSize() const override
113     {
114         return (base_ != nullptr) ? capacity_ : -1;
115     }
116 
117     /**
118      * @brief Get the memory's flags set by the creator, refer to {@Flags}
119      * @return the memory's flags if the memory is valid, otherwise 0.
120      */
GetFlags()121     virtual uint32_t GetFlags() const final
122     {
123         return (base_ != nullptr) ? flags_ : 0;
124     }
125 
126     bool Marshalling(Parcel& out) const override;
127     bool WriteToParcel(MessageParcel& out) const;
128     bool ReadFromParcel(MessageParcel& in);
129     static AVSharedMemoryBase* Unmarshalling(Parcel& in);
130 
131 protected:
132     AVSharedMemoryBase(int32_t fd, int32_t size, uint32_t flags, const std::string &name);
133 
134 private:
135     int32_t MapMemory(bool isRemote);
136     void Close() noexcept;
137 
138     uint8_t *base_ = nullptr;
139     int32_t capacity_ = 0;
140     uint32_t flags_ = 0;
141     std::string name_;
142     int32_t fd_ = 0;
143     int32_t size_ = 0;
144     static constexpr int32_t INVALID_POSITION = -1;
145 };
146 } // namespace AVSession
147 } // namespace OHOS
148 
149 #endif // AV_SHARED_MEMORY_BASE_H