1 /* 2 * Copyright (c) 2021-2024 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 <cstdint> 20 #include <memory> 21 22 #include "camera_napi_event_emitter.h" 23 #include "camera_napi_template_utils.h" 24 #include "input/camera_device.h" 25 #include "input/camera_manager.h" 26 #include "listener_base.h" 27 #include "native_image.h" 28 #include "output/camera_output_capability.h" 29 #include "output/photo_output.h" 30 #include "task_manager.h" 31 namespace OHOS { 32 namespace CameraStandard { 33 const std::string dataWidth = "dataWidth"; 34 const std::string dataHeight = "dataHeight"; 35 static const std::string CONST_CAPTURE_START = "captureStart"; 36 static const std::string CONST_CAPTURE_END = "captureEnd"; 37 static const std::string CONST_CAPTURE_FRAME_SHUTTER = "frameShutter"; 38 static const std::string CONST_CAPTURE_ERROR = "error"; 39 static const std::string CONST_CAPTURE_PHOTO_AVAILABLE = "photoAvailable"; 40 static const std::string CONST_CAPTURE_DEFERRED_PHOTO_AVAILABLE = "deferredPhotoProxyAvailable"; 41 static const std::string CONST_CAPTURE_PHOTO_ASSET_AVAILABLE = "photoAssetAvailable"; 42 static const std::string CONST_CAPTURE_FRAME_SHUTTER_END = "frameShutterEnd"; 43 static const std::string CONST_CAPTURE_READY = "captureReady"; 44 static const std::string CONST_CAPTURE_ESTIMATED_CAPTURE_DURATION = "estimatedCaptureDuration"; 45 static const std::string CONST_CAPTURE_START_WITH_INFO = "captureStartWithInfo"; 46 47 static const std::string CONST_CAPTURE_QUICK_THUMBNAIL = "quickThumbnail"; 48 static const char CAMERA_PHOTO_OUTPUT_NAPI_CLASS_NAME[] = "PhotoOutput"; 49 50 static const std::string CONST_GAINMAP_SURFACE = "gainmap"; 51 static const std::string CONST_DEEP_SURFACE = "deep"; 52 static const std::string CONST_EXIF_SURFACE = "exif"; 53 static const std::string CONST_DEBUG_SURFACE = "debug"; 54 55 struct CallbackInfo { 56 int32_t captureID; 57 uint64_t timestamp = 0; 58 int32_t frameCount = 0; 59 int32_t errorCode; 60 int32_t duration; 61 }; 62 63 enum PhotoOutputEventType { 64 CAPTURE_START, 65 CAPTURE_END, 66 CAPTURE_FRAME_SHUTTER, 67 CAPTURE_FRAME_SHUTTER_END, 68 CAPTURE_READY, 69 CAPTURE_ERROR, 70 CAPTURE_INVALID_TYPE, 71 CAPTURE_PHOTO_AVAILABLE, 72 CAPTURE_DEFERRED_PHOTO_AVAILABLE, 73 CAPTURE_PHOTO_ASSET_AVAILABLE, 74 CAPTURE_ESTIMATED_CAPTURE_DURATION, 75 CAPTURE_START_WITH_INFO 76 }; 77 78 static EnumHelper<PhotoOutputEventType> PhotoOutputEventTypeHelper({ 79 {CAPTURE_START, CONST_CAPTURE_START}, 80 {CAPTURE_END, CONST_CAPTURE_END}, 81 {CAPTURE_FRAME_SHUTTER, CONST_CAPTURE_FRAME_SHUTTER}, 82 {CAPTURE_ERROR, CONST_CAPTURE_ERROR}, 83 {CAPTURE_PHOTO_AVAILABLE, CONST_CAPTURE_PHOTO_AVAILABLE}, 84 {CAPTURE_DEFERRED_PHOTO_AVAILABLE, CONST_CAPTURE_DEFERRED_PHOTO_AVAILABLE}, 85 {CAPTURE_PHOTO_ASSET_AVAILABLE, CONST_CAPTURE_PHOTO_ASSET_AVAILABLE}, 86 {CAPTURE_FRAME_SHUTTER_END, CONST_CAPTURE_FRAME_SHUTTER_END}, 87 {CAPTURE_READY, CONST_CAPTURE_READY}, 88 {CAPTURE_ESTIMATED_CAPTURE_DURATION, CONST_CAPTURE_ESTIMATED_CAPTURE_DURATION}, 89 {CAPTURE_START_WITH_INFO, CONST_CAPTURE_START_WITH_INFO} 90 }, 91 PhotoOutputEventType::CAPTURE_INVALID_TYPE 92 ); 93 enum SurfaceType { 94 GAINMAP_SURFACE = 0, 95 DEEP_SURFACE = 1, 96 EXIF_SURFACE = 2, 97 DEBUG_SURFACE = 3, 98 INVALID_SURFACE = -1, 99 }; 100 101 static EnumHelper<SurfaceType> SurfaceTypeHelper({ 102 {GAINMAP_SURFACE, CONST_GAINMAP_SURFACE}, 103 {DEEP_SURFACE, CONST_DEEP_SURFACE}, 104 {EXIF_SURFACE, CONST_EXIF_SURFACE}, 105 {DEBUG_SURFACE, CONST_DEBUG_SURFACE}, 106 }, 107 SurfaceType::INVALID_SURFACE 108 ); 109 110 class PhotoBufferProcessor : public Media::IBufferProcessor { 111 public: PhotoBufferProcessor(sptr<Surface> photoSurface)112 explicit PhotoBufferProcessor(sptr<Surface> photoSurface) : photoSurface_(photoSurface) {} ~PhotoBufferProcessor()113 ~PhotoBufferProcessor() 114 { 115 photoSurface_ = nullptr; 116 } BufferRelease(sptr<SurfaceBuffer> & buffer)117 void BufferRelease(sptr<SurfaceBuffer>& buffer) override 118 { 119 if (photoSurface_ != nullptr) { 120 photoSurface_->ReleaseBuffer(buffer, -1); 121 } 122 } 123 124 private: 125 sptr<Surface> photoSurface_ = nullptr; 126 }; 127 128 class PhotoListener : public IBufferConsumerListener, public ListenerBase { 129 public: 130 explicit PhotoListener(napi_env env, const sptr<Surface> photoSurface, wptr<PhotoOutput> photoOutput); 131 ~PhotoListener() = default; 132 void OnBufferAvailable() override; 133 void SaveCallback(const std::string eventName, napi_value callback); 134 void RemoveCallback(const std::string eventName, napi_value callback); 135 void ExecuteDeepyCopySurfaceBuffer(); 136 private: 137 sptr<Surface> photoSurface_; 138 wptr<PhotoOutput> photoOutput_; 139 shared_ptr<PhotoBufferProcessor> bufferProcessor_; 140 void UpdateJSCallback(sptr<Surface> photoSurface) const; 141 void UpdateJSCallbackAsync(sptr<Surface> photoSurface) const; 142 void UpdatePictureJSCallback(const string uri, int32_t cameraShotType, const std::string burstKey) const; 143 void UpdateMainPictureStageOneJSCallback(sptr<SurfaceBuffer> surfaceBuffer, int64_t timestamp) const; 144 void ExecutePhoto(sptr<SurfaceBuffer> surfaceBfuffer, int64_t timestamp) const; 145 void ExecuteDeferredPhoto(sptr<SurfaceBuffer> surfaceBuffer) const; 146 void DeepCopyBuffer(sptr<SurfaceBuffer> newSurfaceBuffer, sptr<SurfaceBuffer> surfaceBuffer) const; 147 void ExecutePhotoAsset(sptr<SurfaceBuffer> surfaceBuffer, bool isHighQuality, int64_t timestamp) const; 148 void CreateMediaLibrary(sptr<SurfaceBuffer> surfaceBuffer, BufferHandle* bufferHandle, bool isHighQuality, 149 std::string& uri, int32_t& cameraShotType, std::string &burstKey, int64_t timestamp) const; 150 void AssembleAuxiliaryPhoto(int64_t timestamp, int32_t captureId); 151 int32_t GetAuxiliaryPhotoCount(sptr<SurfaceBuffer> surfaceBuffer); 152 sptr<CameraPhotoProxy> CreateCameraPhotoProxy(sptr<SurfaceBuffer> surfaceBuffer); 153 uint8_t callbackFlag_ = 0; 154 std::shared_ptr<DeferredProcessing::TaskManager> taskManager_; 155 }; 156 157 class RawPhotoListener : public IBufferConsumerListener, public ListenerBase { 158 public: 159 explicit RawPhotoListener(napi_env env, const sptr<Surface> rawPhotoSurface); 160 ~RawPhotoListener() = default; 161 void OnBufferAvailable() override; 162 163 private: 164 sptr<Surface> rawPhotoSurface_; 165 shared_ptr<PhotoBufferProcessor> bufferProcessor_; 166 void UpdateJSCallback(sptr<Surface> rawPhotoSurface) const; 167 void UpdateJSCallbackAsync(sptr<Surface> rawPhotoSurface) const; 168 void ExecuteRawPhoto(sptr<SurfaceBuffer> rawPhotoSurface) const; 169 }; 170 171 class AuxiliaryPhotoListener : public IBufferConsumerListener { 172 public: 173 explicit AuxiliaryPhotoListener(const std::string surfaceName, const sptr<Surface> surface, 174 wptr<PhotoOutput> photoOutput); 175 ~AuxiliaryPhotoListener() = default; 176 void OnBufferAvailable() override; 177 void ExecuteDeepyCopySurfaceBuffer(); 178 private: 179 void DeepCopyBuffer(sptr<SurfaceBuffer> newSurfaceBuffer, sptr<SurfaceBuffer> surfaceBuffer) const; 180 std::string surfaceName_; 181 sptr<Surface> surface_; 182 wptr<PhotoOutput> photoOutput_; 183 shared_ptr<PhotoBufferProcessor> bufferProcessor_; 184 std::shared_ptr<DeferredProcessing::TaskManager> taskManager_; 185 }; 186 187 class PictureListener : public RefBase { 188 public: 189 explicit PictureListener() = default; 190 ~PictureListener() = default; 191 void InitPictureListeners(napi_env env, wptr<PhotoOutput> photoOutput); 192 sptr<AuxiliaryPhotoListener> gainmapImageListener; 193 sptr<AuxiliaryPhotoListener> deepImageListener; 194 sptr<AuxiliaryPhotoListener> exifImageListener; 195 sptr<AuxiliaryPhotoListener> debugImageListener; 196 }; 197 198 class PhotoOutputCallback : public PhotoStateCallback, 199 public ListenerBase, 200 public std::enable_shared_from_this<PhotoOutputCallback> { 201 public: 202 explicit PhotoOutputCallback(napi_env env); 203 ~PhotoOutputCallback() = default; 204 205 void OnCaptureStarted(const int32_t captureID) const override; 206 void OnCaptureStarted(const int32_t captureID, uint32_t exposureTime) const override; 207 void OnCaptureEnded(const int32_t captureID, const int32_t frameCount) const override; 208 void OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const override; 209 void OnFrameShutterEnd(const int32_t captureId, const uint64_t timestamp) const override; 210 void OnCaptureReady(const int32_t captureId, const uint64_t timestamp) const override; 211 void OnCaptureError(const int32_t captureId, const int32_t errorCode) const override; 212 void OnEstimatedCaptureDuration(const int32_t duration) const override; 213 214 private: 215 void UpdateJSCallback(PhotoOutputEventType eventType, const CallbackInfo& info) const; 216 void UpdateJSCallbackAsync(PhotoOutputEventType eventType, const CallbackInfo& info) const; 217 void ExecuteCaptureStartCb(const CallbackInfo& info) const; 218 void ExecuteCaptureStartWithInfoCb(const CallbackInfo& info) const; 219 void ExecuteCaptureEndCb(const CallbackInfo& info) const; 220 void ExecuteFrameShutterCb(const CallbackInfo& info) const; 221 void ExecuteCaptureErrorCb(const CallbackInfo& info) const; 222 void ExecuteFrameShutterEndCb(const CallbackInfo& info) const; 223 void ExecuteCaptureReadyCb(const CallbackInfo& info) const; 224 void ExecuteEstimatedCaptureDurationCb(const CallbackInfo& info) const; 225 }; 226 227 struct PhotoOutputCallbackInfo { 228 PhotoOutputEventType eventType_; 229 CallbackInfo info_; 230 weak_ptr<const PhotoOutputCallback> listener_; PhotoOutputCallbackInfoPhotoOutputCallbackInfo231 PhotoOutputCallbackInfo( 232 PhotoOutputEventType eventType, CallbackInfo info, shared_ptr<const PhotoOutputCallback> listener) 233 : eventType_(eventType), info_(info), listener_(listener) 234 {} 235 }; 236 237 class ThumbnailListener : public IBufferConsumerListener, public ListenerBase { 238 public: 239 explicit ThumbnailListener(napi_env env, const sptr<PhotoOutput> photoOutput); 240 ~ThumbnailListener() = default; 241 void OnBufferAvailable() override; 242 243 private: 244 wptr<PhotoOutput> photoOutput_; 245 void UpdateJSCallback() const; 246 void UpdateJSCallbackAsync(); 247 }; 248 249 struct ThumbnailListenerInfo { 250 wptr<ThumbnailListener> listener_; ThumbnailListenerInfoThumbnailListenerInfo251 ThumbnailListenerInfo(sptr<ThumbnailListener> listener) 252 : listener_(listener) 253 {} 254 }; 255 256 struct PhotoListenerInfo { 257 sptr<Surface> photoSurface_; 258 const PhotoListener* listener_; PhotoListenerInfoPhotoListenerInfo259 PhotoListenerInfo(sptr<Surface> photoSurface, const PhotoListener* listener) 260 : photoSurface_(photoSurface), listener_(listener) 261 {} 262 std::string uri = ""; 263 int32_t cameraShotType = 0; 264 std::string burstKey = ""; 265 sptr<SurfaceBuffer> surfaceBuffer = nullptr; 266 int64_t timestamp = 0; 267 }; 268 269 struct RawPhotoListenerInfo { 270 sptr<Surface> rawPhotoSurface_; 271 const RawPhotoListener* listener_; RawPhotoListenerInfoRawPhotoListenerInfo272 RawPhotoListenerInfo(sptr<Surface> rawPhotoSurface, const RawPhotoListener* listener) 273 : rawPhotoSurface_(rawPhotoSurface), listener_(listener) 274 {} 275 }; 276 277 struct PhotoOutputAsyncContext; 278 class PhotoOutputNapi : public CameraNapiEventEmitter<PhotoOutputNapi> { 279 public: 280 static napi_value Init(napi_env env, napi_value exports); 281 static napi_value CreatePhotoOutput(napi_env env, Profile& profile, std::string surfaceId); 282 static napi_value CreatePhotoOutput(napi_env env, std::string surfaceId); 283 284 static napi_value Capture(napi_env env, napi_callback_info info); 285 static napi_value BurstCapture(napi_env env, napi_callback_info info); 286 static napi_value ConfirmCapture(napi_env env, napi_callback_info info); 287 static napi_value Release(napi_env env, napi_callback_info info); 288 static napi_value IsMirrorSupported(napi_env env, napi_callback_info info); 289 static napi_value EnableMirror(napi_env env, napi_callback_info info); 290 static napi_value EnableQuickThumbnail(napi_env env, napi_callback_info info); 291 static napi_value IsQuickThumbnailSupported(napi_env env, napi_callback_info info); 292 static napi_value EnableRawDelivery(napi_env env, napi_callback_info info); 293 static napi_value IsRawDeliverySupported(napi_env env, napi_callback_info info); 294 static napi_value DeferImageDeliveryFor(napi_env env, napi_callback_info info); 295 static napi_value IsDeferredImageDeliverySupported(napi_env env, napi_callback_info info); 296 static napi_value IsDeferredImageDeliveryEnabled(napi_env env, napi_callback_info info); 297 static napi_value GetSupportedMovingPhotoVideoCodecTypes(napi_env env, napi_callback_info info); 298 static napi_value SetMovingPhotoVideoCodecType(napi_env env, napi_callback_info info); 299 static napi_value IsDepthDataDeliverySupported(napi_env env, napi_callback_info info); 300 static napi_value EnableDepthDataDelivery(napi_env env, napi_callback_info info); 301 static bool IsPhotoOutput(napi_env env, napi_value obj); 302 static napi_value GetActiveProfile(napi_env env, napi_callback_info info); 303 static napi_value On(napi_env env, napi_callback_info info); 304 static napi_value Once(napi_env env, napi_callback_info info); 305 static napi_value Off(napi_env env, napi_callback_info info); 306 static napi_value GetPhotoRotation(napi_env env, napi_callback_info info); 307 308 static napi_value IsAutoHighQualityPhotoSupported(napi_env env, napi_callback_info info); 309 static napi_value EnableAutoHighQualityPhoto(napi_env env, napi_callback_info info); 310 311 static napi_value IsAutoCloudImageEnhancementSupported(napi_env env, napi_callback_info info); 312 static napi_value EnableAutoCloudImageEnhancement(napi_env env, napi_callback_info info); 313 static napi_value IsMovingPhotoSupported(napi_env env, napi_callback_info info); 314 static napi_value EnableMovingPhoto(napi_env env, napi_callback_info info); 315 316 PhotoOutputNapi(); 317 ~PhotoOutputNapi() override; 318 319 sptr<PhotoOutput> GetPhotoOutput(); 320 bool GetEnableMirror(); 321 322 const EmitterFunctions& GetEmitterFunctions() override; 323 324 private: 325 static void PhotoOutputNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint); 326 static napi_value PhotoOutputNapiConstructor(napi_env env, napi_callback_info info); 327 void CreateMultiChannelPictureLisenter(napi_env env); 328 void CreateSingleChannelPhotoLisenter(napi_env env); 329 void RegisterQuickThumbnailCallbackListener(const std::string& eventName, napi_env env, napi_value callback, 330 const std::vector<napi_value>& args, bool isOnce); 331 void UnregisterQuickThumbnailCallbackListener( 332 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args); 333 void RegisterPhotoAvailableCallbackListener(const std::string& eventName, napi_env env, napi_value callback, 334 const std::vector<napi_value>& args, bool isOnce); 335 void UnregisterPhotoAvailableCallbackListener( 336 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args); 337 void RegisterDeferredPhotoProxyAvailableCallbackListener(const std::string& eventName, napi_env env, 338 napi_value callback, const std::vector<napi_value>& args, bool isOnce); 339 void UnregisterDeferredPhotoProxyAvailableCallbackListener( 340 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args); 341 void RegisterPhotoAssetAvailableCallbackListener(const std::string& eventName, napi_env env, napi_value callback, 342 const std::vector<napi_value>& args, bool isOnce); 343 void UnregisterPhotoAssetAvailableCallbackListener( 344 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args); 345 void RegisterCaptureStartCallbackListener(const std::string& eventName, napi_env env, napi_value callback, 346 const std::vector<napi_value>& args, bool isOnce); 347 void UnregisterCaptureStartCallbackListener( 348 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args); 349 void RegisterCaptureEndCallbackListener(const std::string& eventName, napi_env env, napi_value callback, 350 const std::vector<napi_value>& args, bool isOnce); 351 void UnregisterCaptureEndCallbackListener( 352 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args); 353 void RegisterFrameShutterCallbackListener(const std::string& eventName, napi_env env, napi_value callback, 354 const std::vector<napi_value>& args, bool isOnce); 355 void UnregisterFrameShutterCallbackListener( 356 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args); 357 void RegisterErrorCallbackListener(const std::string& eventName, napi_env env, napi_value callback, 358 const std::vector<napi_value>& args, bool isOnce); 359 void UnregisterErrorCallbackListener( 360 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args); 361 void RegisterFrameShutterEndCallbackListener(const std::string& eventName, napi_env env, napi_value callback, 362 const std::vector<napi_value>& args, bool isOnce); 363 void UnregisterFrameShutterEndCallbackListener( 364 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args); 365 void RegisterReadyCallbackListener(const std::string& eventName, napi_env env, napi_value callback, 366 const std::vector<napi_value>& args, bool isOnce); 367 void UnregisterReadyCallbackListener( 368 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args); 369 void RegisterEstimatedCaptureDurationCallbackListener(const std::string& eventName, napi_env env, 370 napi_value callback, const std::vector<napi_value>& args, bool isOnce); 371 void UnregisterEstimatedCaptureDurationCallbackListener( 372 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args); 373 void RegisterCaptureStartWithInfoCallbackListener(const std::string& eventName, napi_env env, napi_value callback, 374 const std::vector<napi_value>& args, bool isOnce); 375 void UnregisterCaptureStartWithInfoCallbackListener( 376 const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args); 377 378 static thread_local napi_ref sConstructor_; 379 static thread_local sptr<PhotoOutput> sPhotoOutput_; 380 static thread_local sptr<Surface> sPhotoSurface_; 381 382 sptr<PhotoOutput> photoOutput_; 383 std::shared_ptr<Profile> profile_; 384 bool isQuickThumbnailEnabled_ = false; 385 bool isDeferredPhotoEnabled_ = false; 386 bool isMirrorEnabled_ = false; 387 sptr<ThumbnailListener> thumbnailListener_; 388 sptr<PhotoListener> photoListener_; 389 sptr<RawPhotoListener> rawPhotoListener_; 390 sptr<PictureListener> pictureListener_; 391 std::shared_ptr<PhotoOutputCallback> photoOutputCallback_; 392 static thread_local uint32_t photoOutputTaskId; 393 static thread_local napi_ref rawCallback_; 394 }; 395 396 397 struct PhotoOutputNapiCaptureSetting { 398 int32_t quality = -1; 399 }; 400 401 struct PhotoOutputAsyncContext : public AsyncContext { PhotoOutputAsyncContextPhotoOutputAsyncContext402 PhotoOutputAsyncContext(std::string funcName, int32_t taskId) : AsyncContext(funcName, taskId) {}; 403 int32_t quality = -1; 404 int32_t rotation = -1; 405 bool isMirror = false; 406 bool hasPhotoSettings = false; 407 bool isMirrorSettedByUser = false; 408 bool isSupported = false; 409 shared_ptr<Location> location; 410 PhotoOutputNapi* objectInfo = nullptr; 411 std::string surfaceId; 412 }; 413 } // namespace CameraStandard 414 } // namespace OHOS 415 #endif /* PHOTO_OUTPUT_NAPI_H_ */ 416