• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "codec_factory.h"
18 #include <limits>
19 #include <cinttypes>
20 #include <dlfcn.h>
21 #include "avcodec_errors.h"
22 #include "avcodec_log.h"
23 #include "audio_codec_adapter.h"
24 #include "audio_codec.h"
25 #include "codeclist_core.h"
26 #include "codeclist_utils.h"
27 #include "meta/format.h"
28 #ifndef CLIENT_SUPPORT_CODEC
29 #include "fcodec.h"
30 #include "hcodec_loader.h"
31 #endif
32 namespace {
33 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "CodecFactory"};
34 }
35 
36 namespace OHOS {
37 namespace MediaAVCodec {
Instance()38 CodecFactory &CodecFactory::Instance()
39 {
40     static CodecFactory inst;
41     return inst;
42 }
43 
~CodecFactory()44 CodecFactory::~CodecFactory()
45 {
46 }
47 
CreateCodecByMime(bool isEncoder,const std::string & mime,API_VERSION apiVersion)48 std::shared_ptr<CodecBase> CodecFactory::CreateCodecByMime(bool isEncoder,
49     const std::string &mime, API_VERSION apiVersion)
50 {
51     std::shared_ptr<CodecListCore> codecListCore = std::make_shared<CodecListCore>();
52     std::string codecname;
53     Format format;
54     format.PutStringValue("codec_mime", mime);
55     if (isEncoder) {
56         codecname = codecListCore->FindEncoder(format);
57     } else {
58         codecname = codecListCore->FindDecoder(format);
59     }
60     CHECK_AND_RETURN_RET_LOG(!codecname.empty(), nullptr, "Create codec by mime failed: error mime type");
61 
62     std::shared_ptr<CodecBase> codec = CreateCodecByName(codecname, apiVersion);
63     EXPECT_AND_LOGI(codec != nullptr, "Succeed");
64     return codec;
65 }
66 
CreateCodecByName(const std::string & name,API_VERSION apiVersion)67 std::shared_ptr<CodecBase> CodecFactory::CreateCodecByName(const std::string &name, API_VERSION apiVersion)
68 {
69     std::shared_ptr<CodecListCore> codecListCore = std::make_shared<CodecListCore>();
70     CodecType codecType = codecListCore->FindCodecType(name);
71     std::shared_ptr<CodecBase> codec = nullptr;
72     switch (codecType) {
73 #ifndef CLIENT_SUPPORT_CODEC
74         case CodecType::AVCODEC_HCODEC:
75             codec = HCodecLoader::CreateByName(name);
76             break;
77         case CodecType::AVCODEC_VIDEO_CODEC:
78             codec = std::make_shared<Codec::FCodec>(name);
79             break;
80 #else
81         case CodecType::AVCODEC_AUDIO_CODEC:
82             if (apiVersion == API_VERSION::API_VERSION_10) {
83                 codec = std::make_shared<AudioCodecAdapter>(name);
84             } else {
85                 codec = std::make_shared<AudioCodec>();
86                 auto ret = codec->CreateCodecByName(name);
87                 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr,
88                                          "Create codec by name:%{public}s failed", name.c_str());
89             }
90             break;
91 #endif
92         default:
93             AVCODEC_LOGE("Create codec %{public}s failed", name.c_str());
94             return codec;
95     }
96     (void)apiVersion;
97     AVCODEC_LOGD("Create codec %{public}s successful", name.c_str());
98     return codec;
99 }
100 } // namespace MediaAVCodec
101 } // namespace OHOS
102