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 AVDATASRCMEMORY_H
17 #define AVDATASRCMEMORY_H
18 
19 #include <string>
20 #include "nocopyable.h"
21 #include "buffer/avsharedmemorybase.h"
22 
23 namespace OHOS {
24 namespace Media {
25 class __attribute__((visibility("default"))) AVDataSrcMemory
26     : public AVSharedMemoryBase {
27 public:
28     /**
29      * @brief Construct a new AVDataSrcMemory object. This function should only be used in the
30      * local process.
31      *
32      * @param size the memory's size, bytes.
33      * @param flags the memory's accessible flags, refer to {@AVSharedMemory::Flags}.
34      * @param name the debug string
35      */
36     static std::shared_ptr<AVSharedMemory> CreateFromLocal(
37         int32_t size, uint32_t flags, const std::string &name);
38 
39     /**
40      * @brief Construct a new AVDataSrcMemory object. This function should only be used in the
41      * remote process.
42      *
43      * @param fd the memory's fd
44      * @param size the memory's size, bytes.
45      * @param flags the memory's accessible flags, refer to {@AVSharedMemory::Flags}.
46      * @param name the debug string
47      */
48     static std::shared_ptr<AVSharedMemory> CreateFromRemote(
49         int32_t fd, int32_t size, uint32_t flags, const std::string &name);
50 
51     virtual ~AVDataSrcMemory();
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 {@AVSharedMemory::Flags}.
59      * @param name the debug string
60      */
61     AVDataSrcMemory(int32_t size, uint32_t flags, const std::string &name);
62 
63     /**
64      * @brief Get the memory's virtual address
65      * @return the memory's virtual address if the memory is valid, otherwise nullptr.
66      */
GetBase()67     virtual uint8_t *GetBase() const override
68     {
69         return AVSharedMemoryBase::GetBase() + offset_;
70     }
71 
GetInnerBase()72     uint8_t *GetInnerBase() const
73     {
74         return AVSharedMemoryBase::GetBase();
75     }
76 
SetOffset(uint32_t offset)77     void SetOffset(uint32_t offset)
78     {
79         offset_ = offset;
80     }
81 
GetOffset()82     uint32_t GetOffset() const
83     {
84         return offset_;
85     }
86 protected:
87     AVDataSrcMemory(int32_t fd, int32_t size, uint32_t flags, const std::string &name);
88 private:
89     uint32_t offset_;
90 };
91 } // namespace Media
92 } // namespace OHOS
93 #endif