1 /* 2 * Copyright (C) 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 INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MOVING_PHOTO_NAPI_H 17 #define INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MOVING_PHOTO_NAPI_H 18 19 #include <memory> 20 21 #include "media_asset_data_handler.h" 22 #include "media_library_napi.h" 23 24 namespace OHOS { 25 namespace Media { 26 class MovingPhotoNapi; 27 28 struct MovingPhotoAsyncContext : public NapiError { 29 enum RequestContentMode { 30 WRITE_TO_SANDBOX, 31 WRITE_TO_ARRAY_BUFFER, 32 UNDEFINED, 33 }; 34 35 napi_async_work work; 36 napi_deferred deferred; 37 napi_ref callbackRef; 38 MovingPhotoNapi *objectInfo = nullptr; 39 40 std::string movingPhotoUri; 41 SourceMode sourceMode; 42 CompatibleMode compatibleMode; 43 napi_ref progressHandlerRef; 44 napi_env mediaAssetEnv; 45 bool isTranscoder = false; 46 napi_threadsafe_function threadsafeFunction; 47 std::string requestId; 48 ResourceType resourceType; 49 std::string destImageUri; 50 std::string destVideoUri; 51 std::string destLivePhotoUri; 52 std::string destMetadataUri; 53 RequestContentMode requestContentMode = UNDEFINED; 54 void* arrayBufferData = nullptr; 55 size_t arrayBufferLength = 0; 56 int32_t position = 0; 57 bool isVideoReady = false; 58 size_t argc; 59 napi_value argv[NAPI_ARGC_MAX]; 60 }; 61 62 struct MovingPhotoParam { 63 std::string requestId; 64 CompatibleMode compatibleMode; 65 napi_ref progressHandlerRef; 66 napi_threadsafe_function threadsafeFunction; MovingPhotoParamMovingPhotoParam67 MovingPhotoParam() : requestId(""), compatibleMode(CompatibleMode::ORIGINAL_FORMAT_MODE), 68 progressHandlerRef(nullptr), threadsafeFunction(nullptr) {}; 69 }; 70 71 class MovingPhotoNapi { 72 public: MovingPhotoNapi(const std::string & photoUri)73 MovingPhotoNapi(const std::string& photoUri) : photoUri_(photoUri) {}; 74 ~MovingPhotoNapi() = default; 75 EXPORT static napi_value Init(napi_env env, napi_value exports); 76 static int32_t OpenReadOnlyFile(const string& uri, bool isReadImage, int32_t position); 77 static int32_t OpenReadOnlyLivePhoto(const string& destLivePhotoUri, int32_t position); 78 static int32_t OpenReadOnlyMetadata(const string& movingPhotoUri); 79 static napi_value NewMovingPhotoNapi(napi_env env, const string& photoUri, SourceMode sourceMode, 80 MovingPhotoParam &movingPhotoParam); 81 std::string GetUri(); 82 SourceMode GetSourceMode(); 83 void SetSourceMode(SourceMode sourceMode); 84 std::string GetRequestId(); 85 void SetRequestId(const std::string requestId); 86 CompatibleMode GetCompatibleMode(); 87 void SetCompatibleMode(const CompatibleMode compatibleMode); 88 napi_ref GetProgressHandlerRef(); 89 void SetProgressHandlerRef(napi_ref &progressHandlerRef); 90 napi_env GetMediaAssetEnv(); 91 void SetMediaAssetEnv(napi_env mediaAssetEnv); GetThreadsafeFunction()92 napi_threadsafe_function GetThreadsafeFunction() 93 { 94 return threadsafeFunction_; 95 } SetThreadsafeFunction(napi_threadsafe_function threadsafeFunction)96 void SetThreadsafeFunction(napi_threadsafe_function threadsafeFunction) 97 { 98 threadsafeFunction_ = threadsafeFunction; 99 } 100 static int32_t DoMovingPhotoTranscode(napi_env env, int32_t &videoFd, MovingPhotoAsyncContext* context); 101 static void OnProgress(napi_env env, napi_value cb, void *context, void *data); 102 static int32_t GetFdFromUri(const std::string &sandBoxUri); 103 static void SubRequestContent(int32_t fd, MovingPhotoAsyncContext* context); 104 static void RequestCloudContentArrayBuffer(int32_t fd, MovingPhotoAsyncContext* context); 105 static void CallRequestContentCallBack(napi_env env, void* context, int32_t errorCode); 106 private: 107 EXPORT static napi_value Constructor(napi_env env, napi_callback_info info); 108 EXPORT static void Destructor(napi_env env, void* nativeObject, void* finalizeHint); 109 110 EXPORT static napi_value JSRequestContent(napi_env env, napi_callback_info info); 111 EXPORT static napi_value JSGetUri(napi_env env, napi_callback_info info); 112 EXPORT static napi_value JSIsVideoReady(napi_env env, napi_callback_info info); 113 114 static thread_local napi_ref constructor_; 115 std::string photoUri_; 116 SourceMode sourceMode_ = SourceMode::EDITED_MODE; 117 CompatibleMode compatibleMode_ = CompatibleMode::COMPATIBLE_FORMAT_MODE; 118 std::string requestId_; 119 napi_ref progressHandlerRef_ = nullptr; 120 napi_env media_asset_env_ = nullptr; 121 napi_threadsafe_function threadsafeFunction_ = nullptr; 122 }; 123 124 } // Media 125 } // OHOS 126 #endif // INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MOVING_PHOTO_NAPI_H