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_AV_TRANSPORT_SHARED_MEMORY_H
17 #define OHOS_AV_TRANSPORT_SHARED_MEMORY_H
18 
19 #include <memory>
20 #include <string>
21 
22 namespace OHOS {
23 namespace DistributedHardware {
24 constexpr uint8_t INVALID_VALUE_FALG = 0;
25 constexpr uint32_t MAX_CLOCK_UNIT_COUNT = 50;
26 constexpr uint32_t DEFAULT_INVALID_FRAME_NUM = 0;
27 
28 struct AVTransSharedMemory {
29     int32_t fd;
30     int32_t size;
31     std::string name;
32 };
33 
34 struct AVSyncClockUnit {
35     uint32_t index;
36     uint32_t frameNum;
37     int64_t pts;
38 };
39 
40 /**
41  * @brief create shared memory space for av sync.
42  * @param name    name for the shared memory.
43  * @return shared memory struct, include fd, size and name.
44  */
45 AVTransSharedMemory CreateAVTransSharedMemory(const std::string &name, size_t size);
46 
47 /**
48  * @brief close shared memory space.
49  * @param memory    shared memory.
50  */
51 void CloseAVTransSharedMemory(const AVTransSharedMemory &memory) noexcept;
52 
53 /**
54  * @brief write the clock unit into the shared memory space.
55  * @param memory       shared memory
56  * @param clockUnit    the clock unit
57  * @return Returns DH_AVT_SUCCESS(0) if successful, otherwise returns other error code.
58  */
59 int32_t WriteClockUnitToMemory(const AVTransSharedMemory &memory, AVSyncClockUnit &clockUnit);
60 
61 /**
62  * @brief read clock unit from the shared memory space.
63  * @param memory       shared memory
64  * @param clockUnit    the clock unit
65  * @return Returns DH_AVT_SUCCESS(0) if successful, otherwise returns other error code.
66  */
67 int32_t ReadClockUnitFromMemory(const AVTransSharedMemory &memory, AVSyncClockUnit &clockUnit);
68 
69 /**
70  * @brief write frame number and pts into the shared memory space.
71  * @param memory       shared memory
72  * @param frameNum     the frame number
73  * @param timestamp    the pts
74  * @return Returns DH_AVT_SUCCESS(0) if successful, otherwise returns other error code.
75  */
76 int32_t WriteFrameInfoToMemory(const AVTransSharedMemory &memory, uint32_t frameNum, int64_t timestamp);
77 
78 /**
79  * @brief read frame number and pts from the shared memory space.
80  * @param memory       shared memory
81  * @param frameNum     the frame number
82  * @param timestamp    the pts
83  * @return Returns DH_AVT_SUCCESS(0) if successful, otherwise returns other error code.
84  */
85 int32_t ReadFrameInfoFromMemory(const AVTransSharedMemory &memory, uint32_t &frameNum, int64_t &timestamp);
86 
87 /**
88  * @brief reset the shared memory value to all zeros.
89  * @param memory       shared memory
90  * @return Returns DH_AVT_SUCCESS(0) if successful, otherwise returns other error code.
91  */
92 int32_t ResetSharedMemory(const AVTransSharedMemory &memory);
93 
94 bool IsInValidSharedMemory(const AVTransSharedMemory &memory);
95 bool IsInValidClockUnit(const AVSyncClockUnit &clockUnit);
96 
97 std::string MarshalSharedMemory(const AVTransSharedMemory &memory);
98 AVTransSharedMemory UnmarshalSharedMemory(const std::string &jsonStr);
99 
100 uint32_t U8ToU32(const uint8_t *ptr);
101 uint64_t U8ToU64(const uint8_t *ptr);
102 void U32ToU8(uint8_t *ptr, uint32_t value);
103 void U64ToU8(uint8_t *ptr, uint64_t value);
104 } // namespace DistributedHardware
105 } // namespace OHOS
106 #endif // OHOS_AV_TRANSPORT_SHARED_MEMORY_H