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 AVSHAREDMEMORYBASE_H 17 #define AVSHAREDMEMORYBASE_H 18 19 #include <string> 20 #include "nocopyable.h" 21 #include "avsharedmemory.h" 22 23 namespace OHOS { 24 namespace MediaAVCodec { 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 * @param isMapVirAddr the memory's map virtual address flag, the default value is equal to true. 66 * @return AVCS_ERR_OK if success, otherwise the errcode. 67 */ 68 int32_t Init(bool isMapVirAddr = true); 69 70 /** 71 * @brief Get the memory's fd, which only valid when the underlying memory 72 * chunk is allocated through the ashmem. 73 * @return the memory's fd if the memory is allocated through the ashmem, otherwise -1. 74 */ GetFd()75 int32_t GetFd() const 76 { 77 return fd_; 78 } 79 GetName()80 std::string GetName() const 81 { 82 return name_; 83 } 84 85 int32_t Write(const uint8_t *in, int32_t writeSize, int32_t position = INVALID_POSITION); 86 87 int32_t Read(uint8_t *out, int32_t readSize, int32_t position = INVALID_POSITION); 88 89 int32_t GetUsedSize() const; 90 91 void ClearUsedSize(); 92 93 /** 94 * @brief Get the memory's virtual address 95 * @return the memory's virtual address if the memory is valid, otherwise nullptr. 96 */ GetBase()97 virtual uint8_t *GetBase() const override 98 { 99 return base_; 100 } 101 102 /** 103 * @brief Get the memory's size 104 * @return the memory's size if the memory is valid, otherwise -1. 105 */ GetSize()106 virtual int32_t GetSize() const override 107 { 108 return (base_ != nullptr) ? capacity_ : -1; 109 } 110 111 /** 112 * @brief Get the memory's flags set by the creator, refer to {@Flags} 113 * @return the memory's flags if the memory is valid, otherwise 0. 114 */ GetFlags()115 virtual uint32_t GetFlags() const final 116 { 117 return (base_ != nullptr) ? flags_ : 0; 118 } 119 120 protected: 121 AVSharedMemoryBase(int32_t fd, int32_t size, uint32_t flags, const std::string &name); 122 123 private: 124 int32_t MapMemory(bool isRemote); 125 void Close() noexcept; 126 127 uint8_t *base_; 128 int32_t capacity_; 129 uint32_t flags_; 130 std::string name_; 131 int32_t fd_; 132 int32_t size_; 133 static constexpr int32_t INVALID_POSITION = -1; 134 }; 135 } // namespace MediaAVCodec 136 } // namespace OHOS 137 138 #endif