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 #define MLOG_TAG "DeferredPhotoProcessingAdapter"
17 
18 #include "deferred_photo_proc_adapter.h"
19 
20 #ifdef ABILITY_CAMERA_SUPPORT
21 #include "camera_manager.h"
22 #endif
23 #include "ipc_skeleton.h"
24 #include "media_log.h"
25 #ifdef ABILITY_CAMERA_SUPPORT
26 #include "multistages_capture_deferred_photo_proc_session_callback.h"
27 #endif
28 using namespace std;
29 #ifdef ABILITY_CAMERA_SUPPORT
30 using namespace OHOS::CameraStandard;
31 #endif
32 
33 namespace OHOS {
34 namespace Media {
35 
DeferredPhotoProcessingAdapter()36 DeferredPhotoProcessingAdapter::DeferredPhotoProcessingAdapter()
37 {
38 #ifdef ABILITY_CAMERA_SUPPORT
39     const static int32_t INVALID_UID = -1;
40     const static int32_t BASE_USER_RANGE = 200000;
41 
42     int uid = IPCSkeleton::GetCallingUid();
43     if (uid <= INVALID_UID) {
44         MEDIA_ERR_LOG("DeferredPhotoProcessingAdapter invalid uid: %{public}d", uid);
45         return;
46     }
47     int32_t userId = uid / BASE_USER_RANGE;
48     deferredPhotoProcSession_ = CameraManager::CreateDeferredPhotoProcessingSession(userId,
49         make_shared<MultiStagesCaptureDeferredPhotoProcSessionCallback>());
50     if (deferredPhotoProcSession_ == nullptr) {
51         MEDIA_ERR_LOG("CreateDeferredPhotoProcessingSession err");
52     }
53 #endif
54     MEDIA_INFO_LOG("DeferredPhotoProcessingAdapter init succ");
55 }
56 
~DeferredPhotoProcessingAdapter()57 DeferredPhotoProcessingAdapter::~DeferredPhotoProcessingAdapter() {}
58 
BeginSynchronize()59 void DeferredPhotoProcessingAdapter::BeginSynchronize()
60 {
61     MEDIA_INFO_LOG("DeferredPhotoProcessingAdapter::BeginSynchronize");
62 #ifdef ABILITY_CAMERA_SUPPORT
63     if (deferredPhotoProcSession_ == nullptr) {
64         MEDIA_ERR_LOG("BeginSynchronize deferredPhotoProcSession_ is nullptr");
65         return;
66     }
67     deferredPhotoProcSession_->BeginSynchronize();
68 #endif
69 }
70 
EndSynchronize()71 void DeferredPhotoProcessingAdapter::EndSynchronize()
72 {
73     MEDIA_INFO_LOG("DeferredPhotoProcessingAdapter::EndSynchronize");
74 #ifdef ABILITY_CAMERA_SUPPORT
75     if (deferredPhotoProcSession_ == nullptr) {
76         MEDIA_ERR_LOG("EndSynchronize deferredPhotoProcSession_ is nullptr");
77         return;
78     }
79     deferredPhotoProcSession_->EndSynchronize();
80 #endif
81 }
82 
83 #ifdef ABILITY_CAMERA_SUPPORT
AddImage(const std::string & imageId,DpsMetadata & metadata,const bool isTrashed)84 void DeferredPhotoProcessingAdapter::AddImage(const std::string &imageId, DpsMetadata &metadata, const bool isTrashed)
85 {
86     MEDIA_INFO_LOG("enter photoid: %{public}s, isTrashed: %{public}d", imageId.c_str(), isTrashed);
87     if (deferredPhotoProcSession_ == nullptr) {
88         MEDIA_ERR_LOG("AddImage deferredPhotoProcSession_ is nullptr");
89         return;
90     }
91     deferredPhotoProcSession_->AddImage(imageId, metadata, isTrashed);
92 }
93 #endif
94 
RemoveImage(const std::string & imageId,bool isRestorable)95 void DeferredPhotoProcessingAdapter::RemoveImage(const std::string &imageId, bool isRestorable)
96 {
97     MEDIA_INFO_LOG("enter photoid: %{public}s, isRestorable: %{public}d", imageId.c_str(), isRestorable);
98 #ifdef ABILITY_CAMERA_SUPPORT
99     if (deferredPhotoProcSession_ == nullptr) {
100         MEDIA_ERR_LOG("RemoveImage deferredPhotoProcSession_ is nullptr");
101         return;
102     }
103     deferredPhotoProcSession_->RemoveImage(imageId, isRestorable);
104 #endif
105 }
106 
RestoreImage(const std::string & imageId)107 void DeferredPhotoProcessingAdapter::RestoreImage(const std::string &imageId)
108 {
109     MEDIA_INFO_LOG("enter photoid: %{public}s", imageId.c_str());
110 #ifdef ABILITY_CAMERA_SUPPORT
111     if (deferredPhotoProcSession_ == nullptr) {
112         MEDIA_ERR_LOG("RestoreImage deferredPhotoProcSession_ is nullptr");
113         return;
114     }
115     deferredPhotoProcSession_->RestoreImage(imageId);
116 #endif
117 }
118 
ProcessImage(const std::string & appName,const std::string & imageId)119 void DeferredPhotoProcessingAdapter::ProcessImage(const std::string &appName, const std::string &imageId)
120 {
121     MEDIA_INFO_LOG("enter appName: %{public}s, photoid: %{public}s", appName.c_str(), imageId.c_str());
122 #ifdef ABILITY_CAMERA_SUPPORT
123     if (deferredPhotoProcSession_ == nullptr) {
124         MEDIA_ERR_LOG("ProcessImage deferredPhotoProcSession_ is nullptr");
125         return;
126     }
127     deferredPhotoProcSession_->ProcessImage(appName, imageId);
128 #endif
129 }
130 
CancelProcessImage(const std::string & imageId)131 bool DeferredPhotoProcessingAdapter::CancelProcessImage(const std::string &imageId)
132 {
133     MEDIA_INFO_LOG("DeferredPhotoProcessingAdapter::CancelProcessImage photoid: %{public}s", imageId.c_str());
134 #ifdef ABILITY_CAMERA_SUPPORT
135     if (deferredPhotoProcSession_ == nullptr) {
136         MEDIA_ERR_LOG("CancelProcessImage deferredPhotoProcSession_ is nullptr");
137         return false;
138     }
139     return deferredPhotoProcSession_->CancelProcessImage(imageId);
140 #else
141     return false;
142 #endif
143 }
144 
145 } // namespace Media
146 } // namespace OHOS