• 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 "avcodec_log.h"
17 #include "avcodec_errors.h"
18 #include "codeclist_builder.h"
19 #ifndef CLIENT_SUPPORT_CODEC
20 #include "hcodec_loader.h"
21 #endif
22 #include "codec_ability_singleton.h"
23 
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "CodecAbilitySingleton"};
26 }
27 
28 namespace OHOS {
29 namespace MediaAVCodec {
GetCodecLists()30 std::unordered_map<CodecType, std::shared_ptr<CodecListBase>> GetCodecLists()
31 {
32     std::unordered_map<CodecType, std::shared_ptr<CodecListBase>> codecLists;
33 #ifndef CLIENT_SUPPORT_CODEC
34     std::shared_ptr<CodecListBase> vcodecList = std::make_shared<VideoCodecList>();
35     codecLists.insert(std::make_pair(CodecType::AVCODEC_VIDEO_CODEC, vcodecList));
36 #endif
37     std::shared_ptr<CodecListBase> acodecList = std::make_shared<AudioCodecList>();
38     codecLists.insert(std::make_pair(CodecType::AVCODEC_AUDIO_CODEC, acodecList));
39     return codecLists;
40 }
41 
GetInstance()42 CodecAbilitySingleton &CodecAbilitySingleton::GetInstance()
43 {
44     static CodecAbilitySingleton instance;
45     return instance;
46 }
47 
CodecAbilitySingleton()48 CodecAbilitySingleton::CodecAbilitySingleton()
49 {
50 #ifndef CLIENT_SUPPORT_CODEC
51     std::vector<CapabilityData> videoCapaArray;
52     if (HCodecLoader::GetCapabilityList(videoCapaArray) == AVCS_ERR_OK) {
53         RegisterCapabilityArray(videoCapaArray, CodecType::AVCODEC_HCODEC);
54     }
55 #endif
56     std::unordered_map<CodecType, std::shared_ptr<CodecListBase>> codecLists = GetCodecLists();
57     for (auto iter = codecLists.begin(); iter != codecLists.end(); iter++) {
58         CodecType codecType = iter->first;
59         std::vector<CapabilityData> capaArray;
60         int32_t ret = iter->second->GetCapabilityList(capaArray);
61         if (ret == AVCS_ERR_OK) {
62             RegisterCapabilityArray(capaArray, codecType);
63         }
64     }
65     AVCODEC_LOGI("Succeed");
66 }
67 
~CodecAbilitySingleton()68 CodecAbilitySingleton::~CodecAbilitySingleton()
69 {
70     AVCODEC_LOGI("Succeed");
71 }
72 
RegisterCapabilityArray(std::vector<CapabilityData> & capaArray,CodecType codecType)73 void CodecAbilitySingleton::RegisterCapabilityArray(std::vector<CapabilityData> &capaArray, CodecType codecType)
74 {
75     std::lock_guard<std::mutex> lock(mutex_);
76     int32_t beginIdx = capabilityDataArray_.size();
77     for (auto iter = capaArray.begin(); iter != capaArray.end(); iter++) {
78         std::string mimeType = (*iter).mimeType;
79         std::vector<size_t> idxVec;
80         if (mimeCapIdxMap_.find(mimeType) == mimeCapIdxMap_.end()) {
81             mimeCapIdxMap_.insert(std::make_pair(mimeType, idxVec));
82         }
83         if ((*iter).profileLevelsMap.size() > MAX_MAP_SIZE) {
84             while ((*iter).profileLevelsMap.size() > MAX_MAP_SIZE) {
85                 auto rIter = (*iter).profileLevelsMap.end();
86                 (*iter).profileLevelsMap.erase(--rIter);
87             }
88             std::vector<int32_t> newProfiles;
89             (*iter).profiles.swap(newProfiles);
90             auto nIter = (*iter).profileLevelsMap.begin();
91             while (nIter != (*iter).profileLevelsMap.end()) {
92                 (*iter).profiles.emplace_back(nIter->first);
93                 nIter++;
94             }
95         }
96         while ((*iter).measuredFrameRate.size() > MAX_MAP_SIZE) {
97             auto rIter = (*iter).measuredFrameRate.end();
98             (*iter).measuredFrameRate.erase(--rIter);
99         }
100         capabilityDataArray_.emplace_back(*iter);
101         mimeCapIdxMap_.at(mimeType).emplace_back(beginIdx);
102         nameCodecTypeMap_.insert(std::make_pair((*iter).codecName, codecType));
103         beginIdx++;
104     }
105     AVCODEC_LOGD("Register capability successful");
106 }
107 
GetCapabilityArray()108 std::vector<CapabilityData> CodecAbilitySingleton::GetCapabilityArray()
109 {
110     std::lock_guard<std::mutex> lock(mutex_);
111     return capabilityDataArray_;
112 }
113 
GetNameCodecTypeMap()114 std::unordered_map<std::string, CodecType> CodecAbilitySingleton::GetNameCodecTypeMap()
115 {
116     std::lock_guard<std::mutex> lock(mutex_);
117     return nameCodecTypeMap_;
118 }
119 
GetMimeCapIdxMap()120 std::unordered_map<std::string, std::vector<size_t>> CodecAbilitySingleton::GetMimeCapIdxMap()
121 {
122     std::lock_guard<std::mutex> lock(mutex_);
123     return mimeCapIdxMap_;
124 }
125 } // namespace MediaAVCodec
126 } // namespace OHOS
127