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