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 #include "histreamer_query_tool.h"
17
18 #include <dlfcn.h>
19 #include<malloc.h>
20
21 #include "distributed_hardware_log.h"
22
23 namespace OHOS {
24 namespace DistributedHardware {
25 IMPLEMENT_SINGLE_INSTANCE(HiStreamerQueryTool);
26 using QueryAudioEncoderFunc = int32_t (*)(char*);
27 using QueryAudioDecoderFunc = int32_t (*)(char*);
28 using QueryVideoEncoderFunc = int32_t (*)(char*);
29 using QueryVideoDecoderFunc = int32_t (*)(char*);
30
31 QueryAudioEncoderFunc queryAudioEncoderFunc = nullptr;
32 QueryAudioDecoderFunc queryAudioDecoderFunc = nullptr;
33 QueryVideoEncoderFunc queryVideoEncoderFunc = nullptr;
34 QueryVideoDecoderFunc queryVideoDecoderFunc = nullptr;
35
36 const std::string QueryAudioEncoderFuncName = "QueryAudioEncoderAbilityStr";
37 const std::string QueryAudioDecoderFuncName = "QueryAudioDecoderAbilityStr";
38 const std::string QueryVideoEncoderFuncName = "QueryVideoEncoderAbilityStr";
39 const std::string QueryVideoDecoderFuncName = "QueryVideoDecoderAbilityStr";
40
41 const uint32_t MAX_MESSAGES_LEN = 1 * 1024 * 1024;
42
43 #ifdef __LP64__
44 const std::string LIB_LOAD_PATH = "/system/lib64/libhistreamer_ability_querier.z.so";
45 #else
46 const std::string LIB_LOAD_PATH = "/system/lib/libhistreamer_ability_querier.z.so";
47 #endif
48
Init()49 void HiStreamerQueryTool::Init()
50 {
51 if (isInit) {
52 return;
53 }
54 DHLOGI("Start Init HiStreamer Query SO");
55 void *pHandler = dlopen(LIB_LOAD_PATH.c_str(), RTLD_LAZY | RTLD_NODELETE);
56 if (pHandler == nullptr) {
57 DHLOGE("libhistreamer_ability_querier.z.so handler load failed, failed reason : %s", dlerror());
58 return;
59 }
60
61 queryAudioEncoderFunc = (QueryAudioEncoderFunc)dlsym(pHandler,
62 QueryAudioEncoderFuncName.c_str());
63 if (queryAudioEncoderFunc == nullptr) {
64 DHLOGE("get QueryAudioEncoderAbilityStr is null, failed reason : %s", dlerror());
65 dlclose(pHandler);
66 return;
67 }
68
69 queryAudioDecoderFunc = (QueryAudioDecoderFunc)dlsym(pHandler,
70 QueryAudioDecoderFuncName.c_str());
71 if (queryAudioDecoderFunc == nullptr) {
72 DHLOGE("get QueryAudioDecoderAbilityStr is null, failed reason : %s", dlerror());
73 dlclose(pHandler);
74 return;
75 }
76
77 queryVideoEncoderFunc = (QueryVideoEncoderFunc)dlsym(pHandler,
78 QueryVideoEncoderFuncName.c_str());
79 if (queryVideoEncoderFunc == nullptr) {
80 DHLOGE("get QueryVideoEncoderAbilityStr is null, failed reason : %s", dlerror());
81 dlclose(pHandler);
82 return;
83 }
84
85 queryVideoDecoderFunc = (QueryVideoDecoderFunc)dlsym(pHandler,
86 QueryVideoDecoderFuncName.c_str());
87 if (queryVideoDecoderFunc == nullptr) {
88 DHLOGE("get QueryVideoDecoderAbilityStr is null, failed reason : %s", dlerror());
89 dlclose(pHandler);
90 return;
91 }
92
93 DHLOGI("Init Query HiStreamer Tool Success");
94 isInit = true;
95 }
96
QueryHiStreamerPluginInfo(HISTREAM_PLUGIN_TYPE type)97 std::string HiStreamerQueryTool::QueryHiStreamerPluginInfo(HISTREAM_PLUGIN_TYPE type)
98 {
99 Init();
100 if (!isInit || queryAudioEncoderFunc == nullptr || queryAudioDecoderFunc == nullptr ||
101 queryVideoEncoderFunc == nullptr || queryVideoDecoderFunc == nullptr) {
102 DHLOGE("Query HiStreamer Tool Init failed");
103 return "";
104 }
105
106 int32_t len = 0;
107 char* res = reinterpret_cast<char *>(malloc(MAX_MESSAGES_LEN));
108 if (res == nullptr) {
109 DHLOGE("Malloc memory failed");
110 return "";
111 }
112 switch (type) {
113 case HISTREAM_PLUGIN_TYPE::AUDIO_ENCODER: {
114 len = queryAudioEncoderFunc(res);
115 break;
116 }
117 case HISTREAM_PLUGIN_TYPE::AUDIO_DECODER: {
118 len = queryAudioDecoderFunc(res);
119 break;
120 }
121 case HISTREAM_PLUGIN_TYPE::VIDEO_ENCODER: {
122 len = queryVideoEncoderFunc(res);
123 break;
124 }
125 case HISTREAM_PLUGIN_TYPE::VIDEO_DECODER: {
126 len = queryVideoDecoderFunc(res);
127 break;
128 }
129 default:
130 break;
131 }
132
133 std::string result(res, len);
134 free(res);
135 res = nullptr;
136 return result;
137 }
138 }
139 }