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