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 PREVIEW_OUTPUT_NAPI_H_ 17 #define PREVIEW_OUTPUT_NAPI_H_ 18 19 #include <cinttypes> 20 #include <fstream> 21 #include <iostream> 22 #include <sstream> 23 #include <vector> 24 25 #include <fcntl.h> 26 #include <securec.h> 27 #include <sys/stat.h> 28 #include <sys/time.h> 29 #include <sys/types.h> 30 31 #include "camera_log.h" 32 #include "camera_napi_utils.h" 33 #include "camera_output_napi.h" 34 #include "image_receiver.h" 35 #include "surface_utils.h" 36 37 #include "napi/native_api.h" 38 #include "napi/native_node_api.h" 39 #include "input/camera_manager.h" 40 #include "output/camera_output_capability.h" 41 #include "output/preview_output.h" 42 #include "hilog/log.h" 43 44 namespace OHOS { 45 namespace CameraStandard { 46 struct PreviewOutputAsyncContext; 47 static const char CAMERA_PREVIEW_OUTPUT_NAPI_CLASS_NAME[] = "PreviewOutput"; 48 49 class PreviewOutputCallback : public PreviewStateCallback { 50 public: 51 explicit PreviewOutputCallback(napi_env env); 52 ~PreviewOutputCallback() = default; 53 54 void OnFrameStarted() const override; 55 void OnFrameEnded(const int32_t frameCount) const override; 56 void OnError(const int32_t errorCode) const override; 57 void SetCallbackRef(const std::string &eventType, const napi_ref &callbackRef); 58 59 private: 60 void UpdateJSCallback(std::string propName, const int32_t value) const; 61 void UpdateJSCallbackAsync(std::string propName, const int32_t value) const; 62 63 napi_env env_; 64 napi_ref frameStartCallbackRef_ = nullptr; 65 napi_ref frameEndCallbackRef_ = nullptr; 66 napi_ref errorCallbackRef_ = nullptr; 67 }; 68 69 struct PreviewOutputCallbackInfo { 70 std::string eventName_; 71 int32_t value_; 72 const PreviewOutputCallback* listener_; PreviewOutputCallbackInfoPreviewOutputCallbackInfo73 PreviewOutputCallbackInfo(std::string eventName, int32_t value, const PreviewOutputCallback* listener) 74 : eventName_(eventName), value_(value), listener_(listener) {} 75 }; 76 77 class PreviewOutputNapi : public CameraOutputNapi { 78 public: 79 static napi_value Init(napi_env env, napi_value exports); 80 static napi_value CreatePreviewOutput(napi_env env, Profile &profile, std::string surfaceId); 81 static napi_value CreateDeferredPreviewOutput(napi_env env, Profile &profile); 82 static bool IsPreviewOutput(napi_env env, napi_value obj); 83 static napi_value AddDeferredSurface(napi_env env, napi_callback_info info); 84 static napi_value Start(napi_env env, napi_callback_info info); 85 static napi_value Stop(napi_env env, napi_callback_info info); 86 static napi_value Release(napi_env env, napi_callback_info info); 87 static napi_value On(napi_env env, napi_callback_info info); 88 sptr<PreviewOutput> GetPreviewOutput(); 89 90 PreviewOutputNapi(); 91 ~PreviewOutputNapi(); 92 private: 93 static void PreviewOutputNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint); 94 static napi_value PreviewOutputNapiConstructor(napi_env env, napi_callback_info info); 95 static napi_status CreateAsyncTask(napi_env env, napi_value resource, 96 std::unique_ptr<OHOS::CameraStandard::PreviewOutputAsyncContext> &asyncContext); 97 98 napi_env env_; 99 napi_ref wrapper_; 100 sptr<PreviewOutput> previewOutput_; 101 102 static thread_local napi_ref sConstructor_; 103 static thread_local sptr<PreviewOutput> sPreviewOutput_; 104 std::shared_ptr<PreviewOutputCallback> previewCallback_; 105 static thread_local uint32_t previewOutputTaskId; 106 }; 107 108 struct PreviewOutputAsyncContext : public AsyncContext { 109 PreviewOutputNapi* objectInfo; 110 bool bRetBool; 111 std::string surfaceId; ~PreviewOutputAsyncContextPreviewOutputAsyncContext112 ~PreviewOutputAsyncContext() 113 { 114 objectInfo = nullptr; 115 } 116 }; 117 } // namespace CameraStandard 118 } // namespace OHOS 119 #endif /* PREVIEW_OUTPUT_NAPI_H_ */ 120