1 /*
2 * Copyright 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
16 #include <hdf_base.h>
17 #include <hdf_device_desc.h>
18 #include <hdf_sbuf_ipc.h>
19 #include "codec_component_config.h"
20 #include "codec_log_wrapper.h"
21 #include "v1_0/codec_component_manager_stub.h"
22 using namespace OHOS::HDI::Codec::V1_0;
23 namespace {
24 struct HdfCodecComponentManagerHost {
25 struct IDeviceIoService ioService;
26 OHOS::sptr<OHOS::IRemoteObject> stub;
27 };
28 }
29
CodecComponentManagerDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)30 static int32_t CodecComponentManagerDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
31 struct HdfSBuf *reply)
32 {
33 auto *hdfCodecComponentManagerHost =
34 CONTAINER_OF(client->device->service, struct HdfCodecComponentManagerHost, ioService);
35
36 OHOS::MessageParcel *dataParcel = nullptr;
37 OHOS::MessageParcel *replyParcel = nullptr;
38 OHOS::MessageOption option;
39
40 if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
41 CODEC_LOGE("invalid data sbuf object to dispatch");
42 return HDF_ERR_INVALID_PARAM;
43 }
44 if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
45 CODEC_LOGE("invalid reply sbuf object to dispatch");
46 return HDF_ERR_INVALID_PARAM;
47 }
48
49 return hdfCodecComponentManagerHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
50 }
51
HdfCodecComponentManagerDriverInit(struct HdfDeviceObject * deviceObject)52 static int HdfCodecComponentManagerDriverInit(struct HdfDeviceObject *deviceObject)
53 {
54 CODEC_LOGI("HdfCodecComponentManagerDriverInit enter");
55 OHOS::Codec::Omx::CodecComponentConfig::GetInstance()->Init(*deviceObject->property);
56 return HDF_SUCCESS;
57 }
58
HdfCodecComponentManagerDriverBind(struct HdfDeviceObject * deviceObject)59 static int HdfCodecComponentManagerDriverBind(struct HdfDeviceObject *deviceObject)
60 {
61 CODEC_LOGI("HdfCodecComponentManagerDriverBind enter");
62
63 auto *hdfCodecComponentManagerHost = new (std::nothrow) HdfCodecComponentManagerHost;
64 if (hdfCodecComponentManagerHost == nullptr) {
65 CODEC_LOGE("failed to create create HdfCodecComponentManagerHost object");
66 return HDF_FAILURE;
67 }
68
69 hdfCodecComponentManagerHost->ioService.Dispatch = CodecComponentManagerDriverDispatch;
70 hdfCodecComponentManagerHost->ioService.Open = NULL;
71 hdfCodecComponentManagerHost->ioService.Release = NULL;
72
73 auto serviceImpl = ICodecComponentManager::Get(true);
74 if (serviceImpl == nullptr) {
75 CODEC_LOGE("failed to get of implement service");
76 delete hdfCodecComponentManagerHost;
77 return HDF_FAILURE;
78 }
79
80 hdfCodecComponentManagerHost->stub =
81 OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl, ICodecComponentManager::GetDescriptor());
82 if (hdfCodecComponentManagerHost->stub == nullptr) {
83 CODEC_LOGE("failed to get stub object");
84 delete hdfCodecComponentManagerHost;
85 return HDF_FAILURE;
86 }
87
88 deviceObject->service = &hdfCodecComponentManagerHost->ioService;
89 return HDF_SUCCESS;
90 }
91
HdfCodecComponentManagerDriverRelease(struct HdfDeviceObject * deviceObject)92 static void HdfCodecComponentManagerDriverRelease(struct HdfDeviceObject *deviceObject)
93 {
94 CODEC_LOGI("HdfCodecComponentManagerDriverRelease enter");
95 if (deviceObject->service == nullptr) {
96 CODEC_LOGE("HdfCodecComponentManagerDriverRelease not initted");
97 return;
98 }
99
100 auto *hdfCodecComponentManagerHost =
101 CONTAINER_OF(deviceObject->service, struct HdfCodecComponentManagerHost, ioService);
102 delete hdfCodecComponentManagerHost;
103 }
104
105 static struct HdfDriverEntry g_codeccomponentmanagerDriverEntry = {
106 .moduleVersion = 1,
107 .moduleName = "libcodec_driver.z.so",
108 .Bind = HdfCodecComponentManagerDriverBind,
109 .Init = HdfCodecComponentManagerDriverInit,
110 .Release = HdfCodecComponentManagerDriverRelease,
111 };
112
113 #ifndef __cplusplus
114 extern "C" {
115 #endif
116 HDF_INIT(g_codeccomponentmanagerDriverEntry);
117 #ifndef __cplusplus
118 }
119 #endif
120