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