• 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 PHOTO_OUTPUT_NAPI_H_
17 #define PHOTO_OUTPUT_NAPI_H_
18 
19 #include <cinttypes>
20 #include <securec.h>
21 
22 #include "camera_log.h"
23 #include "napi/native_api.h"
24 #include "napi/native_node_api.h"
25 
26 #include "input/camera_manager.h"
27 #include "input/camera_device.h"
28 #include "output/camera_output_capability.h"
29 #include "output/photo_output.h"
30 
31 #include "hilog/log.h"
32 #include "camera_napi_utils.h"
33 
34 #include <fstream>
35 #include <iostream>
36 #include <sstream>
37 #include <vector>
38 
39 #include <fcntl.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <sys/types.h>
43 #include "image_receiver.h"
44 
45 namespace OHOS {
46 namespace CameraStandard {
47 const std::string dataWidth = "dataWidth";
48 const std::string dataHeight = "dataHeight";
49 const std::string thumbnailRegisterName = "quickThumbnail";
50 static const char CAMERA_PHOTO_OUTPUT_NAPI_CLASS_NAME[] = "PhotoOutput";
51 
52 struct CallbackInfo {
53     int32_t captureID;
54     uint64_t timestamp = 0;
55     int32_t frameCount = 0;
56     int32_t errorCode;
57 };
58 
59 class PhotoOutputCallback : public PhotoStateCallback {
60 public:
61     explicit PhotoOutputCallback(napi_env env);
62     ~PhotoOutputCallback() = default;
63 
64     void OnCaptureStarted(const int32_t captureID) const override;
65     void OnCaptureEnded(const int32_t captureID, const int32_t frameCount) const override;
66     void OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const override;
67     void OnCaptureError(const int32_t captureId, const int32_t errorCode) const override;
68     void SetCallbackRef(const std::string &eventType, const napi_ref &callbackRef);
69 
70 private:
71     void UpdateJSCallback(std::string propName, const CallbackInfo &info) const;
72     void UpdateJSCallbackAsync(std::string propName, const CallbackInfo &info) const;
73 
74     napi_env env_;
75     napi_ref captureStartCallbackRef_ = nullptr;
76     napi_ref captureEndCallbackRef_ = nullptr;
77     napi_ref frameShutterCallbackRef_ = nullptr;
78     napi_ref errorCallbackRef_ = nullptr;
79 };
80 
81 class ThumbnailListener : public IBufferConsumerListener {
82 public:
83     explicit ThumbnailListener(napi_env env, const napi_ref &callbackRef, const sptr<PhotoOutput> photoOutput_);
84     ~ThumbnailListener() = default;
85     void OnBufferAvailable() override;
86 
87 private:
88     napi_env env_;
89     napi_ref thumbnailCallbackRef_ = nullptr;
90     sptr<PhotoOutput> photoOutput_;
91     void UpdateJSCallback(sptr<PhotoOutput> photoOutput) const;
92     void UpdateJSCallbackAsync(sptr<PhotoOutput> photoOutput) const;
93 };
94 
95 struct PhotoOutputCallbackInfo {
96     std::string eventName_;
97     CallbackInfo info_;
98     const PhotoOutputCallback* listener_;
PhotoOutputCallbackInfoPhotoOutputCallbackInfo99     PhotoOutputCallbackInfo(std::string eventName, CallbackInfo info, const PhotoOutputCallback* listener)
100         : eventName_(eventName), info_(info), listener_(listener) {}
101 };
102 
103 struct ThumbnailListenerInfo {
104     sptr<PhotoOutput> photoOutput_;
105     const ThumbnailListener* listener_;
ThumbnailListenerInfoThumbnailListenerInfo106     ThumbnailListenerInfo(sptr<PhotoOutput> photoOutput, const ThumbnailListener* listener)
107         : photoOutput_(photoOutput), listener_(listener) {}
108 };
109 
110 class PhotoOutputNapi {
111 public:
112     static napi_value Init(napi_env env, napi_value exports);
113     static napi_value CreatePhotoOutput(napi_env env, Profile &profile, std::string surfaceId);
114     static napi_value GetDefaultCaptureSetting(napi_env env, napi_callback_info info);
115 
116     static napi_value Capture(napi_env env, napi_callback_info info);
117     static napi_value Release(napi_env env, napi_callback_info info);
118     static napi_value IsMirrorSupported(napi_env env, napi_callback_info info);
119     static napi_value SetMirror(napi_env env, napi_callback_info info);
120     static napi_value EnableQuickThumbnail(napi_env env, napi_callback_info info);
121     static napi_value IsQuickThumbnailSupported(napi_env env, napi_callback_info info);
122     static napi_value On(napi_env env, napi_callback_info info);
123 
124     static bool IsPhotoOutput(napi_env env, napi_value obj);
125     PhotoOutputNapi();
126     ~PhotoOutputNapi();
127 
128     sptr<PhotoOutput> GetPhotoOutput();
129 
130 private:
131     static void PhotoOutputNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint);
132     static napi_value PhotoOutputNapiConstructor(napi_env env, napi_callback_info info);
133 
134     static thread_local napi_ref sConstructor_;
135     static thread_local sptr<PhotoOutput> sPhotoOutput_;
136 
137     napi_env env_;
138     napi_ref wrapper_;
139     sptr<PhotoOutput> photoOutput_;
140     Profile profile_;
141     bool isQuickThumbnailEnabled_ = false;
142     std::shared_ptr<PhotoOutputCallback> photoCallback_ = nullptr;
143     static thread_local uint32_t photoOutputTaskId;
144 };
145 
146 struct PhotoOutputAsyncContext : public AsyncContext {
147     int32_t quality = -1;
148     int32_t rotation = -1;
149     double latitude = -1.0;
150     double longitude = -1.0;
151     bool isMirror = false;
152     bool hasPhotoSettings = false;
153     bool bRetBool;
154     bool isSupported = false;
155     std::unique_ptr<Location> location;
156     PhotoOutputNapi* objectInfo;
157     std::string surfaceId;
~PhotoOutputAsyncContextPhotoOutputAsyncContext158     ~PhotoOutputAsyncContext()
159     {
160         objectInfo = nullptr;
161     }
162 };
163 } // namespace CameraStandard
164 } // namespace OHOS
165 #endif /* PHOTO_OUTPUT_NAPI_H_ */
166