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 #include <dlfcn.h>
16 #include "avcodec_log.h"
17 #include "avcodec_errors.h"
18 #include "hcodec_loader.h"
19
20 namespace OHOS {
21 namespace MediaAVCodec {
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "codecLoader"};
24 const char *HCODEC_CREATE_FUNC_NAME = "CreateHCodecByName";
25 const char *HCODEC_GETCAPS_FUNC_NAME = "GetHCodecCapabilityList";
26 const char *HCODEC_LIB_PATH = "libhcodec.z.so";
27 } // namespace
CreateByName(const std::string & name)28 std::shared_ptr<CodecBase> HCodecLoader::CreateByName(const std::string &name)
29 {
30 HCodecLoader &loader = GetInstance();
31 CHECK_AND_RETURN_RET_LOG(loader.Init() == AVCS_ERR_OK, nullptr, "Create codec by name failed: init error");
32 return loader.Create(name);
33 }
34
GetCapabilityList(std::vector<CapabilityData> & caps)35 int32_t HCodecLoader::GetCapabilityList(std::vector<CapabilityData> &caps)
36 {
37 HCodecLoader &loader = GetInstance();
38 CHECK_AND_RETURN_RET_LOG(loader.Init() == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Get capability failed: init error");
39 return loader.GetCaps(caps);
40 }
41
GetInstance()42 HCodecLoader &HCodecLoader::GetInstance()
43 {
44 static HCodecLoader loader;
45 return loader;
46 }
47
Init()48 int32_t HCodecLoader::Init()
49 {
50 if (codecHandle_ != nullptr) {
51 return AVCS_ERR_OK;
52 }
53 void *handle = dlopen(HCODEC_LIB_PATH, RTLD_LAZY);
54 CHECK_AND_RETURN_RET_LOG(handle != nullptr, AVCS_ERR_UNKNOWN, "Load codec failed: %{public}s", HCODEC_LIB_PATH);
55 auto handleSP = std::shared_ptr<void>(handle, dlclose);
56 auto createFunc = reinterpret_cast<CreateByNameFuncType>(dlsym(handle, HCODEC_CREATE_FUNC_NAME));
57 CHECK_AND_RETURN_RET_LOG(createFunc != nullptr, AVCS_ERR_UNKNOWN, "Load createFunc failed: %{public}s",
58 HCODEC_CREATE_FUNC_NAME);
59 auto getCapsFunc = reinterpret_cast<GetCapabilityFuncType>(dlsym(handle, HCODEC_GETCAPS_FUNC_NAME));
60 CHECK_AND_RETURN_RET_LOG(getCapsFunc != nullptr, AVCS_ERR_UNKNOWN, "Load getCapsFunc failed: %{public}s",
61 HCODEC_GETCAPS_FUNC_NAME);
62 codecHandle_ = handleSP;
63 createFunc_ = createFunc;
64 getCapsFunc_ = getCapsFunc;
65 return AVCS_ERR_OK;
66 }
67
Create(const std::string & name)68 std::shared_ptr<CodecBase> HCodecLoader::Create(const std::string &name)
69 {
70 std::shared_ptr<CodecBase> codec;
71 createFunc_(name, codec);
72 return codec;
73 }
74
GetCaps(std::vector<CapabilityData> & caps)75 int32_t HCodecLoader::GetCaps(std::vector<CapabilityData> &caps)
76 {
77 return getCapsFunc_(caps);
78 }
79 } // namespace MediaAVCodec
80 } // namespace OHOS