1 /*
2 * Copyright (c) 2023-2025 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 #include "deferred_photo_processing_session.h"
17
18 #include "dp_utils.h"
19 #include "dps.h"
20 #include "dps_event_report.h"
21 #include "photo_command.h"
22 #include "sync_command.h"
23
24 namespace OHOS {
25 namespace CameraStandard {
26 namespace DeferredProcessing {
DeferredPhotoProcessingSession(const int32_t userId)27 DeferredPhotoProcessingSession::DeferredPhotoProcessingSession(const int32_t userId)
28 : userId_(userId)
29 {
30 DP_DEBUG_LOG("entered. userId: %{public}d", userId_);
31 }
32
~DeferredPhotoProcessingSession()33 DeferredPhotoProcessingSession::~DeferredPhotoProcessingSession()
34 {
35 DP_DEBUG_LOG("entered.");
36 imageIds_.clear();
37 }
38
BeginSynchronize()39 int32_t DeferredPhotoProcessingSession::BeginSynchronize()
40 {
41 DP_INFO_LOG("DPS_PHOTO: BeginSynchronize.");
42 std::lock_guard<std::mutex> lock(mutex_);
43 inSync_.store(true);
44 const std::string imageId = "default";
45 ReportEvent(imageId, static_cast<int32_t>(IDeferredPhotoProcessingSessionIpcCode::COMMAND_BEGIN_SYNCHRONIZE));
46 return DP_OK;
47 }
48
EndSynchronize()49 int32_t DeferredPhotoProcessingSession::EndSynchronize()
50 {
51 DP_INFO_LOG("DPS_PHOTO: EndSynchronize photo job num: %{public}d", static_cast<int32_t>(imageIds_.size()));
52 std::lock_guard<std::mutex> lock(mutex_);
53 if (inSync_.load()) {
54 auto ret = DPS_SendCommand<PhotoSyncCommand>(userId_, imageIds_);
55 inSync_.store(false);
56 imageIds_.clear();
57 DP_CHECK_ERROR_RETURN_RET_LOG(ret != DP_OK, ret, "photo synchronize failed, ret: %{public}d", ret);
58
59 const std::string imageId = "default";
60 ReportEvent(imageId, static_cast<int32_t>(IDeferredPhotoProcessingSessionIpcCode::COMMAND_END_SYNCHRONIZE));
61 }
62 return DP_OK;
63 }
64
AddImage(const std::string & imageId,const DpsMetadata & metadata,bool discardable)65 int32_t DeferredPhotoProcessingSession::AddImage(const std::string& imageId, const DpsMetadata& metadata,
66 bool discardable)
67 {
68 if (inSync_.load()) {
69 std::lock_guard<std::mutex> lock(mutex_);
70 DP_DEBUG_LOG("AddImage error, inSync!");
71 auto info = std::make_shared<PhotoInfo>(discardable, metadata);
72 imageIds_.emplace(imageId, info);
73 } else {
74 auto ret = DPS_SendCommand<AddPhotoCommand>(userId_, imageId, metadata, discardable);
75 DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK,
76 "DPS_PHOTO: add imageId: %{public}s failed. ret: %{public}d", imageId.c_str(), ret);
77 }
78
79 ReportEvent(imageId, static_cast<int32_t>(IDeferredPhotoProcessingSessionIpcCode::COMMAND_ADD_IMAGE));
80 return DP_OK;
81 }
82
RemoveImage(const std::string & imageId,bool restorable)83 int32_t DeferredPhotoProcessingSession::RemoveImage(const std::string& imageId, bool restorable)
84 {
85 if (inSync_.load()) {
86 DP_INFO_LOG("RemoveImage error, inSync!");
87 } else {
88 auto ret = DPS_SendCommand<RemovePhotoCommand>(userId_, imageId, restorable);
89 DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK,
90 "DPS_PHOTO: remove imageId: %{public}s failed. ret: %{public}d", imageId.c_str(), ret);
91 }
92
93 ReportEvent(imageId, static_cast<int32_t>(IDeferredPhotoProcessingSessionIpcCode::COMMAND_REMOVE_IMAGE));
94 return DP_OK;
95 }
96
RestoreImage(const std::string & imageId)97 int32_t DeferredPhotoProcessingSession::RestoreImage(const std::string& imageId)
98 {
99 if (inSync_.load()) {
100 DP_INFO_LOG("RestoreImage error, inSync!");
101 } else {
102 auto ret = DPS_SendCommand<RestorePhotoCommand>(userId_, imageId);
103 DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK,
104 "DPS_PHOTO: restore imageId: %{public}s failed. ret: %{public}u", imageId.c_str(), ret);
105 }
106
107 ReportEvent(imageId, static_cast<int32_t>(IDeferredPhotoProcessingSessionIpcCode::COMMAND_RESTORE_IMAGE));
108 return DP_OK;
109 }
110
ProcessImage(const std::string & appName,const std::string & imageId)111 int32_t DeferredPhotoProcessingSession::ProcessImage(const std::string& appName, const std::string& imageId)
112 {
113 if (inSync_) {
114 DP_INFO_LOG("ProcessImage error, inSync!");
115 } else {
116 auto ret = DPS_SendCommand<ProcessPhotoCommand>(userId_, imageId, appName);
117 DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK,
118 "DPS_PHOTO: process imageId: %{public}s failed. ret: %{public}u", imageId.c_str(), ret);
119 }
120
121 ReportEvent(imageId, static_cast<int32_t>(IDeferredPhotoProcessingSessionIpcCode::COMMAND_PROCESS_IMAGE));
122 return 0;
123 }
124
CancelProcessImage(const std::string & imageId)125 int32_t DeferredPhotoProcessingSession::CancelProcessImage(const std::string& imageId)
126 {
127 if (inSync_) {
128 DP_INFO_LOG("CancelProcessImage error, inSync!");
129 } else {
130 auto ret = DPS_SendCommand<CancelProcessPhotoCommand>(userId_, imageId);
131 DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK,
132 "DPS_PHOTO: cance process imageId: %{public}s failed. ret: %{public}u", imageId.c_str(), ret);
133 }
134
135 ReportEvent(imageId, static_cast<int32_t>(IDeferredPhotoProcessingSessionIpcCode::COMMAND_CANCEL_PROCESS_IMAGE));
136 return 0;
137 }
138
ReportEvent(const std::string & imageId,int32_t event)139 void DeferredPhotoProcessingSession::ReportEvent(const std::string& imageId, int32_t event)
140 {
141 DPSEventInfo dpsEventInfo;
142 dpsEventInfo.operatorStage = static_cast<uint32_t>(event);
143 dpsEventInfo.imageId = imageId;
144 dpsEventInfo.userId = userId_;
145 uint64_t beginTime = GetTimestampMilli();
146 switch (static_cast<int32_t>(event)) {
147 case static_cast<int32_t>(IDeferredPhotoProcessingSessionIpcCode::COMMAND_BEGIN_SYNCHRONIZE): {
148 dpsEventInfo.synchronizeTimeBeginTime = beginTime;
149 DPSEventReport::GetInstance().ReportOperateImage(imageId, userId_, dpsEventInfo);
150 break;
151 }
152 case static_cast<int32_t>(IDeferredPhotoProcessingSessionIpcCode::COMMAND_END_SYNCHRONIZE): {
153 dpsEventInfo.synchronizeTimeEndTime = beginTime;
154 DPSEventReport::GetInstance().ReportOperateImage(imageId, userId_, dpsEventInfo);
155 break;
156 }
157 case static_cast<int32_t>(IDeferredPhotoProcessingSessionIpcCode::COMMAND_ADD_IMAGE): {
158 dpsEventInfo.dispatchTimeBeginTime = beginTime;
159 break;
160 }
161 case static_cast<int32_t>(IDeferredPhotoProcessingSessionIpcCode::COMMAND_REMOVE_IMAGE): {
162 dpsEventInfo.removeTimeBeginTime = beginTime;
163 break;
164 }
165 case static_cast<int32_t>(IDeferredPhotoProcessingSessionIpcCode::COMMAND_RESTORE_IMAGE): {
166 dpsEventInfo.restoreTimeBeginTime = beginTime;
167 break;
168 }
169 case static_cast<int32_t>(IDeferredPhotoProcessingSessionIpcCode::COMMAND_PROCESS_IMAGE): {
170 dpsEventInfo.processTimeBeginTime = beginTime;
171 break;
172 }
173 }
174
175 if (event == static_cast<int32_t>(IDeferredPhotoProcessingSessionIpcCode::COMMAND_BEGIN_SYNCHRONIZE)) {
176 return;
177 } else if (event == static_cast<int32_t>(IDeferredPhotoProcessingSessionIpcCode::COMMAND_END_SYNCHRONIZE)) {
178 DPSEventReport::GetInstance().ReportImageProcessResult(imageId, userId_);
179 } else {
180 DPSEventReport::GetInstance().UpdateEventInfo(dpsEventInfo);
181 }
182 }
183 } // namespace DeferredProcessing
184 } // namespace CameraStandard
185 } // namespace OHOS