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 <mutex> 20 #include <unordered_map> 21 #include "camera_napi_event_emitter.h" 22 #include "camera_napi_event_listener.h" 23 #include "camera_napi_template_utils.h" 24 #include "camera_napi_utils.h" 25 #include "image_receiver.h" 26 #include "input/camera_manager.h" 27 #include "js_native_api_types.h" 28 #include "listener_base.h" 29 #include "output/camera_output_capability.h" 30 #include "output/preview_output.h" 31 #include "surface_utils.h" 32 namespace OHOS { 33 namespace CameraStandard { 34 struct PreviewOutputAsyncContext; 35 static const char CAMERA_PREVIEW_OUTPUT_NAPI_CLASS_NAME[] = "PreviewOutput"; 36 37 enum PreviewOutputEventType { 38 PREVIEW_FRAME_START, 39 PREVIEW_FRAME_END, 40 PREVIEW_FRAME_ERROR, 41 SKETCH_STATUS_CHANGED, 42 PREVIEW_INVALID_TYPE 43 }; 44 45 static EnumHelper<PreviewOutputEventType> PreviewOutputEventTypeHelper({ 46 {PREVIEW_FRAME_START, CONST_PREVIEW_FRAME_START}, 47 {PREVIEW_FRAME_END, CONST_PREVIEW_FRAME_END}, 48 {PREVIEW_FRAME_ERROR, CONST_PREVIEW_FRAME_ERROR}, 49 {SKETCH_STATUS_CHANGED, CONST_SKETCH_STATUS_CHANGED} 50 }, 51 PreviewOutputEventType::PREVIEW_INVALID_TYPE 52 ); 53 54 class PreviewOutputCallback : public PreviewStateCallback, 55 public ListenerBase, 56 public std::enable_shared_from_this<PreviewOutputCallback> { 57 public: 58 explicit PreviewOutputCallback(napi_env env); 59 ~PreviewOutputCallback() = default; 60 61 void OnFrameStarted() const override; 62 void OnFrameEnded(const int32_t frameCount) const override; 63 void OnError(const int32_t errorCode) const override; 64 void OnSketchStatusDataChanged(const SketchStatusData& sketchStatusData) const override; 65 66 private: 67 void UpdateJSCallback(PreviewOutputEventType eventType, const int32_t value) const; 68 void UpdateJSCallbackAsync(PreviewOutputEventType eventType, const int32_t value) const; 69 void OnSketchStatusDataChangedAsync(SketchStatusData sketchStatusData) const; 70 void OnSketchStatusDataChangedCall(SketchStatusData sketchStatusData) const; 71 }; 72 73 struct PreviewOutputCallbackInfo { 74 PreviewOutputEventType eventType_; 75 int32_t value_; 76 weak_ptr<const PreviewOutputCallback> listener_; PreviewOutputCallbackInfoPreviewOutputCallbackInfo77 PreviewOutputCallbackInfo(PreviewOutputEventType eventType, int32_t value, 78 shared_ptr<const PreviewOutputCallback> listener) 79 : eventType_(eventType), value_(value), listener_(listener) {} 80 }; 81 82 struct SketchStatusCallbackInfo { 83 SketchStatusData sketchStatusData_; 84 weak_ptr<const PreviewOutputCallback> listener_; 85 napi_env env_; SketchStatusCallbackInfoSketchStatusCallbackInfo86 SketchStatusCallbackInfo(SketchStatusData sketchStatusData, shared_ptr<const PreviewOutputCallback> 87 listener, napi_env env) 88 : sketchStatusData_(sketchStatusData), listener_(listener), env_(env) 89 {} 90 }; 91 92 class PreviewOutputNapi : public CameraNapiEventEmitter<PreviewOutputNapi>, 93 public CameraNapiEventListener<PreviewOutputCallback> { 94 public: 95 static napi_value Init(napi_env env, napi_value exports); 96 static napi_value CreatePreviewOutput(napi_env env, Profile& profile, std::string surfaceId); 97 static napi_value CreatePreviewOutput(napi_env env, std::string surfaceId); 98 static napi_value CreateDeferredPreviewOutput(napi_env env, Profile& profile); 99 static bool IsPreviewOutput(napi_env env, napi_value obj); 100 static napi_value AddDeferredSurface(napi_env env, napi_callback_info info); 101 static napi_value Start(napi_env env, napi_callback_info info); 102 static napi_value Stop(napi_env env, napi_callback_info info); 103 static napi_value Release(napi_env env, napi_callback_info info); 104 static napi_value IsSketchSupported(napi_env env, napi_callback_info info); 105 static napi_value GetSketchRatio(napi_env env, napi_callback_info info); 106 static napi_value EnableSketch(napi_env env, napi_callback_info info); 107 static napi_value AttachSketchSurface(napi_env env, napi_callback_info info); 108 static napi_value SetFrameRate(napi_env env, napi_callback_info info); 109 static napi_value GetActiveFrameRate(napi_env env, napi_callback_info info); 110 static napi_value GetSupportedFrameRates(napi_env env, napi_callback_info info); 111 sptr<PreviewOutput> GetPreviewOutput(); 112 static napi_value GetActiveProfile(napi_env env, napi_callback_info info); 113 static napi_value On(napi_env env, napi_callback_info info); 114 static napi_value Once(napi_env env, napi_callback_info info); 115 static napi_value Off(napi_env env, napi_callback_info info); 116 static napi_value GetPreviewRotation(napi_env env, napi_callback_info info); 117 static napi_value SetPreviewRotation(napi_env env, napi_callback_info info); 118 119 const EmitterFunctions& GetEmitterFunctions() override; 120 121 PreviewOutputNapi(); 122 ~PreviewOutputNapi() override; 123 124 private: 125 static void PreviewOutputNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint); 126 static napi_value PreviewOutputNapiConstructor(napi_env env, napi_callback_info info); 127 128 void RegisterFrameStartCallbackListener(const std::string& eventName, napi_env env, napi_value callback, 129 const std::vector<napi_value>& args, bool isOnce); 130 void RegisterFrameEndCallbackListener(const std::string& eventName, napi_env env, napi_value callback, 131 const std::vector<napi_value>& args, bool isOnce); 132 void RegisterErrorCallbackListener(const std::string& eventName, napi_env env, napi_value callback, 133 const std::vector<napi_value>& args, bool isOnce); 134 void UnregisterCommonCallbackListener( 135 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args); 136 void RegisterSketchStatusChangedCallbackListener(const std::string& eventName, napi_env env, napi_value callback, 137 const std::vector<napi_value>& args, bool isOnce); 138 void UnregisterSketchStatusChangedCallbackListener( 139 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args); 140 141 napi_env env_; 142 sptr<PreviewOutput> previewOutput_; 143 144 static thread_local napi_ref sConstructor_; 145 static thread_local sptr<PreviewOutput> sPreviewOutput_; 146 static thread_local uint32_t previewOutputTaskId; 147 }; 148 149 struct PreviewOutputAsyncContext : public AsyncContext { PreviewOutputAsyncContextPreviewOutputAsyncContext150 PreviewOutputAsyncContext(std::string funcName, int32_t taskId) : AsyncContext(funcName, taskId) {}; 151 PreviewOutputNapi* objectInfo = nullptr; 152 std::string surfaceId; 153 }; 154 } // namespace CameraStandard 155 } // namespace OHOS 156 #endif /* PREVIEW_OUTPUT_NAPI_H_ */ 157