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 "video_codec_loader.h"
16 #include <dlfcn.h>
17 #include "avcodec_errors.h"
18 #include "avcodec_log.h"
19
20 namespace OHOS {
21 namespace MediaAVCodec {
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FRAMEWORK, "VideoCodecLoader"};
24 } // namespace
25
Init()26 int32_t VideoCodecLoader::Init()
27 {
28 if (codecHandle_ != nullptr) {
29 return AVCS_ERR_OK;
30 }
31 void *handle = dlopen(libPath_, RTLD_LAZY);
32 CHECK_AND_RETURN_RET_LOG(handle != nullptr, AVCS_ERR_UNKNOWN, "Load codec failed: %{public}s", libPath_);
33 auto handleSP = std::shared_ptr<void>(handle, dlclose);
34 auto createFunc = reinterpret_cast<CreateByNameFuncType>(dlsym(handle, createFuncName_));
35 CHECK_AND_RETURN_RET_LOG(createFunc != nullptr, AVCS_ERR_UNKNOWN, "Load createFunc failed: %{public}s",
36 createFuncName_);
37 auto getCapsFunc = reinterpret_cast<GetCapabilityFuncType>(dlsym(handle, getCapsFuncName_));
38 CHECK_AND_RETURN_RET_LOG(getCapsFunc != nullptr, AVCS_ERR_UNKNOWN, "Load getCapsFunc failed: %{public}s",
39 getCapsFuncName_);
40 codecHandle_ = handleSP;
41 createFunc_ = createFunc;
42 getCapsFunc_ = getCapsFunc;
43
44 AVCODEC_LOGI("Init library:%{public}s", libPath_);
45 return AVCS_ERR_OK;
46 }
47
Close()48 void VideoCodecLoader::Close()
49 {
50 codecHandle_ = nullptr;
51 createFunc_ = nullptr;
52 getCapsFunc_ = nullptr;
53 AVCODEC_LOGI("Close library:%{public}s", libPath_);
54 }
55
Create(const std::string & name)56 std::shared_ptr<CodecBase> VideoCodecLoader::Create(const std::string &name)
57 {
58 std::shared_ptr<CodecBase> codec;
59 (void)createFunc_(name, codec);
60 return codec;
61 }
62
GetCaps(std::vector<CapabilityData> & caps)63 int32_t VideoCodecLoader::GetCaps(std::vector<CapabilityData> &caps)
64 {
65 return getCapsFunc_(caps);
66 }
67 } // namespace MediaAVCodec
68 } // namespace OHOS