1 /* 2 * Copyright (C) 2023 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_MEDIA_ASSET_CHANGE_REQUEST_NAPI_H 17 #define INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MEDIA_ASSET_CHANGE_REQUEST_NAPI_H 18 19 #include <vector> 20 #include <buffer_handle_parcel.h> 21 22 #include "avmetadatahelper.h" 23 #include "datashare_helper.h" 24 #include "datashare_predicates.h" 25 #include "file_asset_napi.h" 26 #include "media_asset_edit_data.h" 27 #include "media_change_request_napi.h" 28 #include "photo_proxy.h" 29 #include "unique_fd.h" 30 #include "userfile_manager_types.h" 31 #include "values_bucket.h" 32 33 namespace OHOS { 34 namespace Media { 35 #define EXPORT __attribute__ ((visibility ("default"))) 36 enum class AssetChangeOperation { 37 CREATE_FROM_SCRATCH, 38 CREATE_FROM_URI, 39 GET_WRITE_CACHE_HANDLER, 40 ADD_RESOURCE, 41 SET_EDIT_DATA, 42 SET_FAVORITE, 43 SET_HIDDEN, 44 SET_TITLE, 45 SET_USER_COMMENT, 46 SET_MOVING_PHOTO_EFFECT_MODE, 47 SET_PHOTO_QUALITY_AND_PHOTOID, 48 SET_LOCATION, 49 SET_CAMERA_SHOT_KEY, 50 SAVE_CAMERA_PHOTO, 51 ADD_FILTERS, 52 DISCARD_CAMERA_PHOTO, 53 SET_VIDEO_ENHANCEMENT_ATTR, 54 SET_SUPPORTED_WATERMARK_TYPE, 55 }; 56 57 enum class AddResourceMode { 58 DATA_BUFFER, 59 FILE_URI, 60 PHOTO_PROXY, 61 }; 62 63 class MediaDataSource : public IMediaDataSource { 64 public: MediaDataSource(void * buffer,int64_t size)65 MediaDataSource(void* buffer, int64_t size) : buffer_(buffer), size_(size), readPos_(0) {} 66 ~MediaDataSource() = default; 67 68 int32_t ReadAt(const std::shared_ptr<AVSharedMemory>& mem, uint32_t length, int64_t pos = -1) override; 69 int32_t ReadAt(int64_t pos, uint32_t length, const std::shared_ptr<AVSharedMemory>& mem) override; 70 int32_t ReadAt(uint32_t length, const std::shared_ptr<AVSharedMemory>& mem) override; 71 int32_t GetSize(int64_t& size) override; 72 73 private: 74 int32_t ReadData(const std::shared_ptr<AVSharedMemory>& mem, uint32_t length); 75 76 void* buffer_; 77 int64_t size_; 78 int64_t readPos_; 79 }; 80 81 class MediaAssetChangeRequestNapi : public MediaChangeRequestNapi { 82 public: 83 EXPORT MediaAssetChangeRequestNapi() = default; 84 EXPORT ~MediaAssetChangeRequestNapi() override = default; 85 86 EXPORT static napi_value Init(napi_env env, napi_value exports); 87 88 std::shared_ptr<FileAsset> GetFileAssetInstance() const; 89 bool Contains(AssetChangeOperation changeOperation) const; 90 bool ContainsResource(ResourceType resourceType) const; 91 bool IsMovingPhoto() const; 92 bool CheckMovingPhotoResource(ResourceType resourceType) const; 93 std::string GetFileRealPath() const; 94 AddResourceMode GetAddResourceMode() const; 95 void* GetDataBuffer() const; 96 size_t GetDataBufferSize() const; 97 std::string GetMovingPhotoVideoPath() const; 98 AddResourceMode GetMovingPhotoVideoMode() const; 99 void* GetMovingPhotoVideoBuffer() const; 100 size_t GetMovingPhotoVideoSize() const; 101 std::string GetCacheMovingPhotoVideoName() const; 102 void RecordChangeOperation(AssetChangeOperation changeOperation); 103 uint32_t FetchAddCacheFileId(); 104 void SetCacheFileName(std::string& fileName); 105 void SetCacheMovingPhotoVideoName(std::string& fileName); 106 int32_t SubmitCache(bool isCreation, bool isSetEffectMode); 107 int32_t CopyToMediaLibrary(bool isCreation, AddResourceMode mode); 108 int32_t CreateAssetBySecurityComponent(std::string& assetUri); 109 napi_value ApplyChanges(napi_env env, napi_callback_info info) override; 110 int32_t PutMediaAssetEditData(DataShare::DataShareValuesBucket& valuesBucket); 111 void SetImageFileType(int32_t imageFileType); 112 int32_t GetImageFileType(); 113 114 sptr<PhotoProxy> GetPhotoProxyObj(); 115 void ReleasePhotoProxyObj(); 116 117 private: 118 EXPORT static napi_value Constructor(napi_env env, napi_callback_info info); 119 EXPORT static void Destructor(napi_env env, void* nativeObject, void* finalizeHint); 120 121 EXPORT static napi_value JSGetAsset(napi_env env, napi_callback_info info); 122 EXPORT static napi_value JSCreateAssetRequest(napi_env env, napi_callback_info info); 123 EXPORT static napi_value JSCreateImageAssetRequest(napi_env env, napi_callback_info info); 124 EXPORT static napi_value JSCreateVideoAssetRequest(napi_env env, napi_callback_info info); 125 EXPORT static napi_value CreateAssetRequestFromRealPath(napi_env env, const std::string& realPath); 126 EXPORT static napi_value JSDeleteAssets(napi_env env, napi_callback_info info); 127 EXPORT static napi_value JSSetEditData(napi_env env, napi_callback_info info); 128 EXPORT static napi_value JSSetFavorite(napi_env env, napi_callback_info info); 129 EXPORT static napi_value JSSetHidden(napi_env env, napi_callback_info info); 130 EXPORT static napi_value JSSetTitle(napi_env env, napi_callback_info info); 131 EXPORT static napi_value JSSetUserComment(napi_env env, napi_callback_info info); 132 EXPORT static napi_value JSGetWriteCacheHandler(napi_env env, napi_callback_info info); 133 EXPORT static napi_value JSAddResource(napi_env env, napi_callback_info info); 134 EXPORT static napi_value AddMovingPhotoVideoResource(napi_env env, napi_callback_info info); 135 EXPORT static napi_value JSSetEffectMode(napi_env env, napi_callback_info info); 136 EXPORT static napi_value JSSetLocation(napi_env env, napi_callback_info info); 137 EXPORT static napi_value JSSetCameraShotKey(napi_env env, napi_callback_info info); 138 EXPORT static napi_value JSSaveCameraPhoto(napi_env env, napi_callback_info info); 139 EXPORT static napi_value JSDiscardCameraPhoto(napi_env env, napi_callback_info info); 140 EXPORT static napi_value JSSetVideoEnhancementAttr(napi_env env, napi_callback_info info); 141 EXPORT static napi_value JSSetSupportedWatermarkType(napi_env env, napi_callback_info info); 142 143 bool CheckChangeOperations(napi_env env); 144 bool CheckMovingPhotoWriteOperation(); 145 bool CheckEffectModeWriteOperation(); 146 int32_t CopyFileToMediaLibrary(const UniqueFd& destFd, bool isMovingPhotoVideo = false); 147 int32_t CopyDataBufferToMediaLibrary(const UniqueFd& destFd, bool isMovingPhotoVideo = false); 148 int32_t CopyMovingPhotoVideo(const std::string& assetUri); 149 void SetNewFileAsset(int32_t id, const std::string& uri); 150 151 static thread_local napi_ref constructor_; 152 static std::atomic<uint32_t> cacheFileId_; 153 sptr<PhotoProxy> photoProxy_ = nullptr; 154 std::shared_ptr<FileAsset> fileAsset_ = nullptr; 155 std::shared_ptr<MediaAssetEditData> editData_ = nullptr; 156 DataShare::DataShareValuesBucket creationValuesBucket_; 157 std::string realPath_; 158 std::string cacheFileName_; 159 void* dataBuffer_; 160 size_t dataBufferSize_; 161 AddResourceMode addResourceMode_; 162 std::string movingPhotoVideoRealPath_; 163 std::string cacheMovingPhotoVideoName_; 164 void* movingPhotoVideoDataBuffer_; 165 size_t movingPhotoVideoBufferSize_; 166 AddResourceMode movingPhotoVideoResourceMode_; 167 std::vector<ResourceType> addResourceTypes_; // support adding resource multiple times 168 std::vector<AssetChangeOperation> assetChangeOperations_; 169 int32_t imageFileType_; 170 }; 171 172 struct MediaAssetChangeRequestAsyncContext : public NapiError { 173 size_t argc; 174 napi_value argv[NAPI_ARGC_MAX]; 175 napi_async_work work; 176 napi_deferred deferred; 177 napi_ref callbackRef; 178 179 MediaAssetChangeRequestNapi* objectInfo; 180 std::vector<AssetChangeOperation> assetChangeOperations; 181 std::vector<ResourceType> addResourceTypes; 182 DataShare::DataSharePredicates predicates; 183 DataShare::DataShareValuesBucket valuesBucket; 184 std::vector<std::string> uris; 185 std::string appName; 186 std::string realPath; 187 int32_t fd; 188 }; 189 } // namespace Media 190 } // namespace OHOS 191 192 #endif // INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MEDIA_ASSET_CHANGE_REQUEST_NAPI_H