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_H
17 #define OHOS_MEDIA_AV_SHARED_MEMORY_H
18 
19 #ifndef MEDIA_NO_OHOS
20 #ifndef MEDIA_OHOS
21 #define MEDIA_OHOS
22 #endif
23 #else
24 #ifdef MEDIA_OHOS
25 #undef MEDIA_OHOS
26 #endif
27 #endif
28 
29 #include <memory>
30 #include <string>
31 
32 namespace OHOS {
33 namespace Media {
34 /**
35  * @brief Provides a unified interface to implement convenient memory sharing
36  * mechanism. For those platforms that do not support multi-process, it may
37  * simply encapsulate ordinary memory blocks, not really multi-process shareable memory.
38  */
39 class __attribute__((visibility("default"))) AVSharedMemory {
40 public:
41     virtual ~AVSharedMemory() = default;
42 
43     /**
44      * @brief Enumerates the flag bits used to create a new shared memory.
45      */
46     enum Flags : uint32_t {
47         /**
48          * This flag bit indicates that the remote process is allowed to read and write
49          * the shared memory. If no flags are specified, this is the default memory
50          * sharing policy. If the FLAGS_READ_ONLY bit is set, this flag bit is ignored.
51          */
52         FLAGS_READ_WRITE = 0x1,
53         /**
54          * For platforms that support multiple processes, this flag bit indicates that the
55          * remote process can only read data in the shared memory. If this flag is not set,
56          * the remote process has both read and write permissions by default. Adding this
57          * flag does not affect the process that creates the memory, which always has the
58          * read and write permission on the shared memory. For platforms that do not support
59          * multi-processes, the memory read and write permission control capability may
60          * not be available. In this case, this flag is invalid.
61          */
62         FLAGS_READ_ONLY = 0x2,
63     };
64 
65     /**
66      * @brief Get the memory's virtual address
67      * @return the memory's virtual address if the memory is valid, otherwise nullptr.
68      */
69     virtual uint8_t *GetBase() const = 0;
70 
71     /**
72      * @brief Get the memory's size
73      * @return the memory's size if the memory is valid, otherwise -1.
74      */
75     virtual int32_t GetSize() const = 0;
76 
77     /**
78      * @brief Get the memory's flags set by the creator, refer to {@Flags}
79      * @return the memory's flags if the memory is valid, otherwise 0.
80      */
81     virtual uint32_t GetFlags() const = 0;
82 
83     /**
84      * @brief Identifies different AVSharedMemory
85      * @return unique SharedMemory ID
86      */
GetSharedMemoryID()87     virtual uint64_t GetSharedMemoryID() const
88     {
89         return uniqueSharedMemoryID_;
90     }
91 
92 public:
93     uint64_t uniqueSharedMemoryID_;
94 };
95 } // namespace Media
96 } // namespace OHOS
97 #endif // AVSHAREDMEMORY_H