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