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