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