1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022-2023 Shenzhen Kaihong DID Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci#include "codec_component_manager_service.h"
16094332d3Sopenharmony_ci#include <hdf_base.h>
17094332d3Sopenharmony_ci#include <osal_mem.h>
18094332d3Sopenharmony_ci#include <securec.h>
19094332d3Sopenharmony_ci#include <unistd.h>
20094332d3Sopenharmony_ci#include "codec_adapter_interface.h"
21094332d3Sopenharmony_ci#include "codec_component_capability_config.h"
22094332d3Sopenharmony_ci#include "codec_component_manager_stub.h"
23094332d3Sopenharmony_ci#include "codec_component_type_service.h"
24094332d3Sopenharmony_ci#include "codec_death_recipient.h"
25094332d3Sopenharmony_ci#include "codec_log_wrapper.h"
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_ci#define MAX_COMPONENT_SIZE 32
28094332d3Sopenharmony_cistruct CodecComponentManagerSerivce *g_service = NULL;
29094332d3Sopenharmony_ciuint32_t g_componentId = 0;
30094332d3Sopenharmony_ci
31094332d3Sopenharmony_cistatic void OnRemoteServiceDied(struct HdfDeathRecipient *deathRecipient, struct HdfRemoteService *remote)
32094332d3Sopenharmony_ci{
33094332d3Sopenharmony_ci    CleanRemoteServiceResource(deathRecipient, remote);
34094332d3Sopenharmony_ci}
35094332d3Sopenharmony_ci
36094332d3Sopenharmony_cistatic struct RemoteServiceDeathRecipient g_deathRecipient = {
37094332d3Sopenharmony_ci    .recipient = {
38094332d3Sopenharmony_ci        .OnRemoteDied = OnRemoteServiceDied,
39094332d3Sopenharmony_ci    }
40094332d3Sopenharmony_ci};
41094332d3Sopenharmony_ci
42094332d3Sopenharmony_cistatic void AddDeathRecipientForService(struct CodecCallbackType *callbacks, uint32_t componentId,
43094332d3Sopenharmony_ci                                        struct CodecComponentNode *codecNode)
44094332d3Sopenharmony_ci{
45094332d3Sopenharmony_ci    if (callbacks == NULL) {
46094332d3Sopenharmony_ci        CODEC_LOGE("invalid parameter");
47094332d3Sopenharmony_ci        return;
48094332d3Sopenharmony_ci    }
49094332d3Sopenharmony_ci    bool needAdd = RegisterService(callbacks, componentId, codecNode);
50094332d3Sopenharmony_ci    if (needAdd) {
51094332d3Sopenharmony_ci        CODEC_LOGI("add deathRecipient for remoteService!");
52094332d3Sopenharmony_ci        HdfRemoteServiceAddDeathRecipient(callbacks->remote, &g_deathRecipient.recipient);
53094332d3Sopenharmony_ci    }
54094332d3Sopenharmony_ci}
55094332d3Sopenharmony_ci
56094332d3Sopenharmony_cistatic uint32_t GetNextComponentId()
57094332d3Sopenharmony_ci{
58094332d3Sopenharmony_ci    uint32_t tempId = 0;
59094332d3Sopenharmony_ci    if (g_service == NULL) {
60094332d3Sopenharmony_ci        return tempId;
61094332d3Sopenharmony_ci    }
62094332d3Sopenharmony_ci    struct ComponentTypeNode *pos = NULL;
63094332d3Sopenharmony_ci    struct ComponentTypeNode *next = NULL;
64094332d3Sopenharmony_ci    bool find = false;
65094332d3Sopenharmony_ci
66094332d3Sopenharmony_ci    do {
67094332d3Sopenharmony_ci        tempId = ++g_componentId;
68094332d3Sopenharmony_ci        find = false;
69094332d3Sopenharmony_ci        DLIST_FOR_EACH_ENTRY_SAFE(pos, next, &g_service->head, struct ComponentTypeNode, node)
70094332d3Sopenharmony_ci        {
71094332d3Sopenharmony_ci            if (pos != NULL && tempId == pos->componentId) {
72094332d3Sopenharmony_ci                find = true;
73094332d3Sopenharmony_ci                break;
74094332d3Sopenharmony_ci            }
75094332d3Sopenharmony_ci        }
76094332d3Sopenharmony_ci    } while (find);
77094332d3Sopenharmony_ci    return tempId;
78094332d3Sopenharmony_ci}
79094332d3Sopenharmony_ci
80094332d3Sopenharmony_cistatic int32_t OmxManagerGetComponentNum()
81094332d3Sopenharmony_ci{
82094332d3Sopenharmony_ci    int32_t num = 0;
83094332d3Sopenharmony_ci    if (GetComponentNum(&num) != HDF_SUCCESS) {
84094332d3Sopenharmony_ci        CODEC_LOGE("GetComponentNum error!");
85094332d3Sopenharmony_ci    }
86094332d3Sopenharmony_ci    return num;
87094332d3Sopenharmony_ci}
88094332d3Sopenharmony_ci
89094332d3Sopenharmony_cistatic int32_t OmxManagerGetComponentCapabilityList(CodecCompCapability *capList, int32_t count)
90094332d3Sopenharmony_ci{
91094332d3Sopenharmony_ci    int32_t err = GetComponentCapabilityList(capList, count);
92094332d3Sopenharmony_ci    if (err != HDF_SUCCESS) {
93094332d3Sopenharmony_ci        CODEC_LOGE("GetComponentNum error!");
94094332d3Sopenharmony_ci    }
95094332d3Sopenharmony_ci    return err;
96094332d3Sopenharmony_ci}
97094332d3Sopenharmony_ci
98094332d3Sopenharmony_cistatic int32_t OmxManagerDestroyComponent(uint32_t componentId)
99094332d3Sopenharmony_ci{
100094332d3Sopenharmony_ci    CODEC_LOGI("service impl, %{public}d!", componentId);
101094332d3Sopenharmony_ci    if (g_service == NULL) {
102094332d3Sopenharmony_ci        CODEC_LOGE("g_service is not init!");
103094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
104094332d3Sopenharmony_ci    }
105094332d3Sopenharmony_ci
106094332d3Sopenharmony_ci    struct ComponentTypeNode *pos = NULL;
107094332d3Sopenharmony_ci    struct ComponentTypeNode *next = NULL;
108094332d3Sopenharmony_ci    int32_t err = HDF_SUCCESS;
109094332d3Sopenharmony_ci    pthread_mutex_lock(&g_service->listMute);
110094332d3Sopenharmony_ci
111094332d3Sopenharmony_ci    DLIST_FOR_EACH_ENTRY_SAFE(pos, next, &g_service->head, struct ComponentTypeNode, node)
112094332d3Sopenharmony_ci    {
113094332d3Sopenharmony_ci        if (pos == NULL || componentId != pos->componentId) {
114094332d3Sopenharmony_ci            continue;
115094332d3Sopenharmony_ci        }
116094332d3Sopenharmony_ci
117094332d3Sopenharmony_ci        struct CodecComponentNode *codecNode = CodecComponentTypeServiceGetCodecNode(pos->service);
118094332d3Sopenharmony_ci        if (codecNode != NULL) {
119094332d3Sopenharmony_ci            err = OmxAdapterDestroyComponent(codecNode);
120094332d3Sopenharmony_ci            if (err != HDF_SUCCESS) {
121094332d3Sopenharmony_ci                CODEC_LOGE("OmxAdapterDestroyComponent ret err[%{public}d]!", err);
122094332d3Sopenharmony_ci                break;
123094332d3Sopenharmony_ci            }
124094332d3Sopenharmony_ci            RemoveDestoryedComponent(componentId);
125094332d3Sopenharmony_ci        }
126094332d3Sopenharmony_ci
127094332d3Sopenharmony_ci        DListRemove(&pos->node);
128094332d3Sopenharmony_ci        CodecComponentTypeServiceRelease(pos->service);
129094332d3Sopenharmony_ci        OsalMemFree(pos);
130094332d3Sopenharmony_ci        pos = NULL;
131094332d3Sopenharmony_ci        break;
132094332d3Sopenharmony_ci    }
133094332d3Sopenharmony_ci
134094332d3Sopenharmony_ci    pthread_mutex_unlock(&g_service->listMute);
135094332d3Sopenharmony_ci    return err;
136094332d3Sopenharmony_ci}
137094332d3Sopenharmony_ci
138094332d3Sopenharmony_cistatic int32_t OmxManagerCreateComponent(struct CodecComponentType **component, uint32_t *componentId, char *compName,
139094332d3Sopenharmony_ci                                         int64_t appData, struct CodecCallbackType *callbacks)
140094332d3Sopenharmony_ci{
141094332d3Sopenharmony_ci    CODEC_LOGI("service impl!");
142094332d3Sopenharmony_ci    if (g_service == NULL) {
143094332d3Sopenharmony_ci        CODEC_LOGE("g_service is not init!");
144094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
145094332d3Sopenharmony_ci    }
146094332d3Sopenharmony_ci
147094332d3Sopenharmony_ci    struct CodecComponentType *comp = CodecComponentTypeServiceGet();
148094332d3Sopenharmony_ci    if (comp == NULL) {
149094332d3Sopenharmony_ci        CODEC_LOGE("CodecComponentTypeServiceGet ret null!");
150094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
151094332d3Sopenharmony_ci    }
152094332d3Sopenharmony_ci
153094332d3Sopenharmony_ci    struct ComponentTypeNode *node = (struct ComponentTypeNode *)OsalMemCalloc(sizeof(struct ComponentTypeNode));
154094332d3Sopenharmony_ci    if (node == NULL) {
155094332d3Sopenharmony_ci        CODEC_LOGE("CodecComponentTypeServiceGet ret null!");
156094332d3Sopenharmony_ci        CodecComponentTypeServiceRelease(comp);
157094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
158094332d3Sopenharmony_ci    }
159094332d3Sopenharmony_ci
160094332d3Sopenharmony_ci    struct CodecComponentNode *codecNode = NULL;
161094332d3Sopenharmony_ci    int32_t err = OMXAdapterCreateComponent(&codecNode, compName, appData, callbacks);
162094332d3Sopenharmony_ci    if (err != HDF_SUCCESS) {
163094332d3Sopenharmony_ci        CODEC_LOGE("OMXAdapterCreateComponent err [%{public}x]", err);
164094332d3Sopenharmony_ci        CodecComponentTypeServiceRelease(comp);
165094332d3Sopenharmony_ci        OsalMemFree(node);
166094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
167094332d3Sopenharmony_ci    }
168094332d3Sopenharmony_ci    *component = comp;
169094332d3Sopenharmony_ci    pthread_mutex_lock(&g_service->listMute);
170094332d3Sopenharmony_ci    *componentId = GetNextComponentId();
171094332d3Sopenharmony_ci    CodecComponentTypeServiceSetCodecNode(comp, codecNode);
172094332d3Sopenharmony_ci    DListInsertTail(&node->node, &g_service->head);
173094332d3Sopenharmony_ci    pthread_mutex_unlock(&g_service->listMute);
174094332d3Sopenharmony_ci    node->componentId = *componentId;
175094332d3Sopenharmony_ci    node->service = comp;
176094332d3Sopenharmony_ci#ifdef SUPPORT_ROLE
177094332d3Sopenharmony_ci    err = OmxAdapterSetComponentRole(codecNode, compName);
178094332d3Sopenharmony_ci    if (err != HDF_SUCCESS) {
179094332d3Sopenharmony_ci        CODEC_LOGE("OMXAdapterSetComponentRole err [%{public}x]", err);
180094332d3Sopenharmony_ci        OmxManagerDestroyComponent(*componentId);
181094332d3Sopenharmony_ci        CodecComponentTypeServiceRelease(comp);
182094332d3Sopenharmony_ci        OsalMemFree(node);
183094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
184094332d3Sopenharmony_ci    }
185094332d3Sopenharmony_ci#endif
186094332d3Sopenharmony_ci    CODEC_LOGI("componentId:%{public}d", node->componentId);
187094332d3Sopenharmony_ci    AddDeathRecipientForService(callbacks, *componentId, codecNode);
188094332d3Sopenharmony_ci    return err;
189094332d3Sopenharmony_ci}
190094332d3Sopenharmony_ci
191094332d3Sopenharmony_cistatic void CodecComponentManagerServiceConstruct(struct CodecComponentManager *manager)
192094332d3Sopenharmony_ci{
193094332d3Sopenharmony_ci    if (manager != NULL) {
194094332d3Sopenharmony_ci        manager->GetComponentNum = OmxManagerGetComponentNum;
195094332d3Sopenharmony_ci        manager->GetComponentCapabilityList = OmxManagerGetComponentCapabilityList;
196094332d3Sopenharmony_ci        manager->CreateComponent = OmxManagerCreateComponent;
197094332d3Sopenharmony_ci        manager->DestroyComponent = OmxManagerDestroyComponent;
198094332d3Sopenharmony_ci    }
199094332d3Sopenharmony_ci}
200094332d3Sopenharmony_ci
201094332d3Sopenharmony_cistruct CodecComponentManagerSerivce *CodecComponentManagerSerivceGet(void)
202094332d3Sopenharmony_ci{
203094332d3Sopenharmony_ci    if (g_service == NULL) {
204094332d3Sopenharmony_ci        g_service = (struct CodecComponentManagerSerivce *)OsalMemCalloc(sizeof(struct CodecComponentManagerSerivce));
205094332d3Sopenharmony_ci        if (g_service == NULL) {
206094332d3Sopenharmony_ci            CODEC_LOGE("malloc OmxComponentManagerService obj failed!");
207094332d3Sopenharmony_ci            return NULL;
208094332d3Sopenharmony_ci        }
209094332d3Sopenharmony_ci        DListHeadInit(&g_service->head);
210094332d3Sopenharmony_ci        if (!CodecComponentManagerStubConstruct(&g_service->stub)) {
211094332d3Sopenharmony_ci            CODEC_LOGE("construct SampleStub obj failed!");
212094332d3Sopenharmony_ci            OmxComponentManagerSeriveRelease(g_service);
213094332d3Sopenharmony_ci            g_service = NULL;
214094332d3Sopenharmony_ci        }
215094332d3Sopenharmony_ci        CodecComponentManagerServiceConstruct(&g_service->stub.interface);
216094332d3Sopenharmony_ci    }
217094332d3Sopenharmony_ci    return g_service;
218094332d3Sopenharmony_ci}
219094332d3Sopenharmony_ci
220094332d3Sopenharmony_civoid OmxComponentManagerSeriveRelease(struct CodecComponentManagerSerivce *instance)
221094332d3Sopenharmony_ci{
222094332d3Sopenharmony_ci    if (instance == NULL) {
223094332d3Sopenharmony_ci        return;
224094332d3Sopenharmony_ci    }
225094332d3Sopenharmony_ci    if (g_service == instance) {
226094332d3Sopenharmony_ci        g_service = NULL;
227094332d3Sopenharmony_ci    }
228094332d3Sopenharmony_ci    OsalMemFree(instance);
229094332d3Sopenharmony_ci}
230094332d3Sopenharmony_ci
231094332d3Sopenharmony_civoid CleanRemoteServiceResource(struct HdfDeathRecipient *deathRecipient, struct HdfRemoteService *remote)
232094332d3Sopenharmony_ci{
233094332d3Sopenharmony_ci    uint32_t compIds[MAX_COMPONENT_SIZE];
234094332d3Sopenharmony_ci    uint32_t size = 0;
235094332d3Sopenharmony_ci    int32_t ret = CleanMapperOfDiedService(remote, compIds, &size);
236094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
237094332d3Sopenharmony_ci        CODEC_LOGE("clearn remote resource error!");
238094332d3Sopenharmony_ci        return;
239094332d3Sopenharmony_ci    }
240094332d3Sopenharmony_ci
241094332d3Sopenharmony_ci    if (size == 0) {
242094332d3Sopenharmony_ci        CODEC_LOGE("remoteService no componment resource need to be release!");
243094332d3Sopenharmony_ci        return;
244094332d3Sopenharmony_ci    }
245094332d3Sopenharmony_ci    for (uint32_t i = 0; i < size; i++) {
246094332d3Sopenharmony_ci        OmxManagerDestroyComponent(compIds[i]);
247094332d3Sopenharmony_ci        CODEC_LOGI("destroyComponent done, compId=[%{public}d]", compIds[i]);
248094332d3Sopenharmony_ci    }
249094332d3Sopenharmony_ci
250094332d3Sopenharmony_ci    CODEC_LOGI("remote service died , clean resource success!");
251094332d3Sopenharmony_ci}