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 <hitrace_meter.h>
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 #include "codec_death_recipient.h"
25 namespace OHOS {
26 namespace HDI {
27 namespace Codec {
28 namespace V3_0 {
29 sptr<CodecComponentManagerService> g_codecManagerService = sptr<CodecComponentManagerService>();
30 std::once_flag m_serviceFlag;
31 using OHOS::Codec::Omx::ComponentNode;
CodecComponentManagerImplGetInstance(void)32 extern "C" ICodecComponentManager *CodecComponentManagerImplGetInstance(void)
33 {
34 std::call_once(m_serviceFlag, [] {
35 g_codecManagerService = sptr<CodecComponentManagerService>(new CodecComponentManagerService());
36 CodecDfxService::GetInstance().SetComponentManager(g_codecManagerService);
37 OHOS::Codec::Omx::CodecComponentConfig::GetInstance()->CodecCompCapabilityInit();
38 });
39 return g_codecManagerService;
40 }
41
CodecComponentManagerService()42 CodecComponentManagerService::CodecComponentManagerService() : componentId_(0)
43 {
44 resourceNode_.name = nullptr;
45 resourceNode_.hashValue = 0;
46 resourceNode_.attrData = nullptr;
47 resourceNode_.parent = nullptr;
48 resourceNode_.child = nullptr;
49 resourceNode_.sibling = nullptr;
50 mgr_ = std::make_shared<OHOS::Codec::Omx::ComponentMgr>();
51 }
52
GetComponentNum(int32_t & count)53 int32_t CodecComponentManagerService::GetComponentNum(int32_t &count)
54 {
55 return OHOS::Codec::Omx::CodecComponentConfig::GetInstance()->GetComponentNum(count);
56 }
57
GetComponentCapabilityList(std::vector<CodecCompCapability> & capList,int32_t count)58 int32_t CodecComponentManagerService::GetComponentCapabilityList(std::vector<CodecCompCapability> &capList,
59 int32_t count)
60 {
61 return OHOS::Codec::Omx::CodecComponentConfig::GetInstance()->GetComponentCapabilityList(capList, count);
62 }
63
JudgePassThrouth(void)64 bool CodecComponentManagerService::JudgePassThrouth(void)
65 {
66 uint32_t remotePid = static_cast<uint32_t>(HdfRemoteGetCallingPid());
67 uint32_t curPid = static_cast<uint32_t>(getpid());
68 return remotePid == curPid;
69 }
70
CreateComponent(sptr<ICodecComponent> & component,uint32_t & componentId,const std::string & compName,int64_t appData,const sptr<ICodecCallback> & callbacks)71 int32_t CodecComponentManagerService::CreateComponent(sptr<ICodecComponent> &component, uint32_t &componentId,
72 const std::string &compName, int64_t appData,
73 const sptr<ICodecCallback> &callbacks)
74 {
75 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecCreateComponent");
76 CODEC_LOGD("compName[%{public}s]", compName.c_str());
77 CHECK_AND_RETURN_RET_LOG(callbacks != nullptr, HDF_ERR_INVALID_PARAM, "callbacks is null");
78 std::shared_ptr<ComponentNode> node = std::make_shared<ComponentNode>(callbacks, appData, mgr_);
79 auto err = node->OpenHandle(compName);
80 if (err != HDF_SUCCESS) {
81 CODEC_LOGE("OpenHandle faled, err[%{public}d]", err);
82 node = nullptr;
83 return err;
84 }
85
86 sptr<ICodecComponent> codecComponent(new CodecComponentService(node, mgr_, compName));
87 std::unique_lock<std::mutex> autoLock(mutex_);
88 componentId = GetNextComponentId();
89 componentMap_.emplace(std::make_pair(componentId, codecComponent));
90 component = codecComponent;
91 CODEC_LOGI("componentId[%{public}d]", componentId);
92 if (!JudgePassThrouth()) {
93 RegisterDeathRecipientService(callbacks, componentId, this);
94 }
95 return HDF_SUCCESS;
96 }
97
DestroyComponent(uint32_t componentId)98 int32_t CodecComponentManagerService::DestroyComponent(uint32_t componentId)
99 {
100 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecDestroyComponent");
101 std::unique_lock<std::mutex> autoLock(mutex_);
102 CODEC_LOGI("componentId[%{public}d]", componentId);
103 auto iter = componentMap_.find(componentId);
104 if (iter == componentMap_.end() || iter->second == nullptr) {
105 CODEC_LOGE("can not find component service by componentId[%{public}d]", componentId);
106 return HDF_ERR_INVALID_PARAM;
107 }
108 componentMap_.erase(iter);
109 RemoveMapperOfDestoryedComponent(componentId);
110 return HDF_SUCCESS;
111 }
112
GetNextComponentId(void)113 uint32_t CodecComponentManagerService::GetNextComponentId(void)
114 {
115 uint32_t tempId = 0;
116 do {
117 tempId = ++componentId_;
118 } while (componentMap_.find(tempId) != componentMap_.end());
119 return tempId;
120 }
121
LoadCapabilityData(const DeviceResourceNode & node)122 void CodecComponentManagerService::LoadCapabilityData(const DeviceResourceNode &node)
123 {
124 resourceNode_ = node;
125 }
126
GetManagerMap(std::map<uint32_t,sptr<ICodecComponent>> & dumpMap)127 void CodecComponentManagerService::GetManagerMap(std::map<uint32_t, sptr<ICodecComponent>> &dumpMap)
128 {
129 dumpMap = componentMap_;
130 }
131 } // namespace V3_0
132 } // namespace Codec
133 } // namespace HDI
134 } // namespace OHOS
135