1 /*
2 * Copyright (c) 2022-2023 Shenzhen Kaihong DID 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_component_manager_service.h"
17 #include <hdf_base.h>
18 #include <atomic>
19 #include "codec_component_config.h"
20 #include "codec_component_service.h"
21 #include "codec_log_wrapper.h"
22 #include "component_node.h"
23 #include "codec_dfx_service.h"
24 namespace OHOS {
25 namespace HDI {
26 namespace Codec {
27 namespace V1_0 {
28 sptr<CodecComponentManagerService> g_codecManagerService = nullptr;
29 std::once_flag m_serviceFlag;
30 using OHOS::Codec::Omx::ComponentNode;
CodecComponentManagerImplGetInstance(void)31 extern "C" ICodecComponentManager *CodecComponentManagerImplGetInstance(void)
32 {
33 std::call_once(m_serviceFlag, [] {
34 g_codecManagerService = new (std::nothrow) CodecComponentManagerService();
35 CodecDfxService::GetInstance().SetComponentManager(g_codecManagerService);
36 OHOS::Codec::Omx::CodecComponentConfig::GetInstance()->CodecCompCapabilityInit();
37 });
38 return g_codecManagerService;
39 }
40
CodecComponentManagerService()41 CodecComponentManagerService::CodecComponentManagerService() : componentId_(0)
42 {
43 resourceNode_.name = nullptr;
44 resourceNode_.hashValue = 0;
45 resourceNode_.attrData = nullptr;
46 resourceNode_.parent = nullptr;
47 resourceNode_.child = nullptr;
48 resourceNode_.sibling = nullptr;
49 mgr_ = std::make_shared<OHOS::Codec::Omx::ComponentMgr>();
50 }
51
GetComponentNum(int32_t & count)52 int32_t CodecComponentManagerService::GetComponentNum(int32_t &count)
53 {
54 return OHOS::Codec::Omx::CodecComponentConfig::GetInstance()->GetComponentNum(count);
55 }
56
GetComponentCapabilityList(std::vector<CodecCompCapability> & capList,int32_t count)57 int32_t CodecComponentManagerService::GetComponentCapabilityList(std::vector<CodecCompCapability> &capList,
58 int32_t count)
59 {
60 return OHOS::Codec::Omx::CodecComponentConfig::GetInstance()->GetComponentCapabilityList(capList, count);
61 }
62
CreateComponent(sptr<ICodecComponent> & component,uint32_t & componentId,const std::string & compName,int64_t appData,const sptr<ICodecCallback> & callbacks)63 int32_t CodecComponentManagerService::CreateComponent(sptr<ICodecComponent> &component, uint32_t &componentId,
64 const std::string &compName, int64_t appData,
65 const sptr<ICodecCallback> &callbacks)
66 {
67 CODEC_LOGD("compName[%{public}s]", compName.c_str());
68 CHECK_AND_RETURN_RET_LOG(callbacks != nullptr, HDF_ERR_INVALID_PARAM, "callbacks is null");
69 std::shared_ptr<ComponentNode> node = std::make_shared<ComponentNode>(callbacks, appData, mgr_);
70 auto err = node->OpenHandle(compName);
71 if (err != HDF_SUCCESS) {
72 CODEC_LOGE("OpenHandle faled, err[%{public}d]", err);
73 node = nullptr;
74 return err;
75 }
76
77 sptr<ICodecComponent> codecComponent = new CodecComponentService(node, mgr_, compName);
78 std::unique_lock<std::mutex> autoLock(mutex_);
79 componentId = GetNextComponentId();
80 componentMap_.emplace(std::make_pair(componentId, codecComponent));
81 component = codecComponent;
82 CODEC_LOGI("componentId[%{public}d]", componentId);
83 return HDF_SUCCESS;
84 }
85
DestroyComponent(uint32_t componentId)86 int32_t CodecComponentManagerService::DestroyComponent(uint32_t componentId)
87 {
88 std::unique_lock<std::mutex> autoLock(mutex_);
89 CODEC_LOGI("componentId[%{public}d]", componentId);
90 auto iter = componentMap_.find(componentId);
91 if (iter == componentMap_.end() || iter->second == nullptr) {
92 CODEC_LOGE("can not find component service by componentId[%{public}d]", componentId);
93 return HDF_ERR_INVALID_PARAM;
94 }
95 componentMap_.erase(iter);
96 return HDF_SUCCESS;
97 }
98
GetNextComponentId(void)99 uint32_t CodecComponentManagerService::GetNextComponentId(void)
100 {
101 uint32_t tempId = 0;
102 do {
103 tempId = ++componentId_;
104 } while (componentMap_.find(tempId) != componentMap_.end());
105 return tempId;
106 }
107
LoadCapabilityData(const DeviceResourceNode & node)108 void CodecComponentManagerService::LoadCapabilityData(const DeviceResourceNode &node)
109 {
110 resourceNode_ = node;
111 }
112
GetManagerMap(std::map<uint32_t,sptr<ICodecComponent>> & dumpMap)113 void CodecComponentManagerService::GetManagerMap(std::map<uint32_t, sptr<ICodecComponent>> &dumpMap)
114 {
115 dumpMap = componentMap_;
116 }
117 } // namespace V1_0
118 } // namespace Codec
119 } // namespace HDI
120 } // namespace OHOS
121