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 "fcodec_loader.h"
16 #include <dlfcn.h>
17 #include "avcodec_errors.h"
18 #include "avcodec_log.h"
19 #include "fcodec.h"
20
21 namespace OHOS {
22 namespace MediaAVCodec {
23 namespace {
24 using FCodec = Codec::FCodec;
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FRAMEWORK, "FCodecLoader"};
26 const char *FCODEC_LIB_PATH = "libfcodec.z.so";
27 const char *FCODEC_CREATE_FUNC_NAME = "CreateFCodecByName";
28 const char *FCODEC_GETCAPS_FUNC_NAME = "GetFCodecCapabilityList";
29
30 } // namespace
CreateByName(const std::string & name)31 std::shared_ptr<CodecBase> FCodecLoader::CreateByName(const std::string &name)
32 {
33 FCodecLoader &loader = GetInstance();
34
35 CodecBase *noDeleterPtr = nullptr;
36 {
37 std::lock_guard<std::mutex> lock(loader.mutex_);
38 CHECK_AND_RETURN_RET_LOG(loader.Init() == AVCS_ERR_OK, nullptr, "Create codec by name failed: init error");
39 noDeleterPtr = loader.Create(name).get();
40 CHECK_AND_RETURN_RET_LOG(noDeleterPtr != nullptr, nullptr, "Create fcodec by name failed: no memory");
41 ++(loader.fcodecCount_);
42 }
43 auto deleter = [&loader](CodecBase *ptr) {
44 std::lock_guard<std::mutex> lock(loader.mutex_);
45 FCodec *codec = reinterpret_cast<FCodec*>(ptr);
46 codec->DecStrongRef(codec);
47 --(loader.fcodecCount_);
48 loader.CloseLibrary();
49 };
50 return std::shared_ptr<CodecBase>(noDeleterPtr, deleter);
51 }
52
GetCapabilityList(std::vector<CapabilityData> & caps)53 int32_t FCodecLoader::GetCapabilityList(std::vector<CapabilityData> &caps)
54 {
55 FCodecLoader &loader = GetInstance();
56
57 std::lock_guard<std::mutex> lock(loader.mutex_);
58 CHECK_AND_RETURN_RET_LOG(loader.Init() == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Get capability failed: init error");
59 int32_t ret = loader.GetCaps(caps);
60 loader.CloseLibrary();
61 return ret;
62 }
63
FCodecLoader()64 FCodecLoader::FCodecLoader() : VideoCodecLoader(FCODEC_LIB_PATH, FCODEC_CREATE_FUNC_NAME, FCODEC_GETCAPS_FUNC_NAME) {}
65
CloseLibrary()66 void FCodecLoader::CloseLibrary()
67 {
68 if (fcodecCount_) {
69 return;
70 }
71 Close();
72 }
73
GetInstance()74 FCodecLoader &FCodecLoader::GetInstance()
75 {
76 static FCodecLoader loader;
77 return loader;
78 }
79 } // namespace MediaAVCodec
80 } // namespace OHOS