• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ORIENTATION,
54     SET_VIDEO_ENHANCEMENT_ATTR,
55     SET_SUPPORTED_WATERMARK_TYPE,
56 };
57 
58 enum class AddResourceMode {
59     DATA_BUFFER,
60     FILE_URI,
61     PHOTO_PROXY,
62 };
63 
64 class MediaDataSource : public IMediaDataSource {
65 public:
MediaDataSource(void * buffer,int64_t size)66     MediaDataSource(void* buffer, int64_t size) : buffer_(buffer), size_(size), readPos_(0) {}
67     ~MediaDataSource() = default;
68 
69     int32_t ReadAt(const std::shared_ptr<AVSharedMemory>& mem, uint32_t length, int64_t pos = -1) override;
70     int32_t ReadAt(int64_t pos, uint32_t length, const std::shared_ptr<AVSharedMemory>& mem) override;
71     int32_t ReadAt(uint32_t length, const std::shared_ptr<AVSharedMemory>& mem) override;
72     int32_t GetSize(int64_t& size) override;
73 
74 private:
75     int32_t ReadData(const std::shared_ptr<AVSharedMemory>& mem, uint32_t length);
76 
77     void* buffer_;
78     int64_t size_;
79     int64_t readPos_;
80 };
81 
82 class MediaAssetChangeRequestNapi : public MediaChangeRequestNapi {
83 public:
84     EXPORT MediaAssetChangeRequestNapi() = default;
85     EXPORT ~MediaAssetChangeRequestNapi() override = default;
86 
87     EXPORT static napi_value Init(napi_env env, napi_value exports);
88 
89     std::shared_ptr<FileAsset> GetFileAssetInstance() const;
90     bool Contains(AssetChangeOperation changeOperation) const;
91     bool ContainsResource(ResourceType resourceType) const;
92     bool IsMovingPhoto() const;
93     bool CheckMovingPhotoResource(ResourceType resourceType) const;
94     std::string GetFileRealPath() const;
95     AddResourceMode GetAddResourceMode() const;
96     void* GetDataBuffer() const;
97     size_t GetDataBufferSize() const;
98     std::string GetMovingPhotoVideoPath() const;
99     AddResourceMode GetMovingPhotoVideoMode() const;
100     void* GetMovingPhotoVideoBuffer() const;
101     size_t GetMovingPhotoVideoSize() const;
102     std::string GetCacheMovingPhotoVideoName() const;
103     void RecordChangeOperation(AssetChangeOperation changeOperation);
104     uint32_t FetchAddCacheFileId();
105     void SetCacheFileName(std::string& fileName);
106     void SetCacheMovingPhotoVideoName(std::string& fileName);
107     int32_t SubmitCache(bool isCreation, bool isSetEffectMode);
108     int32_t CopyToMediaLibrary(bool isCreation, AddResourceMode mode);
109     int32_t CreateAssetBySecurityComponent(std::string& assetUri);
110     napi_value ApplyChanges(napi_env env, napi_callback_info info) override;
111     int32_t PutMediaAssetEditData(DataShare::DataShareValuesBucket& valuesBucket);
112     void SetImageFileType(int32_t imageFileType);
113     int32_t GetImageFileType();
114 
115     sptr<PhotoProxy> GetPhotoProxyObj();
116     void ReleasePhotoProxyObj();
117 
118 private:
119     EXPORT static napi_value Constructor(napi_env env, napi_callback_info info);
120     EXPORT static void Destructor(napi_env env, void* nativeObject, void* finalizeHint);
121 
122     EXPORT static napi_value JSGetAsset(napi_env env, napi_callback_info info);
123     EXPORT static napi_value JSCreateAssetRequest(napi_env env, napi_callback_info info);
124     EXPORT static napi_value JSCreateImageAssetRequest(napi_env env, napi_callback_info info);
125     EXPORT static napi_value JSCreateVideoAssetRequest(napi_env env, napi_callback_info info);
126     EXPORT static napi_value CreateAssetRequestFromRealPath(napi_env env, const std::string& realPath);
127     EXPORT static napi_value JSDeleteAssets(napi_env env, napi_callback_info info);
128     EXPORT static napi_value JSSetEditData(napi_env env, napi_callback_info info);
129     EXPORT static napi_value JSSetFavorite(napi_env env, napi_callback_info info);
130     EXPORT static napi_value JSSetHidden(napi_env env, napi_callback_info info);
131     EXPORT static napi_value JSSetTitle(napi_env env, napi_callback_info info);
132     EXPORT static napi_value JSSetUserComment(napi_env env, napi_callback_info info);
133     EXPORT static napi_value JSGetWriteCacheHandler(napi_env env, napi_callback_info info);
134     EXPORT static napi_value JSAddResource(napi_env env, napi_callback_info info);
135     EXPORT static napi_value AddMovingPhotoVideoResource(napi_env env, napi_callback_info info);
136     EXPORT static napi_value JSSetEffectMode(napi_env env, napi_callback_info info);
137     EXPORT static napi_value JSSetLocation(napi_env env, napi_callback_info info);
138     EXPORT static napi_value JSSetCameraShotKey(napi_env env, napi_callback_info info);
139     EXPORT static napi_value JSSaveCameraPhoto(napi_env env, napi_callback_info info);
140     EXPORT static napi_value JSDiscardCameraPhoto(napi_env env, napi_callback_info info);
141     EXPORT static napi_value JSSetOrientation(napi_env env, napi_callback_info info);
142     EXPORT static napi_value JSSetVideoEnhancementAttr(napi_env env, napi_callback_info info);
143     EXPORT static napi_value JSSetSupportedWatermarkType(napi_env env, napi_callback_info info);
144 
145     bool CheckChangeOperations(napi_env env);
146     bool CheckMovingPhotoWriteOperation();
147     bool CheckEffectModeWriteOperation();
148     int32_t CopyFileToMediaLibrary(const UniqueFd& destFd, bool isMovingPhotoVideo = false);
149     int32_t CopyDataBufferToMediaLibrary(const UniqueFd& destFd, bool isMovingPhotoVideo = false);
150     int32_t CopyMovingPhotoVideo(const std::string& assetUri);
151     void SetNewFileAsset(int32_t id, const std::string& uri);
152 
153     static thread_local napi_ref constructor_;
154     static std::atomic<uint32_t> cacheFileId_;
155     sptr<PhotoProxy> photoProxy_ = nullptr;
156     std::shared_ptr<FileAsset> fileAsset_ = nullptr;
157     std::shared_ptr<MediaAssetEditData> editData_ = nullptr;
158     DataShare::DataShareValuesBucket creationValuesBucket_;
159     std::string realPath_;
160     std::string cacheFileName_;
161     void* dataBuffer_;
162     size_t dataBufferSize_;
163     AddResourceMode addResourceMode_;
164     std::string movingPhotoVideoRealPath_;
165     std::string cacheMovingPhotoVideoName_;
166     void* movingPhotoVideoDataBuffer_;
167     size_t movingPhotoVideoBufferSize_;
168     AddResourceMode movingPhotoVideoResourceMode_;
169     std::vector<ResourceType> addResourceTypes_; // support adding resource multiple times
170     std::vector<AssetChangeOperation> assetChangeOperations_;
171     int32_t imageFileType_;
172 };
173 
174 struct MediaAssetChangeRequestAsyncContext : public NapiError {
175     size_t argc;
176     napi_value argv[NAPI_ARGC_MAX];
177     napi_async_work work;
178     napi_deferred deferred;
179     napi_ref callbackRef;
180 
181     MediaAssetChangeRequestNapi* objectInfo;
182     std::vector<AssetChangeOperation> assetChangeOperations;
183     std::vector<ResourceType> addResourceTypes;
184     DataShare::DataSharePredicates predicates;
185     DataShare::DataShareValuesBucket valuesBucket;
186     std::vector<std::string> uris;
187     std::string appName;
188     std::string realPath;
189     int32_t fd;
190 };
191 } // namespace Media
192 } // namespace OHOS
193 
194 #endif // INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MEDIA_ASSET_CHANGE_REQUEST_NAPI_H