1 /* 2 * Copyright (C) 2023-2025 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_AV_TRANSPORT_SHARED_MEMORY_H 17 #define OHOS_AV_TRANSPORT_SHARED_MEMORY_H 18 19 #include <memory> 20 #include <string> 21 #include <securec.h> 22 #include "parcel.h" 23 #include "message_parcel.h" 24 25 namespace OHOS { 26 namespace DistributedHardware { 27 constexpr uint8_t INVALID_VALUE_FALG = 0; 28 constexpr uint32_t MAX_CLOCK_UNIT_COUNT = 50; 29 constexpr uint32_t DEFAULT_INVALID_FRAME_NUM = 0; 30 constexpr size_t NUM_ZERO = 0; 31 constexpr size_t NUM_FOUR = 4; 32 constexpr size_t NUM_EIGHT = 8; 33 34 struct AVTransSharedMemory { 35 int32_t fd; 36 int32_t size; 37 std::string name; 38 void* addr; 39 }; 40 41 struct AVTransSharedMemoryExt : public AVTransSharedMemory, public Parcelable { 42 using AVTransSharedMemory::AVTransSharedMemory; AVTransSharedMemoryExtAVTransSharedMemoryExt43 explicit AVTransSharedMemoryExt() {} ~AVTransSharedMemoryExtAVTransSharedMemoryExt44 virtual ~AVTransSharedMemoryExt() 45 { 46 if (addr != nullptr) { 47 free(addr); 48 addr = nullptr; 49 } 50 } AVTransSharedMemoryExtAVTransSharedMemoryExt51 explicit AVTransSharedMemoryExt(const AVTransSharedMemory& avTransSharedMemory) 52 { 53 fd = avTransSharedMemory.fd; 54 size = avTransSharedMemory.size; 55 name = avTransSharedMemory.name; 56 addr = (char*)malloc(size); 57 if (addr) { 58 auto ret = memcpy_s(addr, size, avTransSharedMemory.addr, size); 59 if (ret != EOK) { 60 free(addr); 61 addr = nullptr; 62 } 63 } else { 64 addr = nullptr; 65 } 66 } MarshallingAVTransSharedMemoryExt67 virtual bool Marshalling(Parcel &parcel) const override 68 { 69 MessageParcel &messageParcel = static_cast<MessageParcel&>(parcel); 70 if (!messageParcel.WriteFileDescriptor(fd)) { 71 return false; 72 } 73 if (!messageParcel.WriteInt32(size)) { 74 return false; 75 } 76 if (!messageParcel.WriteString(name)) { 77 return false; 78 } 79 return true; 80 } 81 UnmarshallingAVTransSharedMemoryExt82 static AVTransSharedMemoryExt *Unmarshalling(Parcel &parcel) 83 { 84 MessageParcel &messageParcel = static_cast<MessageParcel&>(parcel); 85 AVTransSharedMemoryExt *avTransSharedMemory = new (std::nothrow) AVTransSharedMemoryExt(); 86 if (avTransSharedMemory == nullptr) { 87 return nullptr; 88 } 89 avTransSharedMemory->fd = messageParcel.ReadFileDescriptor(); 90 avTransSharedMemory->size = messageParcel.ReadInt32(); 91 avTransSharedMemory->name = messageParcel.ReadString(); 92 return avTransSharedMemory; 93 } 94 }; 95 96 struct AVSyncClockUnit { 97 uint32_t index; 98 uint32_t frameNum; 99 int64_t pts; 100 }; 101 102 /** 103 * @brief create shared memory space for av sync. 104 * @param name name for the shared memory. 105 * @return shared memory struct, include fd, size and name. 106 */ 107 AVTransSharedMemory CreateAVTransSharedMemory(const std::string &name, size_t size); 108 109 /** 110 * @brief close shared memory space. 111 * @param memory shared memory. 112 */ 113 void CloseAVTransSharedMemory(AVTransSharedMemory &memory) noexcept; 114 115 /** 116 * @brief write the clock unit into the shared memory space. 117 * @param memory shared memory 118 * @param clockUnit the clock unit 119 * @return Returns DH_AVT_SUCCESS(0) if successful, otherwise returns other error code. 120 */ 121 int32_t WriteClockUnitToMemory(const AVTransSharedMemory &memory, AVSyncClockUnit &clockUnit); 122 123 /** 124 * @brief read clock unit from the shared memory space. 125 * @param memory shared memory 126 * @param clockUnit the clock unit 127 * @return Returns DH_AVT_SUCCESS(0) if successful, otherwise returns other error code. 128 */ 129 int32_t ReadClockUnitFromMemory(const AVTransSharedMemory &memory, AVSyncClockUnit &clockUnit); 130 131 /** 132 * @brief write frame number and pts into the shared memory space. 133 * @param memory shared memory 134 * @param frameNum the frame number 135 * @param timestamp the pts 136 * @return Returns DH_AVT_SUCCESS(0) if successful, otherwise returns other error code. 137 */ 138 int32_t WriteFrameInfoToMemory(const AVTransSharedMemory &memory, uint32_t frameNum, int64_t timestamp); 139 140 /** 141 * @brief read frame number and pts from the shared memory space. 142 * @param memory shared memory 143 * @param frameNum the frame number 144 * @param timestamp the pts 145 * @return Returns DH_AVT_SUCCESS(0) if successful, otherwise returns other error code. 146 */ 147 int32_t ReadFrameInfoFromMemory(const AVTransSharedMemory &memory, uint32_t &frameNum, int64_t ×tamp); 148 149 /** 150 * @brief reset the shared memory value to all zeros. 151 * @param memory shared memory 152 * @return Returns DH_AVT_SUCCESS(0) if successful, otherwise returns other error code. 153 */ 154 int32_t ResetSharedMemory(const AVTransSharedMemory &memory); 155 156 bool IsInValidSharedMemory(const AVTransSharedMemory &memory); 157 bool IsInValidClockUnit(const AVSyncClockUnit &clockUnit); 158 159 std::string MarshalSharedMemory(const AVTransSharedMemory &memory); 160 AVTransSharedMemory UnmarshalSharedMemory(const std::string &jsonStr); 161 162 uint32_t U8ToU32(const uint8_t *arrayPtr, size_t arraySize); 163 uint64_t U8ToU64(const uint8_t *arrayPtr, size_t arraySize); 164 void U32ToU8(uint8_t *arrayPtr, uint32_t value, size_t arraySize); 165 void U64ToU8(uint8_t *arrayPtr, uint64_t value, size_t arraySize); 166 } // namespace DistributedHardware 167 } // namespace OHOS 168 #endif // OHOS_AV_TRANSPORT_SHARED_MEMORY_H