• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "codec_component_manager_service.h"
16 #include <hdf_base.h>
17 #include <hdf_log.h>
18 #include <osal_mem.h>
19 #include <securec.h>
20 #include <unistd.h>
21 #include "codec_adapter_interface.h"
22 #include "codec_component_capability_config.h"
23 #include "codec_component_manager_stub.h"
24 #include "codec_component_type_service.h"
25 
26 #define HDF_LOG_TAG codec_hdi_server
27 
28 struct CodecComponentManagerSerivce *g_service = NULL;
29 uint32_t g_componentId = 0;
GetNextComponentId()30 static uint32_t GetNextComponentId()
31 {
32     uint32_t tempId = 0;
33     if (g_service == NULL) {
34         return tempId;
35     }
36     struct ComponentTypeNode *pos = NULL;
37     struct ComponentTypeNode *next = NULL;
38     bool find = false;
39 
40     do {
41         tempId = ++g_componentId;
42         find = false;
43         DLIST_FOR_EACH_ENTRY_SAFE(pos, next, &g_service->head, struct ComponentTypeNode, node)
44         {
45             if (pos != NULL && tempId == pos->componentId) {
46                 find = true;
47                 break;
48             }
49         }
50     } while (find);
51     return tempId;
52 }
53 
OmxManagerGetComponentNum()54 static int32_t OmxManagerGetComponentNum()
55 {
56     int32_t num = 0;
57     if (GetComponentNum(&num) != HDF_SUCCESS) {
58         HDF_LOGE("%{public}s, GetComponentNum error!", __func__);
59     }
60     return num;
61 }
62 
OmxManagerGetComponentCapabilityList(CodecCompCapability * capList,int32_t count)63 static int32_t OmxManagerGetComponentCapabilityList(CodecCompCapability *capList, int32_t count)
64 {
65     int32_t err = GetComponentCapabilityList(capList, count);
66     if (err != HDF_SUCCESS) {
67         HDF_LOGE("%{public}s, GetComponentNum error!", __func__);
68     }
69     return err;
70 }
71 
OmxManagerCreateComponent(struct CodecComponentType ** component,uint32_t * componentId,char * compName,int64_t appData,struct CodecCallbackType * callbacks)72 static int32_t OmxManagerCreateComponent(struct CodecComponentType **component, uint32_t *componentId, char *compName,
73                                          int64_t appData, struct CodecCallbackType *callbacks)
74 {
75     HDF_LOGI("%{public}s, service impl!", __func__);
76     if (g_service == NULL) {
77         HDF_LOGE("%{public}s, g_service is not init!", __func__);
78         return HDF_ERR_INVALID_PARAM;
79     }
80 
81     struct CodecComponentType *comp = CodecComponentTypeServiceGet();
82     if (comp == NULL) {
83         HDF_LOGE("%{public}s, CodecComponentTypeServiceGet ret null!", __func__);
84         return HDF_ERR_INVALID_PARAM;
85     }
86 
87     struct ComponentTypeNode *node = (struct ComponentTypeNode *)OsalMemCalloc(sizeof(struct ComponentTypeNode));
88     if (node == NULL) {
89         HDF_LOGE("%{public}s, CodecComponentTypeServiceGet ret null!", __func__);
90         CodecComponentTypeServiceRelease(comp);
91         return HDF_ERR_INVALID_PARAM;
92     }
93 
94     struct CodecComponentNode *codecNode = NULL;
95     int32_t err = OMXAdapterCreateComponent(&codecNode, compName, appData, callbacks);
96     if (err != HDF_SUCCESS) {
97         HDF_LOGE("%{public}s, OMXAdapterCreateComponent err [%{public}x]", __func__, err);
98         CodecComponentTypeServiceRelease(comp);
99         OsalMemFree(node);
100         return HDF_ERR_INVALID_PARAM;
101     }
102 #ifdef SUPPORT_ROLE
103     err = OmxAdapterSetComponentRole(codecNode, compName);
104     if (err != HDF_SUCCESS) {
105         HDF_LOGE("%{public}s, OMXAdapterSetComponentRole err [%{public}x]", __func__, err);
106         CodecComponentTypeServiceRelease(comp);
107         OsalMemFree(node);
108         return HDF_ERR_INVALID_PARAM;
109     }
110 #endif
111     *component = comp;
112     pthread_mutex_lock(&g_service->listMute);
113     *componentId = GetNextComponentId();
114     CodecComponentTypeServiceSetCodecNode(comp, codecNode);
115     DListInsertTail(&node->node, &g_service->head);
116     pthread_mutex_unlock(&g_service->listMute);
117 
118     node->componentId = *componentId;
119     node->service = comp;
120     HDF_LOGI("%{public}s: comp is %{public}p, componentId:%{public}d", __func__, comp, node->componentId);
121     return err;
122 }
123 
OmxManagerDestroyComponent(uint32_t componentId)124 static int32_t OmxManagerDestroyComponent(uint32_t componentId)
125 {
126     HDF_LOGI("%{public}s, service impl, %{public}d!", __func__, componentId);
127     if (g_service == NULL) {
128         HDF_LOGE("%{public}s, g_service is not init!", __func__);
129         return HDF_ERR_INVALID_PARAM;
130     }
131     struct ComponentTypeNode *pos = NULL;
132     struct ComponentTypeNode *next = NULL;
133     int32_t err = HDF_SUCCESS;
134     pthread_mutex_lock(&g_service->listMute);
135     DLIST_FOR_EACH_ENTRY_SAFE(pos, next, &g_service->head, struct ComponentTypeNode, node)
136     {
137         if (pos == NULL || componentId != pos->componentId) {
138             continue;
139         }
140         struct CodecComponentNode *codecNode = CodecComponentTypeServiceGetCodecNode(pos->service);
141         if (codecNode != NULL) {
142             err = OmxAdapterDestroyComponent(codecNode);
143             if (err != HDF_SUCCESS) {
144                 HDF_LOGE("%{public}s, OmxAdapterDestroyComponent ret err[%{public}d]!", __func__, err);
145                 break;
146             }
147         }
148         DListRemove(&pos->node);
149         CodecComponentTypeServiceRelease(pos->service);
150         OsalMemFree(pos);
151         pos = NULL;
152         break;
153     }
154     pthread_mutex_unlock(&g_service->listMute);
155     return err;
156 }
157 
CodecComponentManagerServiceConstruct(struct CodecComponentManager * manager)158 static void CodecComponentManagerServiceConstruct(struct CodecComponentManager *manager)
159 {
160     if (manager != NULL) {
161         manager->GetComponentNum = OmxManagerGetComponentNum;
162         manager->GetComponentCapabilityList = OmxManagerGetComponentCapabilityList;
163         manager->CreateComponent = OmxManagerCreateComponent;
164         manager->DestroyComponent = OmxManagerDestroyComponent;
165     }
166 }
167 
CodecComponentManagerSerivceGet(void)168 struct CodecComponentManagerSerivce *CodecComponentManagerSerivceGet(void)
169 {
170     if (g_service == NULL) {
171         g_service = (struct CodecComponentManagerSerivce *)OsalMemCalloc(sizeof(struct CodecComponentManagerSerivce));
172         if (g_service == NULL) {
173             HDF_LOGE("%{public}s: malloc OmxComponentManagerService obj failed!", __func__);
174             return NULL;
175         }
176         DListHeadInit(&g_service->head);
177         if (!CodecComponentManagerStubConstruct(&g_service->stub)) {
178             HDF_LOGE("%{public}s: construct SampleStub obj failed!", __func__);
179             OmxComponentManagerSeriveRelease(g_service);
180             g_service = NULL;
181         }
182         CodecComponentManagerServiceConstruct(&g_service->stub.interface);
183     }
184     return g_service;
185 }
186 
OmxComponentManagerSeriveRelease(struct CodecComponentManagerSerivce * instance)187 void OmxComponentManagerSeriveRelease(struct CodecComponentManagerSerivce *instance)
188 {
189     if (instance == NULL) {
190         return;
191     }
192     if (g_service == instance) {
193         g_service = NULL;
194     }
195     OsalMemFree(instance);
196 }
197