1 /* 2 * Copyright (C) 2021 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 AVSHAREDMEMORYBASE_H 17 #define AVSHAREDMEMORYBASE_H 18 19 #include <string> 20 #include "nocopyable.h" 21 #include "avsharedmemory.h" 22 23 namespace OHOS { 24 namespace Media { 25 class __attribute__((visibility("default"))) AVSharedMemoryBase 26 : public AVSharedMemory, public NoCopyable { 27 public: 28 /** 29 * @brief Construct a new AVSharedMemoryBase 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 AVSharedMemoryBase 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 ~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 {@AVSharedMemory::Flags}. 59 * @param name the debug string 60 */ 61 AVSharedMemoryBase(int32_t size, uint32_t flags, const std::string &name); 62 63 /** 64 * @brief Intialize the memory. Call this interface firstly before the other interface. 65 * @return MSERR_OK if success, otherwise the errcode. 66 */ 67 int32_t Init(); 68 69 /** 70 * @brief Get the memory's fd, which only valid when the underlying memory 71 * chunk is allocated through the ashmem. 72 * @return the memory's fd if the memory is allocated through the ashmem, otherwise -1. 73 */ GetFd()74 int32_t GetFd() const 75 { 76 return fd_; 77 } 78 GetName()79 std::string GetName() const 80 { 81 return name_; 82 } 83 84 /** 85 * @brief Get the memory's virtual address 86 * @return the memory's virtual address if the memory is valid, otherwise nullptr. 87 */ GetBase()88 virtual uint8_t *GetBase() const override 89 { 90 return base_; 91 } 92 93 /** 94 * @brief Get the memory's size 95 * @return the memory's size if the memory is valid, otherwise -1. 96 */ GetSize()97 virtual int32_t GetSize() const override 98 { 99 return (base_ != nullptr) ? size_ : -1; 100 } 101 102 /** 103 * @brief Get the memory's flags set by the creator, refer to {@Flags} 104 * @return the memory's flags if the memory is valid, otherwise 0. 105 */ GetFlags()106 virtual uint32_t GetFlags() const final 107 { 108 return (base_ != nullptr) ? flags_ : 0; 109 } 110 111 protected: 112 AVSharedMemoryBase(int32_t fd, int32_t size, uint32_t flags, const std::string &name); 113 114 private: 115 int32_t MapMemory(bool isRemote); 116 void Close() noexcept; 117 118 uint8_t *base_; 119 int32_t size_; 120 uint32_t flags_; 121 std::string name_; 122 int32_t fd_; 123 }; 124 } // namespace Media 125 } // namespace OHOS 126 127 #endif