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_INNERKITS_NATIVE_INCLUDE_MEDIA_ASSET_MANAGER_H
17 #define INTERFACES_INNERKITS_NATIVE_INCLUDE_MEDIA_ASSET_MANAGER_H
18 
19 #include <mutex>
20 #include <vector>
21 #include <map>
22 
23 #include "datashare_helper.h"
24 #include "media_asset_data_handler_capi.h"
25 #include "media_asset_base_capi.h"
26 
27 namespace OHOS {
28 namespace Media {
29 using MediaAssetDataHandlerPtr = std::shared_ptr<CapiMediaAssetDataHandler>;
30 
31 enum class MultiStagesCapturePhotoStatus {
32     QUERY_INNER_FAIL = 0,
33     HIGH_QUALITY_STATUS,
34     LOW_QUALITY_STATUS,
35 };
36 
37 struct RequestSourceAsyncContext {
38     int fileId = -1; // default value of request file id
39     std::string requestUri;
40     std::string photoId;
41     std::string displayName;
42     std::string mediaPath;
43     std::string callingPkgName;
44     std::string requestId;
45     std::string destUri;
46     NativeOnDataPrepared onDataPreparedHandler;
47     NativeRequestOptions requestOptions;
48     ReturnDataType returnDataType;
49     OH_MediaLibrary_OnImageDataPrepared onRequestImageDataPreparedHandler;
50     OH_MediaLibrary_OnMovingPhotoDataPrepared onRequestMovingPhotoDataPreparedHandler;
51     MultiStagesCapturePhotoStatus photoQuality = MultiStagesCapturePhotoStatus::HIGH_QUALITY_STATUS;
52     bool needsExtraInfo;
53 };
54 
55 struct AssetHandler {
56     std::string photoId;
57     std::string requestId;
58     std::string requestUri;
59     std::string destUri;
60     MediaAssetDataHandlerPtr dataHandler;
61     std::mutex mutex_;
62 
AssetHandlerAssetHandler63     AssetHandler(const std::string &photoId, const std::string &requestId, const std::string &uri,
64         const std::string &destUri, const MediaAssetDataHandlerPtr &handler)
65         : photoId(photoId), requestId(requestId), requestUri(uri), destUri(destUri), dataHandler(handler) {}
66 };
67 
68 class MultiStagesTaskObserver : public DataShare::DataShareObserver {
69 public:
MultiStagesTaskObserver(int fileId)70     MultiStagesTaskObserver(int fileId)
71         : fileId_(fileId) {};
72     void OnChange(const ChangeInfo &changelnfo) override;
73 
74 private:
75     std::map<std::string, AssetHandler *> GetAssetHandlers(const std::string uriString);
76 
77 private:
78     int fileId_;
79 };
80 
81 class MediaAssetManager {
82 public:
83     virtual ~MediaAssetManager() = default;
84 
85 public:
86     virtual bool NativeCancelRequest(const std::string &requestId) = 0;
87     virtual std::string NativeRequestImage(const char* photoUri, const NativeRequestOptions &requestOptions,
88         const char* destPath, const NativeOnDataPrepared &callback) = 0;
89     virtual std::string NativeRequestVideo(const char* videoUri, const NativeRequestOptions &requestOptions,
90         const char* destUri, const NativeOnDataPrepared &callback) = 0;
91 
92     virtual MediaLibrary_ErrorCode NativeRequestImageSource(OH_MediaAsset* mediaAsset,
93         NativeRequestOptions requestOptions, MediaLibrary_RequestId* requestId,
94         OH_MediaLibrary_OnImageDataPrepared callback) = 0;
95     virtual MediaLibrary_ErrorCode NativeRequestMovingPhoto(OH_MediaAsset* mediaAsset,
96         NativeRequestOptions requestOptions, MediaLibrary_RequestId* requestId,
97         OH_MediaLibrary_OnMovingPhotoDataPrepared callback) = 0;
98 };
99 
100 class __attribute__((visibility("default"))) MediaAssetManagerFactory {
101 public:
102     static std::shared_ptr<MediaAssetManager> CreateMediaAssetManager();
103 private:
104     MediaAssetManagerFactory() = default;
105     ~MediaAssetManagerFactory() = default;
106 };
107 } // Media
108 } // OHOS
109 #endif // INTERFACES_INNERKITS_NATIVE_INCLUDE_MEDIA_ASSET_MANAGER_H
110