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 #ifdef IMAGE_DEBUG_FLAG 58 bool isCallBackTest = false; 59 #endif 60 61 private: 62 static napi_value Constructor(napi_env env, napi_callback_info info); 63 static void Destructor(napi_env env, void *nativeObject, void *finalize); 64 65 static napi_value JSCreateImageReceiver(napi_env env, napi_callback_info info); 66 static napi_value JsGetSize(napi_env env, napi_callback_info info); 67 static napi_value JsGetCapacity(napi_env env, napi_callback_info info); 68 static napi_value JsGetFormat(napi_env env, napi_callback_info info); 69 static napi_value JsGetReceivingSurfaceId(napi_env env, napi_callback_info info); 70 static napi_value JsReadLatestImage(napi_env env, napi_callback_info info); 71 static napi_value JsReadNextImage(napi_env env, napi_callback_info info); 72 static napi_value JsOn(napi_env env, napi_callback_info info); 73 static napi_value JsRelease(napi_env env, napi_callback_info info); 74 75 static bool GetNativeFromEnv(napi_env env, napi_callback_info info, std::shared_ptr<ImageReceiver> &native); 76 static napi_value JSCommonProcess(ImageReceiverCommonArgs &args); 77 #ifdef IMAGE_DEBUG_FLAG 78 static napi_value JsTest(napi_env env, napi_callback_info info); 79 static napi_value JsCheckDeviceTest(napi_env env, napi_callback_info info); 80 static napi_value JsTestYUV(napi_env env, napi_callback_info info); 81 #endif 82 void release(); 83 static thread_local napi_ref sConstructor_; 84 static std::shared_ptr<ImageReceiver> staticInstance_; 85 86 napi_env env_ = nullptr; 87 std::shared_ptr<ImageReceiver> imageReceiver_; 88 bool isRelease = false; 89 }; 90 struct ImageReceiverAsyncContext { 91 napi_env env = nullptr; 92 napi_async_work work = nullptr; 93 napi_deferred deferred = nullptr; 94 napi_ref callbackRef = nullptr; 95 ImageReceiverNapi *constructor_ = nullptr; 96 std::shared_ptr<ImageReceiver> receiver_ = nullptr; 97 uint32_t status; 98 }; 99 struct ImageReceiverInnerContext { 100 napi_status status; 101 napi_value result = nullptr; 102 napi_value thisVar = nullptr; 103 size_t argc; 104 std::vector<napi_value> argv; 105 std::string onType; 106 int32_t refCount = 1; 107 std::unique_ptr<ImageReceiverAsyncContext> context = nullptr; 108 }; 109 110 using CommonFunc = bool (*)(ImageReceiverCommonArgs &args, ImageReceiverInnerContext &ic); 111 112 enum class CallType : uint32_t { 113 STATIC = 0, 114 GETTER = 1, 115 ASYNC = 2, 116 }; 117 118 struct ImageReceiverCommonArgs { 119 napi_env env; 120 napi_callback_info info; 121 CallType async; 122 const std::string name; 123 CompleteCallback callBack; 124 size_t argc; 125 CommonFunc queryArgs; 126 CommonFunc nonAsyncBack; 127 bool asyncLater = false; 128 }; 129 130 class ImageReceiverAvaliableListener : public SurfaceBufferAvaliableListener { 131 public: ~ImageReceiverAvaliableListener()132 ~ImageReceiverAvaliableListener() override 133 { 134 if (context && context->env && context->callbackRef) { 135 napi_delete_reference(context->env, context->callbackRef); 136 } 137 context = nullptr; 138 callBack = nullptr; 139 } OnSurfaceBufferAvaliable()140 void OnSurfaceBufferAvaliable() override 141 { 142 ImageReceiverNapi::DoCallBack(context, name, callBack); 143 } 144 std::shared_ptr<ImageReceiverAsyncContext> context = nullptr; 145 std::string name; 146 CompleteCallback callBack = nullptr; 147 }; 148 } // namespace Media 149 } // namespace OHOS 150 #endif // INTERFACES_KITS_JS_COMMON_INCLUDE_IMAGE_RECEIVER_NAPI_H_ 151