1 /*
2  * Copyright (C) 2022 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 #ifndef INTERFACES_KITS_JS_COMMON_INCLUDE_IMAGE_RECEIVER_NAPI_H
17 #define INTERFACES_KITS_JS_COMMON_INCLUDE_IMAGE_RECEIVER_NAPI_H
18 
19 #include <cerrno>
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <ftw.h>
23 #include <securec.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <variant>
27 
28 #include "napi/native_api.h"
29 #include "napi/native_node_api.h"
30 #include "pixel_map.h"
31 #include "image_receiver.h"
32 #include "image_napi_utils.h"
33 
34 namespace OHOS {
35 namespace Media {
36 struct ImageReceiverCommonArgs;
37 struct ImageReceiverAsyncContext;
38 using Context = ImageReceiverAsyncContext* ;
39 using CompleteCallback = void (*)(napi_env env, napi_status status, Context context);
40 struct ImageReceiverCreateArgs {
41     int32_t width;
42     int32_t height;
43     int32_t format;
44     int32_t capicity;
45 };
46 class ImageReceiverNapi {
47 public:
48     ImageReceiverNapi();
49     ~ImageReceiverNapi();
50     static napi_value Init(napi_env env, napi_value exports);
51     static void DoCallBack(std::shared_ptr<ImageReceiverAsyncContext> &context,
52                            std::string name,
53                            CompleteCallback callBack);
54     ImageReceiver* GetNative();
55     static napi_value CreateImageReceiverJsObject(napi_env env, struct ImageReceiverCreateArgs args);
56     void NativeRelease();
57     void UnRegisterReceiverListener();
58 #ifdef IMAGE_DEBUG_FLAG
59     bool isCallBackTest = false;
60 #endif
61 
62 private:
63     static napi_value Constructor(napi_env env, napi_callback_info info);
64     static void Destructor(napi_env env, void *nativeObject, void *finalize);
65 
66     static napi_value JSCreateImageReceiver(napi_env env, napi_callback_info info);
67     static napi_value JsGetSize(napi_env env, napi_callback_info info);
68     static napi_value JsGetCapacity(napi_env env, napi_callback_info info);
69     static napi_value JsGetFormat(napi_env env, napi_callback_info info);
70     static napi_value JsGetReceivingSurfaceId(napi_env env, napi_callback_info info);
71     static napi_value JsReadLatestImage(napi_env env, napi_callback_info info);
72     static napi_value JsReadNextImage(napi_env env, napi_callback_info info);
73     static napi_value JsOn(napi_env env, napi_callback_info info);
74     static napi_value JsOff(napi_env env, napi_callback_info info);
75     static napi_value JsRelease(napi_env env, napi_callback_info info);
76 
77     static bool GetNativeFromEnv(napi_env env, napi_callback_info info, std::shared_ptr<ImageReceiver> &native);
78     static napi_value JSCommonProcess(ImageReceiverCommonArgs &args);
79     static napi_value JsOffOneArg(napi_env env, napi_callback_info info);
80     static napi_value JsOffTwoArgs(napi_env env, napi_callback_info info);
81 #ifdef IMAGE_DEBUG_FLAG
82     static napi_value JsTest(napi_env env, napi_callback_info info);
83     static napi_value JsCheckDeviceTest(napi_env env, napi_callback_info info);
84     static napi_value JsTestYUV(napi_env env, napi_callback_info info);
85 #endif
86     void release();
87     static thread_local napi_ref sConstructor_;
88     static std::shared_ptr<ImageReceiver> staticInstance_;
89 
90     napi_env env_ = nullptr;
91     std::shared_ptr<ImageReceiver> imageReceiver_;
92     bool isRelease = false;
93 };
94 struct ImageReceiverAsyncContext {
95     napi_env env = nullptr;
96     napi_async_work work = nullptr;
97     napi_deferred deferred = nullptr;
98     napi_ref callbackRef = nullptr;
99     ImageReceiverNapi *constructor_ = nullptr;
100     uint32_t status;
101 };
102 struct ImageReceiverInnerContext {
103     napi_status status;
104     napi_value result = nullptr;
105     napi_value thisVar = nullptr;
106     size_t argc;
107     std::vector<napi_value> argv;
108     std::string onType;
109     int32_t refCount = 1;
110     std::unique_ptr<ImageReceiverAsyncContext> context = nullptr;
111 };
112 
113 using CommonFunc = bool (*)(ImageReceiverCommonArgs &args, ImageReceiverInnerContext &ic);
114 
115 enum class CallType : uint32_t {
116     STATIC = 0,
117     GETTER = 1,
118     ASYNC = 2,
119 };
120 
121 struct ImageReceiverCommonArgs {
122     napi_env env;
123     napi_callback_info info;
124     CallType async;
125     const std::string name;
126     CompleteCallback callBack;
127     size_t argc;
128     CommonFunc queryArgs;
129     CommonFunc nonAsyncBack;
130     bool asyncLater = false;
131 };
132 
133 class ImageReceiverAvaliableListener : public SurfaceBufferAvaliableListener {
134 public:
~ImageReceiverAvaliableListener()135     ~ImageReceiverAvaliableListener() override
136     {
137         if (context && context->env && context->callbackRef) {
138             napi_delete_reference(context->env, context->callbackRef);
139         }
140         if (context) {
141             context->callbackRef = nullptr;
142         }
143         context = nullptr;
144         callBack = nullptr;
145     }
OnSurfaceBufferAvaliable()146     void OnSurfaceBufferAvaliable() override
147     {
148         ImageReceiverNapi::DoCallBack(context, name, callBack);
149     }
150     std::shared_ptr<ImageReceiverAsyncContext> context = nullptr;
151     std::string name;
152     CompleteCallback callBack = nullptr;
153 };
154 } // namespace Media
155 } // namespace OHOS
156 #endif // INTERFACES_KITS_JS_COMMON_INCLUDE_IMAGE_RECEIVER_NAPI_H
157