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 #ifndef MEDIA_ASSET_MANAGER_IMPL_H 16 #define MEDIA_ASSET_MANAGER_IMPL_H 17 18 #include <mutex> 19 #include <vector> 20 #include <map> 21 22 #include "data_ability_helper.h" 23 #include "data_ability_observer_stub.h" 24 #include "data_ability_predicates.h" 25 #include "media_file_utils.h" 26 #include "photo_accesshelper_impl.h" 27 28 namespace OHOS { 29 namespace Media { 30 enum class MultiStagesCapturePhotoStatus { 31 QUERY_INNER_FAIL = 0, 32 HIGH_QUALITY_STATUS, 33 LOW_QUALITY_STATUS, 34 }; 35 36 enum class NotifyMode : int32_t { 37 FAST_NOTIFY = 0, 38 WAIT_FOR_HIGH_QUALITY, 39 }; 40 41 enum class CompatibleMode { 42 ORIGINAL_FORMAT_MODE = 0, 43 COMPATIBLE_FORMAT_MODE = 1, 44 }; 45 46 struct AssetHandler { 47 std::string photoId; 48 std::string requestId; 49 std::string requestUri; 50 std::string destUri; 51 int64_t dataHandler; 52 ReturnDataType returnDataType; 53 NotifyMode notifyMode = NotifyMode::FAST_NOTIFY; 54 SourceMode sourceMode = SourceMode::ORIGINAL_MODE; 55 CompatibleMode compatibleMode = CompatibleMode::ORIGINAL_FORMAT_MODE; 56 MultiStagesCapturePhotoStatus photoQuality = MultiStagesCapturePhotoStatus::HIGH_QUALITY_STATUS; 57 bool needsExtraInfo = false; 58 AssetHandlerAssetHandler59 AssetHandler(const std::string &photoId, const std::string &requestId, const std::string &uri, 60 int64_t funcId, ReturnDataType returnDataType) 61 : photoId(photoId), requestId(requestId), 62 requestUri(uri), dataHandler(funcId), returnDataType(returnDataType) {} 63 }; 64 65 struct RetProgressValue { 66 int32_t progress; 67 int32_t type; 68 std::string errorMsg; 69 }; 70 71 struct ProgressHandler { 72 std::string requestId; 73 RetProgressValue retProgressValue; 74 int64_t progressFunc; ProgressHandlerProgressHandler75 ProgressHandler(const std::string &requestId, 76 RetProgressValue &retProgressValue) : requestId(requestId), 77 retProgressValue(retProgressValue) {} 78 }; 79 80 class MultiStagesTaskObserver : public DataShare::DataShareObserver { 81 public: MultiStagesTaskObserver(int fileId)82 MultiStagesTaskObserver(int fileId) 83 : fileId_(fileId) {}; 84 void OnChange(const ChangeInfo &changelnfo) override; 85 private: 86 int fileId_; 87 }; 88 89 struct WriteData { 90 std::string requestUri; 91 std::string destUri; 92 bool isSource; 93 CompatibleMode compatibleMode; 94 }; 95 96 struct MediaAssetManagerContext { 97 int fileId = -1; // default value of request file id 98 std::string photoUri; 99 std::string photoId; 100 std::string displayName; 101 std::string photoPath; 102 std::string requestId; 103 std::string destUri; 104 DeliveryMode deliveryMode; 105 SourceMode sourceMode; 106 ReturnDataType returnDataType; 107 PhotoSubType subType; 108 bool hasReadPermission; 109 bool needsExtraInfo = true; 110 bool hasProcessPhoto; 111 int64_t dataHandler; 112 AssetHandler *assetHandler = nullptr; 113 CompatibleMode compatibleMode = CompatibleMode::ORIGINAL_FORMAT_MODE; 114 MultiStagesCapturePhotoStatus photoQuality = MultiStagesCapturePhotoStatus::HIGH_QUALITY_STATUS; 115 }; 116 117 class MediaAssetManagerImpl { 118 public: 119 static bool ParseRequestMediaArgs(int64_t photoAssetId, 120 RequestOptions &requestOptions, std::unique_ptr<MediaAssetManagerContext> &asyncContext); 121 static char* RequestImage(int64_t contextId, int64_t photoAssetId, 122 RequestOptions requestOptions, int64_t funcId, int32_t &errCode); 123 static char* RequestImageData(int64_t contextId, int64_t photoAssetId, 124 RequestOptions requestOptions, int64_t funcId, int32_t &errCode); 125 static char* RequestMovingPhoto(int64_t contextId, int64_t photoAssetId, 126 RequestOptions requestOptions, int64_t funcId, int32_t &errCode); 127 static char* RequestVideoFile(int64_t contextId, int64_t photoAssetId, 128 RequestOptions requestOptions, char* fileUri, int64_t funcId, int32_t &errCode); 129 static void CancelRequest(int64_t contextId, char* cRequestId, int32_t &errCode); 130 static int64_t LoadMovingPhoto(int64_t contextId, char* cImageFileUri, char* cVideoFileUri, int32_t &errCode); 131 static void OnHandleRequestImage(std::unique_ptr<MediaAssetManagerContext> &asyncContext); 132 static MultiStagesCapturePhotoStatus QueryPhotoStatus(int fileId, const std::string& photoUri, 133 std::string &photoId, bool hasReadPermission); 134 static void NotifyDataPreparedWithoutRegister(std::unique_ptr<MediaAssetManagerContext> &asyncContext); 135 static void RegisterTaskObserver(std::unique_ptr<MediaAssetManagerContext> &asyncContext); 136 static void ProcessImage(const int fileId, const int deliveryMode); 137 static void GetByteArrayObject(const std::string &requestUri, MediaObject &mediaObject, bool isSource); 138 static void GetImageSourceObject(const std::string &requestUri, MediaObject &mediaObject, bool isSource); 139 static void GetMovingPhotoObject(const std::string &requestUri, SourceMode sourceMode, MediaObject &mediaObject); 140 static void SendFile(MediaObject &mediaObject, WriteData &writeData, int srcFd, int destFd, off_t fileSize); 141 static int32_t GetFdFromSandBoxUri(const std::string &sandBoxUri); 142 static void WriteDataToDestPath(WriteData &writeData, MediaObject &mediaObject, std::string requestId); 143 static void NotifyMediaDataPrepared(AssetHandler *assetHandler); 144 static void NotifyOnProgress(int32_t type, int32_t progress, std::string requestId); 145 static void OnHandleRequestVideo(std::unique_ptr<MediaAssetManagerContext> &asyncContext); 146 static void OnHandleProgress(unique_ptr<MediaAssetManagerContext> &asyncContext); 147 static void CancelProcessImage(const std::string &photoId); 148 149 std::mutex sMediaAssetMutex_; 150 }; 151 } 152 } 153 #endif