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 "codec_factory.h"
17 #include <cinttypes>
18 #include <dlfcn.h>
19 #include <limits>
20 #include "avcodec_errors.h"
21 #include "avcodec_log.h"
22 #include "codec_ability_singleton.h"
23 #include "codeclist_core.h"
24 #include "meta/format.h"
25 #ifdef CLIENT_SUPPORT_CODEC
26 #include "audio_codec.h"
27 #include "audio_codec_adapter.h"
28 #else
29 #include "fcodec_loader.h"
30 #include "hevc_decoder_loader.h"
31 #include "hcodec_loader.h"
32 #endif
33
34 namespace {
35 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FRAMEWORK, "CodecFactory"};
36 } // namespace
37
38 namespace OHOS {
39 namespace MediaAVCodec {
Instance()40 CodecFactory &CodecFactory::Instance()
41 {
42 static CodecFactory inst;
43 return inst;
44 }
45
~CodecFactory()46 CodecFactory::~CodecFactory() {}
47
GetCodecNameArrayByMime(const std::string & mime,const bool isEncoder)48 std::vector<std::string> CodecFactory::GetCodecNameArrayByMime(const std::string &mime, const bool isEncoder)
49 {
50 auto codecListCore = std::make_shared<CodecListCore>();
51 auto nameArray = codecListCore->FindCodecNameArray(mime, isEncoder);
52 #ifndef CLIENT_SUPPORT_CODEC
53 auto checkFunc = [](const std::string &str) { return str.find("secure") != std::string::npos; };
54 nameArray.erase(std::remove_if(nameArray.begin(), nameArray.end(), checkFunc), nameArray.end());
55 #endif
56 return nameArray;
57 }
58
CreateCodecByName(const std::string & name,API_VERSION apiVersion)59 std::shared_ptr<CodecBase> CodecFactory::CreateCodecByName(const std::string &name, API_VERSION apiVersion)
60 {
61 std::shared_ptr<CodecListCore> codecListCore = std::make_shared<CodecListCore>();
62 CodecType codecType = codecListCore->FindCodecType(name);
63 std::shared_ptr<CodecBase> codec = nullptr;
64 switch (codecType) {
65 #ifndef CLIENT_SUPPORT_CODEC
66 case CodecType::AVCODEC_HCODEC:
67 codec = HCodecLoader::CreateByName(name);
68 break;
69 case CodecType::AVCODEC_VIDEO_CODEC:
70 codec = FCodecLoader::CreateByName(name);
71 break;
72 case CodecType::AVCODEC_VIDEO_HEVC_DECODER:
73 codec = HevcDecoderLoader::CreateByName(name);
74 break;
75 #else
76 case CodecType::AVCODEC_AUDIO_CODEC:
77 if (apiVersion == API_VERSION::API_VERSION_10) {
78 codec = std::make_shared<AudioCodecAdapter>(name);
79 } else {
80 codec = std::make_shared<AudioCodec>();
81 auto ret = codec->CreateCodecByName(name);
82 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Create codec by name:%{public}s failed",
83 name.c_str());
84 }
85 break;
86 #endif
87 default:
88 AVCODEC_LOGE("Create codec %{public}s failed", name.c_str());
89 return codec;
90 }
91 (void)apiVersion;
92 EXPECT_AND_LOGI(codec != nullptr, "Create codec %{public}s successful", name.c_str());
93 return codec;
94 }
95 } // namespace MediaAVCodec
96 } // namespace OHOS
97