1 /* 2 * Copyright (c) 2021-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 CAMERA_INPUT_NAPI_H_ 17 #define CAMERA_INPUT_NAPI_H_ 18 19 #include <securec.h> 20 21 #include "display_type.h" 22 #include "camera_log.h" 23 #include "napi/native_api.h" 24 #include "napi/native_node_api.h" 25 #include "hilog/log.h" 26 #include "camera_napi_utils.h" 27 28 #include "input/camera_manager.h" 29 #include "input/camera_input.h" 30 31 #include <fstream> 32 #include <iostream> 33 #include <sstream> 34 #include <vector> 35 36 #include <fcntl.h> 37 #include <sys/stat.h> 38 #include <sys/time.h> 39 #include <sys/types.h> 40 41 #include "input/camera_size_napi.h" 42 43 namespace OHOS { 44 namespace CameraStandard { 45 static const char CAMERA_INPUT_NAPI_CLASS_NAME[] = "CameraInput"; 46 47 enum InputAsyncCallbackModes { 48 DEEAULT_ASYNC_CALLBACK = -1, 49 OPEN_ASYNC_CALLBACK = 1, 50 CLOSE_ASYNC_CALLBACK = 2, 51 RELEASE_ASYNC_CALLBACK = 3, 52 }; 53 54 struct CameraInputAsyncContext; 55 56 class ErrorCallbackListener : public ErrorCallback { 57 public: ErrorCallbackListener(napi_env env,napi_ref ref)58 ErrorCallbackListener(napi_env env, napi_ref ref) : env_(env), callbackRef_(ref) {} 59 ~ErrorCallbackListener() = default; 60 void OnError(const int32_t errorType, const int32_t errorMsg) const override; 61 62 private: 63 void OnErrorCallback(const int32_t errorType, const int32_t errorMsg) const; 64 void OnErrorCallbackAsync(const int32_t errorType, const int32_t errorMsg) const; 65 66 napi_env env_; 67 napi_ref callbackRef_ = nullptr; 68 }; 69 70 struct ErrorCallbackInfo { 71 int32_t errorType_; 72 int32_t errorMsg_; 73 const ErrorCallbackListener* listener_; ErrorCallbackInfoErrorCallbackInfo74 ErrorCallbackInfo(int32_t errorType, int32_t errorMsg, const ErrorCallbackListener* listener) 75 : errorType_(errorType), errorMsg_(errorMsg), listener_(listener) {} 76 }; 77 78 class CameraInputNapi { 79 public: 80 static napi_value Init(napi_env env, napi_value exports); 81 static napi_value CreateCameraInput(napi_env env, sptr<CameraInput> cameraInput); 82 CameraInputNapi(); 83 ~CameraInputNapi(); 84 85 static void NapiCreateInt32Logs( 86 napi_env env, int32_t contextMode, std::unique_ptr<JSAsyncContextOutput> &jsContext); 87 static void NapiCreateDoubleLogs( 88 napi_env env, double contextMode, std::unique_ptr<JSAsyncContextOutput> &jsContext); 89 90 static napi_value Open(napi_env env, napi_callback_info info); 91 static napi_value Close(napi_env env, napi_callback_info info); 92 static napi_value Release(napi_env env, napi_callback_info info); 93 static napi_value On(napi_env env, napi_callback_info info); 94 95 sptr<CameraInput> GetCameraInput(); 96 private: 97 static void CameraInputNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint); 98 static napi_value CameraInputNapiConstructor(napi_env env, napi_callback_info info); 99 100 napi_env env_; 101 napi_ref wrapper_; 102 std::string cameraId_; 103 sptr<CameraInput> cameraInput_; 104 105 void RegisterCallback(napi_env env, const std::string &eventType, napi_ref callbackRef); 106 107 static thread_local napi_ref sConstructor_; 108 static thread_local std::string sCameraId_; 109 static thread_local sptr<CameraInput> sCameraInput_; 110 static thread_local uint32_t cameraInputTaskId; 111 }; 112 113 struct CameraInputAsyncContext :public AsyncContext { 114 CameraInputNapi* objectInfo; 115 bool bRetBool; 116 bool isSupported; 117 InputAsyncCallbackModes modeForAsync; 118 std::string cameraId; 119 std::string enumType; 120 std::vector<CameraFormat> vecList; ~CameraInputAsyncContextCameraInputAsyncContext121 ~CameraInputAsyncContext() 122 { 123 objectInfo = nullptr; 124 } 125 }; 126 } // namespace CameraStandard 127 } // namespace OHOS 128 #endif /* CAMERA_INPUT_NAPI_H_ */ 129