1fa7767c5Sopenharmony_ci/* 2fa7767c5Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd. 3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License. 5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at 6fa7767c5Sopenharmony_ci * 7fa7767c5Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fa7767c5Sopenharmony_ci * 9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and 13fa7767c5Sopenharmony_ci * limitations under the License. 14fa7767c5Sopenharmony_ci */ 15fa7767c5Sopenharmony_ci 16fa7767c5Sopenharmony_ci#ifndef OHOS_MEDIA_AV_SHARED_MEMORY_BASE_H 17fa7767c5Sopenharmony_ci#define OHOS_MEDIA_AV_SHARED_MEMORY_BASE_H 18fa7767c5Sopenharmony_ci 19fa7767c5Sopenharmony_ci#include <string> 20fa7767c5Sopenharmony_ci#include "buffer/avsharedmemory.h" 21fa7767c5Sopenharmony_ci#include "nocopyable.h" 22fa7767c5Sopenharmony_ci 23fa7767c5Sopenharmony_cinamespace OHOS { 24fa7767c5Sopenharmony_cinamespace Media { 25fa7767c5Sopenharmony_ciclass __attribute__((visibility("default"))) AVSharedMemoryBase : public AVSharedMemory, public NoCopyable { 26fa7767c5Sopenharmony_cipublic: 27fa7767c5Sopenharmony_ci /** 28fa7767c5Sopenharmony_ci * @brief Construct a new AVSharedMemoryBase object. This function should only be used in the 29fa7767c5Sopenharmony_ci * local process. 30fa7767c5Sopenharmony_ci * 31fa7767c5Sopenharmony_ci * @param size the memory's size, bytes. 32fa7767c5Sopenharmony_ci * @param flags the memory's accessible flags, refer to {@AVSharedMemory::Flags}. 33fa7767c5Sopenharmony_ci * @param name the debug string 34fa7767c5Sopenharmony_ci */ 35fa7767c5Sopenharmony_ci static std::shared_ptr<AVSharedMemory> CreateFromLocal(int32_t size, uint32_t flags, const std::string &name); 36fa7767c5Sopenharmony_ci 37fa7767c5Sopenharmony_ci /** 38fa7767c5Sopenharmony_ci * @brief Construct a new AVSharedMemoryBase object. This function should only be used in the 39fa7767c5Sopenharmony_ci * remote process. 40fa7767c5Sopenharmony_ci * 41fa7767c5Sopenharmony_ci * @param fd the memory's fd 42fa7767c5Sopenharmony_ci * @param size the memory's size, bytes. 43fa7767c5Sopenharmony_ci * @param flags the memory's accessible flags, refer to {@AVSharedMemory::Flags}. 44fa7767c5Sopenharmony_ci * @param name the debug string 45fa7767c5Sopenharmony_ci */ 46fa7767c5Sopenharmony_ci static std::shared_ptr<AVSharedMemory> CreateFromRemote(int32_t fd, int32_t size, uint32_t flags, 47fa7767c5Sopenharmony_ci const std::string &name); 48fa7767c5Sopenharmony_ci 49fa7767c5Sopenharmony_ci ~AVSharedMemoryBase(); 50fa7767c5Sopenharmony_ci 51fa7767c5Sopenharmony_ci /** 52fa7767c5Sopenharmony_ci * @brief Construct a new AVSharedMemoryBase object. This function should only be used in the 53fa7767c5Sopenharmony_ci * local process. 54fa7767c5Sopenharmony_ci * 55fa7767c5Sopenharmony_ci * @param size the memory's size, bytes. 56fa7767c5Sopenharmony_ci * @param flags the memory's accessible flags, refer to {@AVSharedMemory::Flags}. 57fa7767c5Sopenharmony_ci * @param name the debug string 58fa7767c5Sopenharmony_ci */ 59fa7767c5Sopenharmony_ci AVSharedMemoryBase(int32_t size, uint32_t flags, const std::string &name); 60fa7767c5Sopenharmony_ci 61fa7767c5Sopenharmony_ci /** 62fa7767c5Sopenharmony_ci * @brief Intialize the memory. Call this interface firstly before the other interface. 63fa7767c5Sopenharmony_ci * @param isMapVirAddr the memory's map virtual address flag, the default value is equal to true. 64fa7767c5Sopenharmony_ci * @return Status::OK if success, otherwise the errcode. 65fa7767c5Sopenharmony_ci */ 66fa7767c5Sopenharmony_ci int32_t Init(bool isMapVirAddr = true); 67fa7767c5Sopenharmony_ci 68fa7767c5Sopenharmony_ci /** 69fa7767c5Sopenharmony_ci * @brief Get the memory's fd, which only valid when the underlying memory 70fa7767c5Sopenharmony_ci * chunk is allocated through the ashmem. 71fa7767c5Sopenharmony_ci * @return the memory's fd if the memory is allocated through the ashmem, otherwise -1. 72fa7767c5Sopenharmony_ci */ 73fa7767c5Sopenharmony_ci int32_t GetFd() const 74fa7767c5Sopenharmony_ci { 75fa7767c5Sopenharmony_ci return fd_; 76fa7767c5Sopenharmony_ci } 77fa7767c5Sopenharmony_ci 78fa7767c5Sopenharmony_ci std::string GetName() const 79fa7767c5Sopenharmony_ci { 80fa7767c5Sopenharmony_ci return name_; 81fa7767c5Sopenharmony_ci } 82fa7767c5Sopenharmony_ci 83fa7767c5Sopenharmony_ci int32_t Write(const uint8_t *in, int32_t writeSize, int32_t position = INVALID_POSITION); 84fa7767c5Sopenharmony_ci 85fa7767c5Sopenharmony_ci int32_t Read(uint8_t *out, int32_t readSize, int32_t position = INVALID_POSITION); 86fa7767c5Sopenharmony_ci 87fa7767c5Sopenharmony_ci int32_t GetUsedSize() const; 88fa7767c5Sopenharmony_ci 89fa7767c5Sopenharmony_ci void ClearUsedSize(); 90fa7767c5Sopenharmony_ci 91fa7767c5Sopenharmony_ci /** 92fa7767c5Sopenharmony_ci * @brief Get the memory's virtual address 93fa7767c5Sopenharmony_ci * @return the memory's virtual address if the memory is valid, otherwise nullptr. 94fa7767c5Sopenharmony_ci */ 95fa7767c5Sopenharmony_ci virtual uint8_t *GetBase() const override 96fa7767c5Sopenharmony_ci { 97fa7767c5Sopenharmony_ci return base_; 98fa7767c5Sopenharmony_ci } 99fa7767c5Sopenharmony_ci 100fa7767c5Sopenharmony_ci /** 101fa7767c5Sopenharmony_ci * @brief Get the memory's size 102fa7767c5Sopenharmony_ci * @return the memory's size if the memory is valid, otherwise -1. 103fa7767c5Sopenharmony_ci */ 104fa7767c5Sopenharmony_ci virtual int32_t GetSize() const override 105fa7767c5Sopenharmony_ci { 106fa7767c5Sopenharmony_ci return (base_ != nullptr) ? capacity_ : -1; 107fa7767c5Sopenharmony_ci } 108fa7767c5Sopenharmony_ci 109fa7767c5Sopenharmony_ci /** 110fa7767c5Sopenharmony_ci * @brief Get the memory's flags set by the creator, refer to {@Flags} 111fa7767c5Sopenharmony_ci * @return the memory's flags if the memory is valid, otherwise 0. 112fa7767c5Sopenharmony_ci */ 113fa7767c5Sopenharmony_ci virtual uint32_t GetFlags() const final 114fa7767c5Sopenharmony_ci { 115fa7767c5Sopenharmony_ci return (base_ != nullptr) ? flags_ : 0; 116fa7767c5Sopenharmony_ci } 117fa7767c5Sopenharmony_ci 118fa7767c5Sopenharmony_ciprotected: 119fa7767c5Sopenharmony_ci AVSharedMemoryBase(int32_t fd, int32_t size, uint32_t flags, const std::string &name); 120fa7767c5Sopenharmony_ci 121fa7767c5Sopenharmony_ciprivate: 122fa7767c5Sopenharmony_ci int32_t MapMemory(bool isRemote); 123fa7767c5Sopenharmony_ci void Close() noexcept; 124fa7767c5Sopenharmony_ci 125fa7767c5Sopenharmony_ci uint8_t *base_; 126fa7767c5Sopenharmony_ci int32_t capacity_; 127fa7767c5Sopenharmony_ci uint32_t flags_; 128fa7767c5Sopenharmony_ci std::string name_; 129fa7767c5Sopenharmony_ci int32_t fd_; 130fa7767c5Sopenharmony_ci int32_t size_; 131fa7767c5Sopenharmony_ci static constexpr int32_t INVALID_POSITION = -1; 132fa7767c5Sopenharmony_ci}; 133fa7767c5Sopenharmony_ci} // namespace Media 134fa7767c5Sopenharmony_ci} // namespace OHOS 135fa7767c5Sopenharmony_ci 136fa7767c5Sopenharmony_ci#endif