• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 DEPTH_DATA_OUTPUT_NAPI_H_
17 #define DEPTH_DATA_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 "depth_data_napi.h"
23 #include "listener_base.h"
24 #include "input/camera_manager.h"
25 #include "output/camera_output_capability.h"
26 #include "output/depth_data_output.h"
27 #include "surface_utils.h"
28 
29 namespace OHOS {
30 namespace CameraStandard {
31 static const std::string CONST_DEPTH_DATA_AVAILABLE = "depthDataAvailable";
32 static const std::string CONST_DEPTH_DATA_ERROR = "error";
33 
34 static const char CAMERA_DEPTH_DATA_OUTPUT_NAPI_CLASS_NAME[] = "DepthDataOutput";
35 
36 enum DepthDataOutputEventType {
37     DEPTH_DATA_AVAILABLE,
38     DEPTH_DATA_ERROR,
39     DEPTH_DATA_INVALID_TYPE
40 };
41 
42 static EnumHelper<DepthDataOutputEventType> DepthDataOutputEventTypeHelper({
43         {DEPTH_DATA_AVAILABLE, CONST_DEPTH_DATA_AVAILABLE},
44         {DEPTH_DATA_ERROR, CONST_DEPTH_DATA_ERROR}
45     },
46     DepthDataOutputEventType::DEPTH_DATA_INVALID_TYPE
47 );
48 
49 class DepthDataBufferProcessor {
50 public:
DepthDataBufferProcessor(sptr<Surface> depthDataSurface)51     explicit DepthDataBufferProcessor(sptr<Surface> depthDataSurface) : depthDataSurface_(depthDataSurface) {}
~DepthDataBufferProcessor()52     ~DepthDataBufferProcessor()
53     {
54         depthDataSurface_ = nullptr;
55     }
Release(sptr<SurfaceBuffer> & buffer)56     void Release(sptr<SurfaceBuffer>& buffer)
57     {
58         if (depthDataSurface_ != nullptr) {
59             depthDataSurface_->ReleaseBuffer(buffer, -1);
60         }
61     }
62 
63 private:
64     sptr<Surface> depthDataSurface_ = nullptr;
65 };
66 
67 class DepthDataListener : public IBufferConsumerListener, public ListenerBase,
68     public std::enable_shared_from_this<DepthDataListener > {
69 public:
70     explicit DepthDataListener(napi_env env, const sptr<Surface> depthSurface, sptr<DepthDataOutput> depthDataOutput);
71     ~DepthDataListener() = default;
72     void OnBufferAvailable() override;
73     void SaveCallback(const std::string eventName, napi_value callback);
74     void RemoveCallback(const std::string eventName, napi_value callback);
75     void SetDepthProfile(std::shared_ptr<DepthProfile> depthProfile);
76 
77 private:
78     sptr<Surface> depthDataSurface_;
79     sptr<DepthDataOutput> depthDataOutput_;
80     std::shared_ptr<DepthDataBufferProcessor> bufferProcessor_;
81     std::shared_ptr<DepthProfile> depthProfile_;
82     void UpdateJSCallback(sptr<Surface> depthSurface) const;
83     void UpdateJSCallbackAsync(sptr<Surface> depthSurface) const;
84     void ExecuteDepthData(sptr<SurfaceBuffer> surfaceBfuffer) const;
85 };
86 
87 struct DepthDataListenerInfo {
88     sptr<Surface> depthDataSurface_;
89     weak_ptr<const DepthDataListener > listener_;
DepthDataListenerInfoDepthDataListenerInfo90     DepthDataListenerInfo(sptr<Surface> depthDataSurface, shared_ptr<const DepthDataListener > listener)
91         : depthDataSurface_(depthDataSurface), listener_(listener)
92     {}
93 };
94 
95 class DepthDataOutputCallback : public DepthDataStateCallback,
96                               public ListenerBase,
97                               public std::enable_shared_from_this<DepthDataOutputCallback> {
98 public:
99     explicit DepthDataOutputCallback(napi_env env);
100     ~DepthDataOutputCallback() = default;
101 
102     void OnDepthDataError(const int32_t errorCode) const override;
103 
104 private:
105     void UpdateJSCallback(DepthDataOutputEventType eventType, const int32_t value) const;
106     void UpdateJSCallbackAsync(DepthDataOutputEventType eventType, const int32_t value) const;
107     void ExecuteDepthData(sptr<SurfaceBuffer> surfaceBuffer) const;
108 };
109 
110 struct DepthDataOutputCallbackInfo {
111     DepthDataOutputEventType eventType_;
112     int32_t value_;
113     weak_ptr<const DepthDataOutputCallback> listener_;
DepthDataOutputCallbackInfoDepthDataOutputCallbackInfo114     DepthDataOutputCallbackInfo(DepthDataOutputEventType eventType, int32_t value,
115         shared_ptr<const DepthDataOutputCallback> listener)
116         : eventType_(eventType), value_(value), listener_(listener) {}
117 };
118 
119 struct DepthDataOutputAsyncContext;
120 class DepthDataOutputNapi : public CameraNapiEventEmitter<DepthDataOutputNapi> {
121 public:
122     static napi_value Init(napi_env env, napi_value exports);
123     static napi_value CreateDepthDataOutput(napi_env env, DepthProfile& depthProfile);
124     static napi_value Start(napi_env env, napi_callback_info info);
125     static napi_value Stop(napi_env env, napi_callback_info info);
126     static napi_value Release(napi_env env, napi_callback_info info);
127     static napi_value On(napi_env env, napi_callback_info info);
128     static napi_value Once(napi_env env, napi_callback_info info);
129     static napi_value Off(napi_env env, napi_callback_info info);
130     sptr<DepthDataOutput> GetDepthDataOutput();
131     static bool IsDepthDataOutput(napi_env env, napi_value obj);
132 
133     const EmitterFunctions& GetEmitterFunctions() override;
134 
135     DepthDataOutputNapi();
136     ~DepthDataOutputNapi() override;
137 
138 private:
139     static void DepthDataOutputNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint);
140     static napi_value DepthDataOutputNapiConstructor(napi_env env, napi_callback_info info);
141 
142     void RegisterDepthDataAvailableCallbackListener(const std::string& eventName, napi_env env, napi_value callback,
143         const std::vector<napi_value>& args, bool isOnce);
144     void UnregisterDepthDataAvailableCallbackListener(
145         const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args);
146     void RegisterErrorCallbackListener(const std::string& eventName, napi_env env, napi_value callback,
147         const std::vector<napi_value>& args, bool isOnce);
148     void UnregisterErrorCallbackListener(
149         const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args);
150 
151     static thread_local napi_ref sConstructor_;
152     static thread_local sptr<DepthDataOutput> sDepthDataOutput_;
153     static thread_local sptr<Surface> sDepthDataSurface_;
154     static thread_local uint32_t depthDataOutputTaskId;
155 
156     napi_env env_;
157     sptr<DepthDataOutput> depthDataOutput_;
158     sptr<DepthDataListener> depthDataListener_;
159     std::shared_ptr<DepthDataOutputCallback> depthDataCallback_;
160     static thread_local std::shared_ptr<DepthProfile> depthProfile_;
161 };
162 
163 struct DepthDataOutputAsyncContext : public AsyncContext {
164     DepthDataOutputNapi* objectInfo;
165     bool bRetBool;
166     std::string surfaceId;
~DepthDataOutputAsyncContextDepthDataOutputAsyncContext167     ~DepthDataOutputAsyncContext()
168     {
169         objectInfo = nullptr;
170     }
171 };
172 } // namespace CameraStandard
173 } // namespace OHOS
174 #endif /* DEPTH_DATA_OUTPUT_NAPI_H_ */