1 /*
2  * Copyright (C) 2023 Huawei Device 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 "image_receiver_mdk.h"
17 
18 #include "common_utils.h"
19 #include "image_receiver_mdk_kits.h"
20 
21 using namespace OHOS::Media;
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 struct ImageReceiverNative_ {
26     ImageReceiverNapi* napi = nullptr;
27     napi_env env = nullptr;
28 };
29 
30 MIDK_EXPORT
OH_Image_Receiver_InitImageReceiverNative(napi_env env,napi_value source)31 ImageReceiverNative* OH_Image_Receiver_InitImageReceiverNative(napi_env env, napi_value source)
32 {
33     ImageReceiverNapi* napi = ImageReceiver_Unwrap(env, source);
34     if (napi == nullptr) {
35         return nullptr;
36     }
37     std::unique_ptr<ImageReceiverNative> result = std::make_unique<ImageReceiverNative>();
38     result->napi = napi;
39     result->env = env;
40     return result.release();
41 }
42 
43 MIDK_EXPORT
OH_Image_Receiver_CreateImageReceiver(napi_env env,struct OhosImageReceiverInfo info,napi_value * res)44 int32_t OH_Image_Receiver_CreateImageReceiver(napi_env env,
45     struct OhosImageReceiverInfo info, napi_value* res)
46 {
47     ImageReceiverArgs args;
48     args.inNum0 = info.width;
49     args.inNum1 = info.height;
50     args.inNum2 = info.format;
51     args.inNum3 = info.capicity;
52     args.outValue = res;
53     return ImageReceiverNativeEnvCall(ENV_FUNC_IMAGE_RECEIVER_CREATE, env, &args);
54 }
55 
56 MIDK_EXPORT
OH_Image_Receiver_GetReceivingSurfaceId(const ImageReceiverNative * native,char * id,size_t len)57 int32_t OH_Image_Receiver_GetReceivingSurfaceId(const ImageReceiverNative* native, char* id, size_t len)
58 {
59     if (native == nullptr || native->napi == nullptr) {
60         return IMAGE_RESULT_BAD_PARAMETER;
61     }
62     ImageReceiverArgs args;
63     args.id = id;
64     args.inLen = len;
65     return ImageReceiverNativeCtxCall(CTX_FUNC_IMAGE_RECEIVER_GET_RECEIVER_ID, native->napi, &args);
66 }
67 
68 MIDK_EXPORT
OH_Image_Receiver_ReadLatestImage(const ImageReceiverNative * native,napi_value * image)69 int32_t OH_Image_Receiver_ReadLatestImage(const ImageReceiverNative* native, napi_value* image)
70 {
71     if (native == nullptr || native->napi == nullptr || native->env == nullptr) {
72         return IMAGE_RESULT_BAD_PARAMETER;
73     }
74     ImageReceiverArgs args;
75     args.outValue = image;
76     args.inEnv = native->env;
77     return ImageReceiverNativeCtxCall(CTX_FUNC_IMAGE_RECEIVER_READ_LATEST_IMAGE, native->napi, &args);
78 }
79 
80 MIDK_EXPORT
OH_Image_Receiver_ReadNextImage(const ImageReceiverNative * native,napi_value * image)81 int32_t OH_Image_Receiver_ReadNextImage(const ImageReceiverNative* native, napi_value* image)
82 {
83     if (native == nullptr || native->napi == nullptr || native->env == nullptr) {
84         return IMAGE_RESULT_BAD_PARAMETER;
85     }
86     ImageReceiverArgs args;
87     args.outValue = image;
88     args.inEnv = native->env;
89     return ImageReceiverNativeCtxCall(CTX_FUNC_IMAGE_RECEIVER_READ_NEXT_IMAGE, native->napi, &args);
90 }
91 
92 MIDK_EXPORT
OH_Image_Receiver_On(const ImageReceiverNative * native,OH_Image_Receiver_On_Callback callback)93 int32_t OH_Image_Receiver_On(const ImageReceiverNative* native, OH_Image_Receiver_On_Callback callback)
94 {
95     if (native == nullptr || native->napi == nullptr || callback == nullptr) {
96         return IMAGE_RESULT_BAD_PARAMETER;
97     }
98     ImageReceiverArgs args;
99     args.callback = callback;
100     return ImageReceiverNativeCtxCall(CTX_FUNC_IMAGE_RECEIVER_ON, native->napi, &args);
101 }
102 
103 MIDK_EXPORT
OH_Image_Receiver_GetSize(const ImageReceiverNative * native,struct OhosImageSize * size)104 int32_t OH_Image_Receiver_GetSize(const ImageReceiverNative* native, struct OhosImageSize* size)
105 {
106     if (native == nullptr || native->napi == nullptr) {
107         return IMAGE_RESULT_BAD_PARAMETER;
108     }
109     ImageReceiverArgs args;
110     args.outSize = size;
111     return ImageReceiverNativeCtxCall(CTX_FUNC_IMAGE_RECEIVER_GET_SIZE, native->napi, &args);
112 }
113 
114 MIDK_EXPORT
OH_Image_Receiver_GetCapacity(const ImageReceiverNative * native,int32_t * capacity)115 int32_t OH_Image_Receiver_GetCapacity(const ImageReceiverNative* native, int32_t* capacity)
116 {
117     if (native == nullptr || native->napi == nullptr) {
118         return IMAGE_RESULT_BAD_PARAMETER;
119     }
120     ImageReceiverArgs args;
121     args.outNum0 = capacity;
122     return ImageReceiverNativeCtxCall(CTX_FUNC_IMAGE_RECEIVER_GET_CAPACITY, native->napi, &args);
123 }
124 
125 MIDK_EXPORT
OH_Image_Receiver_GetFormat(const ImageReceiverNative * native,int32_t * format)126 int32_t OH_Image_Receiver_GetFormat(const ImageReceiverNative* native, int32_t* format)
127 {
128     if (native == nullptr || native->napi == nullptr) {
129         return IMAGE_RESULT_BAD_PARAMETER;
130     }
131     ImageReceiverArgs args;
132     args.outNum0 = format;
133     return ImageReceiverNativeCtxCall(CTX_FUNC_IMAGE_RECEIVER_GET_FORMAT, native->napi, &args);
134 }
135 
136 MIDK_EXPORT
OH_Image_Receiver_Release(ImageReceiverNative * native)137 int32_t OH_Image_Receiver_Release(ImageReceiverNative* native)
138 {
139     if (native != nullptr) {
140         delete native;
141     }
142     return IMAGE_RESULT_SUCCESS;
143 }
144 #ifdef __cplusplus
145 };
146 #endif
147