1 /*
2  * Copyright (c) 2024 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 #include "dump_buffer_wrap.h"
17 #include <string>
18 #include <dlfcn.h>
19 #include "log.h"
20 
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FOUNDATION, "HiStreamer"};
23 }
24 
25 namespace OHOS {
26 namespace Media {
27 namespace MediaMonitor {
28 
DumpBufferWrap()29 DumpBufferWrap::DumpBufferWrap()
30 {
31 }
32 
~DumpBufferWrap()33 DumpBufferWrap::~DumpBufferWrap()
34 {
35     Close();
36 }
37 
Open()38 bool DumpBufferWrap::Open()
39 {
40     std::string path = "libmedia_monitor_buffer.z.so";
41     handler = dlopen(path.c_str(), RTLD_NOW);
42     if (handler == nullptr) {
43         return false;
44     }
45 
46     newFunc = (BufferNewFunc)dlsym(handler, "DumpBufferNew");
47     FALSE_RETURN_V_MSG_E(newFunc != nullptr, false, "load error");
48 
49     createFunc = (BufferCreateFunc)dlsym(handler, "DumpBufferCreate");
50     FALSE_RETURN_V_MSG_E(createFunc != nullptr, false, "load error");
51 
52     destroyFunc = (BufferDestroyFunc)dlsym(handler, "DumpBufferDestroy");
53     FALSE_RETURN_V_MSG_E(destroyFunc != nullptr, false, "load error");
54 
55     getAddrFunc = (BufferGetAddrFunc)dlsym(handler, "DumpBufferGetAddr");
56     FALSE_RETURN_V_MSG_E(getAddrFunc != nullptr, false, "load error");
57 
58     getSizeFunc = (BufferGetSizeFunc)dlsym(handler, "DumpBufferGetSize");
59     FALSE_RETURN_V_MSG_E(getSizeFunc != nullptr, false, "load error");
60 
61     setSizeFunc = (BufferSetSizeFunc)dlsym(handler, "DumpBufferSetSize");
62     FALSE_RETURN_V_MSG_E(setSizeFunc != nullptr, false, "load error");
63 
64     getIdFunc = (BufferGetUniIdFunc)dlsym(handler, "DumpBufferGetUniqueId");
65     FALSE_RETURN_V_MSG_E(getIdFunc != nullptr, false, "load error");
66 
67     getCapacityFunc = (BufferGetCapacityFunc)dlsym(handler, "DumpBufferGetCapacity");
68     FALSE_RETURN_V_MSG_E(getCapacityFunc != nullptr, false, "load error");
69 
70     writeFunc = (BufferWriteFunc)dlsym(handler, "DumpBufferWrite");
71     FALSE_RETURN_V_MSG_E(writeFunc != nullptr, false, "load error");
72 
73     readFromParcel = (BufferReadFromParcelFunc)dlsym(handler, "DumpBufferReadFromParcel");
74     FALSE_RETURN_V_MSG_E(readFromParcel != nullptr, false, "load error");
75 
76     writeToParcel = (BufferWriteToParcelFunc)dlsym(handler, "DumpBufferWriteToParcel");
77     FALSE_RETURN_V_MSG_E(writeToParcel != nullptr, false, "load error");
78     return true;
79 }
80 
Close()81 void DumpBufferWrap::Close()
82 {
83     if (handler != nullptr) {
84         dlclose(handler);
85         handler = nullptr;
86     }
87 }
88 
NewDumpBuffer()89 DumpBuffer *DumpBufferWrap::NewDumpBuffer()
90 {
91     DumpBuffer* buffer = nullptr;
92     if (newFunc != nullptr) {
93         buffer = newFunc();
94     }
95     return buffer;
96 }
97 
CreateDumpBuffer(int32_t capacity)98 DumpBuffer *DumpBufferWrap::CreateDumpBuffer(int32_t capacity)
99 {
100     DumpBuffer* buffer = nullptr;
101     if (createFunc != nullptr) {
102         buffer = createFunc(capacity);
103     }
104     return buffer;
105 }
106 
DestroyDumpBuffer(DumpBuffer * buffer)107 void DumpBufferWrap::DestroyDumpBuffer(DumpBuffer *buffer)
108 {
109     if (destroyFunc != nullptr) {
110         destroyFunc(buffer);
111     }
112 }
113 
GetAddr(DumpBuffer * buffer)114 uint8_t *DumpBufferWrap::GetAddr(DumpBuffer *buffer)
115 {
116     uint8_t *addr = nullptr;
117     if (getAddrFunc != nullptr) {
118         addr = getAddrFunc(buffer);
119     }
120     return addr;
121 }
122 
GetCapacity(DumpBuffer * buffer)123 int32_t DumpBufferWrap::GetCapacity(DumpBuffer *buffer)
124 {
125     int32_t size = -1;
126     if (getCapacityFunc != nullptr) {
127         size = getCapacityFunc(buffer);
128     }
129     return size;
130 }
131 
GetSize(DumpBuffer * buffer)132 int32_t DumpBufferWrap::GetSize(DumpBuffer *buffer)
133 {
134     int32_t size = -1;
135     if (getSizeFunc != nullptr) {
136         size = getSizeFunc(buffer);
137     }
138     return size;
139 }
140 
Write(DumpBuffer * buffer,const uint8_t * in,int32_t writeSize)141 int32_t DumpBufferWrap::Write(DumpBuffer *buffer, const uint8_t *in, int32_t writeSize)
142 {
143     int32_t size = -1;
144     if (writeFunc != nullptr) {
145         size = writeFunc(buffer, in, writeSize);
146     }
147     return size;
148 }
149 
GetUniqueId(DumpBuffer * buffer)150 uint64_t DumpBufferWrap::GetUniqueId(DumpBuffer *buffer)
151 {
152     uint64_t id = -1;
153     if (getIdFunc != nullptr) {
154         id = getIdFunc(buffer);
155     }
156     return id;
157 }
158 
SetSize(DumpBuffer * buffer,int32_t size)159 bool DumpBufferWrap::SetSize(DumpBuffer *buffer, int32_t size)
160 {
161     bool ret = false;
162     if (setSizeFunc != nullptr) {
163         ret = setSizeFunc(buffer, size);
164     }
165     return ret;
166 }
167 
ReadFromParcel(DumpBuffer * buffer,void * parcel)168 bool DumpBufferWrap::ReadFromParcel(DumpBuffer *buffer, void *parcel)
169 {
170     bool ret = false;
171     if (readFromParcel != nullptr) {
172         ret = readFromParcel(buffer, parcel);
173     }
174     return ret;
175 }
176 
WriteToParcel(DumpBuffer * buffer,void * parcel)177 bool DumpBufferWrap::WriteToParcel(DumpBuffer *buffer, void *parcel)
178 {
179     bool ret = false;
180     if (writeToParcel != nullptr) {
181         ret = writeToParcel(buffer, parcel);
182     }
183     return ret;
184 }
185 
186 } // namespace MediaMonitor
187 } // namespace Media
188 } // namespace OHOS