1 /*
2 * Copyright (C) 2025 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 "avc_encoder_loader.h"
16 #include <dlfcn.h>
17 #include "avcodec_errors.h"
18 #include "avcodec_log.h"
19 #include "avc_encoder.h"
20
21 namespace OHOS {
22 namespace MediaAVCodec {
23 namespace {
24 using AvcEncoder = Codec::AvcEncoder;
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FRAMEWORK, "AvcEncoderLoader"};
26 const char *AVC_ENCODER_LIB_PATH = "libavc_encoder.z.so";
27 const char *AVC_ENCODER_CREATE_FUNC_NAME = "CreateAvcEncoderByName";
28 const char *AVC_ENCODER_GETCAPS_FUNC_NAME = "GetAvcEncoderCapabilityList";
29 } // namespace
30
CreateByName(const std::string & name)31 std::shared_ptr<CodecBase> AvcEncoderLoader::CreateByName(const std::string &name)
32 {
33 AvcEncoderLoader &loader = GetInstance();
34
35 std::lock_guard<std::mutex> lock(loader.mutex_);
36 CHECK_AND_RETURN_RET_LOG(loader.Init() == AVCS_ERR_OK, nullptr, "Create codec by name failed: init error");
37 std::shared_ptr<CodecBase> noDeletePtr = loader.Create(name);
38 if (noDeletePtr == nullptr) {
39 AVCODEC_LOGE("Loader create coder by name failed!");
40 loader.CloseLibrary();
41 }
42 return noDeletePtr;
43 }
44
GetCapabilityList(std::vector<CapabilityData> & caps)45 int32_t AvcEncoderLoader::GetCapabilityList(std::vector<CapabilityData> &caps)
46 {
47 AvcEncoderLoader &loader = GetInstance();
48
49 std::lock_guard<std::mutex> lock(loader.mutex_);
50 CHECK_AND_RETURN_RET_LOG(loader.Init() == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Get capability failed: init error");
51 int32_t ret = loader.GetCaps(caps);
52 if (ret != AVCS_ERR_OK) {
53 AVCODEC_LOGE("Loader get caps failed!");
54 loader.CloseLibrary();
55 }
56 return ret;
57 }
58
AvcEncoderLoader()59 AvcEncoderLoader::AvcEncoderLoader() : VideoCodecLoader(AVC_ENCODER_LIB_PATH,
60 AVC_ENCODER_CREATE_FUNC_NAME, AVC_ENCODER_GETCAPS_FUNC_NAME) {}
61
CloseLibrary()62 void AvcEncoderLoader::CloseLibrary()
63 {
64 Close();
65 }
66
GetInstance()67 AvcEncoderLoader &AvcEncoderLoader::GetInstance()
68 {
69 static AvcEncoderLoader loader;
70 return loader;
71 }
72 } // namespace MediaAVCodec
73 } // namespace OHOS