1 /* 2 * Copyright (C) 2024 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_SENDABLE_IMAGE_RECEIVER_NAPI_H 17 #define INTERFACES_KITS_JS_COMMON_INCLUDE_SENDABLE_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 SendableImageReceiverCommonArgs; 37 struct SendableImageReceiverAsyncContext; 38 using Context = SendableImageReceiverAsyncContext* ; 39 using CompleteCallback = void (*)(napi_env env, napi_status status, Context context); 40 struct SendableImageReceiverCreateArgs { 41 int32_t width; 42 int32_t height; 43 int32_t format; 44 int32_t capicity; 45 }; 46 class SendableImageReceiverNapi { 47 public: 48 SendableImageReceiverNapi(); 49 ~SendableImageReceiverNapi(); 50 static napi_value Init(napi_env env, napi_value exports); 51 static void DoCallBack(std::shared_ptr<SendableImageReceiverAsyncContext> &context, 52 std::string name, 53 CompleteCallback callBack); 54 ImageReceiver* GetNative(); 55 static napi_value CreateImageReceiverJsObject(napi_env env, struct SendableImageReceiverCreateArgs 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 JsRelease(napi_env env, napi_callback_info info); 75 76 static bool GetNativeFromEnv(napi_env env, napi_callback_info info, std::shared_ptr<ImageReceiver> &native); 77 static napi_value JSCommonProcess(SendableImageReceiverCommonArgs &args); 78 #ifdef IMAGE_DEBUG_FLAG 79 static napi_value JsTest(napi_env env, napi_callback_info info); 80 static napi_value JsCheckDeviceTest(napi_env env, napi_callback_info info); 81 static napi_value JsTestYUV(napi_env env, napi_callback_info info); 82 #endif 83 void release(); 84 static thread_local napi_ref sConstructor_; 85 static std::shared_ptr<ImageReceiver> staticInstance_; 86 87 napi_env env_ = nullptr; 88 std::shared_ptr<ImageReceiver> imageReceiver_; 89 bool isRelease = false; 90 }; 91 struct SendableImageReceiverAsyncContext { 92 napi_env env = nullptr; 93 napi_async_work work = nullptr; 94 napi_deferred deferred = nullptr; 95 napi_ref callbackRef = nullptr; 96 SendableImageReceiverNapi *constructor_ = nullptr; 97 uint32_t status; 98 }; 99 struct SendableImageReceiverInnerContext { 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<SendableImageReceiverAsyncContext> context = nullptr; 108 }; 109 110 using CommonFunc = bool (*)(SendableImageReceiverCommonArgs &args, SendableImageReceiverInnerContext &ic); 111 112 enum class CallType : uint32_t { 113 STATIC = 0, 114 GETTER = 1, 115 ASYNC = 2, 116 }; 117 118 struct SendableImageReceiverCommonArgs { 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 SendableImageReceiverAvaliableListener : public SurfaceBufferAvaliableListener { 131 public: ~SendableImageReceiverAvaliableListener()132 ~SendableImageReceiverAvaliableListener() override 133 { 134 if (context && context->env && context->callbackRef) { 135 napi_delete_reference(context->env, context->callbackRef); 136 } 137 if (context) { 138 context->callbackRef = nullptr; 139 } 140 context = nullptr; 141 callBack = nullptr; 142 } OnSurfaceBufferAvaliable()143 void OnSurfaceBufferAvaliable() override 144 { 145 SendableImageReceiverNapi::DoCallBack(context, name, callBack); 146 } 147 std::shared_ptr<SendableImageReceiverAsyncContext> context = nullptr; 148 std::string name; 149 CompleteCallback callBack = nullptr; 150 }; 151 } // namespace Media 152 } // namespace OHOS 153 #endif // INTERFACES_KITS_JS_COMMON_INCLUDE_SENDABLE_IMAGE_RECEIVER_NAPI_H 154